##// END OF EJS Templates
Fix multiline axis label positioning....
Miikka Heikkinen -
r2534:ec1756baf233
parent child
Show More
@@ -22,7 +22,6
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qbarcategoryaxis_p.h"
23 #include "qbarcategoryaxis_p.h"
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <QFontMetrics>
26 #include <QDebug>
25 #include <QDebug>
27 #include <qmath.h>
26 #include <qmath.h>
28
27
@@ -104,7 +103,6 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constra
104 {
103 {
105 Q_UNUSED(constraint)
104 Q_UNUSED(constraint)
106
105
107 QFontMetrics fn(axis()->labelsFont());
108 QSizeF sh;
106 QSizeF sh;
109 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
107 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
110 QStringList ticksList = m_categoriesAxis->categories();
108 QStringList ticksList = m_categoriesAxis->categories();
@@ -114,20 +112,18 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constra
114
112
115 switch (which) {
113 switch (which) {
116 case Qt::MinimumSize: {
114 case Qt::MinimumSize: {
117 QRectF boundingRect = labelBoundingRect(fn, "...");
115 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
118 height = boundingRect.height() + labelPadding();
116 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
119 height += base.height();
120 sh = QSizeF(width, height);
117 sh = QSizeF(width, height);
121 break;
118 break;
122 }
119 }
123 case Qt::PreferredSize:{
120 case Qt::PreferredSize:{
124 int labelHeight = 0;
121 qreal labelHeight = 0.0;
125 foreach (const QString& s, ticksList) {
122 foreach (const QString& s, ticksList) {
126 QRect rect = labelBoundingRect(fn, s);
123 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
127 labelHeight = qMax(rect.height(), labelHeight);
124 labelHeight = qMax(rect.height(), labelHeight);
128 }
125 }
129 height = labelHeight + labelPadding();
126 height = labelHeight + labelPadding() + base.height() + 1.0;
130 height += base.height();
131 sh = QSizeF(width, height);
127 sh = QSizeF(width, height);
132 break;
128 break;
133 }
129 }
@@ -23,7 +23,6
23 #include "qbarcategoryaxis_p.h"
23 #include "qbarcategoryaxis_p.h"
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <qmath.h>
25 #include <qmath.h>
26 #include <QFontMetrics>
27 #include <QDebug>
26 #include <QDebug>
28
27
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -103,7 +102,6 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constra
103 {
102 {
104 Q_UNUSED(constraint)
103 Q_UNUSED(constraint)
105
104
106 QFontMetrics fn(axis()->labelsFont());
107 QSizeF sh;
105 QSizeF sh;
108 QSizeF base = VerticalAxis::sizeHint(which, constraint);
106 QSizeF base = VerticalAxis::sizeHint(which, constraint);
109 QStringList ticksList = m_categoriesAxis->categories();
107 QStringList ticksList = m_categoriesAxis->categories();
@@ -112,23 +110,21 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constra
112
110
113 switch (which) {
111 switch (which) {
114 case Qt::MinimumSize: {
112 case Qt::MinimumSize: {
115 QRectF boundingRect = labelBoundingRect(fn, "...");
113 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
116 width = boundingRect.width() + labelPadding();
114 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
117 width += base.width();
115 if (base.width() > 0.0)
118 if (base.width() > 0)
119 width += labelPadding();
116 width += labelPadding();
120 sh = QSizeF(width, height);
117 sh = QSizeF(width, height);
121 break;
118 break;
122 }
119 }
123 case Qt::PreferredSize:{
120 case Qt::PreferredSize:{
124 int labelWidth = 0;
121 qreal labelWidth = 0.0;
125 foreach (const QString& s, ticksList) {
122 foreach (const QString& s, ticksList) {
126 QRect rect = labelBoundingRect(fn, s);
123 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
127 labelWidth = qMax(rect.width(), labelWidth);
124 labelWidth = qMax(rect.width(), labelWidth);
128 }
125 }
129 width = labelWidth + labelPadding() + 1;
126 width = labelWidth + labelPadding() + base.width() + 1.0;
130 width += base.width();
127 if (base.width() > 0.0)
131 if (base.width() > 0)
132 width += labelPadding();
128 width += labelPadding();
133 sh = QSizeF(width, height);
129 sh = QSizeF(width, height);
134 break;
130 break;
@@ -24,7 +24,6
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "abstractchartlayout_p.h"
25 #include "abstractchartlayout_p.h"
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
28 #include <qmath.h>
27 #include <qmath.h>
29
28
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -78,7 +77,6 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
78 {
77 {
79 Q_UNUSED(constraint)
78 Q_UNUSED(constraint)
80
79
81 QFontMetrics fn(axis()->labelsFont());
82 QSizeF sh;
80 QSizeF sh;
83 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
81 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
84 QStringList ticksList = m_axis->categoriesLabels();
82 QStringList ticksList = m_axis->categoriesLabels();
@@ -87,20 +85,18 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
87
85
88 switch (which) {
86 switch (which) {
89 case Qt::MinimumSize: {
87 case Qt::MinimumSize: {
90 QRectF boundingRect = labelBoundingRect(fn, "...");
88 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
91 height = boundingRect.height() + labelPadding();
89 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
92 height += base.height();
93 sh = QSizeF(width, height);
90 sh = QSizeF(width, height);
94 break;
91 break;
95 }
92 }
96 case Qt::PreferredSize: {
93 case Qt::PreferredSize: {
97 int labelHeight = 0;
94 qreal labelHeight = 0.0;
98 foreach (const QString& s, ticksList) {
95 foreach (const QString& s, ticksList) {
99 QRect rect = labelBoundingRect(fn, s);
96 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
100 labelHeight = qMax(rect.height(), labelHeight);
97 labelHeight = qMax(rect.height(), labelHeight);
101 }
98 }
102 height = labelHeight + labelPadding();
99 height = labelHeight + labelPadding() + base.height() + 1.0;
103 height += base.height();
104 sh = QSizeF(width, height);
100 sh = QSizeF(width, height);
105 break;
101 break;
106 }
102 }
@@ -24,7 +24,6
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "abstractchartlayout_p.h"
25 #include "abstractchartlayout_p.h"
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
28 #include <qmath.h>
27 #include <qmath.h>
29 #include <QDebug>
28 #include <QDebug>
30
29
@@ -78,7 +77,6 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
78 {
77 {
79 Q_UNUSED(constraint)
78 Q_UNUSED(constraint)
80
79
81 QFontMetrics fn(axis()->labelsFont());
82 QSizeF sh;
80 QSizeF sh;
83 QSizeF base = VerticalAxis::sizeHint(which, constraint);
81 QSizeF base = VerticalAxis::sizeHint(which, constraint);
84 QStringList ticksList = m_axis->categoriesLabels();
82 QStringList ticksList = m_axis->categoriesLabels();
@@ -87,20 +85,18 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
87
85
88 switch (which) {
86 switch (which) {
89 case Qt::MinimumSize: {
87 case Qt::MinimumSize: {
90 QRectF boundingRect = labelBoundingRect(fn, "...");
88 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
91 width = boundingRect.width() + labelPadding();
89 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
92 width += base.width();
93 sh = QSizeF(width, height);
90 sh = QSizeF(width, height);
94 break;
91 break;
95 }
92 }
96 case Qt::PreferredSize: {
93 case Qt::PreferredSize: {
97 int labelWidth = 0;
94 qreal labelWidth = 0.0;
98 foreach (const QString& s, ticksList) {
95 foreach (const QString& s, ticksList) {
99 QRect rect = labelBoundingRect(fn, s);
96 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
100 labelWidth = qMax(rect.width(), labelWidth);
97 labelWidth = qMax(rect.width(), labelWidth);
101 }
98 }
102 width = labelWidth + labelPadding() + 1;
99 width = labelWidth + labelPadding() + base.width() + 1.0;
103 width += base.width();
104 sh = QSizeF(width, height);
100 sh = QSizeF(width, height);
105 break;
101 break;
106 }
102 }
@@ -24,10 +24,18
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <qmath.h>
25 #include <qmath.h>
26 #include <QDateTime>
26 #include <QDateTime>
27 #include <QFontMetrics>
28
27
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
29
30 QGraphicsSimpleTextItem *dummyTextItem = 0;
31 class StaticDeleter
32 {
33 public:
34 StaticDeleter() {}
35 ~StaticDeleter() { delete dummyTextItem; }
36 };
37 StaticDeleter staticDeleter;
38
31 ChartAxisElement::ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
39 ChartAxisElement::ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
32 : ChartElement(item),
40 : ChartElement(item),
33 m_axis(axis),
41 m_axis(axis),
@@ -41,6 +49,8 ChartAxisElement::ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, boo
41
49
42 {
50 {
43 //initial initialization
51 //initial initialization
52 if (!dummyTextItem)
53 dummyTextItem = new QGraphicsSimpleTextItem;
44 m_arrow->setHandlesChildEvents(false);
54 m_arrow->setHandlesChildEvents(false);
45 m_arrow->setZValue(ChartPresenter::AxisZValue);
55 m_arrow->setZValue(ChartPresenter::AxisZValue);
46 m_labels->setZValue(ChartPresenter::AxisZValue);
56 m_labels->setZValue(ChartPresenter::AxisZValue);
@@ -184,19 +194,44 void ChartAxisElement::handleVisibleChanged(bool visible)
184 if (presenter()) presenter()->layout()->invalidate();
194 if (presenter()) presenter()->layout()->invalidate();
185 }
195 }
186
196
187 QRect ChartAxisElement::labelBoundingRect(const QFontMetrics &fn, const QString &label) const
197 QRectF ChartAxisElement::textBoundingRect(const QFont &font, const QString &text, qreal angle) const
188 {
198 {
189 QRect boundingRect = fn.boundingRect(label);
199 dummyTextItem->setFont(font);
190 // Take label rotation into account
200 dummyTextItem->setText(text);
191 if (axis()->labelsAngle()) {
201 QRectF boundingRect = dummyTextItem->boundingRect();
202
203 // Take rotation into account
204 if (angle) {
192 QTransform transform;
205 QTransform transform;
193 transform.rotate(axis()->labelsAngle());
206 transform.rotate(angle);
194 boundingRect = transform.mapRect(boundingRect);
207 boundingRect = transform.mapRect(boundingRect);
195 }
208 }
196
209
197 return boundingRect;
210 return boundingRect;
198 }
211 }
199
212
213 // boundingRect parameter returns the rotated bounding rect of the text
214 QString ChartAxisElement::truncatedText(const QFont &font, const QString &text, qreal angle,
215 qreal maxSize, Qt::Orientation constraintOrientation,
216 QRectF &boundingRect) const
217 {
218 QString truncatedString(text);
219 boundingRect = textBoundingRect(font, truncatedString, angle);
220 qreal checkDimension = ((constraintOrientation == Qt::Horizontal)
221 ? boundingRect.width() : boundingRect.height());
222 if (checkDimension > maxSize) {
223 truncatedString.append("...");
224 while (checkDimension > maxSize && truncatedString.length() > 3) {
225 truncatedString.remove(truncatedString.length() - 4, 1);
226 boundingRect = textBoundingRect(font, truncatedString, angle);
227 checkDimension = ((constraintOrientation == Qt::Horizontal)
228 ? boundingRect.width() : boundingRect.height());
229 }
230 }
231
232 return truncatedString;
233 }
234
200 void ChartAxisElement::handleRangeChanged(qreal min, qreal max)
235 void ChartAxisElement::handleRangeChanged(qreal min, qreal max)
201 {
236 {
202 Q_UNUSED(min);
237 Q_UNUSED(min);
@@ -70,7 +70,9 public:
70 QRectF axisGeometry() const { return m_axisRect; }
70 QRectF axisGeometry() const { return m_axisRect; }
71 void setAxisGeometry(const QRectF &axisGeometry) { m_axisRect = axisGeometry; }
71 void setAxisGeometry(const QRectF &axisGeometry) { m_axisRect = axisGeometry; }
72
72
73 QRect labelBoundingRect(const QFontMetrics &fn, const QString &label) const;
73 QRectF textBoundingRect(const QFont &font, const QString &text, qreal angle = 0.0) const;
74 QString truncatedText(const QFont &font, const QString &text, qreal angle, qreal maxSize,
75 Qt::Orientation constraintOrientation, QRectF &boundingRect) const;
74
76
75 void axisSelected();
77 void axisSelected();
76
78
@@ -24,7 +24,6
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QDateTime>
26 #include <QDateTime>
27 #include <QFontMetrics>
28 #include <qmath.h>
27 #include <qmath.h>
29
28
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -85,7 +84,6 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
85 {
84 {
86 Q_UNUSED(constraint)
85 Q_UNUSED(constraint)
87
86
88 QFontMetrics fn(axis()->labelsFont());
89 QSizeF sh;
87 QSizeF sh;
90
88
91 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
89 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
@@ -100,26 +98,24 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
100
98
101 switch (which) {
99 switch (which) {
102 case Qt::MinimumSize: {
100 case Qt::MinimumSize: {
103 QRectF boundingRect = labelBoundingRect(fn, "...");
101 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
104 width = boundingRect.width() / 2.0;
102 width = boundingRect.width() / 2.0;
105 height = boundingRect.height() + labelPadding();
103 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
106 height += base.height();
107 sh = QSizeF(width, height);
104 sh = QSizeF(width, height);
108 break;
105 break;
109 }
106 }
110 case Qt::PreferredSize: {
107 case Qt::PreferredSize: {
111 int labelHeight = 0;
108 qreal labelHeight = 0.0;
112 int firstWidth = -1;
109 qreal firstWidth = -1.0;
113 foreach (const QString& s, ticksList) {
110 foreach (const QString& s, ticksList) {
114 QRect rect = labelBoundingRect(fn, s);
111 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
115 labelHeight = qMax(rect.height(), labelHeight);
112 labelHeight = qMax(rect.height(), labelHeight);
116 width = rect.width();
113 width = rect.width();
117 if (firstWidth < 0)
114 if (firstWidth < 0.0)
118 firstWidth = width;
115 firstWidth = width;
119 }
116 }
120 height = labelHeight + labelPadding();
117 height = labelHeight + labelPadding() + base.height() + 1.0;
121 height += base.height();
118 width = qMax(width, firstWidth) / 2.0;
122 width = qMax(width, qreal(firstWidth)) / 2.0;
123 sh = QSizeF(width, height);
119 sh = QSizeF(width, height);
124 break;
120 break;
125 }
121 }
@@ -23,7 +23,6
23 #include "qdatetimeaxis.h"
23 #include "qdatetimeaxis.h"
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
27 #include <QDateTime>
26 #include <QDateTime>
28 #include <qmath.h>
27 #include <qmath.h>
29
28
@@ -86,7 +85,6 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
86 {
85 {
87 Q_UNUSED(constraint)
86 Q_UNUSED(constraint)
88
87
89 QFontMetrics fn(axis()->labelsFont());
90 QSizeF sh;
88 QSizeF sh;
91
89
92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
90 QSizeF base = VerticalAxis::sizeHint(which, constraint);
@@ -101,26 +99,24 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
101
99
102 switch (which) {
100 switch (which) {
103 case Qt::MinimumSize: {
101 case Qt::MinimumSize: {
104 QRectF boundingRect = labelBoundingRect(fn, "...");
102 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
105 width = boundingRect.width() + labelPadding();
103 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
106 width += base.width();
107 height = boundingRect.height() / 2.0;
104 height = boundingRect.height() / 2.0;
108 sh = QSizeF(width, height);
105 sh = QSizeF(width, height);
109 break;
106 break;
110 }
107 }
111 case Qt::PreferredSize: {
108 case Qt::PreferredSize: {
112 int labelWidth = 0;
109 qreal labelWidth = 0.0;
113 int firstHeight = -1;
110 qreal firstHeight = -1.0;
114 foreach (const QString& s, ticksList) {
111 foreach (const QString& s, ticksList) {
115 QRect rect = labelBoundingRect(fn, s);
112 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
116 labelWidth = qMax(rect.width(), labelWidth);
113 labelWidth = qMax(rect.width(), labelWidth);
117 height = rect.height();
114 height = rect.height();
118 if (firstHeight < 0)
115 if (firstHeight < 0.0)
119 firstHeight = height;
116 firstHeight = height;
120 }
117 }
121 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
118 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
122 width += base.width();
119 height = qMax(height, firstHeight) / 2.0;
123 height = qMax(height, qreal(firstHeight)) / 2.0;
124 sh = QSizeF(width, height);
120 sh = QSizeF(width, height);
125 break;
121 break;
126 }
122 }
@@ -20,7 +20,6
20
20
21 #include "horizontalaxis_p.h"
21 #include "horizontalaxis_p.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23 #include <QFontMetrics>
24 #include <qmath.h>
23 #include <qmath.h>
25 #include <QDebug>
24 #include <QDebug>
26
25
@@ -65,32 +64,13 void HorizontalAxis::updateGeometry()
65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
64 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
66
65
67 qreal width = 0;
66 qreal width = 0;
68 QFontMetrics fn(axis()->labelsFont());
69
67
70 //title
68 //title
71 int titlePad = 0;
69 int titlePad = 0;
72 QRectF titleBoundingRect;
70 QRectF titleBoundingRect;
73 QString titleText = axis()->titleText();
71 QString titleText = axis()->titleText();
74 if (!titleText.isEmpty() && titleItem()->isVisible()) {
72 if (!titleText.isEmpty() && titleItem()->isVisible()) {
75 int size(0);
73 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, gridRect.width(), Qt::Horizontal, QRectF()));
76 size = gridRect.width();
77
78 QGraphicsSimpleTextItem dummyTitle;
79 dummyTitle.setFont(axis()->titleFont());
80 dummyTitle.setText(axis()->titleText());
81 QRectF dummyRect = dummyTitle.boundingRect();
82
83 if (dummyRect.width() > size) {
84 QString string = titleText + "...";
85 while (dummyRect.width() > size && string.length() > 3) {
86 string.remove(string.length() - 4, 1);
87 dummyTitle.setText(string);
88 dummyRect = dummyTitle.boundingRect();
89 }
90 title->setText(string);
91 } else {
92 title->setText(titleText);
93 }
94
74
95 titlePad = titlePadding();
75 titlePad = titlePadding();
96 titleBoundingRect = title->boundingRect();
76 titleBoundingRect = title->boundingRect();
@@ -104,7 +84,6 void HorizontalAxis::updateGeometry()
104 }
84 }
105
85
106 for (int i = 0; i < layout.size(); ++i) {
86 for (int i = 0; i < layout.size(); ++i) {
107
108 //items
87 //items
109 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
88 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
110 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
89 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
@@ -115,18 +94,10 void HorizontalAxis::updateGeometry()
115
94
116 //label text wrapping
95 //label text wrapping
117 QString text = labelList.at(i);
96 QString text = labelList.at(i);
118 QRectF boundingRect = labelBoundingRect(fn, text);
97 QRectF boundingRect;
119 qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
98 qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
120 if (boundingRect.height() > size) {
99 labelItem->setText(truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
121 QString label = text + "...";
100 size, Qt::Vertical, boundingRect));
122 while (boundingRect.height() >= size && label.length() > 3) {
123 label.remove(label.length() - 4, 1);
124 boundingRect = labelBoundingRect(fn, label);
125 }
126 labelItem->setText(label);
127 } else {
128 labelItem->setText(text);
129 }
130
101
131 //label transformation origin point
102 //label transformation origin point
132 const QRectF& rect = labelItem->boundingRect();
103 const QRectF& rect = labelItem->boundingRect();
@@ -214,16 +185,13 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) co
214
185
215 switch (which) {
186 switch (which) {
216 case Qt::MinimumSize: {
187 case Qt::MinimumSize: {
217 QFontMetrics fn(axis()->titleFont());
188 QRectF titleRect = textBoundingRect(axis()->titleFont(), "...");
218 sh = QSizeF(fn.boundingRect("...").width(), fn.height() + (titlePadding() * 2));
189 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
219 break;
190 break;
220 }
191 }
221 case Qt::MaximumSize:
192 case Qt::MaximumSize:
222 case Qt::PreferredSize: {
193 case Qt::PreferredSize: {
223 QGraphicsSimpleTextItem dummyTitle;
194 QRectF titleRect = textBoundingRect(axis()->titleFont(), axis()->titleText());
224 dummyTitle.setFont(axis()->titleFont());
225 dummyTitle.setText(axis()->titleText());
226 QRectF titleRect = dummyTitle.boundingRect();
227 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
195 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
228 break;
196 break;
229 }
197 }
@@ -23,7 +23,6
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
27 #include <qmath.h>
26 #include <qmath.h>
28 #include <QDebug>
27 #include <QDebug>
29
28
@@ -87,7 +86,6 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
87 {
86 {
88 Q_UNUSED(constraint)
87 Q_UNUSED(constraint)
89
88
90 QFontMetrics fn(axis()->labelsFont());
91 QSizeF sh;
89 QSizeF sh;
92
90
93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
91 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
@@ -106,26 +104,24 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
106
104
107 switch (which) {
105 switch (which) {
108 case Qt::MinimumSize:{
106 case Qt::MinimumSize:{
109 QRectF boundingRect = labelBoundingRect(fn, "...");
107 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
110 width = boundingRect.width() / 2.0;
108 width = boundingRect.width() / 2.0;
111 height = boundingRect.height() + labelPadding();
109 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
112 height += base.height();
113 sh = QSizeF(width, height);
110 sh = QSizeF(width, height);
114 break;
111 break;
115 }
112 }
116 case Qt::PreferredSize: {
113 case Qt::PreferredSize: {
117 int labelHeight = 0;
114 qreal labelHeight = 0.0;
118 int firstWidth = -1;
115 qreal firstWidth = -1.0;
119 foreach (const QString& s, ticksList) {
116 foreach (const QString& s, ticksList) {
120 QRect rect = labelBoundingRect(fn, s);
117 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
121 labelHeight = qMax(rect.height(), labelHeight);
118 labelHeight = qMax(rect.height(), labelHeight);
122 width = rect.width();
119 width = rect.width();
123 if (firstWidth < 0)
120 if (firstWidth < 0.0)
124 firstWidth = width;
121 firstWidth = width;
125 }
122 }
126 height = labelHeight + labelPadding();
123 height = labelHeight + labelPadding() + base.height() + 1.0;
127 height += base.height();
124 width = qMax(width, firstWidth) / 2.0;
128 width = qMax(width, qreal(firstWidth)) / 2.0;
129 sh = QSizeF(width, height);
125 sh = QSizeF(width, height);
130 break;
126 break;
131 }
127 }
@@ -23,7 +23,6
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "abstractchartlayout_p.h"
24 #include "abstractchartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
27 #include <qmath.h>
26 #include <qmath.h>
28 #include <QDebug>
27 #include <QDebug>
29
28
@@ -87,7 +86,6 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
87 {
86 {
88 Q_UNUSED(constraint)
87 Q_UNUSED(constraint)
89
88
90 QFontMetrics fn(axis()->labelsFont());
91 QSizeF sh;
89 QSizeF sh;
92
90
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
91 QSizeF base = VerticalAxis::sizeHint(which, constraint);
@@ -106,26 +104,24 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
106
104
107 switch (which) {
105 switch (which) {
108 case Qt::MinimumSize: {
106 case Qt::MinimumSize: {
109 QRectF boundingRect = labelBoundingRect(fn, "...");
107 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
110 width = boundingRect.width() + labelPadding();
108 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
111 width += base.width();
112 height = boundingRect.height() / 2.0;
109 height = boundingRect.height() / 2.0;
113 sh = QSizeF(width, height);
110 sh = QSizeF(width, height);
114 break;
111 break;
115 }
112 }
116 case Qt::PreferredSize: {
113 case Qt::PreferredSize: {
117 int labelWidth = 0;
114 qreal labelWidth = 0.0;
118 int firstHeight = -1;
115 qreal firstHeight = -1.0;
119 foreach (const QString& s, ticksList) {
116 foreach (const QString& s, ticksList) {
120 QRect rect = labelBoundingRect(fn, s);
117 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
121 labelWidth = qMax(rect.width(), labelWidth);
118 labelWidth = qMax(rect.width(), labelWidth);
122 height = rect.height();
119 height = rect.height();
123 if (firstHeight < 0)
120 if (firstHeight < 0.0)
124 firstHeight = height;
121 firstHeight = height;
125 }
122 }
126 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
123 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
127 width += base.width();
124 height = qMax(height, firstHeight) / 2.0;
128 height = qMax(height, qreal(firstHeight)) / 2.0;
129 sh = QSizeF(width, height);
125 sh = QSizeF(width, height);
130 break;
126 break;
131 }
127 }
@@ -23,7 +23,6
23 #include "abstractchartlayout_p.h"
23 #include "abstractchartlayout_p.h"
24 #include "qabstractaxis.h"
24 #include "qabstractaxis.h"
25 #include "qabstractaxis_p.h"
25 #include "qabstractaxis_p.h"
26 #include <QFontMetrics>
27 #include <QDebug>
26 #include <QDebug>
28
27
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -59,7 +58,6 void PolarChartAxisAngular::updateGeometry()
59
58
60 qreal radius = axisGeometry().height() / 2.0;
59 qreal radius = axisGeometry().height() / 2.0;
61
60
62 QFontMetrics fn(axis()->labelsFont());
63 QRectF previousLabelRect;
61 QRectF previousLabelRect;
64 QRectF firstLabelRect;
62 QRectF firstLabelRect;
65
63
@@ -124,7 +122,7 void PolarChartAxisAngular::updateGeometry()
124 const QRectF &rect = labelItem->boundingRect();
122 const QRectF &rect = labelItem->boundingRect();
125 QPointF labelCenter = rect.center();
123 QPointF labelCenter = rect.center();
126 labelItem->setTransformOriginPoint(labelCenter.x(), labelCenter.y());
124 labelItem->setTransformOriginPoint(labelCenter.x(), labelCenter.y());
127 QRectF boundingRect = labelBoundingRect(fn, labelList.at(i));
125 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
128 boundingRect.moveCenter(labelCenter);
126 boundingRect.moveCenter(labelCenter);
129 QPointF positionDiff(rect.topLeft() - boundingRect.topLeft());
127 QPointF positionDiff(rect.topLeft() - boundingRect.topLeft());
130
128
@@ -221,28 +219,9 void PolarChartAxisAngular::updateGeometry()
221 // Title, centered above the chart
219 // Title, centered above the chart
222 QString titleText = axis()->titleText();
220 QString titleText = axis()->titleText();
223 if (!titleText.isEmpty() && axis()->isTitleVisible()) {
221 if (!titleText.isEmpty() && axis()->isTitleVisible()) {
224 int size(0);
222 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, axisGeometry().width(), Qt::Horizontal, QRectF()));
225 size = axisGeometry().width();
226
227 QGraphicsSimpleTextItem dummyTitle;
228 dummyTitle.setFont(axis()->titleFont());
229 dummyTitle.setText(titleText);
230 QRectF dummyRect = dummyTitle.boundingRect();
231
232 if (dummyRect.width() > size) {
233 QString string = titleText + "...";
234 while (dummyRect.width() > size && string.length() > 3) {
235 string.remove(string.length() - 4, 1);
236 dummyTitle.setText(string);
237 dummyRect = dummyTitle.boundingRect();
238 }
239 title->setText(string);
240 } else {
241 title->setText(titleText);
242 }
243
223
244 QRectF titleBoundingRect;
224 QRectF titleBoundingRect = title->boundingRect();
245 titleBoundingRect = title->boundingRect();
246 QPointF titleCenter = center - titleBoundingRect.center();
225 QPointF titleCenter = center - titleBoundingRect.center();
247 title->setPos(titleCenter.x(), axisGeometry().top() - qreal(titlePadding()) * 2.0 - titleBoundingRect.height() - labelHeight);
226 title->setPos(titleCenter.x(), axisGeometry().top() - qreal(titlePadding()) * 2.0 - titleBoundingRect.height() - labelHeight);
248 }
227 }
@@ -339,7 +318,6 qreal PolarChartAxisAngular::preferredAxisRadius(const QSizeF &maxSize)
339 // This is a horrible way to find out the maximum radius for angular axis and its labels.
318 // This is a horrible way to find out the maximum radius for angular axis and its labels.
340 // It just increments the radius down until everyhing fits the constraint size.
319 // It just increments the radius down until everyhing fits the constraint size.
341 // Proper way would be to actually calculate it but this seems to work reasonably fast as it is.
320 // Proper way would be to actually calculate it but this seems to work reasonably fast as it is.
342 QFontMetrics fm(font);
343 bool nextTickVisible = false;
321 bool nextTickVisible = false;
344 for (int i = 0; i < layout.size(); ) {
322 for (int i = 0; i < layout.size(); ) {
345 if ((i == layout.size() - 1)
323 if ((i == layout.size() - 1)
@@ -377,7 +355,7 qreal PolarChartAxisAngular::preferredAxisRadius(const QSizeF &maxSize)
377 continue;
355 continue;
378 }
356 }
379
357
380 QRectF boundingRect = labelBoundingRect(fm, labelList.at(i));
358 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
381 QPointF labelPoint = QLineF::fromPolar(radius + tickWidth(), 90.0 - labelCoordinate).p2();
359 QPointF labelPoint = QLineF::fromPolar(radius + tickWidth(), 90.0 - labelCoordinate).p2();
382
360
383 boundingRect = moveLabelToPosition(labelCoordinate, labelPoint, boundingRect);
361 boundingRect = moveLabelToPosition(labelCoordinate, labelPoint, boundingRect);
@@ -392,12 +370,9 qreal PolarChartAxisAngular::preferredAxisRadius(const QSizeF &maxSize)
392 }
370 }
393
371
394 if (!axis()->titleText().isEmpty() && axis()->isTitleVisible()) {
372 if (!axis()->titleText().isEmpty() && axis()->isTitleVisible()) {
395 QGraphicsSimpleTextItem dummyTitle;
373 QRectF titleRect = textBoundingRect(axis()->titleFont(), axis()->titleText());
396 dummyTitle.setFont(axis()->titleFont());
397 dummyTitle.setText(axis()->titleText());
398 QRectF dummyRect = dummyTitle.boundingRect();
399
374
400 radius -= titlePadding() + (dummyRect.height() / 2.0);
375 radius -= titlePadding() + (titleRect.height() / 2.0);
401 if (radius < 1.0) // safeguard
376 if (radius < 1.0) // safeguard
402 return 1.0;
377 return 1.0;
403 }
378 }
@@ -23,7 +23,6
23 #include "abstractchartlayout_p.h"
23 #include "abstractchartlayout_p.h"
24 #include "qabstractaxis_p.h"
24 #include "qabstractaxis_p.h"
25 #include "linearrowitem_p.h"
25 #include "linearrowitem_p.h"
26 #include <QFontMetrics>
27
26
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
28
@@ -56,7 +55,6 void PolarChartAxisRadial::updateGeometry()
56 QGraphicsLineItem *axisLine = static_cast<QGraphicsLineItem *>(arrowItemList.at(0));
55 QGraphicsLineItem *axisLine = static_cast<QGraphicsLineItem *>(arrowItemList.at(0));
57 axisLine->setLine(line);
56 axisLine->setLine(line);
58
57
59 QFontMetrics fn(axis()->labelsFont());
60 QRectF previousLabelRect;
58 QRectF previousLabelRect;
61 bool firstShade = true;
59 bool firstShade = true;
62 bool nextTickVisible = false;
60 bool nextTickVisible = false;
@@ -112,7 +110,7 void PolarChartAxisRadial::updateGeometry()
112 QRectF labelRect = labelItem->boundingRect();
110 QRectF labelRect = labelItem->boundingRect();
113 QPointF labelCenter = labelRect.center();
111 QPointF labelCenter = labelRect.center();
114 labelItem->setTransformOriginPoint(labelCenter.x(), labelCenter.y());
112 labelItem->setTransformOriginPoint(labelCenter.x(), labelCenter.y());
115 QRectF boundingRect = labelBoundingRect(fn, labelList.at(i));
113 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
116 boundingRect.moveCenter(labelCenter);
114 boundingRect.moveCenter(labelCenter);
117 QPointF positionDiff(labelRect.topLeft() - boundingRect.topLeft());
115 QPointF positionDiff(labelRect.topLeft() - boundingRect.topLeft());
118 QPointF labelPoint = center;
116 QPointF labelPoint = center;
@@ -205,25 +203,9 void PolarChartAxisRadial::updateGeometry()
205 // Title, along the 0 axis
203 // Title, along the 0 axis
206 QString titleText = axis()->titleText();
204 QString titleText = axis()->titleText();
207 if (!titleText.isEmpty() && axis()->isTitleVisible()) {
205 if (!titleText.isEmpty() && axis()->isTitleVisible()) {
208 QGraphicsSimpleTextItem dummyTitle;
206 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, radius, Qt::Horizontal, QRectF()));
209 dummyTitle.setFont(axis()->titleFont());
210 dummyTitle.setText(titleText);
211 QRectF dummyRect = dummyTitle.boundingRect();
212
213 if (dummyRect.width() > radius) {
214 QString string = titleText + "...";
215 while (dummyRect.width() > radius && string.length() > 3) {
216 string.remove(string.length() - 4, 1);
217 dummyTitle.setText(string);
218 dummyRect = dummyTitle.boundingRect();
219 }
220 title->setText(string);
221 } else {
222 title->setText(titleText);
223 }
224
207
225 QRectF titleBoundingRect;
208 QRectF titleBoundingRect = title->boundingRect();
226 titleBoundingRect = title->boundingRect();
227 QPointF titleCenter = titleBoundingRect.center();
209 QPointF titleCenter = titleBoundingRect.center();
228 QPointF arrowCenter = axisLine->boundingRect().center();
210 QPointF arrowCenter = axisLine->boundingRect().center();
229 QPointF titleCenterDiff = arrowCenter - titleCenter;
211 QPointF titleCenterDiff = arrowCenter - titleCenter;
@@ -24,7 +24,6
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include "abstractchartlayout_p.h"
25 #include "abstractchartlayout_p.h"
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
28 #include <qmath.h>
27 #include <qmath.h>
29 #include <QDebug>
28 #include <QDebug>
30
29
@@ -86,7 +85,6 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
86 {
85 {
87 Q_UNUSED(constraint)
86 Q_UNUSED(constraint)
88
87
89 QFontMetrics fn(axis()->labelsFont());
90 QSizeF sh;
88 QSizeF sh;
91
89
92 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
90 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
@@ -98,26 +96,24 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
98
96
99 switch (which) {
97 switch (which) {
100 case Qt::MinimumSize: {
98 case Qt::MinimumSize: {
101 QRectF boundingRect = labelBoundingRect(fn, "...");
99 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
102 width = boundingRect.width() / 2.0;
100 width = boundingRect.width() / 2.0;
103 height = boundingRect.height() + labelPadding();
101 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
104 height += base.height();
105 sh = QSizeF(width, height);
102 sh = QSizeF(width, height);
106 break;
103 break;
107 }
104 }
108 case Qt::PreferredSize: {
105 case Qt::PreferredSize: {
109 int labelHeight = 0;
106 qreal labelHeight = 0.0;
110 int firstWidth = -1;
107 qreal firstWidth = -1.0;
111 foreach (const QString& s, ticksList) {
108 foreach (const QString& s, ticksList) {
112 QRect rect = labelBoundingRect(fn, s);
109 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
113 labelHeight = qMax(rect.height(), labelHeight);
110 labelHeight = qMax(rect.height(), labelHeight);
114 width = rect.width();
111 width = rect.width();
115 if (firstWidth < 0)
112 if (firstWidth < 0.0)
116 firstWidth = width;
113 firstWidth = width;
117 }
114 }
118 height = labelHeight + labelPadding();
115 height = labelHeight + labelPadding() + base.height() + 1.0;
119 height += base.height();
116 width = qMax(width, firstWidth) / 2.0;
120 width = qMax(width, qreal(firstWidth)) / 2.0;
121 sh = QSizeF(width, height);
117 sh = QSizeF(width, height);
122 break;
118 break;
123 }
119 }
@@ -24,7 +24,6
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include "abstractchartlayout_p.h"
25 #include "abstractchartlayout_p.h"
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
28 #include <qmath.h>
27 #include <qmath.h>
29 #include <QDebug>
28 #include <QDebug>
30
29
@@ -87,7 +86,6 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
87 {
86 {
88 Q_UNUSED(constraint)
87 Q_UNUSED(constraint)
89
88
90 QFontMetrics fn(axis()->labelsFont());
91 QSizeF sh;
89 QSizeF sh;
92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
90 QSizeF base = VerticalAxis::sizeHint(which, constraint);
93 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
91 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
@@ -98,26 +96,24 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
98
96
99 switch (which) {
97 switch (which) {
100 case Qt::MinimumSize: {
98 case Qt::MinimumSize: {
101 QRectF boundingRect = labelBoundingRect(fn, "...");
99 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
102 width = boundingRect.width() + labelPadding();
100 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
103 width += base.width();
104 height = boundingRect.height() / 2.0;
101 height = boundingRect.height() / 2.0;
105 sh = QSizeF(width, height);
102 sh = QSizeF(width, height);
106 break;
103 break;
107 }
104 }
108 case Qt::PreferredSize: {
105 case Qt::PreferredSize: {
109 int labelWidth = 0;
106 qreal labelWidth = 0.0;
110 int firstHeight = -1;
107 qreal firstHeight = -1.0;
111 foreach (const QString& s, ticksList) {
108 foreach (const QString& s, ticksList) {
112 QRect rect = labelBoundingRect(fn, s);
109 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
113 labelWidth = qMax(rect.width(), labelWidth);
110 labelWidth = qMax(rect.width(), labelWidth);
114 height = rect.height();
111 height = rect.height();
115 if (firstHeight < 0)
112 if (firstHeight < 0.0)
116 firstHeight = height;
113 firstHeight = height;
117 }
114 }
118 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
115 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
119 width += base.width();
116 height = qMax(height, firstHeight) / 2.0;
120 height = qMax(height, qreal(firstHeight)) / 2.0;
121 sh = QSizeF(width, height);
117 sh = QSizeF(width, height);
122 break;
118 break;
123 }
119 }
@@ -20,7 +20,6
20
20
21 #include "verticalaxis_p.h"
21 #include "verticalaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include <QFontMetrics>
24 #include <QDebug>
23 #include <QDebug>
25
24
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -67,32 +66,12 void VerticalAxis::updateGeometry()
67 else if (axis()->alignment() == Qt::AlignRight)
66 else if (axis()->alignment() == Qt::AlignRight)
68 arrowItem->setLine(axisRect.left(), gridRect.top(), axisRect.left(), gridRect.bottom());
67 arrowItem->setLine(axisRect.left(), gridRect.top(), axisRect.left(), gridRect.bottom());
69
68
70 QFontMetrics fn(axis()->labelsFont());
71
72 //title
69 //title
73 int titlePad = 0;
70 int titlePad = 0;
74 QRectF titleBoundingRect;
71 QRectF titleBoundingRect;
75 QString titleText = axis()->titleText();
72 QString titleText = axis()->titleText();
76 if (!titleText.isEmpty() && titleItem()->isVisible()) {
73 if (!titleText.isEmpty() && titleItem()->isVisible()) {
77 int size(0);
74 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, gridRect.height(), Qt::Horizontal, QRectF()));
78 size = gridRect.height();
79
80 QGraphicsSimpleTextItem dummyTitle;
81 dummyTitle.setFont(axis()->titleFont());
82 dummyTitle.setText(axis()->titleText());
83 QRectF dummyRect = dummyTitle.boundingRect();
84
85 if (dummyRect.width() > size) {
86 QString string = titleText + "...";
87 while (dummyRect.width() > size && string.length() > 3) {
88 string.remove(string.length() - 4, 1);
89 dummyTitle.setText(string);
90 dummyRect = dummyTitle.boundingRect();
91 }
92 title->setText(string);
93 } else {
94 title->setText(titleText);
95 }
96
75
97 titlePad = titlePadding();
76 titlePad = titlePadding();
98 titleBoundingRect = title->boundingRect();
77 titleBoundingRect = title->boundingRect();
@@ -119,23 +98,13 void VerticalAxis::updateGeometry()
119
98
120 //label text wrapping
99 //label text wrapping
121 QString text = labelList.at(i);
100 QString text = labelList.at(i);
122 QRectF boundingRect = labelBoundingRect(fn, text);
101 QRectF boundingRect;
123
124 qreal size = axisRect.right() - axisRect.left() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
102 qreal size = axisRect.right() - axisRect.left() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
125 if (boundingRect.width() > size) {
103 labelItem->setText(truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
126 QString label = text + "...";
104 size, Qt::Horizontal, boundingRect));
127 while (boundingRect.width() > size && label.length() > 3) {
128 label.remove(label.length() - 4, 1);
129 boundingRect = labelBoundingRect(fn, label);
130 }
131 labelItem->setText(label);
132 } else {
133 labelItem->setText(text);
134 }
135
105
136 //label transformation origin point
106 //label transformation origin point
137 const QRectF &rect = labelItem->boundingRect();
107 const QRectF &rect = labelItem->boundingRect();
138
139 QPointF center = rect.center();
108 QPointF center = rect.center();
140 labelItem->setTransformOriginPoint(center.x(), center.y());
109 labelItem->setTransformOriginPoint(center.x(), center.y());
141 int widthDiff = rect.width() - boundingRect.width();
110 int widthDiff = rect.width() - boundingRect.width();
@@ -221,16 +190,13 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) cons
221
190
222 switch (which) {
191 switch (which) {
223 case Qt::MinimumSize: {
192 case Qt::MinimumSize: {
224 QFontMetrics fn(axis()->titleFont());
193 QRectF titleRect = textBoundingRect(axis()->titleFont(), "...");
225 sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect("...").width());
194 sh = QSizeF(titleRect.height() + (titlePadding() * 2), titleRect.width());
226 break;
195 break;
227 }
196 }
228 case Qt::MaximumSize:
197 case Qt::MaximumSize:
229 case Qt::PreferredSize: {
198 case Qt::PreferredSize: {
230 QGraphicsSimpleTextItem dummyTitle;
199 QRectF titleRect = textBoundingRect(axis()->titleFont(), axis()->titleText());
231 dummyTitle.setFont(axis()->titleFont());
232 dummyTitle.setText(axis()->titleText());
233 QRectF titleRect = dummyTitle.boundingRect();
234 sh = QSizeF(titleRect.height() + (titlePadding() * 2), titleRect.width());
200 sh = QSizeF(titleRect.height() + (titlePadding() * 2), titleRect.width());
235 break;
201 break;
236 }
202 }
@@ -22,7 +22,6
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "polarchartaxis_p.h"
23 #include "polarchartaxis_p.h"
24 #include <QDebug>
24 #include <QDebug>
25 #include <QFontMetrics>
26
25
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
27
@@ -58,10 +57,7 QRectF PolarChartLayout::calculateAxisGeometry(const QRectF &geometry, const QLi
58 && chartAxis->axis()->isTitleVisible()
57 && chartAxis->axis()->isTitleVisible()
59 && !chartAxis->axis()->titleText().isEmpty()) {
58 && !chartAxis->axis()->titleText().isEmpty()) {
60 // If axis has angular title, adjust geometry down by the space title takes
59 // If axis has angular title, adjust geometry down by the space title takes
61 QGraphicsSimpleTextItem dummyTitle;
60 QRectF dummyRect = chartAxis->textBoundingRect(chartAxis->axis()->titleFont(), chartAxis->axis()->titleText());
62 dummyTitle.setFont(chartAxis->axis()->titleFont());
63 dummyTitle.setText(chartAxis->axis()->titleText());
64 QRectF dummyRect = dummyTitle.boundingRect();
65 titleHeight = (dummyRect.height() / 2.0) + chartAxis->titlePadding();
61 titleHeight = (dummyRect.height() / 2.0) + chartAxis->titlePadding();
66 }
62 }
67 }
63 }
General Comments 0
You need to be logged in to leave comments. Login now