##// END OF EJS Templates
astyle and manual coding style fixes for src-folder
Jani Honkonen -
r2097:db3c1ea5786f
parent child
Show More
@@ -28,8 +28,8 Q_DECLARE_METATYPE(QVector<QRectF>)
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 AbstractBarAnimation::AbstractBarAnimation(AbstractBarChartItem *item)
30 AbstractBarAnimation::AbstractBarAnimation(AbstractBarChartItem *item)
31 :ChartAnimation(item),
31 : ChartAnimation(item),
32 m_item(item)
32 m_item(item)
33 {
33 {
34 setDuration(ChartAnimationDuration);
34 setDuration(ChartAnimationDuration);
35 setEasingCurve(QEasingCurve::OutQuart);
35 setEasingCurve(QEasingCurve::OutQuart);
@@ -29,9 +29,10 Q_DECLARE_METATYPE(QVector<qreal>)
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31
31
32 AxisAnimation::AxisAnimation(ChartAxis *axis): ChartAnimation(axis),
32 AxisAnimation::AxisAnimation(ChartAxis *axis)
33 m_axis(axis),
33 : ChartAnimation(axis),
34 m_type(DefaultAnimation)
34 m_axis(axis),
35 m_type(DefaultAnimation)
35 {
36 {
36 setDuration(ChartAnimationDuration);
37 setDuration(ChartAnimationDuration);
37 setEasingCurve(QEasingCurve::OutQuart);
38 setEasingCurve(QEasingCurve::OutQuart);
@@ -43,72 +44,76 AxisAnimation::~AxisAnimation()
43
44
44 void AxisAnimation::setAnimationType(Animation type)
45 void AxisAnimation::setAnimationType(Animation type)
45 {
46 {
46 if (state() != QAbstractAnimation::Stopped) stop();
47 if (state() != QAbstractAnimation::Stopped)
47 m_type=type;
48 stop();
49 m_type = type;
48 }
50 }
49
51
50 void AxisAnimation::setAnimationPoint(const QPointF& point)
52 void AxisAnimation::setAnimationPoint(const QPointF& point)
51 {
53 {
52 if (state() != QAbstractAnimation::Stopped) stop();
54 if (state() != QAbstractAnimation::Stopped)
53 m_point=point;
55 stop();
56 m_point = point;
54 }
57 }
55
58
56 void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout)
59 void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout)
57 {
60 {
58 if (state() != QAbstractAnimation::Stopped) stop();
61 if (state() != QAbstractAnimation::Stopped)
59
62 stop();
60 if (newLayout.count() == 0)
63
61 return;
64 if (newLayout.count() == 0)
62
65 return;
63 switch (m_type) {
66
64 case ZoomOutAnimation: {
67 switch (m_type) {
65 QRectF rect = m_axis->presenter()->chartsGeometry();
68 case ZoomOutAnimation: {
66 oldLayout.resize(newLayout.count());
69 QRectF rect = m_axis->presenter()->chartsGeometry();
67
70 oldLayout.resize(newLayout.count());
68 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
71
69 oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
72 for (int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
70 oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
73 oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
71 }
74 oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
72 }
75 }
73 break;
76 }
74 case ZoomInAnimation: {
77 break;
75 int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0);
78 case ZoomInAnimation: {
76 oldLayout.resize(newLayout.count());
79 int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())),
77
80 newLayout.count() - (qreal)1.0);
78 for(int i = 0; i < oldLayout.count(); i++)
81 oldLayout.resize(newLayout.count());
79 oldLayout[i]= oldLayout[index];
82
80 }
83 for (int i = 0; i < oldLayout.count(); i++)
81 break;
84 oldLayout[i] = oldLayout[index];
82 case MoveForwardAnimation: {
85 }
83 oldLayout.resize(newLayout.count());
86 break;
84
87 case MoveForwardAnimation: {
85 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
88 oldLayout.resize(newLayout.count());
86 oldLayout[i]= oldLayout[j];
89
87 }
90 for (int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
88 break;
91 oldLayout[i] = oldLayout[j];
89 case MoveBackwordAnimation: {
92 }
90 oldLayout.resize(newLayout.count());
93 break;
91
94 case MoveBackwordAnimation: {
92 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
95 oldLayout.resize(newLayout.count());
93 oldLayout[i]= oldLayout[j];
96
94 }
97 for (int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
95 break;
98 oldLayout[i] = oldLayout[j];
96 default: {
99 }
97 oldLayout.resize(newLayout.count());
100 break;
98 QRectF rect = m_axis->presenter()->chartsGeometry();
101 default: {
99 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
102 oldLayout.resize(newLayout.count());
100 oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
103 QRectF rect = m_axis->presenter()->chartsGeometry();
101 }
104 for (int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
102 break;
105 oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
103 }
106 }
104
107 break;
105 QVariantAnimation::KeyValues value;
108 }
106 setKeyValues(value); //workaround for wrong interpolation call
109
107 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
110 QVariantAnimation::KeyValues value;
108 setKeyValueAt(1.0, qVariantFromValue(newLayout));
111 setKeyValues(value); //workaround for wrong interpolation call
112 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
113 setKeyValueAt(1.0, qVariantFromValue(newLayout));
109 }
114 }
110
115
111 QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
116 QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
112 {
117 {
113 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
118 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
114 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
119 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
@@ -116,18 +121,17 QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end,
116
121
117 Q_ASSERT(startVector.count() == endVecotr.count()) ;
122 Q_ASSERT(startVector.count() == endVecotr.count()) ;
118
123
119 for(int i = 0; i < startVector.count(); i++){
124 for (int i = 0; i < startVector.count(); i++) {
120 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
125 qreal value = startVector[i] + ((endVecotr[i] - startVector[i]) * progress); //qBound(0.0, progress, 1.0));
121 result << value;
126 result << value;
122 }
127 }
123 return qVariantFromValue(result);
128 return qVariantFromValue(result);
124 }
129 }
125
130
126
131
127 void AxisAnimation::updateCurrentValue (const QVariant &value )
132 void AxisAnimation::updateCurrentValue(const QVariant &value)
128 {
133 {
129 if (state() != QAbstractAnimation::Stopped)//workaround
134 if (state() != QAbstractAnimation::Stopped) { //workaround
130 {
131 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
135 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
132 Q_ASSERT(vector.count() != 0);
136 Q_ASSERT(vector.count() != 0);
133 m_axis->setLayout(vector);
137 m_axis->setLayout(vector);
@@ -47,8 +47,8 public:
47 void setAnimationPoint(const QPointF& point);
47 void setAnimationPoint(const QPointF& point);
48 void setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout);
48 void setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout);
49 protected:
49 protected:
50 QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress ) const;
50 QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const;
51 void updateCurrentValue(const QVariant &value );
51 void updateCurrentValue(const QVariant &value);
52 private:
52 private:
53 ChartAxis *m_axis;
53 ChartAxis *m_axis;
54 Animation m_type;
54 Animation m_type;
@@ -27,7 +27,7 Q_DECLARE_METATYPE(QVector<QRectF>)
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 BarAnimation::BarAnimation(AbstractBarChartItem *item)
29 BarAnimation::BarAnimation(AbstractBarChartItem *item)
30 :AbstractBarAnimation(item)
30 : AbstractBarAnimation(item)
31 {
31 {
32
32
33 }
33 }
@@ -44,7 +44,7 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qr
44
44
45 Q_ASSERT(startVector.count() == endVector.count());
45 Q_ASSERT(startVector.count() == endVector.count());
46
46
47 for(int i = 0; i < startVector.count(); i++) {
47 for (int i = 0; i < startVector.count(); i++) {
48 QRectF start = startVector[i].normalized();
48 QRectF start = startVector[i].normalized();
49 QRectF end = endVector[i].normalized();
49 QRectF end = endVector[i].normalized();
50
50
@@ -62,7 +62,7 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qr
62 y = endVector[i].top() + endVector[i].height() - h;
62 y = endVector[i].top() + endVector[i].height() - h;
63 }
63 }
64
64
65 QRectF value(x,y,w,h);
65 QRectF value(x, y, w, h);
66 result << value.normalized();
66 result << value.normalized();
67 }
67 }
68 return qVariantFromValue(result);
68 return qVariantFromValue(result);
@@ -41,7 +41,7 class ChartAnimation: public QVariantAnimation
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43 public:
43 public:
44 ChartAnimation(QObject *parent = 0):QVariantAnimation(parent){};
44 ChartAnimation(QObject *parent = 0): QVariantAnimation(parent) {};
45 };
45 };
46
46
47 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
@@ -26,8 +26,8 Q_DECLARE_METATYPE(QVector<QRectF>)
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 HorizontalBarAnimation::HorizontalBarAnimation(AbstractBarChartItem *item) :
29 HorizontalBarAnimation::HorizontalBarAnimation(AbstractBarChartItem *item)
30 AbstractBarAnimation(item)
30 : AbstractBarAnimation(item)
31 {
31 {
32 }
32 }
33
33
@@ -45,7 +45,7 QVariant HorizontalBarAnimation::interpolated(const QVariant &from, const QVaria
45
45
46 Q_ASSERT(startVector.count() == endVector.count());
46 Q_ASSERT(startVector.count() == endVector.count());
47
47
48 for(int i = 0; i < startVector.count(); i++) {
48 for (int i = 0; i < startVector.count(); i++) {
49 QRectF start = startVector[i].normalized();
49 QRectF start = startVector[i].normalized();
50 QRectF end = endVector[i].normalized();
50 QRectF end = endVector[i].normalized();
51
51
@@ -63,7 +63,7 QVariant HorizontalBarAnimation::interpolated(const QVariant &from, const QVaria
63 x = end.left();
63 x = end.left();
64 }
64 }
65
65
66 QRectF value(x,y,w,h);
66 QRectF value(x, y, w, h);
67 result << value.normalized();
67 result << value.normalized();
68 }
68 }
69 return qVariantFromValue(result);
69 return qVariantFromValue(result);
@@ -47,13 +47,13 QVariant HorizontalPercentBarAnimation::interpolated(const QVariant &from, const
47
47
48 qreal xAxis = m_item->geometry().x();
48 qreal xAxis = m_item->geometry().x();
49
49
50 for(int i = 0; i < startVector.count(); i++) {
50 for (int i = 0; i < startVector.count(); i++) {
51 qreal h = endVector[i].height();
51 qreal h = endVector[i].height();
52 qreal w = endVector[i].width() * progress;
52 qreal w = endVector[i].width() * progress;
53 qreal x = xAxis + ((endVector[i].left() - xAxis) * progress);
53 qreal x = xAxis + ((endVector[i].left() - xAxis) * progress);
54 qreal y = endVector[i].top();
54 qreal y = endVector[i].top();
55
55
56 QRectF value(x,y,w,h);
56 QRectF value(x, y, w, h);
57 result << value.normalized();
57 result << value.normalized();
58 }
58 }
59 return qVariantFromValue(result);
59 return qVariantFromValue(result);
@@ -47,13 +47,13 QVariant HorizontalStackedBarAnimation::interpolated(const QVariant &from, const
47
47
48 qreal xAxis = m_item->geometry().x();
48 qreal xAxis = m_item->geometry().x();
49
49
50 for(int i = 0; i < startVector.count(); i++) {
50 for (int i = 0; i < startVector.count(); i++) {
51 qreal h = endVector[i].height();
51 qreal h = endVector[i].height();
52 qreal w = endVector[i].width() * progress;
52 qreal w = endVector[i].width() * progress;
53 qreal x = xAxis + ((endVector[i].left() - xAxis) * progress);
53 qreal x = xAxis + ((endVector[i].left() - xAxis) * progress);
54 qreal y = endVector[i].top();
54 qreal y = endVector[i].top();
55
55
56 QRectF value(x,y,w,h);
56 QRectF value(x, y, w, h);
57 result << value.normalized();
57 result << value.normalized();
58 }
58 }
59 return qVariantFromValue(result);
59 return qVariantFromValue(result);
@@ -27,7 +27,7 Q_DECLARE_METATYPE(QVector<QRectF>)
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarAnimation::PercentBarAnimation(PercentBarChartItem *item)
29 PercentBarAnimation::PercentBarAnimation(PercentBarChartItem *item)
30 :AbstractBarAnimation(item)
30 : AbstractBarAnimation(item)
31 {
31 {
32 setDuration(ChartAnimationDuration);
32 setDuration(ChartAnimationDuration);
33 setEasingCurve(QEasingCurve::OutQuart);
33 setEasingCurve(QEasingCurve::OutQuart);
@@ -47,13 +47,13 QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant
47
47
48 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
48 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
49
49
50 for(int i = 0; i < startVector.count(); i++) {
50 for (int i = 0; i < startVector.count(); i++) {
51 qreal w = endVector[i].width();
51 qreal w = endVector[i].width();
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 qreal x = endVector[i].topLeft().x();
53 qreal x = endVector[i].topLeft().x();
54 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
54 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
55
55
56 QRectF value(x,y,w,h);
56 QRectF value(x, y, w, h);
57 result << value.normalized();
57 result << value.normalized();
58 }
58 }
59 return qVariantFromValue(result);
59 return qVariantFromValue(result);
@@ -26,8 +26,8
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 PieAnimation::PieAnimation(PieChartItem *item)
28 PieAnimation::PieAnimation(PieChartItem *item)
29 :ChartAnimation(item),
29 : ChartAnimation(item),
30 m_item(item)
30 m_item(item)
31 {
31 {
32 }
32 }
33
33
@@ -58,8 +58,8 QBrush linearPos(QBrush start, QBrush end, qreal pos)
58 }
58 }
59
59
60 PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem)
60 PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem)
61 :ChartAnimation(sliceItem),
61 : ChartAnimation(sliceItem),
62 m_sliceItem(sliceItem)
62 m_sliceItem(sliceItem)
63 {
63 {
64 }
64 }
65
65
@@ -27,9 +27,9 Q_DECLARE_METATYPE(SplineVector)
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 SplineAnimation::SplineAnimation(SplineChartItem* item):XYAnimation(item),
30 SplineAnimation::SplineAnimation(SplineChartItem* item): XYAnimation(item),
31 m_item(item),
31 m_item(item),
32 m_valid(false)
32 m_valid(false)
33 {
33 {
34 }
34 }
35
35
@@ -40,8 +40,8 SplineAnimation::~SplineAnimation()
40 void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
40 void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
41 {
41 {
42 if (newPoints.count() * 2 - 2 != newControlPoints.count() || newControlPoints.count() < 2) {
42 if (newPoints.count() * 2 - 2 != newControlPoints.count() || newControlPoints.count() < 2) {
43 m_valid=false;
43 m_valid = false;
44 m_dirty=false;
44 m_dirty = false;
45 m_item->setGeometryPoints(newPoints);
45 m_item->setGeometryPoints(newPoints);
46 m_item->setControlGeometryPoints(newControlPoints);
46 m_item->setControlGeometryPoints(newControlPoints);
47 m_item->setDirty(false);
47 m_item->setDirty(false);
@@ -53,61 +53,58 void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPo
53
53
54 if (state() != QAbstractAnimation::Stopped) {
54 if (state() != QAbstractAnimation::Stopped) {
55 stop();
55 stop();
56 m_dirty=false;
56 m_dirty = false;
57 }
57 }
58
58
59 if(!m_dirty) {
59 if (!m_dirty) {
60 m_dirty = true;
60 m_dirty = true;
61 m_oldSpline.first = oldPoints;
61 m_oldSpline.first = oldPoints;
62 m_oldSpline.second = oldControlPoints;
62 m_oldSpline.second = oldControlPoints;
63 }
63 }
64
64
65 m_newSpline.first=newPoints;
65 m_newSpline.first = newPoints;
66 m_newSpline.second=newControlPoints;
66 m_newSpline.second = newControlPoints;
67
67
68
68
69 int x = m_oldSpline.first.count();
69 int x = m_oldSpline.first.count();
70 int y = m_newSpline.first.count();
70 int y = m_newSpline.first.count();
71
71
72 if(x - y == 1 && index >= 0 && y>0) {
72 if (x - y == 1 && index >= 0 && y > 0) {
73 //remove point
73 //remove point
74 if(index>0){
74 if (index > 0) {
75 m_newSpline.first.insert(index, newPoints[index-1]);
75 m_newSpline.first.insert(index, newPoints[index - 1]);
76 m_newSpline.second.insert((index -1) * 2, newPoints[index-1] );
76 m_newSpline.second.insert((index - 1) * 2, newPoints[index - 1]);
77 m_newSpline.second.insert((index -1) * 2 + 1, newPoints[index-1]);
77 m_newSpline.second.insert((index - 1) * 2 + 1, newPoints[index - 1]);
78 }else{
78 } else {
79 m_newSpline.first.insert(index, newPoints[index]);
79 m_newSpline.first.insert(index, newPoints[index]);
80 m_newSpline.second.insert(index * 2, newPoints[index] );
80 m_newSpline.second.insert(index * 2, newPoints[index]);
81 m_newSpline.second.insert(index * 2 + 1, newPoints[index]);
81 m_newSpline.second.insert(index * 2 + 1, newPoints[index]);
82 }
82 }
83 m_index=index;
83 m_index = index;
84 m_type = RemovePointAnimation;
84 m_type = RemovePointAnimation;
85 }
85 }
86
86
87 if(x - y == -1 && index >= 0) {
87 if (x - y == -1 && index >= 0) {
88 //add point
88 //add point
89 if(index>0){
89 if (index > 0) {
90 m_oldSpline.first.insert(index, newPoints[index-1]);
90 m_oldSpline.first.insert(index, newPoints[index - 1]);
91 m_oldSpline.second.insert((index - 1) * 2, newPoints[index-1]);
91 m_oldSpline.second.insert((index - 1) * 2, newPoints[index - 1]);
92 m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index-1]);
92 m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index - 1]);
93 }else{
93 } else {
94 m_oldSpline.first.insert(index, newPoints[index]);
94 m_oldSpline.first.insert(index, newPoints[index]);
95 m_oldSpline.second.insert((index - 1) * 2, newPoints[index]);
95 m_oldSpline.second.insert((index - 1) * 2, newPoints[index]);
96 m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index]);
96 m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index]);
97 }
97 }
98 m_index=index;
98 m_index = index;
99 m_type = AddPointAnimation;
99 m_type = AddPointAnimation;
100 }
100 }
101
101
102 x = m_oldSpline.first.count();
102 x = m_oldSpline.first.count();
103 y = m_newSpline.first.count();
103 y = m_newSpline.first.count();
104
104
105 if(x != y)
105 if (x != y) {
106 {
107 m_type = NewAnimation;
106 m_type = NewAnimation;
108 }
107 } else if (m_type == NewAnimation) {
109 else if(m_type == NewAnimation)
110 {
111 m_type = ReplacePointAnimation;
108 m_type = ReplacePointAnimation;
112 }
109 }
113
110
@@ -115,11 +112,11 void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPo
115 setKeyValueAt(0.0, qVariantFromValue(m_oldSpline));
112 setKeyValueAt(0.0, qVariantFromValue(m_oldSpline));
116 setKeyValueAt(1.0, qVariantFromValue(m_newSpline));
113 setKeyValueAt(1.0, qVariantFromValue(m_newSpline));
117
114
118 m_valid=true;
115 m_valid = true;
119
116
120 }
117 }
121
118
122 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
119 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
123 {
120 {
124
121
125 SplineVector startPair = qVariantValue< SplineVector >(start);
122 SplineVector startPair = qVariantValue< SplineVector >(start);
@@ -127,44 +124,41 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en
127 SplineVector result;
124 SplineVector result;
128
125
129 switch (animationType()) {
126 switch (animationType()) {
130
127 case RemovePointAnimation:
131 case RemovePointAnimation:
128 case AddPointAnimation:
132 case AddPointAnimation:
129 case ReplacePointAnimation: {
133 case ReplacePointAnimation:
134 {
135 if (startPair.first.count() != endPair.first.count())
130 if (startPair.first.count() != endPair.first.count())
136 break;
131 break;
137 Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count());
132 Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count());
138 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
133 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
139 for(int i = 0; i < endPair.first.count(); i++) {
134 for (int i = 0; i < endPair.first.count(); i++) {
140 qreal x = startPair.first[i].x() + ((endPair.first[i].x() - startPair.first[i].x()) * progress);
135 qreal x = startPair.first[i].x() + ((endPair.first[i].x() - startPair.first[i].x()) * progress);
141 qreal y = startPair.first[i].y() + ((endPair.first[i].y() - startPair.first[i].y()) * progress);
136 qreal y = startPair.first[i].y() + ((endPair.first[i].y() - startPair.first[i].y()) * progress);
142 result.first << QPointF(x,y);
137 result.first << QPointF(x, y);
143 if (i + 1 >= endPair.first.count())
138 if (i + 1 >= endPair.first.count())
144 continue;
139 continue;
145 x = startPair.second[i * 2].x() + ((endPair.second[i * 2].x() - startPair.second[i * 2].x()) * progress);
140 x = startPair.second[i * 2].x() + ((endPair.second[i * 2].x() - startPair.second[i * 2].x()) * progress);
146 y = startPair.second[i * 2].y() + ((endPair.second[i * 2].y() - startPair.second[i * 2].y()) * progress);
141 y = startPair.second[i * 2].y() + ((endPair.second[i * 2].y() - startPair.second[i * 2].y()) * progress);
147 result.second << QPointF(x,y);
142 result.second << QPointF(x, y);
148 x = startPair.second[i * 2 + 1].x() + ((endPair.second[i * 2 + 1].x() - startPair.second[i * 2 + 1].x()) * progress);
143 x = startPair.second[i * 2 + 1].x() + ((endPair.second[i * 2 + 1].x() - startPair.second[i * 2 + 1].x()) * progress);
149 y = startPair.second[i * 2 + 1].y() + ((endPair.second[i * 2 + 1].y() - startPair.second[i * 2 + 1].y()) * progress);
144 y = startPair.second[i * 2 + 1].y() + ((endPair.second[i * 2 + 1].y() - startPair.second[i * 2 + 1].y()) * progress);
150 result.second << QPointF(x,y);
145 result.second << QPointF(x, y);
151 }
146 }
152
153 }
147 }
154 break;
148 break;
155 case NewAnimation: {
149 case NewAnimation: {
156 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
150 Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count());
157 int count = endPair.first.count()* qBound(qreal(0), progress, qreal(1));
151 int count = endPair.first.count() * qBound(qreal(0), progress, qreal(1));
158 for(int i = 0; i < count; i++) {
152 for (int i = 0; i < count; i++) {
159 result.first << endPair.first[i];
153 result.first << endPair.first[i];
160 if(i + 1 == count)
154 if (i + 1 == count)
161 break;
155 break;
162 result.second << endPair.second[2 * i];
156 result.second << endPair.second[2 * i];
163 result.second << endPair.second[2 * i + 1];
157 result.second << endPair.second[2 * i + 1];
164 }
158 }
165 }
159 }
166 break;
160 break;
167 default:
161 default:
168 qWarning() << "Unknown type of animation";
162 qWarning() << "Unknown type of animation";
169 break;
163 break;
170 }
164 }
@@ -172,7 +166,7 QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en
172 return qVariantFromValue(result);
166 return qVariantFromValue(result);
173 }
167 }
174
168
175 void SplineAnimation::updateCurrentValue (const QVariant &value )
169 void SplineAnimation::updateCurrentValue(const QVariant &value)
176 {
170 {
177 if (state() != QAbstractAnimation::Stopped && m_valid) { //workaround
171 if (state() != QAbstractAnimation::Stopped && m_valid) { //workaround
178 QPair<QVector<QPointF >, QVector<QPointF > > pair = qVariantValue< QPair< QVector<QPointF>, QVector<QPointF> > >(value);
172 QPair<QVector<QPointF >, QVector<QPointF > > pair = qVariantValue< QPair< QVector<QPointF>, QVector<QPointF> > >(value);
@@ -188,24 +182,21 void SplineAnimation::updateState(QAbstractAnimation::State newState, QAbstractA
188 {
182 {
189 XYAnimation::updateState(newState, oldState);
183 XYAnimation::updateState(newState, oldState);
190
184
191 if(oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped)
185 if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) {
192 {
186 if (m_item->isDirty() && m_type == RemovePointAnimation) {
193 if(m_item->isDirty() && m_type==RemovePointAnimation) {
187 if (!m_newSpline.first.isEmpty()) {
194 if(!m_newSpline.first.isEmpty()) {
195 m_newSpline.first.remove(m_index);
188 m_newSpline.first.remove(m_index);
196 m_newSpline.second.remove((m_index-1) * 2);
189 m_newSpline.second.remove((m_index - 1) * 2);
197 m_newSpline.second.remove((m_index-1) * 2);
190 m_newSpline.second.remove((m_index - 1) * 2);
198 }
191 }
199 m_item->setGeometryPoints(m_newSpline.first);
192 m_item->setGeometryPoints(m_newSpline.first);
200 m_item->setControlGeometryPoints(m_newSpline.second);
193 m_item->setControlGeometryPoints(m_newSpline.second);
201 }
194 }
202 }
195 }
203
196
204 if(oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running)
197 if (oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running) {
205 {
198 if (!m_valid)
206 if(!m_valid) {
199 stop();
207 stop();
208 }
209 }
200 }
210 }
201 }
211
202
@@ -27,7 +27,7 Q_DECLARE_METATYPE(QVector<QRectF>)
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 StackedBarAnimation::StackedBarAnimation(StackedBarChartItem *item)
29 StackedBarAnimation::StackedBarAnimation(StackedBarChartItem *item)
30 :AbstractBarAnimation(item)
30 : AbstractBarAnimation(item)
31 {
31 {
32 setEasingCurve(QEasingCurve::OutQuart);
32 setEasingCurve(QEasingCurve::OutQuart);
33 }
33 }
@@ -46,13 +46,13 QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant
46
46
47 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
47 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
48
48
49 for(int i = 0; i < startVector.count(); i++) {
49 for (int i = 0; i < startVector.count(); i++) {
50 qreal w = endVector[i].width();
50 qreal w = endVector[i].width();
51 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
51 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
52 qreal x = endVector[i].topLeft().x();
52 qreal x = endVector[i].topLeft().x();
53 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
53 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
54
54
55 QRectF value(x,y,w,h);
55 QRectF value(x, y, w, h);
56 result << value.normalized();
56 result << value.normalized();
57 }
57 }
58 return qVariantFromValue(result);
58 return qVariantFromValue(result);
@@ -26,11 +26,12 Q_DECLARE_METATYPE(QVector<QPointF>)
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item),
29 XYAnimation::XYAnimation(XYChart *item)
30 m_type(NewAnimation),
30 : ChartAnimation(item),
31 m_dirty(false),
31 m_type(NewAnimation),
32 m_index(-1),
32 m_dirty(false),
33 m_item(item)
33 m_index(-1),
34 m_item(item)
34 {
35 {
35 setDuration(ChartAnimationDuration);
36 setDuration(ChartAnimationDuration);
36 setEasingCurve(QEasingCurve::OutQuart);
37 setEasingCurve(QEasingCurve::OutQuart);
@@ -42,54 +43,50 XYAnimation::~XYAnimation()
42
43
43 void XYAnimation::setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index)
44 void XYAnimation::setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index)
44 {
45 {
45 m_type = NewAnimation;
46 m_type = NewAnimation;
46
47
47 if (state() != QAbstractAnimation::Stopped){
48 if (state() != QAbstractAnimation::Stopped) {
48 stop();
49 stop();
49 m_dirty=false;
50 m_dirty = false;
50 }
51 }
51
52
52 if(!m_dirty){
53 if (!m_dirty) {
53 m_dirty = true;
54 m_dirty = true;
54 m_oldPoints = oldPoints;
55 m_oldPoints = oldPoints;
55 }
56 }
56
57
57 m_newPoints = newPoints;
58 m_newPoints = newPoints;
58
59
59 int x = m_oldPoints.count();
60 int x = m_oldPoints.count();
60 int y = m_newPoints.count();
61 int y = m_newPoints.count();
61
62
62 if(x - y == 1 && index >= 0 && y > 0){
63 if (x - y == 1 && index >= 0 && y > 0) {
63 //remove point
64 //remove point
64 m_newPoints.insert(index, index > 0 ? newPoints[index-1] : newPoints[index]);
65 m_newPoints.insert(index, index > 0 ? newPoints[index - 1] : newPoints[index]);
65 m_index=index;
66 m_index = index;
66 m_type = RemovePointAnimation;
67 m_type = RemovePointAnimation;
67 }
68 }
68
69
69 if(x - y == -1 && index >= 0){
70 if (x - y == -1 && index >= 0) {
70 //add point
71 //add point
71 m_oldPoints.insert(index, index > 0 ? newPoints[index-1] : newPoints[index]);
72 m_oldPoints.insert(index, index > 0 ? newPoints[index - 1] : newPoints[index]);
72 m_index=index;
73 m_index = index;
73 m_type = AddPointAnimation;
74 m_type = AddPointAnimation;
74 }
75 }
75
76
76 x = m_oldPoints.count();
77 x = m_oldPoints.count();
77 y = m_newPoints.count();
78 y = m_newPoints.count();
78
79
79 if(x != y)
80 if (x != y)
80 {
81 m_type = NewAnimation;
81 m_type = NewAnimation;
82 else if (m_type == NewAnimation)
82 }
83 m_type = ReplacePointAnimation;
83 else if(m_type == NewAnimation)
84 {
85 m_type = ReplacePointAnimation;
86 }
87
84
88 setKeyValueAt(0.0, qVariantFromValue(m_oldPoints));
85 setKeyValueAt(0.0, qVariantFromValue(m_oldPoints));
89 setKeyValueAt(1.0, qVariantFromValue(m_newPoints));
86 setKeyValueAt(1.0, qVariantFromValue(m_newPoints));
90 }
87 }
91
88
92 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
89 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
93 {
90 {
94 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
91 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
95 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
92 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
@@ -99,25 +96,23 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q
99
96
100 case ReplacePointAnimation:
97 case ReplacePointAnimation:
101 case AddPointAnimation:
98 case AddPointAnimation:
102 case RemovePointAnimation:
99 case RemovePointAnimation: {
103 {
100 if (startVector.count() != endVector.count())
104 if (startVector.count() != endVector.count()){
105 break;
101 break;
106 }
107
102
108 for(int i = 0; i < startVector.count(); i++) {
103 for (int i = 0; i < startVector.count(); i++) {
109 qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress);
104 qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress);
110 qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress);
105 qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress);
111 result << QPointF(x, y);
106 result << QPointF(x, y);
112 }
107 }
113
108
114 }
109 }
115 break;
110 break;
116 case NewAnimation: {
111 case NewAnimation: {
117 for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++)
112 for (int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++)
118 result << endVector[i];
113 result << endVector[i];
119 }
114 }
120 break;
115 break;
121 default:
116 default:
122 qWarning() << "Unknown type of animation";
117 qWarning() << "Unknown type of animation";
123 break;
118 break;
@@ -126,28 +121,28 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q
126 return qVariantFromValue(result);
121 return qVariantFromValue(result);
127 }
122 }
128
123
129 void XYAnimation::updateCurrentValue (const QVariant &value)
124 void XYAnimation::updateCurrentValue(const QVariant &value)
130 {
125 {
131 if(state()!=QAbstractAnimation::Stopped){ //workaround
126 if (state() != QAbstractAnimation::Stopped) { //workaround
132
127
133 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
128 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
134 m_item->setGeometryPoints(vector);
129 m_item->setGeometryPoints(vector);
135 m_item->updateGeometry();
130 m_item->updateGeometry();
136 m_item->setDirty(true);
131 m_item->setDirty(true);
137 m_dirty=false;
132 m_dirty = false;
138
133
139 }
134 }
140 }
135 }
141
136
142 void XYAnimation::updateState( QAbstractAnimation::State newState, QAbstractAnimation::State oldState )
137 void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
143 {
138 {
144 if(oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped)
139 if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) {
145 {
140 if (m_item->isDirty() && m_type == RemovePointAnimation) {
146 if(m_item->isDirty() && m_type==RemovePointAnimation){
141 if (!m_newPoints.isEmpty())
147 if(!m_newPoints.isEmpty()) m_newPoints.remove(m_index);
142 m_newPoints.remove(m_index);
148 m_item->setGeometryPoints(m_newPoints);
143 m_item->setGeometryPoints(m_newPoints);
149 }
144 }
150 }
145 }
151 }
146 }
152
147
153 #include "moc_chartanimation_p.cpp"
148 #include "moc_chartanimation_p.cpp"
@@ -44,12 +44,12 protected:
44 public:
44 public:
45 XYAnimation(XYChart *item);
45 XYAnimation(XYChart *item);
46 ~XYAnimation();
46 ~XYAnimation();
47 void setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints,int index = -1);
47 void setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index = -1);
48 Animation animationType() const { return m_type; };
48 Animation animationType() const { return m_type; };
49
49
50 protected:
50 protected:
51 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const;
51 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const;
52 void updateCurrentValue (const QVariant &value );
52 void updateCurrentValue(const QVariant &value);
53 void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
53 void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
54 protected:
54 protected:
55 Animation m_type;
55 Animation m_type;
@@ -35,21 +35,20 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
35
36 AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter)
36 AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter)
37 : ChartItem(presenter),
37 : ChartItem(presenter),
38 m_series(areaSeries),
38 m_series(areaSeries),
39 m_upper(0),
39 m_upper(0),
40 m_lower(0),
40 m_lower(0),
41 m_pointsVisible(false)
41 m_pointsVisible(false)
42 {
42 {
43 setZValue(ChartPresenter::LineChartZValue);
43 setZValue(ChartPresenter::LineChartZValue);
44 m_upper = new AreaBoundItem(this,m_series->upperSeries(),presenter);
44 m_upper = new AreaBoundItem(this, m_series->upperSeries(), presenter);
45 if (m_series->lowerSeries()){
45 if (m_series->lowerSeries())
46 m_lower = new AreaBoundItem(this,m_series->lowerSeries(),presenter);
46 m_lower = new AreaBoundItem(this, m_series->lowerSeries(), presenter);
47 }
48
47
49 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
48 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
50 QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
49 QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
51 QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
50 QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
52 QObject::connect(this,SIGNAL(clicked(QPointF)),areaSeries,SIGNAL(clicked(QPointF)));
51 QObject::connect(this, SIGNAL(clicked(QPointF)), areaSeries, SIGNAL(clicked(QPointF)));
53
52
54 handleUpdated();
53 handleUpdated();
55 }
54 }
@@ -81,8 +80,8 void AreaChartItem::updatePath()
81 } else {
80 } else {
82 QPointF first = path.pointAtPercent(0);
81 QPointF first = path.pointAtPercent(0);
83 QPointF last = path.pointAtPercent(1);
82 QPointF last = path.pointAtPercent(1);
84 path.lineTo(last.x(),m_clipRect.bottom());
83 path.lineTo(last.x(), m_clipRect.bottom());
85 path.lineTo(first.x(),m_clipRect.bottom());
84 path.lineTo(first.x(), m_clipRect.bottom());
86 }
85 }
87 path.closeSubpath();
86 path.closeSubpath();
88 prepareGeometryChange();
87 prepareGeometryChange();
@@ -108,7 +107,7 void AreaChartItem::handleDomainUpdated()
108 {
107 {
109 m_upper->setDomain(domain());
108 m_upper->setDomain(domain());
110 m_upper->handleDomainUpdated();
109 m_upper->handleDomainUpdated();
111 if (m_lower){
110 if (m_lower) {
112 m_lower->setDomain(domain());
111 m_lower->setDomain(domain());
113 m_lower->handleDomainUpdated();
112 m_lower->handleDomainUpdated();
114 }
113 }
@@ -116,7 +115,7 void AreaChartItem::handleDomainUpdated()
116
115
117 void AreaChartItem::handleGeometryChanged(const QRectF &rect)
116 void AreaChartItem::handleGeometryChanged(const QRectF &rect)
118 {
117 {
119 m_clipRect=rect.translated(-rect.topLeft());
118 m_clipRect = rect.translated(-rect.topLeft());
120 setPos(rect.topLeft());
119 setPos(rect.topLeft());
121 m_upper->handleGeometryChanged(rect);
120 m_upper->handleGeometryChanged(rect);
122 if (m_lower)
121 if (m_lower)
@@ -134,10 +133,10 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
134 painter->setClipRect(m_clipRect);
133 painter->setClipRect(m_clipRect);
135 painter->drawPath(m_path);
134 painter->drawPath(m_path);
136 if (m_pointsVisible) {
135 if (m_pointsVisible) {
137 painter->setPen(m_pointPen);
136 painter->setPen(m_pointPen);
138 painter->drawPoints(m_upper->geometryPoints());
137 painter->drawPoints(m_upper->geometryPoints());
139 if (m_lower)
138 if (m_lower)
140 painter->drawPoints(m_lower->geometryPoints());
139 painter->drawPoints(m_lower->geometryPoints());
141 }
140 }
142 painter->restore();
141 painter->restore();
143 }
142 }
@@ -84,7 +84,9 private:
84 class AreaBoundItem : public LineChartItem
84 class AreaBoundItem : public LineChartItem
85 {
85 {
86 public:
86 public:
87 AreaBoundItem(AreaChartItem *item,QLineSeries *lineSeries,ChartPresenter* presenter) : LineChartItem(lineSeries, 0), m_item(item) {
87 AreaBoundItem(AreaChartItem *item, QLineSeries *lineSeries, ChartPresenter* presenter)
88 : LineChartItem(lineSeries, 0), m_item(item)
89 {
88 setPresenter(presenter);
90 setPresenter(presenter);
89 }
91 }
90 ~AreaBoundItem() {}
92 ~AreaBoundItem() {}
@@ -36,11 +36,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 \mainclass
37 \mainclass
38
38
39 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
39 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
40 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
40 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
41 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
41 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
42 In that case QAreaSeries should be initiated with two QLineSeries instances. Please note terms "upper" and "lower" boundary can be misleading in cases
42 In that case QAreaSeries should be initiated with two QLineSeries instances. Please note terms "upper" and "lower" boundary can be misleading in cases
43 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
43 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
44
44
45 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
45 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
46 \image examples_areachart.png
46 \image examples_areachart.png
@@ -162,7 +162,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
162 When series object is added to QChartView or QChart instance ownerships is transferred.
162 When series object is added to QChartView or QChart instance ownerships is transferred.
163 */
163 */
164 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
164 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
165 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries,lowerSeries,this),upperSeries)
165 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries, lowerSeries, this), upperSeries)
166 {
166 {
167 }
167 }
168
168
@@ -180,9 +180,8 QAreaSeries::QAreaSeries(QObject *parent)
180 QAreaSeries::~QAreaSeries()
180 QAreaSeries::~QAreaSeries()
181 {
181 {
182 Q_D(QAreaSeries);
182 Q_D(QAreaSeries);
183 if(d->m_dataset){
183 if (d->m_dataset)
184 d->m_dataset->removeSeries(this);
184 d->m_dataset->removeSeries(this);
185 }
186 }
185 }
187
186
188 /*!
187 /*!
@@ -315,11 +314,11 bool QAreaSeries::pointsVisible() const
315
314
316 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
315 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
317
316
318 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q) :
317 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries* q)
319 QAbstractSeriesPrivate(q),
318 : QAbstractSeriesPrivate(q),
320 m_upperSeries(upperSeries),
319 m_upperSeries(upperSeries),
321 m_lowerSeries(lowerSeries),
320 m_lowerSeries(lowerSeries),
322 m_pointsVisible(false)
321 m_pointsVisible(false)
323 {
322 {
324 }
323 }
325
324
@@ -337,8 +336,7 void QAreaSeriesPrivate::scaleDomain(Domain& domain)
337
336
338 const QList<QPointF>& points = upperSeries->points();
337 const QList<QPointF>& points = upperSeries->points();
339
338
340 for (int i = 0; i < points.count(); i++)
339 for (int i = 0; i < points.count(); i++) {
341 {
342 qreal x = points[i].x();
340 qreal x = points[i].x();
343 qreal y = points[i].y();
341 qreal y = points[i].y();
344 minX = qMin(minX, x);
342 minX = qMin(minX, x);
@@ -346,33 +344,32 void QAreaSeriesPrivate::scaleDomain(Domain& domain)
346 maxX = qMax(maxX, x);
344 maxX = qMax(maxX, x);
347 maxY = qMax(maxY, y);
345 maxY = qMax(maxY, y);
348 }
346 }
349 if(lowerSeries) {
347 if (lowerSeries) {
350
348
351 const QList<QPointF>& points = lowerSeries->points();
349 const QList<QPointF>& points = lowerSeries->points();
352
350
353 for (int i = 0; i < points.count(); i++)
351 for (int i = 0; i < points.count(); i++) {
354 {
355 qreal x = points[i].x();
352 qreal x = points[i].x();
356 qreal y = points[i].y();
353 qreal y = points[i].y();
357 minX = qMin(minX, x);
354 minX = qMin(minX, x);
358 minY = qMin(minY, y);
355 minY = qMin(minY, y);
359 maxX = qMax(maxX, x);
356 maxX = qMax(maxX, x);
360 maxY = qMax(maxY, y);
357 maxY = qMax(maxY, y);
361 }}
358 }
359 }
362
360
363 domain.setRange(minX,maxX,minY,maxY);
361 domain.setRange(minX, maxX, minY, maxY);
364 }
362 }
365
363
366 ChartElement* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
364 ChartElement* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
367 {
365 {
368 Q_Q(QAreaSeries);
366 Q_Q(QAreaSeries);
369
367
370 AreaChartItem* area = new AreaChartItem(q,presenter);
368 AreaChartItem* area = new AreaChartItem(q, presenter);
371 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
369 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
372 area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem()));
370 area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem()));
373 if(q->lowerSeries()) {
371 if (q->lowerSeries())
374 area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem()));
372 area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem()));
375 }
376 }
373 }
377 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
374 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
378 return area;
375 return area;
@@ -382,7 +379,7 QList<LegendMarker*> QAreaSeriesPrivate::createLegendMarker(QLegend* legend)
382 {
379 {
383 Q_Q(QAreaSeries);
380 Q_Q(QAreaSeries);
384 QList<LegendMarker*> list;
381 QList<LegendMarker*> list;
385 return list << new AreaLegendMarker(q,legend);
382 return list << new AreaLegendMarker(q, legend);
386 }
383 }
387
384
388
385
@@ -41,7 +41,7 class QAreaSeriesPrivate: public QAbstractSeriesPrivate
41 Q_OBJECT
41 Q_OBJECT
42
42
43 public:
43 public:
44 QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q);
44 QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries* q);
45
45
46 void scaleDomain(Domain& domain);
46 void scaleDomain(Domain& domain);
47 ChartElement* createGraphics(ChartPresenter* presenter);
47 ChartElement* createGraphics(ChartPresenter* presenter);
@@ -29,8 +29,9 static int label_padding = 5;
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter)
33 m_categoriesAxis(axis)
33 : ChartAxis(axis, presenter),
34 m_categoriesAxis(axis)
34 {
35 {
35
36
36 }
37 }
@@ -43,31 +44,29 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
43 {
44 {
44 int count = m_categoriesAxis->d_ptr->count();
45 int count = m_categoriesAxis->d_ptr->count();
45
46
46 Q_ASSERT(count>=1);
47 Q_ASSERT(count >= 1);
47
48
48 QVector<qreal> points;
49 QVector<qreal> points;
49 points.resize(count+2);
50 points.resize(count + 2);
50
51
51 QRectF rect = presenter()->chartsGeometry();
52 QRectF rect = presenter()->chartsGeometry();
52
53
53 const qreal delta = rect.width()/(count);
54 const qreal delta = rect.width() / (count);
54 qreal offset =-m_min-0.5;
55 qreal offset = -m_min - 0.5;
55
56
56 if(delta<1) return points;
57 if (delta < 1) return points;
57
58
58 if(offset<=0) {
59 if (offset <= 0)
59 offset = int(offset * rect.width()/(m_max - m_min))%int(delta) + delta;
60 offset = int(offset * rect.width() / (m_max - m_min)) % int(delta) + delta;
60 }
61 else
61 else {
62 offset = int(offset * rect.width() / (m_max - m_min)) % int(delta);
62 offset = int(offset * rect.width()/(m_max - m_min))%int(delta);
63 }
64
63
65 points[0] = rect.left();
64 points[0] = rect.left();
66 points[count+1] = rect.right();
65 points[count + 1] = rect.right();
67
66
68 for (int i = 0; i < count; ++i) {
67 for (int i = 0; i < count; ++i) {
69 qreal x = offset + i * delta + rect.left();
68 qreal x = offset + i * delta + rect.left();
70 points[i+1] = x;
69 points[i + 1] = x;
71 }
70 }
72 return points;
71 return points;
73 }
72 }
@@ -76,16 +75,13 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& la
76 {
75 {
77 QStringList result;
76 QStringList result;
78 QRectF rect = presenter()->chartsGeometry();
77 QRectF rect = presenter()->chartsGeometry();
79 qreal d = (m_max - m_min)/rect.width();
78 qreal d = (m_max - m_min) / rect.width();
80 for (int i = 0;i < layout.count()-1; ++i) {
79 for (int i = 0; i < layout.count() - 1; ++i) {
81 qreal x = qFloor((((layout[i+1] + layout[i])/2-rect.left())*d + m_min+0.5));
80 qreal x = qFloor((((layout[i + 1] + layout[i]) / 2 - rect.left()) * d + m_min + 0.5));
82 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
81 if ((x < m_categoriesAxis->categories().count()) && (x >= 0))
83 result << m_categoriesAxis->categories().at(x);
82 result << m_categoriesAxis->categories().at(x);
84 }
83 else
85 else {
84 result << ""; // No label for x coordinate
86 // No label for x coordinate
87 result << "";
88 }
89 }
85 }
90 result << "";
86 result << "";
91 return result;
87 return result;
@@ -96,7 +92,8 void ChartBarCategoryAxisX::updateGeometry()
96 {
92 {
97 const QVector<qreal>& layout = ChartAxis::layout();
93 const QVector<qreal>& layout = ChartAxis::layout();
98
94
99 if(layout.isEmpty()) return;
95 if (layout.isEmpty())
96 return;
100
97
101 QStringList ticksList = createCategoryLabels(layout);
98 QStringList ticksList = createCategoryLabels(layout);
102
99
@@ -110,10 +107,10 void ChartBarCategoryAxisX::updateGeometry()
110
107
111 QRectF chartRect = presenter()->chartsGeometry();
108 QRectF chartRect = presenter()->chartsGeometry();
112
109
113 const qreal delta = chartRect.width()/(m_categoriesAxis->d_ptr->count());
110 const qreal delta = chartRect.width() / (m_categoriesAxis->d_ptr->count());
114
111
115 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
112 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
116 lineItem->setLine( chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom());
113 lineItem->setLine(chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom());
117
114
118 qreal width = chartRect.left();
115 qreal width = chartRect.left();
119 for (int i = 0; i < layout.size(); ++i) {
116 for (int i = 0; i < layout.size(); ++i) {
@@ -125,35 +122,32 void ChartBarCategoryAxisX::updateGeometry()
125 QPointF center = rect.center();
122 QPointF center = rect.center();
126 labelItem->setTransformOriginPoint(center.x(), center.y());
123 labelItem->setTransformOriginPoint(center.x(), center.y());
127
124
128 if(i==0){
125 if (i == 0)
129 labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), chartRect.bottom() + label_padding);
126 labelItem->setPos(layout[i + 1] - (delta) / 2 - center.x(), chartRect.bottom() + label_padding);
130 }else{
127 else
131 labelItem->setPos(layout[i] + (delta)/2 - center.x(), chartRect.bottom() + label_padding);
128 labelItem->setPos(layout[i] + (delta) / 2 - center.x(), chartRect.bottom() + label_padding);
132 }
133
129
134 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()> chartRect.right()) {
130 if (labelItem->pos().x() <= width || labelItem->pos().x() + rect.width() > chartRect.right()) {
135 labelItem->setVisible(false);
131 labelItem->setVisible(false);
136 }
132 } else {
137 else {
138 labelItem->setVisible(true);
133 labelItem->setVisible(true);
139 width=rect.width()+labelItem->pos().x();
134 width = rect.width() + labelItem->pos().x();
140 }
135 }
141
136
142 if ((i+1)%2 && i>1) {
137 if ((i + 1) % 2 && i > 1) {
143 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
138 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
144 rectItem->setRect(layout[i-1], chartRect.top(),layout[i]-layout[i-1], chartRect.height());
139 rectItem->setRect(layout[i - 1], chartRect.top(), layout[i] - layout[i - 1], chartRect.height());
145 }
140 }
146 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
141 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
147 lineItem->setLine(layout[i], chartRect.bottom(),layout[i], chartRect.bottom()+5);
142 lineItem->setLine(layout[i], chartRect.bottom(), layout[i], chartRect.bottom() + 5);
148 }
143 }
149 }
144 }
150
145
151 void ChartBarCategoryAxisX::handleAxisUpdated()
146 void ChartBarCategoryAxisX::handleAxisUpdated()
152 {
147 {
153 if(m_categoriesAxis->categories()!=m_categories)
148 if (m_categoriesAxis->categories() != m_categories) {
154 {
149 m_categories = m_categoriesAxis->categories();
155 m_categories=m_categoriesAxis->categories();
150 if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) updateGeometry();
156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry();
157 }
151 }
158 ChartAxis::handleAxisUpdated();
152 ChartAxis::handleAxisUpdated();
159 }
153 }
@@ -166,35 +160,32 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constra
166 QSizeF sh;
160 QSizeF sh;
167 QSizeF base = ChartAxis::sizeHint(which, constraint);
161 QSizeF base = ChartAxis::sizeHint(which, constraint);
168 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
162 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
169 qreal width=0;
163 qreal width = 0;
170 qreal height=0;
164 qreal height = 0;
171
165
172 switch (which) {
166 switch (which) {
173 case Qt::MinimumSize:
167 case Qt::MinimumSize:
174 width = fn.boundingRect("...").width();
168 width = fn.boundingRect("...").width();
175 height = fn.height()+label_padding;
169 height = fn.height() + label_padding;
176 width=qMax(width,base.width());
170 width = qMax(width, base.width());
177 height+=base.height();
171 height += base.height();
178 sh = QSizeF(width,height);
172 sh = QSizeF(width, height);
179 break;
173 break;
180 case Qt::PreferredSize:{
174 case Qt::PreferredSize:
181
175 for (int i = 0; i < ticksList.size(); ++i) {
182 for (int i = 0; i < ticksList.size(); ++i)
176 QRectF rect = fn.boundingRect(ticksList.at(i));
183 {
177 width += rect.width();
184 QRectF rect = fn.boundingRect(ticksList.at(i));
178 height = qMax(rect.height() + label_padding, height);
185 width+=rect.width();
186 height=qMax(rect.height()+label_padding,height);
187 }
188 width=qMax(width,base.width());
189 height+=base.height();
190 sh = QSizeF(width,height);
191 break;
192 }
179 }
193 default:
180 width = qMax(width, base.width());
194 break;
181 height += base.height();
195 }
182 sh = QSizeF(width, height);
183 break;
184 default:
185 break;
186 }
196
187
197 return sh;
188 return sh;
198 }
189 }
199
190
200 QTCOMMERCIALCHART_END_NAMESPACE
191 QTCOMMERCIALCHART_END_NAMESPACE
@@ -51,7 +51,7 protected:
51 void updateGeometry();
51 void updateGeometry();
52 private:
52 private:
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
54 Q_SLOTS
54 public Q_SLOTS:
55 void handleAxisUpdated();
55 void handleAxisUpdated();
56
56
57 private:
57 private:
@@ -29,8 +29,9 static int label_padding = 5;
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter)
33 m_categoriesAxis(axis)
33 : ChartAxis(axis, presenter),
34 m_categoriesAxis(axis)
34 {
35 {
35 }
36 }
36
37
@@ -42,31 +43,29 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
42 {
43 {
43 int count = m_categoriesAxis->d_ptr->count();
44 int count = m_categoriesAxis->d_ptr->count();
44
45
45 Q_ASSERT(count>=1);
46 Q_ASSERT(count >= 1);
46
47
47 QVector<qreal> points;
48 QVector<qreal> points;
48 points.resize(count+2);
49 points.resize(count + 2);
49
50
50 QRectF rect = presenter()->chartsGeometry();
51 QRectF rect = presenter()->chartsGeometry();
51
52
52 const qreal delta = rect.height()/(count);
53 const qreal delta = rect.height() / (count);
53 qreal offset = - m_min - 0.5;
54 qreal offset = - m_min - 0.5;
54
55
55 if(delta<1) return points;
56 if (delta < 1) return points;
56
57
57 if(offset<=0) {
58 if (offset <= 0)
58 offset = int(offset * rect.height()/(m_max - m_min))%int(delta) + delta;
59 offset = int(offset * rect.height() / (m_max - m_min)) % int(delta) + delta;
59 }
60 else
60 else {
61 offset = int(offset * rect.height() / (m_max - m_min)) % int(delta);
61 offset = int(offset * rect.height()/(m_max - m_min))%int(delta);
62 }
63
62
64 points[0] = rect.bottom();
63 points[0] = rect.bottom();
65 points[count+1] = rect.top();
64 points[count + 1] = rect.top();
66
65
67 for (int i = 0; i < count; ++i) {
66 for (int i = 0; i < count; ++i) {
68 int y = rect.bottom() - i * delta - offset;
67 int y = rect.bottom() - i * delta - offset;
69 points[i+1] = y;
68 points[i + 1] = y;
70 }
69 }
71 return points;
70 return points;
72 }
71 }
@@ -75,16 +74,13 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& la
75 {
74 {
76 QStringList result;
75 QStringList result;
77 QRectF rect = presenter()->chartsGeometry();
76 QRectF rect = presenter()->chartsGeometry();
78 qreal d = (m_max - m_min)/rect.height();
77 qreal d = (m_max - m_min) / rect.height();
79 for (int i = 0;i < layout.count()-1; ++i) {
78 for (int i = 0; i < layout.count() - 1; ++i) {
80 qreal x = qFloor(((rect.height()- (layout[i+1] + layout[i])/2 + rect.top())*d + m_min+0.5));
79 qreal x = qFloor(((rect.height() - (layout[i + 1] + layout[i]) / 2 + rect.top()) * d + m_min + 0.5));
81 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
80 if ((x < m_categoriesAxis->categories().count()) && (x >= 0))
82 result << m_categoriesAxis->categories().at(x);
81 result << m_categoriesAxis->categories().at(x);
83 }
82 else
84 else {
83 result << ""; // No label for x coordinate
85 // No label for x coordinate
86 result << "";
87 }
88 }
84 }
89 result << "";
85 result << "";
90 return result;
86 return result;
@@ -94,7 +90,8 void ChartBarCategoryAxisY::updateGeometry()
94 {
90 {
95 const QVector<qreal>& layout = ChartAxis::layout();
91 const QVector<qreal>& layout = ChartAxis::layout();
96
92
97 if(layout.isEmpty()) return;
93 if (layout.isEmpty())
94 return;
98
95
99 QStringList ticksList = createCategoryLabels(layout);
96 QStringList ticksList = createCategoryLabels(layout);
100
97
@@ -108,7 +105,7 void ChartBarCategoryAxisY::updateGeometry()
108
105
109 QRectF chartRect = presenter()->chartsGeometry();
106 QRectF chartRect = presenter()->chartsGeometry();
110
107
111 const qreal delta = chartRect.height()/(m_categoriesAxis->d_ptr->count());
108 const qreal delta = chartRect.height() / (m_categoriesAxis->d_ptr->count());
112
109
113 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
110 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
114 lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
111 lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
@@ -123,39 +120,34 void ChartBarCategoryAxisY::updateGeometry()
123 QPointF center = rect.center();
120 QPointF center = rect.center();
124 labelItem->setTransformOriginPoint(center.x(), center.y());
121 labelItem->setTransformOriginPoint(center.x(), center.y());
125
122
126 if(i==0) {
123 if (i == 0)
127 labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y());
124 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i + 1] + (delta) / 2 - center.y());
128 }
125 else
129 else {
126 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] - (delta) / 2 - center.y());
130 labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y());
131 }
132
127
133 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < chartRect.top()) {
128 if (labelItem->pos().y() + rect.height() >= height || labelItem->pos().y() < chartRect.top()) {
134 labelItem->setVisible(false);
129 labelItem->setVisible(false);
135 }
130 } else {
136 else {
137 labelItem->setVisible(true);
131 labelItem->setVisible(true);
138 height=labelItem->pos().y();
132 height = labelItem->pos().y();
139 }
133 }
140
134
141 if ((i+1)%2 && i>1) {
135 if ((i + 1) % 2 && i > 1) {
142 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
136 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
143 rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]);
137 rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]);
144 }
138 }
145 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
139 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
146 lineItem->setLine(chartRect.left()-5,layout[i],chartRect.left(),layout[i]);
140 lineItem->setLine(chartRect.left() - 5, layout[i], chartRect.left(), layout[i]);
147 }
141 }
148 }
142 }
149
143
150 void ChartBarCategoryAxisY::handleAxisUpdated()
144 void ChartBarCategoryAxisY::handleAxisUpdated()
151 {
145 {
152
146
153 if(m_categoriesAxis->categories()!=m_categories)
147 if (m_categoriesAxis->categories() != m_categories) {
154 {
148 m_categories = m_categoriesAxis->categories();
155 m_categories=m_categoriesAxis->categories();
149 if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2)
156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) {
157 updateGeometry();
150 updateGeometry();
158 }
159 }
151 }
160 ChartAxis::handleAxisUpdated();
152 ChartAxis::handleAxisUpdated();
161 }
153 }
@@ -168,35 +160,32 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constra
168 QSizeF sh;
160 QSizeF sh;
169 QSizeF base = ChartAxis::sizeHint(which, constraint);
161 QSizeF base = ChartAxis::sizeHint(which, constraint);
170 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
162 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
171 qreal width=0;
163 qreal width = 0;
172 qreal height=0;
164 qreal height = 0;
173
165
174 switch (which) {
166 switch (which) {
175 case Qt::MinimumSize:
167 case Qt::MinimumSize:
176 width = fn.boundingRect("...").width() + label_padding;
168 width = fn.boundingRect("...").width() + label_padding;
177 height = fn.height();
169 height = fn.height();
178 width+=base.width();
170 width += base.width();
179 height=qMax(height,base.height());
171 height = qMax(height, base.height());
180 sh = QSizeF(width,height);
172 sh = QSizeF(width, height);
181 break;
173 break;
182 case Qt::PreferredSize:{
174 case Qt::PreferredSize:
183
175 for (int i = 0; i < ticksList.size(); ++i) {
184 for (int i = 0; i < ticksList.size(); ++i)
176 QRectF rect = fn.boundingRect(ticksList.at(i));
185 {
177 height += rect.height();
186 QRectF rect = fn.boundingRect(ticksList.at(i));
178 width = qMax(rect.width() + label_padding, width);
187 height+=rect.height();
188 width=qMax(rect.width()+label_padding,width);
189 }
190 height=qMax(height,base.height());
191 width+=base.width();
192 sh = QSizeF(width,height);
193 break;
194 }
179 }
195 default:
180 height = qMax(height, base.height());
196 break;
181 width += base.width();
197 }
182 sh = QSizeF(width, height);
183 break;
184 default:
185 break;
186 }
198
187
199 return sh;
188 return sh;
200 }
189 }
201
190
202 QTCOMMERCIALCHART_END_NAMESPACE
191 QTCOMMERCIALCHART_END_NAMESPACE
@@ -51,8 +51,8 protected:
51 void updateGeometry();
51 void updateGeometry();
52 private:
52 private:
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
54 Q_SLOTS
54 public Q_SLOTS:
55 void handleAxisUpdated();
55 void handleAxisUpdated();
56 private:
56 private:
57 QStringList m_categories;
57 QStringList m_categories;
58 QBarCategoryAxis *m_categoriesAxis;
58 QBarCategoryAxis *m_categoriesAxis;
@@ -131,7 +131,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
131 Constructs an axis object which is a child of \a parent.
131 Constructs an axis object which is a child of \a parent.
132 */
132 */
133 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
133 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
134 QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent)
134 QAbstractAxis(*new QBarCategoryAxisPrivate(this), parent)
135 {
135 {
136 }
136 }
137
137
@@ -141,15 +141,15 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
141 QBarCategoryAxis::~QBarCategoryAxis()
141 QBarCategoryAxis::~QBarCategoryAxis()
142 {
142 {
143 Q_D(QBarCategoryAxis);
143 Q_D(QBarCategoryAxis);
144 if(d->m_dataset){
144 if (d->m_dataset)
145 d->m_dataset->removeAxis(this);
145 d->m_dataset->removeAxis(this);
146 }
147 }
146 }
148
147
149 /*!
148 /*!
150 \internal
149 \internal
151 */
150 */
152 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
151 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent)
152 : QAbstractAxis(d, parent)
153 {
153 {
154
154
155 }
155 }
@@ -159,13 +159,14 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):Q
159 */
159 */
160 void QBarCategoryAxis::append(const QStringList &categories)
160 void QBarCategoryAxis::append(const QStringList &categories)
161 {
161 {
162 if(categories.isEmpty()) return;
162 if (categories.isEmpty())
163 return;
163
164
164 Q_D(QBarCategoryAxis);
165 Q_D(QBarCategoryAxis);
165 if (d->m_categories.isEmpty()) {
166 if (d->m_categories.isEmpty()) {
166 d->m_categories.append(categories);
167 d->m_categories.append(categories);
167 setRange(categories.first(),categories.last());
168 setRange(categories.first(), categories.last());
168 }else{
169 } else {
169 d->m_categories.append(categories);
170 d->m_categories.append(categories);
170 d->emitUpdated();
171 d->emitUpdated();
171 }
172 }
@@ -180,8 +181,8 void QBarCategoryAxis::append(const QString &category)
180 Q_D(QBarCategoryAxis);
181 Q_D(QBarCategoryAxis);
181 if (d->m_categories.isEmpty()) {
182 if (d->m_categories.isEmpty()) {
182 d->m_categories.append(category);
183 d->m_categories.append(category);
183 setRange(category,category);
184 setRange(category, category);
184 }else{
185 } else {
185 d->m_categories.append(category);
186 d->m_categories.append(category);
186 d->emitUpdated();
187 d->emitUpdated();
187 }
188 }
@@ -196,10 +197,10 void QBarCategoryAxis::remove(const QString &category)
196 Q_D(QBarCategoryAxis);
197 Q_D(QBarCategoryAxis);
197 if (d->m_categories.contains(category)) {
198 if (d->m_categories.contains(category)) {
198 d->m_categories.removeAt(d->m_categories.indexOf(category));
199 d->m_categories.removeAt(d->m_categories.indexOf(category));
199 if(!d->m_categories.isEmpty())
200 if (!d->m_categories.isEmpty())
200 setRange(d->m_categories.first(),d->m_categories.last());
201 setRange(d->m_categories.first(), d->m_categories.last());
201 else
202 else
202 setRange(QString::null,QString::null);
203 setRange(QString::null, QString::null);
203 emit categoriesChanged();
204 emit categoriesChanged();
204 }
205 }
205 }
206 }
@@ -211,10 +212,10 void QBarCategoryAxis::insert(int index, const QString &category)
211 {
212 {
212 Q_D(QBarCategoryAxis);
213 Q_D(QBarCategoryAxis);
213 if (d->m_categories.isEmpty()) {
214 if (d->m_categories.isEmpty()) {
214 d->m_categories.insert(index,category);
215 d->m_categories.insert(index, category);
215 setRange(category,category);
216 setRange(category, category);
216 }else{
217 } else {
217 d->m_categories.insert(index,category);
218 d->m_categories.insert(index, category);
218 d->emitUpdated();
219 d->emitUpdated();
219 }
220 }
220 emit categoriesChanged();
221 emit categoriesChanged();
@@ -242,16 +243,16 void QBarCategoryAxis::clear()
242 {
243 {
243 Q_D(QBarCategoryAxis);
244 Q_D(QBarCategoryAxis);
244 d->m_categories.clear();
245 d->m_categories.clear();
245 setRange(QString::null,QString::null);
246 setRange(QString::null, QString::null);
246 emit categoriesChanged();
247 emit categoriesChanged();
247 }
248 }
248
249
249 void QBarCategoryAxis::setCategories(const QStringList &categories)
250 void QBarCategoryAxis::setCategories(const QStringList &categories)
250 {
251 {
251 Q_D(QBarCategoryAxis);
252 Q_D(QBarCategoryAxis);
252 if(d->m_categories!=categories) {
253 if (d->m_categories != categories) {
253 d->m_categories = categories;
254 d->m_categories = categories;
254 setRange(categories.first(),categories.last());
255 setRange(categories.first(), categories.last());
255 emit categoriesChanged();
256 emit categoriesChanged();
256 }
257 }
257 }
258 }
@@ -286,7 +287,7 QString QBarCategoryAxis::at(int index) const
286 void QBarCategoryAxis::setMin(const QString& min)
287 void QBarCategoryAxis::setMin(const QString& min)
287 {
288 {
288 Q_D(QBarCategoryAxis);
289 Q_D(QBarCategoryAxis);
289 setRange(min,d->m_maxCategory);
290 setRange(min, d->m_maxCategory);
290 }
291 }
291
292
292 /*!
293 /*!
@@ -304,7 +305,7 QString QBarCategoryAxis::min() const
304 void QBarCategoryAxis::setMax(const QString& max)
305 void QBarCategoryAxis::setMax(const QString& max)
305 {
306 {
306 Q_D(QBarCategoryAxis);
307 Q_D(QBarCategoryAxis);
307 setRange(d->m_minCategory,max);
308 setRange(d->m_minCategory, max);
308 }
309 }
309
310
310 /*!
311 /*!
@@ -326,28 +327,29 void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCa
326 bool changed = false;
327 bool changed = false;
327
328
328 //special case
329 //special case
329 if(minCategory.isNull() && maxCategory.isNull()){
330 if (minCategory.isNull() && maxCategory.isNull()) {
330 d->m_minCategory = minCategory;
331 d->m_minCategory = minCategory;
331 d->m_maxCategory = maxCategory;
332 d->m_maxCategory = maxCategory;
332 d->m_min = 0;
333 d->m_min = 0;
333 d->m_max = 0;
334 d->m_max = 0;
334 emit minChanged(minCategory);
335 emit minChanged(minCategory);
335 emit maxChanged(maxCategory);
336 emit maxChanged(maxCategory);
336 d->m_count=0;
337 d->m_count = 0;
337 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
338 emit rangeChanged(d->m_minCategory, d->m_maxCategory);
338 d->emitUpdated();
339 d->emitUpdated();
339 }
340 }
340
341
341 if(d->m_categories.indexOf(d->m_maxCategory)<d->m_categories.indexOf(d->m_minCategory)) return;
342 if (d->m_categories.indexOf(d->m_maxCategory) < d->m_categories.indexOf(d->m_minCategory))
343 return;
342
344
343 if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) {
345 if (!minCategory.isEmpty() && d->m_minCategory != minCategory && d->m_categories.contains(minCategory)) {
344 d->m_minCategory = minCategory;
346 d->m_minCategory = minCategory;
345 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
347 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
346 changed = true;
348 changed = true;
347 emit minChanged(minCategory);
349 emit minChanged(minCategory);
348 }
350 }
349
351
350 if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) {
352 if (!maxCategory.isEmpty() && d->m_maxCategory != maxCategory && d->m_categories.contains(maxCategory)) {
351 d->m_maxCategory = maxCategory;
353 d->m_maxCategory = maxCategory;
352 d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5;
354 d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5;
353 changed = true;
355 changed = true;
@@ -355,8 +357,8 void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCa
355 }
357 }
356
358
357 if (changed) {
359 if (changed) {
358 d->m_count=d->m_max - d->m_min;
360 d->m_count = d->m_max - d->m_min;
359 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
361 emit rangeChanged(d->m_minCategory, d->m_maxCategory);
360 d->emitUpdated();
362 d->emitUpdated();
361 }
363 }
362 }
364 }
@@ -387,12 +389,12 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
387
389
388 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
390 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
389 {
391 {
390 setRange(min,m_maxCategory);
392 setRange(min, m_maxCategory);
391 }
393 }
392
394
393 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
395 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
394 {
396 {
395 setRange(m_minCategory,max);
397 setRange(m_minCategory, max);
396 }
398 }
397
399
398 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
400 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
@@ -400,7 +402,7 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
400 Q_Q(QBarCategoryAxis);
402 Q_Q(QBarCategoryAxis);
401 QString value1 = min.toString();
403 QString value1 = min.toString();
402 QString value2 = max.toString();
404 QString value2 = max.toString();
403 q->setRange(value1,value2);
405 q->setRange(value1, value2);
404 }
406 }
405
407
406 void QBarCategoryAxisPrivate::handleDomainUpdated()
408 void QBarCategoryAxisPrivate::handleDomainUpdated()
@@ -408,11 +410,10 void QBarCategoryAxisPrivate::handleDomainUpdated()
408 Q_Q(QBarCategoryAxis);
410 Q_Q(QBarCategoryAxis);
409 Domain* domain = qobject_cast<Domain*>(sender());
411 Domain* domain = qobject_cast<Domain*>(sender());
410
412
411 if(m_orientation==Qt::Horizontal) {
413 if (m_orientation == Qt::Horizontal) {
412 m_min = domain->minX();
414 m_min = domain->minX();
413 m_max = domain->maxX();
415 m_max = domain->maxX();
414 }
416 } else if (m_orientation == Qt::Vertical) {
415 else if(m_orientation==Qt::Vertical) {
416 m_min = domain->minY();
417 m_min = domain->minY();
417 m_max = domain->maxY();
418 m_max = domain->maxY();
418 }
419 }
@@ -420,64 +421,57 void QBarCategoryAxisPrivate::handleDomainUpdated()
420 bool changed = false;
421 bool changed = false;
421
422
422 int min = m_min + 0.5;
423 int min = m_min + 0.5;
423 if(min>=0 && min<m_categories.count()) {
424 if (min >= 0 && min < m_categories.count()) {
424 QString minCategory = m_categories.at(min);
425 QString minCategory = m_categories.at(min);
425 if(m_minCategory!=minCategory && !minCategory.isEmpty()) {
426 if (m_minCategory != minCategory && !minCategory.isEmpty()) {
426 m_minCategory=minCategory;
427 m_minCategory = minCategory;
427 changed=true;
428 changed = true;
428 emit q->minChanged(minCategory);
429 emit q->minChanged(minCategory);
429 }
430 }
430 }
431 }
431 int max = m_max - 0.5;
432 int max = m_max - 0.5;
432 if(max>=0 && max<m_categories.count()) {
433 if (max >= 0 && max < m_categories.count()) {
433 QString maxCategory = m_categories.at(max);
434 QString maxCategory = m_categories.at(max);
434 if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) {
435 if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) {
435 m_maxCategory=maxCategory;
436 m_maxCategory = maxCategory;
436 emit q->maxChanged(maxCategory);
437 emit q->maxChanged(maxCategory);
437 }
438 }
438 }
439 }
439
440
440 if (changed) {
441 if (changed)
441 emit q->rangeChanged(m_minCategory,m_maxCategory);
442 emit q->rangeChanged(m_minCategory, m_maxCategory);
442 }
443 }
443 }
444
444
445 ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
445 ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
446 {
446 {
447 Q_Q(QBarCategoryAxis);
447 Q_Q(QBarCategoryAxis);
448 if(m_orientation == Qt::Vertical){
448 if (m_orientation == Qt::Vertical)
449 return new ChartBarCategoryAxisY(q,presenter);
449 return new ChartBarCategoryAxisY(q, presenter);
450 }else{
450 return new ChartBarCategoryAxisX(q, presenter);
451 return new ChartBarCategoryAxisX(q,presenter);
452 }
453 }
451 }
454
452
455 void QBarCategoryAxisPrivate::intializeDomain(Domain* domain)
453 void QBarCategoryAxisPrivate::intializeDomain(Domain* domain)
456 {
454 {
457
455
458 Q_Q(QBarCategoryAxis);
456 Q_Q(QBarCategoryAxis);
459 if(m_max==m_min) {
457 if (m_max == m_min) {
460 int min;
458 int min;
461 int max;
459 int max;
462 if(m_orientation==Qt::Vertical) {
460 if (m_orientation == Qt::Vertical) {
463 min = domain->minY() + 0.5;
461 min = domain->minY() + 0.5;
464 max = domain->maxY() - 0.5;
462 max = domain->maxY() - 0.5;
465 }
463 } else {
466 else {
467 min = domain->minX() + 0.5;
464 min = domain->minX() + 0.5;
468 max = domain->maxX() - 0.5;
465 max = domain->maxX() - 0.5;
469 }
466 }
470
467
471 if(min>0 && min<m_categories.count() && max>0 && max<m_categories.count())
468 if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count())
472 q->setRange(m_categories.at(min),m_categories.at(max));
469 q->setRange(m_categories.at(min), m_categories.at(max));
473 }
470 } else {
474 else {
471 if (m_orientation == Qt::Vertical)
475 if(m_orientation==Qt::Vertical) {
476 domain->setRangeY(m_min, m_max);
472 domain->setRangeY(m_min, m_max);
477 }
473 else
478 else {
479 domain->setRangeX(m_min, m_max);
474 domain->setRangeX(m_min, m_max);
480 }
481 }
475 }
482 }
476 }
483
477
@@ -39,7 +39,7 public:
39 ~QBarCategoryAxis();
39 ~QBarCategoryAxis();
40
40
41 protected:
41 protected:
42 QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent = 0);
42 QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent = 0);
43
43
44 public:
44 public:
45 AxisType type() const;
45 AxisType type() const;
@@ -63,9 +63,9 public:
63
63
64 Q_SIGNALS:
64 Q_SIGNALS:
65 void categoriesChanged();
65 void categoriesChanged();
66 void minChanged(const QString &min);
66 void minChanged(const QString &min);
67 void maxChanged(const QString &max);
67 void maxChanged(const QString &max);
68 void rangeChanged(const QString &min, const QString &max);
68 void rangeChanged(const QString &min, const QString &max);
69
69
70 private:
70 private:
71 Q_DECLARE_PRIVATE(QBarCategoryAxis)
71 Q_DECLARE_PRIVATE(QBarCategoryAxis)
@@ -49,8 +49,8 public:
49 ChartAxis* createGraphics(ChartPresenter* presenter);
49 ChartAxis* createGraphics(ChartPresenter* presenter);
50 void intializeDomain(Domain* domain);
50 void intializeDomain(Domain* domain);
51 void handleDomainUpdated();
51 void handleDomainUpdated();
52 qreal min() { return m_min;}
52 qreal min() { return m_min; }
53 qreal max() { return m_max;}
53 qreal max() { return m_max; }
54 int count() const { return m_count; }
54 int count() const { return m_count; }
55
55
56 private:
56 private:
@@ -30,7 +30,8 static int label_padding = 5;
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
33 ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis, ChartPresenter *presenter)
34 : ChartAxis(axis, presenter)
34 {
35 {
35 }
36 }
36
37
@@ -69,7 +70,8 void ChartCategoryAxisX::updateGeometry()
69 {
70 {
70 const QVector<qreal>& layout = ChartAxis::layout();
71 const QVector<qreal>& layout = ChartAxis::layout();
71
72
72 if(layout.isEmpty()) return;
73 if (layout.isEmpty())
74 return;
73
75
74 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
76 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
75 QStringList ticksList = categoryAxis->categoriesLabels();
77 QStringList ticksList = categoryAxis->categoriesLabels();
@@ -80,9 +82,8 void ChartCategoryAxisX::updateGeometry()
80 QList<QGraphicsItem *> axis = m_arrow->childItems();
82 QList<QGraphicsItem *> axis = m_arrow->childItems();
81
83
82
84
83 for (int i = 0; i < labels.count(); i++) {
85 for (int i = 0; i < labels.count(); i++)
84 labels.at(i)->setVisible(false);
86 labels.at(i)->setVisible(false);
85 }
86
87
87 QRectF chartRect = presenter()->chartsGeometry();
88 QRectF chartRect = presenter()->chartsGeometry();
88 // axis base line
89 // axis base line
@@ -93,18 +94,16 void ChartCategoryAxisX::updateGeometry()
93 for (int i = 0; i < layout.size(); ++i) {
94 for (int i = 0; i < layout.size(); ++i) {
94
95
95 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
96 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
96 if (i < ticksList.count()) {
97 if (i < ticksList.count())
97 labelItem->setText(ticksList.at(i));
98 labelItem->setText(ticksList.at(i));
98 }
99 const QRectF& rect = labelItem->boundingRect();
99 const QRectF& rect = labelItem->boundingRect();
100 QPointF center = rect.center();
100 QPointF center = rect.center();
101 labelItem->setTransformOriginPoint(center.x(), center.y());
101 labelItem->setTransformOriginPoint(center.x(), center.y());
102
102
103 if (i < layout.size() - 1) {
103 if (i < layout.size() - 1)
104 labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), chartRect.bottom() + label_padding);
104 labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), chartRect.bottom() + label_padding);
105 } else {
105 else
106 labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding);
106 labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding);
107 }
108
107
109 // check if the label should be shown
108 // check if the label should be shown
110 if (labelItem->pos().x() + center.x() < chartRect.left() || labelItem->pos().x() + center.x() > chartRect.right())
109 if (labelItem->pos().x() + center.x() < chartRect.left() || labelItem->pos().x() + center.x() > chartRect.right())
@@ -114,7 +113,7 void ChartCategoryAxisX::updateGeometry()
114
113
115 if ((i + 1) % 2 && i > 1) {
114 if ((i + 1) % 2 && i > 1) {
116 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
115 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
117 rectItem->setRect(layout[i - 1],chartRect.top(),layout[i]-layout[i - 1],chartRect.height());
116 rectItem->setRect(layout[i - 1], chartRect.top(), layout[i] - layout[i - 1], chartRect.height());
118 }
117 }
119
118
120 // grid lines and axis line ticks
119 // grid lines and axis line ticks
@@ -122,7 +121,7 void ChartCategoryAxisX::updateGeometry()
122 lineItem->setPos(layout[i], chartRect.top());
121 lineItem->setPos(layout[i], chartRect.top());
123 lineItem->setLine(0, 0, 0, chartRect.height());
122 lineItem->setLine(0, 0, 0, chartRect.height());
124
123
125 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
124 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
126 tickLineItem->setPos(layout[i], chartRect.bottom());
125 tickLineItem->setPos(layout[i], chartRect.bottom());
127 tickLineItem->setLine(0, 0, 0, 5);
126 tickLineItem->setLine(0, 0, 0, 5);
128
127
@@ -134,9 +133,7 void ChartCategoryAxisX::updateGeometry()
134 lineItem->setVisible(true);
133 lineItem->setVisible(true);
135 tickLineItem->setVisible(true);
134 tickLineItem->setVisible(true);
136 }
135 }
137
138 }
136 }
139
140 }
137 }
141
138
142 void ChartCategoryAxisX::handleAxisUpdated()
139 void ChartCategoryAxisX::handleAxisUpdated()
@@ -153,31 +150,28 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint
153 QSizeF sh;
150 QSizeF sh;
154 QSizeF base = ChartAxis::sizeHint(which, constraint);
151 QSizeF base = ChartAxis::sizeHint(which, constraint);
155 QStringList ticksList ; //TODO:
152 QStringList ticksList ; //TODO:
156 qreal width=0;
153 qreal width = 0;
157 qreal height=0;
154 qreal height = 0;
158
155
159 switch (which) {
156 switch (which) {
160 case Qt::MinimumSize:
157 case Qt::MinimumSize:
161 width = fn.boundingRect("...").width();
158 width = fn.boundingRect("...").width();
162 height = fn.height() + label_padding;
159 height = fn.height() + label_padding;
163 width=qMax(width,base.width());
160 width = qMax(width, base.width());
164 height+=base.height();
161 height += base.height();
165 sh = QSizeF(width,height);
162 sh = QSizeF(width, height);
166 break;
163 break;
167 case Qt::PreferredSize: {
164 case Qt::PreferredSize:
168
165 for (int i = 0; i < ticksList.size(); ++i) {
169 for (int i = 0; i < ticksList.size(); ++i)
166 QRectF rect = fn.boundingRect(ticksList.at(i));
170 {
167 width += rect.width();
171 QRectF rect = fn.boundingRect(ticksList.at(i));
168 height = qMax(rect.height() + label_padding, height);
172 width+=rect.width();
173 height=qMax(rect.height()+label_padding,height);
174 }
175 width=qMax(width,base.width());
176 height+=base.height();
177 sh = QSizeF(width,height);
178 break;
179 }
169 }
180 default:
170 width = qMax(width, base.width());
171 height += base.height();
172 sh = QSizeF(width, height);
173 break;
174 default:
181 break;
175 break;
182 }
176 }
183
177
@@ -43,7 +43,7 public:
43 ChartCategoryAxisX(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartCategoryAxisX(QAbstractAxis *axis, ChartPresenter *presenter);
44 ~ChartCategoryAxisX();
44 ~ChartCategoryAxisX();
45
45
46 AxisType axisType() const { return X_AXIS;}
46 AxisType axisType() const { return X_AXIS; }
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
48 protected:
48 protected:
49 QVector<qreal> calculateLayout() const;
49 QVector<qreal> calculateLayout() const;
@@ -30,7 +30,8 static int label_padding = 5;
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
33 ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis, ChartPresenter *presenter)
34 : ChartAxis(axis, presenter)
34 {
35 {
35 }
36 }
36
37
@@ -53,7 +54,7 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
53 if (range > 0) {
54 if (range > 0) {
54 points.resize(tickCount);
55 points.resize(tickCount);
55 qreal scale = rect.height() / range;
56 qreal scale = rect.height() / range;
56 for (int i = 0; i < tickCount; ++i)
57 for (int i = 0; i < tickCount; ++i) {
57 if (i < tickCount - 1) {
58 if (i < tickCount - 1) {
58 int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + rect.bottom();
59 int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + rect.bottom();
59 points[i] = y;
60 points[i] = y;
@@ -61,6 +62,7 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
61 int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + rect.bottom();
62 int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + rect.bottom();
62 points[i] = y;
63 points[i] = y;
63 }
64 }
65 }
64 }
66 }
65
67
66 return points;
68 return points;
@@ -70,9 +72,8 void ChartCategoryAxisY::updateGeometry()
70 {
72 {
71 const QVector<qreal> &layout = ChartAxis::layout();
73 const QVector<qreal> &layout = ChartAxis::layout();
72
74
73 if(layout.isEmpty()) {
75 if (layout.isEmpty())
74 return;
76 return;
75 }
76
77
77 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
78 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
78 QStringList ticksList = categoryAxis->categoriesLabels();
79 QStringList ticksList = categoryAxis->categoriesLabels();
@@ -108,7 +109,7 void ChartCategoryAxisY::updateGeometry()
108 if (i < layout.size() - 1)
109 if (i < layout.size() - 1)
109 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
110 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
110 else
111 else
111 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
112 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] - center.y());
112
113
113 // check if the label should be shown
114 // check if the label should be shown
114 if (labelItem->pos().y() + center.y() < chartRect.top() || labelItem->pos().y() + center.y() > chartRect.bottom())
115 if (labelItem->pos().y() + center.y() < chartRect.top() || labelItem->pos().y() + center.y() > chartRect.bottom())
@@ -116,9 +117,9 void ChartCategoryAxisY::updateGeometry()
116 else
117 else
117 labelItem->setVisible(true);
118 labelItem->setVisible(true);
118
119
119 if ((i+1)%2 && i>1) {
120 if ((i + 1) % 2 && i > 1) {
120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
121 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
121 rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]);
122 rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]);
122 }
123 }
123
124
124 // grid lines and axis line ticks
125 // grid lines and axis line ticks
@@ -126,7 +127,7 void ChartCategoryAxisY::updateGeometry()
126 lineItem->setPos(chartRect.left(), layout[i]);
127 lineItem->setPos(chartRect.left(), layout[i]);
127 lineItem->setLine(0, 0, chartRect.width(), 0);
128 lineItem->setLine(0, 0, chartRect.width(), 0);
128
129
129 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
130 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
130 tickLineItem->setPos(chartRect.left(), layout[i]);
131 tickLineItem->setPos(chartRect.left(), layout[i]);
131 tickLineItem->setLine(-5, 0, 0, 0);
132 tickLineItem->setLine(-5, 0, 0, 0);
132
133
@@ -138,9 +139,7 void ChartCategoryAxisY::updateGeometry()
138 lineItem->setVisible(true);
139 lineItem->setVisible(true);
139 tickLineItem->setVisible(true);
140 tickLineItem->setVisible(true);
140 }
141 }
141
142 }
142 }
143
144 }
143 }
145
144
146 void ChartCategoryAxisY::handleAxisUpdated()
145 void ChartCategoryAxisY::handleAxisUpdated()
@@ -157,35 +156,32 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint
157 QSizeF sh;
156 QSizeF sh;
158 QSizeF base = ChartAxis::sizeHint(which, constraint);
157 QSizeF base = ChartAxis::sizeHint(which, constraint);
159 QStringList ticksList; //TODO::
158 QStringList ticksList; //TODO::
160 qreal width=0;
159 qreal width = 0;
161 qreal height=0;
160 qreal height = 0;
162
161
163 switch (which) {
162 switch (which) {
164 case Qt::MinimumSize:
163 case Qt::MinimumSize:
165 width = fn.boundingRect("...").width()+label_padding;
164 width = fn.boundingRect("...").width() + label_padding;
166 height = fn.height();
165 height = fn.height();
167 width=qMax(width,base.width());
166 width = qMax(width, base.width());
168 height+=base.height();
167 height += base.height();
169 sh = QSizeF(width,height);
168 sh = QSizeF(width, height);
170 break;
169 break;
171 case Qt::PreferredSize:{
170 case Qt::PreferredSize:
172
171 for (int i = 0; i < ticksList.size(); ++i) {
173 for (int i = 0; i < ticksList.size(); ++i)
172 QRectF rect = fn.boundingRect(ticksList.at(i));
174 {
173 height += rect.height();
175 QRectF rect = fn.boundingRect(ticksList.at(i));
174 width = qMax(rect.width() + label_padding, width);
176 height+=rect.height();
177 width=qMax(rect.width()+label_padding,width);
178 }
179 height=qMax(height,base.height());
180 width+=base.width();
181 sh = QSizeF(width,height);
182 break;
183 }
175 }
184 default:
176 height = qMax(height, base.height());
185 break;
177 width += base.width();
186 }
178 sh = QSizeF(width, height);
179 break;
180 default:
181 break;
182 }
187
183
188 return sh;
184 return sh;
189 }
185 }
190
186
191 QTCOMMERCIALCHART_END_NAMESPACE
187 QTCOMMERCIALCHART_END_NAMESPACE
@@ -43,13 +43,13 public:
43 ChartCategoryAxisY(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartCategoryAxisY(QAbstractAxis *axis, ChartPresenter *presenter);
44 ~ChartCategoryAxisY();
44 ~ChartCategoryAxisY();
45
45
46 AxisType axisType() const { return Y_AXIS;}
46 AxisType axisType() const { return Y_AXIS; }
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
48 protected:
48 protected:
49 QVector<qreal> calculateLayout() const;
49 QVector<qreal> calculateLayout() const;
50 void updateGeometry();
50 void updateGeometry();
51
51
52 Q_SLOTS
52 public Q_SLOTS:
53 void handleAxisUpdated();
53 void handleAxisUpdated();
54 };
54 };
55
55
@@ -78,7 +78,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
78 Constructs an axis object which is a child of \a parent.
78 Constructs an axis object which is a child of \a parent.
79 */
79 */
80 QCategoryAxis::QCategoryAxis(QObject *parent):
80 QCategoryAxis::QCategoryAxis(QObject *parent):
81 QValueAxis(*new QCategoryAxisPrivate(this),parent)
81 QValueAxis(*new QCategoryAxisPrivate(this), parent)
82 {
82 {
83 }
83 }
84
84
@@ -92,7 +92,7 QCategoryAxis::~QCategoryAxis()
92 /*!
92 /*!
93 \internal
93 \internal
94 */
94 */
95 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent):QValueAxis(d,parent)
95 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d, QObject *parent): QValueAxis(d, parent)
96 {
96 {
97
97
98 }
98 }
@@ -115,13 +115,12 void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue)
115 {
115 {
116 Q_D(QCategoryAxis);
116 Q_D(QCategoryAxis);
117
117
118 if (!d->m_categories.contains(categoryLabel))
118 if (!d->m_categories.contains(categoryLabel)) {
119 {
119 if (d->m_categories.isEmpty()) {
120 if(d->m_categories.isEmpty()){
121 Range range(d->m_categoryMinimum, categoryEndValue);
120 Range range(d->m_categoryMinimum, categoryEndValue);
122 d->m_categoriesMap.insert(categoryLabel, range);
121 d->m_categoriesMap.insert(categoryLabel, range);
123 d->m_categories.append(categoryLabel);
122 d->m_categories.append(categoryLabel);
124 }else if (categoryEndValue > endValue(d->m_categories.last())){
123 } else if (categoryEndValue > endValue(d->m_categories.last())) {
125 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
124 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
126 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
125 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
127 d->m_categories.append(categoryLabel);
126 d->m_categories.append(categoryLabel);
@@ -137,9 +136,9 void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue)
137 void QCategoryAxis::setStartValue(qreal min)
136 void QCategoryAxis::setStartValue(qreal min)
138 {
137 {
139 Q_D(QCategoryAxis);
138 Q_D(QCategoryAxis);
140 if(d->m_categories.isEmpty()){
139 if (d->m_categories.isEmpty()) {
141 d->m_categoryMinimum = min;
140 d->m_categoryMinimum = min;
142 }else{
141 } else {
143 Range range = d->m_categoriesMap.value(d->m_categories.first());
142 Range range = d->m_categoriesMap.value(d->m_categories.first());
144 if (min < range.second)
143 if (min < range.second)
145 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
144 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
@@ -154,8 +153,7 qreal QCategoryAxis::startValue(const QString& categoryLabel) const
154 Q_D(const QCategoryAxis);
153 Q_D(const QCategoryAxis);
155 if (categoryLabel.isEmpty())
154 if (categoryLabel.isEmpty())
156 return d->m_categoryMinimum;
155 return d->m_categoryMinimum;
157 else
156 return d->m_categoriesMap.value(categoryLabel).first;
158 return d->m_categoriesMap.value(categoryLabel).first;
159 }
157 }
160
158
161 /*!
159 /*!
@@ -224,7 +222,6 void QCategoryAxis::replaceLabel(const QString& oldLabel, const QString& newLabe
224 d->m_categoriesMap.insert(newLabel, range);
222 d->m_categoriesMap.insert(newLabel, range);
225 d->emitUpdated();
223 d->emitUpdated();
226 }
224 }
227
228 }
225 }
229
226
230 /*!
227 /*!
@@ -272,7 +269,7 int QCategoryAxisPrivate::ticksCount() const
272 return m_categories.count() + 1;
269 return m_categories.count() + 1;
273 }
270 }
274
271
275 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
272 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max, int count)
276 {
273 {
277 Q_UNUSED(count);
274 Q_UNUSED(count);
278 Q_UNUSED(min);
275 Q_UNUSED(min);
@@ -282,11 +279,9 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count
282 ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
279 ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
283 {
280 {
284 Q_Q(QCategoryAxis);
281 Q_Q(QCategoryAxis);
285 if(m_orientation == Qt::Vertical){
282 if (m_orientation == Qt::Vertical)
286 return new ChartCategoryAxisY(q,presenter);
283 return new ChartCategoryAxisY(q, presenter);
287 }else{
284 return new ChartCategoryAxisX(q, presenter);
288 return new ChartCategoryAxisX(q,presenter);
289 }
290 }
285 }
291
286
292 #include "moc_qcategoryaxis.cpp"
287 #include "moc_qcategoryaxis.cpp"
@@ -38,7 +38,7 public:
38 ~QCategoryAxis();
38 ~QCategoryAxis();
39
39
40 protected:
40 protected:
41 QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent = 0);
41 QCategoryAxis(QCategoryAxisPrivate &d, QObject *parent = 0);
42
42
43 public:
43 public:
44 AxisType type() const;
44 AxisType type() const;
@@ -49,10 +49,10 public:
49 int ticksCount() const;
49 int ticksCount() const;
50
50
51 Q_SIGNALS:
51 Q_SIGNALS:
52 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
52 void changed(qreal min, qreal max, int tickCount, bool niceNumbers);
53
53
54 public Q_SLOTS:
54 public Q_SLOTS:
55 void handleAxisRangeChanged(qreal min, qreal max,int count);
55 void handleAxisRangeChanged(qreal min, qreal max, int count);
56
56
57 private:
57 private:
58 QMap<QString , Range> m_categoriesMap;
58 QMap<QString , Range> m_categoriesMap;
@@ -31,17 +31,18
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
34 ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter)
35 m_chartAxis(axis),
35 : ChartElement(presenter),
36 m_labelsAngle(0),
36 m_chartAxis(axis),
37 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
37 m_labelsAngle(0),
38 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
38 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
39 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
39 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
40 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
40 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
41 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
41 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
42 m_min(0),
42 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
43 m_max(0),
43 m_min(0),
44 m_animation(0)
44 m_max(0),
45 m_animation(0)
45 {
46 {
46 //initial initialization
47 //initial initialization
47 m_arrow->setZValue(ChartPresenter::AxisZValue);
48 m_arrow->setZValue(ChartPresenter::AxisZValue);
@@ -50,7 +51,7 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartEleme
50 m_shades->setZValue(ChartPresenter::ShadesZValue);
51 m_shades->setZValue(ChartPresenter::ShadesZValue);
51 m_grid->setZValue(ChartPresenter::GridZValue);
52 m_grid->setZValue(ChartPresenter::GridZValue);
52
53
53 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
54 QObject::connect(m_chartAxis->d_ptr.data(), SIGNAL(updated()), this, SLOT(handleAxisUpdated()));
54
55
55 QGraphicsSimpleTextItem item;
56 QGraphicsSimpleTextItem item;
56 m_font = item.font();
57 m_font = item.font();
@@ -63,23 +64,25 ChartAxis::~ChartAxis()
63
64
64 void ChartAxis::setAnimation(AxisAnimation* animation)
65 void ChartAxis::setAnimation(AxisAnimation* animation)
65 {
66 {
66 m_animation=animation;
67 m_animation = animation;
67 }
68 }
68
69
69 void ChartAxis::setLayout(QVector<qreal> &layout)
70 void ChartAxis::setLayout(QVector<qreal> &layout)
70 {
71 {
71 m_layoutVector=layout;
72 m_layoutVector = layout;
72 }
73 }
73
74
74 void ChartAxis::createItems(int count)
75 void ChartAxis::createItems(int count)
75 {
76 {
76 if (m_arrow->children().size() == 0)
77 if (m_arrow->children().size() == 0)
77 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
78 m_arrow->addToGroup(new AxisItem(this, presenter()->rootItem()));
79
78 for (int i = 0; i < count; ++i) {
80 for (int i = 0; i < count; ++i) {
79 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
81 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
80 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
82 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
81 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
83 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
82 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
84 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2)
85 m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
83 }
86 }
84 }
87 }
85
88
@@ -91,7 +94,8 void ChartAxis::deleteItems(int count)
91 QList<QGraphicsItem *> axis = m_arrow->childItems();
94 QList<QGraphicsItem *> axis = m_arrow->childItems();
92
95
93 for (int i = 0; i < count; ++i) {
96 for (int i = 0; i < count; ++i) {
94 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
97 if (lines.size() % 2 && lines.size() > 1)
98 delete(shades.takeLast());
95 delete(lines.takeLast());
99 delete(lines.takeLast());
96 delete(labels.takeLast());
100 delete(labels.takeLast());
97 delete(axis.takeLast());
101 delete(axis.takeLast());
@@ -102,41 +106,39 void ChartAxis::updateLayout(QVector<qreal> &layout)
102 {
106 {
103 int diff = m_layoutVector.size() - layout.size();
107 int diff = m_layoutVector.size() - layout.size();
104
108
105 if (diff>0) {
109 if (diff > 0)
106 deleteItems(diff);
110 deleteItems(diff);
107 }
111 else if (diff < 0)
108 else if (diff<0) {
109 createItems(-diff);
112 createItems(-diff);
110 }
111
113
112 if(diff<0) handleAxisUpdated();
114 if (diff < 0)
115 handleAxisUpdated();
113
116
114 if (m_animation) {
117 if (m_animation) {
115 switch(presenter()->state()){
118 switch (presenter()->state()) {
116 case ChartPresenter::ZoomInState:
119 case ChartPresenter::ZoomInState:
117 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
120 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
118 m_animation->setAnimationPoint(presenter()->statePoint());
121 m_animation->setAnimationPoint(presenter()->statePoint());
119 break;
122 break;
120 case ChartPresenter::ZoomOutState:
123 case ChartPresenter::ZoomOutState:
121 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
124 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
122 m_animation->setAnimationPoint(presenter()->statePoint());
125 m_animation->setAnimationPoint(presenter()->statePoint());
123 break;
126 break;
124 case ChartPresenter::ScrollUpState:
127 case ChartPresenter::ScrollUpState:
125 case ChartPresenter::ScrollLeftState:
128 case ChartPresenter::ScrollLeftState:
126 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
129 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
127 break;
130 break;
128 case ChartPresenter::ScrollDownState:
131 case ChartPresenter::ScrollDownState:
129 case ChartPresenter::ScrollRightState:
132 case ChartPresenter::ScrollRightState:
130 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
133 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
131 break;
134 break;
132 case ChartPresenter::ShowState:
135 case ChartPresenter::ShowState:
133 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
136 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
134 break;
137 break;
135 }
138 }
136 m_animation->setValues(m_layoutVector,layout);
139 m_animation->setValues(m_layoutVector, layout);
137 presenter()->startAnimation(m_animation);
140 presenter()->startAnimation(m_animation);
138 }
141 } else {
139 else {
140 setLayout(layout);
142 setLayout(layout);
141 updateGeometry();
143 updateGeometry();
142 }
144 }
@@ -204,74 +206,67 void ChartAxis::setShadesVisibility(bool visible)
204
206
205 void ChartAxis::setLabelsAngle(int angle)
207 void ChartAxis::setLabelsAngle(int angle)
206 {
208 {
207 foreach(QGraphicsItem* item , m_labels->childItems()) {
209 foreach (QGraphicsItem *item , m_labels->childItems())
208 item->setRotation(angle);
210 item->setRotation(angle);
209 }
210
211
211 m_labelsAngle=angle;
212 m_labelsAngle = angle;
212 }
213 }
213
214
214 void ChartAxis::setLabelsPen(const QPen &pen)
215 void ChartAxis::setLabelsPen(const QPen &pen)
215 {
216 {
216 foreach(QGraphicsItem* item , m_labels->childItems()) {
217 foreach (QGraphicsItem *item , m_labels->childItems())
217 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
218 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
218 }
219 }
219 }
220
220
221 void ChartAxis::setLabelsBrush(const QBrush &brush)
221 void ChartAxis::setLabelsBrush(const QBrush &brush)
222 {
222 {
223 foreach(QGraphicsItem* item , m_labels->childItems()) {
223 foreach (QGraphicsItem *item , m_labels->childItems())
224 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
224 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
225 }
226 }
225 }
227
226
228 void ChartAxis::setLabelsFont(const QFont &font)
227 void ChartAxis::setLabelsFont(const QFont &font)
229 {
228 {
230 foreach(QGraphicsItem* item , m_labels->childItems()) {
229 foreach (QGraphicsItem *item , m_labels->childItems())
231 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
230 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
232 }
231
233 if(m_font!=font) {
232 if (m_font != font) {
234 m_font = font;
233 m_font = font;
235 foreach(QGraphicsItem* item , m_labels->childItems()) {
234 foreach (QGraphicsItem *item , m_labels->childItems())
236 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
235 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
237 }
238 QGraphicsLayoutItem::updateGeometry();
236 QGraphicsLayoutItem::updateGeometry();
239 presenter()->layout()->invalidate();
237 presenter()->layout()->invalidate();
240
241 }
238 }
242 }
239 }
243
240
244 void ChartAxis::setShadesBrush(const QBrush &brush)
241 void ChartAxis::setShadesBrush(const QBrush &brush)
245 {
242 {
246 foreach(QGraphicsItem* item , m_shades->childItems()) {
243 foreach (QGraphicsItem *item , m_shades->childItems())
247 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
244 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
248 }
249 }
245 }
250
246
251 void ChartAxis::setShadesPen(const QPen &pen)
247 void ChartAxis::setShadesPen(const QPen &pen)
252 {
248 {
253 foreach(QGraphicsItem* item , m_shades->childItems()) {
249 foreach (QGraphicsItem *item , m_shades->childItems())
254 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
250 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
255 }
256 }
251 }
257
252
258 void ChartAxis::setArrowPen(const QPen &pen)
253 void ChartAxis::setArrowPen(const QPen &pen)
259 {
254 {
260 foreach(QGraphicsItem* item , m_arrow->childItems()) {
255 foreach (QGraphicsItem *item , m_arrow->childItems())
261 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
256 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
262 }
263 }
257 }
264
258
265 void ChartAxis::setGridPen(const QPen &pen)
259 void ChartAxis::setGridPen(const QPen &pen)
266 {
260 {
267 foreach(QGraphicsItem* item , m_grid->childItems()) {
261 foreach (QGraphicsItem *item , m_grid->childItems())
268 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
262 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
269 }
270 }
263 }
271
264
272 bool ChartAxis::isEmpty()
265 bool ChartAxis::isEmpty()
273 {
266 {
274 return !m_rect.isValid() || presenter()->chartsGeometry().isEmpty() || qFuzzyIsNull(m_min - m_max);
267 return !m_rect.isValid() ||
268 presenter()->chartsGeometry().isEmpty() ||
269 qFuzzyIsNull(m_min - m_max);
275 }
270 }
276
271
277 void ChartAxis::handleDomainUpdated()
272 void ChartAxis::handleDomainUpdated()
@@ -280,18 +275,15 void ChartAxis::handleDomainUpdated()
280 qreal min(0);
275 qreal min(0);
281 qreal max(0);
276 qreal max(0);
282
277
283 if(m_chartAxis->orientation()==Qt::Horizontal) {
278 if (m_chartAxis->orientation() == Qt::Horizontal) {
284 min = domain->minX();
279 min = domain->minX();
285 max = domain->maxX();
280 max = domain->maxX();
286 }
281 } else if (m_chartAxis->orientation() == Qt::Vertical) {
287 else if (m_chartAxis->orientation()==Qt::Vertical)
288 {
289 min = domain->minY();
282 min = domain->minY();
290 max = domain->maxY();
283 max = domain->maxY();
291 }
284 }
292
285
293 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
286 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max)) {
294 {
295 m_min = min;
287 m_min = min;
296 m_max = max;
288 m_max = max;
297
289
@@ -301,9 +293,9 void ChartAxis::handleDomainUpdated()
301 updateLayout(layout);
293 updateLayout(layout);
302 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
294 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
303
295
304 QSizeF after= sizeHint(Qt::MinimumSize);
296 QSizeF after = sizeHint(Qt::MinimumSize);
305
297
306 if(before!=after) {
298 if (before != after) {
307 QGraphicsLayoutItem::updateGeometry();
299 QGraphicsLayoutItem::updateGeometry();
308 //we don't want to call invalidate on layout, since it will change minimum size of component,
300 //we don't want to call invalidate on layout, since it will change minimum size of component,
309 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
301 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
@@ -316,8 +308,8 void ChartAxis::handleDomainUpdated()
316
308
317 void ChartAxis::handleAxisUpdated()
309 void ChartAxis::handleAxisUpdated()
318 {
310 {
319 if(isEmpty()) return;
311 if (isEmpty())
320
312 return;
321
313
322 bool visible = m_chartAxis->isVisible();
314 bool visible = m_chartAxis->isVisible();
323
315
@@ -338,7 +330,7 void ChartAxis::handleAxisUpdated()
338
330
339 void ChartAxis::setTitleText(const QString& title)
331 void ChartAxis::setTitleText(const QString& title)
340 {
332 {
341 if(m_titleText!=title) {
333 if (m_titleText != title) {
342 m_titleText = title;
334 m_titleText = title;
343 m_rect = QRect();
335 m_rect = QRect();
344 QGraphicsLayoutItem::updateGeometry();
336 QGraphicsLayoutItem::updateGeometry();
@@ -356,47 +348,44 void ChartAxis::hide()
356
348
357 void ChartAxis::setGeometry(const QRectF &rect)
349 void ChartAxis::setGeometry(const QRectF &rect)
358 {
350 {
351 m_rect = rect;
359
352
360 m_rect = rect;
353 if (isEmpty())
354 return;
361
355
362 if (isEmpty()) return;
356 if (!m_titleText.isNull()) {
357 QFontMetrics fn(m_title->font());
363
358
364 if(!m_titleText.isNull()) {
359 int size(0);
365 QFontMetrics fn(m_title->font());
366
360
367 int size(0);
361 QRectF chartRect = presenter()->chartsGeometry();
368
362
369 QRectF chartRect = presenter()->chartsGeometry();
363 if (orientation() == Qt::Horizontal)
370
371 if(orientation()==Qt::Horizontal)
372 size = chartRect.width();
364 size = chartRect.width();
373 else if(orientation()==Qt::Vertical)
365 else if (orientation() == Qt::Vertical)
374 size = chartRect.height();
366 size = chartRect.height();
375
367
376 if (fn.boundingRect(m_titleText).width() > size)
368 if (fn.boundingRect(m_titleText).width() > size) {
377 {
369 QString string = m_titleText + "...";
378 QString string = m_titleText + "...";
370 while (fn.boundingRect(string).width() > size && string.length() > 3)
379 while (fn.boundingRect(string).width() > size && string.length() > 3)
380 string.remove(string.length() - 4, 1);
371 string.remove(string.length() - 4, 1);
381 m_title->setText(string);
372 m_title->setText(string);
382 }
373 } else {
383 else
384 m_title->setText(m_titleText);
374 m_title->setText(m_titleText);
385
386 QPointF center = chartRect.center() - m_title->boundingRect().center();
387 if(orientation()==Qt::Horizontal) {
388 m_title->setPos(center.x(),m_rect.bottom()-m_title->boundingRect().height());
389 }
390 else if(orientation()==Qt::Vertical) {
391 m_title->setTransformOriginPoint(m_title->boundingRect().center());
392 m_title->setRotation(270);
393 m_title->setPos(m_rect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y());
394 }
395 }
375 }
396
376
397 QVector<qreal> layout = calculateLayout();
377 QPointF center = chartRect.center() - m_title->boundingRect().center();
398 updateLayout(layout);
378 if (orientation() == Qt::Horizontal) {
379 m_title->setPos(center.x(), m_rect.bottom() - m_title->boundingRect().height());
380 } else if (orientation() == Qt::Vertical) {
381 m_title->setTransformOriginPoint(m_title->boundingRect().center());
382 m_title->setRotation(270);
383 m_title->setPos(m_rect.left() - m_title->boundingRect().width() / 2 + m_title->boundingRect().height() / 2, center.y());
384 }
385 }
399
386
387 QVector<qreal> layout = calculateLayout();
388 updateLayout(layout);
400 }
389 }
401
390
402 void ChartAxis::setInternalRect(const QRectF &size)
391 void ChartAxis::setInternalRect(const QRectF &size)
@@ -412,28 +401,27 void ChartAxis::axisSelected()
412
401
413 QStringList ChartAxis::createNumberLabels(qreal min, qreal max, int ticks) const
402 QStringList ChartAxis::createNumberLabels(qreal min, qreal max, int ticks) const
414 {
403 {
415 Q_ASSERT(max>min);
404 Q_ASSERT(max > min);
416 Q_ASSERT(ticks>1);
405 Q_ASSERT(ticks > 1);
417
406
418 QStringList labels;
407 QStringList labels;
419
408
420 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
409 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
421 n++;
410 n++;
422
411
423 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
412 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
424
413
425 QString format = axis->labelFormat();
414 QString format = axis->labelFormat();
426
415
427 if(format.isNull()) {
416 if (format.isNull()) {
428 for (int i=0; i< ticks; i++) {
417 for (int i = 0; i < ticks; i++) {
429 qreal value = min + (i * (max - min)/ (ticks-1));
418 qreal value = min + (i * (max - min) / (ticks - 1));
430 labels << QString::number(value,'f',n);
419 labels << QString::number(value, 'f', n);
431 }
420 }
432 }
421 } else {
433 else {
434 QByteArray array = format.toAscii();
422 QByteArray array = format.toAscii();
435 for (int i=0; i< ticks; i++) {
423 for (int i = 0; i < ticks; i++) {
436 qreal value = min + (i * (max - min)/ (ticks-1));
424 qreal value = min + (i * (max - min) / (ticks - 1));
437 labels << QString().sprintf(array, value);
425 labels << QString().sprintf(array, value);
438 }
426 }
439 }
427 }
@@ -463,29 +451,24 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
463 QFontMetrics fn(m_title->font());
451 QFontMetrics fn(m_title->font());
464 QSizeF sh;
452 QSizeF sh;
465
453
466 if(m_titleText.isNull()) return sh;
454 if (m_titleText.isNull())
467
455 return sh;
468 switch(which) {
469 case Qt::MinimumSize:
470 if(orientation()==Qt::Horizontal) {
471 sh = QSizeF(fn.boundingRect ("...").width(),fn.height());
472 }
473 else if(orientation()==Qt::Vertical) {
474 sh = QSizeF(fn.height(),fn.boundingRect ("...").width());
475 }
476
456
457 switch (which) {
458 case Qt::MinimumSize:
459 if (orientation() == Qt::Horizontal)
460 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
461 else if (orientation() == Qt::Vertical)
462 sh = QSizeF(fn.height(), fn.boundingRect("...").width());
477 break;
463 break;
478 case Qt::MaximumSize:
464 case Qt::MaximumSize:
479 case Qt::PreferredSize:
465 case Qt::PreferredSize:
480 if(orientation()==Qt::Horizontal) {
466 if (orientation() == Qt::Horizontal)
481 sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(),fn.height());
467 sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(), fn.height());
482 }
468 else if (orientation() == Qt::Vertical)
483 else if(orientation()==Qt::Vertical) {
469 sh = QSizeF(fn.height(), fn.boundingRect(m_chartAxis->title()).width());
484 sh = QSizeF(fn.height(),fn.boundingRect(m_chartAxis->title()).width());
485 }
486
487 break;
470 break;
488 default:
471 default:
489 break;
472 break;
490 }
473 }
491
474
@@ -47,7 +47,7 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
47 Q_OBJECT
47 Q_OBJECT
48 Q_INTERFACES(QGraphicsLayoutItem)
48 Q_INTERFACES(QGraphicsLayoutItem)
49 public:
49 public:
50 enum AxisType{ X_AXIS,Y_AXIS };
50 enum AxisType { X_AXIS, Y_AXIS };
51
51
52 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
52 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
53 ~ChartAxis();
53 ~ChartAxis();
@@ -111,7 +111,7 public:
111 protected:
111 protected:
112 virtual void updateGeometry() = 0;
112 virtual void updateGeometry() = 0;
113 virtual QVector<qreal> calculateLayout() const = 0;
113 virtual QVector<qreal> calculateLayout() const = 0;
114 QStringList createNumberLabels(qreal min, qreal max,int ticks) const;
114 QStringList createNumberLabels(qreal min, qreal max, int ticks) const;
115
115
116
116
117 public Q_SLOTS:
117 public Q_SLOTS:
@@ -156,28 +156,24 public:
156 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
156 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
157
157
158 protected:
158 protected:
159 void mousePressEvent(QGraphicsSceneMouseEvent *event)
159 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
160 {
160 Q_UNUSED(event)
161 Q_UNUSED(event)
161 m_axis->axisSelected();
162 m_axis->axisSelected();
162 }
163 }
163
164
164 QRectF boundingRect() const {
165 QRectF boundingRect() const
165 return shape().boundingRect();
166 {
166 }
167 return shape().boundingRect();
167
168 }
168 QPainterPath shape() const {
169
169 QPainterPath path = QGraphicsLineItem::shape();
170 QPainterPath shape() const
170 QRectF rect = path.boundingRect();
171 {
171 path.addRect(rect.adjusted(0, 0, m_axis->axisType() != ChartAxis::X_AXIS ? 8 : 0, m_axis->axisType() != ChartAxis::Y_AXIS ? 8 : 0));
172 QPainterPath path = QGraphicsLineItem::shape();
172 return path;
173 QRectF rect = path.boundingRect();
173 }
174 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
175 return path;
176 }
177
174
178 private:
175 private:
179 ChartAxis* m_axis;
176 ChartAxis* m_axis;
180
181 };
177 };
182
178
183 QTCOMMERCIALCHART_END_NAMESPACE
179 QTCOMMERCIALCHART_END_NAMESPACE
@@ -31,8 +31,9 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartDateTimeAxisX::ChartDateTimeAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartDateTimeAxisX::ChartDateTimeAxisX(QAbstractAxis *axis, ChartPresenter *presenter)
35 m_tickCount(0)
35 : ChartAxis(axis, presenter),
36 m_tickCount(0)
36 {
37 {
37 }
38 }
38
39
@@ -40,29 +41,29 ChartDateTimeAxisX::~ChartDateTimeAxisX()
40 {
41 {
41 }
42 }
42
43
43 void ChartDateTimeAxisX::createLabels(QStringList &labels,qreal min, qreal max, int ticks)
44 void ChartDateTimeAxisX::createLabels(QStringList &labels, qreal min, qreal max, int ticks)
44 {
45 {
45 Q_ASSERT(max>min);
46 Q_ASSERT(max > min);
46 Q_ASSERT(ticks>1);
47 Q_ASSERT(ticks > 1);
47
48
48 QDateTimeAxis *axis = qobject_cast<QDateTimeAxis *>(m_chartAxis);
49 QDateTimeAxis *axis = qobject_cast<QDateTimeAxis *>(m_chartAxis);
49
50
50 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
51 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
51 n++;
52 n++;
52 for (int i=0; i< ticks; i++) {
53 for (int i = 0; i < ticks; i++) {
53 qreal value = min + (i * (max - min)/ (ticks-1));
54 qreal value = min + (i * (max - min) / (ticks - 1));
54 labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format());
55 labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format());
55 }
56 }
56 }
57 }
57
58
58 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
59 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
59 {
60 {
60 Q_ASSERT(m_tickCount>=2);
61 Q_ASSERT(m_tickCount >= 2);
61
62
62 QVector<qreal> points;
63 QVector<qreal> points;
63 points.resize(m_tickCount);
64 points.resize(m_tickCount);
64 QRectF rect = presenter()->chartsGeometry();
65 QRectF rect = presenter()->chartsGeometry();
65 const qreal deltaX = rect.width()/(m_tickCount-1);
66 const qreal deltaX = rect.width() / (m_tickCount - 1);
66 for (int i = 0; i < m_tickCount; ++i) {
67 for (int i = 0; i < m_tickCount; ++i) {
67 int x = i * deltaX + rect.left();
68 int x = i * deltaX + rect.left();
68 points[i] = x;
69 points[i] = x;
@@ -74,11 +75,12 void ChartDateTimeAxisX::updateGeometry()
74 {
75 {
75 const QVector<qreal>& layout = ChartAxis::layout();
76 const QVector<qreal>& layout = ChartAxis::layout();
76
77
77 if(layout.isEmpty()) return;
78 if (layout.isEmpty())
79 return;
78
80
79 QStringList ticksList;
81 QStringList ticksList;
80
82
81 createLabels(ticksList,m_min,m_max,layout.size());
83 createLabels(ticksList, m_min, m_max, layout.size());
82
84
83 QList<QGraphicsItem *> lines = m_grid->childItems();
85 QList<QGraphicsItem *> lines = m_grid->childItems();
84 QList<QGraphicsItem *> labels = m_labels->childItems();
86 QList<QGraphicsItem *> labels = m_labels->childItems();
@@ -98,27 +100,27 void ChartDateTimeAxisX::updateGeometry()
98 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
100 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
99 lineItem->setLine(layout[i], chartRect.top(), layout[i], chartRect.bottom());
101 lineItem->setLine(layout[i], chartRect.top(), layout[i], chartRect.bottom());
100 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
102 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
101 labelItem->setText(ticksList.at(i));
103 labelItem->setText(ticksList.at(i));
102 const QRectF& rect = labelItem->boundingRect();
104 const QRectF& rect = labelItem->boundingRect();
103 QPointF center = rect.center();
105 QPointF center = rect.center();
104 labelItem->setTransformOriginPoint(center.x(), center.y());
106 labelItem->setTransformOriginPoint(center.x(), center.y());
105 labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding);
107 labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding);
106
108
107 if(labelItem->pos().x()<=width){
109 if (labelItem->pos().x() <= width) {
108 labelItem->setVisible(false);
110 labelItem->setVisible(false);
109 lineItem->setVisible(false);
111 lineItem->setVisible(false);
110 }else{
112 } else {
111 labelItem->setVisible(true);
113 labelItem->setVisible(true);
112 lineItem->setVisible(true);
114 lineItem->setVisible(true);
113 width=rect.width()+labelItem->pos().x();
115 width = rect.width() + labelItem->pos().x();
114 }
115
116 if ((i+1)%2 && i>1) {
117 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
118 rectItem->setRect(layout[i-1],chartRect.top(),layout[i]-layout[i-1],chartRect.height());
119 }
116 }
120 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
117
121 lineItem->setLine(layout[i],chartRect.bottom(),layout[i],chartRect.bottom()+5);
118 if ((i + 1) % 2 && i > 1) {
119 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
120 rectItem->setRect(layout[i - 1], chartRect.top(), layout[i] - layout[i - 1], chartRect.height());
121 }
122 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
123 lineItem->setLine(layout[i], chartRect.bottom(), layout[i], chartRect.bottom() + 5);
122 }
124 }
123 }
125 }
124
126
@@ -137,34 +139,30 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint
137 QFontMetrics fn(m_font);
139 QFontMetrics fn(m_font);
138 QSizeF sh;
140 QSizeF sh;
139
141
140 switch (which) {
142 switch (which) {
141 case Qt::MinimumSize:
143 case Qt::MinimumSize:
142 sh = QSizeF(fn.boundingRect("...").width(),fn.height());
144 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
143 break;
145 break;
144 case Qt::PreferredSize:{
146 case Qt::PreferredSize: {
145
147 const QVector<qreal>& layout = ChartAxis::layout();
146 const QVector<qreal>& layout = ChartAxis::layout();
148 if (layout.isEmpty())
147 if(layout.isEmpty()) break;
148 QStringList ticksList;
149
150
151 qreal width=0;
152 qreal height=0;
153
154 for (int i = 0; i < ticksList.size(); ++i)
155 {
156 QRectF rect = fn.boundingRect(ticksList.at(i));
157 width+=rect.width();
158 height+=qMax(rect.height()+label_padding,height);
159 }
160 sh = QSizeF(width,height);
161 break;
149 break;
150 QStringList ticksList;
151 qreal width = 0;
152 qreal height = 0;
153 for (int i = 0; i < ticksList.size(); ++i) {
154 QRectF rect = fn.boundingRect(ticksList.at(i));
155 width += rect.width();
156 height += qMax(rect.height() + label_padding, height);
162 }
157 }
163 default:
158 sh = QSizeF(width, height);
164 break;
159 break;
165 }
160 }
161 default:
162 break;
163 }
166
164
167 return sh;
165 return sh;
168 }
166 }
169
167
170 QTCOMMERCIALCHART_END_NAMESPACE
168 QTCOMMERCIALCHART_END_NAMESPACE
@@ -46,14 +46,13 public:
46 AxisType axisType() const { return X_AXIS;}
46 AxisType axisType() const { return X_AXIS;}
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
48 protected:
48 protected:
49 void createLabels(QStringList &labels,qreal min, qreal max, int ticks);
49 void createLabels(QStringList &labels, qreal min, qreal max, int ticks);
50 void handleAxisUpdated();
50 void handleAxisUpdated();
51 QVector<qreal> calculateLayout() const;
51 QVector<qreal> calculateLayout() const;
52 void updateGeometry();
52 void updateGeometry();
53
53
54 private:
54 private:
55 int m_tickCount;
55 int m_tickCount;
56
57 };
56 };
58
57
59 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
@@ -31,8 +31,9 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartDateTimeAxisY::ChartDateTimeAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartDateTimeAxisY::ChartDateTimeAxisY(QAbstractAxis *axis, ChartPresenter *presenter)
35 m_tickCount(0)
35 : ChartAxis(axis, presenter),
36 m_tickCount(0)
36 {
37 {
37 }
38 }
38
39
@@ -40,29 +41,29 ChartDateTimeAxisY::~ChartDateTimeAxisY()
40 {
41 {
41 }
42 }
42
43
43 void ChartDateTimeAxisY::createLabels(QStringList &labels,qreal min, qreal max, int ticks)
44 void ChartDateTimeAxisY::createLabels(QStringList &labels, qreal min, qreal max, int ticks)
44 {
45 {
45 Q_ASSERT(max>min);
46 Q_ASSERT(max > min);
46 Q_ASSERT(ticks>1);
47 Q_ASSERT(ticks > 1);
47
48
48 QDateTimeAxis *axis = qobject_cast<QDateTimeAxis *>(m_chartAxis);
49 QDateTimeAxis *axis = qobject_cast<QDateTimeAxis *>(m_chartAxis);
49
50
50 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
51 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
51 n++;
52 n++;
52 for (int i=0; i< ticks; i++) {
53 for (int i = 0; i < ticks; i++) {
53 qreal value = min + (i * (max - min)/ (ticks-1));
54 qreal value = min + (i * (max - min) / (ticks - 1));
54 labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format());
55 labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format());
55 }
56 }
56 }
57 }
57
58
58 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
59 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
59 {
60 {
60 Q_ASSERT(m_tickCount>=2);
61 Q_ASSERT(m_tickCount >= 2);
61
62
62 QVector<qreal> points;
63 QVector<qreal> points;
63 points.resize(m_tickCount);
64 points.resize(m_tickCount);
64 QRectF rect = presenter()->chartsGeometry();
65 QRectF rect = presenter()->chartsGeometry();
65 const qreal deltaY = rect.height()/(m_tickCount-1);
66 const qreal deltaY = rect.height() / (m_tickCount - 1);
66 for (int i = 0; i < m_tickCount; ++i) {
67 for (int i = 0; i < m_tickCount; ++i) {
67 int y = i * -deltaY + rect.bottom();
68 int y = i * -deltaY + rect.bottom();
68 points[i] = y;
69 points[i] = y;
@@ -75,11 +76,12 void ChartDateTimeAxisY::updateGeometry()
75 {
76 {
76 const QVector<qreal> &layout = ChartAxis::layout();
77 const QVector<qreal> &layout = ChartAxis::layout();
77
78
78 if(layout.isEmpty()) return;
79 if (layout.isEmpty())
80 return;
79
81
80 QStringList ticksList;
82 QStringList ticksList;
81
83
82 createLabels(ticksList,m_min,m_max,layout.size());
84 createLabels(ticksList, m_min, m_max, layout.size());
83
85
84 QList<QGraphicsItem *> lines = m_grid->childItems();
86 QList<QGraphicsItem *> lines = m_grid->childItems();
85 QList<QGraphicsItem *> labels = m_labels->childItems();
87 QList<QGraphicsItem *> labels = m_labels->childItems();
@@ -101,29 +103,28 void ChartDateTimeAxisY::updateGeometry()
101 lineItem->setLine(chartRect.left() , layout[i], chartRect.right(), layout[i]);
103 lineItem->setLine(chartRect.left() , layout[i], chartRect.right(), layout[i]);
102 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
104 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
103
105
104 labelItem->setText(ticksList.at(i));
106 labelItem->setText(ticksList.at(i));
105 const QRectF& rect = labelItem->boundingRect();
107 const QRectF& rect = labelItem->boundingRect();
106
108
107 QPointF center = rect.center();
109 QPointF center = rect.center();
108 labelItem->setTransformOriginPoint(center.x(), center.y());
110 labelItem->setTransformOriginPoint(center.x(), center.y());
109 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
111 labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] - center.y());
110
112
111 if(labelItem->pos().y()+rect.height()>height) {
113 if (labelItem->pos().y() + rect.height() > height) {
112 labelItem->setVisible(false);
114 labelItem->setVisible(false);
113 lineItem->setVisible(false);
115 lineItem->setVisible(false);
114 }
116 } else {
115 else {
117 labelItem->setVisible(true);
116 labelItem->setVisible(true);
118 lineItem->setVisible(true);
117 lineItem->setVisible(true);
119 height = labelItem->pos().y();
118 height=labelItem->pos().y();
120 }
119 }
121
120
122 if ((i + 1) % 2 && i > 1) {
121 if ((i+1)%2 && i>1) {
123 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
122 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
124 rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]);
123 rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]);
124 }
125 }
125 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
126 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
126 lineItem->setLine(chartRect.left()-5,layout[i],chartRect.left(),layout[i]);
127 lineItem->setLine(chartRect.left() - 5, layout[i], chartRect.left(), layout[i]);
127 }
128 }
128 }
129 }
129
130
@@ -142,34 +143,30 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint
142 QFontMetrics fn(m_font);
143 QFontMetrics fn(m_font);
143 QSizeF sh;
144 QSizeF sh;
144
145
145 switch (which) {
146 switch (which) {
146 case Qt::MinimumSize:
147 case Qt::MinimumSize:
147 sh = QSizeF(fn.boundingRect("...").width(),fn.height());
148 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
148 break;
149 break;
149 case Qt::PreferredSize:{
150 case Qt::PreferredSize: {
150
151 const QVector<qreal>& layout = ChartAxis::layout();
151 const QVector<qreal>& layout = ChartAxis::layout();
152 if (layout.isEmpty())
152 if(layout.isEmpty()) break;
153 QStringList ticksList;
154
155
156 qreal width=0;
157 qreal height=0;
158
159 for (int i = 0; i < ticksList.size(); ++i)
160 {
161 QRectF rect = fn.boundingRect(ticksList.at(i));
162 width+=rect.width();
163 height+=qMax(rect.height()+label_padding,height);
164 }
165 sh = QSizeF(width,height);
166 break;
153 break;
154 QStringList ticksList;
155 qreal width = 0;
156 qreal height = 0;
157 for (int i = 0; i < ticksList.size(); ++i) {
158 QRectF rect = fn.boundingRect(ticksList.at(i));
159 width += rect.width();
160 height += qMax(rect.height() + label_padding, height);
167 }
161 }
168 default:
162 sh = QSizeF(width, height);
169 break;
163 break;
170 }
164 }
165 default:
166 break;
167 }
171
168
172 return sh;
169 return sh;
173 }
170 }
174
171
175 QTCOMMERCIALCHART_END_NAMESPACE
172 QTCOMMERCIALCHART_END_NAMESPACE
@@ -46,7 +46,7 public:
46 AxisType axisType() const { return Y_AXIS;}
46 AxisType axisType() const { return Y_AXIS;}
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
48 protected:
48 protected:
49 void createLabels(QStringList &labels,qreal min, qreal max, int ticks);
49 void createLabels(QStringList &labels, qreal min, qreal max, int ticks);
50 QVector<qreal> calculateLayout() const;
50 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
51 void updateGeometry();
52 void handleAxisUpdated();
52 void handleAxisUpdated();
@@ -59,7 +59,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
59
59
60 Adding the series to the chart and setting up the QDateTimeAxis.
60 Adding the series to the chart and setting up the QDateTimeAxis.
61 \code
61 \code
62 QChartView *chartView = new QChartView;
62 QChartView *chartView = new QChartView;
63 chartView->chart()->addSeries(series);
63 chartView->chart()->addSeries(series);
64
64
65 // ...
65 // ...
@@ -158,7 +158,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
158 Constructs an axis object which is a child of \a parent.
158 Constructs an axis object which is a child of \a parent.
159 */
159 */
160 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
160 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
161 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
161 QAbstractAxis(*new QDateTimeAxisPrivate(this), parent)
162 {
162 {
163
163
164 }
164 }
@@ -166,7 +166,7 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
166 /*!
166 /*!
167 \internal
167 \internal
168 */
168 */
169 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
169 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d, QObject *parent) : QAbstractAxis(d, parent)
170 {
170 {
171
171
172 }
172 }
@@ -229,7 +229,7 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
229 }
229 }
230
230
231 if (changed) {
231 if (changed) {
232 emit rangeChanged(d->m_min,d->m_max);
232 emit rangeChanged(d->m_min, d->m_max);
233 d->emitUpdated();
233 d->emitUpdated();
234 }
234 }
235 }
235 }
@@ -253,9 +253,9 QString QDateTimeAxis::format() const
253 Sets \a count for ticks on the axis.
253 Sets \a count for ticks on the axis.
254 */
254 */
255 void QDateTimeAxis::setTickCount(int count)
255 void QDateTimeAxis::setTickCount(int count)
256 {
256 {
257 Q_D(QDateTimeAxis);
257 Q_D(QDateTimeAxis);
258 if (d->m_tickCount != count && count >=2) {
258 if (d->m_tickCount != count && count >= 2) {
259 d->m_tickCount = count;
259 d->m_tickCount = count;
260 d->emitUpdated();
260 d->emitUpdated();
261 }
261 }
@@ -301,11 +301,10 void QDateTimeAxisPrivate::handleDomainUpdated()
301 Domain* domain = qobject_cast<Domain*>(sender());
301 Domain* domain = qobject_cast<Domain*>(sender());
302 Q_ASSERT(domain);
302 Q_ASSERT(domain);
303
303
304 if(orientation()==Qt::Horizontal){
304 if (orientation() == Qt::Horizontal)
305 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
305 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
306 }else if(orientation()==Qt::Vertical){
306 else if (orientation() == Qt::Vertical)
307 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
307 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
308 }
309 }
308 }
310
309
311
310
@@ -334,29 +333,24 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
334 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
333 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
335 {
334 {
336 Q_Q(QDateTimeAxis);
335 Q_Q(QDateTimeAxis);
337 if(m_orientation == Qt::Vertical){
336 if (m_orientation == Qt::Vertical)
338 return new ChartDateTimeAxisY(q,presenter);
337 return new ChartDateTimeAxisY(q, presenter);
339 }else{
338 return new ChartDateTimeAxisX(q, presenter);
340 return new ChartDateTimeAxisX(q,presenter);
341 }
342
343 }
339 }
344
340
345 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
341 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
346 {
342 {
347 Q_Q(QDateTimeAxis);
343 Q_Q(QDateTimeAxis);
348 if(m_max == m_min) {
344 if (m_max == m_min) {
349 if(m_orientation==Qt::Vertical){
345 if (m_orientation == Qt::Vertical)
350 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
346 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
351 }else{
347 else
352 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
348 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
353 }
354 } else {
349 } else {
355 if(m_orientation==Qt::Vertical){
350 if (m_orientation == Qt::Vertical)
356 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
351 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
357 }else{
352 else
358 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
353 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
359 }
360 }
354 }
361 }
355 }
362
356
@@ -42,7 +42,7 public:
42 ~QDateTimeAxis();
42 ~QDateTimeAxis();
43
43
44 protected:
44 protected:
45 QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent = 0);
45 QDateTimeAxis(QDateTimeAxisPrivate &d, QObject *parent = 0);
46
46
47 public:
47 public:
48 AxisType type() const;
48 AxisType type() const;
@@ -39,11 +39,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 class QDateTimeAxisPrivate : public QAbstractAxisPrivate
39 class QDateTimeAxisPrivate : public QAbstractAxisPrivate
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42 public:
42 public:
43 QDateTimeAxisPrivate(QDateTimeAxis *q);
43 QDateTimeAxisPrivate(QDateTimeAxis *q);
44 ~QDateTimeAxisPrivate();
44 ~QDateTimeAxisPrivate();
45
45
46 public:
46 public:
47 ChartAxis* createGraphics(ChartPresenter* presenter);
47 ChartAxis* createGraphics(ChartPresenter* presenter);
48 void intializeDomain(Domain* domain);
48 void intializeDomain(Domain* domain);
49 void handleDomainUpdated();
49 void handleDomainUpdated();
@@ -51,13 +51,13 class QDateTimeAxisPrivate : public QAbstractAxisPrivate
51 qreal max(){ return m_max.toMSecsSinceEpoch(); }
51 qreal max(){ return m_max.toMSecsSinceEpoch(); }
52 int count() const { /*TODO:*/ return 0;}
52 int count() const { /*TODO:*/ return 0;}
53
53
54 protected:
54 protected:
55 void setMin(const QVariant &min);
55 void setMin(const QVariant &min);
56 void setMax(const QVariant &max);
56 void setMax(const QVariant &max);
57 void setRange(const QVariant &min, const QVariant &max);
57 void setRange(const QVariant &min, const QVariant &max);
58 int tickCount() const;
58 int tickCount() const;
59
59
60 protected:
60 protected:
61 QDateTime m_min;
61 QDateTime m_min;
62 QDateTime m_max;
62 QDateTime m_max;
63 int m_tickCount;
63 int m_tickCount;
@@ -260,9 +260,9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
260 QChart when axis added.
260 QChart when axis added.
261 */
261 */
262
262
263 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) :
263 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent)
264 QObject(parent),
264 : QObject(parent),
265 d_ptr(&d)
265 d_ptr(&d)
266 {
266 {
267 }
267 }
268
268
@@ -272,7 +272,8 d_ptr(&d)
272
272
273 QAbstractAxis::~QAbstractAxis()
273 QAbstractAxis::~QAbstractAxis()
274 {
274 {
275 if(d_ptr->m_dataset) qFatal("Still binded axis detected !");
275 if (d_ptr->m_dataset)
276 qFatal("Still binded axis detected !");
276 }
277 }
277
278
278 /*!
279 /*!
@@ -280,10 +281,10 QAbstractAxis::~QAbstractAxis()
280 */
281 */
281 void QAbstractAxis::setLinePen(const QPen &pen)
282 void QAbstractAxis::setLinePen(const QPen &pen)
282 {
283 {
283 if (d_ptr->m_axisPen!=pen) {
284 if (d_ptr->m_axisPen != pen) {
284 d_ptr->m_axisPen = pen;
285 d_ptr->m_axisPen = pen;
285 d_ptr->emitUpdated();
286 d_ptr->emitUpdated();
286 }
287 }
287 }
288 }
288
289
289 /*!
290 /*!
@@ -318,7 +319,7 void QAbstractAxis::setLineVisible(bool visible)
318 d_ptr->m_arrowVisible = visible;
319 d_ptr->m_arrowVisible = visible;
319 d_ptr->emitUpdated();
320 d_ptr->emitUpdated();
320 emit lineVisibleChanged(visible);
321 emit lineVisibleChanged(visible);
321 }
322 }
322 }
323 }
323
324
324 bool QAbstractAxis::isLineVisible() const
325 bool QAbstractAxis::isLineVisible() const
@@ -332,7 +333,7 void QAbstractAxis::setGridLineVisible(bool visible)
332 d_ptr->m_gridLineVisible = visible;
333 d_ptr->m_gridLineVisible = visible;
333 d_ptr->emitUpdated();
334 d_ptr->emitUpdated();
334 emit gridVisibleChanged(visible);
335 emit gridVisibleChanged(visible);
335 }
336 }
336 }
337 }
337
338
338 bool QAbstractAxis::isGridLineVisible() const
339 bool QAbstractAxis::isGridLineVisible() const
@@ -348,7 +349,7 void QAbstractAxis::setGridLinePen(const QPen &pen)
348 if (d_ptr->m_gridLinePen != pen) {
349 if (d_ptr->m_gridLinePen != pen) {
349 d_ptr->m_gridLinePen = pen;
350 d_ptr->m_gridLinePen = pen;
350 d_ptr->emitUpdated();
351 d_ptr->emitUpdated();
351 }
352 }
352 }
353 }
353
354
354 /*!
355 /*!
@@ -365,7 +366,7 void QAbstractAxis::setLabelsVisible(bool visible)
365 d_ptr->m_labelsVisible = visible;
366 d_ptr->m_labelsVisible = visible;
366 d_ptr->emitUpdated();
367 d_ptr->emitUpdated();
367 emit labelsVisibleChanged(visible);
368 emit labelsVisibleChanged(visible);
368 }
369 }
369 }
370 }
370
371
371 bool QAbstractAxis::labelsVisible() const
372 bool QAbstractAxis::labelsVisible() const
@@ -381,7 +382,7 void QAbstractAxis::setLabelsPen(const QPen &pen)
381 if (d_ptr->m_labelsPen != pen) {
382 if (d_ptr->m_labelsPen != pen) {
382 d_ptr->m_labelsPen = pen;
383 d_ptr->m_labelsPen = pen;
383 d_ptr->emitUpdated();
384 d_ptr->emitUpdated();
384 }
385 }
385 }
386 }
386
387
387 /*!
388 /*!
@@ -400,7 +401,7 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
400 if (d_ptr->m_labelsBrush != brush) {
401 if (d_ptr->m_labelsBrush != brush) {
401 d_ptr->m_labelsBrush = brush;
402 d_ptr->m_labelsBrush = brush;
402 d_ptr->emitUpdated();
403 d_ptr->emitUpdated();
403 }
404 }
404 }
405 }
405
406
406 /*!
407 /*!
@@ -419,7 +420,7 void QAbstractAxis::setLabelsFont(const QFont &font)
419 if (d_ptr->m_labelsFont != font) {
420 if (d_ptr->m_labelsFont != font) {
420 d_ptr->m_labelsFont = font;
421 d_ptr->m_labelsFont = font;
421 d_ptr->emitUpdated();
422 d_ptr->emitUpdated();
422 }
423 }
423 }
424 }
424
425
425 /*!
426 /*!
@@ -435,7 +436,7 void QAbstractAxis::setLabelsAngle(int angle)
435 if (d_ptr->m_labelsAngle != angle) {
436 if (d_ptr->m_labelsAngle != angle) {
436 d_ptr->m_labelsAngle = angle;
437 d_ptr->m_labelsAngle = angle;
437 d_ptr->emitUpdated();
438 d_ptr->emitUpdated();
438 }
439 }
439 }
440 }
440
441
441 int QAbstractAxis::labelsAngle() const
442 int QAbstractAxis::labelsAngle() const
@@ -463,7 +464,7 void QAbstractAxis::setTitleVisible(bool visible)
463 if (d_ptr->m_titleVisible != visible) {
464 if (d_ptr->m_titleVisible != visible) {
464 d_ptr->m_titleVisible = visible;
465 d_ptr->m_titleVisible = visible;
465 d_ptr->emitUpdated();
466 d_ptr->emitUpdated();
466 }
467 }
467 }
468 }
468
469
469 bool QAbstractAxis::titleVisible() const
470 bool QAbstractAxis::titleVisible() const
@@ -479,7 +480,7 void QAbstractAxis::setTitlePen(const QPen &pen)
479 if (d_ptr->m_titlePen != pen) {
480 if (d_ptr->m_titlePen != pen) {
480 d_ptr->m_titlePen = pen;
481 d_ptr->m_titlePen = pen;
481 d_ptr->emitUpdated();
482 d_ptr->emitUpdated();
482 }
483 }
483 }
484 }
484
485
485 /*!
486 /*!
@@ -498,7 +499,7 void QAbstractAxis::setTitleBrush(const QBrush &brush)
498 if (d_ptr->m_titleBrush != brush) {
499 if (d_ptr->m_titleBrush != brush) {
499 d_ptr->m_titleBrush = brush;
500 d_ptr->m_titleBrush = brush;
500 d_ptr->emitUpdated();
501 d_ptr->emitUpdated();
501 }
502 }
502 }
503 }
503
504
504 /*!
505 /*!
@@ -517,7 +518,7 void QAbstractAxis::setTitleFont(const QFont &font)
517 if (d_ptr->m_titleFont != font) {
518 if (d_ptr->m_titleFont != font) {
518 d_ptr->m_titleFont = font;
519 d_ptr->m_titleFont = font;
519 d_ptr->emitUpdated();
520 d_ptr->emitUpdated();
520 }
521 }
521 }
522 }
522
523
523 /*!
524 /*!
@@ -530,15 +531,15 QFont QAbstractAxis::titleFont() const
530
531
531 void QAbstractAxis::setTitle(const QString& title)
532 void QAbstractAxis::setTitle(const QString& title)
532 {
533 {
533 if (d_ptr->m_title != title) {
534 if (d_ptr->m_title != title) {
534 d_ptr->m_title = title;
535 d_ptr->m_title = title;
535 d_ptr->emitUpdated();
536 d_ptr->emitUpdated();
536 }
537 }
537 }
538 }
538
539
539 QString QAbstractAxis::title() const
540 QString QAbstractAxis::title() const
540 {
541 {
541 return d_ptr->m_title;
542 return d_ptr->m_title;
542 }
543 }
543
544
544
545
@@ -548,7 +549,7 void QAbstractAxis::setShadesVisible(bool visible)
548 d_ptr->m_shadesVisible = visible;
549 d_ptr->m_shadesVisible = visible;
549 d_ptr->emitUpdated();
550 d_ptr->emitUpdated();
550 emit shadesVisibleChanged(visible);
551 emit shadesVisibleChanged(visible);
551 }
552 }
552 }
553 }
553
554
554 bool QAbstractAxis::shadesVisible() const
555 bool QAbstractAxis::shadesVisible() const
@@ -564,7 +565,7 void QAbstractAxis::setShadesPen(const QPen &pen)
564 if (d_ptr->m_shadesPen != pen) {
565 if (d_ptr->m_shadesPen != pen) {
565 d_ptr->m_shadesPen = pen;
566 d_ptr->m_shadesPen = pen;
566 d_ptr->emitUpdated();
567 d_ptr->emitUpdated();
567 }
568 }
568 }
569 }
569
570
570 /*!
571 /*!
@@ -630,8 +631,8 bool QAbstractAxis::isVisible() const
630 */
631 */
631 void QAbstractAxis::setVisible(bool visible)
632 void QAbstractAxis::setVisible(bool visible)
632 {
633 {
633 if(d_ptr->m_visible!=visible){
634 if (d_ptr->m_visible != visible) {
634 d_ptr->m_visible=visible;
635 d_ptr->m_visible = visible;
635 d_ptr->emitUpdated();
636 d_ptr->emitUpdated();
636 emit visibleChanged(visible);
637 emit visibleChanged(visible);
637 }
638 }
@@ -681,7 +682,7 void QAbstractAxis::setMax(const QVariant &max)
681 */
682 */
682 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
683 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
683 {
684 {
684 d_ptr->setRange(min,max);
685 d_ptr->setRange(min, max);
685 }
686 }
686
687
687
688
@@ -730,20 +731,20 QAbstractAxisPrivate::~QAbstractAxisPrivate()
730
731
731 void QAbstractAxisPrivate::emitUpdated()
732 void QAbstractAxisPrivate::emitUpdated()
732 {
733 {
733 if(!m_dirty){
734 if (!m_dirty) {
734 m_dirty=true;
735 m_dirty = true;
735 emit updated();
736 emit updated();
736 }
737 }
737 }
738 }
738
739
739 void QAbstractAxisPrivate::setDirty(bool dirty)
740 void QAbstractAxisPrivate::setDirty(bool dirty)
740 {
741 {
741 m_dirty=dirty;
742 m_dirty = dirty;
742 }
743 }
743
744
744 void QAbstractAxisPrivate::setOrientation(Qt::Orientation orientation)
745 void QAbstractAxisPrivate::setOrientation(Qt::Orientation orientation)
745 {
746 {
746 m_orientation=orientation;
747 m_orientation = orientation;
747 }
748 }
748
749
749
750
@@ -58,7 +58,7 public:
58 Q_DECLARE_FLAGS(AxisTypes, AxisType)
58 Q_DECLARE_FLAGS(AxisTypes, AxisType)
59
59
60 protected:
60 protected:
61 explicit QAbstractAxis(QAbstractAxisPrivate &d,QObject *parent = 0);
61 explicit QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent = 0);
62
62
63 public:
63 public:
64 ~QAbstractAxis();
64 ~QAbstractAxis();
@@ -31,8 +31,9 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis, ChartPresenter *presenter)
35 m_tickCount(0)
35 : ChartAxis(axis, presenter),
36 m_tickCount(0)
36 {
37 {
37 }
38 }
38
39
@@ -42,14 +43,14 ChartValueAxisX::~ChartValueAxisX()
42
43
43 QVector<qreal> ChartValueAxisX::calculateLayout() const
44 QVector<qreal> ChartValueAxisX::calculateLayout() const
44 {
45 {
45 Q_ASSERT(m_tickCount>=2);
46 Q_ASSERT(m_tickCount >= 2);
46
47
47 QVector<qreal> points;
48 QVector<qreal> points;
48 points.resize(m_tickCount);
49 points.resize(m_tickCount);
49
50
50 QRectF rect = presenter()->chartsGeometry();
51 QRectF rect = presenter()->chartsGeometry();
51
52
52 const qreal deltaX = rect.width()/(m_tickCount-1);
53 const qreal deltaX = rect.width() / (m_tickCount - 1);
53 for (int i = 0; i < m_tickCount; ++i) {
54 for (int i = 0; i < m_tickCount; ++i) {
54 int x = i * deltaX + rect.left();
55 int x = i * deltaX + rect.left();
55 points[i] = x;
56 points[i] = x;
@@ -61,9 +62,10 void ChartValueAxisX::updateGeometry()
61 {
62 {
62 const QVector<qreal>& layout = ChartAxis::layout();
63 const QVector<qreal>& layout = ChartAxis::layout();
63
64
64 if(layout.isEmpty()) return;
65 if (layout.isEmpty())
66 return;
65
67
66 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
68 QStringList ticksList = createNumberLabels(m_min, m_max, layout.size());
67
69
68 QList<QGraphicsItem *> lines = m_grid->childItems();
70 QList<QGraphicsItem *> lines = m_grid->childItems();
69 QList<QGraphicsItem *> labels = m_labels->childItems();
71 QList<QGraphicsItem *> labels = m_labels->childItems();
@@ -96,27 +98,27 void ChartValueAxisX::updateGeometry()
96 labelItem->setPos(layout[i] - center.x(), m_internalRect.top() - rect.height() - label_padding);
98 labelItem->setPos(layout[i] - center.x(), m_internalRect.top() - rect.height() - label_padding);
97 else
99 else
98 labelItem->setPos(layout[i] - center.x(), m_internalRect.bottom() + label_padding);
100 labelItem->setPos(layout[i] - center.x(), m_internalRect.bottom() + label_padding);
99 if(labelItem->pos().x() <= width ||
101 if (labelItem->pos().x() <= width ||
100 labelItem->pos().x() < m_rect.left() ||
102 labelItem->pos().x() < m_rect.left() ||
101 labelItem->pos().x() + rect.width() > m_rect.right()){
103 labelItem->pos().x() + rect.width() > m_rect.right()){
102 labelItem->setVisible(false);
104 labelItem->setVisible(false);
103 lineItem->setVisible(false);
105 lineItem->setVisible(false);
104 }else{
106 } else {
105 labelItem->setVisible(true);
107 labelItem->setVisible(true);
106 lineItem->setVisible(true);
108 lineItem->setVisible(true);
107 width=rect.width()+labelItem->pos().x();
109 width = rect.width() + labelItem->pos().x();
108 }
110 }
109
111
110 if ((i+1)%2 && i>1) {
112 if ((i + 1) % 2 && i > 1) {
111 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
113 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
112 rectItem->setRect(layout[i-1],chartRrect.top(),layout[i]-layout[i-1],chartRrect.height());
114 rectItem->setRect(layout[i - 1], chartRrect.top(), layout[i] - layout[i - 1], chartRrect.height());
113 }
115 }
114 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
116 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
115 // lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5);
117 // lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5);
116 if (m_chartAxis->alternativePlacement())
118 if (m_chartAxis->alternativePlacement())
117 lineItem->setLine(layout[i],m_internalRect.top(),layout[i],m_internalRect.top()-5);
119 lineItem->setLine(layout[i], m_internalRect.top(), layout[i], m_internalRect.top() - 5);
118 else
120 else
119 lineItem->setLine(layout[i],m_internalRect.bottom(),layout[i],m_internalRect.bottom()+5);
121 lineItem->setLine(layout[i], m_internalRect.bottom(), layout[i], m_internalRect.bottom() + 5);
120 }
122 }
121 }
123 }
122
124
@@ -136,32 +138,28 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c
136 QSizeF sh;
138 QSizeF sh;
137
139
138 QSizeF base = ChartAxis::sizeHint(which, constraint);
140 QSizeF base = ChartAxis::sizeHint(which, constraint);
139 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
141 QStringList ticksList = createNumberLabels(m_min, m_max, m_tickCount);
140 qreal width=0;
142 qreal width = 0;
141 qreal height=0;
143 qreal height = 0;
142
144
143 switch (which) {
145 switch (which) {
144 case Qt::MinimumSize:{
146 case Qt::MinimumSize: {
145 int count = qMax(ticksList.last().count(),ticksList.first().count());
147 int count = qMax(ticksList.last().count(), ticksList.first().count());
146 width=fn.averageCharWidth()*count;
148 width = fn.averageCharWidth() * count;
147 height=fn.height()+label_padding;
149 height = fn.height() + label_padding;
148 width=qMax(width,base.width());
150 width = qMax(width, base.width());
149 height+=base.height();
151 height += base.height();
150 sh = QSizeF(width,height);
152 sh = QSizeF(width, height);
151 break;
153 break;
152 }
153 case Qt::PreferredSize:{
154 for (int i = 0; i < ticksList.size(); ++i)
155 {
156 width+=fn.averageCharWidth()*ticksList.at(i).count();
157
158 }
154 }
159 height=fn.height()+label_padding;
155 case Qt::PreferredSize:
160 width=qMax(width,base.width());
156 for (int i = 0; i < ticksList.size(); ++i)
161 height+=base.height();
157 width += fn.averageCharWidth() * ticksList.at(i).count();
162 sh = QSizeF(width,height);
158 height = fn.height() + label_padding;
159 width = qMax(width, base.width());
160 height += base.height();
161 sh = QSizeF(width, height);
163 break;
162 break;
164 }
165 default:
163 default:
166 break;
164 break;
167 }
165 }
@@ -31,8 +31,9 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis, ChartPresenter *presenter)
35 m_tickCount(0)
35 : ChartAxis(axis, presenter),
36 m_tickCount(0)
36 {
37 {
37 }
38 }
38
39
@@ -42,14 +43,14 ChartValueAxisY::~ChartValueAxisY()
42
43
43 QVector<qreal> ChartValueAxisY::calculateLayout() const
44 QVector<qreal> ChartValueAxisY::calculateLayout() const
44 {
45 {
45 Q_ASSERT(m_tickCount>=2);
46 Q_ASSERT(m_tickCount >= 2);
46
47
47 QVector<qreal> points;
48 QVector<qreal> points;
48 points.resize(m_tickCount);
49 points.resize(m_tickCount);
49
50
50 QRectF rect = presenter()->chartsGeometry();
51 QRectF rect = presenter()->chartsGeometry();
51
52
52 const qreal deltaY = rect.height()/(m_tickCount-1);
53 const qreal deltaY = rect.height() / (m_tickCount - 1);
53 for (int i = 0; i < m_tickCount; ++i) {
54 for (int i = 0; i < m_tickCount; ++i) {
54 int y = i * -deltaY + rect.bottom();
55 int y = i * -deltaY + rect.bottom();
55 points[i] = y;
56 points[i] = y;
@@ -62,9 +63,10 void ChartValueAxisY::updateGeometry()
62 {
63 {
63 const QVector<qreal> &layout = ChartAxis::layout();
64 const QVector<qreal> &layout = ChartAxis::layout();
64
65
65 if(layout.isEmpty()) return;
66 if (layout.isEmpty())
67 return;
66
68
67 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
69 QStringList ticksList = createNumberLabels(m_min, m_max, layout.size());
68
70
69 QList<QGraphicsItem *> lines = m_grid->childItems();
71 QList<QGraphicsItem *> lines = m_grid->childItems();
70 QList<QGraphicsItem *> labels = m_labels->childItems();
72 QList<QGraphicsItem *> labels = m_labels->childItems();
@@ -81,26 +83,25 void ChartValueAxisY::updateGeometry()
81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
82 // lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
84 // lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
83 if (m_chartAxis->alternativePlacement())
85 if (m_chartAxis->alternativePlacement())
84 lineItem->setLine( m_internalRect.right() , chartRect.top(), m_internalRect.right(), chartRect.bottom());
86 lineItem->setLine(m_internalRect.right(), chartRect.top(), m_internalRect.right(), chartRect.bottom());
85 else
87 else
86 lineItem->setLine( m_internalRect.left() , chartRect.top(), m_internalRect.left(), chartRect.bottom());
88 lineItem->setLine(m_internalRect.left(), chartRect.top(), m_internalRect.left(), chartRect.bottom());
87
89
88 QFontMetrics fn(m_font);
90 QFontMetrics fn(m_font);
89
91
90 for (int i = 0; i < layout.size(); ++i) {
92 for (int i = 0; i < layout.size(); ++i) {
91 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
93 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
92 lineItem->setLine( chartRect.left() , layout[i], chartRect.right(), layout[i]);
94 lineItem->setLine(chartRect.left(), layout[i], chartRect.right(), layout[i]);
93 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
95 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
94
96
95 QString text = ticksList.at(i);
97 QString text = ticksList.at(i);
96
98
97 if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding )
99 if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding) {
98 {
99 QString label = text + "...";
100 QString label = text + "...";
100 while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3)
101 while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3)
101 label.remove(label.length() - 4, 1);
102 label.remove(label.length() - 4, 1);
102 labelItem->setText(label);
103 labelItem->setText(label);
103 }else{
104 } else {
104 labelItem->setText(text);
105 labelItem->setText(text);
105 }
106 }
106
107
@@ -110,30 +111,30 void ChartValueAxisY::updateGeometry()
110 labelItem->setTransformOriginPoint(center.x(), center.y());
111 labelItem->setTransformOriginPoint(center.x(), center.y());
111 // labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
112 // labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
112 if (m_chartAxis->alternativePlacement())
113 if (m_chartAxis->alternativePlacement())
113 labelItem->setPos( m_internalRect.right() + label_padding , layout[i]-center.y());
114 labelItem->setPos(m_internalRect.right() + label_padding , layout[i] - center.y());
114 else
115 else
115 labelItem->setPos( m_internalRect.left() - rect.width() - label_padding , layout[i]-center.y());
116 labelItem->setPos(m_internalRect.left() - rect.width() - label_padding , layout[i] - center.y());
116
117
117 if(labelItem->pos().y() + rect.height() > height ||
118 if (labelItem->pos().y() + rect.height() > height ||
118 labelItem->pos().y() < m_rect.top()) {
119 labelItem->pos().y() < m_rect.top()) {
119 labelItem->setVisible(false);
120 labelItem->setVisible(false);
120 lineItem->setVisible(false);
121 lineItem->setVisible(false);
121 }else{
122 } else {
122 labelItem->setVisible(true);
123 labelItem->setVisible(true);
123 lineItem->setVisible(true);
124 lineItem->setVisible(true);
124 height=labelItem->pos().y();
125 height = labelItem->pos().y();
125 }
126 }
126
127
127 if ((i+1)%2 && i>1) {
128 if ((i + 1) % 2 && i > 1) {
128 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
129 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
129 rectItem->setRect( chartRect.left(),layout[i], chartRect.width(),layout[i-1]-layout[i]);
130 rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]);
130 }
131 }
131 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
132 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
132 // lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]);
133 // lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]);
133 if (m_chartAxis->alternativePlacement())
134 if (m_chartAxis->alternativePlacement())
134 lineItem->setLine( m_internalRect.right()+5,layout[i], m_internalRect.right(),layout[i]);
135 lineItem->setLine(m_internalRect.right() + 5, layout[i], m_internalRect.right(), layout[i]);
135 else
136 else
136 lineItem->setLine( m_internalRect.left()-5,layout[i], m_internalRect.left(),layout[i]);
137 lineItem->setLine(m_internalRect.left() - 5, layout[i], m_internalRect.left(), layout[i]);
137 }
138 }
138 }
139 }
139
140
@@ -152,32 +153,29 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c
152 QFontMetrics fn(m_font);
153 QFontMetrics fn(m_font);
153 QSizeF sh;
154 QSizeF sh;
154 QSizeF base = ChartAxis::sizeHint(which, constraint);
155 QSizeF base = ChartAxis::sizeHint(which, constraint);
155 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
156 QStringList ticksList = createNumberLabels(m_min, m_max, m_tickCount);
156 qreal width=0;
157 qreal width = 0;
157 qreal height=0;
158 qreal height = 0;
158
159
159 switch (which) {
160 switch (which) {
160 case Qt::MinimumSize: {
161 case Qt::MinimumSize: {
161 int count = qMax(ticksList.first().count() , ticksList.last().count());
162 int count = qMax(ticksList.first().count() , ticksList.last().count());
162 width=fn.averageCharWidth()*count+label_padding;
163 width = fn.averageCharWidth() * count + label_padding;
163 height=fn.height();
164 height = fn.height();
164 height=qMax(height,base.height());
165 height = qMax(height, base.height());
165 width+=base.width();
166 width += base.width();
166 sh = QSizeF(width,height);
167 sh = QSizeF(width, height);
167 break;
168 break;
168 }
169 }
169 case Qt::PreferredSize:
170 case Qt::PreferredSize:
170 {
171 for (int i = 0; i < ticksList.size(); ++i) {
171 for (int i = 0; i < ticksList.size(); ++i)
172 width = qMax(qreal(fn.averageCharWidth() * ticksList.at(i).count()) + label_padding, width);
172 {
173 height += fn.height();
173 width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+label_padding,width);
174 height+=fn.height();
175 }
174 }
176 height=qMax(height,base.height());
175 height = qMax(height, base.height());
177 width+=base.width();
176 width += base.width();
178 sh = QSizeF(width,height);
177 sh = QSizeF(width, height);
179 break;
178 break;
180 }
181 default:
179 default:
182 break;
180 break;
183 }
181 }
@@ -152,7 +152,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
152 Constructs an axis object which is a child of \a parent.
152 Constructs an axis object which is a child of \a parent.
153 */
153 */
154 QValueAxis::QValueAxis(QObject *parent) :
154 QValueAxis::QValueAxis(QObject *parent) :
155 QAbstractAxis(*new QValueAxisPrivate(this),parent)
155 QAbstractAxis(*new QValueAxisPrivate(this), parent)
156 {
156 {
157
157
158 }
158 }
@@ -160,7 +160,8 QValueAxis::QValueAxis(QObject *parent) :
160 /*!
160 /*!
161 \internal
161 \internal
162 */
162 */
163 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
163 QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent)
164 : QAbstractAxis(d, parent)
164 {
165 {
165
166
166 }
167 }
@@ -171,9 +172,8 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,p
171 QValueAxis::~QValueAxis()
172 QValueAxis::~QValueAxis()
172 {
173 {
173 Q_D(QValueAxis);
174 Q_D(QValueAxis);
174 if(d->m_dataset) {
175 if (d->m_dataset)
175 d->m_dataset->removeAxis(this);
176 d->m_dataset->removeAxis(this);
176 }
177 }
177 }
178
178
179 void QValueAxis::setMin(qreal min)
179 void QValueAxis::setMin(qreal min)
@@ -209,12 +209,14 void QValueAxis::setRange(qreal min, qreal max)
209 Q_D(QValueAxis);
209 Q_D(QValueAxis);
210 bool changed = false;
210 bool changed = false;
211
211
212 if (min > max) return;
212 if (min > max)
213 return;
213
214
214 if(d->m_niceNumbers) {
215 if (d->m_niceNumbers) {
215 int ticks = d->m_tickCount;
216 int ticks = d->m_tickCount;
216 d->looseNiceNumbers(min, max, ticks);
217 d->looseNiceNumbers(min, max, ticks);
217 if(ticks!=d->m_tickCount) setTickCount(ticks);
218 if (ticks != d->m_tickCount)
219 setTickCount(ticks);
218 }
220 }
219
221
220 if (!qFuzzyIsNull(d->m_min - min)) {
222 if (!qFuzzyIsNull(d->m_min - min)) {
@@ -230,7 +232,7 void QValueAxis::setRange(qreal min, qreal max)
230 }
232 }
231
233
232 if (changed) {
234 if (changed) {
233 emit rangeChanged(min,max);
235 emit rangeChanged(min, max);
234 d->emitUpdated();
236 d->emitUpdated();
235 }
237 }
236 }
238 }
@@ -241,7 +243,7 void QValueAxis::setRange(qreal min, qreal max)
241 void QValueAxis::setTickCount(int count)
243 void QValueAxis::setTickCount(int count)
242 {
244 {
243 Q_D(QValueAxis);
245 Q_D(QValueAxis);
244 if (d->m_tickCount != count && count >=2) {
246 if (d->m_tickCount != count && count >= 2) {
245 d->m_tickCount = count;
247 d->m_tickCount = count;
246 d->emitUpdated();
248 d->emitUpdated();
247 }
249 }
@@ -260,11 +262,10 int QValueAxis::tickCount() const
260 void QValueAxis::setNiceNumbersEnabled(bool enable)
262 void QValueAxis::setNiceNumbersEnabled(bool enable)
261 {
263 {
262 Q_D(QValueAxis);
264 Q_D(QValueAxis);
263 if (d->m_niceNumbers != enable){
265 if (d->m_niceNumbers != enable) {
264 d->m_niceNumbers = enable;
266 d->m_niceNumbers = enable;
265 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) {
267 if (enable && !qFuzzyIsNull(d->m_max - d->m_min))
266 setRange(d->m_min,d->m_max);
268 setRange(d->m_min, d->m_max);
267 }
268 }
269 }
269 }
270 }
270
271
@@ -314,15 +315,14 QValueAxisPrivate::~QValueAxisPrivate()
314
315
315 void QValueAxisPrivate::handleDomainUpdated()
316 void QValueAxisPrivate::handleDomainUpdated()
316 {
317 {
317 Q_Q(QValueAxis);
318 Q_Q(QValueAxis);
318 Domain* domain = qobject_cast<Domain*>(sender());
319 Domain* domain = qobject_cast<Domain*>(sender());
319 Q_ASSERT(domain);
320 Q_ASSERT(domain);
320
321
321 if(orientation()==Qt::Horizontal){
322 if (orientation() == Qt::Horizontal)
322 q->setRange(domain->minX(),domain->maxX());
323 q->setRange(domain->minX(), domain->maxX());
323 }else if(orientation()==Qt::Vertical){
324 else if (orientation() == Qt::Vertical)
324 q->setRange(domain->minY(),domain->maxY());
325 q->setRange(domain->minY(), domain->maxY());
325 }
326 }
326 }
327
327
328
328
@@ -331,7 +331,8 void QValueAxisPrivate::setMin(const QVariant &min)
331 Q_Q(QValueAxis);
331 Q_Q(QValueAxis);
332 bool ok;
332 bool ok;
333 qreal value = min.toReal(&ok);
333 qreal value = min.toReal(&ok);
334 if(ok) q->setMin(value);
334 if (ok)
335 q->setMin(value);
335 }
336 }
336
337
337 void QValueAxisPrivate::setMax(const QVariant &max)
338 void QValueAxisPrivate::setMax(const QVariant &max)
@@ -340,7 +341,8 void QValueAxisPrivate::setMax(const QVariant &max)
340 Q_Q(QValueAxis);
341 Q_Q(QValueAxis);
341 bool ok;
342 bool ok;
342 qreal value = max.toReal(&ok);
343 qreal value = max.toReal(&ok);
343 if(ok) q->setMax(value);
344 if (ok)
345 q->setMax(value);
344 }
346 }
345
347
346 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
348 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
@@ -350,35 +352,31 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
350 bool ok2;
352 bool ok2;
351 qreal value1 = min.toReal(&ok1);
353 qreal value1 = min.toReal(&ok1);
352 qreal value2 = max.toReal(&ok2);
354 qreal value2 = max.toReal(&ok2);
353 if(ok1&&ok2) q->setRange(value1,value2);
355 if (ok1 && ok2)
356 q->setRange(value1, value2);
354 }
357 }
355
358
356 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
359 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
357 {
360 {
358 Q_Q(QValueAxis);
361 Q_Q(QValueAxis);
359 if(m_orientation == Qt::Vertical){
362 if (m_orientation == Qt::Vertical)
360 return new ChartValueAxisY(q,presenter);
363 return new ChartValueAxisY(q, presenter);
361 }else{
364 return new ChartValueAxisX(q, presenter);
362 return new ChartValueAxisX(q,presenter);
363 }
364
365 }
365 }
366
366
367 void QValueAxisPrivate::intializeDomain(Domain* domain)
367 void QValueAxisPrivate::intializeDomain(Domain* domain)
368 {
368 {
369 Q_Q(QValueAxis);
369 Q_Q(QValueAxis);
370 if(qFuzzyCompare(m_max,m_min)) {
370 if (qFuzzyCompare(m_max, m_min)) {
371 if(m_orientation==Qt::Vertical){
371 if (m_orientation == Qt::Vertical)
372 q->setRange(domain->minY(),domain->maxY());
372 q->setRange(domain->minY(), domain->maxY());
373 }else{
373 else
374 q->setRange(domain->minX(), domain->maxX());
374 q->setRange(domain->minX(), domain->maxX());
375 }
376 } else {
375 } else {
377 if(m_orientation==Qt::Vertical){
376 if (m_orientation == Qt::Vertical)
378 domain->setRangeY(m_min, m_max);
377 domain->setRangeY(m_min, m_max);
379 }else{
378 else
380 domain->setRangeX(m_min, m_max);
379 domain->setRangeX(m_min, m_max);
381 }
382 }
380 }
383 }
381 }
384
382
@@ -386,35 +384,34 void QValueAxisPrivate::intializeDomain(Domain* domain)
386
384
387 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
385 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
388 {
386 {
389 qreal range = niceNumber(max-min,true); //range with ceiling
387 qreal range = niceNumber(max - min, true); //range with ceiling
390 qreal step = niceNumber(range/(ticksCount-1),false);
388 qreal step = niceNumber(range / (ticksCount - 1), false);
391 min = qFloor(min/step);
389 min = qFloor(min / step);
392 max = qCeil(max/step);
390 max = qCeil(max / step);
393 ticksCount = int(max-min) +1;
391 ticksCount = int(max - min) + 1;
394 min*=step;
392 min *= step;
395 max*=step;
393 max *= step;
396 }
394 }
397
395
398 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
396 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
399
397
400 qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const
398 qreal QValueAxisPrivate::niceNumber(qreal x, bool ceiling) const
401 {
399 {
402 qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
400 qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
403 qreal q = x/z;//q<10 && q>=1;
401 qreal q = x / z; //q<10 && q>=1;
404
402
405 if(ceiling) {
403 if (ceiling) {
406 if(q <= 1.0) q=1;
404 if (q <= 1.0) q = 1;
407 else if(q <= 2.0) q=2;
405 else if (q <= 2.0) q = 2;
408 else if(q <= 5.0) q=5;
406 else if (q <= 5.0) q = 5;
409 else q=10;
407 else q = 10;
410 }
408 } else {
411 else {
409 if (q < 1.5) q = 1;
412 if(q < 1.5) q=1;
410 else if (q < 3.0) q = 2;
413 else if(q < 3.0) q=2;
411 else if (q < 7.0) q = 5;
414 else if(q < 7.0) q=5;
412 else q = 10;
415 else q=10;
416 }
413 }
417 return q*z;
414 return q * z;
418 }
415 }
419
416
420 #include "moc_qvalueaxis.cpp"
417 #include "moc_qvalueaxis.cpp"
@@ -41,7 +41,7 public:
41 ~QValueAxis();
41 ~QValueAxis();
42
42
43 protected:
43 protected:
44 QValueAxis(QValueAxisPrivate &d,QObject *parent = 0);
44 QValueAxis(QValueAxisPrivate &d, QObject *parent = 0);
45
45
46 public:
46 public:
47 AxisType type() const;
47 AxisType type() const;
@@ -48,7 +48,7 public:
48 void handleDomainUpdated();
48 void handleDomainUpdated();
49 qreal min(){ return m_min; };
49 qreal min(){ return m_min; };
50 qreal max(){ return m_max; };
50 qreal max(){ return m_max; };
51 int count() const { return m_tickCount;}
51 int count() const { return m_tickCount; }
52
52
53 protected:
53 protected:
54 void setMin(const QVariant &min);
54 void setMin(const QVariant &min);
@@ -57,7 +57,7 protected:
57
57
58 private:
58 private:
59 void looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const;
59 void looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const;
60 qreal niceNumber(qreal x,bool ceiling) const;
60 qreal niceNumber(qreal x, bool ceiling) const;
61
61
62 private:
62 private:
63 qreal m_min;
63 qreal m_min;
@@ -69,9 +69,8 QRectF AbstractBarChartItem::boundingRect() const
69 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
69 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
70 {
70 {
71 if (m_animation) {
71 if (m_animation) {
72 m_animation->setup(m_layout,layout);
72 m_animation->setup(m_layout, layout);
73 presenter()->startAnimation(m_animation);
73 presenter()->startAnimation(m_animation);
74
75 } else {
74 } else {
76 setLayout(layout);
75 setLayout(layout);
77 update();
76 update();
@@ -90,9 +89,8 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
90
89
91 m_layout = layout;
90 m_layout = layout;
92
91
93 for (int i=0; i < m_bars.count(); i++) {
92 for (int i = 0; i < m_bars.count(); i++)
94 m_bars.at(i)->setRect(layout.at(i));
93 m_bars.at(i)->setRect(layout.at(i));
95 }
96 }
94 }
97 //handlers
95 //handlers
98
96
@@ -114,27 +112,23 void AbstractBarChartItem::handleGeometryChanged(const QRectF &rect)
114
112
115 void AbstractBarChartItem::handleLayoutChanged()
113 void AbstractBarChartItem::handleLayoutChanged()
116 {
114 {
117 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
115 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
118 // rect size zero.
116 return; // rect size zero.
119 return;
120 }
121 QVector<QRectF> layout = calculateLayout();
117 QVector<QRectF> layout = calculateLayout();
122 applyLayout(layout);
118 applyLayout(layout);
123 }
119 }
124
120
125 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
121 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
126 {
122 {
127 foreach (QGraphicsSimpleTextItem* label, m_labels) {
123 foreach (QGraphicsSimpleTextItem *label, m_labels)
128 label->setVisible(visible);
124 label->setVisible(visible);
129 }
130 update();
125 update();
131 }
126 }
132
127
133 void AbstractBarChartItem::handleDataStructureChanged()
128 void AbstractBarChartItem::handleDataStructureChanged()
134 {
129 {
135 foreach(QGraphicsItem *item, childItems()) {
130 foreach (QGraphicsItem *item, childItems())
136 delete item;
131 delete item;
137 }
138
132
139 m_bars.clear();
133 m_bars.clear();
140 m_labels.clear();
134 m_labels.clear();
@@ -148,12 +142,12 void AbstractBarChartItem::handleDataStructureChanged()
148 QBarSet *set = m_series->d_func()->barsetAt(s);
142 QBarSet *set = m_series->d_func()->barsetAt(s);
149
143
150 // Bars
144 // Bars
151 Bar *bar = new Bar(set,c,this);
145 Bar *bar = new Bar(set, c, this);
152 m_bars.append(bar);
146 m_bars.append(bar);
153 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
147 connect(bar, SIGNAL(clicked(int, QBarSet*)), m_series, SIGNAL(clicked(int, QBarSet*)));
154 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
148 connect(bar, SIGNAL(hovered(bool, QBarSet*)), m_series, SIGNAL(hovered(bool, QBarSet*)));
155 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
149 connect(bar, SIGNAL(clicked(int, QBarSet*)), set, SIGNAL(clicked(int)));
156 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
150 connect(bar, SIGNAL(hovered(bool, QBarSet*)), set, SIGNAL(hovered(bool)));
157 m_layout.append(QRectF(0, 0, 0, 0));
151 m_layout.append(QRectF(0, 0, 0, 0));
158
152
159 // Labels
153 // Labels
@@ -171,13 +165,13 void AbstractBarChartItem::handleVisibleChanged()
171 {
165 {
172 bool visible = m_series->isVisible();
166 bool visible = m_series->isVisible();
173 handleLabelsVisibleChanged(visible);
167 handleLabelsVisibleChanged(visible);
174 foreach(QGraphicsItem *item, childItems())
168 foreach (QGraphicsItem *item, childItems())
175 item->setVisible(visible);
169 item->setVisible(visible);
176 }
170 }
177
171
178 void AbstractBarChartItem::handleOpacityChanged()
172 void AbstractBarChartItem::handleOpacityChanged()
179 {
173 {
180 foreach(QGraphicsItem *item, childItems())
174 foreach (QGraphicsItem *item, childItems())
181 item->setOpacity(m_series->opacity());
175 item->setOpacity(m_series->opacity());
182 }
176 }
183
177
@@ -25,8 +25,8
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) :
28 HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter)
29 AbstractBarChartItem(series, presenter)
29 : AbstractBarChartItem(series, presenter)
30 {
30 {
31 }
31 }
32
32
@@ -57,8 +57,8 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
57 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
58
58
59 qreal yPos = geometry().bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
59 qreal yPos = geometry().bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
60 yPos += setCount*rectHeight/2;
60 yPos += setCount * rectHeight / 2;
61 yPos -= set*rectHeight;
61 yPos -= set * rectHeight;
62
62
63 qreal rectWidth = barSet->value(category) * scaleX;
63 qreal rectWidth = barSet->value(category) * scaleX;
64 Bar* bar = m_bars.at(itemIndex);
64 Bar* bar = m_bars.at(itemIndex);
@@ -67,22 +67,20 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
67 layout.append(rect);
67 layout.append(rect);
68 bar->setPen(barSet->m_pen);
68 bar->setPen(barSet->m_pen);
69 bar->setBrush(barSet->m_brush);
69 bar->setBrush(barSet->m_brush);
70 if (qFuzzyIsNull(rectHeight)) {
70 if (qFuzzyIsNull(rectHeight))
71 bar->setVisible(false);
71 bar->setVisible(false);
72 } else {
72 else
73 bar->setVisible(barsVisible);
73 bar->setVisible(barsVisible);
74 }
75
74
76 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
75 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
77
76
78 if (!qFuzzyIsNull(barSet->value(category))) {
77 if (!qFuzzyIsNull(barSet->value(category)))
79 label->setText(QString::number(barSet->value(category)));
78 label->setText(QString::number(barSet->value(category)));
80 } else {
79 else
81 label->setText(QString(""));
80 label->setText(QString(""));
82 }
83
81
84 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
82 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
85 ,yPos - rectHeight/2 - label->boundingRect().height()/2);
83 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
86 label->setFont(barSet->m_labelFont);
84 label->setFont(barSet->m_labelFont);
87 label->setBrush(barSet->m_labelBrush);
85 label->setBrush(barSet->m_labelBrush);
88
86
@@ -72,9 +72,8 QHorizontalBarSeries::QHorizontalBarSeries(QObject *parent) :
72 QHorizontalBarSeries::~QHorizontalBarSeries()
72 QHorizontalBarSeries::~QHorizontalBarSeries()
73 {
73 {
74 Q_D(QHorizontalBarSeries);
74 Q_D(QHorizontalBarSeries);
75 if(d->m_dataset) {
75 if (d->m_dataset)
76 d->m_dataset->removeSeries(this);
76 d->m_dataset->removeSeries(this);
77 }
78 }
77 }
79
78
80 /*!
79 /*!
@@ -105,17 +104,16 void QHorizontalBarSeriesPrivate::scaleDomain(Domain& domain)
105 maxX = qMax(maxX, max());
104 maxX = qMax(maxX, max());
106 maxY = qMax(maxY, y - (qreal)0.5);
105 maxY = qMax(maxY, y - (qreal)0.5);
107
106
108 domain.setRange(minX,maxX,minY,maxY);
107 domain.setRange(minX, maxX, minY, maxY);
109 }
108 }
110
109
111 ChartElement* QHorizontalBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
110 ChartElement* QHorizontalBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
112 {
111 {
113 Q_Q(QHorizontalBarSeries);
112 Q_Q(QHorizontalBarSeries);
114
113
115 HorizontalBarChartItem* bar = new HorizontalBarChartItem(q,presenter);
114 HorizontalBarChartItem* bar = new HorizontalBarChartItem(q, presenter);
116 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
115 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
117 bar->setAnimation(new HorizontalBarAnimation(bar));
116 bar->setAnimation(new HorizontalBarAnimation(bar));
118 }
119 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
117 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
120 return bar;
118 return bar;
121 }
119 }
@@ -56,7 +56,7 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
56 for (int set = 0; set < setCount; set++) {
56 for (int set = 0; set < setCount; set++) {
57 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
58
58
59 qreal yPos = (m_domainMinY +0.5 -barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight/2;
59 qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2;
60
60
61 qreal rectWidth = barSet->value(category) * percentage * scaleX;
61 qreal rectWidth = barSet->value(category) * percentage * scaleX;
62 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
@@ -65,16 +65,15 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
65 layout.append(rect);
65 layout.append(rect);
66 bar->setPen(barSet->m_pen);
66 bar->setPen(barSet->m_pen);
67 bar->setBrush(barSet->m_brush);
67 bar->setBrush(barSet->m_brush);
68 if (qFuzzyIsNull(rectHeight)) {
68 if (qFuzzyIsNull(rectHeight))
69 bar->setVisible(false);
69 bar->setVisible(false);
70 } else {
70 else
71 bar->setVisible(barsVisible);
71 bar->setVisible(barsVisible);
72 }
73
72
74 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
73 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
75
74
76 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
75 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) {
77 int p = m_series->d_func()->percentageAt(set,category) * 100;
76 int p = m_series->d_func()->percentageAt(set, category) * 100;
78 QString vString(QString::number(p));
77 QString vString(QString::number(p));
79 vString.truncate(3);
78 vString.truncate(3);
80 vString.append("%");
79 vString.append("%");
@@ -83,8 +82,8 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
83 label->setText(QString(""));
82 label->setText(QString(""));
84 }
83 }
85
84
86 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
85 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
87 ,yPos - rectHeight/2 - label->boundingRect().height()/2);
86 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
88 label->setFont(barSet->m_labelFont);
87 label->setFont(barSet->m_labelFont);
89 label->setBrush(barSet->m_labelBrush);
88 label->setBrush(barSet->m_labelBrush);
90
89
@@ -77,9 +77,8 QAbstractSeries::SeriesType QHorizontalPercentBarSeries::type() const
77 QHorizontalPercentBarSeries::~QHorizontalPercentBarSeries()
77 QHorizontalPercentBarSeries::~QHorizontalPercentBarSeries()
78 {
78 {
79 Q_D(QHorizontalPercentBarSeries);
79 Q_D(QHorizontalPercentBarSeries);
80 if(d->m_dataset) {
80 if (d->m_dataset)
81 d->m_dataset->removeSeries(this);
81 d->m_dataset->removeSeries(this);
82 }
83 }
82 }
84
83
85 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -102,17 +101,16 void QHorizontalPercentBarSeriesPrivate::scaleDomain(Domain& domain)
102 minY = qMin(minY, - (qreal)0.5);
101 minY = qMin(minY, - (qreal)0.5);
103 maxY = qMax(maxY, y - (qreal)0.5);
102 maxY = qMax(maxY, y - (qreal)0.5);
104
103
105 domain.setRange(minX,maxX,minY,maxY);
104 domain.setRange(minX, maxX, minY, maxY);
106 }
105 }
107
106
108 ChartElement* QHorizontalPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
107 ChartElement* QHorizontalPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
109 {
108 {
110 Q_Q(QHorizontalPercentBarSeries);
109 Q_Q(QHorizontalPercentBarSeries);
111
110
112 HorizontalPercentBarChartItem* bar = new HorizontalPercentBarChartItem(q,presenter);
111 HorizontalPercentBarChartItem* bar = new HorizontalPercentBarChartItem(q, presenter);
113 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
112 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
114 bar->setAnimation(new HorizontalPercentBarAnimation(bar));
113 bar->setAnimation(new HorizontalPercentBarAnimation(bar));
115 }
116 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
114 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
117 return bar;
115 return bar;
118 }
116 }
@@ -55,40 +55,38 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57
57
58 qreal yPos = (m_domainMinY +0.5 -barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight/2;
58 qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2;
59
59
60 qreal rectWidth = barSet->value(category) * scaleX;
60 qreal rectWidth = barSet->value(category) * scaleX;
61 Bar* bar = m_bars.at(itemIndex);
61 Bar* bar = m_bars.at(itemIndex);
62
62
63 bar->setPen(barSet->m_pen);
63 bar->setPen(barSet->m_pen);
64 bar->setBrush(barSet->m_brush);
64 bar->setBrush(barSet->m_brush);
65 if (qFuzzyIsNull(rectHeight)) {
65 if (qFuzzyIsNull(rectHeight))
66 bar->setVisible(false);
66 bar->setVisible(false);
67 } else {
67 else
68 bar->setVisible(barsVisible);
68 bar->setVisible(barsVisible);
69 }
70
69
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
72
71
73 if (!qFuzzyIsNull(barSet->value(category))) {
72 if (!qFuzzyIsNull(barSet->value(category)))
74 label->setText(QString::number(barSet->value(category)));
73 label->setText(QString::number(barSet->value(category)));
75 } else {
74 else
76 label->setText(QString(""));
75 label->setText(QString(""));
77 }
78 label->setFont(barSet->m_labelFont);
76 label->setFont(barSet->m_labelFont);
79 label->setBrush(barSet->m_labelBrush);
77 label->setBrush(barSet->m_labelBrush);
80
78
81 if (rectWidth > 0) {
79 if (rectWidth > 0) {
82 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
80 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
83 layout.append(rect);
81 layout.append(rect);
84 label->setPos(xMax + (rect.width()/2 - label->boundingRect().width()/2)
82 label->setPos(xMax + (rect.width() / 2 - label->boundingRect().width() / 2),
85 ,yPos - rectHeight/2 - label->boundingRect().height()/2);
83 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
86 xMax += rectWidth;
84 xMax += rectWidth;
87 } else {
85 } else {
88 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
86 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
89 layout.append(rect);
87 layout.append(rect);
90 label->setPos(xMin + (rect.width()/2 - label->boundingRect().width()/2)
88 label->setPos(xMin + (rect.width() / 2 - label->boundingRect().width() / 2),
91 ,yPos - rectHeight/2 - label->boundingRect().height()/2);
89 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
92 xMin += rectWidth;
90 xMin += rectWidth;
93 }
91 }
94 itemIndex++;
92 itemIndex++;
@@ -68,9 +68,8 QHorizontalStackedBarSeries::QHorizontalStackedBarSeries(QObject *parent) :
68 QHorizontalStackedBarSeries::~QHorizontalStackedBarSeries()
68 QHorizontalStackedBarSeries::~QHorizontalStackedBarSeries()
69 {
69 {
70 Q_D(QHorizontalStackedBarSeries);
70 Q_D(QHorizontalStackedBarSeries);
71 if(d->m_dataset) {
71 if (d->m_dataset)
72 d->m_dataset->removeSeries(this);
72 d->m_dataset->removeSeries(this);
73 }
74 }
73 }
75
74
76 /*!
75 /*!
@@ -101,17 +100,16 void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain& domain)
101 maxX = qMax(maxX, top());
100 maxX = qMax(maxX, top());
102 maxY = qMax(maxY, y - (qreal)0.5);
101 maxY = qMax(maxY, y - (qreal)0.5);
103
102
104 domain.setRange(minX,maxX,minY,maxY);
103 domain.setRange(minX, maxX, minY, maxY);
105 }
104 }
106
105
107 ChartElement* QHorizontalStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
106 ChartElement* QHorizontalStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
108 {
107 {
109 Q_Q(QHorizontalStackedBarSeries);
108 Q_Q(QHorizontalStackedBarSeries);
110
109
111 HorizontalStackedBarChartItem* bar = new HorizontalStackedBarChartItem(q,presenter);
110 HorizontalStackedBarChartItem* bar = new HorizontalStackedBarChartItem(q, presenter);
112 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
111 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
113 bar->setAnimation(new HorizontalStackedBarAnimation(bar));
112 bar->setAnimation(new HorizontalStackedBarAnimation(bar));
114 }
115 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
113 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
116 return bar;
114 return bar;
117 }
115 }
@@ -198,11 +198,11 QAbstractBarSeries::~QAbstractBarSeries()
198 /*!
198 /*!
199 \internal
199 \internal
200 */
200 */
201 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent) :
201 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
202 QAbstractSeries(o,parent)
202 : QAbstractSeries(o, parent)
203 {
203 {
204 Q_D(QAbstractSeries);
204 Q_D(QAbstractSeries);
205 QObject::connect(this,SIGNAL(countChanged()),d,SIGNAL(countChanged()));
205 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
206 }
206 }
207
207
208 /*!
208 /*!
@@ -333,9 +333,8 void QAbstractBarSeries::clear()
333 if (success) {
333 if (success) {
334 emit barsetsRemoved(sets);
334 emit barsetsRemoved(sets);
335 emit countChanged();
335 emit countChanged();
336 foreach (QBarSet* set, sets) {
336 foreach (QBarSet *set, sets)
337 delete set;
337 delete set;
338 }
339 }
338 }
340 }
339 }
341
340
@@ -392,10 +391,9 int QAbstractBarSeriesPrivate::categoryCount() const
392 {
391 {
393 // No categories defined. return count of longest set.
392 // No categories defined. return count of longest set.
394 int count = 0;
393 int count = 0;
395 for (int i=0; i<m_barSets.count(); i++) {
394 for (int i = 0; i < m_barSets.count(); i++) {
396 if (m_barSets.at(i)->count() > count) {
395 if (m_barSets.at(i)->count() > count)
397 count = m_barSets.at(i)->count();
396 count = m_barSets.at(i)->count();
398 }
399 }
397 }
400
398
401 return count;
399 return count;
@@ -403,9 +401,8 int QAbstractBarSeriesPrivate::categoryCount() const
403
401
404 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
402 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
405 {
403 {
406 if (width < 0.0) {
404 if (width < 0.0)
407 width = 0.0;
405 width = 0.0;
408 }
409 m_barWidth = width;
406 m_barWidth = width;
410 emit updatedLayout();
407 emit updatedLayout();
411 }
408 }
@@ -434,9 +431,9 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
434
431
435 qreal QAbstractBarSeriesPrivate::min()
432 qreal QAbstractBarSeriesPrivate::min()
436 {
433 {
437 if (m_barSets.count() <= 0) {
434 if (m_barSets.count() <= 0)
438 return 0;
435 return 0;
439 }
436
440 qreal min = INT_MAX;
437 qreal min = INT_MAX;
441
438
442 for (int i = 0; i < m_barSets.count(); i++) {
439 for (int i = 0; i < m_barSets.count(); i++) {
@@ -452,9 +449,9 qreal QAbstractBarSeriesPrivate::min()
452
449
453 qreal QAbstractBarSeriesPrivate::max()
450 qreal QAbstractBarSeriesPrivate::max()
454 {
451 {
455 if (m_barSets.count() <= 0) {
452 if (m_barSets.count() <= 0)
456 return 0;
453 return 0;
457 }
454
458 qreal max = INT_MIN;
455 qreal max = INT_MIN;
459
456
460 for (int i = 0; i < m_barSets.count(); i++) {
457 for (int i = 0; i < m_barSets.count(); i++) {
@@ -471,32 +468,25 qreal QAbstractBarSeriesPrivate::max()
471
468
472 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
469 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
473 {
470 {
474 if ((set < 0) || (set >= m_barSets.count())) {
471 if ((set < 0) || (set >= m_barSets.count()))
475 // No set, no value.
472 return 0; // No set, no value.
476 return 0;
473 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
477 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
474 return 0; // No category, no value.
478 // No category, no value.
479 return 0;
480 }
481
475
482 return m_barSets.at(set)->at(category);
476 return m_barSets.at(set)->at(category);
483 }
477 }
484
478
485 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
479 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
486 {
480 {
487 if ((set < 0) || (set >= m_barSets.count())) {
481 if ((set < 0) || (set >= m_barSets.count()))
488 // No set, no value.
482 return 0; // No set, no value.
489 return 0;
483 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
490 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
484 return 0; // No category, no value.
491 // No category, no value.
492 return 0;
493 }
494
485
495 qreal value = m_barSets.at(set)->at(category);
486 qreal value = m_barSets.at(set)->at(category);
496 qreal sum = categorySum(category);
487 qreal sum = categorySum(category);
497 if ( qFuzzyIsNull(sum) ) {
488 if (qFuzzyIsNull(sum))
498 return 0;
489 return 0;
499 }
500
490
501 return value / sum;
491 return value / sum;
502 }
492 }
@@ -537,9 +527,9 qreal QAbstractBarSeriesPrivate::maxCategorySum()
537
527
538 qreal QAbstractBarSeriesPrivate::minX()
528 qreal QAbstractBarSeriesPrivate::minX()
539 {
529 {
540 if (m_barSets.count() <= 0) {
530 if (m_barSets.count() <= 0)
541 return 0;
531 return 0;
542 }
532
543 qreal min = INT_MAX;
533 qreal min = INT_MAX;
544
534
545 for (int i = 0; i < m_barSets.count(); i++) {
535 for (int i = 0; i < m_barSets.count(); i++) {
@@ -555,9 +545,9 qreal QAbstractBarSeriesPrivate::minX()
555
545
556 qreal QAbstractBarSeriesPrivate::maxX()
546 qreal QAbstractBarSeriesPrivate::maxX()
557 {
547 {
558 if (m_barSets.count() <= 0) {
548 if (m_barSets.count() <= 0)
559 return 0;
549 return 0;
560 }
550
561 qreal max = INT_MIN;
551 qreal max = INT_MIN;
562
552
563 for (int i = 0; i < m_barSets.count(); i++) {
553 for (int i = 0; i < m_barSets.count(); i++) {
@@ -611,11 +601,10 qreal QAbstractBarSeriesPrivate::top()
611 // Returns top of all categories
601 // Returns top of all categories
612 qreal top(0);
602 qreal top(0);
613 int count = categoryCount();
603 int count = categoryCount();
614 for (int i=0; i<count; i++) {
604 for (int i = 0; i < count; i++) {
615 qreal temp = categoryTop(i);
605 qreal temp = categoryTop(i);
616 if (temp > top) {
606 if (temp > top)
617 top = temp;
607 top = temp;
618 }
619 }
608 }
620 return top;
609 return top;
621 }
610 }
@@ -625,11 +614,10 qreal QAbstractBarSeriesPrivate::bottom()
625 // Returns bottom of all categories
614 // Returns bottom of all categories
626 qreal bottom(0);
615 qreal bottom(0);
627 int count = categoryCount();
616 int count = categoryCount();
628 for (int i=0; i<count; i++) {
617 for (int i = 0; i < count; i++) {
629 qreal temp = categoryBottom(i);
618 qreal temp = categoryBottom(i);
630 if (temp < bottom) {
619 if (temp < bottom)
631 bottom = temp;
620 bottom = temp;
632 }
633 }
621 }
634 return bottom;
622 return bottom;
635 }
623 }
@@ -650,7 +638,7 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
650 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
638 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
651 maxY = qMax(maxY, y);
639 maxY = qMax(maxY, y);
652
640
653 domain.setRange(minX,maxX,minY,maxY);
641 domain.setRange(minX, maxX, minY, maxY);
654 }
642 }
655
643
656 ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
644 ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
@@ -664,8 +652,8 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* lege
664 {
652 {
665 Q_Q(QAbstractBarSeries);
653 Q_Q(QAbstractBarSeries);
666 QList<LegendMarker*> markers;
654 QList<LegendMarker*> markers;
667 foreach(QBarSet* set, q->barSets()) {
655 foreach (QBarSet *set, q->barSets()) {
668 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
656 BarLegendMarker* marker = new BarLegendMarker(q, set, legend);
669 markers << marker;
657 markers << marker;
670 }
658 }
671
659
@@ -674,77 +662,65 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* lege
674
662
675 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
663 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
676 {
664 {
677 if ((m_barSets.contains(set)) || (set == 0)) {
665 if ((m_barSets.contains(set)) || (set == 0))
678 // Fail if set is already in list or set is null.
666 return false; // Fail if set is already in list or set is null.
679 return false;
680 }
681
667
682 m_barSets.append(set);
668 m_barSets.append(set);
683 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
669 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
684 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
670 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
685 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
671 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
686
672
687 emit restructuredBars(); // this notifies barchartitem
673 emit restructuredBars(); // this notifies barchartitem
688 return true;
674 return true;
689 }
675 }
690
676
691 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
677 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
692 {
678 {
693 if (!m_barSets.contains(set)) {
679 if (!m_barSets.contains(set))
694 // Fail if set is not in list
680 return false; // Fail if set is not in list
695 return false;
696 }
697
681
698 m_barSets.removeOne(set);
682 m_barSets.removeOne(set);
699 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
683 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
700 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
684 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
701 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
685 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
702
686
703 emit restructuredBars(); // this notifies barchartitem
687 emit restructuredBars(); // this notifies barchartitem
704 return true;
688 return true;
705 }
689 }
706
690
707 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
691 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
708 {
692 {
709 foreach (QBarSet* set, sets) {
693 foreach (QBarSet *set, sets) {
710 if ((set == 0) || (m_barSets.contains(set))) {
694 if ((set == 0) || (m_barSets.contains(set)))
711 // Fail if any of the sets is null or is already appended.
695 return false; // Fail if any of the sets is null or is already appended.
712 return false;
696 if (sets.count(set) != 1)
713 }
697 return false; // Also fail if same set is more than once in given list.
714 if (sets.count(set) != 1) {
715 // Also fail if same set is more than once in given list.
716 return false;
717 }
718 }
698 }
719
699
720 foreach (QBarSet* set, sets) {
700 foreach (QBarSet *set, sets) {
721 m_barSets.append(set);
701 m_barSets.append(set);
722 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
702 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
723 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
703 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
724 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
704 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
725 }
705 }
726
706
727 emit restructuredBars(); // this notifies barchartitem
707 emit restructuredBars(); // this notifies barchartitem
728 return true;
708 return true;
729 }
709 }
730
710
731 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
711 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
732 {
712 {
733 if (sets.count() == 0) {
713 if (sets.count() == 0)
734 return false;
714 return false;
735 }
715
736 foreach (QBarSet* set, sets) {
716 foreach (QBarSet *set, sets) {
737 if ((set == 0) || (!m_barSets.contains(set))) {
717 if ((set == 0) || (!m_barSets.contains(set)))
738 // Fail if any of the sets is null or is not in series
718 return false; // Fail if any of the sets is null or is not in series
739 return false;
719 if (sets.count(set) != 1)
740 }
720 return false; // Also fail if same set is more than once in given list.
741 if (sets.count(set) != 1) {
742 // Also fail if same set is more than once in given list.
743 return false;
744 }
745 }
721 }
746
722
747 foreach (QBarSet* set, sets) {
723 foreach (QBarSet *set, sets) {
748 m_barSets.removeOne(set);
724 m_barSets.removeOne(set);
749 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
725 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
750 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
726 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
@@ -757,10 +733,8 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
757
733
758 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
734 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
759 {
735 {
760 if ((m_barSets.contains(set)) || (set == 0)) {
736 if ((m_barSets.contains(set)) || (set == 0))
761 // Fail if set is already in list or set is null.
737 return false; // Fail if set is already in list or set is null.
762 return false;
763 }
764
738
765 m_barSets.insert(index, set);
739 m_barSets.insert(index, set);
766 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
740 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
@@ -775,34 +749,23 void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis)
775 {
749 {
776 Q_Q(QAbstractBarSeries);
750 Q_Q(QAbstractBarSeries);
777
751
778 if(axis->type()==QAbstractAxis::AxisTypeBarCategory) {
752 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
779
753 switch (q->type()) {
780 switch(q->type()) {
754 case QAbstractSeries::SeriesTypeHorizontalBar:
781
755 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
782 case QAbstractSeries::SeriesTypeHorizontalBar:
756 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
783 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
757 if (axis->orientation() == Qt::Vertical)
784 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
758 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
785
759 break;
786 if(axis->orientation()==Qt::Vertical)
760 case QAbstractSeries::SeriesTypeBar:
787 {
761 case QAbstractSeries::SeriesTypePercentBar:
788 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
762 case QAbstractSeries::SeriesTypeStackedBar:
789 }
763 if (axis->orientation() == Qt::Horizontal)
790 break;
764 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
791 }
765 break;
792 case QAbstractSeries::SeriesTypeBar:
766 default:
793 case QAbstractSeries::SeriesTypePercentBar:
767 qWarning() << "Unexpected series type";
794 case QAbstractSeries::SeriesTypeStackedBar: {
795
796 if(axis->orientation()==Qt::Horizontal)
797 {
798 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
799 }
800 break;
801 }
802 default:
803 qWarning()<<"Unexpected series type";
804 break;
768 break;
805
806 }
769 }
807 }
770 }
808 }
771 }
@@ -811,32 +774,22 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientati
811 {
774 {
812 Q_Q(const QAbstractBarSeries);
775 Q_Q(const QAbstractBarSeries);
813
776
814 switch(q->type()) {
777 switch (q->type()) {
815
778 case QAbstractSeries::SeriesTypeHorizontalBar:
816 case QAbstractSeries::SeriesTypeHorizontalBar:
779 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
817 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
780 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
818 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
781 if (orientation == Qt::Vertical)
819
782 return QAbstractAxis::AxisTypeBarCategory;
820 if(orientation==Qt::Vertical)
783 break;
821 {
784 case QAbstractSeries::SeriesTypeBar:
822 return QAbstractAxis::AxisTypeBarCategory;
785 case QAbstractSeries::SeriesTypePercentBar:
823 }
786 case QAbstractSeries::SeriesTypeStackedBar:
824 break;
787 if (orientation == Qt::Horizontal)
825 }
788 return QAbstractAxis::AxisTypeBarCategory;
826 case QAbstractSeries::SeriesTypeBar:
789 break;
827 case QAbstractSeries::SeriesTypePercentBar:
790 default:
828 case QAbstractSeries::SeriesTypeStackedBar: {
791 qWarning() << "Unexpected series type";
829
830 if(orientation==Qt::Horizontal)
831 {
832 return QAbstractAxis::AxisTypeBarCategory;
833 }
834 break;
835 }
836 default:
837 qWarning()<<"Unexpected series type";
838 break;
792 break;
839
840 }
793 }
841 return QAbstractAxis::AxisTypeValue;
794 return QAbstractAxis::AxisTypeValue;
842
795
@@ -845,9 +798,9 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientati
845 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis)
798 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis)
846 {
799 {
847 QStringList categories;
800 QStringList categories;
848 if(axis->categories().isEmpty()) {
801 if (axis->categories().isEmpty()) {
849 for (int i(1); i < categoryCount()+1; i++)
802 for (int i(1); i < categoryCount() + 1; i++)
850 categories << QString::number(i);
803 categories << QString::number(i);
851 axis->append(categories);
804 axis->append(categories);
852 }
805 }
853 }
806 }
@@ -855,5 +808,4 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis)
855 #include "moc_qabstractbarseries.cpp"
808 #include "moc_qabstractbarseries.cpp"
856 #include "moc_qabstractbarseries_p.cpp"
809 #include "moc_qabstractbarseries_p.cpp"
857
810
858
859 QTCOMMERCIALCHART_END_NAMESPACE
811 QTCOMMERCIALCHART_END_NAMESPACE
@@ -56,7 +56,7 public:
56 bool isLabelsVisible() const;
56 bool isLabelsVisible() const;
57
57
58 protected:
58 protected:
59 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0);
59 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = 0);
60
60
61 Q_SIGNALS:
61 Q_SIGNALS:
62 void clicked(int index, QBarSet *barset);
62 void clicked(int index, QBarSet *barset);
@@ -45,13 +45,12 void QBarModelMapper::setModel(QAbstractItemModel *model)
45 return;
45 return;
46
46
47 Q_D(QBarModelMapper);
47 Q_D(QBarModelMapper);
48 if (d->m_model) {
48 if (d->m_model)
49 disconnect(d->m_model, 0, d, 0);
49 disconnect(d->m_model, 0, d, 0);
50 }
51
50
52 d->m_model = model;
51 d->m_model = model;
53 d->initializeBarFromModel();
52 d->initializeBarFromModel();
54 // connect signals from the model
53 // connect signals from the model
55 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
54 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
56 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
55 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
57 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
56 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
@@ -70,9 +69,8 QAbstractBarSeries* QBarModelMapper::series() const
70 void QBarModelMapper::setSeries(QAbstractBarSeries *series)
69 void QBarModelMapper::setSeries(QAbstractBarSeries *series)
71 {
70 {
72 Q_D(QBarModelMapper);
71 Q_D(QBarModelMapper);
73 if (d->m_series) {
72 if (d->m_series)
74 disconnect(d->m_series, 0, d, 0);
73 disconnect(d->m_series, 0, d, 0);
75 }
76
74
77 if (series == 0)
75 if (series == 0)
78 return;
76 return;
@@ -402,9 +400,10 void QBarModelMapperPrivate::barSetsAdded(QList<QBarSet*> sets)
402 return;
400 return;
403
401
404 int maxCount = 0;
402 int maxCount = 0;
405 for(int i = 0; i < sets.count(); i++)
403 for (int i = 0; i < sets.count(); i++) {
406 if (sets.at(i)->count() > m_count)
404 if (sets.at(i)->count() > m_count)
407 maxCount = sets.at(i)->count();
405 maxCount = sets.at(i)->count();
406 }
408
407
409 if (m_count != -1 && m_count < maxCount)
408 if (m_count != -1 && m_count < maxCount)
410 m_count = maxCount;
409 m_count = maxCount;
@@ -426,7 +425,7 void QBarModelMapperPrivate::barSetsAdded(QList<QBarSet*> sets)
426 m_model->insertRows(firstIndex + m_firstBarSetSection, sets.count());
425 m_model->insertRows(firstIndex + m_firstBarSetSection, sets.count());
427
426
428
427
429 for(int i = firstIndex + m_firstBarSetSection; i < firstIndex + m_firstBarSetSection + sets.count(); i++) {
428 for (int i = firstIndex + m_firstBarSetSection; i < firstIndex + m_firstBarSetSection + sets.count(); i++) {
430 m_model->setHeaderData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, sets.at(i - firstIndex - m_firstBarSetSection)->label());
429 m_model->setHeaderData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, sets.at(i - firstIndex - m_firstBarSetSection)->label());
431 for (int j = 0; j < sets.at(i - firstIndex - m_firstBarSetSection)->count(); j++)
430 for (int j = 0; j < sets.at(i - firstIndex - m_firstBarSetSection)->count(); j++)
432 m_model->setData(barModelIndex(i, j), sets.at(i - firstIndex - m_firstBarSetSection)->at(j));
431 m_model->setData(barModelIndex(i, j), sets.at(i - firstIndex - m_firstBarSetSection)->at(j));
@@ -57,7 +57,7 protected:
57 void setLastBarSetSection(int lastBarSetSection);
57 void setLastBarSetSection(int lastBarSetSection);
58
58
59 Qt::Orientation orientation() const;
59 Qt::Orientation orientation() const;
60 void setOrientation(Qt::Orientation orientation);
60 void setOrientation(Qt::Orientation orientation);
61
61
62 protected:
62 protected:
63 QBarModelMapperPrivate * const d_ptr;
63 QBarModelMapperPrivate * const d_ptr;
@@ -75,7 +75,7 private:
75 void blockSeriesSignals(bool block = true);
75 void blockSeriesSignals(bool block = true);
76
76
77 private:
77 private:
78 QAbstractBarSeries *m_series;
78 QAbstractBarSeries *m_series;
79 QList<QBarSet*> m_barSets;
79 QList<QBarSet*> m_barSets;
80 QAbstractItemModel *m_model;
80 QAbstractItemModel *m_model;
81 int m_first;
81 int m_first;
@@ -250,8 +250,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
250 Constructs QBarSet with a label of \a label and with parent of \a parent
250 Constructs QBarSet with a label of \a label and with parent of \a parent
251 */
251 */
252 QBarSet::QBarSet(const QString label, QObject *parent)
252 QBarSet::QBarSet(const QString label, QObject *parent)
253 : QObject(parent)
253 : QObject(parent),
254 ,d_ptr(new QBarSetPrivate(label,this))
254 d_ptr(new QBarSetPrivate(label, this))
255 {
255 {
256 }
256 }
257
257
@@ -321,7 +321,7 QBarSet& QBarSet::operator << (const qreal &value)
321 void QBarSet::insert(const int index, const qreal value)
321 void QBarSet::insert(const int index, const qreal value)
322 {
322 {
323 d_ptr->insert(index, value);
323 d_ptr->insert(index, value);
324 emit valuesAdded(index,1);
324 emit valuesAdded(index, 1);
325 }
325 }
326
326
327 /*!
327 /*!
@@ -330,10 +330,9 void QBarSet::insert(const int index, const qreal value)
330 */
330 */
331 void QBarSet::remove(const int index, const int count)
331 void QBarSet::remove(const int index, const int count)
332 {
332 {
333 int removedCount = d_ptr->remove(index,count);
333 int removedCount = d_ptr->remove(index, count);
334 if (removedCount > 0) {
334 if (removedCount > 0)
335 emit valuesRemoved(index,removedCount);
335 emit valuesRemoved(index, removedCount);
336 }
337 return;
336 return;
338 }
337 }
339
338
@@ -343,7 +342,7 void QBarSet::remove(const int index, const int count)
343 void QBarSet::replace(const int index, const qreal value)
342 void QBarSet::replace(const int index, const qreal value)
344 {
343 {
345 if (index >= 0 && index < d_ptr->m_values.count()) {
344 if (index >= 0 && index < d_ptr->m_values.count()) {
346 d_ptr->replace(index,value);
345 d_ptr->replace(index, value);
347 emit valueChanged(index);
346 emit valueChanged(index);
348 }
347 }
349 }
348 }
@@ -355,10 +354,8 void QBarSet::replace(const int index, const qreal value)
355 */
354 */
356 qreal QBarSet::at(const int index) const
355 qreal QBarSet::at(const int index) const
357 {
356 {
358 if (index < 0 || index >= d_ptr->m_values.count()) {
357 if (index < 0 || index >= d_ptr->m_values.count())
359 return 0;
358 return 0;
360 }
361
362 return d_ptr->m_values.at(index).y();
359 return d_ptr->m_values.at(index).y();
363 }
360 }
364
361
@@ -385,9 +382,8 int QBarSet::count() const
385 qreal QBarSet::sum() const
382 qreal QBarSet::sum() const
386 {
383 {
387 qreal total(0);
384 qreal total(0);
388 for (int i=0; i < d_ptr->m_values.count(); i++) {
385 for (int i = 0; i < d_ptr->m_values.count(); i++)
389 total += d_ptr->m_values.at(i).y();
386 total += d_ptr->m_values.at(i).y();
390 }
391 return total;
387 return total;
392 }
388 }
393
389
@@ -396,11 +392,11 qreal QBarSet::sum() const
396 */
392 */
397 void QBarSet::setPen(const QPen &pen)
393 void QBarSet::setPen(const QPen &pen)
398 {
394 {
399 if(d_ptr->m_pen!=pen){
395 if (d_ptr->m_pen != pen) {
400 d_ptr->m_pen = pen;
396 d_ptr->m_pen = pen;
401 emit d_ptr->updatedBars();
397 emit d_ptr->updatedBars();
402 emit penChanged();
398 emit penChanged();
403 }
399 }
404 }
400 }
405
401
406 /*!
402 /*!
@@ -416,10 +412,10 QPen QBarSet::pen() const
416 */
412 */
417 void QBarSet::setBrush(const QBrush &brush)
413 void QBarSet::setBrush(const QBrush &brush)
418 {
414 {
419 if(d_ptr->m_brush!=brush){
415 if (d_ptr->m_brush != brush) {
420 d_ptr->m_brush = brush;
416 d_ptr->m_brush = brush;
421 emit d_ptr->updatedBars();
417 emit d_ptr->updatedBars();
422 emit brushChanged();
418 emit brushChanged();
423 }
419 }
424 }
420 }
425
421
@@ -436,7 +432,7 QBrush QBarSet::brush() const
436 */
432 */
437 void QBarSet::setLabelBrush(const QBrush &brush)
433 void QBarSet::setLabelBrush(const QBrush &brush)
438 {
434 {
439 if(d_ptr->m_labelBrush!=brush){
435 if (d_ptr->m_labelBrush != brush) {
440 d_ptr->m_labelBrush = brush;
436 d_ptr->m_labelBrush = brush;
441 emit d_ptr->updatedBars();
437 emit d_ptr->updatedBars();
442 emit labelBrushChanged();
438 emit labelBrushChanged();
@@ -456,7 +452,7 QBrush QBarSet::labelBrush() const
456 */
452 */
457 void QBarSet::setLabelFont(const QFont &font)
453 void QBarSet::setLabelFont(const QFont &font)
458 {
454 {
459 if(d_ptr->m_labelFont!=font) {
455 if (d_ptr->m_labelFont != font) {
460 d_ptr->m_labelFont = font;
456 d_ptr->m_labelFont = font;
461 emit d_ptr->updatedBars();
457 emit d_ptr->updatedBars();
462 emit labelFontChanged();
458 emit labelFontChanged();
@@ -533,7 +529,7 QColor QBarSet::labelColor()
533 Sets the color of labels for this barset
529 Sets the color of labels for this barset
534 */
530 */
535 void QBarSet::setLabelColor(QColor color)
531 void QBarSet::setLabelColor(QColor color)
536 {
532 {
537 QBrush b = labelBrush();
533 QBrush b = labelBrush();
538 if (b == QBrush())
534 if (b == QBrush())
539 b.setStyle(Qt::SolidPattern);
535 b.setStyle(Qt::SolidPattern);
@@ -565,17 +561,16 void QBarSetPrivate::append(QPointF value)
565
561
566 void QBarSetPrivate::append(QList<QPointF> values)
562 void QBarSetPrivate::append(QList<QPointF> values)
567 {
563 {
568 for (int i=0; i<values.count(); i++) {
564 for (int i = 0; i < values.count(); i++)
569 m_values.append(values.at(i));
565 m_values.append(values.at(i));
570 }
571 emit restructuredBars();
566 emit restructuredBars();
572 }
567 }
573
568
574 void QBarSetPrivate::append(QList<qreal> values)
569 void QBarSetPrivate::append(QList<qreal> values)
575 {
570 {
576 int index = m_values.count();
571 int index = m_values.count();
577 for (int i=0; i<values.count(); i++) {
572 for (int i = 0; i < values.count(); i++) {
578 m_values.append(QPointF(index,values.at(i)));
573 m_values.append(QPointF(index, values.at(i)));
579 index++;
574 index++;
580 }
575 }
581 emit restructuredBars();
576 emit restructuredBars();
@@ -597,13 +592,10 int QBarSetPrivate::remove(const int index, const int count)
597 {
592 {
598 int removeCount = count;
593 int removeCount = count;
599
594
600 if ((index <0) || (m_values.count() == 0)) {
595 if ((index < 0) || (m_values.count() == 0))
601 // Invalid index or not values in list, remove nothing.
596 return 0; // Invalid index or not values in list, remove nothing.
602 return 0;
597 else if ((index + count) > m_values.count())
603 } else if ((index + count) > m_values.count()) {
598 removeCount = m_values.count() - index; // Trying to remove more items than list has. Limit amount to be removed.
604 // Trying to remove more items than list has. Limit amount to be removed.
605 removeCount = m_values.count() - index;
606 }
607
599
608 int c = 0;
600 int c = 0;
609 while (c < removeCount) {
601 while (c < removeCount) {
@@ -616,31 +608,27 int QBarSetPrivate::remove(const int index, const int count)
616
608
617 void QBarSetPrivate::replace(const int index, const qreal value)
609 void QBarSetPrivate::replace(const int index, const qreal value)
618 {
610 {
619 m_values.replace(index,QPointF(index,value));
611 m_values.replace(index, QPointF(index, value));
620 emit updatedLayout();
612 emit updatedLayout();
621 }
613 }
622
614
623 void QBarSetPrivate::replace(const int index, const QPointF value)
615 void QBarSetPrivate::replace(const int index, const QPointF value)
624 {
616 {
625 m_values.replace(index,value);
617 m_values.replace(index, value);
626 emit updatedLayout();
618 emit updatedLayout();
627 }
619 }
628
620
629 qreal QBarSetPrivate::pos(const int index)
621 qreal QBarSetPrivate::pos(const int index)
630 {
622 {
631 if (index < 0 || index >= m_values.count()) {
623 if (index < 0 || index >= m_values.count())
632 return 0;
624 return 0;
633 }
634
635 return m_values.at(index).x();
625 return m_values.at(index).x();
636 }
626 }
637
627
638 qreal QBarSetPrivate::value(const int index)
628 qreal QBarSetPrivate::value(const int index)
639 {
629 {
640 if (index < 0 || index >= m_values.count()) {
630 if (index < 0 || index >= m_values.count())
641 return 0;
631 return 0;
642 }
643
644 return m_values.at(index).y();
632 return m_values.at(index).y();
645 }
633 }
646
634
@@ -57,7 +57,7 public:
57 void remove(const int index, const int count = 1);
57 void remove(const int index, const int count = 1);
58 void replace(const int index, const qreal value);
58 void replace(const int index, const qreal value);
59 qreal at(const int index) const;
59 qreal at(const int index) const;
60 qreal operator [] (const int index) const;
60 qreal operator [](const int index) const;
61 int count() const;
61 int count() const;
62 qreal sum() const;
62 qreal sum() const;
63
63
@@ -56,8 +56,8 QVector<QRectF> BarChartItem::calculateLayout()
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57
57
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left();
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left();
59 xPos -= setCount*rectWidth/2;
59 xPos -= setCount * rectWidth / 2;
60 xPos += set*rectWidth;
60 xPos += set * rectWidth;
61
61
62 qreal rectHeight = barSet->value(category) * scaleY;
62 qreal rectHeight = barSet->value(category) * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
@@ -66,22 +66,20 QVector<QRectF> BarChartItem::calculateLayout()
66 layout.append(rect);
66 layout.append(rect);
67 bar->setPen(barSet->m_pen);
67 bar->setPen(barSet->m_pen);
68 bar->setBrush(barSet->m_brush);
68 bar->setBrush(barSet->m_brush);
69 if (qFuzzyIsNull(rectHeight)) {
69 if (qFuzzyIsNull(rectHeight))
70 bar->setVisible(false);
70 bar->setVisible(false);
71 } else {
71 else
72 bar->setVisible(barsVisible);
72 bar->setVisible(barsVisible);
73 }
74
73
75 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
74 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
76
75
77 if (!qFuzzyIsNull(barSet->value(category))) {
76 if (!qFuzzyIsNull(barSet->value(category)))
78 label->setText(QString::number(barSet->value(category)));
77 label->setText(QString::number(barSet->value(category)));
79 } else {
78 else
80 label->setText(QString(""));
79 label->setText(QString(""));
81 }
82
80
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
81 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
84 ,yPos - rectHeight/2 - label->boundingRect().height()/2);
82 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
85 label->setFont(barSet->m_labelFont);
83 label->setFont(barSet->m_labelFont);
86 label->setBrush(barSet->m_labelBrush);
84 label->setBrush(barSet->m_labelBrush);
87
85
@@ -79,9 +79,8 QAbstractSeries::SeriesType QBarSeries::type() const
79 QBarSeries::~QBarSeries()
79 QBarSeries::~QBarSeries()
80 {
80 {
81 Q_D(QBarSeries);
81 Q_D(QBarSeries);
82 if(d->m_dataset) {
82 if (d->m_dataset)
83 d->m_dataset->removeSeries(this);
83 d->m_dataset->removeSeries(this);
84 }
85 }
84 }
86 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87
86
@@ -103,7 +102,7 void QBarSeriesPrivate::scaleDomain(Domain& domain)
103 maxX = qMax(maxX, x - (qreal)0.5);
102 maxX = qMax(maxX, x - (qreal)0.5);
104 maxY = qMax(maxY, max());
103 maxY = qMax(maxY, max());
105
104
106 domain.setRange(minX,maxX,minY,maxY);
105 domain.setRange(minX, maxX, minY, maxY);
107 }
106 }
108
107
109
108
@@ -111,10 +110,9 ChartElement* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
111 {
110 {
112 Q_Q(QBarSeries);
111 Q_Q(QBarSeries);
113
112
114 BarChartItem* bar = new BarChartItem(q,presenter);
113 BarChartItem* bar = new BarChartItem(q, presenter);
115 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
114 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
116 bar->setAnimation(new BarAnimation(bar));
115 bar->setAnimation(new BarAnimation(bar));
117 }
118 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
116 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
119 return bar;
117 return bar;
120 }
118 }
@@ -54,28 +54,27 QVector<QRectF> PercentBarChartItem::calculateLayout()
54 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
55 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
56 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
57 for (int set=0; set < setCount; set++) {
57 for (int set = 0; set < setCount; set++) {
58 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
58 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
59
59
60 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth/2;
60 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
61
61
62 qreal rectHeight = barSet->value(category) * percentage * scaleY;
62 qreal rectHeight = barSet->value(category) * percentage * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
64 bar->setPen(barSet->m_pen);
64 bar->setPen(barSet->m_pen);
65 bar->setBrush(barSet->m_brush);
65 bar->setBrush(barSet->m_brush);
66 if (qFuzzyIsNull(rectHeight)) {
66 if (qFuzzyIsNull(rectHeight))
67 bar->setVisible(false);
67 bar->setVisible(false);
68 } else {
68 else
69 bar->setVisible(barsVisible);
69 bar->setVisible(barsVisible);
70 }
71
70
72 QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight);
71 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
73 layout.append(rect);
72 layout.append(rect);
74
73
75 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
74 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
76
75
77 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
76 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) {
78 int p = m_series->d_func()->percentageAt(set,category) * 100;
77 int p = m_series->d_func()->percentageAt(set, category) * 100;
79 QString vString(QString::number(p));
78 QString vString(QString::number(p));
80 vString.truncate(3);
79 vString.truncate(3);
81 vString.append("%");
80 vString.append("%");
@@ -84,8 +83,8 QVector<QRectF> PercentBarChartItem::calculateLayout()
84 label->setText(QString(""));
83 label->setText(QString(""));
85 }
84 }
86
85
87 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
86 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
88 ,yPos - rectHeight/2 - label->boundingRect().height()/2);
87 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
89 label->setFont(barSet->m_labelFont);
88 label->setFont(barSet->m_labelFont);
90 label->setBrush(barSet->m_labelBrush);
89 label->setBrush(barSet->m_labelBrush);
91
90
@@ -69,9 +69,8 QPercentBarSeries::QPercentBarSeries(QObject *parent)
69 QPercentBarSeries::~QPercentBarSeries()
69 QPercentBarSeries::~QPercentBarSeries()
70 {
70 {
71 Q_D(QPercentBarSeries);
71 Q_D(QPercentBarSeries);
72 if(d->m_dataset) {
72 if (d->m_dataset)
73 d->m_dataset->removeSeries(this);
73 d->m_dataset->removeSeries(this);
74 }
75 }
74 }
76
75
77 /*!
76 /*!
@@ -102,7 +101,7 void QPercentBarSeriesPrivate::scaleDomain(Domain& domain)
102 minY = 0;
101 minY = 0;
103 maxY = 100;
102 maxY = 100;
104
103
105 domain.setRange(minX,maxX,minY,maxY);
104 domain.setRange(minX, maxX, minY, maxY);
106 }
105 }
107
106
108
107
@@ -110,10 +109,9 ChartElement* QPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter
110 {
109 {
111 Q_Q(QPercentBarSeries);
110 Q_Q(QPercentBarSeries);
112
111
113 PercentBarChartItem* bar = new PercentBarChartItem(q,presenter);
112 PercentBarChartItem* bar = new PercentBarChartItem(q, presenter);
114 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
113 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
115 bar->setAnimation(new PercentBarAnimation(bar));
114 bar->setAnimation(new PercentBarAnimation(bar));
116 }
117 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
115 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
118 return bar;
116 return bar;
119 }
117 }
@@ -70,9 +70,8 QStackedBarSeries::QStackedBarSeries(QObject *parent)
70 QStackedBarSeries::~QStackedBarSeries()
70 QStackedBarSeries::~QStackedBarSeries()
71 {
71 {
72 Q_D(QStackedBarSeries);
72 Q_D(QStackedBarSeries);
73 if(d->m_dataset) {
73 if (d->m_dataset)
74 d->m_dataset->removeSeries(this);
74 d->m_dataset->removeSeries(this);
75 }
76 }
75 }
77 /*!
76 /*!
78 Returns QChartSeries::SeriesTypeStackedBar.
77 Returns QChartSeries::SeriesTypeStackedBar.
@@ -102,17 +101,16 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
102 maxX = qMax(maxX, x - (qreal)0.5);
101 maxX = qMax(maxX, x - (qreal)0.5);
103 maxY = qMax(maxY, top());
102 maxY = qMax(maxY, top());
104
103
105 domain.setRange(minX,maxX,minY,maxY);
104 domain.setRange(minX, maxX, minY, maxY);
106 }
105 }
107
106
108 ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
107 ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
109 {
108 {
110 Q_Q(QStackedBarSeries);
109 Q_Q(QStackedBarSeries);
111
110
112 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
111 StackedBarChartItem* bar = new StackedBarChartItem(q, presenter);
113 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
112 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
114 bar->setAnimation(new StackedBarAnimation(bar));
113 bar->setAnimation(new StackedBarAnimation(bar));
115 }
116 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
114 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
117 return bar;
115 return bar;
118 }
116 }
@@ -52,42 +52,41 QVector<QRectF> StackedBarChartItem::calculateLayout()
52 for (int category = 0; category < categoryCount; category++) {
52 for (int category = 0; category < categoryCount; category++) {
53 qreal yMax = height + scaleY * m_domainMinY + geometry().top();
53 qreal yMax = height + scaleY * m_domainMinY + geometry().top();
54 qreal yMin = height + scaleY * m_domainMinY + geometry().top();
54 qreal yMin = height + scaleY * m_domainMinY + geometry().top();
55 for (int set=0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57
57
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth/2;
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
59
59
60 qreal rectHeight = barSet->value(category) * scaleY;
60 qreal rectHeight = barSet->value(category) * scaleY;
61 Bar* bar = m_bars.at(itemIndex);
61 Bar* bar = m_bars.at(itemIndex);
62 bar->setPen(barSet->m_pen);
62 bar->setPen(barSet->m_pen);
63 bar->setBrush(barSet->m_brush);
63 bar->setBrush(barSet->m_brush);
64 if (qFuzzyIsNull(rectHeight)) {
64 if (qFuzzyIsNull(rectHeight))
65 bar->setVisible(false);
65 bar->setVisible(false);
66 } else {
66 else
67 bar->setVisible(barsVisible);
67 bar->setVisible(barsVisible);
68 }
69
68
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71
70
72 if (!qFuzzyIsNull(barSet->value(category))) {
71 if (!qFuzzyIsNull(barSet->value(category)))
73 label->setText(QString::number(barSet->value(category)));
72 label->setText(QString::number(barSet->value(category)));
74 } else {
73 else
75 label->setText(QString(""));
74 label->setText(QString(""));
76 }
75
77 label->setFont(barSet->m_labelFont);
76 label->setFont(barSet->m_labelFont);
78 label->setBrush(barSet->m_labelBrush);
77 label->setBrush(barSet->m_labelBrush);
79
78
80 if (rectHeight < 0) {
79 if (rectHeight < 0) {
81 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
80 QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight);
82 layout.append(rect);
81 layout.append(rect);
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
82 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
84 ,yMax - rectHeight/2 - label->boundingRect().height()/2);
83 yMax - rectHeight / 2 - label->boundingRect().height() / 2);
85 yMax -= rectHeight;
84 yMax -= rectHeight;
86 } else {
85 } else {
87 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
86 QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight);
88 layout.append(rect);
87 layout.append(rect);
89 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
88 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
90 ,yMin - rectHeight/2 - label->boundingRect().height()/2);
89 yMin - rectHeight / 2 - label->boundingRect().height() / 2);
91 yMin -= rectHeight;
90 yMin -= rectHeight;
92 }
91 }
93
92
@@ -64,27 +64,28 void ChartBackground::setDropShadowEnabled(bool enabled)
64
64
65 void ChartBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
65 void ChartBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 {
66 {
67 Q_UNUSED(option);
67 Q_UNUSED(option);
68 Q_UNUSED(widget);
68 Q_UNUSED(widget);
69 painter->save();
69 painter->save();
70 painter->setPen(pen());
70 painter->setPen(pen());
71 painter->setBrush(brush());
71 painter->setBrush(brush());
72 painter->drawRoundRect(rect(),roundness(rect().width()),roundness(rect().height()));
72 painter->drawRoundRect(rect(), roundness(rect().width()), roundness(rect().height()));
73 #ifndef QT_NO_DEBUG
73 #ifndef QT_NO_DEBUG
74 painter->setPen(Qt::black);
74 painter->setPen(Qt::black);
75 QFont font;
75 QFont font;
76 QString build("build %1");
76 QString build("build %1");
77 font.setPointSize(6);
77 font.setPointSize(6);
78 painter->setFont(font);
78 painter->setFont(font);
79 painter->drawText(rect().bottomLeft(),build.arg(ChartConfig::instance()->compilationTime()));
79 painter->drawText(rect().bottomLeft(), build.arg(ChartConfig::instance()->compilationTime()));
80 #endif
80 #endif
81 painter->restore();
81 painter->restore();
82 }
82 }
83
83
84 int ChartBackground::roundness(qreal size) const
84 int ChartBackground::roundness(qreal size) const
85 {
85 {
86 if(qFuzzyIsNull(size)) return 0;
86 if (qFuzzyIsNull(size))
87 return 100*m_diameter/int(size);
87 return 0;
88 return 100 * m_diameter / int(size);
88 }
89 }
89
90
90 int ChartBackground::diameter() const
91 int ChartBackground::diameter() const
@@ -92,9 +93,9 int ChartBackground::diameter() const
92 return m_diameter;
93 return m_diameter;
93 }
94 }
94
95
95 void ChartBackground::setDimeter(int dimater)
96 void ChartBackground::setDiameter(int diameter)
96 {
97 {
97 m_diameter=dimater;
98 m_diameter = diameter;
98 }
99 }
99
100
100 QTCOMMERCIALCHART_END_NAMESPACE
101 QTCOMMERCIALCHART_END_NAMESPACE
@@ -39,13 +39,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 class ChartBackground: public QGraphicsRectItem
39 class ChartBackground: public QGraphicsRectItem
40 {
40 {
41 public:
41 public:
42 ChartBackground(QGraphicsItem *parent =0);
42 ChartBackground(QGraphicsItem *parent = 0);
43 ~ChartBackground();
43 ~ChartBackground();
44
44
45 void setDimeter(int dimater);
45 void setDiameter(int diameter);
46 int diameter() const;
46 int diameter() const;
47 void setDropShadowEnabled(bool enabled);
47 void setDropShadowEnabled(bool enabled);
48 bool isDropShadowEnabled() {return m_dropShadow != 0;}
48 bool isDropShadowEnabled() { return m_dropShadow != 0; }
49
49
50 protected:
50 protected:
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
@@ -48,29 +48,29 const char *gitHead = "unknown";
48
48
49 QTCOMMERCIALCHART_BEGIN_NAMESPACE
49 QTCOMMERCIALCHART_BEGIN_NAMESPACE
50
50
51 class ChartConfig {
51 class ChartConfig
52
52 {
53 private:
53 private:
54 ChartConfig(){
54 ChartConfig() {
55 #if defined(DEVELOPMENT_BUILD) && !defined(QT_NO_DEBUG)
55 #if defined(DEVELOPMENT_BUILD) && !defined(QT_NO_DEBUG)
56 qDebug()<<"buildTime" << buildTime;
56 qDebug() << "buildTime" << buildTime;
57 qDebug()<<"gitHead" << gitHead;
57 qDebug() << "gitHead" << gitHead;
58 #endif
58 #endif
59 m_instance = this;
59 m_instance = this;
60 }
60 }
61 public:
61 public:
62 static ChartConfig* instance(){
62 static ChartConfig* instance() {
63 if(!m_instance){
63 if (!m_instance) {
64 m_instance= new ChartConfig();
64 m_instance = new ChartConfig();
65 }
65 }
66 return m_instance;
66 return m_instance;
67 }
67 }
68
68
69 QString compilationTime(){
69 QString compilationTime() {
70 return buildTime;
70 return buildTime;
71 }
71 }
72
72
73 QString compilationHead(){
73 QString compilationHead() {
74 return gitHead;
74 return gitHead;
75 }
75 }
76
76
@@ -79,7 +79,7 private:
79 };
79 };
80
80
81
81
82 ChartConfig* ChartConfig::m_instance=0;
82 ChartConfig* ChartConfig::m_instance = 0;
83
83
84 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
85
85
@@ -31,12 +31,12
31 #include "qpieseries.h"
31 #include "qpieseries.h"
32
32
33 #ifndef QT_ON_ARM
33 #ifndef QT_ON_ARM
34 #include "qdatetimeaxis.h"
34 #include "qdatetimeaxis.h"
35 #endif
35 #endif
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 ChartDataSet::ChartDataSet(QChart *parent):QObject(parent)
39 ChartDataSet::ChartDataSet(QChart *parent): QObject(parent)
40 {
40 {
41
41
42 }
42 }
@@ -50,13 +50,13 void ChartDataSet::addSeries(QAbstractSeries* series)
50 {
50 {
51 Domain* domain = m_seriesDomainMap.value(series);
51 Domain* domain = m_seriesDomainMap.value(series);
52
52
53 if(domain) {
53 if (domain) {
54 qWarning() << "Can not add series. Series already on the chart";
54 qWarning() << "Can not add series. Series already on the chart";
55 return;
55 return;
56 }
56 }
57
57
58 domain = new Domain(series);
58 domain = new Domain(series);
59 m_seriesDomainMap.insert(series,domain);
59 m_seriesDomainMap.insert(series, domain);
60 series->d_ptr->scaleDomain(*domain);
60 series->d_ptr->scaleDomain(*domain);
61
61
62 createSeriesIndex(series);
62 createSeriesIndex(series);
@@ -65,15 +65,15 void ChartDataSet::addSeries(QAbstractSeries* series)
65 series->d_ptr->m_chart = qobject_cast<QChart*>(parent());
65 series->d_ptr->m_chart = qobject_cast<QChart*>(parent());
66 series->d_ptr->m_dataset = this;
66 series->d_ptr->m_dataset = this;
67
67
68 emit seriesAdded(series,domain);
68 emit seriesAdded(series, domain);
69
69
70 }
70 }
71
71
72 void ChartDataSet::removeSeries(QAbstractSeries* series)
72 void ChartDataSet::removeSeries(QAbstractSeries* series)
73 {
73 {
74
74
75 if(!m_seriesDomainMap.contains(series)) {
75 if (!m_seriesDomainMap.contains(series)) {
76 qWarning()<<"Can not remove series. Series not found on the chart.";
76 qWarning() << "Can not remove series. Series not found on the chart.";
77 return;
77 return;
78 }
78 }
79
79
@@ -98,22 +98,21 void ChartDataSet::createSeriesIndex(QAbstractSeries* series)
98 {
98 {
99 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
99 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
100
100
101 int key=0;
101 int key = 0;
102 while (i.hasNext()) {
102 while (i.hasNext()) {
103 i.next();
103 i.next();
104 if(i.key()!=key) {
104 if (i.key() != key)
105 break;
105 break;
106 }
107 key++;
106 key++;
108 }
107 }
109
108
110 m_indexSeriesMap.insert(key,series);
109 m_indexSeriesMap.insert(key, series);
111 }
110 }
112
111
113 void ChartDataSet::removeSeriesIndex(QAbstractSeries* series)
112 void ChartDataSet::removeSeriesIndex(QAbstractSeries* series)
114 {
113 {
115 int key = seriesIndex(series);
114 int key = seriesIndex(series);
116 Q_ASSERT(key!=-1);
115 Q_ASSERT(key != -1);
117 m_indexSeriesMap.remove(key);
116 m_indexSeriesMap.remove(key);
118 }
117 }
119
118
@@ -137,13 +136,16 void ChartDataSet::createDefaultAxes()
137 // Select the required axis x and axis y types based on the types of the current series
136 // Select the required axis x and axis y types based on the types of the current series
138 while (i.hasNext()) {
137 while (i.hasNext()) {
139 i.next();
138 i.next();
140
141 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
139 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
142 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
140 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
143 if(axisX) typeX&=axisX->type();
141 if (axisX)
144 else typeX|=i.key()->d_ptr->defaultAxisType(Qt::Horizontal);
142 typeX &= axisX->type();
145 if(axisY) typeY&=axisY->type();
143 else
146 else typeY|=i.key()->d_ptr->defaultAxisType(Qt::Vertical);
144 typeX |= i.key()->d_ptr->defaultAxisType(Qt::Horizontal);
145 if (axisY)
146 typeY &= axisY->type();
147 else
148 typeY |= i.key()->d_ptr->defaultAxisType(Qt::Vertical);
147 }
149 }
148
150
149 // Create the axes of the types selected
151 // Create the axes of the types selected
@@ -170,9 +172,9 void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation ori
170 i.toFront();
172 i.toFront();
171 while (i.hasNext()) {
173 while (i.hasNext()) {
172 i.next();
174 i.next();
173 initializeAxis(axis,i.key());
175 initializeAxis(axis, i.key());
174 }
176 }
175 emit axisAdded(axis,i.value());
177 emit axisAdded(axis, i.value());
176 }
178 }
177 }
179 }
178
180
@@ -180,7 +182,7 QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type, Qt::Orient
180 {
182 {
181 QAbstractAxis* axis = 0;
183 QAbstractAxis* axis = 0;
182
184
183 switch(type) {
185 switch (type) {
184 case QAbstractAxis::AxisTypeValue:
186 case QAbstractAxis::AxisTypeValue:
185 axis = new QValueAxis(this);
187 axis = new QValueAxis(this);
186 break;
188 break;
@@ -200,27 +202,26 QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type, Qt::Orient
200 break;
202 break;
201 }
203 }
202
204
203 if(axis)
205 if (axis)
204 axis->d_ptr->setOrientation(orientation);
206 axis->d_ptr->setOrientation(orientation);
205
207
206 return axis;
208 return axis;
207 }
209 }
208
210
209 void ChartDataSet::initializeAxis(QAbstractAxis* axis,QAbstractSeries* series)
211 void ChartDataSet::initializeAxis(QAbstractAxis* axis, QAbstractSeries* series)
210 {
212 {
211 Domain* domain = m_seriesDomainMap.value(series);
213 Domain* domain = m_seriesDomainMap.value(series);
212 axis->d_ptr->m_dataset = this;
214 axis->d_ptr->m_dataset = this;
213 series->d_ptr->initializeAxis(axis);
215 series->d_ptr->initializeAxis(axis);
214 axis->d_ptr->intializeDomain(domain);
216 axis->d_ptr->intializeDomain(domain);
215 if(axis->orientation()==Qt::Horizontal) {
217 if (axis->orientation() == Qt::Horizontal) {
216 QObject::connect(axis->d_ptr.data(),SIGNAL(updated()),domain,SLOT(handleAxisUpdated()));
218 QObject::connect(axis->d_ptr.data(), SIGNAL(updated()), domain, SLOT(handleAxisUpdated()));
217 QObject::connect(domain,SIGNAL(updated()),axis->d_ptr.data(),SLOT(handleDomainUpdated()));
219 QObject::connect(domain, SIGNAL(updated()), axis->d_ptr.data(), SLOT(handleDomainUpdated()));
218 m_seriesAxisXMap.insert(series,axis);
220 m_seriesAxisXMap.insert(series, axis);
219 }
221 } else {
220 else {
222 QObject::connect(axis->d_ptr.data(), SIGNAL(updated()), domain, SLOT(handleAxisUpdated()));
221 QObject::connect(axis->d_ptr.data(),SIGNAL(updated()),domain,SLOT(handleAxisUpdated()));
223 QObject::connect(domain, SIGNAL(updated()), axis->d_ptr.data(), SLOT(handleDomainUpdated()));
222 QObject::connect(domain,SIGNAL(updated()),axis->d_ptr.data(),SLOT(handleDomainUpdated()));
224 m_seriesAxisYMap.insert(series, axis);
223 m_seriesAxisYMap.insert(series,axis);
224 }
225 }
225 axis->d_ptr->emitUpdated();
226 axis->d_ptr->emitUpdated();
226 }
227 }
@@ -229,27 +230,27 void ChartDataSet::removeAxes(QAbstractSeries* series)
229 {
230 {
230 QAbstractAxis* axisX = m_seriesAxisXMap.take(series);
231 QAbstractAxis* axisX = m_seriesAxisXMap.take(series);
231
232
232 if(axisX) {
233 if (axisX) {
233 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
234 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
234 int x = axesX.indexOf(axisX);
235 int x = axesX.indexOf(axisX);
235
236
236 if(x==-1) {
237 if (x == -1) {
237 emit axisRemoved(axisX);
238 emit axisRemoved(axisX);
238 axisX->d_ptr->m_dataset=0;
239 axisX->d_ptr->m_dataset = 0;
239 axisX->deleteLater();
240 axisX->deleteLater();
240 }
241 }
241 }
242 }
242
243
243 QAbstractAxis* axisY = m_seriesAxisYMap.take(series);
244 QAbstractAxis* axisY = m_seriesAxisYMap.take(series);
244
245
245 if(axisY) {
246 if (axisY) {
246 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
247 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
247
248
248 int y = axesY.indexOf(axisY);
249 int y = axesY.indexOf(axisY);
249
250
250 if(y==-1) {
251 if (y == -1) {
251 emit axisRemoved(axisY);
252 emit axisRemoved(axisY);
252 axisY->d_ptr->m_dataset=0;
253 axisY->d_ptr->m_dataset = 0;
253 axisY->deleteLater();
254 axisY->deleteLater();
254 }
255 }
255 }
256 }
@@ -257,40 +258,36 void ChartDataSet::removeAxes(QAbstractSeries* series)
257
258
258 void ChartDataSet::removeAxis(QAbstractAxis* axis)
259 void ChartDataSet::removeAxis(QAbstractAxis* axis)
259 {
260 {
260 if(!axis->d_ptr->m_dataset) {
261 if (!axis->d_ptr->m_dataset) {
261 qWarning()<<"UnBound axis found !";
262 qWarning() << "UnBound axis found !";
262 return;
263 return;
263 }
264 }
264
265
265 QMap<QAbstractSeries*, QAbstractAxis*> *seriesAxisMap;
266 QMap<QAbstractSeries*, QAbstractAxis*> *seriesAxisMap;
266
267
267 if(axis->orientation()==Qt::Vertical) {
268 if (axis->orientation() == Qt::Vertical)
268 seriesAxisMap= &m_seriesAxisYMap;
269 seriesAxisMap = &m_seriesAxisYMap;
269 }
270 else
270 else {
271 seriesAxisMap = &m_seriesAxisXMap;
271 seriesAxisMap= &m_seriesAxisXMap;
272 }
273
272
274 QMapIterator<QAbstractSeries*, QAbstractAxis*> i(*seriesAxisMap);
273 QMapIterator<QAbstractSeries*, QAbstractAxis*> i(*seriesAxisMap);
275
274
276 while (i.hasNext()) {
275 while (i.hasNext()) {
277 i.next();
276 i.next();
278 if(i.value()==axis) {
277 if (i.value() == axis)
279 removeSeries(i.key());
278 removeSeries(i.key());
280 }
281 }
279 }
282 }
280 }
283
281
284 void ChartDataSet::removeAllSeries()
282 void ChartDataSet::removeAllSeries()
285 {
283 {
286 QList<QAbstractSeries*> series = m_seriesDomainMap.keys();
284 QList<QAbstractSeries*> series = m_seriesDomainMap.keys();
287 foreach(QAbstractSeries *s , series) {
285 foreach (QAbstractSeries *s , series)
288 removeSeries(s);
286 removeSeries(s);
289 }
290
287
291 Q_ASSERT(m_seriesAxisXMap.count()==0);
288 Q_ASSERT(m_seriesAxisXMap.count() == 0);
292 Q_ASSERT(m_seriesAxisXMap.count()==0);
289 Q_ASSERT(m_seriesAxisXMap.count() == 0);
293 Q_ASSERT(m_seriesDomainMap.count()==0);
290 Q_ASSERT(m_seriesDomainMap.count() == 0);
294
291
295 qDeleteAll(series);
292 qDeleteAll(series);
296 }
293 }
@@ -306,7 +303,7 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
306
303
307 while (i.hasNext()) {
304 while (i.hasNext()) {
308 i.next();
305 i.next();
309 i.value()->zoomIn(rect,size);
306 i.value()->zoomIn(rect, size);
310 }
307 }
311
308
312 blockAxisSignals(false);
309 blockAxisSignals(false);
@@ -323,7 +320,7 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
323
320
324 while (i.hasNext()) {
321 while (i.hasNext()) {
325 i.next();
322 i.next();
326 i.value()->zoomOut(rect,size);
323 i.value()->zoomOut(rect, size);
327 }
324 }
328
325
329 blockAxisSignals(false);
326 blockAxisSignals(false);
@@ -336,16 +333,16 void ChartDataSet::blockAxisSignals(bool enabled)
336 i.next();
333 i.next();
337 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
334 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
338 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
335 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
339 if(axisX) {
336 if (axisX) {
340 axisX->d_ptr->blockSignals(enabled);
337 axisX->d_ptr->blockSignals(enabled);
341 if(!enabled) {
338 if (!enabled) {
342 axisX->d_ptr->setDirty(false);
339 axisX->d_ptr->setDirty(false);
343 axisX->d_ptr->emitUpdated();
340 axisX->d_ptr->emitUpdated();
344 }
341 }
345 }
342 }
346 if(axisY) {
343 if (axisY) {
347 axisY->d_ptr->blockSignals(enabled);
344 axisY->d_ptr->blockSignals(enabled);
348 if(!enabled) {
345 if (!enabled) {
349 axisY->d_ptr->setDirty(false);
346 axisY->d_ptr->setDirty(false);
350 axisY->d_ptr->emitUpdated();
347 axisY->d_ptr->emitUpdated();
351 }
348 }
@@ -355,11 +352,12 void ChartDataSet::blockAxisSignals(bool enabled)
355
352
356 int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type)
353 int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type)
357 {
354 {
358 int count=0;
355 int count = 0;
359 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
356 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
360 while (i.hasNext()) {
357 while (i.hasNext()) {
361 i.next();
358 i.next();
362 if(i.key()->type()==type) count++;
359 if (i.key()->type() == type)
360 count++;
363 }
361 }
364 return count;
362 return count;
365 }
363 }
@@ -377,13 +375,14 int ChartDataSet::seriesIndex(QAbstractSeries *series)
377
375
378 QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const
376 QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const
379 {
377 {
380 if(series == 0) {
378 if (series == 0) {
381
379
382 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisXMap);
380 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisXMap);
383
381
384 while (i.hasNext()) {
382 while (i.hasNext()) {
385 i.next();
383 i.next();
386 if(i.value()->isVisible()) return i.value();
384 if (i.value()->isVisible())
385 return i.value();
387 }
386 }
388 return 0;
387 return 0;
389 }
388 }
@@ -392,12 +391,12 QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const
392
391
393 QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const
392 QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const
394 {
393 {
395 if(series == 0) {
394 if (series == 0) {
396 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisYMap);
395 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisYMap);
397
396
398 while (i.hasNext()) {
397 while (i.hasNext()) {
399 i.next();
398 i.next();
400 if(i.value()->isVisible()) return i.value();
399 if (i.value()->isVisible()) return i.value();
401 }
400 }
402 return 0;
401 return 0;
403 }
402 }
@@ -408,25 +407,25 void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Ori
408 {
407 {
409 Q_ASSERT(axis);
408 Q_ASSERT(axis);
410
409
411 if(!series) {
410 if (!series) {
412 qWarning() << "Series not found on the chart.";
411 qWarning() << "Series not found on the chart.";
413 return;
412 return;
414 }
413 }
415
414
416 Domain* domain = m_seriesDomainMap.value(series);
415 Domain* domain = m_seriesDomainMap.value(series);
417
416
418 if(!domain) {
417 if (!domain) {
419 qWarning() << "Series not found on the chart.";
418 qWarning() << "Series not found on the chart.";
420 return;
419 return;
421 }
420 }
422
421
423 if(orientation==Qt::Horizontal && axis->orientation()==Qt::Vertical) {
422 if (orientation == Qt::Horizontal && axis->orientation() == Qt::Vertical) {
424 qWarning()<<"Axis already defined as axis Y";
423 qWarning() << "Axis already defined as axis Y";
425 return;
424 return;
426 }
425 }
427
426
428 if(orientation==Qt::Vertical && axis->orientation()==Qt::Horizontal) {
427 if (orientation == Qt::Vertical && axis->orientation() == Qt::Horizontal) {
429 qWarning()<<"Axis already defined as axis X";
428 qWarning() << "Axis already defined as axis X";
430 return;
429 return;
431 }
430 }
432
431
@@ -434,11 +433,10 void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Ori
434
433
435 QMap<QAbstractSeries*, QAbstractAxis*> *seriesAxisMap;
434 QMap<QAbstractSeries*, QAbstractAxis*> *seriesAxisMap;
436
435
437 if(orientation==Qt::Vertical) {
436 if (orientation == Qt::Vertical)
438 seriesAxisMap= &m_seriesAxisYMap;
437 seriesAxisMap = &m_seriesAxisYMap;
439 }else{
438 else
440 seriesAxisMap= &m_seriesAxisXMap;
439 seriesAxisMap = &m_seriesAxisXMap;
441 }
442
440
443 if (seriesAxisMap->value(series) == axis) {
441 if (seriesAxisMap->value(series) == axis) {
444 qWarning() << "The axis already set for the series";
442 qWarning() << "The axis already set for the series";
@@ -447,21 +445,21 void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Ori
447
445
448 QAbstractAxis *oldAxis = seriesAxisMap->take(series);
446 QAbstractAxis *oldAxis = seriesAxisMap->take(series);
449 QList<QAbstractAxis*> axes = seriesAxisMap->values();
447 QList<QAbstractAxis*> axes = seriesAxisMap->values();
450 if(oldAxis) {
448 if (oldAxis) {
451 if(axes.indexOf(oldAxis)==-1) {
449 if (axes.indexOf(oldAxis) == -1) {
452 emit axisRemoved(oldAxis);
450 emit axisRemoved(oldAxis);
453 oldAxis->disconnect();
451 oldAxis->disconnect();
454 QObject::disconnect(domain,0,oldAxis,0);
452 QObject::disconnect(domain, 0, oldAxis, 0);
455 oldAxis->d_ptr->m_dataset=0;
453 oldAxis->d_ptr->m_dataset = 0;
456 oldAxis->deleteLater();
454 oldAxis->deleteLater();
457 }
455 }
458 }
456 }
459
457
460 if(axes.indexOf(axis)==-1) {
458 if (axes.indexOf(axis) == -1) {
461 initializeAxis(axis,series);
459 initializeAxis(axis, series);
462 emit axisAdded(axis,domain);
460 emit axisAdded(axis, domain);
463 }else{
461 } else {
464 initializeAxis(axis,series);
462 initializeAxis(axis, series);
465 }
463 }
466 }
464 }
467
465
@@ -470,13 +468,13 Domain* ChartDataSet::domain(QAbstractSeries *series) const
470 return m_seriesDomainMap.value(series);
468 return m_seriesDomainMap.value(series);
471 }
469 }
472
470
473 void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size)
471 void ChartDataSet::scrollDomain(qreal dx, qreal dy, const QSizeF& size)
474 {
472 {
475 blockAxisSignals(true);
473 blockAxisSignals(true);
476 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
474 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
477 while (i.hasNext()) {
475 while (i.hasNext()) {
478 i.next();
476 i.next();
479 i.value()->move(dx,dy,size);
477 i.value()->move(dx, dy, size);
480 }
478 }
481 blockAxisSignals(false);
479 blockAxisSignals(false);
482 }
480 }
@@ -43,7 +43,7 class QTCOMMERCIALCHART_AUTOTEST_EXPORT ChartDataSet : public QObject
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 public:
45 public:
46 ChartDataSet(QChart* parent=0);
46 ChartDataSet(QChart* parent = 0);
47 virtual ~ChartDataSet();
47 virtual ~ChartDataSet();
48
48
49 void addSeries(QAbstractSeries* series);
49 void addSeries(QAbstractSeries* series);
@@ -53,7 +53,7 public:
53
53
54 void zoomInDomain(const QRectF& rect, const QSizeF& size);
54 void zoomInDomain(const QRectF& rect, const QSizeF& size);
55 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
55 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
56 void scrollDomain(qreal dx,qreal dy,const QSizeF& size);
56 void scrollDomain(qreal dx, qreal dy, const QSizeF& size);
57
57
58 int seriesCount(QAbstractSeries::SeriesType type);
58 int seriesCount(QAbstractSeries::SeriesType type);
59 int seriesIndex(QAbstractSeries *series);
59 int seriesIndex(QAbstractSeries *series);
@@ -73,14 +73,14 Q_SIGNALS:
73 void seriesAdded(QAbstractSeries* series, Domain* domain);
73 void seriesAdded(QAbstractSeries* series, Domain* domain);
74 void seriesRemoved(QAbstractSeries* series);
74 void seriesRemoved(QAbstractSeries* series);
75 // void seriesUpdated(QAbstractSeries* series);
75 // void seriesUpdated(QAbstractSeries* series);
76 void axisAdded(QAbstractAxis* axis,Domain* domain);
76 void axisAdded(QAbstractAxis* axis, Domain* domain);
77 void axisRemoved(QAbstractAxis* axis);
77 void axisRemoved(QAbstractAxis* axis);
78
78
79 private:
79 private:
80 void calculateDomain(QAbstractSeries* series,Domain* domain);
80 void calculateDomain(QAbstractSeries* series, Domain* domain);
81 void createAxes(QAbstractAxis::AxisTypes type,Qt::Orientation orientation);
81 void createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation orientation);
82 QAbstractAxis* createAxis(QAbstractAxis::AxisType type,Qt::Orientation orientation);
82 QAbstractAxis* createAxis(QAbstractAxis::AxisType type, Qt::Orientation orientation);
83 void initializeAxis(QAbstractAxis* axis,QAbstractSeries* series);
83 void initializeAxis(QAbstractAxis* axis, QAbstractSeries* series);
84 void removeAxes(QAbstractSeries* series);
84 void removeAxes(QAbstractSeries* series);
85 void blockAxisSignals(bool enabled);
85 void blockAxisSignals(bool enabled);
86 void createSeriesIndex(QAbstractSeries* series);
86 void createSeriesIndex(QAbstractSeries* series);
@@ -24,15 +24,16
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 ChartElement::ChartElement(ChartPresenter *presenter):QObject(presenter),
27 ChartElement::ChartElement(ChartPresenter *presenter)
28 m_presenter(presenter),
28 : QObject(presenter),
29 m_domain(0)
29 m_presenter(presenter),
30 m_domain(0)
30 {
31 {
31 }
32 }
32
33
33 void ChartElement::setPresenter(ChartPresenter *presenter)
34 void ChartElement::setPresenter(ChartPresenter *presenter)
34 {
35 {
35 m_presenter=presenter;
36 m_presenter = presenter;
36 }
37 }
37
38
38 ChartPresenter* ChartElement::presenter() const
39 ChartPresenter* ChartElement::presenter() const
@@ -42,7 +43,7 ChartPresenter* ChartElement::presenter() const
42
43
43 void ChartElement::setDomain(Domain *domain)
44 void ChartElement::setDomain(Domain *domain)
44 {
45 {
45 m_domain=domain;
46 m_domain = domain;
46 }
47 }
47
48
48 Domain* ChartElement::domain() const
49 Domain* ChartElement::domain() const
@@ -53,21 +54,21 Domain* ChartElement::domain() const
53 void ChartElement::handleGeometryChanged(const QRectF& rect)
54 void ChartElement::handleGeometryChanged(const QRectF& rect)
54 {
55 {
55 Q_UNUSED(rect);
56 Q_UNUSED(rect);
56 qWarning()<<"Slot not implemented";
57 qWarning() << "Slot not implemented";
57 }
58 }
58
59
59 void ChartElement::handleDomainChanged(qreal minX,qreal maxX,qreal minY,qreal maxY)
60 void ChartElement::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
60 {
61 {
61 Q_UNUSED(minX);
62 Q_UNUSED(minX);
62 Q_UNUSED(maxX);
63 Q_UNUSED(maxX);
63 Q_UNUSED(minY);
64 Q_UNUSED(minY);
64 Q_UNUSED(maxY);
65 Q_UNUSED(maxY);
65 qWarning()<<"Slot not implemented";
66 qWarning() << "Slot not implemented";
66 }
67 }
67
68
68 void ChartElement::handleDomainUpdated()
69 void ChartElement::handleDomainUpdated()
69 {
70 {
70 qWarning()<<"Slot not implemented";
71 qWarning() << "Slot not implemented";
71 }
72 }
72
73
73
74
@@ -49,7 +49,7 public:
49
49
50 public Q_SLOTS:
50 public Q_SLOTS:
51 virtual void handleGeometryChanged(const QRectF& rect);
51 virtual void handleGeometryChanged(const QRectF& rect);
52 virtual void handleDomainChanged(qreal minX,qreal maxX,qreal minY,qreal maxY);
52 virtual void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
53 virtual void handleDomainUpdated();
53 virtual void handleDomainUpdated();
54 virtual ChartAnimation* animation() const { return 0; };
54 virtual ChartAnimation* animation() const { return 0; };
55
55
@@ -38,7 +38,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class ChartItem : public QGraphicsItem, public ChartElement
39 class ChartItem : public QGraphicsItem, public ChartElement
40 {
40 {
41 enum ChartItemTypes{ AXIS_ITEM = UserType + 1, XYLINE_ITEM };
41 enum ChartItemTypes { AXIS_ITEM = UserType + 1, XYLINE_ITEM };
42 public:
42 public:
43 ChartItem(ChartPresenter *presenter) : QGraphicsItem(presenter ? presenter->rootItem() : 0), ChartElement(presenter) {}
43 ChartItem(ChartPresenter *presenter) : QGraphicsItem(presenter ? presenter->rootItem() : 0), ChartElement(presenter) {}
44 };
44 };
@@ -30,10 +30,10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 static const qreal golden_ratio = 0.25;
31 static const qreal golden_ratio = 0.25;
32
32
33 ChartLayout::ChartLayout(ChartPresenter* presenter):
33 ChartLayout::ChartLayout(ChartPresenter* presenter)
34 m_presenter(presenter),
34 : m_presenter(presenter),
35 m_margins(20,20,20,20),
35 m_margins(20, 20, 20, 20),
36 m_minChartRect(0,0,200,200)
36 m_minChartRect(0, 0, 200, 200)
37 {
37 {
38
38
39 }
39 }
@@ -53,33 +53,30 void ChartLayout::setGeometry(const QRectF& rect)
53 QLegend* legend = m_presenter->legend();
53 QLegend* legend = m_presenter->legend();
54 ChartBackground* background = m_presenter->backgroundElement();
54 ChartBackground* background = m_presenter->backgroundElement();
55
55
56 QRectF contentGeometry = calculateBackgroundGeometry(rect,background);
56 QRectF contentGeometry = calculateBackgroundGeometry(rect, background);
57
57
58 contentGeometry = calculateContentGeometry(contentGeometry);
58 contentGeometry = calculateContentGeometry(contentGeometry);
59
59
60 if (title && title->isVisible()) {
60 if (title && title->isVisible())
61 contentGeometry = calculateTitleGeometry(contentGeometry,title);
61 contentGeometry = calculateTitleGeometry(contentGeometry, title);
62 }
63
62
64 if (legend->isAttachedToChart() && legend->isVisible()) {
63 if (legend->isAttachedToChart() && legend->isVisible())
65 contentGeometry = calculateLegendGeometry(contentGeometry,legend);
64 contentGeometry = calculateLegendGeometry(contentGeometry, legend);
66 }
67
65
68 calculateChartGeometry(contentGeometry,axes);
66 calculateChartGeometry(contentGeometry, axes);
69
67
70 //TODO remove me
68 //TODO remove me
71 #ifdef SHOW_LAYOUT
69 #ifdef SHOW_LAYOUT
72 LayoutDebuger* debuger = LayoutDebuger::instance();
70 LayoutDebuger* debuger = LayoutDebuger::instance();
73 debuger->reset();
71 debuger->reset();
74 debuger->setPen(QPen(Qt::red));
72 debuger->setPen(QPen(Qt::red));
75 debuger->add(backgroundGeometry,m_presenter->rootItem());
73 debuger->add(backgroundGeometry, m_presenter->rootItem());
76 debuger->add(titleGeometry,m_presenter->rootItem());
74 debuger->add(titleGeometry, m_presenter->rootItem());
77 debuger->add(legendGeometry ,m_presenter->rootItem());
75 debuger->add(legendGeometry , m_presenter->rootItem());
78 debuger->add(axisGeometry ,m_presenter->rootItem());
76 debuger->add(axisGeometry , m_presenter->rootItem());
79 debuger->add(geometry,m_presenter->rootItem());
77 debuger->add(geometry, m_presenter->rootItem());
80 foreach(LegendMarker* marker,legend->d_ptr->markers()){
78 foreach (LegendMarker *marker, legend->d_ptr->markers())
81 debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem());
79 debuger->add(marker->mapRectToScene(marker->boundingRect()), m_presenter->rootItem());
82 }
83 #endif
80 #endif
84
81
85 QGraphicsLayout::setGeometry(rect);
82 QGraphicsLayout::setGeometry(rect);
@@ -87,21 +84,22 void ChartLayout::setGeometry(const QRectF& rect)
87
84
88 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
85 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
89 {
86 {
90 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
87 return geometry.adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom());
91 }
88 }
92
89
93 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
90 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
94 {
91 {
95 return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom());
92 return minimum.adjusted(0, 0, m_margins.left() + m_margins.right(), m_margins.top() + m_margins.bottom());
96 }
93 }
97
94
98
95
99 QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const
96 QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry, ChartBackground* background) const
100 {
97 {
101 qreal left, top, right, bottom;
98 qreal left, top, right, bottom;
102 getContentsMargins(&left, &top, &right, &bottom);
99 getContentsMargins(&left, &top, &right, &bottom);
103 QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom);
100 QRectF backgroundGeometry = geometry.adjusted(left, top, -right, -bottom);
104 if(background) background->setRect(backgroundGeometry);
101 if (background)
102 background->setRect(backgroundGeometry);
105 return backgroundGeometry;
103 return backgroundGeometry;
106 }
104 }
107
105
@@ -109,14 +107,14 QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const
109 {
107 {
110 qreal left, top, right, bottom;
108 qreal left, top, right, bottom;
111 getContentsMargins(&left, &top, &right, &bottom);
109 getContentsMargins(&left, &top, &right, &bottom);
112 return minimum.adjusted(0,0,left + right,top+bottom);
110 return minimum.adjusted(0, 0, left + right, top + bottom);
113 }
111 }
114
112
115 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const
113 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const
116 {
114 {
117
115
118 QSizeF vertical(0,0);
116 QSizeF vertical(0, 0);
119 QSizeF horizontal(0,0);
117 QSizeF horizontal(0, 0);
120
118
121 int topAxisCount = 0;
119 int topAxisCount = 0;
122 int bottomAxisCount = 0;
120 int bottomAxisCount = 0;
@@ -125,26 +123,24 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<C
125
123
126 // check axis size
124 // check axis size
127 foreach(ChartAxis* axis , axes) {
125 foreach(ChartAxis* axis , axes) {
128 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
126 if (axis->orientation() == Qt::Vertical && axis->isVisible()) {
129 // vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
127 // vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
130 if (axis->alternativePlacement())
128 if (axis->alternativePlacement())
131 rightAxisCount++;
129 rightAxisCount++;
132 else
130 else
133 leftAxisCount++;
131 leftAxisCount++;
134 }
132 } else if (axis->orientation() == Qt::Horizontal && axis->isVisible()) {
135 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
136 // horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
133 // horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
137 if (axis->alternativePlacement())
134 if (axis->alternativePlacement())
138 topAxisCount++;
135 topAxisCount++;
139 else
136 else
140 bottomAxisCount++;
137 bottomAxisCount++;
141 }
138 }
142
143 }
139 }
144
140
145 qreal width = qMin(vertical.width(),geometry.width() * golden_ratio);
141 qreal width = qMin(vertical.width(), geometry.width() * golden_ratio);
146
142
147 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
143 QRectF rect = geometry.adjusted(width, vertical.height() / 2, -horizontal.width() / 2, -horizontal.height());
148
144
149 // axis area width
145 // axis area width
150 // TODO: replase with dynamic size code
146 // TODO: replase with dynamic size code
@@ -160,16 +156,15 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<C
160 topAxisCount = 0;
156 topAxisCount = 0;
161
157
162 // adjust the axes internal and external rects
158 // adjust the axes internal and external rects
163 foreach(ChartAxis* axis , axes) {
159 foreach (ChartAxis *axis , axes) {
164 if(axis->orientation()==Qt::Vertical) {
160 if (axis->orientation() == Qt::Vertical) {
165 axis->setInternalRect(rect.adjusted(-leftAxisCount * axisWidth, 0, rightAxisCount * axisWidth, 0));
161 axis->setInternalRect(rect.adjusted(-leftAxisCount * axisWidth, 0, rightAxisCount * axisWidth, 0));
166 axis->setGeometry(rect.adjusted(-(leftAxisCount + 1) * axisWidth, 0, (rightAxisCount + 1) * axisWidth, 0));
162 axis->setGeometry(rect.adjusted(-(leftAxisCount + 1) * axisWidth, 0, (rightAxisCount + 1) * axisWidth, 0));
167 if (axis->alternativePlacement())
163 if (axis->alternativePlacement())
168 rightAxisCount++;
164 rightAxisCount++;
169 else
165 else
170 leftAxisCount++;
166 leftAxisCount++;
171 }
167 } else if (axis->orientation() == Qt::Horizontal) {
172 else if(axis->orientation()==Qt::Horizontal) {
173 axis->setInternalRect(rect.adjusted(0, -topAxisCount * axisWidth, 0, bottomAxisCount * axisWidth));
168 axis->setInternalRect(rect.adjusted(0, -topAxisCount * axisWidth, 0, bottomAxisCount * axisWidth));
174 axis->setGeometry(rect.adjusted(0, -(topAxisCount + 1) * axisWidth, 0, (bottomAxisCount + 1) * axisWidth));
169 axis->setGeometry(rect.adjusted(0, -(topAxisCount + 1) * axisWidth, 0, (bottomAxisCount + 1) * axisWidth));
175 if (axis->alternativePlacement())
170 if (axis->alternativePlacement())
@@ -184,103 +179,98 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<C
184
179
185 QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const
180 QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const
186 {
181 {
187 QSizeF vertical(0,0);
182 QSizeF vertical(0, 0);
188 QSizeF horizontal(0,0);
183 QSizeF horizontal(0, 0);
189
184
190 // check axis size
185 // check axis size
191 foreach(ChartAxis* axis , axes) {
186 foreach (ChartAxis *axis , axes) {
192 if(axis->orientation()==Qt::Vertical && axis->isVisible()){
187 if (axis->orientation() == Qt::Vertical && axis->isVisible())
193 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
188 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
194 }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
189 else if (axis->orientation() == Qt::Horizontal && axis->isVisible())
195 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
190 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
196 }
197 }
191 }
198 return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height());
192 return minimum.adjusted(0, 0, horizontal.width() + vertical.width(), horizontal.height() + vertical.height());
199 }
193 }
200
194
201 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const
195 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry, QLegend* legend) const
202 {
196 {
203 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1));
197 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1));
204 QRectF legendRect;
198 QRectF legendRect;
205 QRectF result;
199 QRectF result;
206
200
207 switch (legend->alignment()) {
201 switch (legend->alignment()) {
208 case Qt::AlignTop: {
202 case Qt::AlignTop:
209 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
203 legendRect = QRectF(geometry.topLeft(), QSizeF(geometry.width(), size.height()));
210 result = geometry.adjusted(0,legendRect.height(),0,0);
204 result = geometry.adjusted(0, legendRect.height(), 0, 0);
211 break;
205 break;
212 }
206 case Qt::AlignBottom:
213 case Qt::AlignBottom: {
207 legendRect = QRectF(QPointF(geometry.left(), geometry.bottom() - size.height()), QSizeF(geometry.width(), size.height()));
214 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
208 result = geometry.adjusted(0, 0, 0, -legendRect.height());
215 result = geometry.adjusted(0,0,0,-legendRect.height());
216 break;
209 break;
217 }
218 case Qt::AlignLeft: {
210 case Qt::AlignLeft: {
219 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
211 qreal width = qMin(size.width(), geometry.width() * golden_ratio);
220 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
212 legendRect = QRectF(geometry.topLeft(), QSizeF(width, geometry.height()));
221 result = geometry.adjusted(width,0,0,0);
213 result = geometry.adjusted(width, 0, 0, 0);
222 break;
214 break;
223 }
215 }
224 case Qt::AlignRight: {
216 case Qt::AlignRight: {
225 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
217 qreal width = qMin(size.width(), geometry.width() * golden_ratio);
226 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
218 legendRect = QRectF(QPointF(geometry.right() - width, geometry.top()), QSizeF(width, geometry.height()));
227 result = geometry.adjusted(0,0,-width,0);
219 result = geometry.adjusted(0, 0, -width, 0);
228 break;
220 break;
229 }
221 }
230 default: {
222 default:
231 legendRect = QRectF(0,0,0,0);
223 legendRect = QRectF(0, 0, 0, 0);
232 result = geometry;
224 result = geometry;
233 break;
225 break;
234 }
226 }
235 }
236
227
237 legend->setGeometry(legendRect);
228 legend->setGeometry(legendRect);
238
229
239 return result;
230 return result;
240 }
231 }
241
232
242 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
233 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry, QLegend* legend) const
243 {
234 {
244 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
235 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1));
245 return geometry.adjusted(0,0,minSize.width(),minSize.height());
236 return geometry.adjusted(0, 0, minSize.width(), minSize.height());
246 }
237 }
247
238
248 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
239 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry, ChartTitle* title) const
249 {
240 {
250 title->setGeometry(geometry);
241 title->setGeometry(geometry);
251 QPointF center = geometry.center() - title->boundingRect().center();
242 QPointF center = geometry.center() - title->boundingRect().center();
252 title->setPos(center.x(),title->pos().y());
243 title->setPos(center.x(), title->pos().y());
253 return geometry.adjusted(0,title->boundingRect().height(),0,0);
244 return geometry.adjusted(0, title->boundingRect().height(), 0, 0);
254 }
245 }
255
246
256 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
247 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum, ChartTitle* title) const
257 {
248 {
258 QSizeF min = title->sizeHint(Qt::MinimumSize);
249 QSizeF min = title->sizeHint(Qt::MinimumSize);
259 return minimum.adjusted(0,0,min.width(),min.height());
250 return minimum.adjusted(0, 0, min.width(), min.height());
260 }
251 }
261
252
262 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
253 QSizeF ChartLayout::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
263 {
254 {
264 Q_UNUSED(constraint);
255 Q_UNUSED(constraint);
265 if(which == Qt::MinimumSize){
256 if (which == Qt::MinimumSize) {
266 QList<ChartAxis*> axes = m_presenter->axisItems();
257 QList<ChartAxis*> axes = m_presenter->axisItems();
267 ChartTitle* title = m_presenter->titleElement();
258 ChartTitle* title = m_presenter->titleElement();
268 QLegend* legend = m_presenter->legend();
259 QLegend* legend = m_presenter->legend();
269 QRectF minimumRect(0,0,0,0);
260 QRectF minimumRect(0, 0, 0, 0);
270 minimumRect = calculateBackgroundMinimum(minimumRect);
261 minimumRect = calculateBackgroundMinimum(minimumRect);
271 minimumRect = calculateContentMinimum(minimumRect);
262 minimumRect = calculateContentMinimum(minimumRect);
272 minimumRect = calculateTitleMinimum(minimumRect,title);
263 minimumRect = calculateTitleMinimum(minimumRect, title);
273 minimumRect = calculateLegendMinimum(minimumRect,legend);
264 minimumRect = calculateLegendMinimum(minimumRect, legend);
274 minimumRect = calculateAxisMinimum(minimumRect,axes);
265 minimumRect = calculateAxisMinimum(minimumRect, axes);
275 return minimumRect.united(m_minChartRect).size().toSize();
266 return minimumRect.united(m_minChartRect).size().toSize();
276 }else
267 }
277 return QSize(-1,-1);
268 return QSize(-1, -1);
278 }
269 }
279
270
280 void ChartLayout::setMargins(const QMargins& margins)
271 void ChartLayout::setMargins(const QMargins& margins)
281 {
272 {
282
273 if (m_margins != margins) {
283 if(m_margins != margins){
284 m_margins = margins;
274 m_margins = margins;
285 updateGeometry();
275 updateGeometry();
286 }
276 }
@@ -46,22 +46,22 public:
46 void adjustChartGeometry();
46 void adjustChartGeometry();
47
47
48 protected:
48 protected:
49 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
49 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
50 int count() const { return 0; }
50 int count() const { return 0; }
51 QGraphicsLayoutItem* itemAt(int) const { return 0; };
51 QGraphicsLayoutItem* itemAt(int) const { return 0; };
52 void removeAt(int){};
52 void removeAt(int){};
53
53
54 private:
54 private:
55 QRectF calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const;
55 QRectF calculateBackgroundGeometry(const QRectF& geometry, ChartBackground* background) const;
56 QRectF calculateContentGeometry(const QRectF& geometry) const;
56 QRectF calculateContentGeometry(const QRectF& geometry) const;
57 QRectF calculateTitleGeometry(const QRectF& geometry, ChartTitle* title) const;
57 QRectF calculateTitleGeometry(const QRectF& geometry, ChartTitle* title) const;
58 QRectF calculateChartGeometry(const QRectF& geometry,const QList<ChartAxis*>& axes) const;
58 QRectF calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const;
59 QRectF calculateLegendGeometry(const QRectF& geometry, QLegend* legend) const;
59 QRectF calculateLegendGeometry(const QRectF& geometry, QLegend* legend) const;
60 QRectF calculateBackgroundMinimum(const QRectF& minimum) const;
60 QRectF calculateBackgroundMinimum(const QRectF& minimum) const;
61 QRectF calculateContentMinimum(const QRectF& minimum) const;
61 QRectF calculateContentMinimum(const QRectF& minimum) const;
62 QRectF calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const;
62 QRectF calculateTitleMinimum(const QRectF& minimum, ChartTitle* title) const;
63 QRectF calculateAxisMinimum(const QRectF& minimum,const QList<ChartAxis*>& axes) const;
63 QRectF calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const;
64 QRectF calculateLegendMinimum(const QRectF& minimum,QLegend* legend) const;
64 QRectF calculateLegendMinimum(const QRectF& minimum, QLegend* legend) const;
65
65
66 private:
66 private:
67 ChartPresenter* m_presenter;
67 ChartPresenter* m_presenter;
@@ -35,15 +35,16
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
38 ChartPresenter::ChartPresenter(QChart* chart, ChartDataSet* dataset)
39 m_chart(chart),
39 : QObject(chart),
40 m_dataset(dataset),
40 m_chart(chart),
41 m_chartTheme(0),
41 m_dataset(dataset),
42 m_options(QChart::NoAnimation),
42 m_chartTheme(0),
43 m_state(ShowState),
43 m_options(QChart::NoAnimation),
44 m_layout(new ChartLayout(this)),
44 m_state(ShowState),
45 m_background(0),
45 m_layout(new ChartLayout(this)),
46 m_title(0)
46 m_background(0),
47 m_title(0)
47 {
48 {
48
49
49 }
50 }
@@ -57,12 +58,10 void ChartPresenter::setChartsGeometry(const QRectF& rect)
57 {
58 {
58 Q_ASSERT(rect.isValid());
59 Q_ASSERT(rect.isValid());
59
60
60 if(m_chartsRect!=rect) {
61 if (m_chartsRect != rect) {
61 m_chartsRect=rect;
62 m_chartsRect = rect;
62 foreach(ChartElement* chart, m_chartItems)
63 foreach (ChartElement *chart, m_chartItems)
63 {
64 chart->handleGeometryChanged(rect);
64 chart->handleGeometryChanged(rect);
65 }
66 }
65 }
67 }
66 }
68
67
@@ -71,25 +70,25 QRectF ChartPresenter::chartsGeometry() const
71 return m_chartsRect;
70 return m_chartsRect;
72 }
71 }
73
72
74 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain)
73 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis, Domain* domain)
75 {
74 {
76 ChartAxis* item = axis->d_ptr->createGraphics(this);
75 ChartAxis* item = axis->d_ptr->createGraphics(this);
77 item->setDomain(domain);
76 item->setDomain(domain);
78
77
79 if(m_options.testFlag(QChart::GridAxisAnimations)){
78 if (m_options.testFlag(QChart::GridAxisAnimations))
80 item->setAnimation(new AxisAnimation(item));
79 item->setAnimation(new AxisAnimation(item));
81 }
82
80
83 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
81 QObject::connect(this, SIGNAL(geometryChanged(QRectF)), item, SLOT(handleGeometryChanged(QRectF)));
84 QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated()));
82 QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated()));
85 QObject::connect(axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleAxisVisibleChanged(bool)));
83 QObject::connect(axis, SIGNAL(visibleChanged(bool)), this, SLOT(handleAxisVisibleChanged(bool)));
86
84
87 //initialize
85 //initialize
88 domain->emitUpdated();
86 domain->emitUpdated();
89 m_chartTheme->decorate(axis);
87 m_chartTheme->decorate(axis);
90 axis->d_ptr->setDirty(false);
88 axis->d_ptr->setDirty(false);
91 axis->d_ptr->emitUpdated();
89 axis->d_ptr->emitUpdated();
92 if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect);
90 if (m_chartsRect.isValid())
91 item->handleGeometryChanged(m_chartsRect);
93
92
94 m_axisItems.insert(axis, item);
93 m_axisItems.insert(axis, item);
95 selectVisibleAxis();
94 selectVisibleAxis();
@@ -102,24 +101,25 void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis)
102 selectVisibleAxis();
101 selectVisibleAxis();
103 item->hide();
102 item->hide();
104 item->disconnect();
103 item->disconnect();
105 QObject::disconnect(this,0,item,0);
104 QObject::disconnect(this, 0, item, 0);
106 item->deleteLater();
105 item->deleteLater();
107 }
106 }
108
107
109
108
110 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
109 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series, Domain* domain)
111 {
110 {
112 ChartElement *item = series->d_ptr->createGraphics(this);
111 ChartElement *item = series->d_ptr->createGraphics(this);
113 Q_ASSERT(item);
112 Q_ASSERT(item);
114 item->setDomain(domain);
113 item->setDomain(domain);
115
114
116 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
115 QObject::connect(this, SIGNAL(geometryChanged(QRectF)), item, SLOT(handleGeometryChanged(QRectF)));
117 QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated()));
116 QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated()));
118 //initialize
117 //initialize
119 item->handleDomainUpdated();
118 item->handleDomainUpdated();
120
119
121 if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect);
120 if (m_chartsRect.isValid())
122 m_chartItems.insert(series,item);
121 item->handleGeometryChanged(m_chartsRect);
122 m_chartItems.insert(series, item);
123 }
123 }
124
124
125 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
125 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
@@ -161,27 +161,26 void ChartPresenter::selectVisibleAxis()
161
161
162 void ChartPresenter::handleAxisVisibleChanged(bool visible)
162 void ChartPresenter::handleAxisVisibleChanged(bool visible)
163 {
163 {
164 QAbstractAxis* axis = static_cast<QAbstractAxis*> (sender());
164 QAbstractAxis* axis = static_cast<QAbstractAxis*>(sender());
165 Q_ASSERT(axis);
165 Q_ASSERT(axis);
166 if(visible){
166 if (visible) {
167
167
168 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
168 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
169
169
170 while (i.hasNext()) {
170 while (i.hasNext()) {
171 i.next();
171 i.next();
172 if(i.key()==axis) {
172 if (i.key() == axis)
173 continue;
173 continue;
174 }
174 if (i.key()->orientation() == axis->orientation())
175 if(i.key()->orientation()==axis->orientation()) {
175 i.key()->setVisible(false);
176 i.key()->setVisible(false);
177 }
178 }
176 }
179 }
177 }
180 }
178 }
181
179
182 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
180 void ChartPresenter::setTheme(QChart::ChartTheme theme, bool force)
183 {
181 {
184 if(m_chartTheme && m_chartTheme->id() == theme) return;
182 if (m_chartTheme && m_chartTheme->id() == theme)
183 return;
185 delete m_chartTheme;
184 delete m_chartTheme;
186 m_chartTheme = ChartTheme::createTheme(theme);
185 m_chartTheme = ChartTheme::createTheme(theme);
187 m_chartTheme->setForced(force);
186 m_chartTheme->setForced(force);
@@ -202,8 +201,8 QChart::ChartTheme ChartPresenter::theme()
202
201
203 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
202 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
204 {
203 {
205 if(m_options!=options) {
204 if (m_options != options) {
206 m_options=options;
205 m_options = options;
207 resetAllElements();
206 resetAllElements();
208 }
207 }
209
208
@@ -213,30 +212,30 void ChartPresenter::resetAllElements()
213 {
212 {
214 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
213 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
215 while (i.hasNext()) {
214 while (i.hasNext()) {
216 i.next();
215 i.next();
217 Domain* domain = i.value()->domain();
216 Domain* domain = i.value()->domain();
218 QAbstractAxis* axis = i.key();
217 QAbstractAxis* axis = i.key();
219 handleAxisRemoved(axis);
218 handleAxisRemoved(axis);
220 handleAxisAdded(axis,domain);
219 handleAxisAdded(axis, domain);
221 }
220 }
222
221
223 QMapIterator<QAbstractSeries*, ChartElement*> j(m_chartItems);
222 QMapIterator<QAbstractSeries*, ChartElement*> j(m_chartItems);
224 while (j.hasNext()) {
223 while (j.hasNext()) {
225 j.next();
224 j.next();
226 Domain* domain = j.value()->domain();
225 Domain* domain = j.value()->domain();
227 QAbstractSeries* series = j.key();
226 QAbstractSeries* series = j.key();
228 handleSeriesRemoved(series);
227 handleSeriesRemoved(series);
229 handleSeriesAdded(series,domain);
228 handleSeriesAdded(series, domain);
230 }
229 }
231
230
232 layout()->invalidate();
231 layout()->invalidate();
233 }
232 }
234
233
235 void ChartPresenter::zoomIn(qreal factor)
234 void ChartPresenter::zoomIn(qreal factor)
236 {
235 {
237 QRectF rect = chartsGeometry();
236 QRectF rect = chartsGeometry();
238 rect.setWidth(rect.width()/factor);
237 rect.setWidth(rect.width() / factor);
239 rect.setHeight(rect.height()/factor);
238 rect.setHeight(rect.height() / factor);
240 rect.moveCenter(chartsGeometry().center());
239 rect.moveCenter(chartsGeometry().center());
241 zoomIn(rect);
240 zoomIn(rect);
242 }
241 }
@@ -249,8 +248,8 void ChartPresenter::zoomIn(const QRectF& rect)
249 return;
248 return;
250
249
251 m_state = ZoomInState;
250 m_state = ZoomInState;
252 m_statePoint = QPointF(r.center().x()/chartsGeometry().width(),r.center().y()/chartsGeometry().height());
251 m_statePoint = QPointF(r.center().x() / chartsGeometry().width(), r.center().y() / chartsGeometry().height());
253 m_dataset->zoomInDomain(r,chartsGeometry().size());
252 m_dataset->zoomInDomain(r, chartsGeometry().size());
254 m_state = ShowState;
253 m_state = ShowState;
255 }
254 }
256
255
@@ -262,23 +261,23 void ChartPresenter::zoomOut(qreal factor)
262 chartRect.setSize(chartsGeometry().size());
261 chartRect.setSize(chartsGeometry().size());
263
262
264 QRectF rect;
263 QRectF rect;
265 rect.setSize(chartRect.size()/factor);
264 rect.setSize(chartRect.size() / factor);
266 rect.moveCenter(chartRect.center());
265 rect.moveCenter(chartRect.center());
267 if (!rect.isValid())
266 if (!rect.isValid())
268 return;
267 return;
269 m_statePoint = QPointF(rect.center().x()/chartsGeometry().width(),rect.center().y()/chartsGeometry().height());
268 m_statePoint = QPointF(rect.center().x() / chartsGeometry().width(), rect.center().y() / chartsGeometry().height());
270 m_dataset->zoomOutDomain(rect, chartRect.size());
269 m_dataset->zoomOutDomain(rect, chartRect.size());
271 m_state = ShowState;
270 m_state = ShowState;
272 }
271 }
273
272
274 void ChartPresenter::scroll(qreal dx,qreal dy)
273 void ChartPresenter::scroll(qreal dx, qreal dy)
275 {
274 {
276 if(dx<0) m_state=ScrollLeftState;
275 if (dx < 0) m_state = ScrollLeftState;
277 if(dx>0) m_state=ScrollRightState;
276 if (dx > 0) m_state = ScrollRightState;
278 if(dy<0) m_state=ScrollUpState;
277 if (dy < 0) m_state = ScrollUpState;
279 if(dy>0) m_state=ScrollDownState;
278 if (dy > 0) m_state = ScrollDownState;
280
279
281 m_dataset->scrollDomain(dx,dy,chartsGeometry().size());
280 m_dataset->scrollDomain(dx, dy, chartsGeometry().size());
282 m_state = ShowState;
281 m_state = ShowState;
283 }
282 }
284
283
@@ -307,17 +306,18 void ChartPresenter::createTitleItem()
307
306
308 void ChartPresenter::handleAnimationFinished()
307 void ChartPresenter::handleAnimationFinished()
309 {
308 {
310 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
309 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
311 if(m_animations.empty()) emit animationsFinished();
310 if (m_animations.empty())
311 emit animationsFinished();
312 }
312 }
313
313
314 void ChartPresenter::startAnimation(ChartAnimation* animation)
314 void ChartPresenter::startAnimation(ChartAnimation* animation)
315 {
315 {
316 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
316 if (animation->state() != QAbstractAnimation::Stopped)
317 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
317 animation->stop();
318 if(!m_animations.isEmpty()){
318 QObject::connect(animation, SIGNAL(finished()), this, SLOT(handleAnimationFinished()), Qt::UniqueConnection);
319 if (!m_animations.isEmpty())
319 m_animations.append(animation);
320 m_animations.append(animation);
320 }
321 QTimer::singleShot(0, animation, SLOT(start()));
321 QTimer::singleShot(0, animation, SLOT(start()));
322 }
322 }
323
323
@@ -330,7 +330,8 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
330
330
331 QBrush ChartPresenter::backgroundBrush() const
331 QBrush ChartPresenter::backgroundBrush() const
332 {
332 {
333 if (!m_background) return QBrush();
333 if (!m_background)
334 return QBrush();
334 return m_background->brush();
335 return m_background->brush();
335 }
336 }
336
337
@@ -343,7 +344,8 void ChartPresenter::setBackgroundPen(const QPen& pen)
343
344
344 QPen ChartPresenter::backgroundPen() const
345 QPen ChartPresenter::backgroundPen() const
345 {
346 {
346 if (!m_background) return QPen();
347 if (!m_background)
348 return QPen();
347 return m_background->pen();
349 return m_background->pen();
348 }
350 }
349
351
@@ -356,7 +358,8 void ChartPresenter::setTitle(const QString& title)
356
358
357 QString ChartPresenter::title() const
359 QString ChartPresenter::title() const
358 {
360 {
359 if (!m_title) return QString();
361 if (!m_title)
362 return QString();
360 return m_title->text();
363 return m_title->text();
361 }
364 }
362
365
@@ -369,7 +372,8 void ChartPresenter::setTitleFont(const QFont& font)
369
372
370 QFont ChartPresenter::titleFont() const
373 QFont ChartPresenter::titleFont() const
371 {
374 {
372 if (!m_title) return QFont();
375 if (!m_title)
376 return QFont();
373 return m_title->font();
377 return m_title->font();
374 }
378 }
375
379
@@ -382,7 +386,8 void ChartPresenter::setTitleBrush(const QBrush &brush)
382
386
383 QBrush ChartPresenter::titleBrush() const
387 QBrush ChartPresenter::titleBrush() const
384 {
388 {
385 if (!m_title) return QBrush();
389 if (!m_title)
390 return QBrush();
386 return m_title->brush();
391 return m_title->brush();
387 }
392 }
388
393
@@ -395,7 +400,8 void ChartPresenter::setBackgroundVisible(bool visible)
395
400
396 bool ChartPresenter::isBackgroundVisible() const
401 bool ChartPresenter::isBackgroundVisible() const
397 {
402 {
398 if (!m_background) return false;
403 if (!m_background)
404 return false;
399 return m_background->isVisible();
405 return m_background->isVisible();
400 }
406 }
401
407
@@ -407,7 +413,8 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
407
413
408 bool ChartPresenter::isBackgroundDropShadowEnabled() const
414 bool ChartPresenter::isBackgroundDropShadowEnabled() const
409 {
415 {
410 if (!m_background) return false;
416 if (!m_background)
417 return false;
411 return m_background->isDropShadowEnabled();
418 return m_background->isDropShadowEnabled();
412 }
419 }
413
420
@@ -78,7 +78,7 public:
78 ZoomOutState
78 ZoomOutState
79 };
79 };
80
80
81 ChartPresenter(QChart* chart,ChartDataSet *dataset);
81 ChartPresenter(QChart* chart, ChartDataSet *dataset);
82 virtual ~ChartPresenter();
82 virtual ~ChartPresenter();
83
83
84 ChartTheme *chartTheme() const { return m_chartTheme; }
84 ChartTheme *chartTheme() const { return m_chartTheme; }
@@ -113,7 +113,7 public:
113
113
114 void setVisible(bool visible);
114 void setVisible(bool visible);
115
115
116 void setTheme(QChart::ChartTheme theme,bool force = true);
116 void setTheme(QChart::ChartTheme theme, bool force = true);
117 QChart::ChartTheme theme();
117 QChart::ChartTheme theme();
118
118
119 void setAnimationOptions(QChart::AnimationOptions options);
119 void setAnimationOptions(QChart::AnimationOptions options);
@@ -122,7 +122,7 public:
122 void zoomIn(qreal factor);
122 void zoomIn(qreal factor);
123 void zoomIn(const QRectF& rect);
123 void zoomIn(const QRectF& rect);
124 void zoomOut(qreal factor);
124 void zoomOut(qreal factor);
125 void scroll(qreal dx,qreal dy);
125 void scroll(qreal dx, qreal dy);
126
126
127 void setChartsGeometry(const QRectF& rect);
127 void setChartsGeometry(const QRectF& rect);
128 QRectF chartsGeometry() const;
128 QRectF chartsGeometry() const;
@@ -143,9 +143,9 private:
143 void selectVisibleAxis();
143 void selectVisibleAxis();
144
144
145 public Q_SLOTS:
145 public Q_SLOTS:
146 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
146 void handleSeriesAdded(QAbstractSeries* series, Domain* domain);
147 void handleSeriesRemoved(QAbstractSeries* series);
147 void handleSeriesRemoved(QAbstractSeries* series);
148 void handleAxisAdded(QAbstractAxis* axis,Domain* domain);
148 void handleAxisAdded(QAbstractAxis* axis, Domain* domain);
149 void handleAxisRemoved(QAbstractAxis* axis);
149 void handleAxisRemoved(QAbstractAxis* axis);
150 void handleAxisVisibleChanged(bool visible);
150 void handleAxisVisibleChanged(bool visible);
151
151
@@ -75,13 +75,13 ChartTheme::ChartTheme(QChart::ChartTheme id) :
75 m_gridLinePen(QPen(QRgb(0x000000))),
75 m_gridLinePen(QPen(QRgb(0x000000))),
76 m_force(false)
76 m_force(false)
77 {
77 {
78 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
78 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
79 }
79 }
80
80
81
81
82 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
82 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
83 {
83 {
84 switch(theme) {
84 switch (theme) {
85 case QChart::ChartThemeLight:
85 case QChart::ChartThemeLight:
86 return new ChartThemeLight();
86 return new ChartThemeLight();
87 case QChart::ChartThemeBlueCerulean:
87 case QChart::ChartThemeBlueCerulean:
@@ -105,7 +105,7 void ChartTheme::decorate(QChart *chart)
105 {
105 {
106 QBrush brush;
106 QBrush brush;
107
107
108 if(m_force || brush == chart->backgroundBrush())
108 if (m_force || brush == chart->backgroundBrush())
109 chart->setBackgroundBrush(m_chartBackgroundGradient);
109 chart->setBackgroundBrush(m_chartBackgroundGradient);
110 chart->setTitleFont(m_masterFont);
110 chart->setTitleFont(m_masterFont);
111 chart->setTitleBrush(m_labelBrush);
111 chart->setTitleBrush(m_labelBrush);
@@ -136,7 +136,7 void ChartTheme::decorate(QAreaSeries *series, int index)
136 QPen pen;
136 QPen pen;
137 QBrush brush;
137 QBrush brush;
138
138
139 if (m_force || pen == series->pen()){
139 if (m_force || pen == series->pen()) {
140 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
140 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
141 pen.setWidthF(2);
141 pen.setWidthF(2);
142 series->setPen(pen);
142 series->setPen(pen);
@@ -149,11 +149,11 void ChartTheme::decorate(QAreaSeries *series, int index)
149 }
149 }
150
150
151
151
152 void ChartTheme::decorate(QLineSeries *series,int index)
152 void ChartTheme::decorate(QLineSeries *series, int index)
153 {
153 {
154 QPen pen;
154 QPen pen;
155 if(m_force || pen == series->pen()){
155 if (m_force || pen == series->pen()) {
156 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
156 pen.setColor(m_seriesColors.at(index % m_seriesColors.size()));
157 pen.setWidthF(2);
157 pen.setWidthF(2);
158 series->setPen(pen);
158 series->setPen(pen);
159 }
159 }
@@ -167,7 +167,7 void ChartTheme::decorate(QAbstractBarSeries *series, int index)
167
167
168 qreal takeAtPos = 0.5;
168 qreal takeAtPos = 0.5;
169 qreal step = 0.2;
169 qreal step = 0.2;
170 if (sets.count() > 1 ) {
170 if (sets.count() > 1) {
171 step = 1.0 / (qreal) sets.count();
171 step = 1.0 / (qreal) sets.count();
172 if (sets.count() % m_seriesGradients.count())
172 if (sets.count() % m_seriesGradients.count())
173 step *= m_seriesGradients.count();
173 step *= m_seriesGradients.count();
@@ -228,7 +228,7 void ChartTheme::decorate(QPieSeries *series, int index)
228 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
228 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
229
229
230 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
230 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
231 qreal pos = (qreal) (i + 1) / (qreal) series->count();
231 qreal pos = (qreal)(i + 1) / (qreal) series->count();
232 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
232 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
233
233
234 QPieSlice *s = series->slices().at(i);
234 QPieSlice *s = series->slices().at(i);
@@ -251,8 +251,8 void ChartTheme::decorate(QPieSeries *series, int index)
251 void ChartTheme::decorate(QSplineSeries *series, int index)
251 void ChartTheme::decorate(QSplineSeries *series, int index)
252 {
252 {
253 QPen pen;
253 QPen pen;
254 if(m_force || pen == series->pen()){
254 if (m_force || pen == series->pen()) {
255 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
255 pen.setColor(m_seriesColors.at(index % m_seriesColors.size()));
256 pen.setWidthF(2);
256 pen.setWidthF(2);
257 series->setPen(pen);
257 series->setPen(pen);
258 }
258 }
@@ -264,54 +264,46 void ChartTheme::decorate(QAbstractAxis *axis)
264 QBrush brush;
264 QBrush brush;
265 QFont font;
265 QFont font;
266
266
267 bool axisX = axis->orientation()== Qt::Horizontal;
267 bool axisX = axis->orientation() == Qt::Horizontal;
268
268
269 if (axis->isLineVisible()) {
269 if (axis->isLineVisible()) {
270
270
271 if(m_force || brush == axis->labelsBrush()){
271 if (m_force || brush == axis->labelsBrush())
272 axis->setLabelsBrush(m_labelBrush);
272 axis->setLabelsBrush(m_labelBrush);
273 }
274 if(m_force || pen == axis->labelsPen()){
275 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
276 }
277
273
274 if (m_force || pen == axis->labelsPen())
275 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
278
276
279 if (m_force || axis->shadesVisible()) {
277 if (m_force || axis->shadesVisible()) {
280
278
281 if(m_force || brush == axis->shadesBrush()){
279 if (m_force || brush == axis->shadesBrush())
282 axis->setShadesBrush(m_backgroundShadesBrush);
280 axis->setShadesBrush(m_backgroundShadesBrush);
283 }
284
281
285 if(m_force || pen == axis->shadesPen()){
282 if (m_force || pen == axis->shadesPen())
286 axis->setShadesPen(m_backgroundShadesPen);
283 axis->setShadesPen(m_backgroundShadesPen);
287 }
288
284
289 if( m_force && (m_backgroundShades == BackgroundShadesBoth
285 if (m_force && (m_backgroundShades == BackgroundShadesBoth
290 || (m_backgroundShades == BackgroundShadesVertical && axisX)
286 || (m_backgroundShades == BackgroundShadesVertical && axisX)
291 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
287 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))) {
292 axis->setShadesVisible(true);
288 axis->setShadesVisible(true);
293
294 }
289 }
295 }
290 }
296
291
297 if(m_force || pen == axis->linePen()){
292 if (m_force || pen == axis->linePen())
298 axis->setLinePen(m_axisLinePen);
293 axis->setLinePen(m_axisLinePen);
299 }
300
294
301 if(m_force || pen == axis->gridLinePen()){
295 if (m_force || pen == axis->gridLinePen())
302 axis->setGridLinePen(m_gridLinePen);
296 axis->setGridLinePen(m_gridLinePen);
303 }
304
297
305 if(m_force || font == axis->labelsFont()){
298 if (m_force || font == axis->labelsFont())
306 axis->setLabelsFont(m_labelFont);
299 axis->setLabelsFont(m_labelFont);
307 }
308 }
300 }
309 }
301 }
310
302
311 void ChartTheme::generateSeriesGradients()
303 void ChartTheme::generateSeriesGradients()
312 {
304 {
313 // Generate gradients in HSV color space
305 // Generate gradients in HSV color space
314 foreach (const QColor& color, m_seriesColors) {
306 foreach (const QColor &color, m_seriesColors) {
315 QLinearGradient g;
307 QLinearGradient g;
316 qreal h = color.hsvHueF();
308 qreal h = color.hsvHueF();
317 qreal s = color.hsvSaturationF();
309 qreal s = color.hsvSaturationF();
@@ -69,7 +69,7 protected:
69 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight);
69 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight);
70 public:
70 public:
71 static ChartTheme *createTheme(QChart::ChartTheme theme);
71 static ChartTheme *createTheme(QChart::ChartTheme theme);
72 QChart::ChartTheme id() const {return m_id;}
72 QChart::ChartTheme id() const { return m_id; }
73 void decorate(QChart *chart);
73 void decorate(QChart *chart);
74 void decorate(QLegend *legend);
74 void decorate(QLegend *legend);
75 void decorate(QAbstractBarSeries *series, int index);
75 void decorate(QAbstractBarSeries *series, int index);
@@ -25,7 +25,8
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 ChartTitle::ChartTitle(QGraphicsItem* parent):QGraphicsSimpleTextItem(parent)
28 ChartTitle::ChartTitle(QGraphicsItem* parent)
29 : QGraphicsSimpleTextItem(parent)
29 {
30 {
30
31
31 }
32 }
@@ -37,7 +38,7 ChartTitle::~ChartTitle()
37
38
38 void ChartTitle::setText(const QString &text)
39 void ChartTitle::setText(const QString &text)
39 {
40 {
40 m_text=text;
41 m_text = text;
41 }
42 }
42
43
43 QString ChartTitle::text() const
44 QString ChartTitle::text() const
@@ -47,48 +48,47 QString ChartTitle::text() const
47
48
48 void ChartTitle::setGeometry(const QRectF &rect)
49 void ChartTitle::setGeometry(const QRectF &rect)
49 {
50 {
50 QFontMetrics fn(font());
51 QFontMetrics fn(font());
51
52
52 int width = rect.width();
53 int width = rect.width();
53
54
54 if (fn.boundingRect(m_text).width() > width)
55 if (fn.boundingRect(m_text).width() > width) {
55 {
56 QString string = m_text + "...";
56 QString string = m_text + "...";
57 while (fn.boundingRect(string).width() > width && string.length() > 3)
57 while (fn.boundingRect(string).width() > width && string.length() > 3)
58 string.remove(string.length() - 4, 1);
58 string.remove(string.length() - 4, 1);
59 QGraphicsSimpleTextItem::setText(string);
59 QGraphicsSimpleTextItem::setText(string);
60 } else {
60 }
61 QGraphicsSimpleTextItem::setText(m_text);
61 else
62 }
62 QGraphicsSimpleTextItem::setText(m_text);
63
63
64 setPos(rect.topLeft());
64 setPos(rect.topLeft());
65 }
65 }
66
66
67
67
68 QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
68 QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
69 {
69 {
70 Q_UNUSED(constraint);
70 Q_UNUSED(constraint);
71 QFontMetrics fn (font ());
71 QFontMetrics fn(font());
72 QSizeF sh;
72 QSizeF sh;
73
73
74 switch(which) {
74 switch (which) {
75 case Qt::MinimumSize:
75 case Qt::MinimumSize:
76 sh = QSizeF(fn.boundingRect ("...").width(),fn.height());
76 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
77 break;
77 break;
78 case Qt::PreferredSize:
78 case Qt::PreferredSize:
79 sh = QSizeF(fn.boundingRect(m_text).width(),fn.height());
79 sh = QSizeF(fn.boundingRect(m_text).width(), fn.height());
80 break;
80 break;
81 case Qt::MaximumSize:
81 case Qt::MaximumSize:
82 sh = QSizeF(fn.boundingRect(m_text).width(),fn.height());
82 sh = QSizeF(fn.boundingRect(m_text).width(), fn.height());
83 break;
83 break;
84 case Qt::MinimumDescent:
84 case Qt::MinimumDescent:
85 sh = QSizeF(0, fn.descent ());
85 sh = QSizeF(0, fn.descent());
86 break;
86 break;
87 default:
87 default:
88 break;
88 break;
89 }
89 }
90
90
91 return sh;
91 return sh;
92 }
92 }
93
93
94 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -23,11 +23,12
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 Domain::Domain(QObject* parent) : QObject(parent),
26 Domain::Domain(QObject* parent)
27 m_minX(0),
27 : QObject(parent),
28 m_maxX(0),
28 m_minX(0),
29 m_minY(0),
29 m_maxX(0),
30 m_maxY(0)
30 m_minY(0),
31 m_maxY(0)
31 {
32 {
32 }
33 }
33
34
@@ -41,27 +42,26 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
41 bool axisYChanged = false;
42 bool axisYChanged = false;
42
43
43 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
44 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
44 m_minX=minX;
45 m_minX = minX;
45 m_maxX=maxX;
46 m_maxX = maxX;
46 axisXChanged=true;
47 axisXChanged = true;
47 emit rangeXChanged(m_minX,m_maxX);
48 emit rangeXChanged(m_minX, m_maxX);
48 }
49 }
49
50
50 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
51 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
51 m_minY=minY;
52 m_minY = minY;
52 m_maxY=maxY;
53 m_maxY = maxY;
53 axisYChanged=true;
54 axisYChanged = true;
54 emit rangeYChanged(m_minY,m_maxY);
55 emit rangeYChanged(m_minY, m_maxY);
55 }
56 }
56
57
57 if(axisXChanged || axisYChanged) {
58 if (axisXChanged || axisYChanged)
58 emit updated();
59 emit updated();
59 }
60 }
60 }
61
61
62 void Domain::setRangeX(qreal min, qreal max)
62 void Domain::setRangeX(qreal min, qreal max)
63 {
63 {
64 setRange(min,max,m_minY, m_maxY);
64 setRange(min, max, m_minY, m_maxY);
65 }
65 }
66
66
67 void Domain::setRangeY(qreal min, qreal max)
67 void Domain::setRangeY(qreal min, qreal max)
@@ -121,7 +121,7 void Domain::zoomIn(const QRectF& rect, const QSizeF& size)
121 minY = maxY - dy * rect.bottom();
121 minY = maxY - dy * rect.bottom();
122 maxY = maxY - dy * rect.top();
122 maxY = maxY - dy * rect.top();
123
123
124 setRange(minX,maxX,minY,maxY);
124 setRange(minX, maxX, minY, maxY);
125 }
125 }
126
126
127 void Domain::zoomOut(const QRectF& rect, const QSizeF& size)
127 void Domain::zoomOut(const QRectF& rect, const QSizeF& size)
@@ -139,10 +139,10 void Domain::zoomOut(const QRectF& rect, const QSizeF& size)
139 maxY = minY + dy * rect.bottom();
139 maxY = minY + dy * rect.bottom();
140 minY = maxY - dy * size.height();
140 minY = maxY - dy * size.height();
141
141
142 setRange(minX,maxX,minY,maxY);
142 setRange(minX, maxX, minY, maxY);
143 }
143 }
144
144
145 void Domain::move(qreal dx,qreal dy,const QSizeF& size)
145 void Domain::move(qreal dx, qreal dy, const QSizeF& size)
146 {
146 {
147 qreal x = spanX() / size.width();
147 qreal x = spanX() / size.width();
148 qreal y = spanY() / size.height();
148 qreal y = spanY() / size.height();
@@ -152,15 +152,15 void Domain::move(qreal dx,qreal dy,const QSizeF& size)
152 qreal minY = m_minY;
152 qreal minY = m_minY;
153 qreal maxY = m_maxY;
153 qreal maxY = m_maxY;
154
154
155 if(dx!=0) {
155 if (dx != 0) {
156 minX = minX + x * dx;
156 minX = minX + x * dx;
157 maxX = maxX + x * dx;
157 maxX = maxX + x * dx;
158 }
158 }
159 if(dy!=0) {
159 if (dy != 0) {
160 minY = minY + y * dy;
160 minY = minY + y * dy;
161 maxY = maxY + y * dy;
161 maxY = maxY + y * dy;
162 }
162 }
163 setRange(minX,maxX,minY,maxY);
163 setRange(minX, maxX, minY, maxY);
164 }
164 }
165
165
166 void Domain::emitUpdated()
166 void Domain::emitUpdated()
@@ -173,19 +173,18 void Domain::handleAxisUpdated()
173 QAbstractAxisPrivate* axis = qobject_cast<QAbstractAxisPrivate*>(sender());
173 QAbstractAxisPrivate* axis = qobject_cast<QAbstractAxisPrivate*>(sender());
174 Q_ASSERT(axis);
174 Q_ASSERT(axis);
175 axis->setDirty(false);
175 axis->setDirty(false);
176 if(axis->orientation()==Qt::Horizontal){
176 if (axis->orientation() == Qt::Horizontal)
177 setRangeX(axis->min(),axis->max());
177 setRangeX(axis->min(), axis->max());
178 }else if(axis->orientation()==Qt::Vertical){
178 else if (axis->orientation() == Qt::Vertical)
179 setRangeY(axis->min(),axis->max());
179 setRangeY(axis->min(), axis->max());
180 }
181 }
180 }
182
181
183 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2)
182 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2)
184 {
183 {
185 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
184 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
186 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
185 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
187 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
186 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
188 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
187 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
189 }
188 }
190
189
191
190
@@ -197,7 +196,7 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const Domain &domain1, const
197
196
198 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain)
197 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain)
199 {
198 {
200 dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')';
199 dbg.nospace() << "Domain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')';
201 return dbg.maybeSpace();
200 return dbg.maybeSpace();
202 }
201 }
203
202
@@ -36,10 +36,11
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class QTCOMMERCIALCHART_AUTOTEST_EXPORT Domain: public QObject {
39 class QTCOMMERCIALCHART_AUTOTEST_EXPORT Domain: public QObject
40 {
40 Q_OBJECT
41 Q_OBJECT
41 public:
42 public:
42 explicit Domain(QObject* object=0);
43 explicit Domain(QObject* object = 0);
43 virtual ~Domain();
44 virtual ~Domain();
44
45
45 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY);
46 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY);
@@ -65,7 +66,7 public:
65
66
66 void zoomIn(const QRectF& rect, const QSizeF& size);
67 void zoomIn(const QRectF& rect, const QSizeF& size);
67 void zoomOut(const QRectF& rect, const QSizeF& size);
68 void zoomOut(const QRectF& rect, const QSizeF& size);
68 void move(qreal dx,qreal dy,const QSizeF& size);
69 void move(qreal dx, qreal dy, const QSizeF& size);
69 void emitUpdated();
70 void emitUpdated();
70
71
71 Q_SIGNALS:
72 Q_SIGNALS:
@@ -26,8 +26,8
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 LegendLayout::LegendLayout(QLegend* legend):
29 LegendLayout::LegendLayout(QLegend* legend)
30 m_legend(legend)
30 : m_legend(legend)
31 {
31 {
32
32
33 }
33 }
@@ -40,59 +40,56 LegendLayout::~LegendLayout()
40 void LegendLayout::setOffset(qreal x, qreal y)
40 void LegendLayout::setOffset(qreal x, qreal y)
41 {
41 {
42 bool scrollHorizontal = true;
42 bool scrollHorizontal = true;
43 switch(m_legend->alignment()) {
43 switch (m_legend->alignment()) {
44 case Qt::AlignTop:
44 case Qt::AlignTop:
45 case Qt::AlignBottom: {
45 case Qt::AlignBottom:
46 scrollHorizontal = true;
46 scrollHorizontal = true;
47 break;
47 break;
48 }
48 case Qt::AlignLeft:
49 case Qt::AlignLeft:
49 case Qt::AlignRight:
50 case Qt::AlignRight: {
50 scrollHorizontal = false;
51 scrollHorizontal = false;
51 break;
52 break;
53 }
54 }
52 }
55
53
56 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
54 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
57 if (!m_legend->isAttachedToChart()) {
55 if (!m_legend->isAttachedToChart())
58 scrollHorizontal = !scrollHorizontal;
56 scrollHorizontal = !scrollHorizontal;
59 }
60
57
61 QRectF boundingRect = geometry();
58 QRectF boundingRect = geometry();
62 qreal left, top, right, bottom;
59 qreal left, top, right, bottom;
63 getContentsMargins(&left, &top, &right, &bottom);
60 getContentsMargins(&left, &top, &right, &bottom);
64 boundingRect.adjust(left,top,-right,-bottom);
61 boundingRect.adjust(left, top, -right, -bottom);
65
62
66 // Limit offset between m_minOffset and m_maxOffset
63 // Limit offset between m_minOffset and m_maxOffset
67 if (scrollHorizontal) {
64 if (scrollHorizontal) {
68 if(m_width<=boundingRect.width()) return;
65 if (m_width <= boundingRect.width())
66 return;
69
67
70 if (x != m_offsetX) {
68 if (x != m_offsetX) {
71 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
69 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
72 m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top());
70 m_legend->d_ptr->items()->setPos(-m_offsetX, boundingRect.top());
73 }
71 }
74 }
72 } else {
75 else {
73 if (m_height <= boundingRect.height())
76 if(m_height<=boundingRect.height()) return;
74 return;
77
75
78 if (y != m_offsetY) {
76 if (y != m_offsetY) {
79 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
77 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
80 m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY);
78 m_legend->d_ptr->items()->setPos(boundingRect.left(), -m_offsetY);
81 }
79 }
82 }
80 }
83 }
81 }
84
82
85 QPointF LegendLayout::offset() const
83 QPointF LegendLayout::offset() const
86 {
84 {
87 return QPointF(m_offsetX,m_offsetY);
85 return QPointF(m_offsetX, m_offsetY);
88 }
86 }
89
87
90 void LegendLayout::invalidate()
88 void LegendLayout::invalidate()
91 {
89 {
92 QGraphicsLayout::invalidate();
90 QGraphicsLayout::invalidate();
93 if(m_legend->isAttachedToChart()){
91 if (m_legend->isAttachedToChart())
94 m_legend->d_ptr->m_presenter->layout()->invalidate();
92 m_legend->d_ptr->m_presenter->layout()->invalidate();
95 }
96 }
93 }
97
94
98 void LegendLayout::setGeometry(const QRectF& rect)
95 void LegendLayout::setGeometry(const QRectF& rect)
@@ -101,84 +98,79 void LegendLayout::setGeometry(const QRectF& rect)
101
98
102 QGraphicsLayout::setGeometry(rect);
99 QGraphicsLayout::setGeometry(rect);
103
100
104 if(m_legend->isAttachedToChart()) {
101 if (m_legend->isAttachedToChart())
105 setAttachedGeometry(rect);
102 setAttachedGeometry(rect);
106 } else {
103 else
107 setDettachedGeometry(rect);
104 setDettachedGeometry(rect);
108 }
109
110 }
105 }
111
106
112 void LegendLayout::setAttachedGeometry(const QRectF& rect)
107 void LegendLayout::setAttachedGeometry(const QRectF& rect)
113 {
108 {
114 if (!rect.isValid()) return;
109 if (!rect.isValid())
115 m_offsetX=0;
110 return;
116 m_offsetY=0;
117
111
118 QSizeF size(0,0);
112 m_offsetX = 0;
113 m_offsetY = 0;
119
114
120 if( m_legend->d_ptr->markers().isEmpty()) return;
115 QSizeF size(0, 0);
121
116
122 m_width=0;
117 if (m_legend->d_ptr->markers().isEmpty())
123 m_height=0;
118 return;
119
120 m_width = 0;
121 m_height = 0;
124
122
125 qreal left, top, right, bottom;
123 qreal left, top, right, bottom;
126 getContentsMargins(&left, &top, &right, &bottom);
124 getContentsMargins(&left, &top, &right, &bottom);
127
125
128 QRectF geometry = rect.adjusted(left,top,-right,-bottom);
126 QRectF geometry = rect.adjusted(left, top, -right, -bottom);
129
130 switch(m_legend->alignment()) {
131
127
132 case Qt::AlignTop:
128 switch (m_legend->alignment()) {
133 case Qt::AlignBottom: {
129 case Qt::AlignTop:
134 QPointF point(0,0);
130 case Qt::AlignBottom: {
135 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
131 QPointF point(0, 0);
132 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
136 if (marker->isVisible()) {
133 if (marker->isVisible()) {
137 marker->setGeometry(geometry);
134 marker->setGeometry(geometry);
138 marker->setPos(point.x(),geometry.height()/2 - marker->boundingRect().height()/2);
135 marker->setPos(point.x(), geometry.height() / 2 - marker->boundingRect().height() / 2);
139 const QRectF& rect = marker->boundingRect();
136 const QRectF& rect = marker->boundingRect();
140 size = size.expandedTo(rect.size());
137 size = size.expandedTo(rect.size());
141 qreal w = rect.width();
138 qreal w = rect.width();
142 m_width+=w;
139 m_width += w;
143 point.setX(point.x() + w);
140 point.setX(point.x() + w);
144 }
141 }
145 }
142 }
146 if(m_width<geometry.width()) {
143 if (m_width < geometry.width())
147 m_legend->d_ptr->items()->setPos(geometry.width()/2-m_width/2,geometry.top());
144 m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top());
148
145 else
149 }
150 else {
151 m_legend->d_ptr->items()->setPos(geometry.topLeft());
146 m_legend->d_ptr->items()->setPos(geometry.topLeft());
152 }
147 m_height = size.height();
153 m_height=size.height();
154 }
148 }
155 break;
149 break;
156 case Qt::AlignLeft:
150 case Qt::AlignLeft:
157 case Qt::AlignRight: {
151 case Qt::AlignRight: {
158 QPointF point(0,0);
152 QPointF point(0, 0);
159 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
153 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
160 if (marker->isVisible()) {
154 if (marker->isVisible()) {
161 marker->setGeometry(geometry);
155 marker->setGeometry(geometry);
162 marker->setPos(point);
156 marker->setPos(point);
163 const QRectF& rect = marker->boundingRect();
157 const QRectF& rect = marker->boundingRect();
164 qreal h = rect.height();
158 qreal h = rect.height();
165 size = size.expandedTo(rect.size());
159 size = size.expandedTo(rect.size());
166 m_height+=h;
160 m_height += h;
167 point.setY(point.y() + h);
161 point.setY(point.y() + h);
168 }
169 }
162 }
170 if(m_height<geometry.height()) {
171 m_legend->d_ptr->items()->setPos(geometry.left(),geometry.height()/2-m_height/2);
172 }
173 else {
174 m_legend->d_ptr->items()->setPos(geometry.topLeft());
175 }
176 m_width=size.width();
177 }
163 }
164
165 if (m_height < geometry.height())
166 m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2);
167 else
168 m_legend->d_ptr->items()->setPos(geometry.topLeft());
169 m_width = size.width();
178 break;
170 break;
171 }
179 }
172 }
180
173
181
182 m_minOffsetX = -left;
174 m_minOffsetX = -left;
183 m_minOffsetY = - top;
175 m_minOffsetY = - top;
184 m_maxOffsetX = m_width - geometry.width() - right;
176 m_maxOffsetX = m_width - geometry.width() - right;
@@ -187,47 +179,49 void LegendLayout::setAttachedGeometry(const QRectF& rect)
187
179
188 void LegendLayout::setDettachedGeometry(const QRectF& rect)
180 void LegendLayout::setDettachedGeometry(const QRectF& rect)
189 {
181 {
190 if (!rect.isValid()) return;
182 if (!rect.isValid())
183 return;
191
184
192 // Detached layout is different.
185 // Detached layout is different.
193 // In detached mode legend may have multiple rows and columns, so layout calculations
186 // In detached mode legend may have multiple rows and columns, so layout calculations
194 // differ a log from attached mode.
187 // differ a log from attached mode.
195 // Also the scrolling logic is bit different.
188 // Also the scrolling logic is bit different.
196
189
197 m_offsetX=0;
190 m_offsetX = 0;
198 m_offsetY=0;
191 m_offsetY = 0;
199
192
200 qreal left, top, right, bottom;
193 qreal left, top, right, bottom;
201 getContentsMargins(&left, &top, &right, &bottom);
194 getContentsMargins(&left, &top, &right, &bottom);
202 QRectF geometry = rect.adjusted(left,top,-right,-bottom);
195 QRectF geometry = rect.adjusted(left, top, -right, -bottom);
203
196
204 QSizeF size(0,0);
197 QSizeF size(0, 0);
205
198
206 QList<LegendMarker *> markers = m_legend->d_ptr->markers();
199 QList<LegendMarker *> markers = m_legend->d_ptr->markers();
207
200
208 if(markers.isEmpty()) return;
201 if (markers.isEmpty())
202 return;
209
203
210 switch (m_legend->alignment()) {
204 switch (m_legend->alignment()) {
211 case Qt::AlignTop: {
205 case Qt::AlignTop: {
212 QPointF point(0,0);
206 QPointF point(0, 0);
213 m_width = 0;
207 m_width = 0;
214 m_height = 0;
208 m_height = 0;
215 for (int i=0; i<markers.count(); i++) {
209 for (int i = 0; i < markers.count(); i++) {
216 LegendMarker *marker = markers.at(i);
210 LegendMarker *marker = markers.at(i);
217 if (marker->isVisible()) {
211 if (marker->isVisible()) {
218 marker->setGeometry(geometry);
212 marker->setGeometry(geometry);
219 marker->setPos(point.x(),point.y());
213 marker->setPos(point.x(), point.y());
220 const QRectF& boundingRect = marker->boundingRect();
214 const QRectF& boundingRect = marker->boundingRect();
221 qreal w = boundingRect.width();
215 qreal w = boundingRect.width();
222 qreal h = boundingRect.height();
216 qreal h = boundingRect.height();
223 m_width = qMax(m_width,w);
217 m_width = qMax(m_width, w);
224 m_height = qMax(m_height,h);
218 m_height = qMax(m_height, h);
225 point.setX(point.x() + w);
219 point.setX(point.x() + w);
226 if (point.x() + w > geometry.left() + geometry.width() - right) {
220 if (point.x() + w > geometry.left() + geometry.width() - right) {
227 // Next item would go off rect.
221 // Next item would go off rect.
228 point.setX(0);
222 point.setX(0);
229 point.setY(point.y() + h);
223 point.setY(point.y() + h);
230 if (i+1 < markers.count()) {
224 if (i + 1 < markers.count()) {
231 m_height += h;
225 m_height += h;
232 }
226 }
233 }
227 }
@@ -241,26 +235,26 void LegendLayout::setDettachedGeometry(const QRectF& rect)
241 m_maxOffsetY = m_height - geometry.height() - bottom;
235 m_maxOffsetY = m_height - geometry.height() - bottom;
242 }
236 }
243 break;
237 break;
244 case Qt::AlignBottom: {
238 case Qt::AlignBottom: {
245 QPointF point(0,geometry.height());
239 QPointF point(0, geometry.height());
246 m_width = 0;
240 m_width = 0;
247 m_height = 0;
241 m_height = 0;
248 for (int i=0; i<markers.count(); i++) {
242 for (int i = 0; i < markers.count(); i++) {
249 LegendMarker *marker = markers.at(i);
243 LegendMarker *marker = markers.at(i);
250 if (marker->isVisible()) {
244 if (marker->isVisible()) {
251 marker->setGeometry(geometry);
245 marker->setGeometry(geometry);
252 const QRectF& boundingRect = marker->boundingRect();
246 const QRectF& boundingRect = marker->boundingRect();
253 qreal w = boundingRect.width();
247 qreal w = boundingRect.width();
254 qreal h = boundingRect.height();
248 qreal h = boundingRect.height();
255 m_width = qMax(m_width,w);
249 m_width = qMax(m_width, w);
256 m_height = qMax(m_height,h);
250 m_height = qMax(m_height, h);
257 marker->setPos(point.x(),point.y() - h);
251 marker->setPos(point.x(), point.y() - h);
258 point.setX(point.x() + w);
252 point.setX(point.x() + w);
259 if (point.x() + w > geometry.left() + geometry.width() - right) {
253 if (point.x() + w > geometry.left() + geometry.width() - right) {
260 // Next item would go off rect.
254 // Next item would go off rect.
261 point.setX(0);
255 point.setX(0);
262 point.setY(point.y() - h);
256 point.setY(point.y() - h);
263 if (i+1 < markers.count()) {
257 if (i + 1 < markers.count()) {
264 m_height += h;
258 m_height += h;
265 }
259 }
266 }
260 }
@@ -274,27 +268,27 void LegendLayout::setDettachedGeometry(const QRectF& rect)
274 m_maxOffsetY = -bottom;
268 m_maxOffsetY = -bottom;
275 }
269 }
276 break;
270 break;
277 case Qt::AlignLeft: {
271 case Qt::AlignLeft: {
278 QPointF point(0,0);
272 QPointF point(0, 0);
279 m_width = 0;
273 m_width = 0;
280 m_height = 0;
274 m_height = 0;
281 qreal maxWidth = 0;
275 qreal maxWidth = 0;
282 for (int i=0; i<markers.count(); i++) {
276 for (int i = 0; i < markers.count(); i++) {
283 LegendMarker *marker = markers.at(i);
277 LegendMarker *marker = markers.at(i);
284 if (marker->isVisible()) {
278 if (marker->isVisible()) {
285 marker->setGeometry(geometry);
279 marker->setGeometry(geometry);
286 const QRectF& boundingRect = marker->boundingRect();
280 const QRectF& boundingRect = marker->boundingRect();
287 qreal w = boundingRect.width();
281 qreal w = boundingRect.width();
288 qreal h = boundingRect.height();
282 qreal h = boundingRect.height();
289 m_height = qMax(m_height,h);
283 m_height = qMax(m_height, h);
290 maxWidth = qMax(maxWidth,w);
284 maxWidth = qMax(maxWidth, w);
291 marker->setPos(point.x(),point.y());
285 marker->setPos(point.x(), point.y());
292 point.setY(point.y() + h);
286 point.setY(point.y() + h);
293 if (point.y() + h > geometry.bottom() - bottom) {
287 if (point.y() + h > geometry.bottom() - bottom) {
294 // Next item would go off rect.
288 // Next item would go off rect.
295 point.setX(point.x() + maxWidth);
289 point.setX(point.x() + maxWidth);
296 point.setY(0);
290 point.setY(0);
297 if (i+1 < markers.count()) {
291 if (i + 1 < markers.count()) {
298 m_width += maxWidth;
292 m_width += maxWidth;
299 maxWidth = 0;
293 maxWidth = 0;
300 }
294 }
@@ -310,27 +304,27 void LegendLayout::setDettachedGeometry(const QRectF& rect)
310 m_maxOffsetY = m_height - geometry.height() - bottom;
304 m_maxOffsetY = m_height - geometry.height() - bottom;
311 }
305 }
312 break;
306 break;
313 case Qt::AlignRight: {
307 case Qt::AlignRight: {
314 QPointF point(geometry.width(),0);
308 QPointF point(geometry.width(), 0);
315 m_width = 0;
309 m_width = 0;
316 m_height = 0;
310 m_height = 0;
317 qreal maxWidth = 0;
311 qreal maxWidth = 0;
318 for (int i=0; i<markers.count(); i++) {
312 for (int i = 0; i < markers.count(); i++) {
319 LegendMarker *marker = markers.at(i);
313 LegendMarker *marker = markers.at(i);
320 if (marker->isVisible()) {
314 if (marker->isVisible()) {
321 marker->setGeometry(geometry);
315 marker->setGeometry(geometry);
322 const QRectF& boundingRect = marker->boundingRect();
316 const QRectF& boundingRect = marker->boundingRect();
323 qreal w = boundingRect.width();
317 qreal w = boundingRect.width();
324 qreal h = boundingRect.height();
318 qreal h = boundingRect.height();
325 m_height = qMax(m_height,h);
319 m_height = qMax(m_height, h);
326 maxWidth = qMax(maxWidth,w);
320 maxWidth = qMax(maxWidth, w);
327 marker->setPos(point.x() - w,point.y());
321 marker->setPos(point.x() - w, point.y());
328 point.setY(point.y() + h);
322 point.setY(point.y() + h);
329 if (point.y() + h > geometry.bottom()-bottom) {
323 if (point.y() + h > geometry.bottom() - bottom) {
330 // Next item would go off rect.
324 // Next item would go off rect.
331 point.setX(point.x() - maxWidth);
325 point.setX(point.x() - maxWidth);
332 point.setY(0);
326 point.setY(0);
333 if (i+1 < markers.count()) {
327 if (i + 1 < markers.count()) {
334 m_width += maxWidth;
328 m_width += maxWidth;
335 maxWidth = 0;
329 maxWidth = 0;
336 }
330 }
@@ -346,47 +340,42 void LegendLayout::setDettachedGeometry(const QRectF& rect)
346 m_maxOffsetY = m_height - geometry.height() - bottom;
340 m_maxOffsetY = m_height - geometry.height() - bottom;
347 }
341 }
348 break;
342 break;
349 default:
343 default:
350 break;
344 break;
351 }
345 }
352
346
353 }
347 }
354
348
355 QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
349 QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
356 {
350 {
357 QSizeF size(0, 0);
351 QSizeF size(0, 0);
358 qreal left, top, right, bottom;
352 qreal left, top, right, bottom;
359 getContentsMargins(&left, &top, &right, &bottom);
353 getContentsMargins(&left, &top, &right, &bottom);
360
354
361 if(constraint.isValid()) {
355 if (constraint.isValid()) {
362 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
356 foreach (LegendMarker *marker, m_legend->d_ptr->markers())
363 size = size.expandedTo(marker->effectiveSizeHint(which));
357 size = size.expandedTo(marker->effectiveSizeHint(which));
364 }
365 size = size.boundedTo(constraint);
358 size = size.boundedTo(constraint);
366 }
359 } else if (constraint.width() >= 0) {
367 else if (constraint.width() >= 0) {
368 qreal width = 0;
360 qreal width = 0;
369 qreal height = 0;
361 qreal height = 0;
370 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
362 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
371 width+=marker->effectiveSizeHint(which).width();
363 width += marker->effectiveSizeHint(which).width();
372 height=qMax(height,marker->effectiveSizeHint(which).height());
364 height = qMax(height, marker->effectiveSizeHint(which).height());
373 }
365 }
374
366
375 size = QSizeF(qMin(constraint.width(),width), height);
367 size = QSizeF(qMin(constraint.width(), width), height);
376 }
368 } else if (constraint.height() >= 0) {
377 else if (constraint.height() >= 0) {
378 qreal width = 0;
369 qreal width = 0;
379 qreal height = 0;
370 qreal height = 0;
380 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
371 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
381 width=qMax(width,marker->effectiveSizeHint(which).width());
372 width = qMax(width, marker->effectiveSizeHint(which).width());
382 height+=height,marker->effectiveSizeHint(which).height();
373 height += height, marker->effectiveSizeHint(which).height();
383 }
374 }
384 size = QSizeF(width,qMin(constraint.height(),height));
375 size = QSizeF(width, qMin(constraint.height(), height));
385 }
376 } else {
386 else {
377 foreach (LegendMarker *marker, m_legend->d_ptr->markers())
387 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
388 size = size.expandedTo(marker->effectiveSizeHint(which));
378 size = size.expandedTo(marker->effectiveSizeHint(which));
389 }
390 }
379 }
391 size += QSize(left + right, top + bottom);
380 size += QSize(left + right, top + bottom);
392 return size;
381 return size;
@@ -41,7 +41,7 public:
41
41
42 void invalidate();
42 void invalidate();
43 protected:
43 protected:
44 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
44 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF()) const;
45 int count() const { return 0; }
45 int count() const { return 0; }
46 QGraphicsLayoutItem* itemAt(int) const { return 0; };
46 QGraphicsLayoutItem* itemAt(int) const { return 0; };
47 void removeAt(int){};
47 void removeAt(int){};
@@ -60,7 +60,6 private:
60 qreal m_maxOffsetY;
60 qreal m_maxOffsetY;
61 qreal m_width;
61 qreal m_width;
62 qreal m_height;
62 qreal m_height;
63
64 };
63 };
65
64
66 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
@@ -39,8 +39,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
40 QGraphicsObject(legend),
40 QGraphicsObject(legend),
41 m_series(series),
41 m_series(series),
42 m_markerRect(0,0,10.0,10.0),
42 m_markerRect(0, 0, 10.0, 10.0),
43 m_boundingRect(0,0,0,0),
43 m_boundingRect(0, 0, 0, 0),
44 m_legend(legend),
44 m_legend(legend),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
46 m_rectItem(new QGraphicsRectItem(this)),
46 m_rectItem(new QGraphicsRectItem(this)),
@@ -75,7 +75,7 void LegendMarker::setFont(const QFont &font)
75 {
75 {
76 m_textItem->setFont(font);
76 m_textItem->setFont(font);
77 QFontMetrics fn(font);
77 QFontMetrics fn(font);
78 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
78 m_markerRect = QRectF(0, 0, fn.height() / 2, fn.height() / 2);
79 updateGeometry();
79 updateGeometry();
80 m_legend->layout()->invalidate();
80 m_legend->layout()->invalidate();
81 }
81 }
@@ -115,31 +115,28 QBrush LegendMarker::labelBrush() const
115
115
116 void LegendMarker::setGeometry(const QRectF& rect)
116 void LegendMarker::setGeometry(const QRectF& rect)
117 {
117 {
118 QFontMetrics fn (font());
118 QFontMetrics fn(font());
119
119
120 int width = rect.width();
120 int width = rect.width();
121 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
121 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
122 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
122 qreal y = qMax(m_markerRect.height() + 2 * m_margin, fn.height() + 2 * m_margin);
123
123
124 if (fn.boundingRect(m_text).width() + x > width)
124 if (fn.boundingRect(m_text).width() + x > width) {
125 {
126 QString string = m_text + "...";
125 QString string = m_text + "...";
127 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
126 while (fn.boundingRect(string).width() + x > width && string.length() > 3)
128 string.remove(string.length() - 4, 1);
127 string.remove(string.length() - 4, 1);
129 m_textItem->setText(string);
128 m_textItem->setText(string);
130 }
129 } else {
131 else
132 m_textItem->setText(m_text);
130 m_textItem->setText(m_text);
131 }
133
132
134 const QRectF& textRect = m_textItem->boundingRect();
133 const QRectF& textRect = m_textItem->boundingRect();
135
134 m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2);
136
137 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
138 m_rectItem->setRect(m_markerRect);
135 m_rectItem->setRect(m_markerRect);
139 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
136 m_rectItem->setPos(m_margin, y / 2 - m_markerRect.height() / 2);
140
137
141 prepareGeometryChange();
138 prepareGeometryChange();
142 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
139 m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y);
143 }
140 }
144
141
145 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
142 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -156,17 +153,17 QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) cons
156 QFontMetrics fn(m_textItem->font());
153 QFontMetrics fn(m_textItem->font());
157 QSizeF sh;
154 QSizeF sh;
158
155
159 switch (which) {
156 switch (which) {
160 case Qt::MinimumSize:
157 case Qt::MinimumSize:
161 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
158 sh = QSizeF(fn.boundingRect("...").width() + 2 * m_margin + m_space + m_markerRect.width(), qMax(m_markerRect.height() + 2 * m_margin, fn.height() + 2 * m_margin));
162 break;
159 break;
163 case Qt::PreferredSize:
160 case Qt::PreferredSize:
164 sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
161 sh = QSizeF(fn.boundingRect(m_text).width() + 2 * m_margin + m_space + m_markerRect.width(), qMax(m_markerRect.height() + 2 * m_margin, fn.height() + 2 * m_margin));
165 break;
162 break;
166 default:
163 default:
167 break;
164 break;
168 }
165 }
169 return sh;
166 return sh;
170 }
167 }
171
168
172 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
169 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
@@ -177,11 +174,12 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
177
174
178 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
175 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179
176
180 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
177 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series, QLegend *legend)
181 m_series(series)
178 : LegendMarker(series, legend),
179 m_series(series)
182 {
180 {
183 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
181 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
184 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
182 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(updated()));
185 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
183 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
186 updated();
184 updated();
187 }
185 }
@@ -194,8 +192,9 void AreaLegendMarker::updated()
194
192
195 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
193 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
196
194
197 BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
195 BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries, QBarSet *barset, QLegend *legend)
198 m_barset(barset)
196 : LegendMarker(barseries, legend),
197 m_barset(barset)
199 {
198 {
200 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
199 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
201 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
200 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
@@ -211,8 +210,9 void BarLegendMarker::updated()
211
210
212 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
213
212
214 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
213 PieLegendMarker::PieLegendMarker(QPieSeries* series, QPieSlice *pieslice, QLegend *legend)
215 m_pieslice(pieslice)
214 : LegendMarker(series, legend),
215 m_pieslice(pieslice)
216 {
216 {
217 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
217 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
218 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
218 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
@@ -227,11 +227,12 void PieLegendMarker::updated()
227
227
228 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
228 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229
229
230 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
230 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend)
231 m_series(series)
231 : LegendMarker(series, legend),
232 m_series(series)
232 {
233 {
233 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
234 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
234 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
235 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(updated()));
235 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
236 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
236 updated();
237 updated();
237 }
238 }
@@ -240,14 +241,10 void XYLegendMarker::updated()
240 {
241 {
241 setLabel(m_series->name());
242 setLabel(m_series->name());
242
243
243 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
244 if (m_series->type() == QAbstractSeries::SeriesTypeScatter)
244 {
245 setBrush(m_series->brush());
245 setBrush(m_series->brush());
246
246 else
247 }
248 else {
249 setBrush(QBrush(m_series->pen().color()));
247 setBrush(QBrush(m_series->pen().color()));
250 }
251 }
248 }
252
249
253 #include "moc_legendmarker_p.cpp"
250 #include "moc_legendmarker_p.cpp"
@@ -78,7 +78,7 public:
78
78
79 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
79 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
80
80
81 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
81 QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const;
82
82
83 protected:
83 protected:
84 // From QGraphicsObject
84 // From QGraphicsObject
@@ -124,7 +124,7 private:
124 class BarLegendMarker : public LegendMarker
124 class BarLegendMarker : public LegendMarker
125 {
125 {
126 public:
126 public:
127 BarLegendMarker(QAbstractBarSeries *barseries, QBarSet *barset,QLegend *legend);
127 BarLegendMarker(QAbstractBarSeries *barseries, QBarSet *barset, QLegend *legend);
128 protected:
128 protected:
129 void updated();
129 void updated();
130 private:
130 private:
@@ -41,28 +41,17 class LegendScroller: public QLegend, public Scroller
41 {
41 {
42
42
43 public:
43 public:
44 LegendScroller(QChart *chart):QLegend(chart)
44 LegendScroller(QChart *chart): QLegend(chart) { }
45 {
46 }
47
45
48 void setOffset(const QPointF& point)
46 void setOffset(const QPointF& point) { d_ptr->setOffset(point.x(), point.y()); }
49 {
50 d_ptr->setOffset(point.x(), point.y());
51 }
52 QPointF offset() const
53 {
54 return d_ptr->offset();
55 }
56
47
57 void mousePressEvent(QGraphicsSceneMouseEvent* event){
48 QPointF offset() const { return d_ptr->offset(); }
58 Scroller::mousePressEvent(event);
49
59 }
50 void mousePressEvent(QGraphicsSceneMouseEvent* event) { Scroller::mousePressEvent(event); }
60 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
51
61 Scroller::mouseMoveEvent(event);
52 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) { Scroller::mouseMoveEvent(event); }
62 }
53
63 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
54 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { Scroller::mouseReleaseEvent(event); }
64 Scroller::mouseReleaseEvent(event);
65 }
66 };
55 };
67
56
68 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
@@ -167,13 +167,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
167 Constructs the legend object and sets the parent to \a parent
167 Constructs the legend object and sets the parent to \a parent
168 */
168 */
169
169
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
170 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
172 {
172 {
173 setZValue(ChartPresenter::LegendZValue);
173 setZValue(ChartPresenter::LegendZValue);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
175 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
176 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
177 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
177 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
178 setLayout(d_ptr->m_layout);
178 setLayout(d_ptr->m_layout);
179 }
179 }
@@ -193,13 +193,13 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
193 Q_UNUSED(option)
193 Q_UNUSED(option)
194 Q_UNUSED(widget)
194 Q_UNUSED(widget)
195
195
196 if(!d_ptr->m_backgroundVisible) return;
196 if (!d_ptr->m_backgroundVisible)
197 return;
197
198
198 painter->setOpacity(opacity());
199 painter->setOpacity(opacity());
199 painter->setPen(d_ptr->m_pen);
200 painter->setPen(d_ptr->m_pen);
200 painter->setBrush(d_ptr->m_brush);
201 painter->setBrush(d_ptr->m_brush);
201 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
202 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
202
203 }
203 }
204
204
205
205
@@ -263,10 +263,8 void QLegend::setFont(const QFont &font)
263 {
263 {
264 if (d_ptr->m_font != font) {
264 if (d_ptr->m_font != font) {
265 d_ptr->m_font = font;
265 d_ptr->m_font = font;
266
266 foreach (LegendMarker *marker, d_ptr->markers())
267 foreach (LegendMarker *marker, d_ptr->markers()) {
268 marker->setFont(d_ptr->m_font);
267 marker->setFont(d_ptr->m_font);
269 }
270 layout()->invalidate();
268 layout()->invalidate();
271 emit fontChanged(font);
269 emit fontChanged(font);
272 }
270 }
@@ -274,7 +272,7 void QLegend::setFont(const QFont &font)
274
272
275 QFont QLegend::font() const
273 QFont QLegend::font() const
276 {
274 {
277 return d_ptr->m_font;
275 return d_ptr->m_font;
278 }
276 }
279
277
280 void QLegend::setBorderColor(QColor color)
278 void QLegend::setBorderColor(QColor color)
@@ -334,7 +332,7 QColor QLegend::labelColor() const
334
332
335 void QLegend::setAlignment(Qt::Alignment alignment)
333 void QLegend::setAlignment(Qt::Alignment alignment)
336 {
334 {
337 if(d_ptr->m_alignment!=alignment) {
335 if (d_ptr->m_alignment != alignment) {
338 d_ptr->m_alignment = alignment;
336 d_ptr->m_alignment = alignment;
339 layout()->invalidate();
337 layout()->invalidate();
340 }
338 }
@@ -379,7 +377,7 bool QLegend::isAttachedToChart()
379 */
377 */
380 void QLegend::setBackgroundVisible(bool visible)
378 void QLegend::setBackgroundVisible(bool visible)
381 {
379 {
382 if(d_ptr->m_backgroundVisible != visible) {
380 if (d_ptr->m_backgroundVisible != visible) {
383 d_ptr->m_backgroundVisible = visible;
381 d_ptr->m_backgroundVisible = visible;
384 update();
382 update();
385 emit backgroundVisibleChanged(visible);
383 emit backgroundVisibleChanged(visible);
@@ -399,37 +397,38 bool QLegend::isBackgroundVisible() const
399 */
397 */
400 void QLegend::hideEvent(QHideEvent *event)
398 void QLegend::hideEvent(QHideEvent *event)
401 {
399 {
402 if(isAttachedToChart()) d_ptr->m_presenter->layout()->invalidate();
400 if (isAttachedToChart())
403 QGraphicsWidget::hideEvent(event);
401 d_ptr->m_presenter->layout()->invalidate();
402 QGraphicsWidget::hideEvent(event);
404 }
403 }
405 /*!
404 /*!
406 \internal \a event see QGraphicsWidget for details
405 \internal \a event see QGraphicsWidget for details
407 */
406 */
408 void QLegend::showEvent(QShowEvent *event)
407 void QLegend::showEvent(QShowEvent *event)
409 {
408 {
410 if(isAttachedToChart()) {
409 if (isAttachedToChart()) {
411 d_ptr->items()->setVisible(false);
410 d_ptr->items()->setVisible(false);
412 layout()->invalidate();
411 layout()->invalidate();
413 }
412 }
414 QGraphicsWidget::showEvent(event);
413 QGraphicsWidget::showEvent(event);
415 //layout activation will show the items
414 //layout activation will show the items
416 }
415 }
417
416
418 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
417 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
419
418
420 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
419 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q)
421 q_ptr(q),
420 : q_ptr(q),
422 m_presenter(presenter),
421 m_presenter(presenter),
423 m_layout(new LegendLayout(q)),
422 m_layout(new LegendLayout(q)),
424 m_chart(chart),
423 m_chart(chart),
425 m_items(new QGraphicsItemGroup(q)),
424 m_items(new QGraphicsItemGroup(q)),
426 m_alignment(Qt::AlignTop),
425 m_alignment(Qt::AlignTop),
427 m_brush(QBrush()),
426 m_brush(QBrush()),
428 m_pen(QPen()),
427 m_pen(QPen()),
429 m_labelBrush(QBrush()),
428 m_labelBrush(QBrush()),
430 m_diameter(5),
429 m_diameter(5),
431 m_attachedToChart(true),
430 m_attachedToChart(true),
432 m_backgroundVisible(false)
431 m_backgroundVisible(false)
433 {
432 {
434
433
435 }
434 }
@@ -441,7 +440,7 QLegendPrivate::~QLegendPrivate()
441
440
442 void QLegendPrivate::setOffset(qreal x, qreal y)
441 void QLegendPrivate::setOffset(qreal x, qreal y)
443 {
442 {
444 m_layout->setOffset(x,y);
443 m_layout->setOffset(x, y);
445 }
444 }
446
445
447 QPointF QLegendPrivate::offset() const
446 QPointF QLegendPrivate::offset() const
@@ -451,7 +450,7 QPointF QLegendPrivate::offset() const
451
450
452 int QLegendPrivate::roundness(qreal size)
451 int QLegendPrivate::roundness(qreal size)
453 {
452 {
454 return 100*m_diameter/int(size);
453 return 100 * m_diameter / int(size);
455 }
454 }
456
455
457 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
456 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
@@ -460,12 +459,12 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
460
459
461 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
460 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
462
461
463 foreach (LegendMarker* marker, markers) {
462 foreach (LegendMarker *marker, markers) {
464 marker->setFont(m_font);
463 marker->setFont(m_font);
465 marker->setLabelBrush(m_labelBrush);
464 marker->setLabelBrush(m_labelBrush);
466 marker->setVisible(series->isVisible());
465 marker->setVisible(series->isVisible());
467 m_items->addToGroup(marker);
466 m_items->addToGroup(marker);
468 m_markers<<marker;
467 m_markers << marker;
469 }
468 }
470
469
471 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
470 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
@@ -494,10 +493,9 void QLegendPrivate::handleSeriesVisibleChanged()
494 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
493 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
495 Q_ASSERT(series);
494 Q_ASSERT(series);
496
495
497 foreach (LegendMarker* marker, m_markers) {
496 foreach (LegendMarker *marker, m_markers) {
498 if (marker->series() == series) {
497 if (marker->series() == series)
499 marker->setVisible(series->isVisible());
498 marker->setVisible(series->isVisible());
500 }
501 }
499 }
502 m_layout->invalidate();
500 m_layout->invalidate();
503 }
501 }
@@ -78,9 +78,8 private:
78 bool m_attachedToChart;
78 bool m_attachedToChart;
79 bool m_backgroundVisible;
79 bool m_backgroundVisible;
80
80
81 friend class QLegend;
81 friend class QLegend;
82 friend class LegendLayout;
82 friend class LegendLayout;
83
84 };
83 };
85
84
86 QTCOMMERCIALCHART_END_NAMESPACE
85 QTCOMMERCIALCHART_END_NAMESPACE
@@ -29,14 +29,14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 const qreal mouseEventMinWidth(12);
30 const qreal mouseEventMinWidth(12);
31
31
32 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):
32 LineChartItem::LineChartItem(QLineSeries* series, ChartPresenter *presenter)
33 XYChart(series, presenter),
33 : XYChart(series, presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
35 m_series(series),
35 m_series(series),
36 m_pointsVisible(false)
36 m_pointsVisible(false)
37 {
37 {
38 setZValue(ChartPresenter::LineChartZValue);
38 setZValue(ChartPresenter::LineChartZValue);
39 QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
39 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
40 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
40 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
41 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
41 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
42 handleUpdated();
42 handleUpdated();
@@ -44,7 +44,7 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):
44
44
45 QRectF LineChartItem::boundingRect() const
45 QRectF LineChartItem::boundingRect() const
46 {
46 {
47 return m_rect;
47 return m_rect;
48 }
48 }
49
49
50 QPainterPath LineChartItem::shape() const
50 QPainterPath LineChartItem::shape() const
@@ -56,8 +56,7 void LineChartItem::updateGeometry()
56 {
56 {
57 m_points = geometryPoints();
57 m_points = geometryPoints();
58
58
59 if(m_points.size()==0)
59 if (m_points.size() == 0) {
60 {
61 prepareGeometryChange();
60 prepareGeometryChange();
62 m_path = QPainterPath();
61 m_path = QPainterPath();
63 m_rect = QRect();
62 m_rect = QRect();
@@ -66,20 +65,18 void LineChartItem::updateGeometry()
66
65
67 QPainterPath linePath(m_points.at(0));
66 QPainterPath linePath(m_points.at(0));
68
67
69 if(m_pointsVisible) {
68 if (m_pointsVisible) {
70
69
71 int size = m_linePen.width();
70 int size = m_linePen.width();
72 linePath.addEllipse(m_points.at(0),size,size);
71 linePath.addEllipse(m_points.at(0), size, size);
73 for(int i=1; i< m_points.size();i++) {
72 for (int i = 1; i < m_points.size(); i++) {
74 linePath.lineTo(m_points.at(i));
73 linePath.lineTo(m_points.at(i));
75 linePath.addEllipse(m_points.at(i),size,size);
74 linePath.addEllipse(m_points.at(i), size, size);
76 }
75 }
77
76
78 } else {
77 } else {
79
78 for (int i = 1; i < m_points.size(); i++)
80 for(int i=1; i< m_points.size();i++) {
81 linePath.lineTo(m_points.at(i));
79 linePath.lineTo(m_points.at(i));
82 }
83 }
80 }
84
81
85 m_linePath = linePath;
82 m_linePath = linePath;
@@ -122,10 +119,9 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
122
119
123 if (m_pointsVisible) {
120 if (m_pointsVisible) {
124 painter->drawPath(m_linePath);
121 painter->drawPath(m_linePath);
125 }
122 } else {
126 else {
123 for (int i(1); i < m_points.size(); i++)
127 for (int i(1); i < m_points.size();i++)
124 painter->drawLine(m_points.at(i - 1), m_points.at(i));
128 painter->drawLine(m_points.at(i-1), m_points.at(i));
129 }
125 }
130 painter->restore();
126 painter->restore();
131 }
127 }
@@ -41,10 +41,10 class ChartPresenter;
41
41
42 class LineChartItem : public XYChart , public QGraphicsItem
42 class LineChartItem : public XYChart , public QGraphicsItem
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 Q_INTERFACES(QGraphicsItem)
45 Q_INTERFACES(QGraphicsItem)
46 public:
46 public:
47 explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter);
47 explicit LineChartItem(QLineSeries *series, ChartPresenter *presenter);
48 ~LineChartItem() {}
48 ~LineChartItem() {}
49
49
50 //from QGraphicsItem
50 //from QGraphicsItem
@@ -32,13 +32,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 \mainclass
33 \mainclass
34
34
35 A line chart is used to show information as a series of data points
35 A line chart is used to show information as a series of data points
36 connected by straight lines.
36 connected by straight lines.
37
37
38 \image examples_linechart.png
38 \image examples_linechart.png
39
39
40 Creating basic line chart is simple:
40 Creating basic line chart is simple:
41 \code
41 \code
42 QLineSeries* series = new QLineSeries();
42 QLineSeries* series = new QLineSeries();
43 series->append(0, 6);
43 series->append(0, 6);
44 series->append(2, 4);
44 series->append(2, 4);
@@ -98,7 +98,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
98 Constructs empty series object which is a child of \a parent.
98 Constructs empty series object which is a child of \a parent.
99 When series object is added to QChartView or QChart instance ownerships is transferred.
99 When series object is added to QChartView or QChart instance ownerships is transferred.
100 */
100 */
101 QLineSeries::QLineSeries(QObject *parent) : QXYSeries(*new QLineSeriesPrivate(this),parent)
101 QLineSeries::QLineSeries(QObject *parent)
102 : QXYSeries(*new QLineSeriesPrivate(this), parent)
102 {
103 {
103
104
104 }
105 }
@@ -106,7 +107,8 QLineSeries::QLineSeries(QObject *parent) : QXYSeries(*new QLineSeriesPrivate(th
106 /*!
107 /*!
107 \internal
108 \internal
108 */
109 */
109 QLineSeries::QLineSeries(QLineSeriesPrivate &d,QObject *parent) : QXYSeries (d,parent)
110 QLineSeries::QLineSeries(QLineSeriesPrivate &d, QObject *parent)
111 : QXYSeries(d, parent)
110 {
112 {
111
113
112 }
114 }
@@ -117,9 +119,8 QLineSeries::QLineSeries(QLineSeriesPrivate &d,QObject *parent) : QXYSeries (d,p
117 QLineSeries::~QLineSeries()
119 QLineSeries::~QLineSeries()
118 {
120 {
119 Q_D(QLineSeries);
121 Q_D(QLineSeries);
120 if(d->m_dataset){
122 if (d->m_dataset)
121 d->m_dataset->removeSeries(this);
123 d->m_dataset->removeSeries(this);
122 }
123 }
124 }
124
125
125 QAbstractSeries::SeriesType QLineSeries::type() const
126 QAbstractSeries::SeriesType QLineSeries::type() const
@@ -141,7 +142,7 QDebug operator<< (QDebug debug, const QLineSeries series)
141
142
142 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
143 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
143
144
144 QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries* q):QXYSeriesPrivate(q)
145 QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries* q): QXYSeriesPrivate(q)
145 {
146 {
146
147
147 };
148 };
@@ -149,10 +150,9 QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries* q):QXYSeriesPrivate(q)
149 ChartElement* QLineSeriesPrivate::createGraphics(ChartPresenter* presenter)
150 ChartElement* QLineSeriesPrivate::createGraphics(ChartPresenter* presenter)
150 {
151 {
151 Q_Q(QLineSeries);
152 Q_Q(QLineSeries);
152 LineChartItem* line = new LineChartItem(q,presenter);
153 LineChartItem* line = new LineChartItem(q, presenter);
153 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
154 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
154 line->setAnimation(new XYAnimation(line));
155 line->setAnimation(new XYAnimation(line));
155 }
156 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
156 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
157 return line;
157 return line;
158 }
158 }
@@ -39,7 +39,7 public:
39 QAbstractSeries::SeriesType type() const;
39 QAbstractSeries::SeriesType type() const;
40
40
41 protected:
41 protected:
42 QLineSeries(QLineSeriesPrivate &d,QObject *parent = 0);
42 QLineSeries(QLineSeriesPrivate &d, QObject *parent = 0);
43
43
44 private:
44 private:
45 Q_DECLARE_PRIVATE(QLineSeries)
45 Q_DECLARE_PRIVATE(QLineSeries)
@@ -33,9 +33,9
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
35 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
36 :ChartItem(presenter),
36 : ChartItem(presenter),
37 m_series(series),
37 m_series(series),
38 m_animation(0)
38 m_animation(0)
39 {
39 {
40 Q_ASSERT(series);
40 Q_ASSERT(series);
41
41
@@ -62,7 +62,7 PieChartItem::~PieChartItem()
62
62
63 void PieChartItem::setAnimation(PieAnimation* animation)
63 void PieChartItem::setAnimation(PieAnimation* animation)
64 {
64 {
65 m_animation=animation;
65 m_animation = animation;
66 }
66 }
67
67
68 ChartAnimation* PieChartItem::animation() const
68 ChartAnimation* PieChartItem::animation() const
@@ -129,13 +129,12 void PieChartItem::updateLayout()
129 m_holeSize *= m_series->holeSize();
129 m_holeSize *= m_series->holeSize();
130
130
131 // set layouts for existing slice items
131 // set layouts for existing slice items
132 foreach (QPieSlice* slice, m_series->slices()) {
132 foreach (QPieSlice *slice, m_series->slices()) {
133 PieSliceItem *sliceItem = m_sliceItems.value(slice);
133 PieSliceItem *sliceItem = m_sliceItems.value(slice);
134 if (sliceItem) {
134 if (sliceItem) {
135 PieSliceData sliceData = updateSliceGeometry(slice);
135 PieSliceData sliceData = updateSliceGeometry(slice);
136 if (m_animation){
136 if (m_animation)
137 presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData));
137 presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData));
138 }
139 else
138 else
140 sliceItem->setLayout(sliceData);
139 sliceItem->setLayout(sliceData);
141 }
140 }
@@ -33,13 +33,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 QPointF offset(qreal angle, qreal length)
34 QPointF offset(qreal angle, qreal length)
35 {
35 {
36 qreal dx = qSin(angle*(M_PI/180)) * length;
36 qreal dx = qSin(angle * (M_PI / 180)) * length;
37 qreal dy = qCos(angle*(M_PI/180)) * length;
37 qreal dy = qCos(angle * (M_PI / 180)) * length;
38 return QPointF(dx, -dy);
38 return QPointF(dx, -dy);
39 }
39 }
40
40
41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
42 :QGraphicsObject(parent),
42 : QGraphicsObject(parent),
43 m_hovered(false)
43 m_hovered(false)
44 {
44 {
45 setAcceptHoverEvents(true);
45 setAcceptHoverEvents(true);
@@ -85,8 +85,7 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*op
85 painter->setBrush(m_data.m_labelBrush);
85 painter->setBrush(m_data.m_labelBrush);
86 painter->setFont(m_data.m_labelFont);
86 painter->setFont(m_data.m_labelFont);
87
87
88 switch (m_data.m_labelPosition)
88 switch (m_data.m_labelPosition) {
89 {
90 case QPieSlice::LabelOutside:
89 case QPieSlice::LabelOutside:
91 painter->setClipRect(parentItem()->boundingRect());
90 painter->setClipRect(parentItem()->boundingRect());
92 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
91 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
@@ -163,13 +162,12 void PieSliceItem::updateGeometry()
163 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
162 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
164
163
165 // text position
164 // text position
166 switch (m_data.m_labelPosition)
165 switch (m_data.m_labelPosition) {
167 {
168 case QPieSlice::LabelOutside:
166 case QPieSlice::LabelOutside:
169 m_labelTextRect.moveBottomLeft(labelTextStart);
167 m_labelTextRect.moveBottomLeft(labelTextStart);
170 break;
168 break;
171 case QPieSlice::LabelInsideHorizontal:
169 case QPieSlice::LabelInsideHorizontal:
172 case QPieSlice::LabelInsideTangential:{
170 case QPieSlice::LabelInsideTangential: {
173 QPointF textCenter;
171 QPointF textCenter;
174 if (m_data.m_holeRadius > 0)
172 if (m_data.m_holeRadius > 0)
175 textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2);
173 textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2);
@@ -177,8 +175,8 void PieSliceItem::updateGeometry()
177 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
175 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
178 m_labelTextRect.moveCenter(textCenter);
176 m_labelTextRect.moveCenter(textCenter);
179 break;
177 break;
180 }
178 }
181 case QPieSlice::LabelInsideNormal:{
179 case QPieSlice::LabelInsideNormal: {
182 QPointF textCenter;
180 QPointF textCenter;
183 if (m_data.m_holeRadius > 0)
181 if (m_data.m_holeRadius > 0)
184 textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2);
182 textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2);
@@ -186,7 +184,7 void PieSliceItem::updateGeometry()
186 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
184 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
187 m_labelTextRect.moveCenter(textCenter);
185 m_labelTextRect.moveCenter(textCenter);
188 break;
186 break;
189 }
187 }
190 }
188 }
191
189
192 // bounding rect
190 // bounding rect
@@ -199,7 +197,7 void PieSliceItem::updateGeometry()
199 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
197 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
200 {
198 {
201 if (slice->isExploded()) {
199 if (slice->isExploded()) {
202 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
200 qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2);
203 qreal len = radius * slice->explodeDistanceFactor();
201 qreal len = radius * slice->explodeDistanceFactor();
204 point += offset(centerAngle, len);
202 point += offset(centerAngle, len);
205 }
203 }
@@ -209,15 +207,15 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
209 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
207 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
210 {
208 {
211 // calculate center angle
209 // calculate center angle
212 *centerAngle = startAngle + (angleSpan/2);
210 *centerAngle = startAngle + (angleSpan / 2);
213
211
214 // calculate slice rectangle
212 // calculate slice rectangle
215 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
213 QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2);
216
214
217 // slice path
215 // slice path
218 QPainterPath path;
216 QPainterPath path;
219 if (m_data.m_holeRadius > 0) {
217 if (m_data.m_holeRadius > 0) {
220 QRectF insideRect(center.x() - m_data.m_holeRadius, center.y()-m_data.m_holeRadius, m_data.m_holeRadius*2, m_data.m_holeRadius*2);
218 QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2);
221 path.arcMoveTo(rect, -startAngle + 90);
219 path.arcMoveTo(rect, -startAngle + 90);
222 path.arcTo(rect, -startAngle + 90, -angleSpan);
220 path.arcTo(rect, -startAngle + 90, -angleSpan);
223 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
221 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
@@ -263,9 +261,8 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length
263 if (angle < 180) { // arm swings the other way on the left side
261 if (angle < 180) { // arm swings the other way on the left side
264 parm2 += QPointF(textWidth, 0);
262 parm2 += QPointF(textWidth, 0);
265 *textStart = parm1;
263 *textStart = parm1;
266 }
264 } else {
267 else {
265 parm2 += QPointF(-textWidth, 0);
268 parm2 += QPointF(-textWidth,0);
269 *textStart = parm2;
266 *textStart = parm2;
270 }
267 }
271
268
@@ -308,7 +308,7 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
308 else
308 else
309 m_model->insertColumns(firstIndex + m_first, slices.count());
309 m_model->insertColumns(firstIndex + m_first, slices.count());
310
310
311 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
311 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
312 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
312 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
313 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
313 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
314 }
314 }
@@ -347,11 +347,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
347 /*!
347 /*!
348 Constructs a series object which is a child of \a parent.
348 Constructs a series object which is a child of \a parent.
349 */
349 */
350 QPieSeries::QPieSeries(QObject *parent) :
350 QPieSeries::QPieSeries(QObject *parent)
351 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
351 : QAbstractSeries(*new QPieSeriesPrivate(this), parent)
352 {
352 {
353 Q_D(QPieSeries);
353 Q_D(QPieSeries);
354 QObject::connect(this,SIGNAL(countChanged()),d,SIGNAL(countChanged()));
354 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
355 }
355 }
356
356
357 /*!
357 /*!
@@ -394,14 +394,14 bool QPieSeries::append(QList<QPieSlice*> slices)
394 if (slices.count() == 0)
394 if (slices.count() == 0)
395 return false;
395 return false;
396
396
397 foreach (QPieSlice* s, slices) {
397 foreach (QPieSlice *s, slices) {
398 if (!s || d->m_slices.contains(s))
398 if (!s || d->m_slices.contains(s))
399 return false;
399 return false;
400 if (s->series()) // already added to some series
400 if (s->series()) // already added to some series
401 return false;
401 return false;
402 }
402 }
403
403
404 foreach (QPieSlice* s, slices) {
404 foreach (QPieSlice *s, slices) {
405 s->setParent(this);
405 s->setParent(this);
406 QPieSlicePrivate::fromSlice(s)->m_series = this;
406 QPieSlicePrivate::fromSlice(s)->m_series = this;
407 d->m_slices << s;
407 d->m_slices << s;
@@ -409,7 +409,7 bool QPieSeries::append(QList<QPieSlice*> slices)
409
409
410 d->updateDerivativeData();
410 d->updateDerivativeData();
411
411
412 foreach (QPieSlice* s, slices) {
412 foreach (QPieSlice *s, slices) {
413 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
413 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
414 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
414 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
415 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
415 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
@@ -540,7 +540,7 void QPieSeries::clear()
540 return;
540 return;
541
541
542 QList<QPieSlice*> slices = d->m_slices;
542 QList<QPieSlice*> slices = d->m_slices;
543 foreach (QPieSlice* s, d->m_slices)
543 foreach (QPieSlice *s, d->m_slices)
544 d->m_slices.removeOne(s);
544 d->m_slices.removeOne(s);
545
545
546 d->updateDerivativeData();
546 d->updateDerivativeData();
@@ -548,7 +548,7 void QPieSeries::clear()
548 emit removed(slices);
548 emit removed(slices);
549 emit countChanged();
549 emit countChanged();
550
550
551 foreach (QPieSlice* s, slices)
551 foreach (QPieSlice *s, slices)
552 delete s;
552 delete s;
553 }
553 }
554
554
@@ -719,7 +719,7 qreal QPieSeries::pieEndAngle() const
719 void QPieSeries::setLabelsVisible(bool visible)
719 void QPieSeries::setLabelsVisible(bool visible)
720 {
720 {
721 Q_D(QPieSeries);
721 Q_D(QPieSeries);
722 foreach (QPieSlice* s, d->m_slices)
722 foreach (QPieSlice *s, d->m_slices)
723 s->setLabelVisible(visible);
723 s->setLabelVisible(visible);
724 }
724 }
725
725
@@ -734,7 +734,7 void QPieSeries::setLabelsVisible(bool visible)
734 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
734 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
735 {
735 {
736 Q_D(QPieSeries);
736 Q_D(QPieSeries);
737 foreach (QPieSlice* s, d->m_slices)
737 foreach (QPieSlice *s, d->m_slices)
738 s->setLabelPosition(position);
738 s->setLabelPosition(position);
739 }
739 }
740
740
@@ -761,7 +761,7 void QPieSeriesPrivate::updateDerivativeData()
761 {
761 {
762 // calculate sum of all slices
762 // calculate sum of all slices
763 qreal sum = 0;
763 qreal sum = 0;
764 foreach (QPieSlice* s, m_slices)
764 foreach (QPieSlice *s, m_slices)
765 sum += s->value();
765 sum += s->value();
766
766
767 if (!qFuzzyIsNull(m_sum - sum)) {
767 if (!qFuzzyIsNull(m_sum - sum)) {
@@ -777,7 +777,7 void QPieSeriesPrivate::updateDerivativeData()
777 qreal sliceAngle = m_pieStartAngle;
777 qreal sliceAngle = m_pieStartAngle;
778 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
778 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
779 QVector<QPieSlice*> changed;
779 QVector<QPieSlice*> changed;
780 foreach (QPieSlice* s, m_slices) {
780 foreach (QPieSlice *s, m_slices) {
781 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
781 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
782 d->setPercentage(s->value() / m_sum);
782 d->setPercentage(s->value() / m_sum);
783 d->setStartAngle(sliceAngle);
783 d->setStartAngle(sliceAngle);
@@ -843,10 +843,9 void QPieSeriesPrivate::scaleDomain(Domain& domain)
843 ChartElement* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
843 ChartElement* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
844 {
844 {
845 Q_Q(QPieSeries);
845 Q_Q(QPieSeries);
846 PieChartItem* pie = new PieChartItem(q,presenter);
846 PieChartItem* pie = new PieChartItem(q, presenter);
847 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
847 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
848 pie->setAnimation(new PieAnimation(pie));
848 pie->setAnimation(new PieAnimation(pie));
849 }
850 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
849 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
851 return pie;
850 return pie;
852 }
851 }
@@ -855,8 +854,8 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
855 {
854 {
856 Q_Q(QPieSeries);
855 Q_Q(QPieSeries);
857 QList<LegendMarker*> markers;
856 QList<LegendMarker*> markers;
858 foreach(QPieSlice* slice, q->slices()) {
857 foreach (QPieSlice *slice, q->slices()) {
859 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
858 PieLegendMarker* marker = new PieLegendMarker(q, slice, legend);
860 markers << marker;
859 markers << marker;
861 }
860 }
862 return markers;
861 return markers;
@@ -435,8 +435,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
435 \sa QPieSeries::append(), QPieSeries::insert()
435 \sa QPieSeries::append(), QPieSeries::insert()
436 */
436 */
437 QPieSlice::QPieSlice(QObject *parent)
437 QPieSlice::QPieSlice(QObject *parent)
438 :QObject(parent),
438 : QObject(parent),
439 d_ptr(new QPieSlicePrivate(this))
439 d_ptr(new QPieSlicePrivate(this))
440 {
440 {
441
441
442 }
442 }
@@ -446,8 +446,8 QPieSlice::QPieSlice(QObject *parent)
446 \sa QPieSeries::append(), QPieSeries::insert()
446 \sa QPieSeries::append(), QPieSeries::insert()
447 */
447 */
448 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
448 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
449 :QObject(parent),
449 : QObject(parent),
450 d_ptr(new QPieSlicePrivate(this))
450 d_ptr(new QPieSlicePrivate(this))
451 {
451 {
452 setValue(value);
452 setValue(value);
453 setLabel(label);
453 setLabel(label);
@@ -677,9 +677,9 QPieSeries *QPieSlice::series() const
677 }
677 }
678
678
679 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
679 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
680 :QObject(parent),
680 : QObject(parent),
681 q_ptr(parent),
681 q_ptr(parent),
682 m_series(0)
682 m_series(0)
683 {
683 {
684
684
685 }
685 }
@@ -137,7 +137,8 QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) :
137 */
137 */
138 QAbstractSeries::~QAbstractSeries()
138 QAbstractSeries::~QAbstractSeries()
139 {
139 {
140 if(d_ptr->m_dataset) qFatal("Still binded series detected !");
140 if (d_ptr->m_dataset)
141 qFatal("Still binded series detected !");
141 }
142 }
142
143
143 void QAbstractSeries::setName(const QString& name)
144 void QAbstractSeries::setName(const QString& name)
@@ -223,12 +224,12 void QAbstractSeries::hide()
223
224
224 ///////////////////////////////////////////////////////////////////////////////////////////////////
225 ///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
226 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q):
227 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q)
227 q_ptr(q),
228 : q_ptr(q),
228 m_chart(0),
229 m_chart(0),
229 m_dataset(0),
230 m_dataset(0),
230 m_visible(true),
231 m_visible(true),
231 m_opacity(1.0)
232 m_opacity(1.0)
232 {
233 {
233 }
234 }
234
235
@@ -104,11 +104,12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
104 /*!
104 /*!
105 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
105 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
106 */
106 */
107 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
107 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
108 d_ptr(new QChartPrivate())
108 : QGraphicsWidget(parent, wFlags),
109 d_ptr(new QChartPrivate())
109 {
110 {
110 d_ptr->m_dataset = new ChartDataSet(this);
111 d_ptr->m_dataset = new ChartDataSet(this);
111 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
112 d_ptr->m_presenter = new ChartPresenter(this, d_ptr->m_dataset);
112 d_ptr->createConnections();
113 d_ptr->createConnections();
113 d_ptr->m_legend = new LegendScroller(this);
114 d_ptr->m_legend = new LegendScroller(this);
114 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
115 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
@@ -124,7 +125,7 QChart::~QChart()
124 //delete first presenter , since this is a root of all the graphical items
125 //delete first presenter , since this is a root of all the graphical items
125 setLayout(0);
126 setLayout(0);
126 delete d_ptr->m_presenter;
127 delete d_ptr->m_presenter;
127 d_ptr->m_presenter=0;
128 d_ptr->m_presenter = 0;
128 }
129 }
129
130
130 /*!
131 /*!
@@ -353,7 +354,7 QAbstractAxis* QChart::axisY(QAbstractSeries *series) const
353 */
354 */
354 void QChart::createDefaultAxes()
355 void QChart::createDefaultAxes()
355 {
356 {
356 d_ptr->m_dataset->createDefaultAxes();
357 d_ptr->m_dataset->createDefaultAxes();
357 }
358 }
358
359
359 /*!
360 /*!
@@ -457,7 +458,7 QList<QAbstractSeries*> QChart::series() const
457 */
458 */
458 void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series)
459 void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series)
459 {
460 {
460 d_ptr->m_dataset->setAxis(series,axis,Qt::Horizontal);
461 d_ptr->m_dataset->setAxis(series, axis, Qt::Horizontal);
461 }
462 }
462
463
463 /*!
464 /*!
@@ -465,9 +466,9 void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series)
465
466
466 \sa axisX(), axisY(), setAxisX(), createDefaultAxes()
467 \sa axisX(), axisY(), setAxisX(), createDefaultAxes()
467 */
468 */
468 void QChart::setAxisY( QAbstractAxis* axis , QAbstractSeries *series)
469 void QChart::setAxisY(QAbstractAxis *axis , QAbstractSeries *series)
469 {
470 {
470 d_ptr->m_dataset->setAxis(series,axis,Qt::Vertical);
471 d_ptr->m_dataset->setAxis(series, axis, Qt::Vertical);
471 }
472 }
472
473
473 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
474 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -487,10 +488,10 QChartPrivate::~QChartPrivate()
487
488
488 void QChartPrivate::createConnections()
489 void QChartPrivate::createConnections()
489 {
490 {
490 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
491 QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
491 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*)));
492 QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*)));
492 QObject::connect(m_dataset,SIGNAL(axisAdded(QAbstractAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAbstractAxis*,Domain*)));
493 QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*,Domain*)));
493 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAbstractAxis*)),m_presenter,SLOT(handleAxisRemoved(QAbstractAxis*)));
494 QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)),m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*)));
494 //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF)));
495 //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF)));
495 }
496 }
496
497
@@ -61,7 +61,7 public:
61 enum AnimationOption {
61 enum AnimationOption {
62 NoAnimation = 0x0,
62 NoAnimation = 0x0,
63 GridAxisAnimations = 0x1,
63 GridAxisAnimations = 0x1,
64 SeriesAnimations =0x2,
64 SeriesAnimations = 0x2,
65 AllAnimations = 0x3
65 AllAnimations = 0x3
66 };
66 };
67
67
@@ -47,7 +47,6 struct QChartPrivate
47 ChartPresenter *m_presenter;
47 ChartPresenter *m_presenter;
48
48
49 void createConnections();
49 void createConnections();
50
51 };
50 };
52
51
53 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
@@ -74,14 +74,14
74
74
75 static inline QDebug chartDebug(int numargs,...)
75 static inline QDebug chartDebug(int numargs,...)
76 {
76 {
77 va_list valist;
77 va_list valist;
78 va_start(valist,numargs);
78 va_start(valist, numargs);
79 //for( int i = 0 ; i < numargs; i++ )
79 //for( int i = 0 ; i < numargs; i++ )
80 int line=va_arg(valist,int);
80 int line = va_arg(valist, int);
81 char* file=va_arg(valist,char*);
81 char* file = va_arg(valist, char*);
82 char* function=va_arg(valist,char*);
82 char* function = va_arg(valist, char*);
83 va_end(valist);
83 va_end(valist);
84 return qDebug()<<QString().append(function).append("(").append(file).append(":%1)").arg(line);
84 return qDebug() << QString().append(function).append("(").append(file).append(":%1)").arg(line);
85 }
85 }
86 #endif
86 #endif
87
87
@@ -53,9 +53,9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
53 Constructs a chartView object with parent \a parent.
53 Constructs a chartView object with parent \a parent.
54 */
54 */
55
55
56 QChartView::QChartView(QWidget *parent) :
56 QChartView::QChartView(QWidget *parent)
57 QGraphicsView(parent),
57 : QGraphicsView(parent),
58 d_ptr(new QChartViewPrivate(this))
58 d_ptr(new QChartViewPrivate(this))
59 {
59 {
60
60
61 }
61 }
@@ -64,9 +64,9 QChartView::QChartView(QWidget *parent) :
64 Constructs a chartView object with parent \a parent to display a \a chart.
64 Constructs a chartView object with parent \a parent to display a \a chart.
65 */
65 */
66
66
67 QChartView::QChartView(QChart *chart,QWidget *parent) :
67 QChartView::QChartView(QChart *chart, QWidget *parent)
68 QGraphicsView(parent),
68 : QGraphicsView(parent),
69 d_ptr(new QChartViewPrivate(this,chart))
69 d_ptr(new QChartViewPrivate(this, chart))
70 {
70 {
71
71
72 }
72 }
@@ -104,11 +104,11 void QChartView::setChart(QChart *chart)
104 */
104 */
105 void QChartView::setRubberBand(const RubberBands& rubberBand)
105 void QChartView::setRubberBand(const RubberBands& rubberBand)
106 {
106 {
107 d_ptr->m_rubberBandFlags=rubberBand;
107 d_ptr->m_rubberBandFlags = rubberBand;
108
108
109 if (!d_ptr->m_rubberBandFlags) {
109 if (!d_ptr->m_rubberBandFlags) {
110 delete d_ptr->m_rubberBand;
110 delete d_ptr->m_rubberBand;
111 d_ptr->m_rubberBand=0;
111 d_ptr->m_rubberBand = 0;
112 return;
112 return;
113 }
113 }
114
114
@@ -132,7 +132,7 QChartView::RubberBands QChartView::rubberBand() const
132 */
132 */
133 void QChartView::mousePressEvent(QMouseEvent *event)
133 void QChartView::mousePressEvent(QMouseEvent *event)
134 {
134 {
135 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
135 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
136
136
137 QRectF plotArea = d_ptr->m_chart->plotArea();
137 QRectF plotArea = d_ptr->m_chart->plotArea();
138
138
@@ -142,8 +142,7 void QChartView::mousePressEvent(QMouseEvent *event)
142 d_ptr->m_rubberBand->show();
142 d_ptr->m_rubberBand->show();
143 event->accept();
143 event->accept();
144 }
144 }
145 }
145 } else {
146 else {
147 QGraphicsView::mousePressEvent(event);
146 QGraphicsView::mousePressEvent(event);
148 }
147 }
149 }
148 }
@@ -154,7 +153,7 void QChartView::mousePressEvent(QMouseEvent *event)
154 */
153 */
155 void QChartView::mouseMoveEvent(QMouseEvent *event)
154 void QChartView::mouseMoveEvent(QMouseEvent *event)
156 {
155 {
157 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
156 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
158 QRect rect = d_ptr->m_chart->plotArea().toRect();
157 QRect rect = d_ptr->m_chart->plotArea().toRect();
159 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
158 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
160 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
159 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
@@ -164,11 +163,10 void QChartView::mouseMoveEvent(QMouseEvent *event)
164 }
163 }
165 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
164 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
166 d_ptr->m_rubberBandOrigin.setX(rect.left());
165 d_ptr->m_rubberBandOrigin.setX(rect.left());
167 width= rect.width();
166 width = rect.width();
168 }
167 }
169 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
168 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized());
170 }
169 } else {
171 else {
172 QGraphicsView::mouseMoveEvent(event);
170 QGraphicsView::mouseMoveEvent(event);
173 }
171 }
174 }
172 }
@@ -179,7 +177,7 void QChartView::mouseMoveEvent(QMouseEvent *event)
179 */
177 */
180 void QChartView::mouseReleaseEvent(QMouseEvent *event)
178 void QChartView::mouseReleaseEvent(QMouseEvent *event)
181 {
179 {
182 if(d_ptr->m_rubberBand) {
180 if (d_ptr->m_rubberBand) {
183 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
181 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
184 d_ptr->m_rubberBand->hide();
182 d_ptr->m_rubberBand->hide();
185 QRect rect = d_ptr->m_rubberBand->geometry();
183 QRect rect = d_ptr->m_rubberBand->geometry();
@@ -187,12 +185,11 void QChartView::mouseReleaseEvent(QMouseEvent *event)
187 event->accept();
185 event->accept();
188 }
186 }
189
187
190 if(event->button()==Qt::RightButton){
188 if (event->button() == Qt::RightButton) {
191 d_ptr->m_chart->zoomOut();
189 d_ptr->m_chart->zoomOut();
192 event->accept();
190 event->accept();
193 }
191 }
194 }
192 } else {
195 else {
196 QGraphicsView::mouseReleaseEvent(event);
193 QGraphicsView::mouseReleaseEvent(event);
197 }
194 }
198 }
195 }
@@ -208,12 +205,12 void QChartView::resizeEvent(QResizeEvent *event)
208
205
209 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
206 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
210
207
211 QChartViewPrivate::QChartViewPrivate(QChartView *q,QChart* chart):
208 QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart* chart)
212 q_ptr(q),
209 : q_ptr(q),
213 m_scene(new QGraphicsScene(q)),
210 m_scene(new QGraphicsScene(q)),
214 m_chart(chart),
211 m_chart(chart),
215 m_rubberBand(0),
212 m_rubberBand(0),
216 m_rubberBandFlags(QChartView::NoRubberBand)
213 m_rubberBandFlags(QChartView::NoRubberBand)
217 {
214 {
218 q_ptr->setFrameShape(QFrame::NoFrame);
215 q_ptr->setFrameShape(QFrame::NoFrame);
219 q_ptr->setBackgroundRole(QPalette::Window);
216 q_ptr->setBackgroundRole(QPalette::Window);
@@ -221,7 +218,8 m_rubberBandFlags(QChartView::NoRubberBand)
221 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
218 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
222 q_ptr->setScene(m_scene);
219 q_ptr->setScene(m_scene);
223 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
220 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
224 if(!m_chart) m_chart = new QChart();
221 if (!m_chart)
222 m_chart = new QChart();
225 m_scene->addItem(m_chart);
223 m_scene->addItem(m_chart);
226 }
224 }
227
225
@@ -39,7 +39,7 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
39 Q_ENUMS(RubberBand)
39 Q_ENUMS(RubberBand)
40 public:
40 public:
41
41
42 enum RubberBand{
42 enum RubberBand {
43 NoRubberBand = 0x0,
43 NoRubberBand = 0x0,
44 VerticalRubberBand = 0x1,
44 VerticalRubberBand = 0x1,
45 HorizonalRubberBand = 0x2,
45 HorizonalRubberBand = 0x2,
@@ -49,7 +49,7 public:
49 Q_DECLARE_FLAGS(RubberBands, RubberBand)
49 Q_DECLARE_FLAGS(RubberBands, RubberBand)
50
50
51 explicit QChartView(QWidget *parent = 0);
51 explicit QChartView(QWidget *parent = 0);
52 explicit QChartView(QChart *chart,QWidget *parent = 0);
52 explicit QChartView(QChart *chart, QWidget *parent = 0);
53 ~QChartView();
53 ~QChartView();
54
54
55 void setRubberBand(const RubberBands& rubberBands);
55 void setRubberBand(const RubberBands& rubberBands);
@@ -40,8 +40,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 class QChart;
40 class QChart;
41 class QChartView;
41 class QChartView;
42
42
43 class QChartViewPrivate {
43 class QChartViewPrivate
44
44 {
45 public:
45 public:
46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
47 ~QChartViewPrivate();
47 ~QChartViewPrivate();
@@ -128,7 +128,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
128 /*!
128 /*!
129 Constructs a series object which is a child of \a parent.
129 Constructs a series object which is a child of \a parent.
130 */
130 */
131 QScatterSeries::QScatterSeries(QObject *parent) : QXYSeries(*new QScatterSeriesPrivate(this),parent)
131 QScatterSeries::QScatterSeries(QObject *parent)
132 : QXYSeries(*new QScatterSeriesPrivate(this), parent)
132 {
133 {
133 }
134 }
134
135
@@ -138,9 +139,8 QScatterSeries::QScatterSeries(QObject *parent) : QXYSeries(*new QScatterSeriesP
138 QScatterSeries::~QScatterSeries()
139 QScatterSeries::~QScatterSeries()
139 {
140 {
140 Q_D(QScatterSeries);
141 Q_D(QScatterSeries);
141 if(d->m_dataset) {
142 if (d->m_dataset)
142 d->m_dataset->removeSeries(this);
143 d->m_dataset->removeSeries(this);
143 }
144 }
144 }
145
145
146 QAbstractSeries::SeriesType QScatterSeries::type() const
146 QAbstractSeries::SeriesType QScatterSeries::type() const
@@ -253,10 +253,9 QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries* q) :
253 ChartElement* QScatterSeriesPrivate::createGraphics(ChartPresenter* presenter)
253 ChartElement* QScatterSeriesPrivate::createGraphics(ChartPresenter* presenter)
254 {
254 {
255 Q_Q(QScatterSeries);
255 Q_Q(QScatterSeries);
256 ScatterChartItem *scatter = new ScatterChartItem(q,presenter);
256 ScatterChartItem *scatter = new ScatterChartItem(q, presenter);
257 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
257 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
258 scatter->setAnimation(new XYAnimation(scatter));
258 scatter->setAnimation(new XYAnimation(scatter));
259 }
260 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
259 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
261 return scatter;
260 return scatter;
262 }
261 }
@@ -29,16 +29,16
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) :
32 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter)
33 XYChart(series,presenter),
33 : XYChart(series, presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
35 m_series(series),
35 m_series(series),
36 m_items(this),
36 m_items(this),
37 m_visible(true),
37 m_visible(true),
38 m_shape(QScatterSeries::MarkerShapeRectangle),
38 m_shape(QScatterSeries::MarkerShapeRectangle),
39 m_size(15)
39 m_size(15)
40 {
40 {
41 QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated()));
41 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
42 QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
42 QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
43 QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
43 QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
44
44
@@ -62,21 +62,20 void ScatterChartItem::createPoints(int count)
62 QGraphicsItem *item = 0;
62 QGraphicsItem *item = 0;
63
63
64 switch (m_shape) {
64 switch (m_shape) {
65 case QScatterSeries::MarkerShapeCircle: {
65 case QScatterSeries::MarkerShapeCircle: {
66 item = new CircleMarker(0,0,m_size,m_size,this);
66 item = new CircleMarker(0, 0, m_size, m_size, this);
67 const QRectF& rect = item->boundingRect();
67 const QRectF& rect = item->boundingRect();
68 item->setPos(-rect.width()/2,-rect.height()/2);
68 item->setPos(-rect.width() / 2, -rect.height() / 2);
69 break;
69 break;
70 }
70 }
71 case QScatterSeries::MarkerShapeRectangle: {
71 case QScatterSeries::MarkerShapeRectangle: {
72 item = new RectangleMarker(0,0,m_size,m_size,this);
72 item = new RectangleMarker(0, 0, m_size, m_size, this);
73 item->setPos(-m_size/2,-m_size/2);
73 item->setPos(-m_size / 2, -m_size / 2);
74 break;
74 break;
75 }
75 }
76 default:
76 default:
77 qWarning()<<"Unsupported marker type";
77 qWarning() << "Unsupported marker type";
78 break;
78 break;
79
80 }
79 }
81 m_items.addToGroup(item);
80 m_items.addToGroup(item);
82 }
81 }
@@ -103,22 +102,20 void ScatterChartItem::updateGeometry()
103
102
104 const QVector<QPointF>& points = geometryPoints();
103 const QVector<QPointF>& points = geometryPoints();
105
104
106 if(points.size()==0)
105 if (points.size() == 0) {
107 {
108 deletePoints(m_items.childItems().count());
106 deletePoints(m_items.childItems().count());
109 return;
107 return;
110 }
108 }
111
109
112 int diff = m_items.childItems().size() - points.size();
110 int diff = m_items.childItems().size() - points.size();
113
111
114 if(diff>0) {
112 if (diff > 0)
115 deletePoints(diff);
113 deletePoints(diff);
116 }
114 else if (diff < 0)
117 else if(diff<0) {
118 createPoints(-diff);
115 createPoints(-diff);
119 }
120
116
121 if(diff!=0) handleUpdated();
117 if (diff != 0)
118 handleUpdated();
122
119
123 QList<QGraphicsItem*> items = m_items.childItems();
120 QList<QGraphicsItem*> items = m_items.childItems();
124
121
@@ -126,14 +123,12 void ScatterChartItem::updateGeometry()
126 QGraphicsItem* item = items.at(i);
123 QGraphicsItem* item = items.at(i);
127 const QPointF& point = points.at(i);
124 const QPointF& point = points.at(i);
128 const QRectF& rect = item->boundingRect();
125 const QRectF& rect = item->boundingRect();
129 m_markerMap[item]=point;
126 m_markerMap[item] = point;
130 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
127 item->setPos(point.x() - rect.width() / 2, point.y() - rect.height() / 2);
131 if(!m_visible || !clipRect().contains(point)) {
128 if (!m_visible || !clipRect().contains(point))
132 item->setVisible(false);
129 item->setVisible(false);
133 }
130 else
134 else {
135 item->setVisible(true);
131 item->setVisible(true);
136 }
137 }
132 }
138
133
139 prepareGeometryChange();
134 prepareGeometryChange();
@@ -150,34 +145,33 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *
150
145
151 void ScatterChartItem::setPen(const QPen& pen)
146 void ScatterChartItem::setPen(const QPen& pen)
152 {
147 {
153 foreach(QGraphicsItem* item , m_items.childItems()) {
148 foreach (QGraphicsItem *item , m_items.childItems())
154 static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen);
149 static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen);
155 }
156 }
150 }
157
151
158 void ScatterChartItem::setBrush(const QBrush& brush)
152 void ScatterChartItem::setBrush(const QBrush& brush)
159 {
153 {
160 foreach(QGraphicsItem* item , m_items.childItems()) {
154 foreach (QGraphicsItem *item , m_items.childItems())
161 static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush);
155 static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush);
162 }
163 }
156 }
164
157
165 void ScatterChartItem::handleUpdated()
158 void ScatterChartItem::handleUpdated()
166 {
159 {
167 int count = m_items.childItems().count();
160 int count = m_items.childItems().count();
168
161
169 if(count==0) return;
162 if (count == 0)
163 return;
170
164
171 bool recreate = m_visible != m_series->isVisible()
165 bool recreate = m_visible != m_series->isVisible()
172 || m_size != m_series->markerSize()
166 || m_size != m_series->markerSize()
173 || m_shape != m_series->markerShape();
167 || m_shape != m_series->markerShape();
174
168
175 m_visible = m_series->isVisible();
169 m_visible = m_series->isVisible();
176 m_size = m_series->markerSize();
170 m_size = m_series->markerSize();
177 m_shape = m_series->markerShape();
171 m_shape = m_series->markerShape();
178 setOpacity(m_series->opacity());
172 setOpacity(m_series->opacity());
179
173
180 if(recreate) {
174 if (recreate) {
181 // TODO: optimize handleUpdate to recreate points only in case shape changed
175 // TODO: optimize handleUpdate to recreate points only in case shape changed
182 deletePoints(count);
176 deletePoints(count);
183 createPoints(count);
177 createPoints(count);
@@ -80,40 +80,42 class CircleMarker: public QGraphicsEllipseItem
80 {
80 {
81
81
82 public:
82 public:
83 CircleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) : QGraphicsEllipseItem(x,y,w,h,parent),
83 CircleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent)
84 m_parent(parent)
84 : QGraphicsEllipseItem(x, y, w, h, parent),
85 m_parent(parent)
85 {
86 {
86 }
87 }
87
88
88 protected:
89 protected:
89 void mousePressEvent(QGraphicsSceneMouseEvent *event)
90 void mousePressEvent(QGraphicsSceneMouseEvent *event)
90 {
91 {
91 m_parent->markerSelected(this);
92 m_parent->markerSelected(this);
92 QGraphicsEllipseItem::mousePressEvent(event);
93 QGraphicsEllipseItem::mousePressEvent(event);
93 }
94 }
94
95
95 private:
96 private:
96 ScatterChartItem* m_parent;
97 ScatterChartItem* m_parent;
97 };
98 };
98
99
99 class RectangleMarker: public QGraphicsRectItem
100 class RectangleMarker: public QGraphicsRectItem
100 {
101 {
101
102
102 public:
103 public:
103 RectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) : QGraphicsRectItem(x,y,w,h,parent),
104 RectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent)
104 m_parent(parent)
105 : QGraphicsRectItem(x, y, w, h, parent),
105 {
106 m_parent(parent)
106 }
107 {
108 }
107
109
108 protected:
110 protected:
109 void mousePressEvent(QGraphicsSceneMouseEvent *event)
111 void mousePressEvent(QGraphicsSceneMouseEvent *event)
110 {
112 {
111 m_parent->markerSelected(this);
113 m_parent->markerSelected(this);
112 QGraphicsRectItem::mousePressEvent(event);
114 QGraphicsRectItem::mousePressEvent(event);
113 }
115 }
114
116
115 private:
117 private:
116 ScatterChartItem* m_parent;
118 ScatterChartItem* m_parent;
117 };
119 };
118
120
119 QTCOMMERCIALCHART_END_NAMESPACE
121 QTCOMMERCIALCHART_END_NAMESPACE
@@ -25,11 +25,11
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 Scroller::Scroller():
28 Scroller::Scroller()
29 m_ticker(this),
29 : m_ticker(this),
30 m_state(Idle),
30 m_state(Idle),
31 m_moveThreshold(10),
31 m_moveThreshold(10),
32 m_timeTreshold(50)
32 m_timeTreshold(50)
33 {
33 {
34
34
35 }
35 }
@@ -41,30 +41,25 Scroller::~Scroller()
41 void Scroller::mousePressEvent(QGraphicsSceneMouseEvent* event)
41 void Scroller::mousePressEvent(QGraphicsSceneMouseEvent* event)
42 {
42 {
43 if (event->button() == Qt::LeftButton) {
43 if (event->button() == Qt::LeftButton) {
44
45 switch (m_state) {
44 switch (m_state) {
46 case Idle:
45 case Idle:
47 {
48 m_state = Pressed;
46 m_state = Pressed;
49 m_offset = offset();
47 m_offset = offset();
50 m_press = event->pos();
48 m_press = event->pos();
51 m_timeStamp = QTime::currentTime();
49 m_timeStamp = QTime::currentTime();
52 event->accept();
50 event->accept();
53 break;
51 break;
54 }
55 case Scroll:
52 case Scroll:
56 {
57 m_state = Stop;
53 m_state = Stop;
58 m_speed = QPointF(0, 0);
54 m_speed = QPointF(0, 0);
59 m_offset = offset();
55 m_offset = offset();
60 m_press = event->pos();
56 m_press = event->pos();
61 event->accept();
57 event->accept();
62 break;
58 break;
63 }
64 case Pressed:
59 case Pressed:
65 case Move:
60 case Move:
66 case Stop:
61 case Stop:
67 qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state;
62 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
68 event->ignore();
63 event->ignore();
69 break;
64 break;
70 }
65 }
@@ -78,34 +73,27 void Scroller::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
78 switch (m_state) {
73 switch (m_state) {
79 case Pressed:
74 case Pressed:
80 case Stop:
75 case Stop:
81 {
82 if (qAbs(delta.x()) > m_moveThreshold || qAbs(delta.y()) > m_moveThreshold) {
76 if (qAbs(delta.x()) > m_moveThreshold || qAbs(delta.y()) > m_moveThreshold) {
83 m_state = Move;
77 m_state = Move;
84 m_timeStamp = QTime::currentTime();
78 m_timeStamp = QTime::currentTime();
85 m_distance = QPointF(0, 0);
79 m_distance = QPointF(0, 0);
86 m_press = event->pos();
80 m_press = event->pos();
87 event->accept();
81 event->accept();
88 break;
82 } else {
89 }
90 else {
91 event->ignore();
83 event->ignore();
92 break;
93 }
84 }
94 }
85 break;
95 case Move:
86 case Move:
96 {
97 setOffset(m_offset - delta);
87 setOffset(m_offset - delta);
98 calculateSpeed(event->pos());
88 calculateSpeed(event->pos());
99 event->accept();
89 event->accept();
100 break;
90 break;
101 }
102 case Idle:
91 case Idle:
103 case Scroll:
92 case Scroll:
104 qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state;
93 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
105 event->ignore();
94 event->ignore();
106 break;
95 break;
107 }
96 }
108
109 }
97 }
110
98
111 void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
99 void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
@@ -121,7 +109,6 void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
121 event->accept();
109 event->accept();
122 break;
110 break;
123 case Pressed:
111 case Pressed:
124 {
125 m_state = Idle;
112 m_state = Idle;
126 //if (m_timeStamp.elapsed() < m_clickedPressDelay) {
113 //if (m_timeStamp.elapsed() < m_clickedPressDelay) {
127
114
@@ -129,30 +116,24 void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
129 //}
116 //}
130 event->accept();
117 event->accept();
131 break;
118 break;
132 }
133 case Move:
119 case Move:
134 {
135 calculateSpeed(event->pos());
120 calculateSpeed(event->pos());
136 m_offset = offset();
121 m_offset = offset();
137 m_press = event->pos();
122 m_press = event->pos();
138 if (m_speed == QPointF(0, 0)) {
123 if (m_speed == QPointF(0, 0)) {
139 m_state = Idle;
124 m_state = Idle;
140 }
125 } else {
141 else {
142 m_speed /= 3.75;
126 m_speed /= 3.75;
143 m_state = Scroll;
127 m_state = Scroll;
144 m_ticker.start(25);
128 m_ticker.start(25);
145 }
129 }
146 event->accept();
130 event->accept();
147 break;
131 break;
148 }
149
150 case Stop:
132 case Stop:
151 case Idle:
133 case Idle:
152 qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state;
134 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
153 event->ignore();
135 event->ignore();
154 break;
136 break;
155
156 }
137 }
157 }
138 }
158 }
139 }
@@ -160,25 +141,23 void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
160 void Scroller::scrollTick()
141 void Scroller::scrollTick()
161 {
142 {
162 switch (m_state) {
143 switch (m_state) {
163 case Scroll:
144 case Scroll:
164 {
145 lowerSpeed(m_speed);
165 lowerSpeed(m_speed);
146 setOffset(m_offset - m_speed);
166 setOffset(m_offset - m_speed);
147 m_offset = offset();
167 m_offset = offset();
148 if (m_speed == QPointF(0, 0)) {
168 if (m_speed == QPointF(0, 0)) {
149 m_state = Idle;
169 m_state = Idle;
170 m_ticker.stop();
171 }
172 break;
173 }
174 case Stop:
175 m_ticker.stop();
176 break;
177 case Idle:
178 case Move:
179 case Pressed:
180 qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state;
181 m_ticker.stop();
150 m_ticker.stop();
151 }
152 break;
153 case Stop:
154 m_ticker.stop();
155 break;
156 case Idle:
157 case Move:
158 case Pressed:
159 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
160 m_ticker.stop();
182 break;
161 break;
183
162
184 }
163 }
@@ -212,8 +191,7 void Scroller::calculateSpeed(const QPointF& position)
212 if (fraction != 0) {
191 if (fraction != 0) {
213 m_fraction.setX(qAbs(m_speed.x() / fraction));
192 m_fraction.setX(qAbs(m_speed.x() / fraction));
214 m_fraction.setY(qAbs(m_speed.y() / fraction));
193 m_fraction.setY(qAbs(m_speed.y() / fraction));
215 }
194 } else {
216 else {
217 m_fraction.setX(1);
195 m_fraction.setX(1);
218 m_fraction.setY(1);
196 m_fraction.setY(1);
219 }
197 }
@@ -222,17 +200,17 void Scroller::calculateSpeed(const QPointF& position)
222
200
223 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
224
202
225 ScrollTicker::ScrollTicker(Scroller *scroller,QObject* parent):QObject(parent),
203 ScrollTicker::ScrollTicker(Scroller *scroller, QObject* parent)
226 m_scroller(scroller)
204 : QObject(parent),
205 m_scroller(scroller)
227 {
206 {
228
207
229 }
208 }
230
209
231 void ScrollTicker::start(int interval)
210 void ScrollTicker::start(int interval)
232 {
211 {
233 if (!m_timer.isActive()){
212 if (!m_timer.isActive())
234 m_timer.start(interval, this);
213 m_timer.start(interval, this);
235 }
236 }
214 }
237
215
238 void ScrollTicker::stop()
216 void ScrollTicker::stop()
@@ -46,7 +46,7 class ScrollTicker : public QObject
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 public:
48 public:
49 explicit ScrollTicker(Scroller *scroller,QObject *parent = 0);
49 explicit ScrollTicker(Scroller *scroller, QObject *parent = 0);
50 void start(int interval);
50 void start(int interval);
51 void stop();
51 void stop();
52 protected:
52 protected:
@@ -77,7 +77,6 public:
77 public:
77 public:
78 void scrollTick();
78 void scrollTick();
79
79
80
81 public:
80 public:
82 void mousePressEvent(QGraphicsSceneMouseEvent* event);
81 void mousePressEvent(QGraphicsSceneMouseEvent* event);
83 void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
82 void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
@@ -85,7 +84,7 public:
85
84
86 private:
85 private:
87 void calculateSpeed(const QPointF& position);
86 void calculateSpeed(const QPointF& position);
88 void lowerSpeed(QPointF& speed,qreal maxSpeed=100);
87 void lowerSpeed(QPointF& speed, qreal maxSpeed = 100);
89
88
90 private:
89 private:
91 ScrollTicker m_ticker;
90 ScrollTicker m_ticker;
@@ -98,8 +97,6 private:
98 QPointF m_fraction;
97 QPointF m_fraction;
99 int m_moveThreshold;
98 int m_moveThreshold;
100 int m_timeTreshold;
99 int m_timeTreshold;
101
102
103 };
100 };
104
101
105 QTCOMMERCIALCHART_END_NAMESPACE
102 QTCOMMERCIALCHART_END_NAMESPACE
@@ -98,14 +98,14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
98 When series object is added to QChartView or QChart instance then the ownerships is transferred.
98 When series object is added to QChartView or QChart instance then the ownerships is transferred.
99 */
99 */
100
100
101 QSplineSeries::QSplineSeries(QObject *parent) :
101 QSplineSeries::QSplineSeries(QObject *parent)
102 QLineSeries(*new QSplineSeriesPrivate(this),parent)
102 : QLineSeries(*new QSplineSeriesPrivate(this), parent)
103 {
103 {
104 Q_D(QSplineSeries);
104 Q_D(QSplineSeries);
105 QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
105 QObject::connect(this, SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
106 QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
106 QObject::connect(this, SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
107 QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
107 QObject::connect(this, SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
108 QObject::connect(this,SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints()));
108 QObject::connect(this, SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints()));
109 }
109 }
110
110
111 /*!
111 /*!
@@ -114,9 +114,8 QSplineSeries::QSplineSeries(QObject *parent) :
114 QSplineSeries::~QSplineSeries()
114 QSplineSeries::~QSplineSeries()
115 {
115 {
116 Q_D(QSplineSeries);
116 Q_D(QSplineSeries);
117 if(d->m_dataset){
117 if (d->m_dataset)
118 d->m_dataset->removeSeries(this);
118 d->m_dataset->removeSeries(this);
119 }
120 }
119 }
121
120
122 QAbstractSeries::SeriesType QSplineSeries::type() const
121 QAbstractSeries::SeriesType QSplineSeries::type() const
@@ -126,7 +125,8 QAbstractSeries::SeriesType QSplineSeries::type() const
126
125
127 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
128
127
129 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
128 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q)
129 : QLineSeriesPrivate(q)
130 {
130 {
131 }
131 }
132
132
@@ -141,8 +141,7 void QSplineSeriesPrivate::calculateControlPoints()
141
141
142 int n = points.count() - 1;
142 int n = points.count() - 1;
143
143
144 if (n == 1)
144 if (n == 1) {
145 {
146 //for n==1
145 //for n==1
147 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
146 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
148 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
147 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
@@ -169,35 +168,33 void QSplineSeriesPrivate::calculateControlPoints()
169 vector[0] = points[0].x() + 2 * points[1].x();
168 vector[0] = points[0].x() + 2 * points[1].x();
170
169
171
170
172 for (int i = 1; i < n - 1; ++i){
171 for (int i = 1; i < n - 1; ++i)
173 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
172 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
174 }
175
173
176 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
174 vector[n - 1] = (8 * points[n - 1].x() + points[n].x()) / 2.0;
177
175
178 QVector<qreal> xControl = firstControlPoints(vector);
176 QVector<qreal> xControl = firstControlPoints(vector);
179
177
180 vector[0] = points[0].y() + 2 * points[1].y();
178 vector[0] = points[0].y() + 2 * points[1].y();
181
179
182 for (int i = 1; i < n - 1; ++i) {
180 for (int i = 1; i < n - 1; ++i)
183 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
181 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
184 }
185
182
186 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
183 vector[n - 1] = (8 * points[n - 1].y() + points[n].y()) / 2.0;
187
184
188 QVector<qreal> yControl = firstControlPoints(vector);
185 QVector<qreal> yControl = firstControlPoints(vector);
189
186
190 for (int i = 0,j =0; i < n; ++i, ++j) {
187 for (int i = 0, j = 0; i < n; ++i, ++j) {
191
188
192 m_controlPoints[j].setX(xControl[i]);
189 m_controlPoints[j].setX(xControl[i]);
193 m_controlPoints[j].setY(yControl[i]);
190 m_controlPoints[j].setY(yControl[i]);
194
191
195 j++;
192 j++;
196
193
197 if (i < n - 1){
194 if (i < n - 1) {
198 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
195 m_controlPoints[j].setX(2 * points[i + 1].x() - xControl[i + 1]);
199 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
196 m_controlPoints[j].setY(2 * points[i + 1].y() - yControl[i + 1]);
200 }else{
197 } else {
201 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
198 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
202 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
199 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
203 }
200 }
@@ -221,8 +218,9 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& ve
221 for (int i = 1; i < count; i++) {
218 for (int i = 1; i < count; i++) {
222 temp[i] = 1 / b;
219 temp[i] = 1 / b;
223 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
220 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
224 result[i]=(vector[i] - result[i - 1]) / b;
221 result[i] = (vector[i] - result[i - 1]) / b;
225 }
222 }
223
226 for (int i = 1; i < count; i++)
224 for (int i = 1; i < count; i++)
227 result[count - i - 1] -= temp[count - i] * result[count - i];
225 result[count - i - 1] -= temp[count - i] * result[count - i];
228
226
@@ -241,7 +239,7 void QSplineSeriesPrivate::updateControlPoints()
241 {
239 {
242 Q_Q(QSplineSeries);
240 Q_Q(QSplineSeries);
243 if (q->count() > 1) {
241 if (q->count() > 1) {
244 m_controlPoints.resize(2*q->count()-2);
242 m_controlPoints.resize(2 * q->count() - 2);
245 calculateControlPoints();
243 calculateControlPoints();
246 }
244 }
247 }
245 }
@@ -249,11 +247,9 void QSplineSeriesPrivate::updateControlPoints()
249 ChartElement* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
247 ChartElement* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
250 {
248 {
251 Q_Q(QSplineSeries);
249 Q_Q(QSplineSeries);
252 SplineChartItem* spline = new SplineChartItem(q,presenter);
250 SplineChartItem *spline = new SplineChartItem(q, presenter);
253 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
251 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
254 spline->setAnimation(new SplineAnimation(spline));
252 spline->setAnimation(new SplineAnimation(spline));
255 }
256
257 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
253 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
258 return spline;
254 return spline;
259 }
255 }
@@ -27,15 +27,15
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) :
30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter)
31 XYChart(series, presenter),
31 : XYChart(series, presenter),
32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 m_series(series),
33 m_series(series),
34 m_pointsVisible(false),
34 m_pointsVisible(false),
35 m_animation(0)
35 m_animation(0)
36 {
36 {
37 setZValue(ChartPresenter::SplineChartZValue);
37 setZValue(ChartPresenter::SplineChartZValue);
38 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
38 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
39 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
39 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
40 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
40 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
41 handleUpdated();
41 handleUpdated();
@@ -53,7 +53,7 QPainterPath SplineChartItem::shape() const
53
53
54 void SplineChartItem::setAnimation(SplineAnimation* animation)
54 void SplineChartItem::setAnimation(SplineAnimation* animation)
55 {
55 {
56 m_animation=animation;
56 m_animation = animation;
57 XYChart::setAnimation(animation);
57 XYChart::setAnimation(animation);
58 }
58 }
59
59
@@ -64,7 +64,7 ChartAnimation* SplineChartItem::animation() const
64
64
65 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
65 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
66 {
66 {
67 m_controlPoints=points;
67 m_controlPoints = points;
68 }
68 }
69
69
70 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
70 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
@@ -72,16 +72,15 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
72 return m_controlPoints;
72 return m_controlPoints;
73 }
73 }
74
74
75 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
75 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
76 {
76 {
77 QVector<QPointF> controlPoints;
77 QVector<QPointF> controlPoints;
78
78
79 if(newPoints.count()>=2) {
79 if (newPoints.count() >= 2)
80 controlPoints.resize(newPoints.count()*2-2);
80 controlPoints.resize(newPoints.count() * 2 - 2);
81 }
82
81
83 for (int i = 0; i < newPoints.size() - 1; i++) {
82 for (int i = 0; i < newPoints.size() - 1; i++) {
84 controlPoints[2*i] = calculateGeometryControlPoint(2 * i);
83 controlPoints[2 * i] = calculateGeometryControlPoint(2 * i);
85 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
84 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
86 }
85 }
87
86
@@ -92,11 +91,10 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF>
92 m_controlPoints = controlPoints;
91 m_controlPoints = controlPoints;
93 setDirty(false);
92 setDirty(false);
94
93
95 if (m_animation) {
94 if (m_animation)
96 presenter()->startAnimation(m_animation);
95 presenter()->startAnimation(m_animation);
97 } else {
96 else
98 updateGeometry();
97 updateGeometry();
99 }
100 }
98 }
101
99
102 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
100 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
@@ -109,20 +107,20 void SplineChartItem::updateGeometry()
109 const QVector<QPointF> &points = m_points;
107 const QVector<QPointF> &points = m_points;
110 const QVector<QPointF> &controlPoints = m_controlPoints;
108 const QVector<QPointF> &controlPoints = m_controlPoints;
111
109
112 if ((points.size()<2) || (controlPoints.size()<2)) {
110 if ((points.size() < 2) || (controlPoints.size() < 2)) {
113 prepareGeometryChange();
111 prepareGeometryChange();
114 m_path = QPainterPath();
112 m_path = QPainterPath();
115 m_rect = QRect();
113 m_rect = QRect();
116 return;
114 return;
117 }
115 }
118
116
119 Q_ASSERT(points.count()*2-2 == controlPoints.count());
117 Q_ASSERT(points.count() * 2 - 2 == controlPoints.count());
120
118
121 QPainterPath splinePath(points.at(0));
119 QPainterPath splinePath(points.at(0));
122
120
123 for (int i = 0; i < points.size() - 1; i++) {
121 for (int i = 0; i < points.size() - 1; i++) {
124 const QPointF& point = points.at(i + 1);
122 const QPointF& point = points.at(i + 1);
125 splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point);
123 splinePath.cubicTo(controlPoints[2 * i], controlPoints[2 * i + 1], point);
126 }
124 }
127
125
128 prepareGeometryChange();
126 prepareGeometryChange();
@@ -140,7 +138,7 void SplineChartItem::handleUpdated()
140 m_pointsVisible = m_series->pointsVisible();
138 m_pointsVisible = m_series->pointsVisible();
141 m_linePen = m_series->pen();
139 m_linePen = m_series->pen();
142 m_pointPen = m_series->pen();
140 m_pointPen = m_series->pen();
143 m_pointPen.setWidthF(2*m_pointPen.width());
141 m_pointPen.setWidthF(2 * m_pointPen.width());
144 update();
142 update();
145 }
143 }
146
144
@@ -60,7 +60,7 public Q_SLOTS:
60
60
61 protected:
61 protected:
62 void updateGeometry();
62 void updateGeometry();
63 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
63 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index);
64 void mousePressEvent(QGraphicsSceneMouseEvent *event);
64 void mousePressEvent(QGraphicsSceneMouseEvent *event);
65
65
66 private:
66 private:
@@ -37,8 +37,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 class ChartThemeBlueCerulean: public ChartTheme
37 class ChartThemeBlueCerulean: public ChartTheme
38 {
38 {
39 public:
39 public:
40 ChartThemeBlueCerulean() : ChartTheme(QChart::ChartThemeBlueCerulean)
40 ChartThemeBlueCerulean() : ChartTheme(QChart::ChartThemeBlueCerulean) {
41 {
42 // Series colors
41 // Series colors
43 m_seriesColors << QRgb(0xc7e85b);
42 m_seriesColors << QRgb(0xc7e85b);
44 m_seriesColors << QRgb(0x1cb54f);
43 m_seriesColors << QRgb(0x1cb54f);
@@ -74,8 +74,8 public:
74 GetBValue(colorGradientActiveCaptionLeft));
74 GetBValue(colorGradientActiveCaptionLeft));
75 g.setColorAt(0.0, start);
75 g.setColorAt(0.0, start);
76 QColor end = QColor(GetRValue(colorGradientActiveCaptionRight),
76 QColor end = QColor(GetRValue(colorGradientActiveCaptionRight),
77 GetGValue(colorGradientActiveCaptionRight),
77 GetGValue(colorGradientActiveCaptionRight),
78 GetBValue(colorGradientActiveCaptionRight));
78 GetBValue(colorGradientActiveCaptionRight));
79 g.setColorAt(1.0, end);
79 g.setColorAt(1.0, end);
80 m_seriesColors.append(colorAt(g, 0.5));
80 m_seriesColors.append(colorAt(g, 0.5));
81
81
@@ -87,11 +87,11 public:
87 DWORD colorWindow;
87 DWORD colorWindow;
88 colorWindow = GetSysColor(COLOR_WINDOW);
88 colorWindow = GetSysColor(COLOR_WINDOW);
89 backgroundGradient.setColorAt(0.0, QColor(GetRValue(colorWindow),
89 backgroundGradient.setColorAt(0.0, QColor(GetRValue(colorWindow),
90 GetGValue(colorWindow),
90 GetGValue(colorWindow),
91 GetBValue(colorWindow)));
91 GetBValue(colorWindow)));
92 backgroundGradient.setColorAt(1.0, QColor(GetRValue(colorWindow),
92 backgroundGradient.setColorAt(1.0, QColor(GetRValue(colorWindow),
93 GetGValue(colorWindow),
93 GetGValue(colorWindow),
94 GetBValue(colorWindow)));
94 GetBValue(colorWindow)));
95 // Axes and other
95 // Axes and other
96 m_axisLinePen = QPen(0xd6d6d6);
96 m_axisLinePen = QPen(0xd6d6d6);
97 m_axisLinePen.setWidth(1);
97 m_axisLinePen.setWidth(1);
@@ -43,7 +43,7 public:
43
43
44 QXYSeries* series() const;
44 QXYSeries* series() const;
45 void setSeries(QXYSeries *series);
45 void setSeries(QXYSeries *series);
46
46
47 int xRow() const;
47 int xRow() const;
48 void setXRow(int xRow);
48 void setXRow(int xRow);
49
49
@@ -43,12 +43,12 public:
43
43
44 QXYSeries* series() const;
44 QXYSeries* series() const;
45 void setSeries(QXYSeries *series);
45 void setSeries(QXYSeries *series);
46
46
47 int xColumn() const;
47 int xColumn() const;
48 void setXColumn(int xColumn);
48 void setXColumn(int xColumn);
49
49
50 int yColumn() const;
50 int yColumn() const;
51 void setYColumn(int yColumn);
51 void setYColumn(int yColumn);
52
52
53 int firstRow() const;
53 int firstRow() const;
54 void setFirstRow(int firstRow);
54 void setFirstRow(int firstRow);
@@ -52,9 +52,8 void QXYModelMapper::setModel(QAbstractItemModel *model)
52 return;
52 return;
53
53
54 Q_D(QXYModelMapper);
54 Q_D(QXYModelMapper);
55 if (d->m_model) {
55 if (d->m_model)
56 disconnect(d->m_model, 0, d, 0);
56 disconnect(d->m_model, 0, d, 0);
57 }
58
57
59 d->m_model = model;
58 d->m_model = model;
60 d->initializeXYFromModel();
59 d->initializeXYFromModel();
@@ -82,9 +81,8 QXYSeries* QXYModelMapper::series() const
82 void QXYModelMapper::setSeries(QXYSeries *series)
81 void QXYModelMapper::setSeries(QXYSeries *series)
83 {
82 {
84 Q_D(QXYModelMapper);
83 Q_D(QXYModelMapper);
85 if (d->m_series) {
84 if (d->m_series)
86 disconnect(d->m_series, 0, d, 0);
85 disconnect(d->m_series, 0, d, 0);
87 }
88
86
89 if (series == 0)
87 if (series == 0)
90 return;
88 return;
@@ -173,7 +173,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
173 Constructs empty series object which is a child of \a parent.
173 Constructs empty series object which is a child of \a parent.
174 When series object is added to QChartView or QChart instance ownerships is transferred.
174 When series object is added to QChartView or QChart instance ownerships is transferred.
175 */
175 */
176 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
176 QXYSeries::QXYSeries(QXYSeriesPrivate &d, QObject *parent)
177 : QAbstractSeries(d, parent)
177 {
178 {
178 }
179 }
179
180
@@ -188,9 +189,9 QXYSeries::~QXYSeries()
188 /*!
189 /*!
189 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
190 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
190 */
191 */
191 void QXYSeries::append(qreal x,qreal y)
192 void QXYSeries::append(qreal x, qreal y)
192 {
193 {
193 append(QPointF(x,y));
194 append(QPointF(x, y));
194 }
195 }
195
196
196 /*!
197 /*!
@@ -200,8 +201,8 void QXYSeries::append(qreal x,qreal y)
200 void QXYSeries::append(const QPointF &point)
201 void QXYSeries::append(const QPointF &point)
201 {
202 {
202 Q_D(QXYSeries);
203 Q_D(QXYSeries);
203 d->m_points<<point;
204 d->m_points << point;
204 emit pointAdded(d->m_points.count()-1);
205 emit pointAdded(d->m_points.count() - 1);
205 }
206 }
206
207
207 /*!
208 /*!
@@ -210,29 +211,29 void QXYSeries::append(const QPointF &point)
210 */
211 */
211 void QXYSeries::append(const QList<QPointF> &points)
212 void QXYSeries::append(const QList<QPointF> &points)
212 {
213 {
213 foreach(const QPointF& point , points) {
214 foreach (const QPointF &point , points)
214 append(point);
215 append(point);
215 }
216 }
216 }
217
217
218 /*!
218 /*!
219 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
219 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
220 \sa QXYSeries::pointReplaced()
220 \sa QXYSeries::pointReplaced()
221 */
221 */
222 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
222 void QXYSeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY)
223 {
223 {
224 replace(QPointF(oldX,oldY),QPointF(newX,newY));
224 replace(QPointF(oldX, oldY), QPointF(newX, newY));
225 }
225 }
226
226
227 /*!
227 /*!
228 Replaces \a oldPoint with \a newPoint.
228 Replaces \a oldPoint with \a newPoint.
229 \sa QXYSeries::pointReplaced()
229 \sa QXYSeries::pointReplaced()
230 */
230 */
231 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
231 void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint)
232 {
232 {
233 Q_D(QXYSeries);
233 Q_D(QXYSeries);
234 int index = d->m_points.indexOf(oldPoint);
234 int index = d->m_points.indexOf(oldPoint);
235 if(index==-1) return;
235 if (index == -1)
236 return;
236 d->m_points[index] = newPoint;
237 d->m_points[index] = newPoint;
237 emit pointReplaced(index);
238 emit pointReplaced(index);
238 }
239 }
@@ -253,9 +254,9 void QXYSeries::replace(QList<QPointF> points)
253 /*!
254 /*!
254 Removes current \a x and \a y value.
255 Removes current \a x and \a y value.
255 */
256 */
256 void QXYSeries::remove(qreal x,qreal y)
257 void QXYSeries::remove(qreal x, qreal y)
257 {
258 {
258 remove(QPointF(x,y));
259 remove(QPointF(x, y));
259 }
260 }
260
261
261 /*!
262 /*!
@@ -267,7 +268,8 void QXYSeries::remove(const QPointF &point)
267 {
268 {
268 Q_D(QXYSeries);
269 Q_D(QXYSeries);
269 int index = d->m_points.indexOf(point);
270 int index = d->m_points.indexOf(point);
270 if(index==-1) return;
271 if (index == -1)
272 return;
271 d->m_points.remove(index);
273 d->m_points.remove(index);
272 emit pointRemoved(index);
274 emit pointRemoved(index);
273 }
275 }
@@ -342,7 +344,7 QPen QXYSeries::pen() const
342 void QXYSeries::setBrush(const QBrush &brush)
344 void QXYSeries::setBrush(const QBrush &brush)
343 {
345 {
344 Q_D(QXYSeries);
346 Q_D(QXYSeries);
345 if (d->m_brush!=brush) {
347 if (d->m_brush != brush) {
346 d->m_brush = brush;
348 d->m_brush = brush;
347 emit d->updated();
349 emit d->updated();
348 }
350 }
@@ -371,7 +373,7 QColor QXYSeries::color() const
371 void QXYSeries::setPointsVisible(bool visible)
373 void QXYSeries::setPointsVisible(bool visible)
372 {
374 {
373 Q_D(QXYSeries);
375 Q_D(QXYSeries);
374 if (d->m_pointsVisible != visible){
376 if (d->m_pointsVisible != visible) {
375 d->m_pointsVisible = visible;
377 d->m_pointsVisible = visible;
376 emit d->updated();
378 emit d->updated();
377 }
379 }
@@ -426,7 +428,7 void QXYSeriesPrivate::scaleDomain(Domain& domain)
426
428
427 const QList<QPointF>& points = q->points();
429 const QList<QPointF>& points = q->points();
428
430
429 if (!points.isEmpty()){
431 if (!points.isEmpty()) {
430 minX = points[0].x();
432 minX = points[0].x();
431 minY = points[0].y();
433 minY = points[0].y();
432 maxX = minX;
434 maxX = minX;
@@ -442,14 +444,14 void QXYSeriesPrivate::scaleDomain(Domain& domain)
442 }
444 }
443 }
445 }
444
446
445 domain.setRange(minX,maxX,minY,maxY);
447 domain.setRange(minX, maxX, minY, maxY);
446 }
448 }
447
449
448 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
450 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
449 {
451 {
450 Q_Q(QXYSeries);
452 Q_Q(QXYSeries);
451 QList<LegendMarker*> list;
453 QList<LegendMarker*> list;
452 return list << new XYLegendMarker(q,legend);
454 return list << new XYLegendMarker(q, legend);
453 }
455 }
454
456
455 void QXYSeriesPrivate::initializeAxis(QAbstractAxis* axis)
457 void QXYSeriesPrivate::initializeAxis(QAbstractAxis* axis)
@@ -40,15 +40,15 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
40 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
40 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
41
41
42 protected:
42 protected:
43 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
43 explicit QXYSeries(QXYSeriesPrivate &d, QObject *parent = 0);
44
44
45 public:
45 public:
46 ~QXYSeries();
46 ~QXYSeries();
47 void append(qreal x, qreal y);
47 void append(qreal x, qreal y);
48 void append(const QPointF &point);
48 void append(const QPointF &point);
49 void append(const QList<QPointF> &points);
49 void append(const QList<QPointF> &points);
50 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
50 void replace(qreal oldX, qreal oldY, qreal newX, qreal newY);
51 void replace(const QPointF &oldPoint,const QPointF &newPoint);
51 void replace(const QPointF &oldPoint, const QPointF &newPoint);
52 void remove(qreal x, qreal y);
52 void remove(qreal x, qreal y);
53 void remove(const QPointF &point);
53 void remove(const QPointF &point);
54 void insert(int index, const QPointF &point);
54 void insert(int index, const QPointF &point);
@@ -32,14 +32,15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 //TODO: optimize : remove points which are not visible
33 //TODO: optimize : remove points which are not visible
34
34
35 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):ChartElement(presenter),
35 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter)
36 m_minX(0),
36 : ChartElement(presenter),
37 m_maxX(0),
37 m_minX(0),
38 m_minY(0),
38 m_maxX(0),
39 m_maxY(0),
39 m_minY(0),
40 m_series(series),
40 m_maxY(0),
41 m_animation(0),
41 m_series(series),
42 m_dirty(true)
42 m_animation(0),
43 m_dirty(true)
43 {
44 {
44 QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
45 QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
45 QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced()));
46 QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced()));
@@ -60,44 +61,44 void XYChart::setClipRect(const QRectF &rect)
60
61
61 void XYChart::setAnimation(XYAnimation* animation)
62 void XYChart::setAnimation(XYAnimation* animation)
62 {
63 {
63 m_animation=animation;
64 m_animation = animation;
64 }
65 }
65
66
66 void XYChart::setDirty(bool dirty)
67 void XYChart::setDirty(bool dirty)
67 {
68 {
68 m_dirty=dirty;
69 m_dirty = dirty;
69 }
70 }
70
71
71 QPointF XYChart::calculateGeometryPoint(const QPointF &point) const
72 QPointF XYChart::calculateGeometryPoint(const QPointF &point) const
72 {
73 {
73 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
74 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
74 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
75 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
75 qreal x = (point.x() - m_minX)* deltaX;
76 qreal x = (point.x() - m_minX) * deltaX;
76 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
77 qreal y = (point.y() - m_minY) * -deltaY + m_size.height();
77 return QPointF(x,y);
78 return QPointF(x, y);
78 }
79 }
79
80
80 QPointF XYChart::calculateGeometryPoint(int index) const
81 QPointF XYChart::calculateGeometryPoint(int index) const
81 {
82 {
82 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
83 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
83 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
84 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
84 const QList<QPointF>& vector = m_series->points();
85 const QList<QPointF>& vector = m_series->points();
85 qreal x = (vector[index].x() - m_minX)* deltaX;
86 qreal x = (vector[index].x() - m_minX) * deltaX;
86 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
87 qreal y = (vector[index].y() - m_minY) * -deltaY + m_size.height();
87 return QPointF(x,y);
88 return QPointF(x, y);
88 }
89 }
89
90
90 QVector<QPointF> XYChart::calculateGeometryPoints() const
91 QVector<QPointF> XYChart::calculateGeometryPoints() const
91 {
92 {
92 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
93 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
93 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
94 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
94
95
95 QVector<QPointF> result;
96 QVector<QPointF> result;
96 result.resize(m_series->count());
97 result.resize(m_series->count());
97 const QList<QPointF>& vector = m_series->points();
98 const QList<QPointF>& vector = m_series->points();
98 for (int i = 0; i < m_series->count(); ++i) {
99 for (int i = 0; i < m_series->count(); ++i) {
99 qreal x = (vector[i].x() - m_minX)* deltaX;
100 qreal x = (vector[i].x() - m_minX) * deltaX;
100 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
101 qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height();
101 result[i].setX(x);
102 result[i].setX(x);
102 result[i].setY(y);
103 result[i].setY(y);
103 }
104 }
@@ -106,14 +107,14 QVector<QPointF> XYChart::calculateGeometryPoints() const
106
107
107 QPointF XYChart::calculateDomainPoint(const QPointF &point) const
108 QPointF XYChart::calculateDomainPoint(const QPointF &point) const
108 {
109 {
109 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
110 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
110 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
111 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
111 qreal x = point.x()/deltaX +m_minX;
112 qreal x = point.x() / deltaX + m_minX;
112 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
113 qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY;
113 return QPointF(x,y);
114 return QPointF(x, y);
114 }
115 }
115
116
116 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
117 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
117 {
118 {
118
119
119 if (m_animation) {
120 if (m_animation) {
@@ -121,8 +122,7 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoin
121 m_points = newPoints;
122 m_points = newPoints;
122 setDirty(false);
123 setDirty(false);
123 presenter()->startAnimation(m_animation);
124 presenter()->startAnimation(m_animation);
124 }
125 } else {
125 else {
126 m_points = newPoints;
126 m_points = newPoints;
127 updateGeometry();
127 updateGeometry();
128 }
128 }
@@ -132,55 +132,55 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoin
132
132
133 void XYChart::handlePointAdded(int index)
133 void XYChart::handlePointAdded(int index)
134 {
134 {
135 Q_ASSERT(index<m_series->count());
135 Q_ASSERT(index < m_series->count());
136 Q_ASSERT(index>=0);
136 Q_ASSERT(index >= 0);
137
137
138 QVector<QPointF> points;
138 QVector<QPointF> points;
139
139
140 if(m_dirty) {
140 if (m_dirty) {
141 points = calculateGeometryPoints();
141 points = calculateGeometryPoints();
142 } else {
142 } else {
143 points = m_points;
143 points = m_points;
144 QPointF point = calculateGeometryPoint(index);
144 QPointF point = calculateGeometryPoint(index);
145 points.insert(index, point);
145 points.insert(index, point);
146 }
146 }
147
147
148 updateChart(m_points,points,index);
148 updateChart(m_points, points, index);
149 }
149 }
150
150
151 void XYChart::handlePointRemoved(int index)
151 void XYChart::handlePointRemoved(int index)
152 {
152 {
153 Q_ASSERT(index<=m_series->count());
153 Q_ASSERT(index <= m_series->count());
154 Q_ASSERT(index>=0);
154 Q_ASSERT(index >= 0);
155
155
156 QVector<QPointF> points;
156 QVector<QPointF> points;
157
157
158 if(m_dirty) {
158 if (m_dirty) {
159 points = calculateGeometryPoints();
159 points = calculateGeometryPoints();
160 } else {
160 } else {
161 points = m_points;
161 points = m_points;
162 points.remove(index);
162 points.remove(index);
163 }
163 }
164
164
165 updateChart(m_points,points,index);
165 updateChart(m_points, points, index);
166 }
166 }
167
167
168 void XYChart::handlePointReplaced(int index)
168 void XYChart::handlePointReplaced(int index)
169 {
169 {
170 Q_ASSERT(index<m_series->count());
170 Q_ASSERT(index < m_series->count());
171 Q_ASSERT(index>=0);
171 Q_ASSERT(index >= 0);
172
172
173 QVector<QPointF> points;
173 QVector<QPointF> points;
174
174
175 if(m_dirty) {
175 if (m_dirty) {
176 points = calculateGeometryPoints();
176 points = calculateGeometryPoints();
177 } else {
177 } else {
178 QPointF point = calculateGeometryPoint(index);
178 QPointF point = calculateGeometryPoint(index);
179 points = m_points;
179 points = m_points;
180 points.replace(index,point);
180 points.replace(index, point);
181 }
181 }
182
182
183 updateChart(m_points,points,index);
183 updateChart(m_points, points, index);
184 }
184 }
185
185
186 void XYChart::handlePointsReplaced()
186 void XYChart::handlePointsReplaced()
@@ -192,29 +192,29 void XYChart::handlePointsReplaced()
192
192
193 void XYChart::handleDomainUpdated()
193 void XYChart::handleDomainUpdated()
194 {
194 {
195 m_minX=domain()->minX();
195 m_minX = domain()->minX();
196 m_maxX=domain()->maxX();
196 m_maxX = domain()->maxX();
197 m_minY=domain()->minY();
197 m_minY = domain()->minY();
198 m_maxY=domain()->maxY();
198 m_maxY = domain()->maxY();
199 if (isEmpty()) return;
199 if (isEmpty()) return;
200
200
201 QVector<QPointF> points = calculateGeometryPoints();
201 QVector<QPointF> points = calculateGeometryPoints();
202
202
203 updateChart(m_points,points);
203 updateChart(m_points, points);
204 }
204 }
205
205
206 void XYChart::handleGeometryChanged(const QRectF &rect)
206 void XYChart::handleGeometryChanged(const QRectF &rect)
207 {
207 {
208 Q_ASSERT(rect.isValid());
208 Q_ASSERT(rect.isValid());
209 m_size=rect.size();
209 m_size = rect.size();
210 m_clipRect=rect.translated(-rect.topLeft());
210 m_clipRect = rect.translated(-rect.topLeft());
211 m_origin=rect.topLeft();
211 m_origin = rect.topLeft();
212
212
213 if (isEmpty()) return;
213 if (isEmpty()) return;
214
214
215 QVector<QPointF> points = calculateGeometryPoints();
215 QVector<QPointF> points = calculateGeometryPoints();
216
216
217 updateChart(m_points,points);
217 updateChart(m_points, points);
218 }
218 }
219
219
220 bool XYChart::isEmpty()
220 bool XYChart::isEmpty()
@@ -43,9 +43,9 class QXYSeries;
43
43
44 class XYChart : public ChartElement
44 class XYChart : public ChartElement
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48 explicit XYChart(QXYSeries *series, ChartPresenter *presenter);
48 explicit XYChart(QXYSeries *series, ChartPresenter *presenter);
49 ~XYChart() {}
49 ~XYChart() {}
50
50
51 void setGeometryPoints(const QVector<QPointF>& points);
51 void setGeometryPoints(const QVector<QPointF>& points);
@@ -76,7 +76,7 Q_SIGNALS:
76 void clicked(const QPointF& point);
76 void clicked(const QPointF& point);
77
77
78 protected:
78 protected:
79 virtual void updateChart(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index = -1);
79 virtual void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index = -1);
80 QPointF calculateGeometryPoint(const QPointF &point) const;
80 QPointF calculateGeometryPoint(const QPointF &point) const;
81 QPointF calculateGeometryPoint(int index) const;
81 QPointF calculateGeometryPoint(int index) const;
82 QPointF calculateDomainPoint(const QPointF &point) const;
82 QPointF calculateDomainPoint(const QPointF &point) const;
@@ -99,7 +99,6 protected:
99 bool m_dirty;
99 bool m_dirty;
100
100
101 friend class AreaChartItem;
101 friend class AreaChartItem;
102
103 };
102 };
104
103
105 QTCOMMERCIALCHART_END_NAMESPACE
104 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now