##// END OF EJS Templates
Change sizeHint of vertical axis, caculate width based on each label
Michal Klocek -
r2336:03ff7a7fe0a8
parent child
Show More
@@ -1,129 +1,128
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 73 QGraphicsLayoutItem::updateGeometry();
74 74 if(presenter()) presenter()->layout()->invalidate();
75 75 }
76 76
77 77 void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
78 78 {
79 79 Q_UNUSED(format);
80 80 QGraphicsLayoutItem::updateGeometry();
81 81 if(presenter()) presenter()->layout()->invalidate();
82 82 }
83 83
84 84 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 85 {
86 86 Q_UNUSED(constraint)
87 87
88 88 QFontMetrics fn(font());
89 89 QSizeF sh;
90 90
91 91 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
92 92 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
93 93 qreal width = 0;
94 94 qreal height = 0;
95 95
96 96 if(ticksList.empty()){
97 97 return sh;
98 98 }
99 99
100
101 100 switch (which) {
102 101 case Qt::MinimumSize:{
103 102 int count = qMax(ticksList.last().count(),ticksList.first().count());
104 103 width = fn.averageCharWidth() * count;
105 104 height = fn.height() + labelPadding();
106 105 width = qMax(width,base.width());
107 106 height += base.height();
108 107 sh = QSizeF(width,height);
109 108 break;
110 109 }
111 110 case Qt::PreferredSize: {
112 111 int count = qMax(ticksList.last().count(),ticksList.first().count());
113 112 width=fn.averageCharWidth() * count;
114 113 height=fn.height()+labelPadding();
115 114 width=qMax(width,base.width());
116 115 height+=base.height();
117 116 sh = QSizeF(width,height);
118 117 break;
119 118 }
120 119 default:
121 120 break;
122 121 }
123 122
124 123 return sh;
125 124 }
126 125
127 126 #include "moc_chartdatetimeaxisx_p.cpp"
128 127
129 128 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,128 +1,135
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 37 QObject::connect(m_axis,SIGNAL(formatChanged(QString)),this, SLOT(handleFormatChanged(QString)));
38 38 }
39 39
40 40 ChartDateTimeAxisY::~ChartDateTimeAxisY()
41 41 {
42 42 }
43 43
44 44 QVector<qreal> ChartDateTimeAxisY::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 deltaY = gridRect.height() / (tickCount - 1);
54 54 for (int i = 0; i < tickCount; ++i) {
55 55 int y = i * -deltaY + gridRect.bottom();
56 56 points[i] = y;
57 57 }
58 58
59 59 return points;
60 60 }
61 61
62 62 void ChartDateTimeAxisY::updateGeometry()
63 63 {
64 64 const QVector<qreal> &layout = ChartAxis::layout();
65 65 if (layout.isEmpty())
66 66 return;
67 67 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
68 68 VerticalAxis::updateGeometry();
69 69 }
70 70
71 71 void ChartDateTimeAxisY::handleTickCountChanged(int tick)
72 72 {
73 73 Q_UNUSED(tick)
74 74 QGraphicsLayoutItem::updateGeometry();
75 75 if(presenter()) presenter()->layout()->invalidate();
76 76 }
77 77
78 78 void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
79 79 {
80 80 Q_UNUSED(format);
81 81 QGraphicsLayoutItem::updateGeometry();
82 82 if(presenter()) presenter()->layout()->invalidate();
83 83 }
84 84
85 85 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 86 {
87 87 Q_UNUSED(constraint)
88 88
89 89 QFontMetrics fn(font());
90 90 QSizeF sh;
91 91
92 92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
93 93 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
94 94 qreal width = 0;
95 95 qreal height = 0;
96 96
97
97 98 if(ticksList.empty()){
98 99 return sh;
99 100 }
100 101
102 int labelWidth = 0;
103
104 foreach(const QString& s, ticksList)
105 {
106 labelWidth=qMax(fn.width(s),labelWidth);
107 }
108
101 109 switch (which) {
102 110 case Qt::MinimumSize: {
103 111 width = fn.boundingRect("...").width() + labelPadding();
104 112 width += base.width();
105 113 height = fn.height();
106 114 height = qMax(height,base.height());
107 115 sh = QSizeF(width,height);
108 116 break;
109 117 }
110 118 case Qt::PreferredSize: {
111 int count = qMax(ticksList.first().count() , ticksList.last().count());
112 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
119 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
113 120 width += base.width();
114 121 height = fn.height() * ticksList.count();
115 122 height = qMax(height,base.height());
116 123 sh = QSizeF(width,height);
117 124 break;
118 125 }
119 126 default:
120 127 break;
121 128 }
122 129
123 130 return sh;
124 131 }
125 132
126 133 #include "moc_chartdatetimeaxisy_p.cpp"
127 134
128 135 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,130 +1,131
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 "chartvalueaxisy_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "qvalueaxis.h"
25 25 #include "chartlayout_p.h"
26 26 #include <QGraphicsLayout>
27 27 #include <QFontMetrics>
28 28 #include <qmath.h>
29 29 #include <QDebug>
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, QGraphicsItem* item)
34 34 : VerticalAxis(axis, item),
35 35 m_axis(axis)
36 36 {
37 37 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
38 38 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
39 39 }
40 40
41 41 ChartValueAxisY::~ChartValueAxisY()
42 42 {
43 43 }
44 44
45 45 QVector<qreal> ChartValueAxisY::calculateLayout() const
46 46 {
47 47 int tickCount = m_axis->tickCount();
48 48
49 49 Q_ASSERT(tickCount >= 2);
50 50
51 51 QVector<qreal> points;
52 52 points.resize(tickCount);
53 53
54 54 const QRectF &gridRect = gridGeometry();
55 55
56 56 const qreal deltaY = gridRect.height() / (tickCount - 1);
57 57 for (int i = 0; i < tickCount; ++i) {
58 58 points[i] = i * -deltaY + gridRect.bottom();
59 59 }
60 60
61 61 return points;
62 62 }
63 63
64 64 void ChartValueAxisY::updateGeometry()
65 65 {
66 66 const QVector<qreal> &layout = ChartAxis::layout();
67 67 if (layout.isEmpty())
68 68 return;
69 69 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
70 70 VerticalAxis::updateGeometry();
71 71 }
72 72
73 73 void ChartValueAxisY::handleTickCountChanged(int tick)
74 74 {
75 75 Q_UNUSED(tick);
76 76 QGraphicsLayoutItem::updateGeometry();
77 77 if(presenter()) presenter()->layout()->invalidate();
78 78 }
79 79
80 80 void ChartValueAxisY::handleLabelFormatChanged(const QString &format)
81 81 {
82 82 Q_UNUSED(format);
83 83 QGraphicsLayoutItem::updateGeometry();
84 84 if(presenter()) presenter()->layout()->invalidate();
85 85 }
86 86
87 87 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
88 88 {
89 89 Q_UNUSED(constraint)
90 90
91 91 QFontMetrics fn(font());
92 92 QSizeF sh;
93 93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
95 95 qreal width = 0;
96 96 qreal height = 0;
97 97
98 int count = 1;
98 int labelWidth = 0;
99 99
100 if(!ticksList.empty()){
101 count = qMax(ticksList.last().count(),ticksList.first().count());
100 foreach(const QString& s, ticksList)
101 {
102 labelWidth=qMax(fn.width(s),labelWidth);
102 103 }
103 104
104 105 switch (which) {
105 106 case Qt::MinimumSize: {
106 107 width = fn.boundingRect("...").width() + labelPadding();
107 108 width += base.width();
108 109 height = fn.height();
109 110 height = qMax(height,base.height());
110 111 sh = QSizeF(width,height);
111 112 break;
112 113 }
113 114 case Qt::PreferredSize:
114 115 {
115 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
116 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
116 117 width += base.width();
117 118 height = fn.height() * ticksList.count();
118 119 height = qMax(height,base.height());
119 120 sh = QSizeF(width,height);
120 121 break;
121 122 }
122 123 default:
123 124 break;
124 125 }
125 126 return sh;
126 127 }
127 128
128 129 #include "moc_chartvalueaxisy_p.cpp"
129 130
130 131 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now