src/layout.h Source File

· · ·
layout.h
1 /***************************************************************************
2 ** **
3 ** QCustomPlot, an easy to use, modern plotting widget for Qt **
4 ** Copyright (C) 2011, 2012, 2013 Emanuel Eichhammer **
5 ** **
6 ** This program is free software: you can redistribute it and/or modify **
7 ** it under the terms of the GNU General Public License as published by **
8 ** the Free Software Foundation, either version 3 of the License, or **
9 ** (at your option) any later version. **
10 ** **
11 ** This program is distributed in the hope that it will be useful, **
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14 ** GNU General Public License for more details. **
15 ** **
16 ** You should have received a copy of the GNU General Public License **
17 ** along with this program. If not, see http://www.gnu.org/licenses/. **
18 ** **
19 ****************************************************************************
20 ** Author: Emanuel Eichhammer **
21 ** Website/Contact: http://www.qcustomplot.com/ **
22 ** Date: 04.11.13 **
23 ** Version: 1.1.0 **
24 ****************************************************************************/
25
26 #ifndef QCP_LAYOUT_H
27 #define QCP_LAYOUT_H
28
29 #include "global.h"
30 #include "layer.h"
31
32 class QCPLayout;
33 class QCPLayoutElement;
34 class QCustomPlot;
35
36 class QCP_LIB_DECL QCPMarginGroup : public QObject
37 {
38 Q_OBJECT
39 public:
40 QCPMarginGroup(QCustomPlot *parentPlot);
41 ~QCPMarginGroup();
42
43 // non-virtual methods:
44 QList<QCPLayoutElement*> elements(QCP::MarginSide side) const { return mChildren.value(side); }
45 bool isEmpty() const;
46 void clear();
47
48 protected:
49 // non-property members:
50 QCustomPlot *mParentPlot;
51 QHash<QCP::MarginSide, QList<QCPLayoutElement*> > mChildren;
52
53 // non-virtual methods:
54 int commonMargin(QCP::MarginSide side) const;
55 void addChild(QCP::MarginSide side, QCPLayoutElement *element);
56 void removeChild(QCP::MarginSide side, QCPLayoutElement *element);
57
58 private:
59 Q_DISABLE_COPY(QCPMarginGroup)
60
61 friend class QCPLayoutElement;
62 };
63
64
65 class QCP_LIB_DECL QCPLayoutElement : public QCPLayerable
66 {
67 Q_OBJECT
69 Q_PROPERTY(QCPLayout* layout READ layout)
70 Q_PROPERTY(QRect rect READ rect)
71 Q_PROPERTY(QRect outerRect READ outerRect WRITE setOuterRect)
72 Q_PROPERTY(QMargins margins READ margins WRITE setMargins)
73 Q_PROPERTY(QMargins minimumMargins READ minimumMargins WRITE setMinimumMargins)
74 Q_PROPERTY(QSize minimumSize READ minimumSize WRITE setMinimumSize)
75 Q_PROPERTY(QSize maximumSize READ maximumSize WRITE setMaximumSize)
77 public:
78 explicit QCPLayoutElement(QCustomPlot *parentPlot=0);
79 virtual ~QCPLayoutElement();
80
81 // getters:
82 QCPLayout *layout() const { return mParentLayout; }
83 QRect rect() const { return mRect; }
84 QRect outerRect() const { return mOuterRect; }
85 QMargins margins() const { return mMargins; }
86 QMargins minimumMargins() const { return mMinimumMargins; }
87 QCP::MarginSides autoMargins() const { return mAutoMargins; }
88 QSize minimumSize() const { return mMinimumSize; }
89 QSize maximumSize() const { return mMaximumSize; }
90 QCPMarginGroup *marginGroup(QCP::MarginSide side) const { return mMarginGroups.value(side, (QCPMarginGroup*)0); }
91 QHash<QCP::MarginSide, QCPMarginGroup*> marginGroups() const { return mMarginGroups; }
92
93 // setters:
94 void setOuterRect(const QRect &rect);
95 void setMargins(const QMargins &margins);
96 void setMinimumMargins(const QMargins &margins);
97 void setAutoMargins(QCP::MarginSides sides);
98 void setMinimumSize(const QSize &size);
99 void setMinimumSize(int width, int height);
100 void setMaximumSize(const QSize &size);
101 void setMaximumSize(int width, int height);
102 void setMarginGroup(QCP::MarginSides sides, QCPMarginGroup *group);
103
104 // introduced virtual methods:
105 virtual void update();
106 virtual QSize minimumSizeHint() const;
107 virtual QSize maximumSizeHint() const;
108 virtual QList<QCPLayoutElement*> elements(bool recursive) const;
109
110 // reimplemented virtual methods:
111 virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
112
113 protected:
114 // property members:
115 QCPLayout *mParentLayout;
116 QSize mMinimumSize, mMaximumSize;
117 QRect mRect, mOuterRect;
118 QMargins mMargins, mMinimumMargins;
119 QCP::MarginSides mAutoMargins;
120 QHash<QCP::MarginSide, QCPMarginGroup*> mMarginGroups;
121
122 // introduced virtual methods:
123 virtual int calculateAutoMargin(QCP::MarginSide side);
124 // events:
125 virtual void mousePressEvent(QMouseEvent *event) {Q_UNUSED(event)}
126 virtual void mouseMoveEvent(QMouseEvent *event) {Q_UNUSED(event)}
127 virtual void mouseReleaseEvent(QMouseEvent *event) {Q_UNUSED(event)}
128 virtual void mouseDoubleClickEvent(QMouseEvent *event) {Q_UNUSED(event)}
129 virtual void wheelEvent(QWheelEvent *event) {Q_UNUSED(event)}
130
131 // reimplemented virtual methods:
132 virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const { Q_UNUSED(painter) }
133 virtual void draw(QCPPainter *painter) { Q_UNUSED(painter) }
134 virtual void parentPlotInitialized(QCustomPlot *parentPlot);
135
136 private:
137 Q_DISABLE_COPY(QCPLayoutElement)
138
139 friend class QCustomPlot;
140 friend class QCPLayout;
141 friend class QCPMarginGroup;
142 };
143
144
145 class QCP_LIB_DECL QCPLayout : public QCPLayoutElement
146 {
147 Q_OBJECT
148 public:
149 explicit QCPLayout();
150
151 // reimplemented virtual methods:
152 virtual void update();
153 virtual QList<QCPLayoutElement*> elements(bool recursive) const;
154
155 // introduced virtual methods:
156 virtual int elementCount() const = 0;
157 virtual QCPLayoutElement* elementAt(int index) const = 0;
158 virtual QCPLayoutElement* takeAt(int index) = 0;
159 virtual bool take(QCPLayoutElement* element) = 0;
160 virtual void simplify();
161
162 // non-virtual methods:
163 bool removeAt(int index);
164 bool remove(QCPLayoutElement* element);
165 void clear();
166
167 protected:
168 // introduced virtual methods:
169 virtual void updateLayout();
170
171 // non-virtual methods:
172 void sizeConstraintsChanged() const;
173 void adoptElement(QCPLayoutElement *el);
174 void releaseElement(QCPLayoutElement *el);
175 QVector<int> getSectionSizes(QVector<int> maxSizes, QVector<int> minSizes, QVector<double> stretchFactors, int totalSize) const;
176
177 private:
178 Q_DISABLE_COPY(QCPLayout)
179 friend class QCPLayoutElement;
180 };
181
182
183 class QCP_LIB_DECL QCPLayoutGrid : public QCPLayout
184 {
185 Q_OBJECT
187 Q_PROPERTY(int rowCount READ rowCount)
188 Q_PROPERTY(int columnCount READ columnCount)
189 Q_PROPERTY(QList<double> columnStretchFactors READ columnStretchFactors WRITE setColumnStretchFactors)
190 Q_PROPERTY(QList<double> rowStretchFactors READ rowStretchFactors WRITE setRowStretchFactors)
191 Q_PROPERTY(int columnSpacing READ columnSpacing WRITE setColumnSpacing)
192 Q_PROPERTY(int rowSpacing READ rowSpacing WRITE setRowSpacing)
194 public:
195 explicit QCPLayoutGrid();
196 virtual ~QCPLayoutGrid();
197
198 // getters:
199 int rowCount() const;
200 int columnCount() const;
201 QList<double> columnStretchFactors() const { return mColumnStretchFactors; }
202 QList<double> rowStretchFactors() const { return mRowStretchFactors; }
203 int columnSpacing() const { return mColumnSpacing; }
204 int rowSpacing() const { return mRowSpacing; }
205
206 // setters:
207 void setColumnStretchFactor(int column, double factor);
208 void setColumnStretchFactors(const QList<double> &factors);
209 void setRowStretchFactor(int row, double factor);
210 void setRowStretchFactors(const QList<double> &factors);
211 void setColumnSpacing(int pixels);
212 void setRowSpacing(int pixels);
213
214 // reimplemented virtual methods:
215 virtual void updateLayout();
216 virtual int elementCount() const;
217 virtual QCPLayoutElement* elementAt(int index) const;
218 virtual QCPLayoutElement* takeAt(int index);
219 virtual bool take(QCPLayoutElement* element);
220 virtual QList<QCPLayoutElement*> elements(bool recursive) const;
221 virtual void simplify();
222 virtual QSize minimumSizeHint() const;
223 virtual QSize maximumSizeHint() const;
224
225 // non-virtual methods:
226 QCPLayoutElement *element(int row, int column) const;
227 bool addElement(int row, int column, QCPLayoutElement *element);
228 bool hasElement(int row, int column);
229 void expandTo(int newRowCount, int newColumnCount);
230 void insertRow(int newIndex);
231 void insertColumn(int newIndex);
232
233 protected:
234 // property members:
235 QList<QList<QCPLayoutElement*> > mElements;
236 QList<double> mColumnStretchFactors;
237 QList<double> mRowStretchFactors;
238 int mColumnSpacing, mRowSpacing;
239
240 // non-virtual methods:
241 void getMinimumRowColSizes(QVector<int> *minColWidths, QVector<int> *minRowHeights) const;
242 void getMaximumRowColSizes(QVector<int> *maxColWidths, QVector<int> *maxRowHeights) const;
243
244 private:
245 Q_DISABLE_COPY(QCPLayoutGrid)
246 };
247
248
249 class QCP_LIB_DECL QCPLayoutInset : public QCPLayout
250 {
251 Q_OBJECT
252 public:
256 enum InsetPlacement {ipFree
257 ,ipBorderAligned
258 };
259
260 explicit QCPLayoutInset();
261 virtual ~QCPLayoutInset();
262
263 // getters:
264 InsetPlacement insetPlacement(int index) const;
265 Qt::Alignment insetAlignment(int index) const;
266 QRectF insetRect(int index) const;
267
268 // setters:
269 void setInsetPlacement(int index, InsetPlacement placement);
270 void setInsetAlignment(int index, Qt::Alignment alignment);
271 void setInsetRect(int index, const QRectF &rect);
272
273 // reimplemented virtual methods:
274 virtual void updateLayout();
275 virtual int elementCount() const;
276 virtual QCPLayoutElement* elementAt(int index) const;
277 virtual QCPLayoutElement* takeAt(int index);
278 virtual bool take(QCPLayoutElement* element);
279 virtual void simplify() {}
280 virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
281
282 // non-virtual methods:
283 void addElement(QCPLayoutElement *element, Qt::Alignment alignment);
284 void addElement(QCPLayoutElement *element, const QRectF &rect);
285
286 protected:
287 // property members:
288 QList<QCPLayoutElement*> mElements;
289 QList<InsetPlacement> mInsetPlacement;
290 QList<Qt::Alignment> mInsetAlignment;
291 QList<QRectF> mInsetRect;
292
293 private:
294 Q_DISABLE_COPY(QCPLayoutInset)
295 };
296
297 #endif // QCP_LAYOUT_H
Generated with Doxygen 1.8.1.2