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