@@ -1,138 +1,138 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2014 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.io |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Charts module. |
|
8 | 8 | ** |
|
9 | 9 | ** Licensees holding valid commercial license for Qt may use this file in |
|
10 | 10 | ** accordance with the Qt License Agreement provided with the Software |
|
11 | 11 | ** or, alternatively, in accordance with the terms contained in a written |
|
12 | 12 | ** agreement between you and Digia. |
|
13 | 13 | ** |
|
14 | 14 | ** If you have questions regarding the use of this file, please use |
|
15 | 15 | ** contact form at http://qt.io |
|
16 | 16 | ** |
|
17 | 17 | ****************************************************************************/ |
|
18 | 18 | |
|
19 | 19 | #include <private/chartbarcategoryaxisx_p.h> |
|
20 | 20 | #include <private/chartpresenter_p.h> |
|
21 | 21 | #include <private/qbarcategoryaxis_p.h> |
|
22 | 22 | #include <private/abstractchartlayout_p.h> |
|
23 | 23 | #include <QtCore/QDebug> |
|
24 | 24 | #include <QtCore/QtMath> |
|
25 | 25 | |
|
26 | 26 | QT_CHARTS_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item) |
|
29 | 29 | : HorizontalAxis(axis, item, true), |
|
30 | 30 | m_categoriesAxis(axis) |
|
31 | 31 | { |
|
32 | 32 | QObject::connect(m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged())); |
|
33 | 33 | handleCategoriesChanged(); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | ChartBarCategoryAxisX::~ChartBarCategoryAxisX() |
|
37 | 37 | { |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const |
|
41 | 41 | { |
|
42 | 42 | QVector<qreal> points; |
|
43 | 43 | const QRectF& gridRect = gridGeometry(); |
|
44 | 44 | qreal range = max() - min(); |
|
45 | 45 | const qreal delta = gridRect.width() / range; |
|
46 | 46 | |
|
47 | 47 | if (delta < 2) |
|
48 | 48 | return points; |
|
49 | 49 | |
|
50 | 50 | qreal adjustedMin = min() + 0.5; |
|
51 | 51 | qreal offset = (ceil(adjustedMin) - adjustedMin) * delta; |
|
52 | 52 | |
|
53 | 53 | int count = qFloor(range); |
|
54 | 54 | if (count < 1) |
|
55 | 55 | return points; |
|
56 | 56 | |
|
57 | 57 | points.resize(count + 2); |
|
58 | 58 | |
|
59 | 59 | for (int i = 0; i < count + 2; ++i) |
|
60 | 60 | points[i] = offset + (qreal(i) * delta) + gridRect.left(); |
|
61 | 61 | |
|
62 | 62 | return points; |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const |
|
66 | 66 | { |
|
67 | 67 | QStringList result ; |
|
68 | 68 | const QRectF &gridRect = gridGeometry(); |
|
69 | 69 | qreal d = (max() - min()) / gridRect.width(); |
|
70 | 70 | |
|
71 | 71 | for (int i = 0; i < layout.count() - 1; ++i) { |
|
72 | 72 | qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5)); |
|
73 | if (x < max() && (x >= 0)) { | |
|
73 | if (x < max() && (x >= 0) && x < m_categoriesAxis->categories().count()) { | |
|
74 | 74 | result << m_categoriesAxis->categories().at(x); |
|
75 | 75 | } else { |
|
76 | 76 | // No label for x coordinate |
|
77 | 77 | result << QString(); |
|
78 | 78 | } |
|
79 | 79 | } |
|
80 | 80 | result << QString(); |
|
81 | 81 | return result; |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | void ChartBarCategoryAxisX::updateGeometry() |
|
86 | 86 | { |
|
87 | 87 | const QVector<qreal>& layout = ChartAxisElement::layout(); |
|
88 | 88 | if (layout.isEmpty()) |
|
89 | 89 | return; |
|
90 | 90 | setLabels(createCategoryLabels(layout)); |
|
91 | 91 | HorizontalAxis::updateGeometry(); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void ChartBarCategoryAxisX::handleCategoriesChanged() |
|
95 | 95 | { |
|
96 | 96 | QGraphicsLayoutItem::updateGeometry(); |
|
97 | 97 | if(presenter()) presenter()->layout()->invalidate(); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
101 | 101 | { |
|
102 | 102 | Q_UNUSED(constraint) |
|
103 | 103 | |
|
104 | 104 | QSizeF sh; |
|
105 | 105 | QSizeF base = HorizontalAxis::sizeHint(which, constraint); |
|
106 | 106 | QStringList ticksList = m_categoriesAxis->categories(); |
|
107 | 107 | |
|
108 | 108 | qreal width = 0; // Width is irrelevant for X axes with interval labels |
|
109 | 109 | qreal height = 0; |
|
110 | 110 | |
|
111 | 111 | switch (which) { |
|
112 | 112 | case Qt::MinimumSize: { |
|
113 | 113 | QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), |
|
114 | 114 | QStringLiteral("..."), |
|
115 | 115 | axis()->labelsAngle()); |
|
116 | 116 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; |
|
117 | 117 | sh = QSizeF(width, height); |
|
118 | 118 | break; |
|
119 | 119 | } |
|
120 | 120 | case Qt::PreferredSize:{ |
|
121 | 121 | qreal labelHeight = 0.0; |
|
122 | 122 | foreach (const QString& s, ticksList) { |
|
123 | 123 | QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle()); |
|
124 | 124 | labelHeight = qMax(rect.height(), labelHeight); |
|
125 | 125 | } |
|
126 | 126 | height = labelHeight + labelPadding() + base.height() + 1.0; |
|
127 | 127 | sh = QSizeF(width, height); |
|
128 | 128 | break; |
|
129 | 129 | } |
|
130 | 130 | default: |
|
131 | 131 | break; |
|
132 | 132 | } |
|
133 | 133 | return sh; |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | #include "moc_chartbarcategoryaxisx_p.cpp" |
|
137 | 137 | |
|
138 | 138 | QT_CHARTS_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now