2 * This file is part of the sigrok 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
24 #include <boost/foreach.hpp>
34 #include "../../logicdata.h"
35 #include "../../logicdatasnapshot.h"
36 #include "../../sigsession.h"
38 using namespace boost;
44 const double View::MaxScale = 1e9;
45 const double View::MinScale = 1e-15;
47 const int View::LabelMarginWidth = 70;
48 const int View::RulerHeight = 30;
50 const int View::SignalHeight = 50;
52 View::View(SigSession &session, QWidget *parent) :
53 QAbstractScrollArea(parent),
55 _viewport(new Viewport(*this)),
56 _ruler(new Ruler(*this)),
57 _header(new Header(*this)),
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()));
70 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
71 setViewport(_viewport);
74 SigSession& View::session()
79 double View::scale() const
84 double View::offset() const
89 int View::v_offset() const
94 void View::zoom(double steps)
96 zoom(steps, (width() - LabelMarginWidth) / 2);
99 void View::set_scale_offset(double scale, double offset)
109 void View::update_scroll()
113 const QSize areaSize = _viewport->size();
115 // Set the horizontal scroll bar
116 double length = 0, offset = 0;
117 const shared_ptr<SignalData> sig_data = _session.get_data();
119 length = _data_length /
120 (sig_data->get_samplerate() * _scale);
121 offset = _offset / _scale;
124 horizontalScrollBar()->setPageStep(areaSize.width());
125 horizontalScrollBar()->setRange(0,
126 max((int)(length - areaSize.width()), 0));
127 horizontalScrollBar()->setSliderPosition(offset);
129 // Set the vertical scrollbar
130 verticalScrollBar()->setPageStep(areaSize.height());
131 verticalScrollBar()->setRange(0,
132 _viewport->get_total_height() - areaSize.height());
135 void View::zoom(double steps, int offset)
137 const double cursor_offset = _offset + _scale * offset;
138 _scale *= pow(3.0/2.0, -steps);
139 _scale = max(min(_scale, MaxScale), MinScale);
140 _offset = cursor_offset - _scale * offset;
147 bool View::viewportEvent(QEvent *e)
151 case QEvent::MouseButtonPress:
152 case QEvent::MouseButtonRelease:
153 case QEvent::MouseButtonDblClick:
154 case QEvent::MouseMove:
159 return QAbstractScrollArea::viewportEvent(e);
163 void View::resizeEvent(QResizeEvent *e)
165 _ruler->setGeometry(_viewport->x(), 0,
166 _viewport->width(), _viewport->y());
167 _header->setGeometry(0, _viewport->y(),
168 _viewport->x(), _viewport->height());
172 void View::h_scroll_value_changed(int value)
174 _offset = _scale * value;
179 void View::v_scroll_value_changed(int value)
186 void View::data_updated()
188 // Get the new data length
190 shared_ptr<LogicData> sig_data = _session.get_data();
192 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
193 sig_data->get_snapshots();
194 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
196 _data_length = max(_data_length,
197 s->get_sample_count());
200 // Update the scroll bars