##// END OF EJS Templates
Added HTML support for various text items...
Miikka Heikkinen -
r2539:74f3dbde7a75
parent child
Show More
@@ -112,7 +112,7 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constra
112 112
113 113 switch (which) {
114 114 case Qt::MinimumSize: {
115 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
115 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
116 116 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
117 117 sh = QSizeF(width, height);
118 118 break;
@@ -120,7 +120,7 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constra
120 120 case Qt::PreferredSize:{
121 121 qreal labelHeight = 0.0;
122 122 foreach (const QString& s, ticksList) {
123 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
123 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
124 124 labelHeight = qMax(rect.height(), labelHeight);
125 125 }
126 126 height = labelHeight + labelPadding() + base.height() + 1.0;
@@ -110,7 +110,7 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constra
110 110
111 111 switch (which) {
112 112 case Qt::MinimumSize: {
113 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
113 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
114 114 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
115 115 if (base.width() > 0.0)
116 116 width += labelPadding();
@@ -120,7 +120,7 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constra
120 120 case Qt::PreferredSize:{
121 121 qreal labelWidth = 0.0;
122 122 foreach (const QString& s, ticksList) {
123 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
123 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
124 124 labelWidth = qMax(rect.width(), labelWidth);
125 125 }
126 126 width = labelWidth + labelPadding() + base.width() + 1.0;
@@ -61,18 +61,16 void CartesianChartAxis::createItems(int count)
61 61 for (int i = 0; i < count; ++i) {
62 62 QGraphicsLineItem *arrow = new QGraphicsLineItem(this);
63 63 QGraphicsLineItem *grid = new QGraphicsLineItem(this);
64 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
65 QGraphicsSimpleTextItem *title = titleItem();
64 QGraphicsTextItem *label = new QGraphicsTextItem(this);
65 QGraphicsTextItem *title = titleItem();
66 66 arrow->setPen(axis()->linePen());
67 67 grid->setPen(axis()->gridLinePen());
68 68 label->setFont(axis()->labelsFont());
69 label->setPen(axis()->labelsPen());
70 label->setBrush(axis()->labelsBrush());
69 label->setDefaultTextColor(axis()->labelsBrush().color());
71 70 label->setRotation(axis()->labelsAngle());
72 71 title->setFont(axis()->titleFont());
73 title->setPen(axis()->titlePen());
74 title->setBrush(axis()->titleBrush());
75 title->setText(axis()->titleText());
72 title->setDefaultTextColor(axis()->titleBrush().color());
73 title->setHtml(axis()->titleText());
76 74 arrowGroup()->addToGroup(arrow);
77 75 gridGroup()->addToGroup(grid);
78 76 labelGroup()->addToGroup(label);
@@ -85,7 +85,7 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
85 85
86 86 switch (which) {
87 87 case Qt::MinimumSize: {
88 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
88 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
89 89 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
90 90 sh = QSizeF(width, height);
91 91 break;
@@ -93,7 +93,7 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
93 93 case Qt::PreferredSize: {
94 94 qreal labelHeight = 0.0;
95 95 foreach (const QString& s, ticksList) {
96 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
96 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
97 97 labelHeight = qMax(rect.height(), labelHeight);
98 98 }
99 99 height = labelHeight + labelPadding() + base.height() + 1.0;
@@ -85,7 +85,7 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
85 85
86 86 switch (which) {
87 87 case Qt::MinimumSize: {
88 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
88 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
89 89 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
90 90 sh = QSizeF(width, height);
91 91 break;
@@ -93,7 +93,7 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
93 93 case Qt::PreferredSize: {
94 94 qreal labelWidth = 0.0;
95 95 foreach (const QString& s, ticksList) {
96 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
96 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
97 97 labelWidth = qMax(rect.width(), labelWidth);
98 98 }
99 99 width = labelWidth + labelPadding() + base.width() + 1.0;
@@ -27,14 +27,15
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 QGraphicsSimpleTextItem *dummyTextItem = 0;
31 class StaticDeleter
30 static const char *labelFormatMatchString = "%[\\-\\+#\\s\\d\\.lhjztL]*([dicuoxfegXFEG])";
31 static QRegExp *labelFormatMatcher = 0;
32 class StaticLabelFormatMatcherDeleter
32 33 {
33 34 public:
34 StaticDeleter() {}
35 ~StaticDeleter() { delete dummyTextItem; }
35 StaticLabelFormatMatcherDeleter() {}
36 ~StaticLabelFormatMatcherDeleter() { delete labelFormatMatcher; }
36 37 };
37 StaticDeleter staticDeleter;
38 StaticLabelFormatMatcherDeleter staticLabelFormatMatcherDeleter;
38 39
39 40 ChartAxisElement::ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
40 41 : ChartElement(item),
@@ -44,13 +45,11 ChartAxisElement::ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, boo
44 45 m_arrow(new QGraphicsItemGroup(item)),
45 46 m_shades(new QGraphicsItemGroup(item)),
46 47 m_labels(new QGraphicsItemGroup(item)),
47 m_title(new QGraphicsSimpleTextItem(item)),
48 m_title(new QGraphicsTextItem(item)),
48 49 m_intervalAxis(intervalAxis)
49 50
50 51 {
51 52 //initial initialization
52 if (!dummyTextItem)
53 dummyTextItem = new QGraphicsSimpleTextItem;
54 53 m_arrow->setHandlesChildEvents(false);
55 54 m_arrow->setZValue(ChartPresenter::AxisZValue);
56 55 m_labels->setZValue(ChartPresenter::AxisZValue);
@@ -130,20 +129,19 void ChartAxisElement::handleLabelsAngleChanged(int angle)
130 129
131 130 void ChartAxisElement::handleLabelsPenChanged(const QPen &pen)
132 131 {
133 foreach (QGraphicsItem *item, m_labels->childItems())
134 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
132 Q_UNUSED(pen)
135 133 }
136 134
137 135 void ChartAxisElement::handleLabelsBrushChanged(const QBrush &brush)
138 136 {
139 137 foreach (QGraphicsItem *item, m_labels->childItems())
140 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
138 static_cast<QGraphicsTextItem *>(item)->setDefaultTextColor(brush.color());
141 139 }
142 140
143 141 void ChartAxisElement::handleLabelsFontChanged(const QFont &font)
144 142 {
145 143 foreach (QGraphicsItem *item, m_labels->childItems())
146 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
144 static_cast<QGraphicsTextItem *>(item)->setFont(font);
147 145 QGraphicsLayoutItem::updateGeometry();
148 146 presenter()->layout()->invalidate();
149 147 }
@@ -152,17 +150,17 void ChartAxisElement::handleTitleTextChanged(const QString &title)
152 150 {
153 151 QGraphicsLayoutItem::updateGeometry();
154 152 presenter()->layout()->invalidate();
155 m_title->setText(title);
153 m_title->setHtml(title);
156 154 }
157 155
158 156 void ChartAxisElement::handleTitlePenChanged(const QPen &pen)
159 157 {
160 m_title->setPen(pen);
158 Q_UNUSED(pen)
161 159 }
162 160
163 161 void ChartAxisElement::handleTitleBrushChanged(const QBrush &brush)
164 162 {
165 m_title->setBrush(brush);
163 m_title->setDefaultTextColor(brush.color());
166 164 }
167 165
168 166 void ChartAxisElement::handleTitleFontChanged(const QFont &font)
@@ -194,44 +192,6 void ChartAxisElement::handleVisibleChanged(bool visible)
194 192 if (presenter()) presenter()->layout()->invalidate();
195 193 }
196 194
197 QRectF ChartAxisElement::textBoundingRect(const QFont &font, const QString &text, qreal angle) const
198 {
199 dummyTextItem->setFont(font);
200 dummyTextItem->setText(text);
201 QRectF boundingRect = dummyTextItem->boundingRect();
202
203 // Take rotation into account
204 if (angle) {
205 QTransform transform;
206 transform.rotate(angle);
207 boundingRect = transform.mapRect(boundingRect);
208 }
209
210 return boundingRect;
211 }
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
235 195 void ChartAxisElement::handleRangeChanged(qreal min, qreal max)
236 196 {
237 197 Q_UNUSED(min);
@@ -270,6 +230,32 qreal ChartAxisElement::max() const
270 230 return m_axis->d_ptr->max();
271 231 }
272 232
233 static void appendFormattedLabel(const QString &capStr, const QByteArray &array,
234 QStringList &labels, qreal value)
235 {
236 if (capStr.isEmpty()) {
237 labels << QString();
238 } else if (capStr.at(0) == QLatin1Char('d')
239 || capStr.at(0) == QLatin1Char('i')
240 || capStr.at(0) == QLatin1Char('c')) {
241 labels << QString().sprintf(array, (qint64)value);
242 } else if (capStr.at(0) == QLatin1Char('u')
243 || capStr.at(0) == QLatin1Char('o')
244 || capStr.at(0) == QLatin1Char('x')
245 || capStr.at(0) == QLatin1Char('X')) {
246 labels << QString().sprintf(array, (quint64)value);
247 } else if (capStr.at(0) == QLatin1Char('f')
248 || capStr.at(0) == QLatin1Char('F')
249 || capStr.at(0) == QLatin1Char('e')
250 || capStr.at(0) == QLatin1Char('E')
251 || capStr.at(0) == QLatin1Char('g')
252 || capStr.at(0) == QLatin1Char('G')) {
253 labels << QString().sprintf(array, value);
254 } else {
255 labels << QString();
256 }
257 }
258
273 259 QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, const QString &format)
274 260 {
275 261 QStringList labels;
@@ -287,23 +273,14 QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks,
287 273 }
288 274 } else {
289 275 QByteArray array = format.toLatin1();
276 QString capStr;
277 if (!labelFormatMatcher)
278 labelFormatMatcher = new QRegExp(labelFormatMatchString);
279 if (labelFormatMatcher->indexIn(format, 0) != -1)
280 capStr = labelFormatMatcher->cap(1);
290 281 for (int i = 0; i < ticks; i++) {
291 282 qreal value = min + (i * (max - min) / (ticks - 1));
292 if (format.contains("d")
293 || format.contains("i")
294 || format.contains("c")) {
295 labels << QString().sprintf(array, (qint64)value);
296 } else if (format.contains("u")
297 || format.contains("o")
298 || format.contains("x", Qt::CaseInsensitive)) {
299 labels << QString().sprintf(array, (quint64)value);
300 } else if (format.contains("f", Qt::CaseInsensitive)
301 || format.contains("e", Qt::CaseInsensitive)
302 || format.contains("g", Qt::CaseInsensitive)) {
303 labels << QString().sprintf(array, value);
304 } else {
305 labels << QString();
306 }
283 appendFormattedLabel(capStr, array, labels, value);
307 284 }
308 285 }
309 286
@@ -335,23 +312,14 QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal b
335 312 }
336 313 } else {
337 314 QByteArray array = format.toLatin1();
315 QString capStr;
316 if (!labelFormatMatcher)
317 labelFormatMatcher = new QRegExp(labelFormatMatchString);
318 if (labelFormatMatcher->indexIn(format, 0) != -1)
319 capStr = labelFormatMatcher->cap(1);
338 320 for (int i = firstTick; i < ticks + firstTick; i++) {
339 321 qreal value = qPow(base, i);
340 if (format.contains("d")
341 || format.contains("i")
342 || format.contains("c")) {
343 labels << QString().sprintf(array, (qint64)value);
344 } else if (format.contains("u")
345 || format.contains("o")
346 || format.contains("x", Qt::CaseInsensitive)) {
347 labels << QString().sprintf(array, (quint64)value);
348 } else if (format.contains("f", Qt::CaseInsensitive)
349 || format.contains("e", Qt::CaseInsensitive)
350 || format.contains("g", Qt::CaseInsensitive)) {
351 labels << QString().sprintf(array, value);
352 } else {
353 labels << QString();
354 }
322 appendFormattedLabel(capStr, array, labels, value);
355 323 }
356 324 }
357 325
@@ -70,10 +70,6 public:
70 70 QRectF axisGeometry() const { return m_axisRect; }
71 71 void setAxisGeometry(const QRectF &axisGeometry) { m_axisRect = axisGeometry; }
72 72
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;
76
77 73 void axisSelected();
78 74
79 75 //this flag indicates that axis is used to show intervals it means labels are in between ticks
@@ -102,7 +98,7 protected:
102 98 QList<QGraphicsItem *> labelItems() { return m_labels->childItems(); }
103 99 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems(); }
104 100 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems(); }
105 QGraphicsSimpleTextItem *titleItem() const { return m_title.data(); }
101 QGraphicsTextItem *titleItem() const { return m_title.data(); }
106 102 QGraphicsItemGroup *gridGroup() { return m_grid.data(); }
107 103 QGraphicsItemGroup *labelGroup() { return m_labels.data(); }
108 104 QGraphicsItemGroup *shadeGroup() { return m_shades.data(); }
@@ -144,7 +140,7 private:
144 140 QScopedPointer<QGraphicsItemGroup> m_arrow;
145 141 QScopedPointer<QGraphicsItemGroup> m_shades;
146 142 QScopedPointer<QGraphicsItemGroup> m_labels;
147 QScopedPointer<QGraphicsSimpleTextItem> m_title;
143 QScopedPointer<QGraphicsTextItem> m_title;
148 144 bool m_intervalAxis;
149 145 };
150 146
@@ -98,7 +98,7 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
98 98
99 99 switch (which) {
100 100 case Qt::MinimumSize: {
101 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
101 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
102 102 width = boundingRect.width() / 2.0;
103 103 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
104 104 sh = QSizeF(width, height);
@@ -108,7 +108,7 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
108 108 qreal labelHeight = 0.0;
109 109 qreal firstWidth = -1.0;
110 110 foreach (const QString& s, ticksList) {
111 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
111 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
112 112 labelHeight = qMax(rect.height(), labelHeight);
113 113 width = rect.width();
114 114 if (firstWidth < 0.0)
@@ -99,7 +99,7 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
99 99
100 100 switch (which) {
101 101 case Qt::MinimumSize: {
102 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
102 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
103 103 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
104 104 height = boundingRect.height() / 2.0;
105 105 sh = QSizeF(width, height);
@@ -109,7 +109,7 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
109 109 qreal labelWidth = 0.0;
110 110 qreal firstHeight = -1.0;
111 111 foreach (const QString& s, ticksList) {
112 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
112 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
113 113 labelWidth = qMax(rect.width(), labelWidth);
114 114 height = rect.height();
115 115 if (firstHeight < 0.0)
@@ -20,6 +20,7
20 20
21 21 #include "horizontalaxis_p.h"
22 22 #include "qabstractaxis_p.h"
23 #include "chartpresenter_p.h"
23 24 #include <qmath.h>
24 25 #include <QDebug>
25 26
@@ -47,7 +48,7 void HorizontalAxis::updateGeometry()
47 48 QList<QGraphicsItem *> labels = labelItems();
48 49 QList<QGraphicsItem *> shades = shadeItems();
49 50 QList<QGraphicsItem *> arrow = arrowItems();
50 QGraphicsSimpleTextItem *title = titleItem();
51 QGraphicsTextItem *title = titleItem();
51 52
52 53 Q_ASSERT(labels.size() == labelList.size());
53 54 Q_ASSERT(layout.size() == labelList.size());
@@ -70,7 +71,7 void HorizontalAxis::updateGeometry()
70 71 QRectF titleBoundingRect;
71 72 QString titleText = axis()->titleText();
72 73 if (!titleText.isEmpty() && titleItem()->isVisible()) {
73 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, gridRect.width(), Qt::Horizontal, QRectF()));
74 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), gridRect.width(), Qt::Horizontal, QRectF()));
74 75
75 76 titlePad = titlePadding();
76 77 titleBoundingRect = title->boundingRect();
@@ -87,7 +88,7 void HorizontalAxis::updateGeometry()
87 88 //items
88 89 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
89 90 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
90 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
91 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
91 92
92 93 //grid line
93 94 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
@@ -96,8 +97,8 void HorizontalAxis::updateGeometry()
96 97 QString text = labelList.at(i);
97 98 QRectF boundingRect;
98 99 qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
99 labelItem->setText(truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
100 size, Qt::Vertical, boundingRect));
100 labelItem->setHtml(ChartPresenter::truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
101 size, Qt::Vertical, boundingRect));
101 102
102 103 //label transformation origin point
103 104 const QRectF& rect = labelItem->boundingRect();
@@ -185,13 +186,13 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) co
185 186
186 187 switch (which) {
187 188 case Qt::MinimumSize: {
188 QRectF titleRect = textBoundingRect(axis()->titleFont(), "...");
189 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
189 190 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
190 191 break;
191 192 }
192 193 case Qt::MaximumSize:
193 194 case Qt::PreferredSize: {
194 QRectF titleRect = textBoundingRect(axis()->titleFont(), axis()->titleText());
195 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
195 196 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
196 197 break;
197 198 }
@@ -104,7 +104,7 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
104 104
105 105 switch (which) {
106 106 case Qt::MinimumSize:{
107 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
107 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
108 108 width = boundingRect.width() / 2.0;
109 109 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
110 110 sh = QSizeF(width, height);
@@ -114,7 +114,7 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint
114 114 qreal labelHeight = 0.0;
115 115 qreal firstWidth = -1.0;
116 116 foreach (const QString& s, ticksList) {
117 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
117 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
118 118 labelHeight = qMax(rect.height(), labelHeight);
119 119 width = rect.width();
120 120 if (firstWidth < 0.0)
@@ -104,7 +104,7 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
104 104
105 105 switch (which) {
106 106 case Qt::MinimumSize: {
107 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
107 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
108 108 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
109 109 height = boundingRect.height() / 2.0;
110 110 sh = QSizeF(width, height);
@@ -114,7 +114,7 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint
114 114 qreal labelWidth = 0.0;
115 115 qreal firstHeight = -1.0;
116 116 foreach (const QString& s, ticksList) {
117 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
117 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
118 118 labelWidth = qMax(rect.width(), labelWidth);
119 119 height = rect.height();
120 120 if (firstHeight < 0.0)
@@ -51,7 +51,7 void PolarChartAxisAngular::updateGeometry()
51 51 QList<QGraphicsItem *> gridItemList = gridItems();
52 52 QList<QGraphicsItem *> labelItemList = labelItems();
53 53 QList<QGraphicsItem *> shadeItemList = shadeItems();
54 QGraphicsSimpleTextItem *title = titleItem();
54 QGraphicsTextItem *title = titleItem();
55 55
56 56 QGraphicsEllipseItem *axisLine = static_cast<QGraphicsEllipseItem *>(arrowItemList.at(0));
57 57 axisLine->setRect(axisGeometry());
@@ -73,7 +73,7 void PolarChartAxisAngular::updateGeometry()
73 73
74 74 QGraphicsLineItem *gridLineItem = static_cast<QGraphicsLineItem *>(gridItemList.at(i));
75 75 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(arrowItemList.at(i + 1));
76 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labelItemList.at(i));
76 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labelItemList.at(i));
77 77 QGraphicsPathItem *shadeItem = 0;
78 78 if (i == 0)
79 79 shadeItem = static_cast<QGraphicsPathItem *>(shadeItemList.at(0));
@@ -118,11 +118,11 void PolarChartAxisAngular::updateGeometry()
118 118
119 119 // Angular axis label
120 120 if (axis()->labelsVisible() && labelVisible) {
121 labelItem->setText(labelList.at(i));
121 labelItem->setHtml(labelList.at(i));
122 122 const QRectF &rect = labelItem->boundingRect();
123 123 QPointF labelCenter = rect.center();
124 124 labelItem->setTransformOriginPoint(labelCenter.x(), labelCenter.y());
125 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
125 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
126 126 boundingRect.moveCenter(labelCenter);
127 127 QPointF positionDiff(rect.topLeft() - boundingRect.topLeft());
128 128
@@ -219,7 +219,7 void PolarChartAxisAngular::updateGeometry()
219 219 // Title, centered above the chart
220 220 QString titleText = axis()->titleText();
221 221 if (!titleText.isEmpty() && axis()->isTitleVisible()) {
222 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, axisGeometry().width(), Qt::Horizontal, QRectF()));
222 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), axisGeometry().width(), Qt::Horizontal, QRectF()));
223 223
224 224 QRectF titleBoundingRect = title->boundingRect();
225 225 QPointF titleCenter = center - titleBoundingRect.center();
@@ -245,18 +245,16 void PolarChartAxisAngular::createItems(int count)
245 245 for (int i = 0; i < count; ++i) {
246 246 QGraphicsLineItem *arrow = new QGraphicsLineItem(presenter()->rootItem());
247 247 QGraphicsLineItem *grid = new QGraphicsLineItem(presenter()->rootItem());
248 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(presenter()->rootItem());
249 QGraphicsSimpleTextItem *title = titleItem();
248 QGraphicsTextItem *label = new QGraphicsTextItem(presenter()->rootItem());
249 QGraphicsTextItem *title = titleItem();
250 250 arrow->setPen(axis()->linePen());
251 251 grid->setPen(axis()->gridLinePen());
252 252 label->setFont(axis()->labelsFont());
253 label->setPen(axis()->labelsPen());
254 label->setBrush(axis()->labelsBrush());
253 label->setDefaultTextColor(axis()->labelsBrush().color());
255 254 label->setRotation(axis()->labelsAngle());
256 255 title->setFont(axis()->titleFont());
257 title->setPen(axis()->titlePen());
258 title->setBrush(axis()->titleBrush());
259 title->setText(axis()->titleText());
256 title->setDefaultTextColor(axis()->titleBrush().color());
257 title->setHtml(axis()->titleText());
260 258 arrowGroup()->addToGroup(arrow);
261 259 gridGroup()->addToGroup(grid);
262 260 labelGroup()->addToGroup(label);
@@ -355,7 +353,7 qreal PolarChartAxisAngular::preferredAxisRadius(const QSizeF &maxSize)
355 353 continue;
356 354 }
357 355
358 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
356 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
359 357 QPointF labelPoint = QLineF::fromPolar(radius + tickWidth(), 90.0 - labelCoordinate).p2();
360 358
361 359 boundingRect = moveLabelToPosition(labelCoordinate, labelPoint, boundingRect);
@@ -370,7 +368,7 qreal PolarChartAxisAngular::preferredAxisRadius(const QSizeF &maxSize)
370 368 }
371 369
372 370 if (!axis()->titleText().isEmpty() && axis()->isTitleVisible()) {
373 QRectF titleRect = textBoundingRect(axis()->titleFont(), axis()->titleText());
371 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
374 372
375 373 radius -= titlePadding() + (titleRect.height() / 2.0);
376 374 if (radius < 1.0) // safeguard
@@ -48,7 +48,7 void PolarChartAxisRadial::updateGeometry()
48 48 QList<QGraphicsItem *> gridItemList = gridItems();
49 49 QList<QGraphicsItem *> labelItemList = labelItems();
50 50 QList<QGraphicsItem *> shadeItemList = shadeItems();
51 QGraphicsSimpleTextItem* title = titleItem();
51 QGraphicsTextItem* title = titleItem();
52 52 qreal radius = axisGeometry().height() / 2.0;
53 53
54 54 QLineF line(center, center + QPointF(0, -radius));
@@ -66,7 +66,7 void PolarChartAxisRadial::updateGeometry()
66 66
67 67 QGraphicsEllipseItem *gridItem = static_cast<QGraphicsEllipseItem *>(gridItemList.at(i));
68 68 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(arrowItemList.at(i + 1));
69 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labelItemList.at(i));
69 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labelItemList.at(i));
70 70 QGraphicsPathItem *shadeItem = 0;
71 71 if (i == 0)
72 72 shadeItem = static_cast<QGraphicsPathItem *>(shadeItemList.at(0));
@@ -106,11 +106,11 void PolarChartAxisRadial::updateGeometry()
106 106
107 107 // Radial axis label
108 108 if (axis()->labelsVisible() && labelVisible) {
109 labelItem->setText(labelList.at(i));
109 labelItem->setHtml(labelList.at(i));
110 110 QRectF labelRect = labelItem->boundingRect();
111 111 QPointF labelCenter = labelRect.center();
112 112 labelItem->setTransformOriginPoint(labelCenter.x(), labelCenter.y());
113 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
113 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), labelList.at(i), axis()->labelsAngle());
114 114 boundingRect.moveCenter(labelCenter);
115 115 QPointF positionDiff(labelRect.topLeft() - boundingRect.topLeft());
116 116 QPointF labelPoint = center;
@@ -203,7 +203,7 void PolarChartAxisRadial::updateGeometry()
203 203 // Title, along the 0 axis
204 204 QString titleText = axis()->titleText();
205 205 if (!titleText.isEmpty() && axis()->isTitleVisible()) {
206 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, radius, Qt::Horizontal, QRectF()));
206 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), radius, Qt::Horizontal, QRectF()));
207 207
208 208 QRectF titleBoundingRect = title->boundingRect();
209 209 QPointF titleCenter = titleBoundingRect.center();
@@ -234,18 +234,16 void PolarChartAxisRadial::createItems(int count)
234 234 for (int i = 0; i < count; ++i) {
235 235 QGraphicsLineItem *arrow = new QGraphicsLineItem(presenter()->rootItem());
236 236 QGraphicsEllipseItem *grid = new QGraphicsEllipseItem(presenter()->rootItem());
237 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(presenter()->rootItem());
238 QGraphicsSimpleTextItem *title = titleItem();
237 QGraphicsTextItem *label = new QGraphicsTextItem(presenter()->rootItem());
238 QGraphicsTextItem *title = titleItem();
239 239 arrow->setPen(axis()->linePen());
240 240 grid->setPen(axis()->gridLinePen());
241 241 label->setFont(axis()->labelsFont());
242 label->setPen(axis()->labelsPen());
243 label->setBrush(axis()->labelsBrush());
242 label->setDefaultTextColor(axis()->labelsBrush().color());
244 243 label->setRotation(axis()->labelsAngle());
245 244 title->setFont(axis()->titleFont());
246 title->setPen(axis()->titlePen());
247 title->setBrush(axis()->titleBrush());
248 title->setText(axis()->titleText());
245 title->setDefaultTextColor(axis()->titleBrush().color());
246 title->setHtml(axis()->titleText());
249 247 arrowGroup()->addToGroup(arrow);
250 248 gridGroup()->addToGroup(grid);
251 249 labelGroup()->addToGroup(label);
@@ -92,12 +92,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
92 92
93 93 /*!
94 94 \property QAbstractAxis::labelsPen
95 \deprecated
95 96 The pen of the labels.
96 97 */
97 98
98 99 /*!
99 100 \property QAbstractAxis::labelsBrush
100 The brush of the labels.
101 The brush of the labels. Only the color of the brush is relevant.
101 102 */
102 103
103 104 /*!
@@ -210,21 +211,22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
210 211
211 212 /*!
212 213 \property QAbstractAxis::titleText
213 The title of the axis. Empty by default.
214 The title of the axis. Empty by default. Axis titles support html formatting.
214 215 */
215 216 /*!
216 217 \qmlproperty String AbstractAxis::titleText
217 The title of the axis. Empty by default.
218 The title of the axis. Empty by default. Axis titles support html formatting.
218 219 */
219 220
220 221 /*!
221 222 \property QAbstractAxis::titlePen
223 \deprecated
222 224 The pen of the title text.
223 225 */
224 226
225 227 /*!
226 228 \property QAbstractAxis::titleBrush
227 The brush of the title text.
229 The brush of the title text. Only the color of the brush is relevant.
228 230 */
229 231
230 232 /*!
@@ -297,6 +299,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
297 299
298 300 /*!
299 301 \fn void QAbstractAxis::labelsPenChanged(const QPen& pen)
302 \deprecated
300 303 The pen of the axis labels has changed to \a pen.
301 304 */
302 305
@@ -366,6 +369,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
366 369
367 370 /*!
368 371 \fn void QAbstractAxis::titlePenChanged(const QPen& pen)
372 \deprecated
369 373 The pen of the axis shades has changed to \a pen.
370 374 */
371 375
@@ -544,9 +548,6 bool QAbstractAxis::labelsVisible() const
544 548 return d_ptr->m_labelsVisible;
545 549 }
546 550
547 /*!
548 Sets \a pen used to draw labels.
549 */
550 551 void QAbstractAxis::setLabelsPen(const QPen &pen)
551 552 {
552 553 if (d_ptr->m_labelsPen != pen) {
@@ -555,9 +556,6 void QAbstractAxis::setLabelsPen(const QPen &pen)
555 556 }
556 557 }
557 558
558 /*!
559 Returns the pen used to labels.
560 */
561 559 QPen QAbstractAxis::labelsPen() const
562 560 {
563 561 if (d_ptr->m_labelsPen == QChartPrivate::defaultPen())
@@ -651,9 +649,6 bool QAbstractAxis::isTitleVisible() const
651 649 return d_ptr->m_titleVisible;
652 650 }
653 651
654 /*!
655 Sets \a pen used to draw title.
656 */
657 652 void QAbstractAxis::setTitlePen(const QPen &pen)
658 653 {
659 654 if (d_ptr->m_titlePen != pen) {
@@ -662,9 +657,6 void QAbstractAxis::setTitlePen(const QPen &pen)
662 657 }
663 658 }
664 659
665 /*!
666 Returns the pen used to title.
667 */
668 660 QPen QAbstractAxis::titlePen() const
669 661 {
670 662 if (d_ptr->m_titlePen == QChartPrivate::defaultPen())
@@ -96,7 +96,7 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
96 96
97 97 switch (which) {
98 98 case Qt::MinimumSize: {
99 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
99 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
100 100 width = boundingRect.width() / 2.0;
101 101 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
102 102 sh = QSizeF(width, height);
@@ -106,7 +106,7 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
106 106 qreal labelHeight = 0.0;
107 107 qreal firstWidth = -1.0;
108 108 foreach (const QString& s, ticksList) {
109 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
109 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
110 110 labelHeight = qMax(rect.height(), labelHeight);
111 111 width = rect.width();
112 112 if (firstWidth < 0.0)
@@ -96,7 +96,7 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
96 96
97 97 switch (which) {
98 98 case Qt::MinimumSize: {
99 QRectF boundingRect = textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
99 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle());
100 100 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
101 101 height = boundingRect.height() / 2.0;
102 102 sh = QSizeF(width, height);
@@ -106,7 +106,7 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
106 106 qreal labelWidth = 0.0;
107 107 qreal firstHeight = -1.0;
108 108 foreach (const QString& s, ticksList) {
109 QRectF rect = textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
109 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
110 110 labelWidth = qMax(rect.width(), labelWidth);
111 111 height = rect.height();
112 112 if (firstHeight < 0.0)
@@ -20,6 +20,7
20 20
21 21 #include "verticalaxis_p.h"
22 22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 24 #include <QDebug>
24 25
25 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -46,7 +47,7 void VerticalAxis::updateGeometry()
46 47 QList<QGraphicsItem *> labels = labelItems();
47 48 QList<QGraphicsItem *> shades = shadeItems();
48 49 QList<QGraphicsItem *> arrow = arrowItems();
49 QGraphicsSimpleTextItem *title = titleItem();
50 QGraphicsTextItem *title = titleItem();
50 51
51 52 Q_ASSERT(labels.size() == labelList.size());
52 53 Q_ASSERT(layout.size() == labelList.size());
@@ -71,7 +72,7 void VerticalAxis::updateGeometry()
71 72 QRectF titleBoundingRect;
72 73 QString titleText = axis()->titleText();
73 74 if (!titleText.isEmpty() && titleItem()->isVisible()) {
74 title->setText(truncatedText(axis()->titleFont(), titleText, 0.0, gridRect.height(), Qt::Horizontal, QRectF()));
75 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), gridRect.height(), Qt::Horizontal, QRectF()));
75 76
76 77 titlePad = titlePadding();
77 78 titleBoundingRect = title->boundingRect();
@@ -91,7 +92,7 void VerticalAxis::updateGeometry()
91 92 //items
92 93 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
93 94 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(arrow.at(i + 1));
94 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
95 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
95 96
96 97 //grid line
97 98 gridItem->setLine(gridRect.left(), layout[i], gridRect.right(), layout[i]);
@@ -100,7 +101,7 void VerticalAxis::updateGeometry()
100 101 QString text = labelList.at(i);
101 102 QRectF boundingRect;
102 103 qreal size = axisRect.right() - axisRect.left() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
103 labelItem->setText(truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
104 labelItem->setHtml(ChartPresenter::truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
104 105 size, Qt::Horizontal, boundingRect));
105 106
106 107 //label transformation origin point
@@ -190,13 +191,13 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) cons
190 191
191 192 switch (which) {
192 193 case Qt::MinimumSize: {
193 QRectF titleRect = textBoundingRect(axis()->titleFont(), "...");
194 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
194 195 sh = QSizeF(titleRect.height() + (titlePadding() * 2), titleRect.width());
195 196 break;
196 197 }
197 198 case Qt::MaximumSize:
198 199 case Qt::PreferredSize: {
199 QRectF titleRect = textBoundingRect(axis()->titleFont(), axis()->titleText());
200 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
200 201 sh = QSizeF(titleRect.height() + (titlePadding() * 2), titleRect.width());
201 202 break;
202 203 }
@@ -100,7 +100,7 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
100 100
101 101 for (int i = 0; i < m_bars.count(); i++) {
102 102 m_bars.at(i)->setRect(layout.at(i));
103 QGraphicsSimpleTextItem *label = m_labels.at(i);
103 QGraphicsTextItem *label = m_labels.at(i);
104 104 label->setPos(layout.at(i).center() - label->boundingRect().center());
105 105
106 106 }
@@ -135,7 +135,7 void AbstractBarChartItem::handleLayoutChanged()
135 135
136 136 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
137 137 {
138 foreach (QGraphicsSimpleTextItem *label, m_labels)
138 foreach (QGraphicsTextItem *label, m_labels)
139 139 label->setVisible(visible);
140 140 update();
141 141 }
@@ -164,7 +164,7 void AbstractBarChartItem::handleDataStructureChanged()
164 164 // m_layout.append(QRectF(0, 0, 1, 1));
165 165
166 166 // Labels
167 m_labels.append(new QGraphicsSimpleTextItem(this));
167 m_labels.append(new QGraphicsTextItem(this));
168 168 }
169 169 }
170 170
@@ -206,10 +206,10 void AbstractBarChartItem::handleUpdatedBars()
206 206 bar->setBrush(barSet->m_brush);
207 207 bar->update();
208 208
209 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
210 label->setText(QString("%1").arg(barSet->value(category)));
209 QGraphicsTextItem *label = m_labels.at(itemIndex);
210 label->setHtml(QString("%1").arg(barSet->value(category)));
211 211 label->setFont(barSet->m_labelFont);
212 label->setBrush(barSet->m_labelBrush);
212 label->setDefaultTextColor(barSet->m_labelBrush.color());
213 213 label->update();
214 214 itemIndex++;
215 215 }
@@ -85,7 +85,7 protected:
85 85
86 86 QAbstractBarSeries *m_series; // Not owned.
87 87 QList<Bar *> m_bars;
88 QList<QGraphicsSimpleTextItem *> m_labels;
88 QList<QGraphicsTextItem *> m_labels;
89 89 QSizeF m_oldSize;
90 90 };
91 91
@@ -108,14 +108,14 void HorizontalPercentBarChartItem::handleUpdatedBars()
108 108 bar->setBrush(barSet->m_brush);
109 109 bar->update();
110 110
111 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
111 QGraphicsTextItem *label = m_labels.at(itemIndex);
112 112 int p = m_series->d_func()->percentageAt(set, category) * 100;
113 113 QString vString(QString::number(p));
114 114 vString.truncate(3);
115 115 vString.append("%");
116 label->setText(vString);
116 label->setHtml(vString);
117 117 label->setFont(barSet->m_labelFont);
118 label->setBrush(barSet->m_labelBrush);
118 label->setDefaultTextColor(barSet->m_labelBrush.color());
119 119 label->update();
120 120 itemIndex++;
121 121 }
@@ -110,14 +110,14 void PercentBarChartItem::handleUpdatedBars()
110 110 bar->setBrush(barSet->m_brush);
111 111 bar->update();
112 112
113 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
113 QGraphicsTextItem *label = m_labels.at(itemIndex);
114 114 int p = m_series->d_func()->percentageAt(set, category) * 100;
115 115 QString vString(QString::number(p));
116 116 vString.truncate(3);
117 117 vString.append("%");
118 label->setText(vString);
118 label->setHtml(vString);
119 119 label->setFont(barSet->m_labelFont);
120 label->setBrush(barSet->m_labelBrush);
120 label->setDefaultTextColor(barSet->m_labelBrush.color());
121 121 label->update();
122 122 itemIndex++;
123 123 }
@@ -36,6 +36,15
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 QGraphicsTextItem *dummyTextItem = 0;
40 class StaticDummyTextDeleter
41 {
42 public:
43 StaticDummyTextDeleter() {}
44 ~StaticDummyTextDeleter() { delete dummyTextItem; }
45 };
46 StaticDummyTextDeleter staticDummyTextDeleter;
47
39 48 ChartPresenter::ChartPresenter(QChart *chart, QChart::ChartType type)
40 49 : QObject(chart),
41 50 m_chart(chart),
@@ -286,7 +295,7 QFont ChartPresenter::titleFont() const
286 295 void ChartPresenter::setTitleBrush(const QBrush &brush)
287 296 {
288 297 createTitleItem();
289 m_title->setBrush(brush);
298 m_title->setDefaultTextColor(brush.color());
290 299 m_layout->invalidate();
291 300 }
292 301
@@ -294,7 +303,7 QBrush ChartPresenter::titleBrush() const
294 303 {
295 304 if (!m_title)
296 305 return QBrush();
297 return m_title->brush();
306 return QBrush(m_title->defaultTextColor());
298 307 }
299 308
300 309 void ChartPresenter::setBackgroundVisible(bool visible)
@@ -378,6 +387,58 ChartTitle *ChartPresenter::titleElement()
378 387 return m_title;
379 388 }
380 389
390 QRectF ChartPresenter::textBoundingRect(const QFont &font, const QString &text, qreal angle)
391 {
392 if (!dummyTextItem)
393 dummyTextItem = new QGraphicsTextItem;
394
395 dummyTextItem->setFont(font);
396 dummyTextItem->setHtml(text);
397 QRectF boundingRect = dummyTextItem->boundingRect();
398
399 // Take rotation into account
400 if (angle) {
401 QTransform transform;
402 transform.rotate(angle);
403 boundingRect = transform.mapRect(boundingRect);
404 }
405
406 return boundingRect;
407 }
408
409 // boundingRect parameter returns the rotated bounding rect of the text
410 QString ChartPresenter::truncatedText(const QFont &font, const QString &text, qreal angle,
411 qreal maxSize, Qt::Orientation constraintOrientation,
412 QRectF &boundingRect)
413 {
414 QString truncatedString(text);
415 boundingRect = textBoundingRect(font, truncatedString, angle);
416 qreal checkDimension = ((constraintOrientation == Qt::Horizontal)
417 ? boundingRect.width() : boundingRect.height());
418 if (checkDimension > maxSize) {
419 truncatedString.append("...");
420 while (checkDimension > maxSize && truncatedString.length() > 3) {
421 // Crude truncation logic - simply remove any html tag completely
422 int removeIndex(-1);
423 int removeCount(1);
424 if (truncatedString.at(truncatedString.length() - 4) == QLatin1Char('>')) {
425 removeIndex = truncatedString.lastIndexOf(QLatin1Char('<'));
426 if (removeIndex != -1)
427 removeCount = truncatedString.length() - 3 - removeIndex;
428 }
429 if (removeIndex == -1)
430 removeIndex = truncatedString.length() - 4;
431
432 truncatedString.remove(removeIndex, removeCount);
433 boundingRect = textBoundingRect(font, truncatedString, angle);
434 checkDimension = ((constraintOrientation == Qt::Horizontal)
435 ? boundingRect.width() : boundingRect.height());
436 }
437 }
438
439 return truncatedString;
440 }
441
381 442 #include "moc_chartpresenter_p.cpp"
382 443
383 444 QTCOMMERCIALCHART_END_NAMESPACE
@@ -141,6 +141,9 public:
141 141 QChart::ChartType chartType() const { return m_chart->chartType(); }
142 142 QChart *chart() { return m_chart; }
143 143
144 static QRectF textBoundingRect(const QFont &font, const QString &text, qreal angle = 0.0);
145 static QString truncatedText(const QFont &font, const QString &text, qreal angle, qreal maxSize,
146 Qt::Orientation constraintOrientation, QRectF &boundingRect);
144 147 private:
145 148 void createBackgroundItem();
146 149 void createPlotAreaBackgroundItem();
@@ -19,6 +19,7
19 19 ****************************************************************************/
20 20
21 21 #include "charttitle_p.h"
22 #include "chartpresenter_p.h"
22 23 #include <QFont>
23 24 #include <QFontMetrics>
24 25 #include <QDebug>
@@ -26,7 +27,7
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 28
28 29 ChartTitle::ChartTitle(QGraphicsItem *parent)
29 : QGraphicsSimpleTextItem(parent)
30 : QGraphicsTextItem(parent)
30 31 {
31 32
32 33 }
@@ -48,19 +49,7 QString ChartTitle::text() const
48 49
49 50 void ChartTitle::setGeometry(const QRectF &rect)
50 51 {
51 QFontMetrics fn(font());
52
53 int width = rect.width();
54
55 if (fn.boundingRect(m_text).width() > width) {
56 QString string = m_text + "...";
57 while (fn.boundingRect(string).width() > width && string.length() > 3)
58 string.remove(string.length() - 4, 1);
59 QGraphicsSimpleTextItem::setText(string);
60 } else {
61 QGraphicsSimpleTextItem::setText(m_text);
62 }
63
52 QGraphicsTextItem::setHtml(ChartPresenter::truncatedText(font(), m_text, qreal(0.0), rect.width(), Qt::Horizontal, QRectF()));
64 53 setPos(rect.topLeft());
65 54 }
66 55
@@ -68,22 +57,25 void ChartTitle::setGeometry(const QRectF &rect)
68 57 QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
69 58 {
70 59 Q_UNUSED(constraint);
71 QFontMetrics fn(font());
72 60 QSizeF sh;
73 61
74 62 switch (which) {
75 case Qt::MinimumSize:
76 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
63 case Qt::MinimumSize: {
64 QRectF titleRect = ChartPresenter::textBoundingRect(font(), "...");
65 sh = QSizeF(titleRect.width(), titleRect.height());
77 66 break;
67 }
78 68 case Qt::PreferredSize:
79 sh = fn.boundingRect(m_text).size();
80 break;
81 case Qt::MaximumSize:
82 sh = fn.boundingRect(m_text).size();
69 case Qt::MaximumSize: {
70 QRectF titleRect = ChartPresenter::textBoundingRect(font(), m_text);
71 sh = QSizeF(titleRect.width(), titleRect.height());
83 72 break;
84 case Qt::MinimumDescent:
73 }
74 case Qt::MinimumDescent: {
75 QFontMetrics fn(font());
85 76 sh = QSizeF(0, fn.descent());
86 77 break;
78 }
87 79 default:
88 80 break;
89 81 }
@@ -31,11 +31,11
31 31 #define CHARTTITLE_P_H_
32 32
33 33 #include "qchartglobal.h"
34 #include <QGraphicsSimpleTextItem>
34 #include <QGraphicsTextItem>
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 class ChartTitle : public QGraphicsSimpleTextItem
38 class ChartTitle : public QGraphicsTextItem
39 39 {
40 40 public:
41 41 ChartTitle(QGraphicsItem *parent = 0);
@@ -57,7 +57,7 QRectF PolarChartLayout::calculateAxisGeometry(const QRectF &geometry, const QLi
57 57 && chartAxis->axis()->isTitleVisible()
58 58 && !chartAxis->axis()->titleText().isEmpty()) {
59 59 // If axis has angular title, adjust geometry down by the space title takes
60 QRectF dummyRect = chartAxis->textBoundingRect(chartAxis->axis()->titleFont(), chartAxis->axis()->titleText());
60 QRectF dummyRect = ChartPresenter::textBoundingRect(chartAxis->axis()->titleFont(), chartAxis->axis()->titleText());
61 61 titleHeight = (dummyRect.height() / 2.0) + chartAxis->titlePadding();
62 62 }
63 63 }
@@ -20,13 +20,14
20 20
21 21 #include <QPainter>
22 22 #include <QGraphicsSceneEvent>
23 #include <QGraphicsSimpleTextItem>
23 #include <QGraphicsTextItem>
24 24
25 25 #include "qlegend.h"
26 26 #include "qlegend_p.h"
27 27 #include "qlegendmarker.h"
28 28 #include "qlegendmarker_p.h"
29 29 #include "legendmarkeritem_p.h"
30 #include "chartpresenter_p.h"
30 31
31 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 33
@@ -35,7 +36,7 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject
35 36 m_marker(marker),
36 37 m_markerRect(0,0,10.0,10.0),
37 38 m_boundingRect(0,0,0,0),
38 m_textItem(new QGraphicsSimpleTextItem(this)),
39 m_textItem(new QGraphicsTextItem(this)),
39 40 m_rectItem(new QGraphicsRectItem(this)),
40 41 m_margin(4),
41 42 m_space(4),
@@ -99,41 +100,32 QString LegendMarkerItem::label() const
99 100
100 101 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
101 102 {
102 m_textItem->setBrush(brush);
103 m_textItem->setDefaultTextColor(brush.color());
103 104 }
104 105
105 106 QBrush LegendMarkerItem::labelBrush() const
106 107 {
107 return m_textItem->brush();
108 return QBrush(m_textItem->defaultTextColor());
108 109 }
109 110
110 111 void LegendMarkerItem::setGeometry(const QRectF &rect)
111 112 {
112 QFontMetrics fn (m_font);
113
114 113 int width = rect.width();
115 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
116 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
117
118 if (fn.boundingRect(m_label).width() + x > width)
119 {
120 QString string = m_label + "...";
121 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
122 string.remove(string.length() - 4, 1);
123 m_textItem->setText(string);
124 }
125 else
126 m_textItem->setText(m_label);
114 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
115 QRectF truncatedRect;
127 116
128 const QRectF &textRect = m_textItem->boundingRect();
117 m_textItem->setHtml(ChartPresenter::truncatedText(m_font, m_label, qreal(0.0), width - x, Qt::Horizontal, truncatedRect));
129 118
119 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
120
121 const QRectF &textRect = m_textItem->boundingRect();
130 122
131 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
123 m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2);
132 124 m_rectItem->setRect(m_markerRect);
133 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
125 m_rectItem->setPos(m_margin, y / 2 - m_markerRect.height() / 2);
134 126
135 127 prepareGeometryChange();
136 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
128 m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y);
137 129 }
138 130
139 131 QRectF LegendMarkerItem::boundingRect() const
@@ -35,7 +35,7
35 35 #include <QFont>
36 36 #include <QBrush>
37 37 #include <QPen>
38 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsTextItem>
39 39 #include <QGraphicsLayoutItem>
40 40
41 41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -78,7 +78,7 protected:
78 78 QLegendMarkerPrivate *m_marker; // Knows
79 79 QRectF m_markerRect;
80 80 QRectF m_boundingRect;
81 QGraphicsSimpleTextItem *m_textItem;
81 QGraphicsTextItem *m_textItem;
82 82 QGraphicsRectItem *m_rectItem;
83 83 qreal m_margin;
84 84 qreal m_space;
@@ -34,7 +34,6
34 34 #include <QGraphicsObject>
35 35 #include <QBrush>
36 36 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
38 37 #include <QGraphicsLayoutItem>
39 38
40 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -75,11 +75,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
75 75
76 76 /*!
77 77 \property QAbstractSeries::name
78 \brief name of the series property. The name is shown in legend for QXYSeries.
78 \brief name of the series property. The name is shown in legend for series and supports html formatting.
79 79 */
80 80 /*!
81 81 \qmlproperty string AbstractSeries::name
82 Name of the series. The name is shown in legend for QXYSeries.
82 Name of the series. The name is shown in legend for series and supports html formatting.
83 83 */
84 84
85 85 /*!
@@ -119,7 +119,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
119 119
120 120 /*!
121 121 \property QChart::title
122 Title is the name (label) of a chart. It is shown as a headline on top of the chart.
122 Title is the name (label) of a chart. It is shown as a headline on top of the chart. Chart title supports html formatting.
123 123 */
124 124
125 125 /*!
@@ -754,7 +754,7 void tst_QChart::titleBrush()
754 754 {
755 755 QFETCH(QBrush, titleBrush);
756 756 m_chart->setTitleBrush(titleBrush);
757 QCOMPARE(m_chart->titleBrush(), titleBrush);
757 QCOMPARE(m_chart->titleBrush().color(), titleBrush.color());
758 758 }
759 759
760 760 void tst_QChart::titleFont_data()
General Comments 0
You need to be logged in to leave comments. Login now