1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4// W A R N I N G
5// -------------
6//
7// This file is not part of the Qt Chart API. It exists purely as an
8// implementation detail. This header file may change from version to
9// version without notice, or even be removed.
10//
11// We mean it.
12
13#ifndef CHARTAXISELEMENT_H
14#define CHARTAXISELEMENT_H
15
16#include <QtCharts/QChartGlobal>
17#include <QtCharts/private/qchartglobal_p.h>
18#include <private/chartelement_p.h>
19#include <private/axisanimation_p.h>
20#include <private/datetimeaxislabel_p.h>
21#include <private/valueaxislabel_p.h>
22#include <QtWidgets/QGraphicsItem>
23#include <QtWidgets/QGraphicsLayoutItem>
24#include <QtCharts/qdatetimeaxis.h>
25#include <QtCharts/QValueAxis>
26#include <QtGui/QFont>
27
28QT_BEGIN_NAMESPACE
29
30class ChartPresenter;
31class QAbstractAxis;
32
33class Q_CHARTS_PRIVATE_EXPORT ChartAxisElement : public ChartElement, public QGraphicsLayoutItem
34{
35 Q_OBJECT
36
37 using QGraphicsLayoutItem::setGeometry;
38public:
39 ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis = false);
40 ~ChartAxisElement();
41
42 virtual QRectF gridGeometry() const = 0;
43 virtual void setGeometry(const QRectF &axis, const QRectF &grid) = 0;
44 virtual bool emptyAxis() const = 0;
45
46 void setAnimation(AxisAnimation *animation) { m_animation = animation; }
47 AxisAnimation *animation() const override { return m_animation; }
48
49 QAbstractAxis *axis() const { return m_axis; }
50 void setLayout(const QList<qreal> &layout) { m_layout = layout; }
51 QList<qreal> &layout() { return m_layout; } // Modifiable reference
52 void setDynamicMinorTickLayout(const QList<qreal> &layout)
53 {
54 m_dynamicMinorTickLayout = layout;
55 }
56 QList<qreal> &dynamicMinorTicklayout()
57 {
58 return m_dynamicMinorTickLayout;
59 } // Modifiable reference
60 inline qreal labelPadding() const { return qreal(4.0); }
61 inline qreal titlePadding() const { return qreal(2.0); }
62 inline qreal colorScalePadding() const { return qreal { 8.0 }; }
63 void setLabels(const QStringList &labels) { m_labelsList = labels; }
64 QStringList labels() const { return m_labelsList; }
65
66 qreal min() const;
67 qreal max() const;
68
69 qreal tickInterval() const;
70 qreal tickAnchor() const;
71
72 QRectF axisGeometry() const { return m_axisRect; }
73 void setAxisGeometry(const QRectF &axisGeometry) { m_axisRect = axisGeometry; }
74
75 void axisSelected();
76
77 //this flag indicates that axis is used to show intervals it means labels are in between ticks
78 bool intervalAxis() const { return m_intervalAxis; }
79
80 QStringList createValueLabels(qreal max, qreal min, int ticks,
81 qreal tickInterval, qreal tickAnchor,
82 QValueAxis::TickType tickType, const QString &format) const;
83 QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks,
84 const QString &format) const;
85 QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format) const;
86 QStringList createColorLabels(qreal min, qreal max, int ticks) const;
87
88 // from QGraphicsLayoutItem
89 QRectF boundingRect() const override
90 {
91 return QRectF();
92 }
93
94 // from QGraphicsLayoutItem
95 void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override
96 {
97 }
98
99 bool labelsEditable() const;
100 void setLabelsEditable(bool labelsEditable);
101 bool labelsVisible() const;
102
103protected:
104 virtual QList<qreal> calculateLayout() const = 0;
105 virtual void updateLayout(const QList<qreal> &layout) = 0;
106
107 QList<QGraphicsItem *> gridItems() { return m_grid->childItems(); }
108 QList<QGraphicsItem *> minorGridItems() { return m_minorGrid->childItems(); }
109 QList<QGraphicsItem *> labelItems() { return m_labels->childItems(); }
110 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems(); }
111 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems(); }
112 QList<QGraphicsItem *> minorArrowItems() { return m_minorArrow->childItems(); }
113 QGraphicsTextItem *titleItem() const { return m_title.data(); }
114 QGraphicsPixmapItem *colorScaleItem() const { return m_colorScale.get(); }
115 QGraphicsItemGroup *gridGroup() { return m_grid.data(); }
116 QGraphicsItemGroup *minorGridGroup() { return m_minorGrid.data(); }
117 QGraphicsItemGroup *labelGroup() { return m_labels.data(); }
118 QGraphicsItemGroup *shadeGroup() { return m_shades.data(); }
119 QGraphicsItemGroup *arrowGroup() { return m_arrow.data(); }
120 QGraphicsItemGroup *minorArrowGroup() { return m_minorArrow.data(); }
121
122 void prepareColorScale(const qreal width, const qreal height);
123
124public Q_SLOTS:
125 void handleVisibleChanged(bool visible);
126 void handleArrowVisibleChanged(bool visible);
127 void handleGridVisibleChanged(bool visible);
128 void handleLabelsVisibleChanged(bool visible);
129 void handleShadesVisibleChanged(bool visible);
130 void handleLabelsAngleChanged(int angle);
131 virtual void handleShadesBrushChanged(const QBrush &brush) = 0;
132 virtual void handleShadesPenChanged(const QPen &pen) = 0;
133 virtual void handleArrowPenChanged(const QPen &pen) = 0;
134 virtual void handleGridPenChanged(const QPen &pen) = 0;
135 virtual void handleMinorArrowPenChanged(const QPen &pen) = 0;
136 virtual void handleMinorGridPenChanged(const QPen &pen) = 0;
137 virtual void handleMinorGridLineColorChanged(const QColor &color) = 0;
138 virtual void handleGridLineColorChanged(const QColor &color) = 0;
139 void handleLabelsBrushChanged(const QBrush &brush);
140 void handleLabelsFontChanged(const QFont &font);
141 void handleTitleBrushChanged(const QBrush &brush);
142 void handleTitleFontChanged(const QFont &font);
143 void handleTitleTextChanged(const QString &title);
144 void handleTitleVisibleChanged(bool visible);
145 void handleRangeChanged(qreal min, qreal max);
146 void handleReverseChanged(bool reverse);
147 void handleMinorArrowVisibleChanged(bool visible);
148 void handleMinorGridVisibleChanged(bool visible);
149 void handleLabelsPositionChanged();
150 void handleTruncateLabelsChanged();
151 void handleColorScaleSizeChanged();
152 void handleColorScaleGradientChanged();
153 void valueLabelEdited(qreal oldValue, qreal newValue);
154 void dateTimeLabelEdited(const QDateTime &oldTime, const QDateTime &newTime);
155
156Q_SIGNALS:
157 void clicked();
158
159private:
160 void connectSlots();
161 QString formatLabel(const QString &formatSpec, const QByteArray &array,
162 qreal value, int precision, const QString &preStr,
163 const QString &postStr) const;
164
165 QAbstractAxis *m_axis;
166 AxisAnimation *m_animation;
167 QList<qreal> m_layout;
168 QList<qreal> m_dynamicMinorTickLayout;
169 QStringList m_labelsList;
170 QRectF m_axisRect;
171 QScopedPointer<QGraphicsItemGroup> m_grid;
172 QScopedPointer<QGraphicsItemGroup> m_arrow;
173 QScopedPointer<QGraphicsItemGroup> m_minorGrid;
174 QScopedPointer<QGraphicsItemGroup> m_minorArrow;
175 QScopedPointer<QGraphicsItemGroup> m_shades;
176 QScopedPointer<QGraphicsItemGroup> m_labels;
177 QScopedPointer<QGraphicsTextItem> m_title;
178 std::unique_ptr<QGraphicsPixmapItem> m_colorScale;
179 bool m_intervalAxis;
180 bool m_labelsEditable = false;
181};
182
183QT_END_NAMESPACE
184
185#endif /* CHARTAXISELEMENT_H */
186

source code of qtcharts/src/charts/axis/chartaxiselement_p.h