##// END OF EJS Templates
Fixed: datetimeaxis not redrawing when datetime format changed
Marek Rosa -
r2326:96f0cba4f31e
parent child
Show More
@@ -1,120 +1,127
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 QObject::connect(m_axis,SIGNAL(formatChanged(QString)),this, SLOT(handleFormatChanged(QString)));
37 38 }
38 39
39 40 ChartDateTimeAxisX::~ChartDateTimeAxisX()
40 41 {
41 42 }
42 43
43 44 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
44 45 {
45 46 int tickCount = m_axis->tickCount();
46 47
47 48 Q_ASSERT(tickCount >= 2);
48 49
49 50 QVector<qreal> points;
50 51 points.resize(tickCount);
51 52 const QRectF &gridRect = gridGeometry();
52 53 const qreal deltaX = gridRect.width() / (tickCount - 1);
53 54 for (int i = 0; i < tickCount; ++i) {
54 55 int x = i * deltaX + gridRect.left();
55 56 points[i] = x;
56 57 }
57 58 return points;
58 59 }
59 60
60 61 void ChartDateTimeAxisX::updateGeometry()
61 62 {
62 63 const QVector<qreal>& layout = ChartAxis::layout();
63 64 if (layout.isEmpty())
64 65 return;
65 66 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
66 67 HorizontalAxis::updateGeometry();
67 68 }
68 69
69 70 void ChartDateTimeAxisX::handleTickCountChanged(int tick)
70 71 {
71 72 Q_UNUSED(tick)
72 73 if(presenter()) presenter()->layout()->invalidate();
73 74 }
74 75
76 void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
77 {
78 Q_UNUSED(format);
79 if(presenter()) presenter()->layout()->invalidate();
80 }
81
75 82 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
76 83 {
77 84 Q_UNUSED(constraint)
78 85
79 86 QFontMetrics fn(font());
80 87 QSizeF sh;
81 88
82 89 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
83 90 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
84 91 qreal width = 0;
85 92 qreal height = 0;
86 93
87 94 if(ticksList.empty()){
88 95 return sh;
89 96 }
90 97
91 98
92 99 switch (which) {
93 100 case Qt::MinimumSize:{
94 101 int count = qMax(ticksList.last().count(),ticksList.first().count());
95 102 width = fn.averageCharWidth() * count;
96 103 height = fn.height() + labelPadding();
97 104 width = qMax(width,base.width());
98 105 height += base.height();
99 106 sh = QSizeF(width,height);
100 107 break;
101 108 }
102 109 case Qt::PreferredSize: {
103 110 int count = qMax(ticksList.last().count(),ticksList.first().count());
104 111 width=fn.averageCharWidth() * count;
105 112 height=fn.height()+labelPadding();
106 113 width=qMax(width,base.width());
107 114 height+=base.height();
108 115 sh = QSizeF(width,height);
109 116 break;
110 117 }
111 118 default:
112 119 break;
113 120 }
114 121
115 122 return sh;
116 123 }
117 124
118 125 #include "moc_chartdatetimeaxisx_p.cpp"
119 126
120 127 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,61
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTDATETIMEAXISX_H
31 31 #define CHARTDATETIMEAXISX_H
32 32
33 33 #include "horizontalaxis_p.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QDateTimeAxis;
38 38 class ChartPresenter;
39 39
40 40 class ChartDateTimeAxisX : public HorizontalAxis
41 41 {
42 42 Q_OBJECT
43 43 public:
44 44 ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem* item = 0);
45 45 ~ChartDateTimeAxisX();
46 46
47 47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 48 protected:
49 49 QVector<qreal> calculateLayout() const;
50 50 void updateGeometry();
51 51 private Q_SLOTS:
52 52 void handleTickCountChanged(int tick);
53 void handleFormatChanged(const QString &format);
53 54
54 55 private:
55 56 QDateTimeAxis *m_axis;
56 57 };
57 58
58 59 QTCOMMERCIALCHART_END_NAMESPACE
59 60
60 61 #endif /* CHARTDATETIMEAXISX_H */
@@ -1,119 +1,126
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 "chartdatetimeaxisy_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 <QFontMetrics>
27 27 #include <QDateTime>
28 28 #include <qmath.h>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem* item)
33 33 : VerticalAxis(axis, item),
34 34 m_axis(axis)
35 35 {
36 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 38 }
38 39
39 40 ChartDateTimeAxisY::~ChartDateTimeAxisY()
40 41 {
41 42 }
42 43
43 44 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
44 45 {
45 46 int tickCount = m_axis->tickCount();
46 47
47 48 Q_ASSERT(tickCount >= 2);
48 49
49 50 QVector<qreal> points;
50 51 points.resize(tickCount);
51 52 const QRectF &gridRect = gridGeometry();
52 53 const qreal deltaY = gridRect.height() / (tickCount - 1);
53 54 for (int i = 0; i < tickCount; ++i) {
54 55 int y = i * -deltaY + gridRect.bottom();
55 56 points[i] = y;
56 57 }
57 58
58 59 return points;
59 60 }
60 61
61 62 void ChartDateTimeAxisY::updateGeometry()
62 63 {
63 64 const QVector<qreal> &layout = ChartAxis::layout();
64 65 if (layout.isEmpty())
65 66 return;
66 67 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
67 68 VerticalAxis::updateGeometry();
68 69 }
69 70
70 71 void ChartDateTimeAxisY::handleTickCountChanged(int tick)
71 72 {
72 73 Q_UNUSED(tick)
73 74 if(presenter()) presenter()->layout()->invalidate();
74 75 }
75 76
77 void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
78 {
79 Q_UNUSED(format);
80 if(presenter()) presenter()->layout()->invalidate();
81 }
82
76 83 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
77 84 {
78 85 Q_UNUSED(constraint)
79 86
80 87 QFontMetrics fn(font());
81 88 QSizeF sh;
82 89
83 90 QSizeF base = VerticalAxis::sizeHint(which, constraint);
84 91 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
85 92 qreal width = 0;
86 93 qreal height = 0;
87 94
88 95 if(ticksList.empty()){
89 96 return sh;
90 97 }
91 98
92 99 switch (which) {
93 100 case Qt::MinimumSize: {
94 101 width = fn.boundingRect("...").width() + labelPadding();
95 102 width += base.width();
96 103 height = fn.height();
97 104 height = qMax(height,base.height());
98 105 sh = QSizeF(width,height);
99 106 break;
100 107 }
101 108 case Qt::PreferredSize: {
102 109 int count = qMax(ticksList.first().count() , ticksList.last().count());
103 110 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
104 111 width += base.width();
105 112 height = fn.height() * ticksList.count();
106 113 height = qMax(height,base.height());
107 114 sh = QSizeF(width,height);
108 115 break;
109 116 }
110 117 default:
111 118 break;
112 119 }
113 120
114 121 return sh;
115 122 }
116 123
117 124 #include "moc_chartdatetimeaxisy_p.cpp"
118 125
119 126 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,61
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTDATETIMEAXISY_H
31 31 #define CHARTDATETIMEAXISY_H
32 32
33 33 #include "verticalaxis_p.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QDateTimeAxis;
38 38 class ChartPresenter;
39 39
40 40 class ChartDateTimeAxisY : public VerticalAxis
41 41 {
42 42 Q_OBJECT
43 43 public:
44 44 ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem* item = 0);
45 45 ~ChartDateTimeAxisY();
46 46
47 47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 48 protected:
49 49 QVector<qreal> calculateLayout() const;
50 50 void updateGeometry();
51 51 private Q_SLOTS:
52 52 void handleTickCountChanged(int tick);
53 void handleFormatChanged(const QString &format);
54
53 55 private:
54 56 QDateTimeAxis *m_axis;
55 57 };
56 58
57 59 QTCOMMERCIALCHART_END_NAMESPACE
58 60
59 61 #endif /* CHARTDATETIMEAXISY_H */
General Comments 0
You need to be logged in to leave comments. Login now