CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
activitygraph.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef ACTIVITYGRAPH_H
31 #define ACTIVITYGRAPH_H
32 
33 
34 #include <QtGui/QWindow>
35 #include <QtCore/QCoreApplication>
36 #include <QtGui/QPainter>
37 #include <QtGui/QGuiApplication>
38 #include <QtGui/QScreen>
39 #include <QBasicTimer>
40 #include <QMouseEvent>
41 
42 #include <QDebug>
43 #include <QtCore/qmath.h>
44 #include <time.h>
45 #include <QGLWidget>
46 #include <QFontDatabase>
47 
48 #include <QTime>
49 
50 #include "common.h"
51 
52 namespace cavapa_gui{
58 struct GraphRegion{
63 
68 };
69 
79  QFont font;
80 
84  QBrush background;
85 
89  QBrush selection;
90 
97 
104 
110  QPen def_pen;
111 
118 
124  QPen half_line;
125 
130 
131 };
132 
143 class ActivityGraph : public QWidget
144 {
145  Q_OBJECT
146 
147 public:
148  ActivityGraph(QWidget *parent = 0);
149  ~ActivityGraph();
150 
152 
153  void setPrinting();
154 
155  void paint(QPainter &painter);
156 
163  void updateStatistics(const std::vector<FrameStats>& stats);
164 
165  void resetGraph();
166 
167  FrameTime QDateTimeToFrameTime(QDateTime time);
168  QDateTime FrameTimetoQDateTime(FrameTime ftime);
169 
170  std::vector<GraphMarker> getMarkers();
171 
172  QDateTime getStartDateTime();
174 
175  QDateTime getSelectedTimePoint();
176 
177 public slots:
178 
179  void zoomIn(QDateTime start, QDateTime end);
180  void zoomOut();
181 
182  void scrollLeft();
183  void scrollRight();
184  void scrollMouse(int position);
185 
186  void setStartTime(QDateTime time);
187  void setEndTime(QDateTime time);
188 
189  void setTimeRelative(bool relative);
190 
191  void showActivity(bool activity);
192  void showCount(bool count);
193  void showMarkerTexts(bool show);
194 
195  void addMarker(std::string text);
196  void removeSelectedMarker();
197 
198  void setFollowLatest(bool latest);
199  void setLatestLength(QTime length);
200 
201  void setNewSettings(GraphSettings settings);
202 
203  void setMarkers(std::vector<GraphMarker> markers);
204 
205  void setStartDateTime(QDateTime start);
206  void setCurrentPosition(QDateTime point);
207 
208 protected:
209  // Qt events
210  void paintEvent(QPaintEvent *event);
211  void mousePressEvent(QMouseEvent *event);
212  void mouseReleaseEvent(QMouseEvent *event);
213  void mouseMoveEvent(QMouseEvent *event);
214  void mouseDoubleClickEvent(QMouseEvent *event);
215  void timerEvent(QTimerEvent *event);
216 
217  // normalization from dataarrays to normarrays
218  void normalizedata();
219 
220 private:
221  int m_frame;
222 
223  // flags to define drawing type (changed by parent)
224  bool printing;
225  bool relativetime;
226  bool followlatest;
227  bool drawselection;
228  bool drawactivity;
229  bool drawcount;
230  bool showmarkertexts;
231 
232  bool marktomarkselection;
233  int lastmark;
234 
235  // graph top/bottom and left/right marigins, defined in constructor
236  int ymarigin;
237  int xmarigin;
238 
239  // dynamic sizes
240  int graphwidth;
241  int graphheight;
242 
243  // (real) time point when calculation launched
244  QDateTime startingtime;
245  // zero time point for relative time type
246  QDateTime zerotime;
247 
248  // selected time point with mouse click
249  QDateTime selectedtime;
250 
251  // widget's own update timer that requests stats from parent
252  QBasicTimer timer;
253 
254  // size of framestat area
255  FrameTime selection;
256  FrameTime previousselection;
257 
258  // size of sub section selection
259  FrameTime areaselection;
260  FrameTime screenarea;
261 
262  // selection start and end timepoints
263  QDateTime st;
264  QDateTime et;
265  FrameTime startframe;
266  FrameTime endframe;
267 
268  // size of received vector elements
269  int vectorsize;
270 
271  // curve data (input & normalized)
272  double dataarray_activity[100];
273  double normarray_activity[100];
274 
275  double dataarray_count[100];
276  double normarray_count[100];
277 
278  // maximum values from normalization
279  double maxvalue_a;
280  double maxvalue_c;
281 
282  // pausepoints for each statistic requests
283  // ready but not implemented from base
284  bool pausepoint[100];
285  QDateTime pausetime[100];
286 
287  // zooming variables TODO: infinite history?
288  int depth;
289  GraphRegion history[10];
290  int scrollspeed;
291 
292  // timestamp variables
293  std::string axistext[11];
294  int highlight;
295 
296  // mouse variables and flags
297  bool mousepressed;
298  bool mousedragging;
299  int mousex;
300  int mousey;
301  int selection_start;
302  int selection_end;
303  QPoint firstPos;
304  QPoint lastPos;
305  double startratio;
306  double endratio;
307 
308  // frameposition helpers
309  FrameTime selectedframe;
310  FrameTime selectedendframe;
311 
312  // first and last frame of whole calculation
313  FrameTime beginframe;
314  bool gotbeginframe;
315  FrameTime endingframe;
316 
317  // start and end of zoomed selection
318  FrameTime firstframe;
319  FrameTime lastframe;
320 
321  // length of realtime time window
322  FrameTime followlength;
323 
324  // markers in vector array
325  std::vector<GraphMarker> marks;
326  int selectedmarker;
327 
328  // graph's font for timestamps/markers
329  QFont timestampfont;
330 
331  // graph's fill brushes
332  QBrush background_brush;
333  QBrush areaselection_brush;
334 
335  // graph's pens (colors) for text/curves/lines
336  QPen activity_pen;
337  QPen count_pen;
338  QPen text_sel_pen;
339  QPen pen;
340  QPen half_pen;
341 
342 signals:
351  void statisticsRequested(FrameTime start, FrameTime stop, int points);
357  void sendTimeArea(QDateTime start, QDateTime end);
363  void sendFrameTimePosition(FrameTime position);
369  void sendScrollPosition(int position);
370 
375  void changeRTLabel(bool rt);
376 
382  void changeAbsoluteEnabled(bool ae);
383 
388  void changeFixed();
389 
394  void sendSelectedPoint(QDateTime point);
395 
399  void sendAddNewMarkerNotify();
400 
406  void setZoomInActive(bool active);
411  void setZoomOutActive(bool active);
412 
417  void setScrollLeftActive(bool active);
418 
424  void setScrollRightActive(bool active);
425 
430  void setScrollBarSize(int size);
431 
435  void markerChanged();
436 };
437 
438 }
439 #endif // ACTIVITYGRAPH_H
440 
void statisticsRequested(FrameTime start, FrameTime stop, int points)
Signal to be emitted when the graph wants data to be retrieved.
void sendAddNewMarkerNotify()
Sends a notify to parent of marker adding (when doubleclickin)
void changeFixed()
Sets parent's fixed time window -checkbox unchecked.
void resetGraph()
Resets the graph.
Definition: activitygraph.cpp:1297
Main class for Cavapa graphic curve display widget.
Definition: activitygraph.h:143
void mouseReleaseEvent(QMouseEvent *event)
Mouse release event of the graph.
Definition: activitygraph.cpp:954
void timerEvent(QTimerEvent *event)
Graph's own timer event to request data from parent.
Definition: activitygraph.cpp:1181
void setScrollBarSize(int size)
Sets scroll bar size.
QBrush selection
The graph selection color as QBrush, default = Qt::darkRed.
Definition: activitygraph.h:89
void setScrollRightActive(bool active)
Sets right slide button enabled or disabled.
void changeRTLabel(bool rt)
Changes the realtime label of parent.
QPen half_line
The default pen for horizontal half lines, etc.
Definition: activitygraph.h:124
void setTimeRelative(bool relative)
Changes time type between relative/absolute.
Definition: activitygraph.cpp:323
QPen def_pen
The default pen style for lines, etc.
Definition: activitygraph.h:110
QBrush background
The graph background color as QBrush, default = Qt::black.
Definition: activitygraph.h:84
void setFollowLatest(bool latest)
Sets the graph to follow only the latest data.
Definition: activitygraph.cpp:187
void showMarkerTexts(bool show)
Changes between showing/hiding marker texts without mouseover or selecting.
Definition: activitygraph.cpp:353
void showActivity(bool activity)
Changes between showing/hiding activity curve.
Definition: activitygraph.cpp:336
QDateTime getStartDateTime()
Gets the start time of the graph.
Definition: activitygraph.cpp:1274
void setPrinting()
Sets render function ready to print graph into image file.
Definition: activitygraph.cpp:1204
ActivityGraph(QWidget *parent=0)
Creates and initializes the graph.
Definition: activitygraph.cpp:38
void setStartTime(QDateTime time)
Gets the graph region starttime from the timeEdit of parent.
Definition: activitygraph.cpp:200
void normalizedata()
Normalizes input data arrays into normalized arrays.
Definition: activitygraph.cpp:1138
void setCurrentPosition(QDateTime point)
Sets the current selection to time point.
Definition: activitygraph.cpp:1322
void setNewSettings(GraphSettings settings)
Gets new settings for font, colors and line widths.
Definition: activitygraph.cpp:1212
FrameTime start
The start point of the region.
Definition: activitygraph.h:62
QPen text_selection
The default pen for text selection, etc.
Definition: activitygraph.h:117
void sendSelectedPoint(QDateTime point)
Sends QDateTime of selected position.
void changeAbsoluteEnabled(bool ae)
Sets absolute radio button enabled or disabled.
void removeSelectedMarker()
Removes the selected time marker (if an active marker is selected).
Definition: activitygraph.cpp:778
void setZoomOutActive(bool active)
Sets zoom out button enabled or disabled.
void showCount(bool count)
Changes between showing/hiding count curve.
Definition: activitygraph.cpp:344
Struct for one region of the graph.
Definition: activitygraph.h:58
void zoomOut()
Zooms graph out.
Definition: activitygraph.cpp:118
int linewidth
The line width for curves.
Definition: activitygraph.h:129
void setStartDateTime(QDateTime start)
Sets start datetime for the graph.
Definition: activitygraph.cpp:1306
void setMarkers(std::vector< GraphMarker > markers)
Sets the markers from parent.
Definition: activitygraph.cpp:1257
QDateTime FrameTimetoQDateTime(FrameTime ftime)
Changes FrameTime into QDateTime according the beginframe.
Definition: activitygraph.cpp:1243
std::uint64_t FrameTime
Used to store milliseconds interval in frame times.
Definition: common.h:138
Graph settings type for font/colors/line widths.
Definition: activitygraph.h:75
void paintEvent(QPaintEvent *event)
Qt paint event for drawing the graph.
Definition: activitygraph.cpp:361
QPen activity_curve
The activity curve style as QPen, default = QPen(Qt::magenta, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); Change only the first two parameters (color and width).
Definition: activitygraph.h:96
void scrollLeft()
Scrolls graph left.
Definition: activitygraph.cpp:283
void addMarker(std::string text)
Adds new marker to the selected position.
Definition: activitygraph.cpp:766
void mouseDoubleClickEvent(QMouseEvent *event)
Double-clicking the graph notifies the parent to add new marker.
Definition: activitygraph.cpp:1006
FrameTime end
The end point of the region.
Definition: activitygraph.h:67
void zoomIn(QDateTime start, QDateTime end)
Zooms graph in.
Definition: activitygraph.cpp:143
void setZoomInActive(bool active)
Sets zoom in button enabled or disabled.
GraphRegion sendGraphRegion()
Sends graph selection start and end positions.
Definition: activitygraph.cpp:1126
FrameTime getStartFrame()
Gets the first frame of the graph.
Definition: activitygraph.cpp:1290
void markerChanged()
Informs the parent of marker adding.
void setScrollLeftActive(bool active)
Sets left slide button enabled or disabled.
QPen count_curve
The count curve style as QPen, default = QPen(Qt::cyan, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); Change only the first two parameters (color and width).
Definition: activitygraph.h:103
void sendScrollPosition(int position)
Sends correct scroll bar position when zooming in/out.
QDateTime getSelectedTimePoint()
Gets the time point of currently selected frame.
Definition: activitygraph.cpp:1314
void sendTimeArea(QDateTime start, QDateTime end)
Sends Selection start and end time points.
void mousePressEvent(QMouseEvent *event)
Mouse Press event of the graph.
Definition: activitygraph.cpp:793
FrameTime QDateTimeToFrameTime(QDateTime time)
Changes QDateTime to FrameTime according the starting time of calculation.
Definition: activitygraph.cpp:1229
void scrollMouse(int position)
Scrolls zoomed graph area with scrollbar/mouse.
Definition: activitygraph.cpp:245
void scrollRight()
Scrolls graph right.
Definition: activitygraph.cpp:307
void mouseMoveEvent(QMouseEvent *event)
Mouse move event of the graph.
Definition: activitygraph.cpp:857
void setLatestLength(QTime length)
Sets the length of latest-data mode.
Definition: activitygraph.cpp:1171
void updateStatistics(const std::vector< FrameStats > &stats)
Updates the graph with new statistics.
Definition: activitygraph.cpp:1015
QFont font
The graph text font as QFont, default = "Arial".
Definition: activitygraph.h:79
void setEndTime(QDateTime time)
Gets graph region endtime from timeEdit of parent.
Definition: activitygraph.cpp:222
std::vector< GraphMarker > getMarkers()
Gets markers from the graph.
Definition: activitygraph.cpp:1265
void sendFrameTimePosition(FrameTime position)
Sends frametime of the selection position.
void paint(QPainter &painter)
Draws the graph.
Definition: activitygraph.cpp:376