##// END OF EJS Templates
legend signal fix for barchart
sauimone -
r953:4f43e4b42147
parent child
Show More
@@ -1,80 +1,80
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QBARSET_H
22 22 #define QBARSET_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QBrush>
27 27 #include <QFont>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QBarSetPrivate;
31 31
32 32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 33 {
34 34 Q_OBJECT
35 35
36 36 public:
37 37 QBarSet(QString name, QObject *parent = 0);
38 38 virtual ~QBarSet();
39 39
40 40 void setName(QString name);
41 41 QString name() const;
42 42 QBarSet& operator << (const qreal &value); // appends new value to set
43 43 void insertValue(int i, qreal value);
44 44 void removeValue(int i);
45 45 int count() const; // count of values in set
46 46 qreal valueAt(int index) const; // for modifying individual values
47 47 void setValue(int index, qreal value); // setter for individual value
48 48 qreal sum() const; // sum of all values in the set
49 49
50 50 void setPen(const QPen &pen);
51 51 QPen pen() const;
52 52
53 53 void setBrush(const QBrush &brush);
54 54 QBrush brush() const;
55 55
56 56 void setLabelPen(const QPen &pen);
57 57 QPen labelPen() const;
58 58
59 59 void setLabelBrush(const QBrush &brush);
60 60 QBrush labelBrush() const;
61 61
62 62 void setLabelFont(const QFont &font);
63 63 QFont labelFont() const;
64 64
65 65 void setLabelsVisible(bool visible = true);
66 66 bool labelsVisible() const;
67 67
68 68 Q_SIGNALS:
69 69 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
70 70
71 71 private:
72 72 QScopedPointer<QBarSetPrivate> d_ptr;
73 73 Q_DISABLE_COPY(QBarSet)
74 74 friend class QBarSeries;
75
75 friend class BarLegendMarker;
76 76 };
77 77
78 78 QTCOMMERCIALCHART_END_NAMESPACE
79 79
80 80 #endif // QBARSET_H
@@ -1,194 +1,195
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "legendmarker_p.h"
22 22 #include "qxyseries.h"
23 23 #include "qxyseries_p.h"
24 24 #include "qlegend.h"
25 25 #include "qbarseries.h"
26 26 #include "qpieseries.h"
27 27 #include "qpieslice.h"
28 28 #include "qbarset.h"
29 #include "qbarset_p.h"
29 30 #include "qareaseries.h"
30 31 #include "qareaseries_p.h"
31 32 #include <QPainter>
32 33 #include <QGraphicsSceneEvent>
33 34 #include <QGraphicsSimpleTextItem>
34 35
35 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 37
37 38 LegendMarker::LegendMarker(QSeries* series,QLegend *legend) : QGraphicsObject(legend),
38 39 m_series(series),
39 40 m_markerRect(0,0,10.0,10.0),
40 41 m_boundingRect(0,0,0,0),
41 42 m_legend(legend),
42 43 m_textItem(new QGraphicsSimpleTextItem(this)),
43 44 m_rectItem(new QGraphicsRectItem(this))
44 45 {
45 46 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
46 47 m_rectItem->setRect(m_markerRect);
47 48 updateLayout();
48 49 }
49 50
50 51 void LegendMarker::setPen(const QPen &pen)
51 52 {
52 53 m_textItem->setPen(pen);
53 54 updateLayout();
54 55 }
55 56
56 57 QPen LegendMarker::pen() const
57 58 {
58 59 return m_textItem->pen();
59 60 }
60 61
61 62 void LegendMarker::setBrush(const QBrush &brush)
62 63 {
63 64 m_rectItem->setBrush(brush);
64 65 }
65 66
66 67 QBrush LegendMarker::brush() const
67 68 {
68 69 return m_rectItem->brush();
69 70 }
70 71
71 72 void LegendMarker::setLabel(const QString name)
72 73 {
73 74 m_textItem->setText(name);
74 75 updateLayout();
75 76 }
76 77
77 78 void LegendMarker::setSize(const QSize& size)
78 79 {
79 80 m_markerRect = QRectF(0,0,size.width(),size.height());
80 81 }
81 82
82 83 QString LegendMarker::label() const
83 84 {
84 85 return m_textItem->text();
85 86 }
86 87
87 88 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
88 89 {
89 90 Q_UNUSED(option)
90 91 Q_UNUSED(widget)
91 92 Q_UNUSED(painter)
92 93 }
93 94
94 95 QRectF LegendMarker::boundingRect() const
95 96 {
96 97 return m_boundingRect;
97 98 }
98 99
99 100 void LegendMarker::updateLayout()
100 101 {
101 102
102 103 static const qreal margin = 2;
103 104 static const qreal space = 4;
104 105
105 106 const QRectF& textRect = m_textItem->boundingRect();
106 107 prepareGeometryChange();
107 108 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
108 109 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
109 110 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
110 111
111 112 }
112 113
113 114 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
114 115 {
115 116 QGraphicsObject::mousePressEvent(event);
116 117 emit selected();
117 118 }
118 119
119 120 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120 121
121 122 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
122 123 m_series(series)
123 124 {
124 125 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
125 126 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
126 127 updated();
127 128 }
128 129
129 130 void AreaLegendMarker::updated()
130 131 {
131 132 setBrush(m_series->brush());
132 133 setLabel(m_series->name());
133 134 }
134 135
135 136 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136 137
137 BarLegendMarker::BarLegendMarker(QBarSeries *series,QBarSet *barset, QLegend *legend) : LegendMarker(series,legend),
138 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
138 139 m_barset(barset)
139 140 {
140 QObject::connect(this, SIGNAL(selected()),series, SIGNAL(selected()));
141 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(updated()));
141 QObject::connect(this, SIGNAL(selected()),barseries, SIGNAL(selected()));
142 QObject::connect(barset->d_ptr.data(), SIGNAL(valueChanged()), this, SLOT(updated()));
142 143 updated();
143 144 }
144 145
145 146 void BarLegendMarker::updated()
146 147 {
147 148 setBrush(m_barset->brush());
148 149 setLabel(m_barset->name());
149 150 }
150 151
151 152 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
152 153
153 154 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
154 155 m_pieslice(pieslice)
155 156 {
156 157 QObject::connect(this, SIGNAL(selected()),pieslice, SIGNAL(selected()));
157 158 QObject::connect(pieslice, SIGNAL(changed()), this, SLOT(updated()));
158 159 QObject::connect(pieslice, SIGNAL(destroyed()), this, SLOT(deleteLater())); //TODO:checkthis
159 160 updated();
160 161 }
161 162
162 163 void PieLegendMarker::updated()
163 164 {
164 165 setBrush(m_pieslice->brush());
165 166 setLabel(m_pieslice->label());
166 167 }
167 168
168 169 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169 170
170 171 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
171 172 m_series(series)
172 173 {
173 174 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
174 175 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
175 176 updated();
176 177 }
177 178
178 179 void XYLegendMarker::updated()
179 180 {
180 181 setLabel(m_series->name());
181 182
182 183 if(m_series->type()== QSeries::SeriesTypeScatter)
183 184 {
184 185 setBrush(m_series->brush());
185 186
186 187 }
187 188 else {
188 189 setBrush(QBrush(m_series->pen().color()));
189 190 }
190 191 }
191 192
192 193 #include "moc_legendmarker_p.cpp"
193 194
194 195 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,128 +1,128
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef LEGENDMARKER_P_H
22 22 #define LEGENDMARKER_P_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include <QGraphicsObject>
26 26 #include <QBrush>
27 27 #include <QPen>
28 28 #include <QGraphicsSimpleTextItem>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class QSeries;
33 33 class QAreaSeries;
34 34 class QXYSeries;
35 35 class QBarSet;
36 36 class QBarSeries;
37 37 class QPieSlice;
38 38 class QLegend;
39 39 class QPieSeries;
40 40
41 41 class LegendMarker : public QGraphicsObject
42 42 {
43 43 Q_OBJECT
44 44
45 45 public:
46 46 explicit LegendMarker(QSeries* m_series,QLegend *parent);
47 47
48 48 void setPen(const QPen &pen);
49 49 QPen pen() const;
50 50 void setBrush(const QBrush &brush);
51 51 QBrush brush() const;
52 52
53 53 void setSize(const QSize& size);
54 54
55 55 void setLabel(const QString label);
56 56 QString label() const;
57 57
58 58 QSeries* series() const { return m_series;}
59 59
60 60 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
61 61
62 62 QRectF boundingRect() const;
63 63
64 64 void updateLayout();
65 65
66 66 protected:
67 67 // From QGraphicsObject
68 68 void mousePressEvent(QGraphicsSceneMouseEvent *event);
69 69
70 70 Q_SIGNALS:
71 71 void selected();
72 72
73 73 public Q_SLOTS:
74 74 virtual void updated() = 0;
75 75
76 76 protected:
77 77 QSeries* m_series;
78 78 QRectF m_markerRect;
79 79 QRectF m_boundingRect;
80 80 QLegend* m_legend;
81 81 QGraphicsSimpleTextItem *m_textItem;
82 82 QGraphicsRectItem *m_rectItem;
83 83
84 84 };
85 85
86 86 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87 87 class XYLegendMarker : public LegendMarker
88 88 {
89 89 public:
90 90 XYLegendMarker(QXYSeries *series, QLegend *legend);
91 91 protected:
92 92 void updated();
93 93 private:
94 94 QXYSeries *m_series;
95 95 };
96 96 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97 97 class AreaLegendMarker : public LegendMarker
98 98 {
99 99 public:
100 100 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
101 101 protected:
102 102 void updated();
103 103 private:
104 104 QAreaSeries *m_series;
105 105 };
106 106 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107 107 class BarLegendMarker : public LegendMarker
108 108 {
109 109 public:
110 BarLegendMarker(QBarSeries *barseires, QBarSet *barset,QLegend *legend);
110 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
111 111 protected:
112 112 void updated();
113 113 private:
114 114 QBarSet *m_barset;
115 115 };
116 116 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
117 117 class PieLegendMarker : public LegendMarker
118 118 {
119 119 public:
120 120 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
121 121 protected:
122 122 void updated();
123 123 private:
124 124 QPieSlice *m_pieslice;
125 125 };
126 126
127 127 QTCOMMERCIALCHART_END_NAMESPACE
128 128 #endif // LEGENDMARKER_P_H
General Comments 0
You need to be logged in to leave comments. Login now