]> sigrok.org Git - pulseview.git/blame_incremental - sigview.cpp
Added ruler numbering
[pulseview.git] / sigview.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "sigview.h"
22
23#include "sigsession.h"
24#include "signal.h"
25
26#include "extdef.h"
27
28#include <QMouseEvent>
29#include <QTextStream>
30
31#include <math.h>
32
33#include <boost/foreach.hpp>
34
35using namespace boost;
36using namespace std;
37
38const int SigView::SignalHeight = 50;
39const int SigView::LabelMarginWidth = 70;
40const int SigView::RulerHeight = 30;
41
42const int SigView::ScaleUnits[3] = {1, 2, 5};
43
44const QString SigView::SIPrefixes[9] =
45 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
46const int SigView::FirstSIPrefixPower = -15;
47
48SigView::SigView(SigSession &session, QWidget *parent) :
49 QGLWidget(parent),
50 _session(session),
51 _scale(1e-6),
52 _offset(0)
53{
54 connect(&_session, SIGNAL(dataUpdated()),
55 this, SLOT(dataUpdated()));
56
57 setMouseTracking(true);
58 setAutoFillBackground(false);
59}
60
61void SigView::initializeGL()
62{
63}
64
65void SigView::resizeGL(int width, int height)
66{
67 setupViewport(width, height);
68}
69
70void SigView::paintEvent(QPaintEvent *event)
71{
72 int offset;
73
74 const vector< shared_ptr<Signal> > &sigs =
75 _session.get_signals();
76
77 // Prepare for OpenGL rendering
78 makeCurrent();
79 glMatrixMode(GL_MODELVIEW);
80 glPushMatrix();
81
82 setupViewport(width(), height());
83
84 qglClearColor(Qt::white);
85 glClear(GL_COLOR_BUFFER_BIT);
86
87 // Plot the signal
88 offset = RulerHeight;
89 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
90 {
91 assert(s);
92
93 const QRect signal_rect(LabelMarginWidth, offset,
94 width() - LabelMarginWidth, SignalHeight);
95
96 s->paint(*this, signal_rect, _scale, _offset);
97
98 offset += SignalHeight;
99 }
100
101 // Prepare for QPainter rendering
102 glMatrixMode(GL_MODELVIEW);
103 glPopMatrix();
104
105 QPainter painter(this);
106 painter.setRenderHint(QPainter::Antialiasing);
107
108 // Paint the labels
109 offset = RulerHeight;
110 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
111 {
112 assert(s);
113
114 const QRect label_rect(0, offset,
115 LabelMarginWidth, SignalHeight);
116 s->paint_label(painter, label_rect);
117
118 offset += SignalHeight;
119 }
120
121 // Paint the ruler
122 paintRuler(painter);
123
124 painter.end();
125}
126
127void SigView::dataUpdated()
128{
129 update();
130}
131
132void SigView::mouseMoveEvent(QMouseEvent *event)
133{
134 assert(event);
135}
136
137void SigView::mousePressEvent(QMouseEvent *event)
138{
139 assert(event);
140}
141
142void SigView::mouseReleaseEvent(QMouseEvent *event)
143{
144 assert(event);
145
146 const double cursor_offset = _offset + _scale * (double)event->x();
147
148 switch(event->button())
149 {
150 case Qt::LeftButton:
151 _scale *= 2.0 / 3.0;
152 break;
153
154 case Qt::RightButton:
155 _scale *= 3.0 / 2.0;
156 break;
157 }
158
159 _offset = cursor_offset - _scale * (double)event->x();
160
161 update();
162}
163
164void SigView::setupViewport(int width, int height)
165{
166 glViewport(0, 0, (GLint)width, (GLint)height);
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
169 glOrtho(0, width, height, 0, -1, 1);
170 glMatrixMode(GL_MODELVIEW);
171}
172
173void SigView::paintRuler(QPainter &p)
174{
175 const double MinSpacing = 80;
176
177 const double min_period = _scale * MinSpacing;
178
179 const int order = (int)floorf(log10f(min_period));
180 const double order_decimal = pow(10, order);
181
182 int unit = 0;
183 double tick_period = 0.0f;
184
185 do
186 {
187 tick_period = order_decimal * ScaleUnits[unit++];
188 } while(tick_period < min_period && unit < countof(ScaleUnits));
189
190 const int prefix = (order - FirstSIPrefixPower) / 3;
191 assert(prefix >= 0);
192 assert(prefix < countof(SIPrefixes));
193
194 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
195 Qt::AlignLeft | Qt::AlignTop, "8").height();
196
197 // Draw the tick marks
198 p.setPen(Qt::black);
199
200 double t = ceil(_offset / tick_period) * tick_period;
201 double x = 0.0;
202 while((x = (t - _offset) / _scale + LabelMarginWidth) < width())
203 {
204 QString s;
205 QTextStream ts(&s);
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);
210 t += tick_period;
211 }
212}