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::SignalHeight = 50;
44 const int Viewport::MinorTickSubdivision = 4;
45 const int Viewport::ScaleUnits[3] = {1, 2, 5};
47 const QString Viewport::SIPrefixes[9] =
48 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
49 const int Viewport::FirstSIPrefixPower = -15;
51 Viewport::Viewport(View &parent) :
55 setMouseTracking(true);
56 setAutoFillBackground(false);
59 int Viewport::get_total_height() const
62 BOOST_FOREACH(const shared_ptr<Signal> s,
63 _view.session().get_signals()) {
65 height += SignalHeight;
71 void Viewport::initializeGL()
75 void Viewport::resizeGL(int width, int height)
77 setup_viewport(width, height);
80 void Viewport::paintEvent(QPaintEvent *event)
84 const vector< shared_ptr<Signal> > &sigs =
85 _view.session().get_signals();
87 // Prepare for OpenGL rendering
89 glMatrixMode(GL_MODELVIEW);
92 setup_viewport(width(), height());
94 qglClearColor(Qt::white);
95 glClear(GL_COLOR_BUFFER_BIT);
98 glEnable(GL_SCISSOR_TEST);
99 glScissor(View::LabelMarginWidth, 0, width(), height());
100 offset = View::RulerHeight - _view.v_offset();
101 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
105 const QRect signal_rect(View::LabelMarginWidth, offset,
106 width() - View::LabelMarginWidth, SignalHeight);
108 s->paint(*this, signal_rect, _view.scale(), _view.offset());
110 offset += SignalHeight;
113 glDisable(GL_SCISSOR_TEST);
115 // Prepare for QPainter rendering
116 glMatrixMode(GL_MODELVIEW);
119 QPainter painter(this);
120 painter.setRenderHint(QPainter::Antialiasing);
123 offset = View::RulerHeight - _view.v_offset();
124 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
128 const QRect label_rect(0, offset,
129 View::LabelMarginWidth, SignalHeight);
130 s->paint_label(painter, label_rect);
132 offset += SignalHeight;
136 paint_ruler(painter);
141 void Viewport::mousePressEvent(QMouseEvent *event)
145 _mouse_down_point = event->pos();
146 _mouse_down_offset = _view.offset();
149 void Viewport::mouseMoveEvent(QMouseEvent *event)
153 if(event->buttons() & Qt::LeftButton)
155 _view.set_scale_offset(_view.scale(),
157 (_mouse_down_point - event->pos()).x() *
162 void Viewport::mouseReleaseEvent(QMouseEvent *event)
167 void Viewport::wheelEvent(QWheelEvent *event)
170 _view.zoom(event->delta() / 120, event->x() -
171 View::LabelMarginWidth);
174 void Viewport::setup_viewport(int width, int height)
176 glViewport(0, 0, (GLint)width, (GLint)height);
177 glMatrixMode(GL_PROJECTION);
179 glOrtho(0, width, height, 0, -1, 1);
180 glMatrixMode(GL_MODELVIEW);
183 void Viewport::paint_ruler(QPainter &p)
185 const double MinSpacing = 80;
187 const double min_period = _view.scale() * MinSpacing;
189 const int order = (int)floorf(log10f(min_period));
190 const double order_decimal = pow(10, order);
193 double tick_period = 0.0f;
197 tick_period = order_decimal * ScaleUnits[unit++];
198 } while(tick_period < min_period && unit < countof(ScaleUnits));
200 const int prefix = (order - FirstSIPrefixPower) / 3;
202 assert(prefix < countof(SIPrefixes));
204 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
205 Qt::AlignLeft | Qt::AlignTop, "8").height();
207 // Draw the tick marks
210 const double minor_tick_period = tick_period / MinorTickSubdivision;
211 const double first_major_division =
212 floor(_view.offset() / tick_period);
213 const double first_minor_division =
214 ceil(_view.offset() / minor_tick_period);
215 const double t0 = first_major_division * tick_period;
217 int division = (int)round(first_minor_division -
218 first_major_division * MinorTickSubdivision);
221 const double t = t0 + division * minor_tick_period;
222 const double x = (t - _view.offset()) / _view.scale() +
223 View::LabelMarginWidth;
228 if(division % MinorTickSubdivision == 0)
233 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
234 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
235 Qt::TextDontClip, s);
236 p.drawLine(x, text_height, x, View::RulerHeight);
242 (text_height + View::RulerHeight) / 2, x,