##// END OF EJS Templates
removed handlethemechange for legend. Too complex solution. Legend now listens the changed signals from series
sauimone -
r587:f0e1920224d0
parent child
Show More
@@ -1,191 +1,193
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QToolTip>
3 #include <QToolTip>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 /*!
7 /*!
8 \class QBarSet
8 \class QBarSet
9 \brief part of QtCommercial chart API.
9 \brief part of QtCommercial chart API.
10
10
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 First value of set is assumed to belong to first category, second to second category and so on.
12 First value of set is assumed to belong to first category, second to second category and so on.
13 If set has fewer values than there are categories, then the missing values are assumed to be
13 If set has fewer values than there are categories, then the missing values are assumed to be
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn void QBarSet::clicked(QString category)
22 \fn void QBarSet::clicked(QString category)
23 \brief signals that set has been clicked
23 \brief signals that set has been clicked
24 Parameter \a category describes on which category was clicked
24 Parameter \a category describes on which category was clicked
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSet::rightClicked(QString category)
28 \fn void QBarSet::rightClicked(QString category)
29 \brief signals that set has been clicked with right mouse button
29 \brief signals that set has been clicked with right mouse button
30 Parameter \a category describes on which category was clicked
30 Parameter \a category describes on which category was clicked
31 */
31 */
32
32
33 /*!
33 /*!
34 \fn void QBarSet::hoverEnter(QPoint pos)
34 \fn void QBarSet::hoverEnter(QPoint pos)
35 \brief signals that mouse has entered over the set at position \a pos.
35 \brief signals that mouse has entered over the set at position \a pos.
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn void QBarSet::hoverLeave()
39 \fn void QBarSet::hoverLeave()
40 \brief signals that mouse has left from the set.
40 \brief signals that mouse has left from the set.
41 */
41 */
42
42
43 /*!
43 /*!
44 \fn void QBarSet::toggleFloatingValues()
44 \fn void QBarSet::toggleFloatingValues()
45 \brief \internal
45 \brief \internal
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
50 \brief \internal \a pos \a tip
50 \brief \internal \a pos \a tip
51 */
51 */
52
52
53
53
54 /*!
54 /*!
55 Constructs QBarSet with a name of \a name and with parent of \a parent
55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 */
56 */
57 QBarSet::QBarSet(QString name, QObject *parent)
57 QBarSet::QBarSet(QString name, QObject *parent)
58 : QObject(parent)
58 : QObject(parent)
59 ,mName(name)
59 ,mName(name)
60 {
60 {
61 }
61 }
62
62
63 /*!
63 /*!
64 Sets new \a name for set.
64 Sets new \a name for set.
65 */
65 */
66 void QBarSet::setName(QString name)
66 void QBarSet::setName(QString name)
67 {
67 {
68 mName = name;
68 mName = name;
69 }
69 }
70
70
71 /*!
71 /*!
72 Returns name of the set.
72 Returns name of the set.
73 */
73 */
74 QString QBarSet::name()
74 QString QBarSet::name()
75 {
75 {
76 return mName;
76 return mName;
77 }
77 }
78
78
79 /*!
79 /*!
80 Appends new value \a value to the end of set.
80 Appends new value \a value to the end of set.
81 */
81 */
82 QBarSet& QBarSet::operator << (const qreal &value)
82 QBarSet& QBarSet::operator << (const qreal &value)
83 {
83 {
84 mValues.append(value);
84 mValues.append(value);
85 return *this;
85 return *this;
86 }
86 }
87
87
88 /*!
88 /*!
89 Returns count of values in set.
89 Returns count of values in set.
90 */
90 */
91 int QBarSet::count()
91 int QBarSet::count()
92 {
92 {
93 return mValues.count();
93 return mValues.count();
94 }
94 }
95
95
96 /*!
96 /*!
97 Returns value of set indexed by \a index
97 Returns value of set indexed by \a index
98 */
98 */
99 qreal QBarSet::valueAt(int index)
99 qreal QBarSet::valueAt(int index)
100 {
100 {
101 return mValues.at(index);
101 return mValues.at(index);
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets a new value \a value to set, indexed by \a index
105 Sets a new value \a value to set, indexed by \a index
106 */
106 */
107 void QBarSet::setValue(int index, qreal value)
107 void QBarSet::setValue(int index, qreal value)
108 {
108 {
109 mValues.replace(index,value);
109 mValues.replace(index,value);
110 }
110 }
111
111
112 /*!
112 /*!
113 Returns total sum of all values in barset.
113 Returns total sum of all values in barset.
114 */
114 */
115 qreal QBarSet::total()
115 qreal QBarSet::total()
116 {
116 {
117 qreal total(0);
117 qreal total(0);
118 for (int i=0; i<mValues.count(); i++) {
118 for (int i=0; i<mValues.count(); i++) {
119 total += mValues.at(i);
119 total += mValues.at(i);
120 }
120 }
121 return total;
121 return total;
122 }
122 }
123
123
124 /*!
124 /*!
125 Sets pen for set. Bars of this set are drawn using \a pen
125 Sets pen for set. Bars of this set are drawn using \a pen
126 */
126 */
127 void QBarSet::setPen(const QPen pen)
127 void QBarSet::setPen(const QPen pen)
128 {
128 {
129 mPen = pen;
129 mPen = pen;
130 emit changed();
130 }
131 }
131
132
132 /*!
133 /*!
133 Returns pen of the set.
134 Returns pen of the set.
134 */
135 */
135 QPen QBarSet::pen() const
136 QPen QBarSet::pen() const
136 {
137 {
137 return mPen;
138 return mPen;
138 }
139 }
139
140
140 /*!
141 /*!
141 Sets brush for the set. Bars of this set are drawn using \a brush
142 Sets brush for the set. Bars of this set are drawn using \a brush
142 */
143 */
143 void QBarSet::setBrush(const QBrush brush)
144 void QBarSet::setBrush(const QBrush brush)
144 {
145 {
145 mBrush = brush;
146 mBrush = brush;
147 emit changed();
146 }
148 }
147
149
148 /*!
150 /*!
149 Returns brush of the set.
151 Returns brush of the set.
150 */
152 */
151 QBrush QBarSet::brush() const
153 QBrush QBarSet::brush() const
152 {
154 {
153 return mBrush;
155 return mBrush;
154 }
156 }
155
157
156 /*!
158 /*!
157 Sets the pen for floating values that are drawn on top of this set
159 Sets the pen for floating values that are drawn on top of this set
158 */
160 */
159 void QBarSet::setFloatingValuePen(const QPen pen)
161 void QBarSet::setFloatingValuePen(const QPen pen)
160 {
162 {
161 mFloatingValuePen = pen;
163 mFloatingValuePen = pen;
162 }
164 }
163
165
164 /*!
166 /*!
165 Returns the pen for floating values that are drawn on top of this set
167 Returns the pen for floating values that are drawn on top of this set
166 */
168 */
167 QPen QBarSet::floatingValuePen() const
169 QPen QBarSet::floatingValuePen() const
168 {
170 {
169 return mFloatingValuePen;
171 return mFloatingValuePen;
170 }
172 }
171
173
172 /*!
174 /*!
173 \internal \a pos
175 \internal \a pos
174 */
176 */
175 void QBarSet::barHoverEnterEvent(QPoint pos)
177 void QBarSet::barHoverEnterEvent(QPoint pos)
176 {
178 {
177 emit showToolTip(pos, mName);
179 emit showToolTip(pos, mName);
178 emit hoverEnter(pos);
180 emit hoverEnter(pos);
179 }
181 }
180
182
181 /*!
183 /*!
182 \internal
184 \internal
183 */
185 */
184 void QBarSet::barHoverLeaveEvent()
186 void QBarSet::barHoverLeaveEvent()
185 {
187 {
186 // Emit signal to user of charts
188 // Emit signal to user of charts
187 emit hoverLeave();
189 emit hoverLeave();
188 }
190 }
189
191
190 #include "moc_qbarset.cpp"
192 #include "moc_qbarset.cpp"
191 QTCOMMERCIALCHART_END_NAMESPACE
193 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,70 +1,71
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet(QString name, QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15
15
16 void setName(QString name);
16 void setName(QString name);
17 QString name();
17 QString name();
18 QBarSet& operator << (const qreal &value); // appends new value to set
18 QBarSet& operator << (const qreal &value); // appends new value to set
19
19
20 // TODO: remove indices eventually. Use as internal?
20 // TODO: remove indices eventually. Use as internal?
21 int count(); // count of values in set
21 int count(); // count of values in set
22 qreal valueAt(int index); // for modifying individual values
22 qreal valueAt(int index); // for modifying individual values
23 void setValue(int index, qreal value); // setter for individual value
23 void setValue(int index, qreal value); // setter for individual value
24 qreal total(); // total values in the set
24 qreal total(); // total values in the set
25
25
26 // TODO:
26 // TODO:
27 //qreal value(QString category);
27 //qreal value(QString category);
28 //void setValue(QString category, qreal value);
28 //void setValue(QString category, qreal value);
29
29
30 void setPen(const QPen pen);
30 void setPen(const QPen pen);
31 QPen pen() const;
31 QPen pen() const;
32
32
33 void setBrush(const QBrush brush);
33 void setBrush(const QBrush brush);
34 QBrush brush() const;
34 QBrush brush() const;
35
35
36 void setFloatingValuePen(const QPen pen);
36 void setFloatingValuePen(const QPen pen);
37 QPen floatingValuePen() const;
37 QPen floatingValuePen() const;
38
38
39 Q_SIGNALS:
39 Q_SIGNALS:
40 void clicked(QString category); // Clicked and hover signals exposed to user
40 void clicked(QString category); // Clicked and hover signals exposed to user
41 void rightClicked(QString category);
41 void rightClicked(QString category);
42 void toggleFloatingValues();
42 void toggleFloatingValues();
43
43
44 // TODO: Expose this to user or not?
44 // TODO: Expose this to user or not?
45 // TODO: TO PIMPL --->
45 // TODO: TO PIMPL --->
46 void changed();
46 void hoverEnter(QPoint pos);
47 void hoverEnter(QPoint pos);
47 void hoverLeave();
48 void hoverLeave();
48 void showToolTip(QPoint pos, QString tip); // Private signal
49 void showToolTip(QPoint pos, QString tip); // Private signal
49 // <--- TO PIMPL
50 // <--- TO PIMPL
50
51
51 public Q_SLOTS:
52 public Q_SLOTS:
52 // These are for internal communication
53 // These are for internal communication
53 // TODO: TO PIMPL --->
54 // TODO: TO PIMPL --->
54 void barHoverEnterEvent(QPoint pos);
55 void barHoverEnterEvent(QPoint pos);
55 void barHoverLeaveEvent();
56 void barHoverLeaveEvent();
56 // <--- TO PIMPL
57 // <--- TO PIMPL
57
58
58 private:
59 private:
59
60
60 QString mName;
61 QString mName;
61 QList<qreal> mValues; // TODO: replace with map (category, value)
62 QList<qreal> mValues; // TODO: replace with map (category, value)
62 QMap<QString,qreal> mMappedValues;
63 QMap<QString,qreal> mMappedValues;
63 QPen mPen;
64 QPen mPen;
64 QBrush mBrush;
65 QBrush mBrush;
65 QPen mFloatingValuePen;
66 QPen mFloatingValuePen;
66 };
67 };
67
68
68 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
69
70
70 #endif // QBARSET_H
71 #endif // QBARSET_H
@@ -1,121 +1,144
1 #include "qchartglobal.h"
1 #include "qchartglobal.h"
2 #include "legendmarker_p.h"
2 #include "legendmarker_p.h"
3 #include <qpieslice.h>
4 #include <qbarset.h>
3 #include <QPainter>
5 #include <QPainter>
4 #include <QGraphicsSceneEvent>
6 #include <QGraphicsSceneEvent>
5
7
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
9
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
10 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
9 : QGraphicsObject(parent)
11 : QGraphicsObject(parent)
10 ,mBoundingRect(0,0,1,1)
12 ,mBoundingRect(0,0,1,1)
11 ,mMarkerBoundingRect(0,0,1,1)
13 ,mMarkerBoundingRect(0,0,1,1)
12 ,mSeries(series)
14 ,mSeries(series)
13 ,mBarset(0)
15 ,mBarset(0)
14 ,mPieslice(0)
16 ,mPieslice(0)
15 ,mType(LegendMarkerTypeSeries)
17 ,mType(LegendMarkerTypeSeries)
16 ,mTextItem(new QGraphicsSimpleTextItem(this))
18 ,mTextItem(new QGraphicsSimpleTextItem(this))
17 {
19 {
18 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
20 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
19 }
21 }
20
22
21 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
23 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
22 : QGraphicsObject(parent)
24 : QGraphicsObject(parent)
23 ,mBoundingRect(0,0,1,1)
25 ,mBoundingRect(0,0,1,1)
24 ,mMarkerBoundingRect(0,0,1,1)
26 ,mMarkerBoundingRect(0,0,1,1)
25 ,mSeries(series)
27 ,mSeries(series)
26 ,mBarset(barset)
28 ,mBarset(barset)
27 ,mPieslice(0)
29 ,mPieslice(0)
28 ,mType(LegendMarkerTypeBarset)
30 ,mType(LegendMarkerTypeBarset)
29 ,mTextItem(new QGraphicsSimpleTextItem(this))
31 ,mTextItem(new QGraphicsSimpleTextItem(this))
30 {
32 {
31 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
33 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
32 }
34 }
33
35
34 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
36 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
35 : QGraphicsObject(parent)
37 : QGraphicsObject(parent)
36 ,mBoundingRect(0,0,1,1)
38 ,mBoundingRect(0,0,1,1)
37 ,mMarkerBoundingRect(0,0,1,1)
39 ,mMarkerBoundingRect(0,0,1,1)
38 ,mSeries(series)
40 ,mSeries(series)
39 ,mBarset(0)
41 ,mBarset(0)
40 ,mPieslice(pieslice)
42 ,mPieslice(pieslice)
41 ,mType(LegendMarkerTypePieslice)
43 ,mType(LegendMarkerTypePieslice)
42 ,mTextItem(new QGraphicsSimpleTextItem(this))
44 ,mTextItem(new QGraphicsSimpleTextItem(this))
43 {
45 {
44 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
46 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
45 }
47 }
46
48
47 void LegendMarker::setBoundingRect(const QRectF rect)
49 void LegendMarker::setBoundingRect(const QRectF rect)
48 {
50 {
49 mBoundingRect = rect;
51 mBoundingRect = rect;
50 // Calculate Marker pos
52 // Calculate Marker pos
51
53
52 // TODO: remove hard coding. 5 is marigin around marker
54 // TODO: remove hard coding. 5 is marigin around marker
53 QSizeF markerSize(10,10);
55 QSizeF markerSize(10,10);
54 qreal x = mBoundingRect.x() + 5;
56 qreal x = mBoundingRect.x() + 5;
55 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
57 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
56 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
58 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
57
59
58 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
60 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
59 }
61 }
60
62
61 void LegendMarker::setBrush(const QBrush brush)
63 void LegendMarker::setBrush(const QBrush brush)
62 {
64 {
63 mBrush = brush;
65 mBrush = brush;
64 }
66 }
65
67
66 QBrush LegendMarker::brush() const
68 QBrush LegendMarker::brush() const
67 {
69 {
68 return mBrush;
70 return mBrush;
69 }
71 }
70
72
71 void LegendMarker::setName(const QString name)
73 void LegendMarker::setName(const QString name)
72 {
74 {
73 mTextItem.setText(name);
75 mTextItem.setText(name);
74 }
76 }
75
77
76 QString LegendMarker::name() const
78 QString LegendMarker::name() const
77 {
79 {
78 return mTextItem.text();
80 return mTextItem.text();
79 }
81 }
80
82
81 QSeries* LegendMarker::series() const
83 QSeries* LegendMarker::series() const
82 {
84 {
83 return mSeries;
85 return mSeries;
84 }
86 }
85
87
86 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
88 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
87 {
89 {
88 painter->setBrush(mBrush);
90 painter->setBrush(mBrush);
89 painter->drawRect(mMarkerBoundingRect);
91 painter->drawRect(mMarkerBoundingRect);
90 }
92 }
91
93
92 QRectF LegendMarker::boundingRect() const
94 QRectF LegendMarker::boundingRect() const
93 {
95 {
94 return mBoundingRect;
96 return mBoundingRect;
95 }
97 }
96
98
97 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
99 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
98 {
100 {
99 switch (mType)
101 switch (mType)
100 {
102 {
101 case LegendMarkerTypeSeries: {
103 case LegendMarkerTypeSeries: {
102 emit clicked(mSeries,event->button());
104 emit clicked(mSeries,event->button());
103 break;
105 break;
104 }
106 }
105 case LegendMarkerTypeBarset: {
107 case LegendMarkerTypeBarset: {
106 emit clicked(mBarset,event->button());
108 emit clicked(mBarset,event->button());
107 break;
109 break;
108 }
110 }
109 case LegendMarkerTypePieslice: {
111 case LegendMarkerTypePieslice: {
110 emit clicked(mPieslice,event->button());
112 emit clicked(mPieslice,event->button());
111 break;
113 break;
112 }
114 }
113 default: {
115 default: {
114 break;
116 break;
115 }
117 }
116 }
118 }
117 }
119 }
118
120
121 void LegendMarker::changed()
122 {
123 switch (mType)
124 {
125 case LegendMarkerTypeSeries: {
126 // TODO:
127 break;
128 }
129 case LegendMarkerTypeBarset: {
130 setBrush(mBarset->brush());
131 setName(mBarset->name());
132 break;
133 }
134 case LegendMarkerTypePieslice: {
135 setBrush(mPieslice->sliceBrush());
136 setName(mPieslice->label());
137 break;
138 }
139 }
140 }
141
119 #include "moc_legendmarker_p.cpp"
142 #include "moc_legendmarker_p.cpp"
120
143
121 QTCOMMERCIALCHART_END_NAMESPACE
144 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,69
1 #ifndef LEGENDMARKER_P_H
1 #ifndef LEGENDMARKER_P_H
2 #define LEGENDMARKER_P_H
2 #define LEGENDMARKER_P_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
5 #include <QGraphicsObject>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QSeries;
10 class QSeries;
11 class QBarSet;
11 class QBarSet;
12 class QPieSlice;
12 class QPieSlice;
13
13
14 // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse...
14 // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse...
15 class LegendMarker : public QGraphicsObject
15 class LegendMarker : public QGraphicsObject
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18
18
19 enum LegendMarkerType {
19 enum LegendMarkerType {
20 LegendMarkerTypeSeries,
20 LegendMarkerTypeSeries,
21 LegendMarkerTypeBarset,
21 LegendMarkerTypeBarset,
22 LegendMarkerTypePieslice
22 LegendMarkerTypePieslice
23 };
23 };
24
24
25 public:
25 public:
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
28 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
28 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
29 void setBoundingRect(const QRectF rect);
29 void setBoundingRect(const QRectF rect);
30
30
31 void setBrush(const QBrush brush);
31 void setBrush(const QBrush brush);
32 QBrush brush() const;
32 QBrush brush() const;
33
33
34 void setName(const QString name);
34 void setName(const QString name);
35 QString name() const;
35 QString name() const;
36
36
37 QSeries* series() const;
37 QSeries* series() const;
38
38
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
40
40
41 QRectF boundingRect() const;
41 QRectF boundingRect() const;
42
42
43 public:
43 public:
44 // From QGraphicsObject
44 // From QGraphicsObject
45 void mousePressEvent(QGraphicsSceneMouseEvent *event);
45 void mousePressEvent(QGraphicsSceneMouseEvent *event);
46
46
47 Q_SIGNALS:
47 Q_SIGNALS:
48 void clicked(QSeries* series, Qt::MouseButton button);
48 void clicked(QSeries* series, Qt::MouseButton button);
49 void clicked(QBarSet* barset, Qt::MouseButton button);
49 void clicked(QBarSet* barset, Qt::MouseButton button);
50 void clicked(QPieSlice* pieslice, Qt::MouseButton button);
50 void clicked(QPieSlice* pieslice, Qt::MouseButton button);
51
51
52 public Q_SLOTS:
53 void changed();
54
52 private:
55 private:
53 QRectF mBoundingRect;
56 QRectF mBoundingRect;
54 QRectF mMarkerBoundingRect;
57 QRectF mMarkerBoundingRect;
55 QBrush mBrush;
58 QBrush mBrush;
56 QSeries* mSeries;
59 QSeries* mSeries;
57 QBarSet* mBarset;
60 QBarSet* mBarset;
58 QPieSlice* mPieslice;
61 QPieSlice* mPieslice;
59
62
60 LegendMarkerType mType;
63 LegendMarkerType mType;
61 QGraphicsSimpleTextItem mTextItem;
64 QGraphicsSimpleTextItem mTextItem;
62
65
63 };
66 };
64
67
65 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
66 #endif // LEGENDMARKER_P_H
69 #endif // LEGENDMARKER_P_H
@@ -1,224 +1,216
1 #include "qchartglobal.h"
1 #include "qchartglobal.h"
2 #include "qlegend.h"
2 #include "qlegend.h"
3 #include "qseries.h"
3 #include "qseries.h"
4 #include "legendmarker_p.h"
4 #include "legendmarker_p.h"
5 #include "qxyseries.h"
5 #include "qxyseries.h"
6 #include "qlineseries.h"
6 #include "qlineseries.h"
7 #include "qareaseries.h"
7 #include "qareaseries.h"
8 #include "qscatterseries.h"
8 #include "qscatterseries.h"
9 #include "qsplineseries.h"
9 #include "qsplineseries.h"
10 #include "qbarseries.h"
10 #include "qbarseries.h"
11 #include "qstackedbarseries.h"
11 #include "qstackedbarseries.h"
12 #include "qpercentbarseries.h"
12 #include "qpercentbarseries.h"
13 #include "qbarset.h"
13 #include "qbarset.h"
14 #include "qpieseries.h"
14 #include "qpieseries.h"
15 #include "qpieslice.h"
15 #include "qpieslice.h"
16 #include <QPainter>
16 #include <QPainter>
17 #include <QPen>
17 #include <QPen>
18
18
19 #include <QGraphicsSceneEvent>
19 #include <QGraphicsSceneEvent>
20
20
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22
22
23 QLegend::QLegend(QGraphicsItem *parent)
23 QLegend::QLegend(QGraphicsItem *parent)
24 : QGraphicsObject(parent)
24 : QGraphicsObject(parent)
25 ,mBoundingRect(0,0,1,1)
25 ,mBoundingRect(0,0,1,1)
26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
27 ,mMinimumSize(50,20) // TODO: magic numbers
27 ,mMinimumSize(50,20) // TODO: magic numbers
28 {
28 {
29 }
29 }
30
30
31 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
31 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 {
32 {
33 painter->setBrush(mBackgroundBrush);
33 painter->setBrush(mBackgroundBrush);
34 painter->drawRect(mBoundingRect);
34 painter->drawRect(mBoundingRect);
35 }
35 }
36
36
37 QRectF QLegend::boundingRect() const
37 QRectF QLegend::boundingRect() const
38 {
38 {
39 return mBoundingRect;
39 return mBoundingRect;
40 }
40 }
41
41
42 void QLegend::setBackgroundBrush(const QBrush& brush)
42 void QLegend::setBackgroundBrush(const QBrush& brush)
43 {
43 {
44 mBackgroundBrush = brush;
44 mBackgroundBrush = brush;
45 }
45 }
46
46
47 QBrush QLegend::backgroundBrush() const
47 QBrush QLegend::backgroundBrush() const
48 {
48 {
49 return mBackgroundBrush;
49 return mBackgroundBrush;
50 }
50 }
51
51
52 QSizeF QLegend::minimumSize() const
52 QSizeF QLegend::minimumSize() const
53 {
53 {
54 return mMinimumSize;
54 return mMinimumSize;
55 }
55 }
56
56
57 void QLegend::setMinimumSize(const QSizeF size)
57 void QLegend::setMinimumSize(const QSizeF size)
58 {
58 {
59 mMinimumSize = size;
59 mMinimumSize = size;
60 }
60 }
61
61
62 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
62 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
63 {
63 {
64 mSeriesList.append(series);
64 mSeriesList.append(series);
65 createMarkers(series);
65 createMarkers(series);
66 layoutChanged();
66 layoutChanged();
67 }
67 }
68
68
69 void QLegend::handleSeriesRemoved(QSeries* series)
69 void QLegend::handleSeriesRemoved(QSeries* series)
70 {
70 {
71 if (series->type() == QSeries::SeriesTypeArea)
71 if (series->type() == QSeries::SeriesTypeArea)
72 {
72 {
73 // This is special case. Area series has upper and lower series, which each have markers
73 // This is special case. Area series has upper and lower series, which each have markers
74 QAreaSeries* s = static_cast<QAreaSeries*> (series);
74 QAreaSeries* s = static_cast<QAreaSeries*> (series);
75 deleteMarkers(s->upperSeries());
75 deleteMarkers(s->upperSeries());
76 deleteMarkers(s->lowerSeries());
76 deleteMarkers(s->lowerSeries());
77 } else {
77 } else {
78 deleteMarkers(series);
78 deleteMarkers(series);
79 }
79 }
80
80
81 mSeriesList.removeOne(series);
81 mSeriesList.removeOne(series);
82 layoutChanged();
82 layoutChanged();
83 }
83 }
84
84
85 void QLegend::handleGeometryChanged(const QRectF& size)
85 void QLegend::handleGeometryChanged(const QRectF& size)
86 {
86 {
87 mBoundingRect = size;
87 mBoundingRect = size;
88 layoutChanged();
88 layoutChanged();
89 }
89 }
90
90
91 void QLegend::handleThemeChanged()
92 {
93 foreach(QSeries *s, mSeriesList) {
94 deleteMarkers(s);
95 }
96 foreach(QSeries *s, mSeriesList) {
97 createMarkers(s);
98 }
99 }
100
101 void QLegend::createMarkers(QSeries *series)
91 void QLegend::createMarkers(QSeries *series)
102 {
92 {
103 switch (series->type())
93 switch (series->type())
104 {
94 {
105 case QSeries::SeriesTypeLine: {
95 case QSeries::SeriesTypeLine: {
106 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
96 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
107 appendMarkers(lineSeries);
97 appendMarkers(lineSeries);
108 break;
98 break;
109 }
99 }
110 case QSeries::SeriesTypeArea: {
100 case QSeries::SeriesTypeArea: {
111 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
101 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
112 appendMarkers(areaSeries->upperSeries());
102 appendMarkers(areaSeries->upperSeries());
113 if(areaSeries->lowerSeries())
103 if(areaSeries->lowerSeries())
114 appendMarkers(areaSeries->lowerSeries());
104 appendMarkers(areaSeries->lowerSeries());
115 break;
105 break;
116 }
106 }
117
107
118 case QSeries::SeriesTypeBar: {
108 case QSeries::SeriesTypeBar: {
119 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
109 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
120 appendMarkers(barSeries);
110 appendMarkers(barSeries);
121 break;
111 break;
122 }
112 }
123
113
124 case QSeries::SeriesTypeStackedBar: {
114 case QSeries::SeriesTypeStackedBar: {
125 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
115 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
126 appendMarkers(stackedBarSeries);
116 appendMarkers(stackedBarSeries);
127 break;
117 break;
128 }
118 }
129
119
130 case QSeries::SeriesTypePercentBar: {
120 case QSeries::SeriesTypePercentBar: {
131 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
121 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
132 appendMarkers(percentBarSeries);
122 appendMarkers(percentBarSeries);
133 break;
123 break;
134 }
124 }
135
125
136 case QSeries::SeriesTypeScatter: {
126 case QSeries::SeriesTypeScatter: {
137 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
127 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
138 appendMarkers(scatterSeries);
128 appendMarkers(scatterSeries);
139 break;
129 break;
140 }
130 }
141
131
142 case QSeries::SeriesTypePie: {
132 case QSeries::SeriesTypePie: {
143 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
133 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
144 appendMarkers(pieSeries);
134 appendMarkers(pieSeries);
145 break;
135 break;
146 }
136 }
147
137
148 case QSeries::SeriesTypeSpline: {
138 case QSeries::SeriesTypeSpline: {
149 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
139 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
150 appendMarkers(splineSeries);
140 appendMarkers(splineSeries);
151 break;
141 break;
152 }
142 }
153 default: {
143 default: {
154 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
144 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
155 break;
145 break;
156 }
146 }
157 }
147 }
158 }
148 }
159
149
160 void QLegend::appendMarkers(QXYSeries* series)
150 void QLegend::appendMarkers(QXYSeries* series)
161 {
151 {
162 LegendMarker* marker = new LegendMarker(series,this);
152 LegendMarker* marker = new LegendMarker(series,this);
163 marker->setName(series->name());
153 marker->setName(series->name());
164 marker->setBrush(series->brush());
154 marker->setBrush(series->brush());
165 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
155 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
166 mMarkers.append(marker);
156 mMarkers.append(marker);
167 childItems().append(marker);
157 childItems().append(marker);
168 }
158 }
169
159
170 void QLegend::appendMarkers(QBarSeries *series)
160 void QLegend::appendMarkers(QBarSeries *series)
171 {
161 {
172 foreach(QBarSet* s, series->barSets()) {
162 foreach(QBarSet* s, series->barSets()) {
173 LegendMarker* marker = new LegendMarker(series,s,this);
163 LegendMarker* marker = new LegendMarker(series,s,this);
174 marker->setName(s->name());
164 marker->setName(s->name());
175 marker->setBrush(s->brush());
165 marker->setBrush(s->brush());
176 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
166 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
167 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
177 mMarkers.append(marker);
168 mMarkers.append(marker);
178 childItems().append(marker);
169 childItems().append(marker);
179 }
170 }
180 }
171 }
181
172
182 void QLegend::appendMarkers(QPieSeries *series)
173 void QLegend::appendMarkers(QPieSeries *series)
183 {
174 {
184 foreach(QPieSlice* s, series->slices()) {
175 foreach(QPieSlice* s, series->slices()) {
185 LegendMarker* marker = new LegendMarker(series,s,this);
176 LegendMarker* marker = new LegendMarker(series,s,this);
186 marker->setName(s->label());
177 marker->setName(s->label());
187 marker->setBrush(s->sliceBrush());
178 marker->setBrush(s->sliceBrush());
188 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
179 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
180 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
189 mMarkers.append(marker);
181 mMarkers.append(marker);
190 childItems().append(marker);
182 childItems().append(marker);
191 }
183 }
192 }
184 }
193
185
194 void QLegend::deleteMarkers(QSeries *series)
186 void QLegend::deleteMarkers(QSeries *series)
195 {
187 {
196 // Search all markers that belong to given series and delete them.
188 // Search all markers that belong to given series and delete them.
197 foreach (LegendMarker *m, mMarkers) {
189 foreach (LegendMarker *m, mMarkers) {
198 if (m->series() == series) {
190 if (m->series() == series) {
199 mMarkers.removeOne(m);
191 mMarkers.removeOne(m);
200 delete m;
192 delete m;
201 }
193 }
202 }
194 }
203 }
195 }
204
196
205 void QLegend::layoutChanged()
197 void QLegend::layoutChanged()
206 {
198 {
207 // Calculate layout for markers and text
199 // Calculate layout for markers and text
208 if (mMarkers.count() <= 0) {
200 if (mMarkers.count() <= 0) {
209 // Nothing to do
201 // Nothing to do
210 return;
202 return;
211 }
203 }
212
204
213 qreal steps = mMarkers.count();
205 qreal steps = mMarkers.count();
214 qreal xStep = mBoundingRect.width() / steps;
206 qreal xStep = mBoundingRect.width() / steps;
215 qreal x=mBoundingRect.x();
207 qreal x=mBoundingRect.x();
216 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
208 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
217 foreach (LegendMarker* m, mMarkers) {
209 foreach (LegendMarker* m, mMarkers) {
218 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
210 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
219 x += xStep;
211 x += xStep;
220 }
212 }
221 }
213 }
222
214
223 #include "moc_qlegend.cpp"
215 #include "moc_qlegend.cpp"
224 QTCOMMERCIALCHART_END_NAMESPACE
216 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,66
1 #ifndef QLEGEND_H
1 #ifndef QLEGEND_H
2 #define QLEGEND_H
2 #define QLEGEND_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QGraphicsObject>
6 #include <QGraphicsObject>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class Domain;
10 class Domain;
11 class LegendMarker;
11 class LegendMarker;
12 class QPieSlice;
12 class QPieSlice;
13 class QXYSeries;
13 class QXYSeries;
14 class QBarSet;
14 class QBarSet;
15 class QBarSeries;
15 class QBarSeries;
16 class QPieSeries;
16 class QPieSeries;
17
17
18 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
18 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
19 {
19 {
20 Q_OBJECT
20 Q_OBJECT
21 public:
21 public:
22
22
23 explicit QLegend(QGraphicsItem *parent = 0);
23 explicit QLegend(QGraphicsItem *parent = 0);
24
24
25 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
25 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
26 QRectF boundingRect() const;
26 QRectF boundingRect() const;
27
27
28 void setBackgroundBrush(const QBrush& brush);
28 void setBackgroundBrush(const QBrush& brush);
29 QBrush backgroundBrush() const;
29 QBrush backgroundBrush() const;
30
30
31 QSizeF minimumSize() const;
31 QSizeF minimumSize() const;
32 void setMinimumSize(const QSizeF size);
32 void setMinimumSize(const QSizeF size);
33
33
34 signals:
34 signals:
35 // for interactions.
35 // for interactions.
36 void clicked(QSeries* series, Qt::MouseButton button);
36 void clicked(QSeries* series, Qt::MouseButton button);
37 void clicked(QBarSet* barset, Qt::MouseButton button);
37 void clicked(QBarSet* barset, Qt::MouseButton button);
38 void clicked(QPieSlice* slice, Qt::MouseButton button);
38 void clicked(QPieSlice* slice, Qt::MouseButton button);
39
39
40 public slots:
40 public slots:
41 void handleSeriesAdded(QSeries* series,Domain* domain);
41 void handleSeriesAdded(QSeries* series,Domain* domain);
42 void handleSeriesRemoved(QSeries* series);
42 void handleSeriesRemoved(QSeries* series);
43 void handleGeometryChanged(const QRectF& size);
43 void handleGeometryChanged(const QRectF& size);
44
44
45 // PIMPL --->
46 // Internal slot. Legend needs to know when theme has changed (or color of some series, if user changes it)
47 void handleThemeChanged();
48 // <--- PIMPL
49
50 private:
45 private:
51 // PIMPL --->
46 // PIMPL --->
52 void createMarkers(QSeries* series);
47 void createMarkers(QSeries* series);
53 void appendMarkers(QXYSeries* series);
48 void appendMarkers(QXYSeries* series);
54 void appendMarkers(QBarSeries* series);
49 void appendMarkers(QBarSeries* series);
55 void appendMarkers(QPieSeries* series);
50 void appendMarkers(QPieSeries* series);
56 void deleteMarkers(QSeries* series);
51 void deleteMarkers(QSeries* series);
57 void layoutChanged();
52 void layoutChanged();
58 // <--- PIMPL
53 // <--- PIMPL
59
54
60
55
61 QRectF mBoundingRect;
56 QRectF mBoundingRect;
62 QList<QSeries*> mSeriesList;
57 QList<QSeries*> mSeriesList;
63 QList<LegendMarker*> mMarkers;
58 QList<LegendMarker*> mMarkers;
64
59
65 QBrush mBackgroundBrush;
60 QBrush mBackgroundBrush;
66 QSizeF mMinimumSize;
61 QSizeF mMinimumSize;
67 };
62 };
68
63
69 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
70
65
71 #endif // QLEGEND_H
66 #endif // QLEGEND_H
General Comments 0
You need to be logged in to leave comments. Login now