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 int SigView::SignalHeight = 50;
39 const int SigView::LabelMarginWidth = 70;
40 const int SigView::RulerHeight = 30;
42 const int SigView::ScaleUnits[3] = {1, 2, 5};
44 const QString SigView::SIPrefixes[9] =
45 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
46 const int SigView::FirstSIPrefixPower = -15;
48 SigView::SigView(SigSession &session, QWidget *parent) :
54 connect(&_session, SIGNAL(dataUpdated()),
55 this, SLOT(dataUpdated()));
57 setMouseTracking(true);
58 setAutoFillBackground(false);
61 void SigView::initializeGL()
65 void SigView::resizeGL(int width, int height)
67 setupViewport(width, height);
70 void SigView::paintEvent(QPaintEvent *event)
74 const vector< shared_ptr<Signal> > &sigs =
75 _session.get_signals();
77 // Prepare for OpenGL rendering
79 glMatrixMode(GL_MODELVIEW);
82 setupViewport(width(), height());
84 qglClearColor(Qt::white);
85 glClear(GL_COLOR_BUFFER_BIT);
89 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
93 const QRect signal_rect(LabelMarginWidth, offset,
94 width() - LabelMarginWidth, SignalHeight);
96 s->paint(*this, signal_rect, _scale, _offset);
98 offset += SignalHeight;
101 // Prepare for QPainter rendering
102 glMatrixMode(GL_MODELVIEW);
105 QPainter painter(this);
106 painter.setRenderHint(QPainter::Antialiasing);
109 offset = RulerHeight;
110 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
114 const QRect label_rect(0, offset,
115 LabelMarginWidth, SignalHeight);
116 s->paint_label(painter, label_rect);
118 offset += SignalHeight;
127 void SigView::dataUpdated()
132 void SigView::mouseMoveEvent(QMouseEvent *event)
137 void SigView::mousePressEvent(QMouseEvent *event)
142 void SigView::mouseReleaseEvent(QMouseEvent *event)
146 const double cursor_offset = _offset + _scale * (double)event->x();
148 switch(event->button())
154 case Qt::RightButton:
159 _offset = cursor_offset - _scale * (double)event->x();
164 void SigView::setupViewport(int width, int height)
166 glViewport(0, 0, (GLint)width, (GLint)height);
167 glMatrixMode(GL_PROJECTION);
169 glOrtho(0, width, height, 0, -1, 1);
170 glMatrixMode(GL_MODELVIEW);
173 void SigView::paintRuler(QPainter &p)
175 const double MinSpacing = 80;
177 const double min_period = _scale * MinSpacing;
179 const int order = (int)floorf(log10f(min_period));
180 const double order_decimal = pow(10, order);
183 double tick_period = 0.0f;
187 tick_period = order_decimal * ScaleUnits[unit++];
188 } while(tick_period < min_period && unit < countof(ScaleUnits));
190 const int prefix = (order - FirstSIPrefixPower) / 3;
192 assert(prefix < countof(SIPrefixes));
194 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
195 Qt::AlignLeft | Qt::AlignTop, "8").height();
197 // Draw the tick marks
200 double t = ceil(_offset / tick_period) * tick_period;
202 while((x = (t - _offset) / _scale + LabelMarginWidth) < width())
206 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
207 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
208 Qt::TextDontClip, s);
209 p.drawLine(x, text_height, x, RulerHeight);