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