DLS  1.6
Graph.h
1 /*****************************************************************************
2  *
3  * Copyright (C) 2009 - 2017 Florian Pose <fp@igh-essen.com>
4  *
5  * This file is part of the DLS widget library.
6  *
7  * The DLS widget library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of the License,
10  * or (at your option) any later version.
11  *
12  * The DLS widget library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with the DLS widget library. If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  ****************************************************************************/
22 
23 #ifndef DLS_GRAPH_H
24 #define DLS_GRAPH_H
25 
26 #include <QtGlobal>
27 #include <QFrame>
28 #include <QThread>
29 #include <QMutex>
30 #include <QSvgRenderer>
31 #include <QAction>
32 #include <QScrollBar>
33 #include <QReadWriteLock>
34 #include <QTouchEvent>
35 #include <QUrl>
36 #include <QDir>
37 #include <QMenu>
38 
39 #include "export.h"
40 
41 #include <LibDLS/Job.h>
42 
43 #include "Scale.h"
44 
45 class QDomElement;
46 
47 namespace LibDLS {
48  class Data;
49 }
50 
51 namespace QtDls {
52  class Model;
53  class Channel;
54 }
55 
56 namespace DLS {
57 
58 class Section;
59 class Graph;
60 
61 /****************************************************************************/
62 
65 class DLSWIDGETS_PUBLIC GraphWorker:
66  public QObject {
67  Q_OBJECT
68 
69  public:
70  GraphWorker(Graph *);
71  ~GraphWorker();
72 
73  void clearData();
74 
75  static int dataCallback(LibDLS::Data *, void *);
76 
77  int width;
78 
79  const QList<LibDLS::Data *> &genData() const { return genericData; }
80  const QList<LibDLS::Data *> &minData() const { return minimumData; }
81  const QList<LibDLS::Data *> &maxData() const { return maximumData; }
82 
83  public slots:
84  void doWork();
85  signals:
86  void notifySection(Section *section);
87  void finished();
88  private:
89  Graph * const graph;
90  QList<LibDLS::Data *> genericData;
91  QList<LibDLS::Data *> minimumData;
92  QList<LibDLS::Data *> maximumData;
93  QList<LibDLS::Job::Message> messages;
94 
95  void newData(LibDLS::Data *);
96  static void clearDataList(QList<LibDLS::Data *> &);
97 
98 };
99 
100 /****************************************************************************/
101 
104 class DLSWIDGETS_PUBLIC Graph:
105  public QFrame
106 {
107  Q_OBJECT
108 
109  friend class GraphWorker;
110  friend class Section; // FIXME
111 
112  public:
113  Graph(QWidget *parent = 0);
114  virtual ~Graph();
115 
116  void setDropModel(QtDls::Model *);
117 
118  virtual QSize sizeHint() const;
119 
120  bool load(const QString &, QtDls::Model *);
121  bool save(const QString &);
122 
123  enum RenderFlag {
124  Plain = 0,
125  MeasuringLine = 1,
126  All = MeasuringLine
127  };
128  Q_DECLARE_FLAGS(RenderFlags, RenderFlag)
129  bool renderPage(QPainter &, const QRect &, unsigned int = 0,
130  RenderFlags = All);
131 
132  void connectChannels(QtDls::Model *);
133  bool dirInUse(const LibDLS::Directory *);
134 
135  Section *appendSection();
136  Section *insertSectionBefore(Section *);
137  void removeSection(Section *);
138 
139  void updateRange();
140  void setRange(const LibDLS::Time &, const LibDLS::Time &);
141  const LibDLS::Time &getStart() const { return scale.getStart(); };
142  const LibDLS::Time &getEnd() const { return scale.getEnd(); };
143  QSet<QUrl> urls();
144  const QList<LibDLS::Job::Message> & getMessages() const {return messages;};
145  const QString &getMessageFilter() const { return messageFilter; }
146 
147  struct ChannelInfo {
148  QUrl url;
149  unsigned int jobId;
150  unsigned int dirIndex;
151  };
152  QList<ChannelInfo> channelInfo();
153 
154  LibDLS::Time getMeasureTime() const { return measureTime; }
155 
156  enum Interaction {
157  Zoom,
158  Pan,
159  Measure
160  };
161 
162  enum NamedRange {
163  Today,
164  Yesterday,
165  ThisWeek,
166  LastWeek,
167  ThisMonth,
168  LastMonth,
169  ThisYear,
170  LastYear
171  };
172 
173  QMenu *getMenu() { return &menu; };
174 
175  signals:
176  void logMessage(const QString &);
177 
182  void asyncUpdate();
183 
184  public slots:
185  void previousView();
186  void nextView();
187  void loadData();
188  void zoomIn();
189  void zoomOut();
190  void zoomReset();
191  void setInteraction(Interaction);
192  void setNamedRange(NamedRange);
193  void pan(double);
194  void print();
195  void setShowMessages(bool);
196  void setMessageFilter(const QString &);
197  void clearSections();
198  void showExport();
199 
200  protected:
201  bool event(QEvent *);
202  void mousePressEvent(QMouseEvent *);
203  void mouseReleaseEvent(QMouseEvent *);
204  void mouseMoveEvent(QMouseEvent *);
205  void leaveEvent(QEvent *);
206  void keyPressEvent(QKeyEvent *event);
207  void resizeEvent(QResizeEvent *);
208  void paintEvent(QPaintEvent *);
209  void contextMenuEvent(QContextMenuEvent *);
210  void dragEnterEvent(QDragEnterEvent *);
211  void dragLeaveEvent(QDragLeaveEvent *);
212  void dragMoveEvent(QDragMoveEvent *);
213  void dropEvent(QDropEvent *);
214  void wheelEvent(QWheelEvent *);
215  void retranslate();
216 
217  private:
218  Scale scale;
219  QDir dir;
220  QList<Section *> sections;
221  QReadWriteLock rwLockSections;
222  bool autoRange;
223  QtDls::Model *dropModel;
224  Section *dropSection;
225  int dropLine;
226  int dropRemaining;
227  QPoint startPos;
228  QPoint endPos;
229  LibDLS::Time dragStart;
230  LibDLS::Time dragEnd;
231  bool zooming;
232  Interaction interaction;
233  bool panning;
234  bool measuring;
235  LibDLS::Time measureTime;
236 
237  QThread thread;
238  GraphWorker worker;
239  bool workerBusy;
240  bool reloadPending;
241  int pendingWidth;
242  QSvgRenderer busySvg;
243 
244  QMenu menu;
245  QMenu gotoMenu;
246  QAction fixMeasuringAction;
247  QAction removeMeasuringAction;
248  QAction prevViewAction;
249  QAction nextViewAction;
250  QAction loadDataAction;
251  QAction zoomAction;
252  QAction panAction;
253  QAction measureAction;
254  QAction zoomInAction;
255  QAction zoomOutAction;
256  QAction zoomResetAction;
257  QAction *gotoMenuAction;
258  QAction pickDateAction;
259  QAction gotoTodayAction;
260  QAction gotoYesterdayAction;
261  QAction gotoThisWeekAction;
262  QAction gotoLastWeekAction;
263  QAction gotoThisMonthAction;
264  QAction gotoLastMonthAction;
265  QAction gotoThisYearAction;
266  QAction gotoLastYearAction;
267  QAction sectionPropertiesAction;
268  QAction removeSectionAction;
269  QAction clearSectionsAction;
270  QAction messagesAction;
271  QAction filterAction;
272  QAction printAction;
273  QAction exportAction;
274  Section *selectedSection;
275  const int splitterWidth;
276  Section *splitterSection;
277  Section *movingSection;
278  int startHeight;
279  QScrollBar scrollBar;
280  bool scrollBarNeeded;
281  int scaleWidth;
282 
283  struct View {
284  LibDLS::Time start;
285  LibDLS::Time end;
286  };
287  QList<View> views;
288  QList<View>::iterator currentView;
289 
290  // Message display
291  bool showMessages;
292  int messageAreaHeight;
293  bool mouseOverMsgSplitter;
294  bool movingMsgSplitter;
295  QList<LibDLS::Job::Message> messages;
296  QMutex msgMutex;
297  static QColor messageColor[];
298  static QString messagePixmap[];
299  QString messageFilter;
300 
301  QMutex loggingMutex;
302 
303  int touchX0;
304  bool touchPanning;
305 
306  LibDLS::Time touchT0;
307  LibDLS::Time touchT1;
308  bool touchZooming;
309 
310  QFile debugFile;
311  QString debugString;
312 
313  void updateDragging(QPoint);
314  void resetDragging();
315  void updateCursor();
316  void updateActions();
317  void updateMeasuring();
318  void updateScrollBar();
319  Section *sectionFromPos(const QPoint &);
320  Section *splitterSectionFromPos(const QPoint &);
321  static void drawDropRect(QPainter &, const QRect &);
322  void newView();
323  bool loadSections(const QDomElement &, QtDls::Model *, const QDir &);
324  void drawMessages(QPainter &, const QRect &);
325  static void staticLoggingCallback(const char *, void *);
326  void loggingCallback(const char *);
327  void updateTouch(QTouchEvent *);
328  bool touchPanStart(const QPoint &);
329  void touchPanUpdate(const QTouchEvent::TouchPoint &);
330  void touchZoomStart(int, int);
331  void touchZoomUpdate(int, int);
332  int getDataWidth() const;
333  QSet<QtDls::Channel *> displayedChannels();
334  QList<Section *>::const_iterator lastSectionOnPage(
335  QList<Section *>::const_iterator, int) const;
336  int renderCommon(QPainter &, const QRect &) const;
337  void renderSections(QPainter &, const QRect &,
338  QList<Section *>::const_iterator,
339  QList<Section *>::const_iterator, int, RenderFlags);
340 
341  private slots:
342  void interactionSlot();
343  void removeSelectedSection();
344  void sectionProperties();
345  void sliderValueChanged(int);
346  void pickDate();
347  void gotoDate();
348  void workerFinished();
349  void updateSection(Section *section);
350  void showMessagesChanged();
351  void filterTriggered();
352  void fixMeasuringLine();
353  void removeMeasuringLine();
354 };
355 
356 Q_DECLARE_OPERATORS_FOR_FLAGS(Graph::RenderFlags)
357 
358 /****************************************************************************/
359 
360 } // namespace
361 
362 #endif
DLS Data Directory.
Definition: Dir.h:82
Scale.
Definition: Scale.h:38
Definition: Graph.h:51
Block of data values.
Definition: Data.h:40
Working class hero.
Definition: Graph.h:65
Definition: Channel.h:44
Graph widget.
Definition: Graph.h:104
Datentyp zur Speicherung der Zeit in Mikrosekunden.
Definition: Time.h:46
Definition: Graph.h:56
Graph section.
Definition: Section.h:61