]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
Moved view classes into the pv::view:: namespace
[pulseview.git] / pv / view / view.cpp
1 /*
2  * This file is part of the sigrok 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 <math.h>
23
24 #include <boost/foreach.hpp>
25
26 #include <QEvent>
27 #include <QScrollBar>
28
29 #include "view.h"
30 #include "viewport.h"
31
32 #include "../../logicdata.h"
33 #include "../../logicdatasnapshot.h"
34 #include "../../sigsession.h"
35
36 using namespace boost;
37 using namespace std;
38
39 namespace pv {
40 namespace view {
41
42 const double View::MaxScale = 1e9;
43 const double View::MinScale = 1e-15;
44
45 const int View::LabelMarginWidth = 70;
46 const int View::RulerHeight = 30;
47
48 View::View(SigSession &session, QWidget *parent) :
49         QAbstractScrollArea(parent),
50         _session(session),
51         _viewport(new Viewport(*this)),
52         _data_length(0),
53         _scale(1e-6),
54         _offset(0),
55         _v_offset(0)
56 {
57         connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
58                 this, SLOT(h_scroll_value_changed(int)));
59         connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
60                 this, SLOT(v_scroll_value_changed(int)));
61         connect(&_session, SIGNAL(data_updated()),
62                 this, SLOT(data_updated()));
63         setViewport(_viewport);
64 }
65
66 double View::scale() const
67 {
68         return _scale;
69 }
70
71 double View::offset() const
72 {
73         return _offset;
74 }
75
76 int View::v_offset() const
77 {
78         return _v_offset;
79 }
80
81 void View::zoom(double steps)
82 {
83         zoom(steps, (width() - LabelMarginWidth) / 2);
84 }
85
86 void View::set_scale_offset(double scale, double offset)
87 {
88         _scale = scale;
89         _offset = offset;
90         update_scroll();
91         _viewport->update();
92 }
93
94 void View::update_scroll()
95 {
96         assert(_viewport);
97
98         const QSize areaSize = _viewport->size();
99
100         // Set the horizontal scroll bar
101         double length = 0, offset = 0;
102         const shared_ptr<SignalData> sig_data = _session.get_data();
103         if(sig_data) {
104                 length = _data_length /
105                         (sig_data->get_samplerate() * _scale);
106                 offset = _offset / _scale;
107         }
108
109         horizontalScrollBar()->setPageStep(areaSize.width());
110         horizontalScrollBar()->setRange(0,
111                 max((int)(length - areaSize.width()), 0));
112         horizontalScrollBar()->setSliderPosition(offset);
113
114         // Set the vertical scrollbar
115         verticalScrollBar()->setPageStep(areaSize.height());
116         verticalScrollBar()->setRange(0,
117                 _viewport->get_total_height() - areaSize.height());
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         _viewport->update();
127         update_scroll();
128 }
129
130 bool View::viewportEvent(QEvent *e)
131 {
132         switch(e->type()) {
133         case QEvent::Paint:
134         case QEvent::MouseButtonPress:
135         case QEvent::MouseButtonRelease:
136         case QEvent::MouseButtonDblClick:
137         case QEvent::MouseMove:
138         case QEvent::Wheel:
139                 return false;
140
141         default:
142                 return QAbstractScrollArea::viewportEvent(e);
143         }
144 }
145
146 void View::resizeEvent(QResizeEvent *e)
147 {
148         update_scroll();
149 }
150
151 void View::h_scroll_value_changed(int value)
152 {
153         _offset = _scale * value;
154         _viewport->update();
155 }
156
157 void View::v_scroll_value_changed(int value)
158 {
159         _v_offset = value;
160         _viewport->update();
161 }
162
163 void View::data_updated()
164 {
165         // Get the new data length
166         _data_length = 0;
167         shared_ptr<LogicData> sig_data = _session.get_data();
168         if(sig_data) {
169                 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
170                         sig_data->get_snapshots();
171                 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
172                         if(s)
173                                 _data_length = max(_data_length,
174                                         s->get_sample_count());
175         }
176
177         // Update the scroll bars
178         update_scroll();
179
180         // Repaint the view
181         _viewport->update();
182 }
183
184 } // namespace view
185 } // namespace pv