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
23 #include "sigsession.h"
28 #include <QMouseEvent>
29 #include <QTextStream>
33 #include <boost/foreach.hpp>
35 using namespace boost;
38 const double SigView::MaxScale = 1e9;
39 const double SigView::MinScale = 1e-15;
41 const int SigView::SignalHeight = 50;
42 const int SigView::LabelMarginWidth = 70;
43 const int SigView::RulerHeight = 30;
45 const int SigView::MinorTickSubdivision = 4;
46 const int SigView::ScaleUnits[3] = {1, 2, 5};
48 const QString SigView::SIPrefixes[9] =
49 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
50 const int SigView::FirstSIPrefixPower = -15;
52 SigView::SigView(SigSession &session, QWidget *parent) :
58 connect(&_session, SIGNAL(data_updated()),
59 this, SLOT(data_updated()));
61 setMouseTracking(true);
62 setAutoFillBackground(false);
65 void SigView::initializeGL()
69 void SigView::resizeGL(int width, int height)
71 setup_viewport(width, height);
74 void SigView::paintEvent(QPaintEvent *event)
78 const vector< shared_ptr<Signal> > &sigs =
79 _session.get_signals();
81 // Prepare for OpenGL rendering
83 glMatrixMode(GL_MODELVIEW);
86 setup_viewport(width(), height());
88 qglClearColor(Qt::white);
89 glClear(GL_COLOR_BUFFER_BIT);
92 glEnable(GL_SCISSOR_TEST);
93 glScissor(LabelMarginWidth, 0, width(), height());
95 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
99 const QRect signal_rect(LabelMarginWidth, offset,
100 width() - LabelMarginWidth, SignalHeight);
102 s->paint(*this, signal_rect, _scale, _offset);
104 offset += SignalHeight;
107 glDisable(GL_SCISSOR_TEST);
109 // Prepare for QPainter rendering
110 glMatrixMode(GL_MODELVIEW);
113 QPainter painter(this);
114 painter.setRenderHint(QPainter::Antialiasing);
117 offset = RulerHeight;
118 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
122 const QRect label_rect(0, offset,
123 LabelMarginWidth, SignalHeight);
124 s->paint_label(painter, label_rect);
126 offset += SignalHeight;
130 paint_ruler(painter);
135 void SigView::data_updated()
140 void SigView::mousePressEvent(QMouseEvent *event)
144 _mouse_down_point = event->pos();
145 _mouse_down_offset = _offset;
148 void SigView::mouseMoveEvent(QMouseEvent *event)
152 if(event->buttons() & Qt::LeftButton)
154 _offset = _mouse_down_offset + (_mouse_down_point - event->pos()).x() * _scale;
159 void SigView::mouseReleaseEvent(QMouseEvent *event)
164 void SigView::wheelEvent(QWheelEvent *event)
168 const double x = event->x() - LabelMarginWidth;
169 const double cursor_offset = _offset + _scale * x;
170 _scale *= powf(3.0/2.0, -event->delta() / 120);
171 _scale = max(min(_scale, MaxScale), MinScale);
172 _offset = cursor_offset - _scale * x;
176 void SigView::setup_viewport(int width, int height)
178 glViewport(0, 0, (GLint)width, (GLint)height);
179 glMatrixMode(GL_PROJECTION);
181 glOrtho(0, width, height, 0, -1, 1);
182 glMatrixMode(GL_MODELVIEW);
185 void SigView::paint_ruler(QPainter &p)
187 const double MinSpacing = 80;
189 const double min_period = _scale * MinSpacing;
191 const int order = (int)floorf(log10f(min_period));
192 const double order_decimal = pow(10, order);
195 double tick_period = 0.0f;
199 tick_period = order_decimal * ScaleUnits[unit++];
200 } while(tick_period < min_period && unit < countof(ScaleUnits));
202 const int prefix = (order - FirstSIPrefixPower) / 3;
204 assert(prefix < countof(SIPrefixes));
206 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
207 Qt::AlignLeft | Qt::AlignTop, "8").height();
209 // Draw the tick marks
212 const double minor_tick_period = tick_period / MinorTickSubdivision;
213 const double first_major_division = floor(_offset / tick_period);
214 const double first_minor_division = ceil(_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 - _offset) / _scale + LabelMarginWidth;
227 if(division % MinorTickSubdivision == 0)
232 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
233 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
234 Qt::TextDontClip, s);
235 p.drawLine(x, text_height, x, RulerHeight);
240 p.drawLine(x, (text_height + RulerHeight) / 2, x, RulerHeight);