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