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