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>
35 #include "../logicdata.h"
36 #include "../logicdatasnapshot.h"
37 #include "../sigsession.h"
39 using namespace boost;
45 const double View::MaxScale = 1e9;
46 const double View::MinScale = 1e-15;
48 const int View::LabelMarginWidth = 70;
49 const int View::RulerHeight = 30;
51 const int View::MaxScrollValue = INT_MAX / 2;
53 const int View::SignalHeight = 50;
55 View::View(SigSession &session, QWidget *parent) :
56 QAbstractScrollArea(parent),
58 _viewport(new Viewport(*this)),
59 _ruler(new Ruler(*this)),
60 _header(new Header(*this)),
66 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
67 this, SLOT(h_scroll_value_changed(int)));
68 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
69 this, SLOT(v_scroll_value_changed(int)));
70 connect(&_session, SIGNAL(data_updated()),
71 this, SLOT(data_updated()));
73 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
74 setViewport(_viewport);
77 SigSession& View::session()
82 double View::scale() const
87 double View::offset() const
92 int View::v_offset() const
97 void View::zoom(double steps)
99 zoom(steps, (width() - LabelMarginWidth) / 2);
102 void View::zoom(double steps, int offset)
104 const double cursor_offset = _offset + _scale * offset;
105 _scale *= pow(3.0/2.0, -steps);
106 _scale = max(min(_scale, MaxScale), MinScale);
107 _offset = cursor_offset - _scale * offset;
115 void View::set_scale_offset(double scale, double offset)
125 void View::get_scroll_layout(double &length, double &offset) const
127 const shared_ptr<SignalData> sig_data = _session.get_data();
131 length = _data_length / (sig_data->get_samplerate() * _scale);
132 offset = _offset / _scale;
135 void View::update_scroll()
139 const QSize areaSize = _viewport->size();
141 // Set the horizontal scroll bar
142 double length = 0, offset = 0;
143 get_scroll_layout(length, offset);
144 length = max(length - areaSize.width(), 0.0);
146 horizontalScrollBar()->setPageStep(areaSize.width());
148 if(length < MaxScrollValue) {
149 horizontalScrollBar()->setRange(0, length);
150 horizontalScrollBar()->setSliderPosition(offset);
152 horizontalScrollBar()->setRange(0, MaxScrollValue);
153 horizontalScrollBar()->setSliderPosition(
154 _offset * MaxScrollValue / (_scale * length));
157 // Set the vertical scrollbar
158 verticalScrollBar()->setPageStep(areaSize.height());
159 verticalScrollBar()->setRange(0,
160 _viewport->get_total_height() - areaSize.height());
163 bool View::viewportEvent(QEvent *e)
167 case QEvent::MouseButtonPress:
168 case QEvent::MouseButtonRelease:
169 case QEvent::MouseButtonDblClick:
170 case QEvent::MouseMove:
175 return QAbstractScrollArea::viewportEvent(e);
179 void View::resizeEvent(QResizeEvent *e)
181 _ruler->setGeometry(_viewport->x(), 0,
182 _viewport->width(), _viewport->y());
183 _header->setGeometry(0, _viewport->y(),
184 _viewport->x(), _viewport->height());
188 void View::h_scroll_value_changed(int value)
190 const int range = horizontalScrollBar()->maximum();
191 if(range < MaxScrollValue)
192 _offset = _scale * value;
194 double length = 0, offset;
195 get_scroll_layout(length, offset);
196 _offset = _scale * length * value / MaxScrollValue;
203 void View::v_scroll_value_changed(int value)
210 void View::data_updated()
212 // Get the new data length
214 shared_ptr<LogicData> sig_data = _session.get_data();
216 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
217 sig_data->get_snapshots();
218 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
220 _data_length = max(_data_length,
221 s->get_sample_count());
224 // Update the scroll bars