1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <private/chartbarcategoryaxisx_p.h> |
31 | #include <private/chartpresenter_p.h> |
32 | #include <private/qbarcategoryaxis_p.h> |
33 | #include <private/abstractchartlayout_p.h> |
34 | #include <QtCore/QDebug> |
35 | #include <QtCore/QtMath> |
36 | |
37 | QT_CHARTS_BEGIN_NAMESPACE |
38 | |
39 | ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item) |
40 | : HorizontalAxis(axis, item, true), |
41 | m_categoriesAxis(axis) |
42 | { |
43 | QObject::connect(sender: m_categoriesAxis,SIGNAL(categoriesChanged()),receiver: this, SLOT(handleCategoriesChanged())); |
44 | handleCategoriesChanged(); |
45 | } |
46 | |
47 | ChartBarCategoryAxisX::~ChartBarCategoryAxisX() |
48 | { |
49 | } |
50 | |
51 | QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const |
52 | { |
53 | QVector<qreal> points; |
54 | const QRectF& gridRect = gridGeometry(); |
55 | qreal range = max() - min(); |
56 | const qreal delta = gridRect.width() / range; |
57 | |
58 | if (delta < 2) |
59 | return points; |
60 | |
61 | qreal adjustedMin = min() + 0.5; |
62 | qreal offset = (qRound(d: adjustedMin) - adjustedMin) * delta; |
63 | |
64 | int count = qFloor(v: range); |
65 | if (count < 1) |
66 | return points; |
67 | |
68 | points.resize(size: count + 2); |
69 | |
70 | for (int i = 0; i < count + 2; ++i) |
71 | points[i] = offset + (qreal(i) * delta) + gridRect.left(); |
72 | |
73 | return points; |
74 | } |
75 | |
76 | QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const |
77 | { |
78 | QStringList result ; |
79 | const QRectF &gridRect = gridGeometry(); |
80 | qreal d = (max() - min()) / gridRect.width(); |
81 | |
82 | for (int i = 0; i < layout.count() - 1; ++i) { |
83 | int x = qFloor(v: (((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5)); |
84 | if (x < max() && (x >= 0) && x < m_categoriesAxis->categories().count()) { |
85 | result << m_categoriesAxis->categories().at(i: x); |
86 | } else { |
87 | // No label for x coordinate |
88 | result << QString(); |
89 | } |
90 | } |
91 | result << QString(); |
92 | return result; |
93 | } |
94 | |
95 | |
96 | void ChartBarCategoryAxisX::updateGeometry() |
97 | { |
98 | const QVector<qreal>& layout = ChartAxisElement::layout(); |
99 | if (layout.isEmpty()) |
100 | return; |
101 | setLabels(createCategoryLabels(layout)); |
102 | HorizontalAxis::updateGeometry(); |
103 | } |
104 | |
105 | void ChartBarCategoryAxisX::handleCategoriesChanged() |
106 | { |
107 | QGraphicsLayoutItem::updateGeometry(); |
108 | if(presenter()) presenter()->layout()->invalidate(); |
109 | } |
110 | |
111 | QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
112 | { |
113 | Q_UNUSED(constraint) |
114 | |
115 | QSizeF sh; |
116 | QSizeF base = HorizontalAxis::sizeHint(which, constraint); |
117 | QStringList ticksList = m_categoriesAxis->categories(); |
118 | |
119 | qreal width = 0; // Width is irrelevant for X axes with interval labels |
120 | qreal height = 0; |
121 | |
122 | switch (which) { |
123 | case Qt::MinimumSize: { |
124 | QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), |
125 | QStringLiteral("..." ), |
126 | angle: axis()->labelsAngle()); |
127 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; |
128 | sh = QSizeF(width, height); |
129 | break; |
130 | } |
131 | case Qt::PreferredSize:{ |
132 | qreal labelHeight = 0.0; |
133 | foreach (const QString& s, ticksList) { |
134 | QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); |
135 | labelHeight = qMax(a: rect.height(), b: labelHeight); |
136 | } |
137 | height = labelHeight + labelPadding() + base.height() + 1.0; |
138 | sh = QSizeF(width, height); |
139 | break; |
140 | } |
141 | default: |
142 | break; |
143 | } |
144 | return sh; |
145 | } |
146 | |
147 | QT_CHARTS_END_NAMESPACE |
148 | |
149 | #include "moc_chartbarcategoryaxisx_p.cpp" |
150 | |