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