##// END OF EJS Templates
Fix shades for log and category axes....
Miikka Heikkinen -
r2721:cc2d0eaf2462
parent child
Show More
@@ -1,198 +1,201
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <private/cartesianchartaxis_p.h>
22 22 #include <QtCharts/QAbstractAxis>
23 23 #include <private/qabstractaxis_p.h>
24 24 #include <private/chartpresenter_p.h>
25 25 #include <private/abstractchartlayout_p.h>
26 26 #include <private/abstractdomain_p.h>
27 27 #include <private/linearrowitem_p.h>
28 28 #include <QtCharts/QValueAxis>
29 29 #include <QtCharts/QLogValueAxis>
30 30 #include <QtWidgets/QGraphicsLayout>
31 31 #include <QtGui/QTextDocument>
32 32
33 33 QT_CHARTS_BEGIN_NAMESPACE
34 34
35 35 CartesianChartAxis::CartesianChartAxis(QAbstractAxis *axis, QGraphicsItem *item , bool intervalAxis)
36 36 : ChartAxisElement(axis, item, intervalAxis)
37 37 {
38 38 Q_ASSERT(item);
39 39 }
40 40
41 41
42 42 CartesianChartAxis::~CartesianChartAxis()
43 43 {
44 44 }
45 45
46 46 void CartesianChartAxis::createItems(int count)
47 47 {
48 48 if (arrowItems().size() == 0) {
49 49 QGraphicsLineItem *arrow = new LineArrowItem(this, this);
50 50 arrow->setPen(axis()->linePen());
51 51 arrowGroup()->addToGroup(arrow);
52 52 }
53 53
54 54 if (intervalAxis() && gridItems().size() == 0) {
55 55 for (int i = 0 ; i < 2 ; i ++){
56 56 QGraphicsLineItem *item = new QGraphicsLineItem(this);
57 57 item->setPen(axis()->gridLinePen());
58 58 gridGroup()->addToGroup(item);
59 QGraphicsRectItem *shades = new QGraphicsRectItem(this);
60 shades->setPen(axis()->shadesPen());
61 shades->setBrush(axis()->shadesBrush());
62 shadeGroup()->addToGroup(shades);
59 63 }
60 64 }
61 65
62 66 QGraphicsTextItem *title = titleItem();
63 67 title->setFont(axis()->titleFont());
64 68 title->setDefaultTextColor(axis()->titleBrush().color());
65 69 title->setHtml(axis()->titleText());
66 70
67 71 for (int i = 0; i < count; ++i) {
68 72 QGraphicsLineItem *arrow = new QGraphicsLineItem(this);
69 73 QGraphicsLineItem *grid = new QGraphicsLineItem(this);
70 74 QGraphicsTextItem *label = new QGraphicsTextItem(this);
71 75 label->document()->setDocumentMargin(ChartPresenter::textMargin());
72 76 arrow->setPen(axis()->linePen());
73 77 grid->setPen(axis()->gridLinePen());
74 78 label->setFont(axis()->labelsFont());
75 79 label->setDefaultTextColor(axis()->labelsBrush().color());
76 80 label->setRotation(axis()->labelsAngle());
77 81 arrowGroup()->addToGroup(arrow);
78 82 gridGroup()->addToGroup(grid);
79 83 labelGroup()->addToGroup(label);
80 84
81 if ((gridItems().size()) % 2 && gridItems().size() > 2) {
82 QGraphicsRectItem* shades = new QGraphicsRectItem(this);
85 if (gridItems().size() == 1 || (((gridItems().size() + 1) % 2) && gridItems().size() > 0)) {
86 QGraphicsRectItem *shades = new QGraphicsRectItem(this);
83 87 shades->setPen(axis()->shadesPen());
84 88 shades->setBrush(axis()->shadesBrush());
85 89 shadeGroup()->addToGroup(shades);
86 90 }
87 91 }
88
89 92 }
90 93
91 94 void CartesianChartAxis::deleteItems(int count)
92 95 {
93 96 QList<QGraphicsItem *> lines = gridItems();
94 97 QList<QGraphicsItem *> labels = labelItems();
95 98 QList<QGraphicsItem *> shades = shadeItems();
96 99 QList<QGraphicsItem *> axis = arrowItems();
97 100
98 101 for (int i = 0; i < count; ++i) {
99 if (lines.size() % 2 && lines.size() > 1)
102 if (lines.size() == 1 || (((lines.size() + 1) % 2) && lines.size() > 0))
100 103 delete(shades.takeLast());
101 104 delete(lines.takeLast());
102 105 delete(labels.takeLast());
103 106 delete(axis.takeLast());
104 107 }
105 108 }
106 109
107 110 void CartesianChartAxis::updateLayout(QVector<qreal> &layout)
108 111 {
109 112 int diff = ChartAxisElement::layout().size() - layout.size();
110 113
111 114 if (diff > 0)
112 115 deleteItems(diff);
113 116 else if (diff < 0)
114 117 createItems(-diff);
115 118
116 119 if (animation()) {
117 120 switch (presenter()->state()) {
118 121 case ChartPresenter::ZoomInState:
119 122 animation()->setAnimationType(AxisAnimation::ZoomInAnimation);
120 123 animation()->setAnimationPoint(presenter()->statePoint());
121 124 break;
122 125 case ChartPresenter::ZoomOutState:
123 126 animation()->setAnimationType(AxisAnimation::ZoomOutAnimation);
124 127 animation()->setAnimationPoint(presenter()->statePoint());
125 128 break;
126 129 case ChartPresenter::ScrollUpState:
127 130 case ChartPresenter::ScrollLeftState:
128 131 animation()->setAnimationType(AxisAnimation::MoveBackwordAnimation);
129 132 break;
130 133 case ChartPresenter::ScrollDownState:
131 134 case ChartPresenter::ScrollRightState:
132 135 animation()->setAnimationType(AxisAnimation::MoveForwardAnimation);
133 136 break;
134 137 case ChartPresenter::ShowState:
135 138 animation()->setAnimationType(AxisAnimation::DefaultAnimation);
136 139 break;
137 140 }
138 141 animation()->setValues(ChartAxisElement::layout(), layout);
139 142 presenter()->startAnimation(animation());
140 143 } else {
141 144 setLayout(layout);
142 145 updateGeometry();
143 146 }
144 147 }
145 148
146 149 bool CartesianChartAxis::isEmpty()
147 150 {
148 151 return axisGeometry().isEmpty()
149 152 || gridGeometry().isEmpty()
150 153 || qFuzzyCompare(min(), max());
151 154 }
152 155
153 156 void CartesianChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
154 157 {
155 158 m_gridRect = grid;
156 159 setAxisGeometry(axis);
157 160
158 161 if (isEmpty())
159 162 return;
160 163
161 164 QVector<qreal> layout = calculateLayout();
162 165 updateLayout(layout);
163 166 }
164 167
165 168 QSizeF CartesianChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
166 169 {
167 170 Q_UNUSED(which);
168 171 Q_UNUSED(constraint);
169 172 return QSizeF();
170 173 }
171 174
172 175 void CartesianChartAxis::handleArrowPenChanged(const QPen &pen)
173 176 {
174 177 foreach (QGraphicsItem *item, arrowItems())
175 178 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
176 179 }
177 180
178 181 void CartesianChartAxis::handleGridPenChanged(const QPen &pen)
179 182 {
180 183 foreach (QGraphicsItem *item, gridItems())
181 184 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
182 185 }
183 186
184 187 void CartesianChartAxis::handleShadesBrushChanged(const QBrush &brush)
185 188 {
186 189 foreach (QGraphicsItem *item, shadeItems())
187 190 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
188 191 }
189 192
190 193 void CartesianChartAxis::handleShadesPenChanged(const QPen &pen)
191 194 {
192 195 foreach (QGraphicsItem *item, shadeItems())
193 196 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
194 197 }
195 198
196 199 #include "moc_cartesianchartaxis_p.cpp"
197 200
198 201 QT_CHARTS_END_NAMESPACE
@@ -1,227 +1,242
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <private/horizontalaxis_p.h>
22 22 #include <private/qabstractaxis_p.h>
23 23 #include <private/chartpresenter_p.h>
24 24 #include <QtCore/QtMath>
25 25 #include <QtCore/QDebug>
26 26
27 27 QT_CHARTS_BEGIN_NAMESPACE
28 28
29 29 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
30 30 : CartesianChartAxis(axis, item, intervalAxis)
31 31 {
32 32 }
33 33
34 34 HorizontalAxis::~HorizontalAxis()
35 35 {
36 36 }
37 37
38 38 void HorizontalAxis::updateGeometry()
39 39 {
40 40 const QVector<qreal> &layout = ChartAxisElement::layout();
41 41
42 42 if (layout.isEmpty())
43 43 return;
44 44
45 45 QStringList labelList = labels();
46 46
47 47 QList<QGraphicsItem *> lines = gridItems();
48 48 QList<QGraphicsItem *> labels = labelItems();
49 49 QList<QGraphicsItem *> shades = shadeItems();
50 50 QList<QGraphicsItem *> arrow = arrowItems();
51 51 QGraphicsTextItem *title = titleItem();
52 52
53 53 Q_ASSERT(labels.size() == labelList.size());
54 54 Q_ASSERT(layout.size() == labelList.size());
55 55
56 56 const QRectF &axisRect = axisGeometry();
57 57 const QRectF &gridRect = gridGeometry();
58 58
59 59 //arrow
60 60 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(arrow.at(0));
61 61
62 62 if (axis()->alignment() == Qt::AlignTop)
63 63 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
64 64 else if (axis()->alignment() == Qt::AlignBottom)
65 65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
66 66
67 67 qreal width = 0;
68 68 const QLatin1String ellipsis("...");
69 69
70 70 //title
71 71 QRectF titleBoundingRect;
72 72 QString titleText = axis()->titleText();
73 73 qreal availableSpace = axisRect.height() - labelPadding();
74 74 if (!titleText.isEmpty() && titleItem()->isVisible()) {
75 75 availableSpace -= titlePadding() * 2.0;
76 76 qreal minimumLabelHeight = ChartPresenter::textBoundingRect(axis()->labelsFont(),
77 77 QStringLiteral("...")).height();
78 78 qreal titleSpace = availableSpace - minimumLabelHeight;
79 79 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0),
80 80 gridRect.width(), titleSpace,
81 81 titleBoundingRect));
82 82 title->setTextWidth(titleBoundingRect.width());
83 83
84 84 titleBoundingRect = title->boundingRect();
85 85
86 86 QPointF center = gridRect.center() - titleBoundingRect.center();
87 87 if (axis()->alignment() == Qt::AlignTop)
88 88 title->setPos(center.x(), axisRect.top() + titlePadding());
89 89 else if (axis()->alignment() == Qt::AlignBottom)
90 90 title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePadding());
91 91
92 92 availableSpace -= titleBoundingRect.height();
93 93 }
94 94
95 95 for (int i = 0; i < layout.size(); ++i) {
96 96 //items
97 97 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
98 98 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
99 99 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
100 100
101 101 //grid line
102 102 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
103 103
104 104 //label text wrapping
105 105 QString text = labelList.at(i);
106 106 QRectF boundingRect;
107 107 // don't truncate empty labels
108 108 if (text.isEmpty()) {
109 109 labelItem->setHtml(text);
110 110 } else {
111 111 qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding());
112 112 QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
113 113 axis()->labelsAngle(),
114 114 labelWidth,
115 115 availableSpace, boundingRect);
116 116 labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
117 117 truncatedText).width());
118 118 labelItem->setHtml(truncatedText);
119 119 }
120 120
121 121 //label transformation origin point
122 122 const QRectF& rect = labelItem->boundingRect();
123 123 QPointF center = rect.center();
124 124 labelItem->setTransformOriginPoint(center.x(), center.y());
125 125 qreal heightDiff = rect.height() - boundingRect.height();
126 126 qreal widthDiff = rect.width() - boundingRect.width();
127 127
128 128 //ticks and label position
129 129 if (axis()->alignment() == Qt::AlignTop) {
130 130 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2.0) - labelPadding());
131 131 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
132 132 } else if (axis()->alignment() == Qt::AlignBottom) {
133 133 labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2.0) + labelPadding());
134 134 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
135 135 }
136 136
137 137 //label in between
138 138 bool forceHide = false;
139 139 if (intervalAxis() && (i + 1) != layout.size()) {
140 140 qreal leftBound = qMax(layout[i], gridRect.left());
141 141 qreal rightBound = qMin(layout[i + 1], gridRect.right());
142 142 const qreal delta = rightBound - leftBound;
143 143 // Hide label in case visible part of the category at the grid edge is too narrow
144 144 if (delta < boundingRect.width()
145 145 && (leftBound == gridRect.left() || rightBound == gridRect.right())
146 146 && !intervalAxis()) {
147 147 forceHide = true;
148 148 } else {
149 149 labelItem->setPos(leftBound + (delta / 2.0) - center.x(), labelItem->pos().y());
150 150 }
151 151 }
152 152
153 153 //label overlap detection - compensate one pixel for rounding errors
154 154 if ((labelItem->pos().x() < width && labelItem->toPlainText() == ellipsis) || forceHide ||
155 155 (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) ||
156 156 (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) {
157 157 labelItem->setVisible(false);
158 158 } else {
159 159 labelItem->setVisible(true);
160 160 width = boundingRect.width() + labelItem->pos().x();
161 161 }
162 162
163 163 //shades
164 if ((i + 1) % 2 && i > 1) {
165 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
166 qreal leftBound = qMax(layout[i - 1], gridRect.left());
167 qreal rightBound = qMin(layout[i], gridRect.right());
168 rectItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, gridRect.height());
169 if (rectItem->rect().width() <= 0.0)
170 rectItem->setVisible(false);
164 QGraphicsRectItem *shadeItem = 0;
165 if (i == 0)
166 shadeItem = static_cast<QGraphicsRectItem *>(shades.at(0));
167 else if (i % 2)
168 shadeItem = static_cast<QGraphicsRectItem *>(shades.at((i / 2) + 1));
169 if (shadeItem) {
170 qreal leftBound;
171 qreal rightBound;
172 if (i == 0) {
173 leftBound = gridRect.left();
174 rightBound = layout[0];
175 } else {
176 leftBound = layout[i];
177 if (i == layout.size() - 1)
178 rightBound = gridRect.right();
179 else
180 rightBound = qMin(layout[i + 1], gridRect.right());
181 }
182 shadeItem->setRect(leftBound, gridRect.top(), rightBound - leftBound,
183 gridRect.height());
184 if (shadeItem->rect().width() <= 0.0)
185 shadeItem->setVisible(false);
171 186 else
172 rectItem->setVisible(true);
187 shadeItem->setVisible(true);
173 188 }
174 189
175 190 // check if the grid line and the axis tick should be shown
176 191 qreal x = gridItem->line().p1().x();
177 192 if (x < gridRect.left() || x > gridRect.right()) {
178 193 gridItem->setVisible(false);
179 194 tickItem->setVisible(false);
180 195 } else {
181 196 gridItem->setVisible(true);
182 197 tickItem->setVisible(true);
183 198 }
184 199
185 200 }
186 201
187 202 //begin/end grid line in case labels between
188 203 if (intervalAxis()) {
189 204 QGraphicsLineItem *gridLine;
190 205 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
191 206 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
192 207 gridLine->setVisible(true);
193 208 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
194 209 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
195 210 gridLine->setVisible(true);
196 211 }
197 212 }
198 213
199 214 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
200 215 {
201 216 Q_UNUSED(constraint);
202 217 QSizeF sh(0,0);
203 218
204 219 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
205 220 return sh;
206 221
207 222 switch (which) {
208 223 case Qt::MinimumSize: {
209 224 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(),
210 225 QStringLiteral("..."));
211 226 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0));
212 227 break;
213 228 }
214 229 case Qt::MaximumSize:
215 230 case Qt::PreferredSize: {
216 231 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
217 232 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0));
218 233 break;
219 234 }
220 235 default:
221 236 break;
222 237 }
223 238
224 239 return sh;
225 240 }
226 241
227 242 QT_CHARTS_END_NAMESPACE
@@ -1,231 +1,247
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <private/verticalaxis_p.h>
22 22 #include <QtCharts/QAbstractAxis>
23 23 #include <private/chartpresenter_p.h>
24 24 #include <QtCore/QDebug>
25 25
26 26 QT_CHARTS_BEGIN_NAMESPACE
27 27
28 28 VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
29 29 : CartesianChartAxis(axis, item, intervalAxis)
30 30 {
31 31 }
32 32
33 33 VerticalAxis::~VerticalAxis()
34 34 {
35 35 }
36 36
37 37 void VerticalAxis::updateGeometry()
38 38 {
39 39 const QVector<qreal> &layout = ChartAxisElement::layout();
40 40
41 41 if (layout.isEmpty())
42 42 return;
43 43
44 44 QStringList labelList = labels();
45 45
46 46 QList<QGraphicsItem *> lines = gridItems();
47 47 QList<QGraphicsItem *> labels = labelItems();
48 48 QList<QGraphicsItem *> shades = shadeItems();
49 49 QList<QGraphicsItem *> arrow = arrowItems();
50 50 QGraphicsTextItem *title = titleItem();
51 51
52 52 Q_ASSERT(labels.size() == labelList.size());
53 53 Q_ASSERT(layout.size() == labelList.size());
54 54
55 55 const QRectF &axisRect = axisGeometry();
56 56 const QRectF &gridRect = gridGeometry();
57 57
58 58 qreal height = axisRect.bottom();
59 59
60 60
61 61 //arrow
62 62 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(arrow.at(0));
63 63
64 64 //arrow position
65 65 if (axis()->alignment() == Qt::AlignLeft)
66 66 arrowItem->setLine(axisRect.right(), gridRect.top(), axisRect.right(), gridRect.bottom());
67 67 else if (axis()->alignment() == Qt::AlignRight)
68 68 arrowItem->setLine(axisRect.left(), gridRect.top(), axisRect.left(), gridRect.bottom());
69 69
70 70 //title
71 71 QRectF titleBoundingRect;
72 72 QString titleText = axis()->titleText();
73 73 qreal availableSpace = axisRect.width() - labelPadding();
74 74 if (!titleText.isEmpty() && titleItem()->isVisible()) {
75 75 availableSpace -= titlePadding() * 2.0;
76 76 qreal minimumLabelWidth = ChartPresenter::textBoundingRect(axis()->labelsFont(),
77 77 QStringLiteral("...")).width();
78 78 qreal titleSpace = availableSpace - minimumLabelWidth;
79 79 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(90.0),
80 80 titleSpace, gridRect.height(),
81 81 titleBoundingRect));
82 82 title->setTextWidth(titleBoundingRect.height());
83 83
84 84 titleBoundingRect = title->boundingRect();
85 85
86 86 QPointF center = gridRect.center() - titleBoundingRect.center();
87 87 if (axis()->alignment() == Qt::AlignLeft)
88 88 title->setPos(axisRect.left() - titleBoundingRect.width() / 2.0 + titleBoundingRect.height() / 2.0 + titlePadding(), center.y());
89 89 else if (axis()->alignment() == Qt::AlignRight)
90 90 title->setPos(axisRect.right() - titleBoundingRect.width() / 2.0 - titleBoundingRect.height() / 2.0 - titlePadding(), center.y());
91 91
92 92 title->setTransformOriginPoint(titleBoundingRect.center());
93 93 title->setRotation(270);
94 94
95 95 availableSpace -= titleBoundingRect.height();
96 96 }
97 97
98 98 for (int i = 0; i < layout.size(); ++i) {
99 99 //items
100 100 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
101 101 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(arrow.at(i + 1));
102 102 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
103 103
104 104 //grid line
105 105 gridItem->setLine(gridRect.left(), layout[i], gridRect.right(), layout[i]);
106 106
107 107 //label text wrapping
108 108 QString text = labelList.at(i);
109 109 QRectF boundingRect;
110 110 // don't truncate empty labels
111 111 if (text.isEmpty()) {
112 112 labelItem->setHtml(text);
113 113 } else {
114 114 qreal labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding());
115 115 QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
116 116 axis()->labelsAngle(),
117 117 availableSpace,
118 118 labelHeight, boundingRect);
119 119 labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
120 120 truncatedText).width());
121 121 labelItem->setHtml(truncatedText);
122 122 }
123 123
124 124 //label transformation origin point
125 125 const QRectF &rect = labelItem->boundingRect();
126 126 QPointF center = rect.center();
127 127 labelItem->setTransformOriginPoint(center.x(), center.y());
128 128 qreal widthDiff = rect.width() - boundingRect.width();
129 129 qreal heightDiff = rect.height() - boundingRect.height();
130 130
131 131 //ticks and label position
132 132 if (axis()->alignment() == Qt::AlignLeft) {
133 133 labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2.0) - labelPadding(), layout[i] - center.y());
134 134 tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
135 135 } else if (axis()->alignment() == Qt::AlignRight) {
136 136 labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2.0), layout[i] - center.y());
137 137 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
138 138 }
139 139
140 140 //label in between
141 141 bool forceHide = false;
142 142 if (intervalAxis() && (i + 1) != layout.size()) {
143 143 qreal lowerBound = qMin(layout[i], gridRect.bottom());
144 144 qreal upperBound = qMax(layout[i + 1], gridRect.top());
145 145 const qreal delta = lowerBound - upperBound;
146 146 // Hide label in case visible part of the category at the grid edge is too narrow
147 147 if (delta < boundingRect.height()
148 148 && (lowerBound == gridRect.bottom() || upperBound == gridRect.top())
149 149 && !intervalAxis()) {
150 150 forceHide = true;
151 151 } else {
152 152 labelItem->setPos(labelItem->pos().x() , lowerBound - (delta / 2.0) - center.y());
153 153 }
154 154 }
155 155
156 156 //label overlap detection - compensate one pixel for rounding errors
157 157 if (labelItem->pos().y() + boundingRect.height() > height || forceHide ||
158 158 (labelItem->pos().y() + (heightDiff / 2.0) - 1.0) > axisRect.bottom() ||
159 159 labelItem->pos().y() + (heightDiff / 2.0) < (axisRect.top() - 1.0)) {
160 160 labelItem->setVisible(false);
161 161 }
162 162 else {
163 163 labelItem->setVisible(true);
164 164 height=labelItem->pos().y();
165 165 }
166 166
167 167 //shades
168 if ((i + 1) % 2 && i > 1) {
169 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
170 qreal lowerBound = qMin(layout[i - 1], gridRect.bottom());
171 qreal upperBound = qMax(layout[i], gridRect.top());
172 rectItem->setRect(gridRect.left(), upperBound, gridRect.width(), lowerBound - upperBound);
173 if (rectItem->rect().height() <= 0.0)
174 rectItem->setVisible(false);
168 QGraphicsRectItem *shadeItem = 0;
169 if (i == 0)
170 shadeItem = static_cast<QGraphicsRectItem *>(shades.at(0));
171 else if (i % 2)
172 shadeItem = static_cast<QGraphicsRectItem *>(shades.at((i / 2) + 1));
173 if (shadeItem) {
174 qreal lowerBound;
175 qreal upperBound;
176 if (i == 0) {
177 lowerBound = gridRect.bottom();
178 upperBound = layout[0];
179 } else {
180 lowerBound = layout[i];
181 if (i == layout.size() - 1)
182 upperBound = gridRect.top();
183 else
184 upperBound = qMax(layout[i + 1], gridRect.top());
185
186 }
187 shadeItem->setRect(gridRect.left(), upperBound, gridRect.width(),
188 lowerBound - upperBound);
189 if (shadeItem->rect().height() <= 0.0)
190 shadeItem->setVisible(false);
175 191 else
176 rectItem->setVisible(true);
192 shadeItem->setVisible(true);
177 193 }
178 194
179 195 // check if the grid line and the axis tick should be shown
180 196 qreal y = gridItem->line().p1().y();
181 197 if ((y < gridRect.top() || y > gridRect.bottom()))
182 198 {
183 199 gridItem->setVisible(false);
184 200 tickItem->setVisible(false);
185 201 }else{
186 202 gridItem->setVisible(true);
187 203 tickItem->setVisible(true);
188 204 }
189 205
190 206 }
191 207 //begin/end grid line in case labels between
192 208 if (intervalAxis()) {
193 209 QGraphicsLineItem *gridLine;
194 210 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
195 211 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
196 212 gridLine->setVisible(true);
197 213 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size() + 1));
198 214 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
199 215 gridLine->setVisible(true);
200 216 }
201 217 }
202 218
203 219 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
204 220 {
205 221 Q_UNUSED(constraint);
206 222 QSizeF sh(0, 0);
207 223
208 224 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
209 225 return sh;
210 226
211 227 switch (which) {
212 228 case Qt::MinimumSize: {
213 229 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(),
214 230 QStringLiteral("..."));
215 231 sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width());
216 232 break;
217 233 }
218 234 case Qt::MaximumSize:
219 235 case Qt::PreferredSize: {
220 236 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
221 237 sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width());
222 238 break;
223 239 }
224 240 default:
225 241 break;
226 242 }
227 243
228 244 return sh;
229 245 }
230 246
231 247 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now