]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
Moved LabelPadding into View
[pulseview.git] / pv / view / view.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <limits.h>
23 #include <math.h>
24
25 #include <boost/foreach.hpp>
26
27 #include <QEvent>
28 #include <QMouseEvent>
29 #include <QScrollBar>
30
31 #include "header.h"
32 #include "ruler.h"
33 #include "view.h"
34 #include "viewport.h"
35
36 #include "../logicdata.h"
37 #include "../logicdatasnapshot.h"
38 #include "../sigsession.h"
39
40 using namespace boost;
41 using namespace std;
42
43 namespace pv {
44 namespace view {
45
46 const double View::MaxScale = 1e9;
47 const double View::MinScale = 1e-15;
48
49 const int View::LabelMarginWidth = 70;
50 const int View::RulerHeight = 30;
51
52 const int View::MaxScrollValue = INT_MAX / 2;
53
54 const int View::SignalHeight = 50;
55
56 const QColor View::CursorAreaColour(220, 231, 243);
57
58 const QSizeF View::LabelPadding(4, 0);
59
60 View::View(SigSession &session, QWidget *parent) :
61         QAbstractScrollArea(parent),
62         _session(session),
63         _viewport(new Viewport(*this)),
64         _ruler(new Ruler(*this)),
65         _header(new Header(*this)),
66         _data_length(0),
67         _scale(1e-6),
68         _offset(0),
69         _v_offset(0),
70         _show_cursors(false),
71         _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
72                 Cursor(*this, 1.0))),
73         _hover_point(-1, -1)
74 {
75         connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
76                 this, SLOT(h_scroll_value_changed(int)));
77         connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
78                 this, SLOT(v_scroll_value_changed(int)));
79         connect(&_session, SIGNAL(data_updated()),
80                 this, SLOT(data_updated()));
81
82         connect(&_cursors.first, SIGNAL(time_changed()),
83                 this, SLOT(marker_time_changed()));
84         connect(&_cursors.second, SIGNAL(time_changed()),
85                 this, SLOT(marker_time_changed()));
86
87         setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
88         setViewport(_viewport);
89
90         _viewport->installEventFilter(this);
91         _ruler->installEventFilter(this);
92         _header->installEventFilter(this);
93 }
94
95 SigSession& View::session()
96 {
97         return _session;
98 }
99
100 double View::scale() const
101 {
102         return _scale;
103 }
104
105 double View::offset() const
106 {
107         return _offset;
108 }
109
110 int View::v_offset() const
111 {
112         return _v_offset;
113 }
114
115 void View::zoom(double steps)
116 {
117         zoom(steps, (width() - LabelMarginWidth) / 2);
118 }
119
120 void View::zoom(double steps, int offset)
121 {
122         const double cursor_offset = _offset + _scale * offset;
123         _scale *= pow(3.0/2.0, -steps);
124         _scale = max(min(_scale, MaxScale), MinScale);
125         _offset = cursor_offset - _scale * offset;
126
127         _ruler->update();
128         _viewport->update();
129         update_scroll();
130 }
131
132
133 void View::set_scale_offset(double scale, double offset)
134 {
135         _scale = scale;
136         _offset = offset;
137
138         update_scroll();
139         _ruler->update();
140         _viewport->update();
141 }
142
143 bool View::cursors_shown() const
144 {
145         return _show_cursors;
146 }
147
148 void View::show_cursors(bool show)
149 {
150         _show_cursors = show;
151         _ruler->update();
152         _viewport->update();
153 }
154
155 std::pair<Cursor, Cursor>& View::cursors()
156 {
157         return _cursors;
158 }
159
160 const QPoint& View::hover_point() const
161 {
162         return _hover_point;
163 }
164
165 void View::get_scroll_layout(double &length, double &offset) const
166 {
167         const shared_ptr<SignalData> sig_data = _session.get_data();
168         if(!sig_data)
169                 return;
170
171         length = _data_length / (sig_data->get_samplerate() * _scale);
172         offset = _offset / _scale;
173 }
174
175 void View::update_scroll()
176 {
177         assert(_viewport);
178
179         const QSize areaSize = _viewport->size();
180
181         // Set the horizontal scroll bar
182         double length = 0, offset = 0;
183         get_scroll_layout(length, offset);
184         length = max(length - areaSize.width(), 0.0);
185
186         horizontalScrollBar()->setPageStep(areaSize.width());
187
188         if(length < MaxScrollValue) {
189                 horizontalScrollBar()->setRange(0, length);
190                 horizontalScrollBar()->setSliderPosition(offset);
191         } else {
192                 horizontalScrollBar()->setRange(0, MaxScrollValue);
193                 horizontalScrollBar()->setSliderPosition(
194                         _offset * MaxScrollValue / (_scale * length));
195         }
196
197         // Set the vertical scrollbar
198         verticalScrollBar()->setPageStep(areaSize.height());
199         verticalScrollBar()->setRange(0,
200                 _viewport->get_total_height() - areaSize.height());
201 }
202
203 bool View::eventFilter(QObject *object, QEvent *event)
204 {
205         const QEvent::Type type = event->type();
206         if(type == QEvent::MouseMove) {
207
208                 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
209                 if(object == _viewport)
210                         _hover_point = mouse_event->pos();
211                 else if(object == _ruler)
212                         _hover_point = QPoint(mouse_event->x(), 0);
213                 else if(object == _header)
214                         _hover_point = QPoint(0, mouse_event->y());
215                 else
216                         _hover_point = QPoint(-1, -1);
217
218                 hover_point_changed();
219
220         } else if(type == QEvent::Leave) {
221                 _hover_point = QPoint(-1, -1);
222                 hover_point_changed();
223         }
224
225         return QObject::eventFilter(object, event);
226 }
227
228 bool View::viewportEvent(QEvent *e)
229 {
230         switch(e->type()) {
231         case QEvent::Paint:
232         case QEvent::MouseButtonPress:
233         case QEvent::MouseButtonRelease:
234         case QEvent::MouseButtonDblClick:
235         case QEvent::MouseMove:
236         case QEvent::Wheel:
237                 return false;
238
239         default:
240                 return QAbstractScrollArea::viewportEvent(e);
241         }
242 }
243
244 void View::resizeEvent(QResizeEvent *e)
245 {
246         _ruler->setGeometry(_viewport->x(), 0,
247                 _viewport->width(), _viewport->y());
248         _header->setGeometry(0, _viewport->y(),
249                 _viewport->x(), _viewport->height());
250         update_scroll();
251 }
252
253 void View::h_scroll_value_changed(int value)
254 {
255         const int range = horizontalScrollBar()->maximum();
256         if(range < MaxScrollValue)
257                 _offset = _scale * value;
258         else {
259                 double length = 0, offset;
260                 get_scroll_layout(length, offset);
261                 _offset = _scale * length * value / MaxScrollValue;
262         }
263
264         _ruler->update();
265         _viewport->update();
266 }
267
268 void View::v_scroll_value_changed(int value)
269 {
270         _v_offset = value;
271         _header->update();
272         _viewport->update();
273 }
274
275 void View::data_updated()
276 {
277         // Get the new data length
278         _data_length = 0;
279         shared_ptr<LogicData> sig_data = _session.get_data();
280         if(sig_data) {
281                 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
282                         sig_data->get_snapshots();
283                 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
284                         if(s)
285                                 _data_length = max(_data_length,
286                                         s->get_sample_count());
287         }
288
289         // Update the scroll bars
290         update_scroll();
291
292         // Repaint the view
293         _viewport->update();
294 }
295
296 void View::marker_time_changed()
297 {
298         _ruler->update();
299         _viewport->update();
300 }
301
302 } // namespace view
303 } // namespace pv