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