rock_widget_collection  0.1
qwaterfalldisplay.h
Go to the documentation of this file.
1 #ifndef QWATERFALLDISPLAY_H
2 #define QWATERFALLDISPLAY_H
3 
4 #include <QtGui/QFrame>
5 #include <QtGui/QColor>
6 #include <QtGui/QImage>
7 #include <QtCore/QSize>
8 #include <QtCore/QList>
9 #include <QtGui/QPainter>
10 
11 #include <QtDesigner/QDesignerExportWidget>
12 
13 #include <list>
14 #include <vector>
15 
16 /*
17 
18  In General: Pretty straight forward (I think)
19 
20  Remarks:
21  - Data is internally sored as a queue of QVarLengthArrays. This is done purely for performance reasons, so
22  that we don't encoounter some performance issues due to Qts implicit sharing
23  - Currently this class is NOT REENTRANT per se! I had to find that out the hard way.
24  - Drawing is olely done within paintEvent, the data handling is decoupled.
25 
26  */
27 class QDESIGNER_WIDGET_EXPORT QWaterfallDisplay : public QFrame
28 {
29  Q_OBJECT
30  Q_CLASSINFO("Author", "Jan Albiez")
31  Q_PROPERTY(uint rows READ rows WRITE setRows NOTIFY rowsChanged DESIGNABLE true)
32  Q_PROPERTY(uint columns READ columns WRITE setColumns NOTIFY columnsChanged DESIGNABLE true)
33  Q_PROPERTY(Resizestyle resizestyle READ resizestyle WRITE setResizestyle DESIGNABLE true)
34  Q_PROPERTY(QColor backColor READ backColor WRITE setBackColor DESIGNABLE true)
35  Q_PROPERTY(double maxValue READ maxValue WRITE setMaxValue NOTIFY maxChanged DESIGNABLE true)
36  Q_PROPERTY(double minValue READ minValue WRITE setMinValue NOTIFY minChanged DESIGNABLE true)
37  Q_ENUMS(Resizestyle)
38 
39 public:
40  enum Resizestyle {
41  Scale, // Just Scales (could be akward)
42  ScaleAspect, // Scales, keeping the aspect ratio and centers
43  ScaleEven, // Scales only n times where widgetsize > n*rows and then centers for n<1 scales like ScaleAspect
44  Cut, // Just cuts if the widget is smaller and centers if bigger
45  CutMixed // Scales cols even and cuts rows
46  };
47 
48  explicit QWaterfallDisplay(int rows=1, int columns=1, QWidget *parent = 0);
50 
51  QSize minimumSizeHint();
52  QSize sizeHint();
53 
54  // quering the drawing status
55  bool isPaused();
56  bool isDrawing();
57 
58 
59 signals:
60 
61  void rowsChanged(uint);
62  void columnsChanged(uint);
63 
64  void minChanged(double);
65  void maxChanged(double);
66 
67  void drawingPaused();
68  void drawingStarted();
69  void drawing(bool);
70  void paused(bool);
71 
72 
73 public slots:
74 
75  // General Behavior and Display stuff
76 
77  // Setting the Properties columns and rows
78  // Warning: Throws away the current data and start with a blank display!
79  void setColumns(int cols);
80  void setRows(int rows);
81  void setColumnsRows(int cols, int rows); // for conveniance
82  int columns() const;
83  int rows() const;
84 
85  // Changes the resizestyle. Just makes the thing more ugly or nicer
86  void setResizestyle(Resizestyle resizestyle);
87  Resizestyle resizestyle() const;
88 
89  // changes the backgroundcolor -> the area where no waterfalldisplay is within the widget (default == black)
90  void setBackColor(QColor back);
91  QColor backColor() const;
92 
93  // Setting and Getting the properties for max and min values
94  // Warning: forces a repaint of the whole thingy
95  void setMaxValue(double max);
96  void setMinValue(double min);
97  void setMaxMinValue(double max, double min); // for conveniance
98  double maxValue() const;
99  double minValue() const;
100 
101 
102  // Data Handling
103 
104  // Setting new Data
105  // These functions push a new data set into the display queue. If the cueue is longer than rows, the last
106  // line will be discarded (FIFO)
107 
108  // The main data setting function
109  // Uses the values in the vector. The vector will be hardcore truncated to the current columns value,
110  // in the case of the shorter vector, the end of the line will be filled with the background color
111  // while drawing. The vector is copied.
112  void pushData(const std::vector<float> &new_data);
113 
114  // the following functions are implemented for onvenience (expand if you like).
115  // They all follow the same principle: Transform the input into a std::vector and then call
116  // pushData(std::vector<float>)
117 
118  // Uses the values in the double array. the new_data array has elements elements
119  // The data will be copied.
120  void pushDataFloat(const float *new_data, int elements);
121 
122  // Uses the values in the list. The rules of pushData(std::vector<float>) apply here as well
123  void pushDataQList(const QList<float> &new_data);
124 
125  // Interprets the data in the char* as uint_8. new_data has the length (memory) of the parameter lenthg
126  // The data will be copied.
127  void pushDataUint8(const char *new_data, int length);
128 
129  // Interprets the data in the char* as uint_16. new_data has the length (memory) of the parameter lenthg
130  // The data will be copied.
131  void pushDataUint16(const char *new_data, int length);
132 
133  // Interprets the data in the char* as uint_32. new_data has the length (memory) of the parameter lenthg
134  // The data will be copied.
135  void pushDataUint32(const char *new_data, int length);
136 
137 
138  // clear the data
139  void clearData();
140 
141 
142  // Drawing stuff
143 
144  // redraws the complete display, using the current data
145  void redrawData();
146 
147  // pauses the drawing of new data
148  // emits:
149  // - drawingPaused() when stopped
150  // - drawigStarted() when started again
151  // - drawing(bool), paused(bool) when toggeled
152  void pauseDrawing(bool onoff);
153 
154 protected:
155  void paintEvent(QPaintEvent *event);
156  void resizeEvent(QResizeEvent *event);
157 
158 private:
159 
160  // Functions
161 
162  // (re)init the datastorage, you should force a redraw after calling this function!
163  void initData(uint cols, uint rows);
164 
165  // draws one line to the image
166  void drawLine(QRgb *line, const std::vector<float> &data_line);
167 
168  // calculates the size hint variables and calls updateGeometry
169  void calcSizeHint();
170 
171  // Members
172  uint m_cols, m_rows;
173 
174  Resizestyle m_resizes; // internal storage resize style
175 
176  QColor back_color; // internal storage of the back_color
177 
178  double m_max, m_min; // internal storage for max and min
179 
180  int dirty_lines; // used to signal paintEvent the lines to change (quick draw)
181  bool widget_resized; // flag from resizeEvent to paintEvent
182  bool redraw_all; // flag to force a complete redraw
183 
184  bool is_drawing; // flag for storing the drawing state
185 
186  // geometry handling
187  QSize m_size_hint;
188  QSize m_minimum_size;
189 
190  // the image buffers
191  QImage m_buffer; // stores the widget state for quick repainting when no data has changed
192  QImage waterfall; // stores the unscaled waterfall
193 
194  std::list< std::vector<float> > data;
195 };
196 
197 #endif // QWATERFALLDISPLAY_H