]>
Commit | Line | Data |
---|---|---|
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> | |
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, 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 | ||
0bf5872f | 100 | if (CursorAPos > GraphTraceLen) |
101 | CursorAPos= 0; | |
102 | if(CursorBPos > GraphTraceLen) | |
103 | CursorBPos= 0; | |
104 | ||
6658905f | 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 | int startMax = | |
120 | (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint)); | |
121 | if(startMax < 0) { | |
122 | startMax = 0; | |
123 | } | |
124 | if(GraphStart > startMax) { | |
125 | GraphStart = startMax; | |
126 | } | |
127 | ||
128 | int absYMax = 1; | |
129 | ||
130 | int i; | |
131 | for(i = GraphStart; ; i++) { | |
132 | if(i >= GraphTraceLen) { | |
133 | break; | |
134 | } | |
135 | if(fabs((double)GraphBuffer[i]) > absYMax) { | |
136 | absYMax = (int)fabs((double)GraphBuffer[i]); | |
137 | } | |
138 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
139 | if(x > r.right()) { | |
140 | break; | |
141 | } | |
142 | } | |
143 | ||
144 | absYMax = (int)(absYMax*1.2 + 1); | |
145 | ||
146 | // number of points that will be plotted | |
147 | int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint); | |
148 | // one label every 100 pixels, let us say | |
149 | int labels = (r.right() - r.left() - 40) / 100; | |
150 | if(labels <= 0) labels = 1; | |
151 | int pointsPerLabel = span / labels; | |
152 | if(pointsPerLabel <= 0) pointsPerLabel = 1; | |
153 | ||
154 | int yMin = INT_MAX; | |
155 | int yMax = INT_MIN; | |
156 | int yMean = 0; | |
157 | int n = 0; | |
158 | ||
159 | for(i = GraphStart; ; i++) { | |
160 | if(i >= GraphTraceLen) { | |
161 | break; | |
162 | } | |
163 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
164 | if(x > r.right() + GraphPixelsPerPoint) { | |
165 | break; | |
166 | } | |
167 | ||
168 | int y = GraphBuffer[i]; | |
169 | if(y < yMin) { | |
170 | yMin = y; | |
171 | } | |
172 | if(y > yMax) { | |
173 | yMax = y; | |
174 | } | |
175 | yMean += y; | |
176 | n++; | |
177 | ||
178 | y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight; | |
179 | if(i == GraphStart) { | |
180 | penPath.moveTo(x, y); | |
181 | } else { | |
182 | penPath.lineTo(x, y); | |
183 | } | |
184 | ||
185 | if(GraphPixelsPerPoint > 10) { | |
186 | QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3)); | |
187 | painter.fillRect(f, brush); | |
188 | } | |
189 | ||
190 | if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) { | |
191 | whitePath.moveTo(x, zeroHeight - 3); | |
192 | whitePath.lineTo(x, zeroHeight + 3); | |
193 | ||
194 | char str[100]; | |
195 | sprintf(str, "+%d", (i - GraphStart)); | |
196 | ||
197 | painter.setPen(QColor(255, 255, 255)); | |
198 | QRect size; | |
199 | QFontMetrics metrics(painter.font()); | |
200 | size = metrics.boundingRect(str); | |
201 | painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str); | |
202 | ||
203 | penPath.moveTo(x,y); | |
204 | } | |
205 | ||
206 | if(i == CursorAPos || i == CursorBPos) { | |
207 | QPainterPath *cursorPath; | |
208 | ||
209 | if(i == CursorAPos) { | |
210 | cursorPath = &cursorAPath; | |
211 | } else { | |
212 | cursorPath = &cursorBPath; | |
213 | } | |
214 | cursorPath->moveTo(x, r.top()); | |
215 | cursorPath->lineTo(x, r.bottom()); | |
216 | penPath.moveTo(x, y); | |
217 | } | |
218 | } | |
219 | ||
220 | if(n != 0) { | |
221 | yMean /= n; | |
222 | } | |
223 | ||
224 | painter.setPen(QColor(255, 255, 255)); | |
225 | painter.drawPath(whitePath); | |
226 | painter.setPen(pen); | |
227 | painter.drawPath(penPath); | |
228 | painter.setPen(QColor(255, 255, 0)); | |
229 | painter.drawPath(cursorAPath); | |
230 | painter.setPen(QColor(255, 0, 255)); | |
231 | painter.drawPath(cursorBPath); | |
232 | ||
233 | char str[100]; | |
af2d53cb | 234 | sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d (%d) CursorB=%d (%d)", |
6658905f | 235 | GraphStart, yMax, yMin, yMean, n, GraphTraceLen, |
af2d53cb | 236 | CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,GraphPixelsPerPoint,CursorAPos,GraphBuffer[CursorAPos],CursorBPos,GraphBuffer[CursorBPos]); |
6658905f | 237 | |
238 | painter.setPen(QColor(255, 255, 255)); | |
239 | painter.drawText(50, r.bottom() - 20, str); | |
240 | } | |
241 | ||
242 | ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1) | |
243 | { | |
244 | resize(600, 500); | |
245 | ||
246 | QPalette palette(QColor(0,0,0,0)); | |
247 | palette.setColor(QPalette::WindowText, QColor(255,255,255)); | |
248 | palette.setColor(QPalette::Text, QColor(255,255,255)); | |
249 | palette.setColor(QPalette::Button, QColor(100, 100, 100)); | |
250 | setPalette(palette); | |
251 | setAutoFillBackground(true); | |
252 | } | |
253 | ||
254 | void ProxWidget::closeEvent(QCloseEvent *event) | |
255 | { | |
256 | event->ignore(); | |
257 | this->hide(); | |
258 | } | |
259 | ||
260 | void ProxWidget::mouseMoveEvent(QMouseEvent *event) | |
261 | { | |
262 | int x = event->x(); | |
263 | x -= 40; | |
264 | x = (int)(x / GraphPixelsPerPoint); | |
265 | x += GraphStart; | |
266 | if((event->buttons() & Qt::LeftButton)) { | |
267 | CursorAPos = x; | |
268 | } else if (event->buttons() & Qt::RightButton) { | |
269 | CursorBPos = x; | |
270 | } | |
271 | ||
272 | ||
273 | this->update(); | |
274 | } | |
275 | ||
276 | void ProxWidget::keyPressEvent(QKeyEvent *event) | |
277 | { | |
278 | switch(event->key()) { | |
279 | case Qt::Key_Down: | |
280 | if(GraphPixelsPerPoint <= 50) { | |
281 | GraphPixelsPerPoint *= 2; | |
282 | } | |
283 | break; | |
284 | ||
285 | case Qt::Key_Up: | |
286 | if(GraphPixelsPerPoint >= 0.02) { | |
287 | GraphPixelsPerPoint /= 2; | |
288 | } | |
289 | break; | |
290 | ||
291 | case Qt::Key_Right: | |
292 | if(GraphPixelsPerPoint < 20) { | |
293 | GraphStart += (int)(20 / GraphPixelsPerPoint); | |
294 | } else { | |
295 | GraphStart++; | |
296 | } | |
297 | break; | |
298 | ||
299 | case Qt::Key_Left: | |
300 | if(GraphPixelsPerPoint < 20) { | |
301 | GraphStart -= (int)(20 / GraphPixelsPerPoint); | |
302 | } else { | |
303 | GraphStart--; | |
304 | } | |
305 | break; | |
306 | ||
307 | default: | |
308 | QWidget::keyPressEvent(event); | |
309 | return; | |
310 | break; | |
311 | } | |
312 | ||
313 | this->update(); | |
314 | } |