##// END OF EJS Templates
barset: selected is now private signal
sauimone -
r1017:d84649cce525
parent child
Show More
@@ -1,304 +1,303
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 "qbarset.h"
22 22 #include "qbarset_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QBarSet
28 28 \brief part of QtCommercial chart API.
29 29
30 30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 31 First value of set is assumed to belong to first category, second to second category and so on.
32 32 If set has fewer values than there are categories, then the missing values are assumed to be
33 33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34 34
35 35 \mainclass
36 36
37 37 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
38 38 */
39 39
40 40 /*!
41 41 \fn void QBarSet::clicked(QString category)
42 42 \brief signals that set has been clicked
43 43 Parameter \a category describes on which category was clicked
44 44 */
45 45
46 46 /*!
47 47 \fn void QBarSet::hovered(bool status)
48 48 \brief signals that mouse has hovered over the set. If \a status is true, then mouse was entered. If \a status is false, then mouse was left.
49 49
50 50 The signal is emitted if mouse is hovered on top of set
51 51 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
52 52 */
53 53
54 54 /*!
55 55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 56 */
57 57 QBarSet::QBarSet(const QString name, QObject *parent)
58 58 : QObject(parent)
59 59 ,d_ptr(new QBarSetPrivate(name,this))
60 60 {
61 61 }
62 62
63 63 /*!
64 64 Destroys the barset
65 65 */
66 66 QBarSet::~QBarSet()
67 67 {
68 68 // NOTE: d_ptr destroyed by QObject
69 69 }
70 70
71 71 /*!
72 72 Sets new \a name for set.
73 73 */
74 74 void QBarSet::setName(const QString name)
75 75 {
76 76 d_ptr->m_name = name;
77 77 }
78 78
79 79 /*!
80 80 Returns name of the set.
81 81 */
82 82 QString QBarSet::name() const
83 83 {
84 84 return d_ptr->m_name;
85 85 }
86 86
87 87 /*!
88 88 Appends new value \a value to the end of set.
89 89 */
90 90 void QBarSet::append(const qreal value)
91 91 {
92 92 d_ptr->m_values.append(value);
93 93 emit d_ptr->restructuredBars();
94 94 }
95 95
96 96 /*!
97 97 Appends new value \a value to the end of set.
98 98 */
99 99 QBarSet& QBarSet::operator << (const qreal &value)
100 100 {
101 101 append(value);
102 102 return *this;
103 103 }
104 104
105 105 /*!
106 106 Inserts new \a value on the \a index position.
107 107 The value that is currently at this postion is moved to postion index + 1
108 108 \sa remove()
109 109 */
110 110 void QBarSet::insert(const int index, const qreal value)
111 111 {
112 112 d_ptr->m_values.insert(index, value);
113 113 emit d_ptr->updatedBars();
114 114 }
115 115
116 116 /*!
117 117 Removes the value specified by \a index
118 118 \sa insert()
119 119 */
120 120 void QBarSet::remove(const int index)
121 121 {
122 122 d_ptr->m_values.removeAt(index);
123 123 emit d_ptr->updatedBars();
124 124 }
125 125
126 126 /*!
127 127 Sets a new value \a value to set, indexed by \a index
128 128 */
129 129 void QBarSet::replace(const int index, const qreal value)
130 130 {
131 131 d_ptr->m_values.replace(index,value);
132 132 emit d_ptr->updatedBars();
133 133 }
134 134
135 135 /*!
136 136 Returns value of set indexed by \a index
137 137 */
138 138 qreal QBarSet::at(const int index) const
139 139 {
140 140 return d_ptr->m_values.at(index);
141 141 }
142 142
143 143 /*!
144 144 Returns value of set indexed by \a index
145 145 */
146 146 qreal QBarSet::operator [] (int index) const
147 147 {
148 148 return d_ptr->m_values.at(index);
149 149 }
150 150
151 151 /*!
152 152 Returns count of values in set.
153 153 */
154 154 int QBarSet::count() const
155 155 {
156 156 return d_ptr->m_values.count();
157 157 }
158 158
159 159 /*!
160 160 Returns sum of all values in barset.
161 161 */
162 162 qreal QBarSet::sum() const
163 163 {
164 164 qreal total(0);
165 165 for (int i=0; i < d_ptr->m_values.count(); i++) {
166 166 total += d_ptr->m_values.at(i);
167 167 }
168 168 return total;
169 169 }
170 170
171 171 /*!
172 172 Sets pen for set. Bars of this set are drawn using \a pen
173 173 */
174 174 void QBarSet::setPen(const QPen &pen)
175 175 {
176 176 if(d_ptr->m_pen!=pen){
177 177 d_ptr->m_pen = pen;
178 178 emit d_ptr->updatedBars();
179 179 }
180 180 }
181 181
182 182 /*!
183 183 Returns pen of the set.
184 184 */
185 185 QPen QBarSet::pen() const
186 186 {
187 187 return d_ptr->m_pen;
188 188 }
189 189
190 190 /*!
191 191 Sets brush for the set. Bars of this set are drawn using \a brush
192 192 */
193 193 void QBarSet::setBrush(const QBrush &brush)
194 194 {
195 195 if(d_ptr->m_brush!=brush){
196 196 d_ptr->m_brush = brush;
197 197 emit d_ptr->updatedBars();
198 198 }
199 199 }
200 200
201 201 /*!
202 202 Returns brush of the set.
203 203 */
204 204 QBrush QBarSet::brush() const
205 205 {
206 206 return d_ptr->m_brush;
207 207 }
208 208
209 209 /*!
210 210 Sets \a pen of the values that are drawn on top of this barset
211 211 */
212 212 void QBarSet::setLabelPen(const QPen &pen)
213 213 {
214 214 if(d_ptr->m_labelPen!=pen){
215 215 d_ptr->m_labelPen = pen;
216 216 emit d_ptr->updatedBars();
217 217 }
218 218 }
219 219
220 220 /*!
221 221 Returns pen of the values that are drawn on top of this barset
222 222 */
223 223 QPen QBarSet::labelPen() const
224 224 {
225 225 return d_ptr->m_labelPen;
226 226 }
227 227
228 228 /*!
229 229 Sets \a brush of the values that are drawn on top of this barset
230 230 */
231 231 void QBarSet::setLabelBrush(const QBrush &brush)
232 232 {
233 233 if(d_ptr->m_labelBrush!=brush){
234 234 d_ptr->m_labelBrush = brush;
235 235 emit d_ptr->updatedBars();
236 236 }
237 237 }
238 238
239 239 /*!
240 240 Returns brush of the values that are drawn on top of this barset
241 241 */
242 242 QBrush QBarSet::labelBrush() const
243 243 {
244 244 return d_ptr->m_labelBrush;
245 245 }
246 246
247 247 /*!
248 248 Sets the \a font for values that are drawn on top of this barset
249 249 */
250 250 void QBarSet::setLabelFont(const QFont &font)
251 251 {
252 252 if(d_ptr->m_labelFont!=font) {
253 253 d_ptr->m_labelFont = font;
254 254 emit d_ptr->updatedBars();
255 255 }
256 256
257 257 }
258 258
259 259 /*!
260 260 Returns the pen for values that are drawn on top of this set
261 261 */
262 262 QFont QBarSet::labelFont() const
263 263 {
264 264 return d_ptr->m_labelFont;
265 265 }
266 266
267 267 /*!
268 268 Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
269 269 */
270 270
271 271 void QBarSet::setLabelsVisible(bool visible)
272 272 {
273 273 if(d_ptr->m_labelsVisible!=visible) {
274 274 d_ptr->m_labelsVisible = visible;
275 275 emit d_ptr->labelsVisibleChanged(visible);
276 276 }
277 277 }
278 278
279 279 /*!
280 280 Returns the visibility of values
281 281 */
282 282 bool QBarSet::labelsVisible() const
283 283 {
284 284 return d_ptr->m_labelsVisible;
285 285 }
286 286
287 287 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
288 288
289 289 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
290 290 q_ptr(parent),
291 291 m_name(name),
292 292 m_labelsVisible(false)
293 293 {
294
295 294 }
296 295
297 296 QBarSetPrivate::~QBarSetPrivate()
298 297 {
299
300 298 }
299
301 300 #include "moc_qbarset.cpp"
302 301 #include "moc_qbarset_p.cpp"
303 302
304 303 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,40 +1,41
1 1 #ifndef QBARSET_P_H
2 2 #define QBARSET_P_H
3 3
4 4 #include "qbarset.h"
5 5 #include <QMap>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 class QBarSetPrivate : public QObject
10 10 {
11 11 Q_OBJECT
12 12
13 13 public:
14 14 QBarSetPrivate(const QString name, QBarSet *parent);
15 15 ~QBarSetPrivate();
16 16
17 17 Q_SIGNALS:
18 18 void clicked(QString category);
19 void selected();
19 20 void restructuredBars();
20 21 void updatedBars();
21 22 void labelsVisibleChanged(bool visible);
22 23
23 24 public:
24 25 QBarSet * const q_ptr;
25 26 QString m_name;
26 27 QList<qreal> m_values; // TODO: replace with map (category, value)
27 28 QMap<QString, qreal> m_mappedValues;
28 29 QPen m_pen;
29 30 QBrush m_brush;
30 31 QPen m_labelPen;
31 32 QBrush m_labelBrush;
32 33 QFont m_labelFont;
33 34 bool m_labelsVisible;
34 35
35 36 friend class QBarSet;
36 37 };
37 38
38 39 QTCOMMERCIALCHART_END_NAMESPACE
39 40
40 41 #endif // QBARSETPRIVATE_P_H
@@ -1,194 +1,194
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 29 #include "qbarset_p.h"
30 30 #include "qareaseries.h"
31 31 #include "qareaseries_p.h"
32 32 #include <QPainter>
33 33 #include <QGraphicsSceneEvent>
34 34 #include <QGraphicsSimpleTextItem>
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 38 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : QGraphicsObject(legend),
39 39 m_series(series),
40 40 m_markerRect(0,0,10.0,10.0),
41 41 m_boundingRect(0,0,0,0),
42 42 m_legend(legend),
43 43 m_textItem(new QGraphicsSimpleTextItem(this)),
44 44 m_rectItem(new QGraphicsRectItem(this))
45 45 {
46 46 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
47 47 m_rectItem->setRect(m_markerRect);
48 48 updateLayout();
49 49 }
50 50
51 51 void LegendMarker::setPen(const QPen &pen)
52 52 {
53 53 m_textItem->setPen(pen);
54 54 updateLayout();
55 55 }
56 56
57 57 QPen LegendMarker::pen() const
58 58 {
59 59 return m_textItem->pen();
60 60 }
61 61
62 62 void LegendMarker::setBrush(const QBrush &brush)
63 63 {
64 64 m_rectItem->setBrush(brush);
65 65 }
66 66
67 67 QBrush LegendMarker::brush() const
68 68 {
69 69 return m_rectItem->brush();
70 70 }
71 71
72 72 void LegendMarker::setLabel(const QString name)
73 73 {
74 74 m_textItem->setText(name);
75 75 updateLayout();
76 76 }
77 77
78 78 void LegendMarker::setSize(const QSize& size)
79 79 {
80 80 m_markerRect = QRectF(0,0,size.width(),size.height());
81 81 }
82 82
83 83 QString LegendMarker::label() const
84 84 {
85 85 return m_textItem->text();
86 86 }
87 87
88 88 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
89 89 {
90 90 Q_UNUSED(option)
91 91 Q_UNUSED(widget)
92 92 Q_UNUSED(painter)
93 93 }
94 94
95 95 QRectF LegendMarker::boundingRect() const
96 96 {
97 97 return m_boundingRect;
98 98 }
99 99
100 100 void LegendMarker::updateLayout()
101 101 {
102 102
103 103 static const qreal margin = 2;
104 104 static const qreal space = 4;
105 105
106 106 const QRectF& textRect = m_textItem->boundingRect();
107 107 prepareGeometryChange();
108 108 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
109 109 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
110 110 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
111 111
112 112 }
113 113
114 114 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
115 115 {
116 116 QGraphicsObject::mousePressEvent(event);
117 117 emit selected();
118 118 }
119 119
120 120 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121 121
122 122 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
123 123 m_series(series)
124 124 {
125 125 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
126 126 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
127 127 updated();
128 128 }
129 129
130 130 void AreaLegendMarker::updated()
131 131 {
132 132 setBrush(m_series->brush());
133 133 setLabel(m_series->name());
134 134 }
135 135
136 136 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
137 137
138 138 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
139 139 m_barset(barset)
140 140 {
141 QObject::connect(this, SIGNAL(selected()),barseries, SIGNAL(selected()));
141 QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
142 142 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
143 143 updated();
144 144 }
145 145
146 146 void BarLegendMarker::updated()
147 147 {
148 148 setBrush(m_barset->brush());
149 149 setLabel(m_barset->name());
150 150 }
151 151
152 152 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153 153
154 154 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
155 155 m_pieslice(pieslice)
156 156 {
157 157 QObject::connect(this, SIGNAL(selected()),pieslice, SIGNAL(clicked()));
158 158 QObject::connect(pieslice, SIGNAL(changed()), this, SLOT(updated()));
159 159 updated();
160 160 }
161 161
162 162 void PieLegendMarker::updated()
163 163 {
164 164 setBrush(m_pieslice->brush());
165 165 setLabel(m_pieslice->label());
166 166 }
167 167
168 168 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169 169
170 170 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
171 171 m_series(series)
172 172 {
173 173 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
174 174 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
175 175 updated();
176 176 }
177 177
178 178 void XYLegendMarker::updated()
179 179 {
180 180 setLabel(m_series->name());
181 181
182 182 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
183 183 {
184 184 setBrush(m_series->brush());
185 185
186 186 }
187 187 else {
188 188 setBrush(QBrush(m_series->pen().color()));
189 189 }
190 190 }
191 191
192 192 #include "moc_legendmarker_p.cpp"
193 193
194 194 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now