]>
Commit | Line | Data |
---|---|---|
6fa02541 JH |
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 | ||
009e1503 | 23 | #include "sigsession.h" |
e3f65ace JH |
24 | #include "signal.h" |
25 | ||
e9c41f8c JH |
26 | #include "extdef.h" |
27 | ||
7cd5faf8 | 28 | #include <QMouseEvent> |
22016ad3 | 29 | #include <QTextStream> |
7cd5faf8 | 30 | |
e9c41f8c JH |
31 | #include <math.h> |
32 | ||
e3f65ace JH |
33 | #include <boost/foreach.hpp> |
34 | ||
35 | using namespace boost; | |
36 | using namespace std; | |
37 | ||
38 | const int SigView::SignalHeight = 50; | |
3e46726a | 39 | const int SigView::LabelMarginWidth = 70; |
e9c41f8c JH |
40 | const int SigView::RulerHeight = 30; |
41 | ||
438440ea | 42 | const int SigView::MinorTickSubdivision = 4; |
e9c41f8c | 43 | const int SigView::ScaleUnits[3] = {1, 2, 5}; |
009e1503 | 44 | |
22016ad3 JH |
45 | const QString SigView::SIPrefixes[9] = |
46 | {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"}; | |
47 | const int SigView::FirstSIPrefixPower = -15; | |
48 | ||
009e1503 JH |
49 | SigView::SigView(SigSession &session, QWidget *parent) : |
50 | QGLWidget(parent), | |
3b18c57d | 51 | _session(session), |
fb0d32cb | 52 | _scale(1e-6), |
3b18c57d | 53 | _offset(0) |
6fa02541 | 54 | { |
04abfae9 JH |
55 | connect(&_session, SIGNAL(data_updated()), |
56 | this, SLOT(data_updated())); | |
009e1503 | 57 | |
d7002724 | 58 | setMouseTracking(true); |
3e46726a | 59 | setAutoFillBackground(false); |
d7002724 JH |
60 | } |
61 | ||
62 | void SigView::initializeGL() | |
63 | { | |
d7002724 JH |
64 | } |
65 | ||
66 | void SigView::resizeGL(int width, int height) | |
67 | { | |
04abfae9 | 68 | setup_viewport(width, height); |
d7002724 JH |
69 | } |
70 | ||
3e46726a | 71 | void SigView::paintEvent(QPaintEvent *event) |
d7002724 | 72 | { |
3e46726a | 73 | int offset; |
e3f65ace | 74 | |
e3f65ace JH |
75 | const vector< shared_ptr<Signal> > &sigs = |
76 | _session.get_signals(); | |
3e46726a JH |
77 | |
78 | // Prepare for OpenGL rendering | |
79 | makeCurrent(); | |
80 | glMatrixMode(GL_MODELVIEW); | |
81 | glPushMatrix(); | |
82 | ||
04abfae9 | 83 | setup_viewport(width(), height()); |
3e46726a JH |
84 | |
85 | qglClearColor(Qt::white); | |
86 | glClear(GL_COLOR_BUFFER_BIT); | |
87 | ||
88 | // Plot the signal | |
e9c41f8c | 89 | offset = RulerHeight; |
3e46726a JH |
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 | ||
e9c41f8c JH |
109 | // Paint the labels |
110 | offset = RulerHeight; | |
e3f65ace JH |
111 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) |
112 | { | |
113 | assert(s); | |
3e46726a JH |
114 | |
115 | const QRect label_rect(0, offset, | |
116 | LabelMarginWidth, SignalHeight); | |
117 | s->paint_label(painter, label_rect); | |
118 | ||
119 | offset += SignalHeight; | |
e3f65ace | 120 | } |
3e46726a | 121 | |
e9c41f8c | 122 | // Paint the ruler |
04abfae9 | 123 | paint_ruler(painter); |
e9c41f8c | 124 | |
3e46726a | 125 | painter.end(); |
6fa02541 | 126 | } |
009e1503 | 127 | |
04abfae9 | 128 | void SigView::data_updated() |
009e1503 | 129 | { |
e3f65ace | 130 | update(); |
009e1503 JH |
131 | } |
132 | ||
1dd95c70 | 133 | void SigView::mousePressEvent(QMouseEvent *event) |
7cd5faf8 JH |
134 | { |
135 | assert(event); | |
1dd95c70 JH |
136 | |
137 | _mouse_down_point = event->pos(); | |
138 | _mouse_down_offset = _offset; | |
7cd5faf8 JH |
139 | } |
140 | ||
1dd95c70 | 141 | void SigView::mouseMoveEvent(QMouseEvent *event) |
7cd5faf8 JH |
142 | { |
143 | assert(event); | |
1dd95c70 JH |
144 | |
145 | if(event->buttons() & Qt::LeftButton) | |
146 | { | |
147 | _offset = _mouse_down_offset + (_mouse_down_point - event->pos()).x() * _scale; | |
148 | update(); | |
149 | } | |
7cd5faf8 JH |
150 | } |
151 | ||
152 | void SigView::mouseReleaseEvent(QMouseEvent *event) | |
153 | { | |
154 | assert(event); | |
1dd95c70 | 155 | } |
7cd5faf8 | 156 | |
1dd95c70 JH |
157 | void SigView::wheelEvent(QWheelEvent *event) |
158 | { | |
159 | assert(event); | |
fb0d32cb | 160 | |
1dd95c70 JH |
161 | const double x = event->x() - LabelMarginWidth; |
162 | const double cursor_offset = _offset + _scale * x; | |
163 | _scale *= powf(3.0/2.0, -event->delta() / 120); | |
164 | _offset = cursor_offset - _scale * x; | |
3e46726a | 165 | update(); |
7cd5faf8 JH |
166 | } |
167 | ||
04abfae9 | 168 | void SigView::setup_viewport(int width, int height) |
3e46726a JH |
169 | { |
170 | glViewport(0, 0, (GLint)width, (GLint)height); | |
171 | glMatrixMode(GL_PROJECTION); | |
172 | glLoadIdentity(); | |
173 | glOrtho(0, width, height, 0, -1, 1); | |
174 | glMatrixMode(GL_MODELVIEW); | |
175 | } | |
e9c41f8c | 176 | |
04abfae9 | 177 | void SigView::paint_ruler(QPainter &p) |
e9c41f8c | 178 | { |
22016ad3 | 179 | const double MinSpacing = 80; |
e9c41f8c | 180 | |
e9c41f8c JH |
181 | const double min_period = _scale * MinSpacing; |
182 | ||
22016ad3 JH |
183 | const int order = (int)floorf(log10f(min_period)); |
184 | const double order_decimal = pow(10, order); | |
185 | ||
186 | int unit = 0; | |
187 | double tick_period = 0.0f; | |
188 | ||
189 | do | |
e9c41f8c | 190 | { |
22016ad3 JH |
191 | tick_period = order_decimal * ScaleUnits[unit++]; |
192 | } while(tick_period < min_period && unit < countof(ScaleUnits)); | |
e9c41f8c | 193 | |
22016ad3 JH |
194 | const int prefix = (order - FirstSIPrefixPower) / 3; |
195 | assert(prefix >= 0); | |
196 | assert(prefix < countof(SIPrefixes)); | |
e9c41f8c | 197 | |
22016ad3 JH |
198 | const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, |
199 | Qt::AlignLeft | Qt::AlignTop, "8").height(); | |
e9c41f8c | 200 | |
22016ad3 | 201 | // Draw the tick marks |
e9c41f8c JH |
202 | p.setPen(Qt::black); |
203 | ||
438440ea JH |
204 | const double minor_tick_period = tick_period / MinorTickSubdivision; |
205 | const double first_major_division = floor(_offset / tick_period); | |
206 | const double first_minor_division = ceil(_offset / minor_tick_period); | |
207 | const double t0 = first_major_division * tick_period; | |
208 | ||
209 | int division = (int)round(first_minor_division - | |
210 | first_major_division * MinorTickSubdivision); | |
211 | while(1) | |
e9c41f8c | 212 | { |
438440ea JH |
213 | const double t = t0 + division * minor_tick_period; |
214 | const double x = (t - _offset) / _scale + LabelMarginWidth; | |
215 | ||
216 | if(x >= width()) | |
217 | break; | |
218 | ||
219 | if(division % MinorTickSubdivision == 0) | |
220 | { | |
221 | // Draw a major tick | |
222 | QString s; | |
223 | QTextStream ts(&s); | |
224 | ts << (t / order_decimal) << SIPrefixes[prefix] << "s"; | |
225 | p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop | | |
226 | Qt::TextDontClip, s); | |
227 | p.drawLine(x, text_height, x, RulerHeight); | |
228 | } | |
229 | else | |
230 | { | |
231 | // Draw a minor tick | |
232 | p.drawLine(x, (text_height + RulerHeight) / 2, x, RulerHeight); | |
233 | } | |
234 | ||
235 | division++; | |
e9c41f8c JH |
236 | } |
237 | } |