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 "../../sigsession.h"
25 #include "../../signal.h"
27 #include "../../extdef.h"
29 #include <QMouseEvent>
30 #include <QTextStream>
34 #include <boost/foreach.hpp>
36 using namespace boost;
42 const int Viewport::MinorTickSubdivision = 4;
43 const int Viewport::ScaleUnits[3] = {1, 2, 5};
45 const QString Viewport::SIPrefixes[9] =
46 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
47 const int Viewport::FirstSIPrefixPower = -15;
49 Viewport::Viewport(View &parent) :
53 setMouseTracking(true);
54 setAutoFillBackground(false);
57 int Viewport::get_total_height() const
60 BOOST_FOREACH(const shared_ptr<Signal> s,
61 _view.session().get_signals()) {
63 height += View::SignalHeight;
69 void Viewport::initializeGL()
73 void Viewport::resizeGL(int width, int height)
75 setup_viewport(width, height);
78 void Viewport::paintEvent(QPaintEvent *event)
82 const vector< shared_ptr<Signal> > &sigs =
83 _view.session().get_signals();
85 // Prepare for OpenGL rendering
87 glMatrixMode(GL_MODELVIEW);
90 setup_viewport(width(), height());
92 qglClearColor(Qt::white);
93 glClear(GL_COLOR_BUFFER_BIT);
96 glEnable(GL_SCISSOR_TEST);
97 glScissor(0, 0, width(), height());
98 offset = View::RulerHeight - _view.v_offset();
99 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
103 const QRect signal_rect(0, offset,
104 width(), View::SignalHeight);
106 s->paint(*this, signal_rect, _view.scale(), _view.offset());
108 offset += View::SignalHeight;
111 glDisable(GL_SCISSOR_TEST);
113 // Prepare for QPainter rendering
114 glMatrixMode(GL_MODELVIEW);
117 QPainter painter(this);
118 painter.setRenderHint(QPainter::Antialiasing);
121 paint_ruler(painter);
126 void Viewport::mousePressEvent(QMouseEvent *event)
130 _mouse_down_point = event->pos();
131 _mouse_down_offset = _view.offset();
134 void Viewport::mouseMoveEvent(QMouseEvent *event)
138 if(event->buttons() & Qt::LeftButton)
140 _view.set_scale_offset(_view.scale(),
142 (_mouse_down_point - event->pos()).x() *
147 void Viewport::mouseReleaseEvent(QMouseEvent *event)
152 void Viewport::wheelEvent(QWheelEvent *event)
155 _view.zoom(event->delta() / 120, event->x());
158 void Viewport::setup_viewport(int width, int height)
160 glViewport(0, 0, (GLint)width, (GLint)height);
161 glMatrixMode(GL_PROJECTION);
163 glOrtho(0, width, height, 0, -1, 1);
164 glMatrixMode(GL_MODELVIEW);
167 void Viewport::paint_ruler(QPainter &p)
169 const double MinSpacing = 80;
171 const double min_period = _view.scale() * MinSpacing;
173 const int order = (int)floorf(log10f(min_period));
174 const double order_decimal = pow(10, order);
177 double tick_period = 0.0f;
181 tick_period = order_decimal * ScaleUnits[unit++];
182 } while(tick_period < min_period && unit < countof(ScaleUnits));
184 const int prefix = (order - FirstSIPrefixPower) / 3;
186 assert(prefix < countof(SIPrefixes));
188 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
189 Qt::AlignLeft | Qt::AlignTop, "8").height();
191 // Draw the tick marks
194 const double minor_tick_period = tick_period / MinorTickSubdivision;
195 const double first_major_division =
196 floor(_view.offset() / tick_period);
197 const double first_minor_division =
198 ceil(_view.offset() / minor_tick_period);
199 const double t0 = first_major_division * tick_period;
201 int division = (int)round(first_minor_division -
202 first_major_division * MinorTickSubdivision);
205 const double t = t0 + division * minor_tick_period;
206 const double x = (t - _view.offset()) / _view.scale();
211 if(division % MinorTickSubdivision == 0)
216 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
217 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
218 Qt::TextDontClip, s);
219 p.drawLine(x, text_height, x, View::RulerHeight);
225 (text_height + View::RulerHeight) / 2, x,