##// END OF EJS Templates
Fix zorder of axis, and ticks
Michal Klocek -
r272:1946c776c83b
parent child
Show More
@@ -1,300 +1,317
1 1 #include "axisitem_p.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 5 #include <QDebug>
6 6
7 7 static int label_padding = 5;
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
12 12 ChartItem(parent),
13 13 m_type(type),
14 14 m_labelsAngle(0),
15 15 m_shadesEnabled(true),
16 16 m_grid(parent),
17 17 m_shades(parent),
18 18 m_labels(parent),
19 m_origin(0,0)
19 m_axis(parent)
20 20 {
21 21 //initial initialization
22 m_axis.setZValue(ChartPresenter::AxisZValue);
22 23 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 24 m_grid.setZValue(ChartPresenter::GridZValue);
25 setFlags(QGraphicsItem::ItemHasNoContents);
24 26 }
25 27
26 28 AxisItem::~AxisItem()
27 29 {
28 30 }
29 31
30 32 QRectF AxisItem::boundingRect() const
31 33 {
32 34 return m_rect;
33 35 }
34 36
35 37 void AxisItem::createItems(int count)
36 38 {
39 m_axis.addToGroup(new QGraphicsLineItem(this));
37 40 for (int i = 0; i < count; ++i) {
38 41 m_grid.addToGroup(new QGraphicsLineItem(this));
39 42 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
40 43 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
44 m_axis.addToGroup(new QGraphicsLineItem(this));
41 45 }
42 46 }
43 47
44 48 void AxisItem::clear()
45 49 {
46 50 foreach(QGraphicsItem* item , m_shades.childItems()) {
47 51 delete item;
48 52 }
49 53
50 54 foreach(QGraphicsItem* item , m_grid.childItems()) {
51 55 delete item;
52 56 }
53 57
54 58 foreach(QGraphicsItem* item , m_labels.childItems()) {
55 59 delete item;
56 60 }
57 61
62 foreach(QGraphicsItem* item , m_axis.childItems()) {
63 delete item;
64 }
65
58 66 m_thicksList.clear();
59 67
60 68 }
61 69
62 70 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 71 {
64 72
65 73 }
66 74
67 75 void AxisItem::updateItem(int count)
68 76 {
77 if(count ==0) return;
69 78
70 79 QList<QGraphicsItem *> lines = m_grid.childItems();
71 80 QList<QGraphicsItem *> labels = m_labels.childItems();
72 81 QList<QGraphicsItem *> shades = m_shades.childItems();
82 QList<QGraphicsItem *> axis = m_axis.childItems();
73 83
74 84 switch (m_type)
75 85 {
76 86 case X_AXIS:
77 87 {
78 88 const qreal deltaX = m_rect.width() / (count-1);
79 89
80 m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
90 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
91 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
81 92
82 93 for (int i = 0; i < count; ++i) {
83 94 int x = i * deltaX + m_rect.left();
84 95 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
85 96 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
86 97 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
87 98 labelItem->setText(m_thicksList.at(i));
88 99 QPointF center = labelItem->boundingRect().center();
89 100 labelItem->setTransformOriginPoint(center.x(), center.y());
90 101 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
91
92 102 if(i%2){
93 103 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
94 104 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
95 105 }
106 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
107 lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5);
96 108 }
97 109 }
98 110 break;
99 111
100 112 case Y_AXIS:
101 113 {
102 114 const qreal deltaY = m_rect.height()/ (count-1);
103 115
104 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
116 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
117 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
105 118
106 119 for (int i = 0; i < count; ++i) {
107 120 int y = i * -deltaY + m_rect.bottom();
108 121 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
109 122 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
110 123 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
111 124 labelItem->setText(m_thicksList.at(i));
112 125 QPointF center = labelItem->boundingRect().center();
113 126 labelItem->setTransformOriginPoint(center.x(), center.y());
114 127 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
115 128 if(i%2){
116 129 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
117 130 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
118 131 }
132 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
133 lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y);
119 134 }
120 135 }
121 136 break;
122 137 default:
123 138 qDebug()<<"Unknown axis type";
124 139 break;
125 140 }
126 141 }
127 142
128 143 void AxisItem::handleAxisUpdate(QChartAxis* axis)
129 144 {
130 145 if(axis->isAxisVisible()) {
131 146 setAxisOpacity(100);
132 147 }
133 148 else {
134 149 setAxisOpacity(0);
135 150 }
136 151
137 152 if(axis->isGridVisible()) {
138 153 setGridOpacity(100);
139 154 }
140 155 else {
141 156 setGridOpacity(0);
142 157 }
143 158
144 159 if(axis->isLabelsVisible())
145 160 {
146 161 setLabelsOpacity(100);
147 162 }
148 163 else {
149 164 setLabelsOpacity(0);
150 165 }
151 166
152 167 if(axis->isShadesVisible()) {
153 168 setShadesOpacity(axis->shadesOpacity());
154 169 }
155 170 else {
156 171 setShadesOpacity(0);
157 172 }
158 173
159 174 setLabelsAngle(axis->labelsAngle());
160 175 setAxisPen(axis->axisPen());
161 176 setLabelsPen(axis->labelsPen());
162 177 setLabelsBrush(axis->labelsBrush());
163 178 setLabelsFont(axis->labelFont());
164 179 setGridPen(axis->gridPen());
165 180 setShadesPen(axis->shadesPen());
166 181 setShadesBrush(axis->shadesBrush());
167 182 }
168 183
169 184 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
170 185 {
171 186 m_thicksList=labels;
172 187 QList<QGraphicsItem*> items = m_labels.childItems();
173 if(items.size()!=m_thicksList.size()){
188 //if(items.size()!=m_thicksList.size()){
174 189 clear();
175 190 m_thicksList=labels;
176 191 createItems(m_thicksList.size());
177 192 updateItem(m_thicksList.size());
178 193 items = m_labels.childItems();
179 194 handleAxisUpdate(axis);
180 }
195 // }
181 196
182 197 Q_ASSERT(items.size()==m_thicksList.size());
183 198
184 199 int i=0;
185 200 foreach(QGraphicsItem* item, items){
186 201 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
187 202 i++;
188 203 }
189 204 update();
190 205 }
191 206
192 207 void AxisItem::handleGeometryChanged(const QRectF& rect)
193 208 {
194 209 m_rect = rect;
195 210 updateItem(m_thicksList.size());
196 211 update();
197 212 }
198 213
199 214 void AxisItem::setAxisOpacity(qreal opacity)
200 215 {
201 216 m_axis.setOpacity(opacity);
202 217 }
203 218
204 219 qreal AxisItem::axisOpacity() const
205 220 {
206 221 return m_axis.opacity();
207 222 }
208 223
209 224 void AxisItem::setGridOpacity(qreal opacity)
210 225 {
211 226 m_grid.setOpacity(opacity);
212 227 }
213 228
214 229 qreal AxisItem::gridOpacity() const
215 230 {
216 231 return m_grid.opacity();
217 232 }
218 233
219 234 void AxisItem::setLabelsOpacity(qreal opacity)
220 235 {
221 236 m_labels.setOpacity(opacity);
222 237 }
223 238
224 239 qreal AxisItem::labelsOpacity() const
225 240 {
226 241 return m_labels.opacity();
227 242 }
228 243
229 244 void AxisItem::setShadesOpacity(qreal opacity)
230 245 {
231 246 m_shades.setOpacity(opacity);
232 247 }
233 248
234 249 qreal AxisItem::shadesOpacity() const
235 250 {
236 251 return m_shades.opacity();
237 252 }
238 253
239 254 void AxisItem::setLabelsAngle(int angle)
240 255 {
241 256 foreach(QGraphicsItem* item , m_labels.childItems()) {
242 257 QPointF center = item->boundingRect().center();
243 258 item->setRotation(angle);
244 259 }
245 260
246 261 m_labelsAngle=angle;
247 262 }
248 263
249 264 void AxisItem::setLabelsPen(const QPen& pen)
250 265 {
251 266 foreach(QGraphicsItem* item , m_labels.childItems()) {
252 267 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
253 268 }
254 269 }
255 270
256 271 void AxisItem::setLabelsBrush(const QBrush& brush)
257 272 {
258 273 foreach(QGraphicsItem* item , m_labels.childItems()) {
259 274 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
260 275 }
261 276 }
262 277
263 278 void AxisItem::setLabelsFont(const QFont& font)
264 279 {
265 280 foreach(QGraphicsItem* item , m_labels.childItems()) {
266 281 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
267 282 }
268 283 }
269 284
270 285 void AxisItem::setShadesBrush(const QBrush& brush)
271 286 {
272 287 foreach(QGraphicsItem* item , m_shades.childItems()) {
273 288 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
274 289 }
275 290 }
276 291
277 292 void AxisItem::setShadesPen(const QPen& pen)
278 293 {
279 294 foreach(QGraphicsItem* item , m_shades.childItems()) {
280 295 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
281 296 }
282 297 }
283 298
284 299 void AxisItem::setAxisPen(const QPen& pen)
285 300 {
286 m_axis.setPen(pen);
301 foreach(QGraphicsItem* item , m_axis.childItems()) {
302 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
303 }
287 304 }
288 305
289 306 void AxisItem::setGridPen(const QPen& pen)
290 307 {
291 308 foreach(QGraphicsItem* item , m_grid.childItems()) {
292 309 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
293 310 }
294 311 }
295 312
296 313
297 314 //TODO "nice numbers algorithm"
298 315 #include "moc_axisitem_p.cpp"
299 316
300 317 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,77 +1,76
1 1 #ifndef AXISITEM_H_
2 2 #define AXISITEM_H_
3 3
4 4 #include "domain_p.h"
5 5 #include "chartitem_p.h"
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11
12 12 class AxisItem : public QObject, public ChartItem
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 enum AxisType{X_AXIS,Y_AXIS};
17 17
18 18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 19 ~AxisItem();
20 20
21 21 //from QGraphicsItem
22 22 QRectF boundingRect() const;
23 23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 24
25 25 AxisType axisType() const {return m_type;};
26 26
27 27 void setAxisOpacity(qreal opacity);
28 28 qreal axisOpacity() const;
29 29
30 30 void setGridOpacity(qreal opacity);
31 31 qreal gridOpacity() const;
32 32
33 33 void setLabelsOpacity(qreal opacity);
34 34 qreal labelsOpacity() const;
35 35
36 36 void setShadesOpacity(qreal opacity);
37 37 qreal shadesOpacity() const;
38 38
39 39 void setLabelsAngle(int angle);
40 40 int labelsAngle()const { return m_labelsAngle; }
41 41
42 42 void setShadesBrush(const QBrush& brush);
43 43 void setShadesPen(const QPen& pen);
44 44
45 45 void setAxisPen(const QPen& pen);
46 46 void setGridPen(const QPen& pen);
47 47
48 48 void setLabelsPen(const QPen& pen);
49 49 void setLabelsBrush(const QBrush& brush);
50 50 void setLabelsFont(const QFont& font);
51 51
52 52 public slots:
53 53 void handleAxisUpdate(QChartAxis* axis); //look and feel
54 54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
55 55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
56 56 protected:
57 57 void updateItem(int count);
58 58 private:
59 59 void clear();
60 60 void createItems(int count);
61 61 private:
62 62 AxisType m_type;
63 63 QRectF m_rect;
64 64 int m_labelsAngle;
65 65 bool m_shadesEnabled;
66 66 QGraphicsItemGroup m_grid;
67 67 QGraphicsItemGroup m_shades;
68 68 QGraphicsItemGroup m_labels;
69 QGraphicsLineItem m_axis;
69 QGraphicsItemGroup m_axis;
70 70 QStringList m_thicksList;
71 QPointF m_origin;
72 71
73 72 };
74 73
75 74 QTCOMMERCIALCHART_END_NAMESPACE
76 75
77 76 #endif /* AXISITEM_H_ */
@@ -1,184 +1,185
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5 #include <QGraphicsScene>
6 6 #include <QGraphicsSceneResizeEvent>
7 7 #include <QDebug>
8 8
9 9 /*!
10 10 \class QChart
11 11 \brief QtCommercial chart API.
12 12
13 13 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
14 14 representation of different types of QChartSeries and other chart related objects like
15 15 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
16 16 convenience class QChartView instead of QChart.
17 17 */
18 18
19 19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20 20
21 21 /*!
22 22 Constructs a chart object which is a child of parent.
23 23 */
24 24 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
25 25 m_backgroundItem(0),
26 26 m_titleItem(0),
27 27 m_dataset(new ChartDataSet(this)),
28 28 m_presenter(new ChartPresenter(this,m_dataset))
29 29 {
30 30 }
31 31
32 32 /*!
33 33 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
34 34 */
35 35 QChart::~QChart()
36 36 {
37 37 }
38 38
39 39 /*!
40 40 Adds the series and optional y axis onto the chart and takes the ownership of the objects.
41 41 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
42 42 the y axis).
43 43 */
44 44 void QChart::addSeries(QChartSeries* series, QChartAxis* axisY)
45 45 {
46 46 m_dataset->addSeries(series, axisY);
47 47 }
48 48
49 49 void QChart::removeSeries(QChartSeries* series)
50 50 {
51 51 m_dataset->removeSeries(series);
52 52 }
53 53
54 54 void QChart::removeAllSeries()
55 55 {
56 56 m_dataset->removeAllSeries();
57 57 }
58 58
59 59 void QChart::setChartBackgroundBrush(const QBrush& brush)
60 60 {
61 61 createChartBackgroundItem();
62 62 m_backgroundItem->setBrush(brush);
63 63 m_backgroundItem->update();
64 64 }
65 65
66 66 void QChart::setChartBackgroundPen(const QPen& pen)
67 67 {
68 68 createChartBackgroundItem();
69 69 m_backgroundItem->setPen(pen);
70 70 m_backgroundItem->update();
71 71 }
72 72
73 73 void QChart::setChartTitle(const QString& title)
74 74 {
75 75 createChartTitleItem();
76 76 m_titleItem->setPlainText(title);
77 77 }
78 78
79 79 void QChart::setChartTitleFont(const QFont& font)
80 80 {
81 81 createChartTitleItem();
82 82 m_titleItem->setFont(font);
83 83 }
84 84
85 85 void QChart::createChartBackgroundItem()
86 86 {
87 87 if(!m_backgroundItem) {
88 88 m_backgroundItem = new QGraphicsRectItem(this);
89 m_backgroundItem->setPen(Qt::NoPen);
89 90 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
90 91 }
91 92 }
92 93
93 94 void QChart::createChartTitleItem()
94 95 {
95 96 if(!m_titleItem) {
96 97 m_titleItem = new QGraphicsTextItem(this);
97 98 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
98 99 }
99 100 }
100 101
101 102 int QChart::margin() const
102 103 {
103 104 return m_presenter->margin();
104 105 }
105 106
106 107 void QChart::setMargin(int margin)
107 108 {
108 109 m_presenter->setMargin(margin);
109 110 }
110 111
111 112 void QChart::setChartTheme(QChart::ChartTheme theme)
112 113 {
113 114 m_presenter->setChartTheme(theme);
114 115 }
115 116
116 117 QChart::ChartTheme QChart::chartTheme() const
117 118 {
118 119 return m_presenter->chartTheme();
119 120 }
120 121
121 122 void QChart::zoomIn()
122 123 {
123 124 if (!m_dataset->nextDomain()) {
124 125 QRectF rect = m_presenter->geometry();
125 126 rect.setWidth(rect.width()/2);
126 127 rect.setHeight(rect.height()/2);
127 128 rect.moveCenter(m_presenter->geometry().center());
128 129 zoomIn(rect);
129 130 }
130 131 }
131 132
132 133 void QChart::zoomIn(const QRectF& rect)
133 134 {
134 135 if(!rect.isValid()) return;
135 136 QRectF r = rect.normalized();
136 137 int margin = m_presenter->margin();
137 138 r.translate(-margin, -margin);
138 139 m_dataset->addDomain(r,m_presenter->geometry());
139 140 }
140 141
141 142 void QChart::zoomOut()
142 143 {
143 144 m_dataset->previousDomain();
144 145 }
145 146
146 147 void QChart::zoomReset()
147 148 {
148 149 m_dataset->clearDomains();
149 150 }
150 151
151 152 QChartAxis* QChart::axisX() const
152 153 {
153 154 return m_dataset->axisX();
154 155 }
155 156
156 157 QChartAxis* QChart::axisY() const
157 158 {
158 159 return m_dataset->axisY();
159 160 }
160 161
161 162 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
162 163 {
163 164
164 165 m_rect = QRectF(QPoint(0,0),event->newSize());
165 166 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
166 167
167 168 // recalculate title position
168 169 if (m_titleItem) {
169 170 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
170 171 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
171 172 }
172 173
173 174 //recalculate background gradient
174 175 if (m_backgroundItem) {
175 176 m_backgroundItem->setRect(rect);
176 177 }
177 178
178 179 QGraphicsWidget::resizeEvent(event);
179 180 update();
180 181 }
181 182
182 183 #include "moc_qchart.cpp"
183 184
184 185 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now