src/axis.h Source File

· · ·
axis.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_AXIS_H
27 #define QCP_AXIS_H
28
29 #include "global.h"
30 #include "range.h"
31 #include "layer.h"
32 #include "layout.h"
33 #include "lineending.h"
34
35 class QCPPainter;
36 class QCustomPlot;
37 class QCPAxis;
38 class QCPAxisRect;
39 class QCPAbstractPlottable;
40 class QCPGraph;
41 class QCPAbstractItem;
42
43 class QCP_LIB_DECL QCPGrid :public QCPLayerable
44 {
45 Q_OBJECT
47 Q_PROPERTY(bool subGridVisible READ subGridVisible WRITE setSubGridVisible)
48 Q_PROPERTY(bool antialiasedSubGrid READ antialiasedSubGrid WRITE setAntialiasedSubGrid)
49 Q_PROPERTY(bool antialiasedZeroLine READ antialiasedZeroLine WRITE setAntialiasedZeroLine)
50 Q_PROPERTY(QPen pen READ pen WRITE setPen)
51 Q_PROPERTY(QPen subGridPen READ subGridPen WRITE setSubGridPen)
52 Q_PROPERTY(QPen zeroLinePen READ zeroLinePen WRITE setZeroLinePen)
54 public:
55 QCPGrid(QCPAxis *parentAxis);
56
57 // getters:
58 bool subGridVisible() const { return mSubGridVisible; }
59 bool antialiasedSubGrid() const { return mAntialiasedSubGrid; }
60 bool antialiasedZeroLine() const { return mAntialiasedZeroLine; }
61 QPen pen() const { return mPen; }
62 QPen subGridPen() const { return mSubGridPen; }
63 QPen zeroLinePen() const { return mZeroLinePen; }
64
65 // setters:
66 void setSubGridVisible(bool visible);
67 void setAntialiasedSubGrid(bool enabled);
68 void setAntialiasedZeroLine(bool enabled);
69 void setPen(const QPen &pen);
70 void setSubGridPen(const QPen &pen);
71 void setZeroLinePen(const QPen &pen);
72
73 protected:
74 // property members:
75 bool mSubGridVisible;
76 bool mAntialiasedSubGrid, mAntialiasedZeroLine;
77 QPen mPen, mSubGridPen, mZeroLinePen;
78 // non-property members:
79 QCPAxis *mParentAxis;
80
81 // reimplemented virtual methods:
82 virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
83 virtual void draw(QCPPainter *painter);
84
85 // non-virtual methods:
86 void drawGridLines(QCPPainter *painter) const;
87 void drawSubGridLines(QCPPainter *painter) const;
88
89 friend class QCPAxis;
90 };
91
92
93 class QCP_LIB_DECL QCPAxis : public QCPLayerable
94 {
95 Q_OBJECT
97 Q_PROPERTY(AxisType axisType READ axisType)
98 Q_PROPERTY(QCPAxisRect* axisRect READ axisRect)
99 Q_PROPERTY(ScaleType scaleType READ scaleType WRITE setScaleType)
100 Q_PROPERTY(double scaleLogBase READ scaleLogBase WRITE setScaleLogBase)
101 Q_PROPERTY(QCPRange range READ range WRITE setRange)
102 Q_PROPERTY(bool rangeReversed READ rangeReversed WRITE setRangeReversed)
103 Q_PROPERTY(bool autoTicks READ autoTicks WRITE setAutoTicks)
104 Q_PROPERTY(int autoTickCount READ autoTickCount WRITE setAutoTickCount)
105 Q_PROPERTY(bool autoTickLabels READ autoTickLabels WRITE setAutoTickLabels)
106 Q_PROPERTY(bool autoTickStep READ autoTickStep WRITE setAutoTickStep)
107 Q_PROPERTY(bool autoSubTicks READ autoSubTicks WRITE setAutoSubTicks)
108 Q_PROPERTY(bool ticks READ ticks WRITE setTicks)
109 Q_PROPERTY(bool tickLabels READ tickLabels WRITE setTickLabels)
110 Q_PROPERTY(int tickLabelPadding READ tickLabelPadding WRITE setTickLabelPadding)
111 Q_PROPERTY(LabelType tickLabelType READ tickLabelType WRITE setTickLabelType)
112 Q_PROPERTY(QFont tickLabelFont READ tickLabelFont WRITE setTickLabelFont)
113 Q_PROPERTY(QColor tickLabelColor READ tickLabelColor WRITE setTickLabelColor)
114 Q_PROPERTY(double tickLabelRotation READ tickLabelRotation WRITE setTickLabelRotation)
115 Q_PROPERTY(QString dateTimeFormat READ dateTimeFormat WRITE setDateTimeFormat)
116 Q_PROPERTY(QString numberFormat READ numberFormat WRITE setNumberFormat)
117 Q_PROPERTY(int numberPrecision READ numberPrecision WRITE setNumberPrecision)
118 Q_PROPERTY(double tickStep READ tickStep WRITE setTickStep)
119 Q_PROPERTY(QVector<double> tickVector READ tickVector WRITE setTickVector)
120 Q_PROPERTY(QVector<QString> tickVectorLabels READ tickVectorLabels WRITE setTickVectorLabels)
121 Q_PROPERTY(int tickLengthIn READ tickLengthIn WRITE setTickLengthIn)
122 Q_PROPERTY(int tickLengthOut READ tickLengthOut WRITE setTickLengthOut)
123 Q_PROPERTY(int subTickCount READ subTickCount WRITE setSubTickCount)
124 Q_PROPERTY(int subTickLengthIn READ subTickLengthIn WRITE setSubTickLengthIn)
125 Q_PROPERTY(int subTickLengthOut READ subTickLengthOut WRITE setSubTickLengthOut)
126 Q_PROPERTY(QPen basePen READ basePen WRITE setBasePen)
127 Q_PROPERTY(QPen tickPen READ tickPen WRITE setTickPen)
128 Q_PROPERTY(QPen subTickPen READ subTickPen WRITE setSubTickPen)
129 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont)
130 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor)
131 Q_PROPERTY(QString label READ label WRITE setLabel)
132 Q_PROPERTY(int labelPadding READ labelPadding WRITE setLabelPadding)
133 Q_PROPERTY(int padding READ padding WRITE setPadding)
134 Q_PROPERTY(int offset READ offset WRITE setOffset)
135 Q_PROPERTY(SelectableParts selectedParts READ selectedParts WRITE setSelectedParts)
136 Q_PROPERTY(SelectableParts selectableParts READ selectableParts WRITE setSelectableParts)
137 Q_PROPERTY(QFont selectedTickLabelFont READ selectedTickLabelFont WRITE setSelectedTickLabelFont)
138 Q_PROPERTY(QFont selectedLabelFont READ selectedLabelFont WRITE setSelectedLabelFont)
139 Q_PROPERTY(QColor selectedTickLabelColor READ selectedTickLabelColor WRITE setSelectedTickLabelColor)
140 Q_PROPERTY(QColor selectedLabelColor READ selectedLabelColor WRITE setSelectedLabelColor)
141 Q_PROPERTY(QPen selectedBasePen READ selectedBasePen WRITE setSelectedBasePen)
142 Q_PROPERTY(QPen selectedTickPen READ selectedTickPen WRITE setSelectedTickPen)
143 Q_PROPERTY(QPen selectedSubTickPen READ selectedSubTickPen WRITE setSelectedSubTickPen)
144 Q_PROPERTY(QCPLineEnding lowerEnding READ lowerEnding WRITE setLowerEnding)
145 Q_PROPERTY(QCPLineEnding upperEnding READ upperEnding WRITE setUpperEnding)
146 Q_PROPERTY(QCPGrid* grid READ grid)
148 public:
153 enum AxisType { atLeft = 0x01
154 ,atRight = 0x02
155 ,atTop = 0x04
156 ,atBottom = 0x08
157 };
158 Q_FLAGS(AxisType AxisTypes)
159 Q_DECLARE_FLAGS(AxisTypes, AxisType)
166 enum LabelType { ltNumber
167 ,ltDateTime
168 };
169 Q_ENUMS(LabelType)
174 enum ScaleType { stLinear
175 ,stLogarithmic
176 };
177 Q_ENUMS(ScaleType)
182 enum SelectablePart { spNone = 0
183 ,spAxis = 0x001
184 ,spTickLabels = 0x002
185 ,spAxisLabel = 0x004
186 };
187 Q_FLAGS(SelectablePart SelectableParts)
188 Q_DECLARE_FLAGS(SelectableParts, SelectablePart)
189
190 explicit QCPAxis(QCPAxisRect *parent, AxisType type);
191
192 // getters:
193 AxisType axisType() const { return mAxisType; }
194 QCPAxisRect *axisRect() const { return mAxisRect; }
195 ScaleType scaleType() const { return mScaleType; }
196 double scaleLogBase() const { return mScaleLogBase; }
197 const QCPRange range() const { return mRange; }
198 bool rangeReversed() const { return mRangeReversed; }
199 bool autoTicks() const { return mAutoTicks; }
200 int autoTickCount() const { return mAutoTickCount; }
201 bool autoTickLabels() const { return mAutoTickLabels; }
202 bool autoTickStep() const { return mAutoTickStep; }
203 bool autoSubTicks() const { return mAutoSubTicks; }
204 bool ticks() const { return mTicks; }
205 bool tickLabels() const { return mTickLabels; }
206 int tickLabelPadding() const { return mTickLabelPadding; }
207 LabelType tickLabelType() const { return mTickLabelType; }
208 QFont tickLabelFont() const { return mTickLabelFont; }
209 QColor tickLabelColor() const { return mTickLabelColor; }
210 double tickLabelRotation() const { return mTickLabelRotation; }
211 QString dateTimeFormat() const { return mDateTimeFormat; }
212 Qt::TimeSpec dateTimeSpec() const { return mDateTimeSpec; }
213 QString numberFormat() const;
214 int numberPrecision() const { return mNumberPrecision; }
215 double tickStep() const { return mTickStep; }
216 QVector<double> tickVector() const { return mTickVector; }
217 QVector<QString> tickVectorLabels() const { return mTickVectorLabels; }
218 int tickLengthIn() const { return mTickLengthIn; }
219 int tickLengthOut() const { return mTickLengthOut; }
220 int subTickCount() const { return mSubTickCount; }
221 int subTickLengthIn() const { return mSubTickLengthIn; }
222 int subTickLengthOut() const { return mSubTickLengthOut; }
223 QPen basePen() const { return mBasePen; }
224 QPen tickPen() const { return mTickPen; }
225 QPen subTickPen() const { return mSubTickPen; }
226 QFont labelFont() const { return mLabelFont; }
227 QColor labelColor() const { return mLabelColor; }
228 QString label() const { return mLabel; }
229 int labelPadding() const { return mLabelPadding; }
230 int padding() const { return mPadding; }
231 int offset() const { return mOffset; }
232 SelectableParts selectedParts() const { return mSelectedParts; }
233 SelectableParts selectableParts() const { return mSelectableParts; }
234 QFont selectedTickLabelFont() const { return mSelectedTickLabelFont; }
235 QFont selectedLabelFont() const { return mSelectedLabelFont; }
236 QColor selectedTickLabelColor() const { return mSelectedTickLabelColor; }
237 QColor selectedLabelColor() const { return mSelectedLabelColor; }
238 QPen selectedBasePen() const { return mSelectedBasePen; }
239 QPen selectedTickPen() const { return mSelectedTickPen; }
240 QPen selectedSubTickPen() const { return mSelectedSubTickPen; }
241 QCPLineEnding lowerEnding() const { return mLowerEnding; }
242 QCPLineEnding upperEnding() const { return mUpperEnding; }
243 QCPGrid *grid() const { return mGrid; }
244
245 // setters:
246 void setScaleType(ScaleType type);
247 void setScaleLogBase(double base);
248 Q_SLOT void setRange(const QCPRange &range);
249 void setRange(double lower, double upper);
250 void setRange(double position, double size, Qt::AlignmentFlag alignment);
251 void setRangeLower(double lower);
252 void setRangeUpper(double upper);
253 void setRangeReversed(bool reversed);
254 void setAutoTicks(bool on);
255 void setAutoTickCount(int approximateCount);
256 void setAutoTickLabels(bool on);
257 void setAutoTickStep(bool on);
258 void setAutoSubTicks(bool on);
259 void setTicks(bool show);
260 void setTickLabels(bool show);
261 void setTickLabelPadding(int padding);
262 void setTickLabelType(LabelType type);
263 void setTickLabelFont(const QFont &font);
264 void setTickLabelColor(const QColor &color);
265 void setTickLabelRotation(double degrees);
266 void setDateTimeFormat(const QString &format);
267 void setDateTimeSpec(const Qt::TimeSpec &timeSpec);
268 void setNumberFormat(const QString &formatCode);
269 void setNumberPrecision(int precision);
270 void setTickStep(double step);
271 void setTickVector(const QVector<double> &vec);
272 void setTickVectorLabels(const QVector<QString> &vec);
273 void setTickLength(int inside, int outside=0);
274 void setTickLengthIn(int inside);
275 void setTickLengthOut(int outside);
276 void setSubTickCount(int count);
277 void setSubTickLength(int inside, int outside=0);
278 void setSubTickLengthIn(int inside);
279 void setSubTickLengthOut(int outside);
280 void setBasePen(const QPen &pen);
281 void setTickPen(const QPen &pen);
282 void setSubTickPen(const QPen &pen);
283 void setLabelFont(const QFont &font);
284 void setLabelColor(const QColor &color);
285 void setLabel(const QString &str);
286 void setLabelPadding(int padding);
287 void setPadding(int padding);
288 void setOffset(int offset);
289 void setSelectedTickLabelFont(const QFont &font);
290 void setSelectedLabelFont(const QFont &font);
291 void setSelectedTickLabelColor(const QColor &color);
292 void setSelectedLabelColor(const QColor &color);
293 void setSelectedBasePen(const QPen &pen);
294 void setSelectedTickPen(const QPen &pen);
295 void setSelectedSubTickPen(const QPen &pen);
296 Q_SLOT void setSelectableParts(const QCPAxis::SelectableParts &selectableParts);
297 Q_SLOT void setSelectedParts(const QCPAxis::SelectableParts &selectedParts);
298 void setLowerEnding(const QCPLineEnding &ending);
299 void setUpperEnding(const QCPLineEnding &ending);
300
301 // reimplemented virtual methods:
302 virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
303
304 // non-virtual methods:
305 Qt::Orientation orientation() const { return mOrientation; }
306 void moveRange(double diff);
307 void scaleRange(double factor, double center);
308 void setScaleRatio(const QCPAxis *otherAxis, double ratio=1.0);
309 void rescale(bool onlyVisiblePlottables=false);
310 double pixelToCoord(double value) const;
311 double coordToPixel(double value) const;
312 SelectablePart getPartAt(const QPointF &pos) const;
313 QList<QCPAbstractPlottable*> plottables() const;
314 QList<QCPGraph*> graphs() const;
315 QList<QCPAbstractItem*> items() const;
316
317 static AxisType marginSideToAxisType(QCP::MarginSide side);
318
319 signals:
320 void ticksRequest();
321 void rangeChanged(const QCPRange &newRange);
322 void rangeChanged(const QCPRange &newRange, const QCPRange &oldRange);
323 void selectionChanged(const QCPAxis::SelectableParts &parts);
324
325 protected:
326 struct CachedLabel
327 {
328 QPointF offset;
329 QPixmap pixmap;
330 };
331 struct TickLabelData
332 {
333 QString basePart, expPart;
334 QRect baseBounds, expBounds, totalBounds, rotatedTotalBounds;
335 QFont baseFont, expFont;
336 };
337
338 // property members:
339 // axis base:
340 AxisType mAxisType;
341 QCPAxisRect *mAxisRect;
342 int mOffset, mPadding;
343 Qt::Orientation mOrientation;
344 SelectableParts mSelectableParts, mSelectedParts;
345 QPen mBasePen, mSelectedBasePen;
346 QCPLineEnding mLowerEnding, mUpperEnding;
347 // axis label:
348 int mLabelPadding;
349 QString mLabel;
350 QFont mLabelFont, mSelectedLabelFont;
351 QColor mLabelColor, mSelectedLabelColor;
352 // tick labels:
353 int mTickLabelPadding;
354 bool mTickLabels, mAutoTickLabels;
355 double mTickLabelRotation;
356 LabelType mTickLabelType;
357 QFont mTickLabelFont, mSelectedTickLabelFont;
358 QColor mTickLabelColor, mSelectedTickLabelColor;
359 QString mDateTimeFormat;
360 Qt::TimeSpec mDateTimeSpec;
361 int mNumberPrecision;
362 char mNumberFormatChar;
363 bool mNumberBeautifulPowers;
364 bool mNumberMultiplyCross;
365 // ticks and subticks:
366 bool mTicks;
367 double mTickStep;
368 int mSubTickCount, mAutoTickCount;
369 bool mAutoTicks, mAutoTickStep, mAutoSubTicks;
370 int mTickLengthIn, mTickLengthOut, mSubTickLengthIn, mSubTickLengthOut;
371 QPen mTickPen, mSelectedTickPen;
372 QPen mSubTickPen, mSelectedSubTickPen;
373 // scale and range:
374 QCPRange mRange;
375 bool mRangeReversed;
376 ScaleType mScaleType;
377 double mScaleLogBase, mScaleLogBaseLogInv;
378
379 // non-property members:
380 QCPGrid *mGrid;
381 QCache<QString, CachedLabel> mLabelCache;
382 int mLowestVisibleTick, mHighestVisibleTick;
383 QChar mExponentialChar, mPositiveSignChar;
384 QVector<double> mTickVector;
385 QVector<QString> mTickVectorLabels;
386 QVector<double> mSubTickVector;
387 QRect mAxisSelectionBox, mTickLabelsSelectionBox, mLabelSelectionBox;
388 bool mCachedMarginValid;
389 int mCachedMargin;
390
391 // introduced virtual methods:
392 virtual void setupTickVectors();
393 virtual void generateAutoTicks();
394 virtual int calculateAutoSubTickCount(double tickStep) const;
395 virtual int calculateMargin();
396 // tick label drawing/caching:
397 virtual void placeTickLabel(QCPPainter *painter, double position, int distanceToAxis, const QString &text, QSize *tickLabelsSize);
398 virtual void drawTickLabel(QCPPainter *painter, double x, double y, const TickLabelData &labelData) const;
399 virtual TickLabelData getTickLabelData(const QFont &font, const QString &text) const;
400 virtual QPointF getTickLabelDrawOffset(const TickLabelData &labelData) const;
401 virtual void getMaxTickLabelSize(const QFont &font, const QString &text, QSize *tickLabelsSize) const;
402
403 // reimplemented virtual methods:
404 virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
405 virtual void draw(QCPPainter *painter);
406 virtual QCP::Interaction selectionCategory() const;
407 // events:
408 virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
409 virtual void deselectEvent(bool *selectionStateChanged);
410
411 // non-virtual methods:
412 void visibleTickBounds(int &lowIndex, int &highIndex) const;
413 double baseLog(double value) const;
414 double basePow(double value) const;
415 QPen getBasePen() const;
416 QPen getTickPen() const;
417 QPen getSubTickPen() const;
418 QFont getTickLabelFont() const;
419 QFont getLabelFont() const;
420 QColor getTickLabelColor() const;
421 QColor getLabelColor() const;
422
423 private:
424 Q_DISABLE_COPY(QCPAxis)
425
426 friend class QCustomPlot;
427 friend class QCPGrid;
428 friend class QCPAxisRect;
429 };
430 Q_DECLARE_OPERATORS_FOR_FLAGS(QCPAxis::SelectableParts)
431 Q_DECLARE_OPERATORS_FOR_FLAGS(QCPAxis::AxisTypes)
432 Q_DECLARE_METATYPE(QCPAxis::SelectablePart)
433
434 #endif // QCP_AXIS_H
Generated with Doxygen 1.8.1.2