]>
Commit | Line | Data |
---|---|---|
1 | #include <iostream> | |
2 | #include <QPainterPath> | |
3 | #include <QBrush> | |
4 | #include <QPen> | |
5 | #include <QTimer> | |
6 | #include <QCloseEvent> | |
7 | #include <QMouseEvent> | |
8 | #include <QKeyEvent> | |
9 | #include <math.h> | |
10 | #include <limits.h> | |
11 | #include "proxguiqt.h" | |
12 | #include "proxgui.h" | |
13 | ||
14 | void ProxGuiQT::ShowGraphWindow(void) | |
15 | { | |
16 | emit ShowGraphWindowSignal(); | |
17 | } | |
18 | ||
19 | void ProxGuiQT::RepaintGraphWindow(void) | |
20 | { | |
21 | emit RepaintGraphWindowSignal(); | |
22 | } | |
23 | ||
24 | void ProxGuiQT::HideGraphWindow(void) | |
25 | { | |
26 | emit HideGraphWindowSignal(); | |
27 | } | |
28 | ||
29 | void ProxGuiQT::_ShowGraphWindow(void) | |
30 | { | |
31 | if(!plotapp) | |
32 | return; | |
33 | ||
34 | if (!plotwidget) | |
35 | plotwidget = new ProxWidget(); | |
36 | ||
37 | plotwidget->show(); | |
38 | } | |
39 | ||
40 | void ProxGuiQT::_RepaintGraphWindow(void) | |
41 | { | |
42 | if (!plotapp || !plotwidget) | |
43 | return; | |
44 | ||
45 | plotwidget->update(); | |
46 | } | |
47 | ||
48 | void ProxGuiQT::_HideGraphWindow(void) | |
49 | { | |
50 | if (!plotapp || !plotwidget) | |
51 | return; | |
52 | ||
53 | plotwidget->hide(); | |
54 | } | |
55 | ||
56 | void ProxGuiQT::MainLoop() | |
57 | { | |
58 | plotapp = new QApplication(argc, argv); | |
59 | ||
60 | connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow())); | |
61 | connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow())); | |
62 | connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow())); | |
63 | ||
64 | plotapp->exec(); | |
65 | } | |
66 | ||
67 | ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL), | |
68 | argc(argc), argv(argv) | |
69 | { | |
70 | } | |
71 | ||
72 | ProxGuiQT::~ProxGuiQT(void) | |
73 | { | |
74 | if (plotwidget) { | |
75 | delete plotwidget; | |
76 | plotwidget = NULL; | |
77 | } | |
78 | ||
79 | if (plotapp) { | |
80 | plotapp->quit(); | |
81 | delete plotapp; | |
82 | plotapp = NULL; | |
83 | } | |
84 | } | |
85 | ||
86 | void ProxWidget::paintEvent(QPaintEvent *event) | |
87 | { | |
88 | QPainter painter(this); | |
89 | QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath; | |
90 | QRect r; | |
91 | QBrush brush(QColor(100, 255, 100)); | |
92 | QPen pen(QColor(100, 255, 100)); | |
93 | ||
94 | painter.setFont(QFont("Arial", 10)); | |
95 | ||
96 | if(GraphStart < 0) { | |
97 | GraphStart = 0; | |
98 | } | |
99 | ||
100 | if (CursorAPos > GraphTraceLen) | |
101 | CursorAPos= 0; | |
102 | if(CursorBPos > GraphTraceLen) | |
103 | CursorBPos= 0; | |
104 | ||
105 | r = rect(); | |
106 | ||
107 | painter.fillRect(r, QColor(0, 0, 0)); | |
108 | ||
109 | whitePath.moveTo(r.left() + 40, r.top()); | |
110 | whitePath.lineTo(r.left() + 40, r.bottom()); | |
111 | ||
112 | int zeroHeight = r.top() + (r.bottom() - r.top()) / 2; | |
113 | ||
114 | greyPath.moveTo(r.left(), zeroHeight); | |
115 | greyPath.lineTo(r.right(), zeroHeight); | |
116 | painter.setPen(QColor(100, 100, 100)); | |
117 | painter.drawPath(greyPath); | |
118 | ||
119 | // plot X and Y grid lines | |
120 | int i; | |
121 | if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) { | |
122 | for(i = 40; i < r.right(); i += (int)(PlotGridX * GraphPixelsPerPoint)) { | |
123 | //SelectObject(hdc, GreyPenLite); | |
124 | //MoveToEx(hdc, r.left + i, r.top, NULL); | |
125 | //LineTo(hdc, r.left + i, r.bottom); | |
126 | lightgreyPath.moveTo(r.left()+i,r.top()); | |
127 | lightgreyPath.lineTo(r.left()+i,r.bottom()); | |
128 | painter.drawPath(lightgreyPath); | |
129 | } | |
130 | } | |
131 | if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){ | |
132 | for(i = 0; i < ((r.top() + r.bottom())>>1); i += (int)(PlotGridY * GraphPixelsPerPoint)) { | |
133 | lightgreyPath.moveTo(r.left() + 40,zeroHeight + i); | |
134 | lightgreyPath.lineTo(r.right(),zeroHeight + i); | |
135 | painter.drawPath(lightgreyPath); | |
136 | lightgreyPath.moveTo(r.left() + 40,zeroHeight - i); | |
137 | lightgreyPath.lineTo(r.right(),zeroHeight - i); | |
138 | painter.drawPath(lightgreyPath); | |
139 | } | |
140 | } | |
141 | ||
142 | int startMax = | |
143 | (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint)); | |
144 | if(startMax < 0) { | |
145 | startMax = 0; | |
146 | } | |
147 | if(GraphStart > startMax) { | |
148 | GraphStart = startMax; | |
149 | } | |
150 | ||
151 | int absYMax = 1; | |
152 | ||
153 | for(i = GraphStart; ; i++) { | |
154 | if(i >= GraphTraceLen) { | |
155 | break; | |
156 | } | |
157 | if(fabs((double)GraphBuffer[i]) > absYMax) { | |
158 | absYMax = (int)fabs((double)GraphBuffer[i]); | |
159 | } | |
160 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
161 | if(x > r.right()) { | |
162 | break; | |
163 | } | |
164 | } | |
165 | ||
166 | absYMax = (int)(absYMax*1.2 + 1); | |
167 | ||
168 | // number of points that will be plotted | |
169 | int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint); | |
170 | // one label every 100 pixels, let us say | |
171 | int labels = (r.right() - r.left() - 40) / 100; | |
172 | if(labels <= 0) labels = 1; | |
173 | int pointsPerLabel = span / labels; | |
174 | if(pointsPerLabel <= 0) pointsPerLabel = 1; | |
175 | ||
176 | int yMin = INT_MAX; | |
177 | int yMax = INT_MIN; | |
178 | int yMean = 0; | |
179 | int n = 0; | |
180 | ||
181 | for(i = GraphStart; ; i++) { | |
182 | if(i >= GraphTraceLen) { | |
183 | break; | |
184 | } | |
185 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
186 | if(x > r.right() + GraphPixelsPerPoint) { | |
187 | break; | |
188 | } | |
189 | ||
190 | int y = GraphBuffer[i]; | |
191 | if(y < yMin) { | |
192 | yMin = y; | |
193 | } | |
194 | if(y > yMax) { | |
195 | yMax = y; | |
196 | } | |
197 | yMean += y; | |
198 | n++; | |
199 | ||
200 | y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight; | |
201 | if(i == GraphStart) { | |
202 | penPath.moveTo(x, y); | |
203 | } else { | |
204 | penPath.lineTo(x, y); | |
205 | } | |
206 | ||
207 | if(GraphPixelsPerPoint > 10) { | |
208 | QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3)); | |
209 | painter.fillRect(f, brush); | |
210 | } | |
211 | ||
212 | if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) { | |
213 | whitePath.moveTo(x, zeroHeight - 3); | |
214 | whitePath.lineTo(x, zeroHeight + 3); | |
215 | ||
216 | char str[100]; | |
217 | sprintf(str, "+%d", (i - GraphStart)); | |
218 | ||
219 | painter.setPen(QColor(255, 255, 255)); | |
220 | QRect size; | |
221 | QFontMetrics metrics(painter.font()); | |
222 | size = metrics.boundingRect(str); | |
223 | painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str); | |
224 | ||
225 | penPath.moveTo(x,y); | |
226 | } | |
227 | ||
228 | if(i == CursorAPos || i == CursorBPos) { | |
229 | QPainterPath *cursorPath; | |
230 | ||
231 | if(i == CursorAPos) { | |
232 | cursorPath = &cursorAPath; | |
233 | } else { | |
234 | cursorPath = &cursorBPath; | |
235 | } | |
236 | cursorPath->moveTo(x, r.top()); | |
237 | cursorPath->lineTo(x, r.bottom()); | |
238 | penPath.moveTo(x, y); | |
239 | } | |
240 | } | |
241 | ||
242 | if(n != 0) { | |
243 | yMean /= n; | |
244 | } | |
245 | ||
246 | painter.setPen(QColor(255, 255, 255)); | |
247 | painter.drawPath(whitePath); | |
248 | painter.setPen(pen); | |
249 | painter.drawPath(penPath); | |
250 | painter.setPen(QColor(255, 255, 0)); | |
251 | painter.drawPath(cursorAPath); | |
252 | painter.setPen(QColor(255, 0, 255)); | |
253 | painter.drawPath(cursorBPath); | |
254 | ||
255 | char str[100]; | |
256 | sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]", | |
257 | GraphStart, yMax, yMin, yMean, n, GraphTraceLen, | |
258 | CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,GraphPixelsPerPoint,CursorAPos,GraphBuffer[CursorAPos],CursorBPos,GraphBuffer[CursorBPos]); | |
259 | ||
260 | painter.setPen(QColor(255, 255, 255)); | |
261 | painter.drawText(50, r.bottom() - 20, str); | |
262 | } | |
263 | ||
264 | ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1) | |
265 | { | |
266 | resize(600, 500); | |
267 | ||
268 | QPalette palette(QColor(0,0,0,0)); | |
269 | palette.setColor(QPalette::WindowText, QColor(255,255,255)); | |
270 | palette.setColor(QPalette::Text, QColor(255,255,255)); | |
271 | palette.setColor(QPalette::Button, QColor(100, 100, 100)); | |
272 | setPalette(palette); | |
273 | setAutoFillBackground(true); | |
274 | } | |
275 | ||
276 | void ProxWidget::closeEvent(QCloseEvent *event) | |
277 | { | |
278 | event->ignore(); | |
279 | this->hide(); | |
280 | } | |
281 | ||
282 | void ProxWidget::mouseMoveEvent(QMouseEvent *event) | |
283 | { | |
284 | int x = event->x(); | |
285 | x -= 40; | |
286 | x = (int)(x / GraphPixelsPerPoint); | |
287 | x += GraphStart; | |
288 | if((event->buttons() & Qt::LeftButton)) { | |
289 | CursorAPos = x; | |
290 | } else if (event->buttons() & Qt::RightButton) { | |
291 | CursorBPos = x; | |
292 | } | |
293 | ||
294 | ||
295 | this->update(); | |
296 | } | |
297 | ||
298 | void ProxWidget::keyPressEvent(QKeyEvent *event) | |
299 | { | |
300 | switch(event->key()) { | |
301 | case Qt::Key_Down: | |
302 | if(GraphPixelsPerPoint <= 50) { | |
303 | GraphPixelsPerPoint *= 2; | |
304 | } | |
305 | break; | |
306 | ||
307 | case Qt::Key_Up: | |
308 | if(GraphPixelsPerPoint >= 0.02) { | |
309 | GraphPixelsPerPoint /= 2; | |
310 | } | |
311 | break; | |
312 | ||
313 | case Qt::Key_Right: | |
314 | if(GraphPixelsPerPoint < 20) { | |
315 | GraphStart += (int)(20 / GraphPixelsPerPoint); | |
316 | } else { | |
317 | GraphStart++; | |
318 | } | |
319 | break; | |
320 | ||
321 | case Qt::Key_Left: | |
322 | if(GraphPixelsPerPoint < 20) { | |
323 | GraphStart -= (int)(20 / GraphPixelsPerPoint); | |
324 | } else { | |
325 | GraphStart--; | |
326 | } | |
327 | break; | |
328 | ||
329 | default: | |
330 | QWidget::keyPressEvent(event); | |
331 | return; | |
332 | break; | |
333 | } | |
334 | ||
335 | this->update(); | |
336 | } |