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