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