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