2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <boost/foreach.hpp>
28 #include <QMouseEvent>
37 #include "../logicdata.h"
38 #include "../logicdatasnapshot.h"
39 #include "../sigsession.h"
41 using namespace boost;
47 const double View::MaxScale = 1e9;
48 const double View::MinScale = 1e-15;
50 const int View::LabelMarginWidth = 70;
51 const int View::RulerHeight = 30;
53 const int View::MaxScrollValue = INT_MAX / 2;
55 const int View::SignalHeight = 50;
57 const QColor View::CursorAreaColour(220, 231, 243);
59 const QSizeF View::LabelPadding(4, 0);
61 View::View(SigSession &session, QWidget *parent) :
62 QAbstractScrollArea(parent),
64 _viewport(new Viewport(*this)),
65 _ruler(new Ruler(*this)),
66 _header(new Header(*this)),
72 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
76 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
77 this, SLOT(h_scroll_value_changed(int)));
78 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
79 this, SLOT(v_scroll_value_changed(int)));
80 connect(&_session, SIGNAL(data_updated()),
81 this, SLOT(data_updated()));
83 connect(&_cursors.first, SIGNAL(time_changed()),
84 this, SLOT(marker_time_changed()));
85 connect(&_cursors.second, SIGNAL(time_changed()),
86 this, SLOT(marker_time_changed()));
88 connect(_header, SIGNAL(signals_moved()),
89 this, SLOT(signals_moved()));
91 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
92 setViewport(_viewport);
94 _viewport->installEventFilter(this);
95 _ruler->installEventFilter(this);
96 _header->installEventFilter(this);
99 SigSession& View::session()
104 double View::scale() const
109 double View::offset() const
114 int View::v_offset() const
119 void View::zoom(double steps)
121 zoom(steps, (width() - LabelMarginWidth) / 2);
124 void View::zoom(double steps, int offset)
126 const double cursor_offset = _offset + _scale * offset;
127 _scale *= pow(3.0/2.0, -steps);
128 _scale = max(min(_scale, MaxScale), MinScale);
129 _offset = cursor_offset - _scale * offset;
137 void View::set_scale_offset(double scale, double offset)
147 bool View::cursors_shown() const
149 return _show_cursors;
152 void View::show_cursors(bool show)
154 _show_cursors = show;
159 std::pair<Cursor, Cursor>& View::cursors()
164 const QPoint& View::hover_point() const
169 void View::get_scroll_layout(double &length, double &offset) const
171 const shared_ptr<SignalData> sig_data = _session.get_data();
175 length = _data_length / (sig_data->get_samplerate() * _scale);
176 offset = _offset / _scale;
179 void View::update_scroll()
183 const QSize areaSize = _viewport->size();
185 // Set the horizontal scroll bar
186 double length = 0, offset = 0;
187 get_scroll_layout(length, offset);
188 length = max(length - areaSize.width(), 0.0);
190 horizontalScrollBar()->setPageStep(areaSize.width());
192 if(length < MaxScrollValue) {
193 horizontalScrollBar()->setRange(0, length);
194 horizontalScrollBar()->setSliderPosition(offset);
196 horizontalScrollBar()->setRange(0, MaxScrollValue);
197 horizontalScrollBar()->setSliderPosition(
198 _offset * MaxScrollValue / (_scale * length));
201 // Set the vertical scrollbar
202 verticalScrollBar()->setPageStep(areaSize.height());
203 verticalScrollBar()->setRange(0,
204 _viewport->get_total_height() - areaSize.height());
207 void View::reset_signal_layout()
210 vector< shared_ptr<Signal> > &sigs = _session.get_signals();
211 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
212 s->set_v_offset(offset);
213 offset += SignalHeight;
217 bool View::eventFilter(QObject *object, QEvent *event)
219 const QEvent::Type type = event->type();
220 if(type == QEvent::MouseMove) {
222 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
223 if(object == _viewport)
224 _hover_point = mouse_event->pos();
225 else if(object == _ruler)
226 _hover_point = QPoint(mouse_event->x(), 0);
227 else if(object == _header)
228 _hover_point = QPoint(0, mouse_event->y());
230 _hover_point = QPoint(-1, -1);
232 hover_point_changed();
234 } else if(type == QEvent::Leave) {
235 _hover_point = QPoint(-1, -1);
236 hover_point_changed();
239 return QObject::eventFilter(object, event);
242 bool View::viewportEvent(QEvent *e)
246 case QEvent::MouseButtonPress:
247 case QEvent::MouseButtonRelease:
248 case QEvent::MouseButtonDblClick:
249 case QEvent::MouseMove:
254 return QAbstractScrollArea::viewportEvent(e);
258 void View::resizeEvent(QResizeEvent *e)
260 _ruler->setGeometry(_viewport->x(), 0,
261 _viewport->width(), _viewport->y());
262 _header->setGeometry(0, _viewport->y(),
263 _viewport->x(), _viewport->height());
267 void View::h_scroll_value_changed(int value)
269 const int range = horizontalScrollBar()->maximum();
270 if(range < MaxScrollValue)
271 _offset = _scale * value;
273 double length = 0, offset;
274 get_scroll_layout(length, offset);
275 _offset = _scale * length * value / MaxScrollValue;
282 void View::v_scroll_value_changed(int value)
289 void View::data_updated()
291 // Get the new data length
293 shared_ptr<LogicData> sig_data = _session.get_data();
295 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
296 sig_data->get_snapshots();
297 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
299 _data_length = max(_data_length,
300 s->get_sample_count());
303 // Update the scroll bars
309 /// @todo: Call this only once when the signals are first created.
310 reset_signal_layout();
313 void View::marker_time_changed()
319 void View::signals_moved()