##// END OF EJS Templates
Fix issue with ghost axes being drawn when last axis removed...
Miikka Heikkinen -
r2836:7a48aa2a436d
parent child
Show More
@@ -1,262 +1,264
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/cartesianchartaxis_p.h>
20 20 #include <QtCharts/QAbstractAxis>
21 21 #include <private/qabstractaxis_p.h>
22 22 #include <private/chartpresenter_p.h>
23 23 #include <private/abstractchartlayout_p.h>
24 24 #include <private/abstractdomain_p.h>
25 25 #include <private/linearrowitem_p.h>
26 26 #include <QtCharts/QValueAxis>
27 27 #include <QtCharts/QLogValueAxis>
28 28 #include <QtWidgets/QGraphicsLayout>
29 29 #include <QtGui/QTextDocument>
30 30
31 31 QT_CHARTS_BEGIN_NAMESPACE
32 32
33 33 CartesianChartAxis::CartesianChartAxis(QAbstractAxis *axis, QGraphicsItem *item , bool intervalAxis)
34 34 : ChartAxisElement(axis, item, intervalAxis)
35 35 {
36 36 Q_ASSERT(item);
37 37 }
38 38
39 39
40 40 CartesianChartAxis::~CartesianChartAxis()
41 41 {
42 42 }
43 43
44 44 void CartesianChartAxis::createItems(int count)
45 45 {
46 46 if (arrowItems().size() == 0) {
47 47 QGraphicsLineItem *arrow = new LineArrowItem(this, this);
48 48 arrow->setPen(axis()->linePen());
49 49 arrowGroup()->addToGroup(arrow);
50 50 }
51 51
52 52 if (intervalAxis() && gridItems().size() == 0) {
53 53 for (int i = 0 ; i < 2 ; i ++){
54 54 QGraphicsLineItem *item = new QGraphicsLineItem(this);
55 55 item->setPen(axis()->gridLinePen());
56 56 gridGroup()->addToGroup(item);
57 57 QGraphicsRectItem *shades = new QGraphicsRectItem(this);
58 58 shades->setPen(axis()->shadesPen());
59 59 shades->setBrush(axis()->shadesBrush());
60 60 shadeGroup()->addToGroup(shades);
61 61 }
62 62 }
63 63
64 64 QGraphicsTextItem *title = titleItem();
65 65 title->setFont(axis()->titleFont());
66 66 title->setDefaultTextColor(axis()->titleBrush().color());
67 67 title->setHtml(axis()->titleText());
68 68
69 69 for (int i = 0; i < count; ++i) {
70 70 QGraphicsLineItem *arrow = new QGraphicsLineItem(this);
71 71 QGraphicsLineItem *grid = new QGraphicsLineItem(this);
72 72 QGraphicsTextItem *label = new QGraphicsTextItem(this);
73 73 label->document()->setDocumentMargin(ChartPresenter::textMargin());
74 74 arrow->setPen(axis()->linePen());
75 75 grid->setPen(axis()->gridLinePen());
76 76 label->setFont(axis()->labelsFont());
77 77 label->setDefaultTextColor(axis()->labelsBrush().color());
78 78 label->setRotation(axis()->labelsAngle());
79 79 arrowGroup()->addToGroup(arrow);
80 80 gridGroup()->addToGroup(grid);
81 81 labelGroup()->addToGroup(label);
82 82
83 83 if (gridItems().size() == 1 || (((gridItems().size() + 1) % 2) && gridItems().size() > 0)) {
84 84 QGraphicsRectItem *shades = new QGraphicsRectItem(this);
85 85 shades->setPen(axis()->shadesPen());
86 86 shades->setBrush(axis()->shadesBrush());
87 87 shadeGroup()->addToGroup(shades);
88 88 }
89 89 }
90 90 }
91 91
92 92 void CartesianChartAxis::updateMinorTickItems()
93 93 {
94 94 QValueAxis *valueAxis = qobject_cast<QValueAxis *>(this->axis());
95 95 if (valueAxis) {
96 96 int currentCount = minorArrowItems().size();
97 97 int expectedCount = valueAxis->minorTickCount() * (valueAxis->tickCount() - 1);
98 98 int diff = expectedCount - currentCount;
99 99 if (diff > 0) {
100 100 for (int i = 0; i < diff; i++) {
101 101 QGraphicsLineItem *minorArrow = new QGraphicsLineItem(this);
102 102 QGraphicsLineItem *minorGrid = new QGraphicsLineItem(this);
103 103 minorArrow->setPen(valueAxis->linePen());
104 104 minorGrid->setPen(valueAxis->minorGridLinePen());
105 105 minorArrowGroup()->addToGroup(minorArrow);
106 106 minorGridGroup()->addToGroup(minorGrid);
107 107 }
108 108 } else {
109 109 QList<QGraphicsItem *> minorGridLines = minorGridItems();
110 110 QList<QGraphicsItem *> minorArrows = minorArrowItems();
111 111 for (int i = 0; i > diff; i--) {
112 112 if (!minorGridLines.isEmpty())
113 113 delete(minorGridLines.takeLast());
114 114 if (!minorArrows.isEmpty())
115 115 delete(minorArrows.takeLast());
116 116 }
117 117 }
118 118 }
119 119 }
120 120
121 121 void CartesianChartAxis::deleteItems(int count)
122 122 {
123 123 QList<QGraphicsItem *> lines = gridItems();
124 124 QList<QGraphicsItem *> labels = labelItems();
125 125 QList<QGraphicsItem *> shades = shadeItems();
126 126 QList<QGraphicsItem *> axis = arrowItems();
127 127
128 128 for (int i = 0; i < count; ++i) {
129 129 if (lines.size() == 1 || (((lines.size() + 1) % 2) && lines.size() > 0))
130 130 delete(shades.takeLast());
131 131 delete(lines.takeLast());
132 132 delete(labels.takeLast());
133 133 delete(axis.takeLast());
134 134 }
135 135 }
136 136
137 137 void CartesianChartAxis::updateLayout(QVector<qreal> &layout)
138 138 {
139 139 int diff = ChartAxisElement::layout().size() - layout.size();
140 140
141 141 if (diff > 0)
142 142 deleteItems(diff);
143 143 else if (diff < 0)
144 144 createItems(-diff);
145 145
146 146 updateMinorTickItems();
147 147
148 148 if (animation()) {
149 149 switch (presenter()->state()) {
150 150 case ChartPresenter::ZoomInState:
151 151 animation()->setAnimationType(AxisAnimation::ZoomInAnimation);
152 152 animation()->setAnimationPoint(presenter()->statePoint());
153 153 break;
154 154 case ChartPresenter::ZoomOutState:
155 155 animation()->setAnimationType(AxisAnimation::ZoomOutAnimation);
156 156 animation()->setAnimationPoint(presenter()->statePoint());
157 157 break;
158 158 case ChartPresenter::ScrollUpState:
159 159 case ChartPresenter::ScrollLeftState:
160 160 animation()->setAnimationType(AxisAnimation::MoveBackwordAnimation);
161 161 break;
162 162 case ChartPresenter::ScrollDownState:
163 163 case ChartPresenter::ScrollRightState:
164 164 animation()->setAnimationType(AxisAnimation::MoveForwardAnimation);
165 165 break;
166 166 case ChartPresenter::ShowState:
167 167 animation()->setAnimationType(AxisAnimation::DefaultAnimation);
168 168 break;
169 169 }
170 170 animation()->setValues(ChartAxisElement::layout(), layout);
171 171 presenter()->startAnimation(animation());
172 172 } else {
173 173 setLayout(layout);
174 174 updateGeometry();
175 175 }
176 176 }
177 177
178 178 bool CartesianChartAxis::isEmpty()
179 179 {
180 180 return axisGeometry().isEmpty()
181 181 || gridGeometry().isEmpty()
182 182 || qFuzzyCompare(min(), max());
183 183 }
184 184
185 185 void CartesianChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
186 186 {
187 187 m_gridRect = grid;
188 188 setAxisGeometry(axis);
189 189
190 if (isEmpty())
190 if (isEmpty()) {
191 prepareGeometryChange();
191 192 return;
193 }
192 194
193 195 QVector<qreal> layout = calculateLayout();
194 196 updateLayout(layout);
195 197 }
196 198
197 199 QSizeF CartesianChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
198 200 {
199 201 Q_UNUSED(which);
200 202 Q_UNUSED(constraint);
201 203 return QSizeF();
202 204 }
203 205
204 206 void CartesianChartAxis::handleArrowPenChanged(const QPen &pen)
205 207 {
206 208 foreach (QGraphicsItem *item, arrowItems())
207 209 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
208 210 }
209 211
210 212 void CartesianChartAxis::handleGridPenChanged(const QPen &pen)
211 213 {
212 214 foreach (QGraphicsItem *item, gridItems())
213 215 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
214 216 }
215 217
216 218 void CartesianChartAxis::handleMinorArrowPenChanged(const QPen &pen)
217 219 {
218 220 foreach (QGraphicsItem *item, minorArrowItems())
219 221 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
220 222 }
221 223
222 224 void CartesianChartAxis::handleMinorGridPenChanged(const QPen &pen)
223 225 {
224 226 foreach (QGraphicsItem *item, minorGridItems())
225 227 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
226 228 }
227 229
228 230 void CartesianChartAxis::handleGridLineColorChanged(const QColor &color)
229 231 {
230 232 foreach (QGraphicsItem *item, gridItems()) {
231 233 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem *>(item);
232 234 QPen pen = lineItem->pen();
233 235 pen.setColor(color);
234 236 lineItem->setPen(pen);
235 237 }
236 238 }
237 239
238 240 void CartesianChartAxis::handleMinorGridLineColorChanged(const QColor &color)
239 241 {
240 242 foreach (QGraphicsItem *item, minorGridItems()) {
241 243 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem *>(item);
242 244 QPen pen = lineItem->pen();
243 245 pen.setColor(color);
244 246 lineItem->setPen(pen);
245 247 }
246 248 }
247 249
248 250 void CartesianChartAxis::handleShadesBrushChanged(const QBrush &brush)
249 251 {
250 252 foreach (QGraphicsItem *item, shadeItems())
251 253 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
252 254 }
253 255
254 256 void CartesianChartAxis::handleShadesPenChanged(const QPen &pen)
255 257 {
256 258 foreach (QGraphicsItem *item, shadeItems())
257 259 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
258 260 }
259 261
260 262 #include "moc_cartesianchartaxis_p.cpp"
261 263
262 264 QT_CHARTS_END_NAMESPACE
@@ -1,127 +1,129
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/polarchartaxis_p.h>
20 20 #include <private/qabstractaxis_p.h>
21 21 #include <private/chartpresenter_p.h>
22 22 #include <QtCharts/QValueAxis>
23 23
24 24 QT_CHARTS_BEGIN_NAMESPACE
25 25
26 26 PolarChartAxis::PolarChartAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
27 27 : ChartAxisElement(axis, item, intervalAxis)
28 28 {
29 29 }
30 30
31 31 PolarChartAxis::~PolarChartAxis()
32 32 {
33 33
34 34 }
35 35
36 36 void PolarChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
37 37 {
38 38 Q_UNUSED(grid);
39 39 setAxisGeometry(axis);
40 40
41 if (isEmpty())
41 if (isEmpty()) {
42 prepareGeometryChange();
42 43 return;
44 }
43 45
44 46 QVector<qreal> layout = calculateLayout();
45 47 updateLayout(layout);
46 48 }
47 49
48 50 QRectF PolarChartAxis::gridGeometry() const
49 51 {
50 52 return QRectF();
51 53 }
52 54
53 55 void PolarChartAxis::updateLayout(QVector<qreal> &layout)
54 56 {
55 57 int diff = ChartAxisElement::layout().size() - layout.size();
56 58
57 59 if (animation()) {
58 60 switch (presenter()->state()) {
59 61 case ChartPresenter::ZoomInState:
60 62 case ChartPresenter::ZoomOutState:
61 63 case ChartPresenter::ScrollUpState:
62 64 case ChartPresenter::ScrollLeftState:
63 65 case ChartPresenter::ScrollDownState:
64 66 case ChartPresenter::ScrollRightState:
65 67 case ChartPresenter::ShowState:
66 68 animation()->setAnimationType(AxisAnimation::DefaultAnimation);
67 69 break;
68 70 }
69 71 // Update to "old" geometry before starting animation to avoid incorrectly sized
70 72 // axes lingering in wrong position compared to series plot before animation can kick in.
71 73 // Note that the position mismatch still exists even with this update, but it will be
72 74 // far less ugly.
73 75 updateGeometry();
74 76 }
75 77
76 78 if (diff > 0)
77 79 deleteItems(diff);
78 80 else if (diff < 0)
79 81 createItems(-diff);
80 82
81 83 updateMinorTickItems();
82 84
83 85 if (animation()) {
84 86 animation()->setValues(ChartAxisElement::layout(), layout);
85 87 presenter()->startAnimation(animation());
86 88 } else {
87 89 setLayout(layout);
88 90 updateGeometry();
89 91 }
90 92 }
91 93
92 94 bool PolarChartAxis::isEmpty()
93 95 {
94 96 return !axisGeometry().isValid() || qFuzzyIsNull(min() - max());
95 97 }
96 98
97 99 void PolarChartAxis::deleteItems(int count)
98 100 {
99 101 QList<QGraphicsItem *> gridLines = gridItems();
100 102 QList<QGraphicsItem *> labels = labelItems();
101 103 QList<QGraphicsItem *> shades = shadeItems();
102 104 QList<QGraphicsItem *> axis = arrowItems();
103 105
104 106 for (int i = 0; i < count; ++i) {
105 107 if (gridItems().size() == 1 || (((gridLines.size() + 1) % 2) && gridLines.size() > 0))
106 108 delete(shades.takeLast());
107 109 delete(gridLines.takeLast());
108 110 delete(labels.takeLast());
109 111 delete(axis.takeLast());
110 112 }
111 113 }
112 114
113 115 void PolarChartAxis::handleShadesBrushChanged(const QBrush &brush)
114 116 {
115 117 foreach (QGraphicsItem *item, shadeItems())
116 118 static_cast<QGraphicsPathItem *>(item)->setBrush(brush);
117 119 }
118 120
119 121 void PolarChartAxis::handleShadesPenChanged(const QPen &pen)
120 122 {
121 123 foreach (QGraphicsItem *item, shadeItems())
122 124 static_cast<QGraphicsPathItem *>(item)->setPen(pen);
123 125 }
124 126
125 127 #include "moc_polarchartaxis_p.cpp"
126 128
127 129 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now