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