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>
31 #include "logicdata.h"
32 #include "logicdatasnapshot.h"
33 #include "sigsession.h"
34 #include "sigviewport.h"
36 using namespace boost;
39 const double SigView::MaxScale = 1e9;
40 const double SigView::MinScale = 1e-15;
42 const int SigView::LabelMarginWidth = 70;
43 const int SigView::RulerHeight = 30;
45 SigView::SigView(SigSession &session, QWidget *parent) :
46 QAbstractScrollArea(parent),
48 _viewport(new SigViewport(*this)),
54 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
55 this, SLOT(h_scroll_value_changed(int)));
56 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
57 this, SLOT(v_scroll_value_changed(int)));
58 connect(&_session, SIGNAL(data_updated()),
59 this, SLOT(data_updated()));
60 setViewport(_viewport);
63 double SigView::scale() const
68 double SigView::offset() const
73 int SigView::v_offset() const
78 void SigView::zoom(double steps)
80 zoom(steps, (width() - LabelMarginWidth) / 2);
83 void SigView::set_scale_offset(double scale, double offset)
91 void SigView::update_scroll()
95 const QSize areaSize = _viewport->size();
97 // Set the horizontal scroll bar
98 double length = 0, offset = 0;
99 const shared_ptr<SignalData> sig_data = _session.get_data();
101 length = _data_length /
102 (sig_data->get_samplerate() * _scale);
103 offset = _offset / _scale;
106 horizontalScrollBar()->setPageStep(areaSize.width());
107 horizontalScrollBar()->setRange(0,
108 max((int)(length - areaSize.width()), 0));
109 horizontalScrollBar()->setSliderPosition(offset);
111 // Set the vertical scrollbar
112 verticalScrollBar()->setPageStep(areaSize.height());
113 verticalScrollBar()->setRange(0,
114 _viewport->get_total_height() - areaSize.height());
117 void SigView::zoom(double steps, int offset)
119 const double cursor_offset = _offset + _scale * offset;
120 _scale *= pow(3.0/2.0, -steps);
121 _scale = max(min(_scale, MaxScale), MinScale);
122 _offset = cursor_offset - _scale * offset;
127 bool SigView::viewportEvent(QEvent *e)
131 case QEvent::MouseButtonPress:
132 case QEvent::MouseButtonRelease:
133 case QEvent::MouseButtonDblClick:
134 case QEvent::MouseMove:
139 return QAbstractScrollArea::viewportEvent(e);
143 void SigView::resizeEvent(QResizeEvent *e)
148 void SigView::h_scroll_value_changed(int value)
150 _offset = _scale * value;
154 void SigView::v_scroll_value_changed(int value)
160 void SigView::data_updated()
162 // Get the new data length
164 shared_ptr<LogicData> sig_data = _session.get_data();
166 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
167 sig_data->get_snapshots();
168 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
170 _data_length = max(_data_length,
171 s->get_sample_count());
174 // Update the scroll bars