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