##// END OF EJS Templates
Adds scroll support for categories axis
Michal Klocek -
r1716:6069d438460e
parent child
Show More
@@ -1,36 +1,34
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 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 Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial 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 "chartcategoriesaxisx_p.h"
21 #include "chartcategoriesaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "chartanimator_p.h"
23 #include "qbarcategoriesaxis_p.h"
24 #include "qbarcategoriesaxis.h"
24 #include <cmath>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
27
25
28 static int label_padding = 5;
26 static int label_padding = 5;
29
27
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
29
32 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
30 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
33 m_categoriesAxis(axis)
31 m_categoriesAxis(axis)
34 {
32 {
35
33
36 }
34 }
@@ -46,12 +44,24 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
46 QVector<qreal> points;
44 QVector<qreal> points;
47 points.resize(m_categoriesAxis->categories().count()+1);
45 points.resize(m_categoriesAxis->categories().count()+1);
48
46
49 // TODO: shift logic
47 const qreal delta = m_rect.width()/(m_categoriesAxis->categories().count());
50 const qreal deltaX = m_rect.width()/(m_categoriesAxis->categories().count());
48 const qreal min = m_categoriesAxis->d_ptr->min();
51 for (int i = 0; i < m_categoriesAxis->categories().count()+1; ++i) {
49 const qreal max = m_categoriesAxis->d_ptr->max();
52 int x = i * deltaX + m_rect.left();
50 qreal start =-min-0.5;
51 if(start<=0) {
52 start = fmod(start * m_rect.width()/(max - min),delta) + delta;
53 }
54 else {
55 start = fmod(start * m_rect.width()/(max - min),delta);
56 }
57
58 points[m_categoriesAxis->categories().count()] = m_rect.left() + m_rect.width();
59
60 for (int i = 0; i < m_categoriesAxis->categories().count(); ++i) {
61 qreal x = start + i * delta + m_rect.left();
53 points[i] = x;
62 points[i] = x;
54 }
63 }
64
55 return points;
65 return points;
56 }
66 }
57
67
@@ -73,27 +83,40 void ChartCategoriesAxisX::updateGeometry()
73 QList<QGraphicsItem *> shades = m_shades->childItems();
83 QList<QGraphicsItem *> shades = m_shades->childItems();
74 QList<QGraphicsItem *> axis = m_axis->childItems();
84 QList<QGraphicsItem *> axis = m_axis->childItems();
75
85
76 Q_ASSERT(labels.size()-1 == ticksList.size());
86 Q_ASSERT(labels.size() == ticksList.size());
77 Q_ASSERT(layout.size()-1 == ticksList.size());
87 Q_ASSERT(layout.size() == ticksList.size());
88
89 const qreal delta = m_rect.width()/(m_categoriesAxis->categories().count());
78
90
79 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
91 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
80 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
92 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
81
93
94 qreal width = m_rect.left();
82 for (int i = 0; i < layout.size(); ++i) {
95 for (int i = 0; i < layout.size(); ++i) {
83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
96 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
97 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
98 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 if (i>=1) {
99 labelItem->setText(ticksList.at(i));
87 labelItem->setText(ticksList.at(i-1));
88 const QRectF& rect = labelItem->boundingRect();
100 const QRectF& rect = labelItem->boundingRect();
89 QPointF center = rect.center();
101 QPointF center = rect.center();
90 labelItem->setTransformOriginPoint(center.x(), center.y());
102 labelItem->setTransformOriginPoint(center.x(), center.y());
91 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
103
92 m_minWidth+=rect.width();
104 if(i==layout.size()-1){
93 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
105 labelItem->setPos(layout[i-1] + (delta)/2 - center.x(), m_rect.bottom() + label_padding);
94 }else{
106 }else{
107 labelItem->setPos(layout[i] - (delta)/2 - center.x(), m_rect.bottom() + label_padding);
108 }
109
110 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) {
95 labelItem->setVisible(false);
111 labelItem->setVisible(false);
96 }
112 }
113 else {
114 labelItem->setVisible(true);
115 width=rect.width()+labelItem->pos().x();
116 }
117
118 m_minWidth+=rect.width();
119 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
97
120
98 if ((i+1)%2 && i>1) {
121 if ((i+1)%2 && i>1) {
99 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
122 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
@@ -1,37 +1,35
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 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 Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial 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 "chartcategoriesaxisy_p.h"
21 #include "chartcategoriesaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "chartanimator_p.h"
23 #include "qbarcategoriesaxis_p.h"
24 #include "qbarcategoriesaxis.h"
24 #include <QDebug>
25 #include <QGraphicsLayout>
25 #include <cmath>
26 #include <QFontMetrics>
27 #include <QBarCategoriesAxis>
28
26
29 static int label_padding = 5;
27 static int label_padding = 5;
30
28
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
30
33 ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 m_categoriesAxis(axis)
32 m_categoriesAxis(axis)
35 {
33 {
36 }
34 }
37
35
@@ -46,9 +44,24 QVector<qreal> ChartCategoriesAxisY::calculateLayout() const
46 QVector<qreal> points;
44 QVector<qreal> points;
47 points.resize(m_categoriesAxis->categories().count()+1);
45 points.resize(m_categoriesAxis->categories().count()+1);
48
46
49 const qreal deltaY = m_rect.height()/(m_categoriesAxis->categories().count());
47 qreal delta = m_rect.height()/(m_categoriesAxis->categories().count());
50 for (int i = 0; i < m_categoriesAxis->categories().count()+1; ++i) {
48
51 int y = i * -deltaY + m_rect.bottom();
49 const qreal min = m_categoriesAxis->d_ptr->min();
50 const qreal max = m_categoriesAxis->d_ptr->max();
51
52 qreal start =-min-0.5;
53
54 if(start<=0) {
55 start = fmod(start * m_rect.height()/(max - min),delta) + delta;
56 }
57 else {
58 start = fmod(start * m_rect.height()/(max - min),delta);
59 }
60
61 points[m_categoriesAxis->categories().count()] = m_rect.top();
62
63 for (int i = 0; i < m_categoriesAxis->categories().count(); ++i) {
64 int y = m_rect.bottom() - i * delta - start;
52 points[i] = y;
65 points[i] = y;
53 }
66 }
54
67
@@ -73,28 +86,41 void ChartCategoriesAxisY::updateGeometry()
73 QList<QGraphicsItem *> shades = m_shades->childItems();
86 QList<QGraphicsItem *> shades = m_shades->childItems();
74 QList<QGraphicsItem *> axis = m_axis->childItems();
87 QList<QGraphicsItem *> axis = m_axis->childItems();
75
88
76 Q_ASSERT(labels.size()-1 == ticksList.size());
89 Q_ASSERT(labels.size() == ticksList.size());
77 Q_ASSERT(layout.size()-1 == ticksList.size());
90 Q_ASSERT(layout.size() == ticksList.size());
91
92 const qreal delta = m_rect.height()/(m_categoriesAxis->categories().count());
78
93
79 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
94 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
80 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
95 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
81
96
97 qreal height = m_rect.bottom();
82 for (int i = 0; i < layout.size(); ++i) {
98 for (int i = 0; i < layout.size(); ++i) {
83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
99 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
100 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
101 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 if(i>=1){
102 labelItem->setText(ticksList.at(i));
87 labelItem->setText(ticksList.at(i-1));
88 const QRectF& rect = labelItem->boundingRect();
103 const QRectF& rect = labelItem->boundingRect();
89 QPointF center = rect.center();
104 QPointF center = rect.center();
90 labelItem->setTransformOriginPoint(center.x(), center.y());
105 labelItem->setTransformOriginPoint(center.x(), center.y());
91 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
106
92 m_minWidth+=rect.width();
107 if(i==layout.size()-1) {
93 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
108 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i-1] - (delta)/2 - center.y());
94 }else{
109 }
95 labelItem->setVisible(false);
110 else {
111 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] + (delta)/2 - center.y());
96 }
112 }
97
113
114 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) {
115 labelItem->setVisible(false);
116 }
117 else {
118 labelItem->setVisible(true);
119 height=labelItem->pos().y();
120 }
121
122 m_minWidth+=rect.width();
123 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
98
124
99 if ((i+1)%2 && i>1) {
125 if ((i+1)%2 && i>1) {
100 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
126 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
@@ -105,7 +131,6 void ChartCategoriesAxisY::updateGeometry()
105 }
131 }
106 }
132 }
107
133
108
109 void ChartCategoriesAxisY::handleAxisUpdated()
134 void ChartCategoriesAxisY::handleAxisUpdated()
110 {
135 {
111
136
@@ -311,7 +311,9 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
311 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
311 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
312
312
313 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
313 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
314 QAbstractAxisPrivate(q)
314 QAbstractAxisPrivate(q),
315 m_min(0.0),
316 m_max(0.0)
315 {
317 {
316
318
317 }
319 }
@@ -342,17 +344,27 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &ma
342 qreal QBarCategoriesAxisPrivate::min()
344 qreal QBarCategoriesAxisPrivate::min()
343 {
345 {
344 //TODO:: cache it
346 //TODO:: cache it
345 return m_categories.indexOf(m_minCategory) - 0.5;
347 return m_min;//m_categories.indexOf(m_minCategory) - 0.5;
346 }
348 }
347
349
348 qreal QBarCategoriesAxisPrivate::max()
350 qreal QBarCategoriesAxisPrivate::max()
349 {
351 {
350 //TODO:: cache it
352 //TODO:: cache it
351 return m_categories.indexOf(m_maxCategory) + 0.5;
353 return m_max;//m_categories.indexOf(m_maxCategory) + 0.5;
352 }
354 }
353
355
354 void QBarCategoriesAxisPrivate::handleDomainUpdated()
356 void QBarCategoriesAxisPrivate::handleDomainUpdated()
355 {
357 {
358 Domain* domain = qobject_cast<Domain*>(sender());
359
360 if(m_orientation==Qt::Horizontal){
361 m_min = domain->minX();
362 m_max = domain->maxX();
363 }else if(m_orientation==Qt::Vertical){
364 m_min = domain->minY();
365 m_max = domain->maxY();
366 }
367
356 // Q_Q(QBarCategoriesAxis);
368 // Q_Q(QBarCategoriesAxis);
357
369
358 // TODO: causes crash in some situations. added to known issues
370 // TODO: causes crash in some situations. added to known issues
@@ -62,6 +62,8 private:
62 QStringList m_categories;
62 QStringList m_categories;
63 QString m_minCategory;
63 QString m_minCategory;
64 QString m_maxCategory;
64 QString m_maxCategory;
65 qreal m_min;
66 qreal m_max;
65
67
66 private:
68 private:
67 Q_DECLARE_PUBLIC(QBarCategoriesAxis)
69 Q_DECLARE_PUBLIC(QBarCategoriesAxis)
@@ -374,11 +374,11 void ChartAxis::createCategoryLabels(QStringList &labels,qreal min, qreal max,co
374 Q_ASSERT(max>min);
374 Q_ASSERT(max>min);
375 Q_UNUSED(max);
375 Q_UNUSED(max);
376
376
377 int x = qCeil(min);
377 int x = qFloor(min+0.5);
378 int count = 0;
378 int count = 0;
379
379
380 // Try to find category for x coordinate
380 // Try to find category for x coordinate
381 while (count < categories.count()) {
381 while (count < categories.count()+1) {
382 if ((x < categories.count()) && (x >= 0)) {
382 if ((x < categories.count()) && (x >= 0)) {
383 labels << categories.at(x);
383 labels << categories.at(x);
384 } else {
384 } else {
General Comments 0
You need to be logged in to leave comments. Login now