]> sigrok.org Git - pulseview.git/blob - sigview.cpp
Clip signal painting with glScissor
[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(data_updated()),
56                 this, SLOT(data_updated()));
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         setup_viewport(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         setup_viewport(width(), height());
84
85         qglClearColor(Qt::white);
86         glClear(GL_COLOR_BUFFER_BIT);
87
88         // Plot the signal
89         glEnable(GL_SCISSOR_TEST);
90         glScissor(LabelMarginWidth, 0, width(), height());
91         offset = RulerHeight;
92         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
93         {
94                 assert(s);
95
96                 const QRect signal_rect(LabelMarginWidth, offset,
97                         width() - LabelMarginWidth, SignalHeight);
98
99                 s->paint(*this, signal_rect, _scale, _offset);
100
101                 offset += SignalHeight;
102         }
103
104         glDisable(GL_SCISSOR_TEST);
105
106         // Prepare for QPainter rendering
107         glMatrixMode(GL_MODELVIEW);
108         glPopMatrix();
109
110         QPainter painter(this);
111         painter.setRenderHint(QPainter::Antialiasing);
112
113         // Paint the labels
114         offset = RulerHeight;
115         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
116         {
117                 assert(s);
118
119                 const QRect label_rect(0, offset,
120                         LabelMarginWidth, SignalHeight);
121                 s->paint_label(painter, label_rect);
122
123                 offset += SignalHeight;
124         }
125
126         // Paint the ruler
127         paint_ruler(painter);
128
129         painter.end();
130 }
131
132 void SigView::data_updated()
133 {
134         update();
135 }
136
137 void SigView::mousePressEvent(QMouseEvent *event)
138 {
139         assert(event);
140
141         _mouse_down_point = event->pos();
142         _mouse_down_offset = _offset;
143 }
144
145 void SigView::mouseMoveEvent(QMouseEvent *event)
146 {
147         assert(event);
148
149         if(event->buttons() & Qt::LeftButton)
150         {
151                 _offset = _mouse_down_offset + (_mouse_down_point - event->pos()).x() * _scale;
152                 update();
153         }
154 }
155
156 void SigView::mouseReleaseEvent(QMouseEvent *event)
157 {
158         assert(event);
159 }
160
161 void SigView::wheelEvent(QWheelEvent *event)
162 {
163         assert(event);
164
165         const double x = event->x() - LabelMarginWidth;
166         const double cursor_offset = _offset + _scale * x;
167         _scale *= powf(3.0/2.0, -event->delta() / 120);
168         _offset = cursor_offset - _scale * x;
169         update();
170 }
171
172 void SigView::setup_viewport(int width, int height)
173 {
174         glViewport(0, 0, (GLint)width, (GLint)height);
175         glMatrixMode(GL_PROJECTION);
176         glLoadIdentity();
177         glOrtho(0, width, height, 0, -1, 1);
178         glMatrixMode(GL_MODELVIEW);
179 }
180
181 void SigView::paint_ruler(QPainter &p)
182 {
183         const double MinSpacing = 80;
184
185         const double min_period = _scale * MinSpacing;
186
187         const int order = (int)floorf(log10f(min_period));
188         const double order_decimal = pow(10, order);
189
190         int unit = 0;
191         double tick_period = 0.0f;
192
193         do
194         {
195                 tick_period = order_decimal * ScaleUnits[unit++];
196         } while(tick_period < min_period && unit < countof(ScaleUnits));
197
198         const int prefix = (order - FirstSIPrefixPower) / 3;
199         assert(prefix >= 0);
200         assert(prefix < countof(SIPrefixes));
201
202         const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
203                 Qt::AlignLeft | Qt::AlignTop, "8").height();
204
205         // Draw the tick marks
206         p.setPen(Qt::black);
207
208         const double minor_tick_period = tick_period / MinorTickSubdivision;
209         const double first_major_division = floor(_offset / tick_period);
210         const double first_minor_division = ceil(_offset / minor_tick_period);
211         const double t0 = first_major_division * tick_period;
212
213         int division = (int)round(first_minor_division -
214                 first_major_division * MinorTickSubdivision);
215         while(1)
216         {
217                 const double t = t0 + division * minor_tick_period;
218                 const double x = (t - _offset) / _scale + LabelMarginWidth;
219
220                 if(x >= width())
221                         break;
222
223                 if(division % MinorTickSubdivision == 0)
224                 {
225                         // Draw a major tick
226                         QString s;
227                         QTextStream ts(&s);
228                         ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
229                         p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
230                                 Qt::TextDontClip, s);
231                         p.drawLine(x, text_height, x, RulerHeight);
232                 }
233                 else
234                 {
235                         // Draw a minor tick
236                         p.drawLine(x, (text_height + RulerHeight) / 2, x, RulerHeight);
237                 }
238
239                 division++;
240         }
241 }