##// END OF EJS Templates
fix to example. Event handling prototyping.
sauimone -
r2169:04485231feed
parent child
Show More
@@ -1,301 +1,301
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 "mainwidget.h"
22 22 #include <QChart>
23 23 #include <QChartView>
24 24 #include <QPushButton>
25 25 #include <QLabel>
26 26 #include <QDebug>
27 27 #include <QLegend>
28 28 #include <QFormLayout>
29 29 #include <QPieSeries>
30 30 #include <QPieSlice>
31 31 #include <QLegendMarker>
32 32
33 33 QTCOMMERCIALCHART_USE_NAMESPACE
34 34
35 35 MainWidget::MainWidget(QWidget *parent) :
36 36 QWidget(parent)
37 37 {
38 38 // Create buttons for ui
39 39 m_buttonLayout = new QGridLayout();
40 40 QPushButton *detachLegendButton = new QPushButton("Toggle attached");
41 41 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached()));
42 42 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
43 43
44 44 QPushButton *addSliceButton = new QPushButton("add slice");
45 45 connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSlice()));
46 46 m_buttonLayout->addWidget(addSliceButton, 2, 0);
47 47 QPushButton *removeSliceButton = new QPushButton("remove slice");
48 48 connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSlice()));
49 49 m_buttonLayout->addWidget(removeSliceButton, 3, 0);
50 50
51 51 QPushButton *alignButton = new QPushButton("Align (Bottom)");
52 52 connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment()));
53 53 m_buttonLayout->addWidget(alignButton, 4, 0);
54 54
55 55 QPushButton *boldButton = new QPushButton("Toggle bold");
56 56 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
57 57 m_buttonLayout->addWidget(boldButton, 5, 0);
58 58
59 59 QPushButton *italicButton = new QPushButton("Toggle italic");
60 60 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
61 61 m_buttonLayout->addWidget(italicButton, 6, 0);
62 62
63 63 QPushButton *infoButton = new QPushButton("Debug info");
64 64 connect(infoButton, SIGNAL(clicked()), this, SLOT(showDebugInfo()));
65 65 m_buttonLayout->addWidget(infoButton, 7, 0);
66 66
67 67 QPushButton *connectButton = new QPushButton("Connect markers");
68 68 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectMarkers()));
69 69 m_buttonLayout->addWidget(connectButton, 8, 0);
70 70
71 71 QPushButton *disConnectButton = new QPushButton("Disconnect markers");
72 72 connect(disConnectButton, SIGNAL(clicked()), this, SLOT(disconnectMarkers()));
73 m_buttonLayout->addWidget(connectButton, 8, 0);
73 m_buttonLayout->addWidget(disConnectButton, 9, 0);
74 74
75 75
76 76 m_legendPosX = new QDoubleSpinBox();
77 77 m_legendPosY = new QDoubleSpinBox();
78 78 m_legendWidth = new QDoubleSpinBox();
79 79 m_legendHeight = new QDoubleSpinBox();
80 80
81 81 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
82 82 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 83 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 84 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 85
86 86 QFormLayout* legendLayout = new QFormLayout();
87 87 legendLayout->addRow("HPos", m_legendPosX);
88 88 legendLayout->addRow("VPos", m_legendPosY);
89 89 legendLayout->addRow("Width", m_legendWidth);
90 90 legendLayout->addRow("Height", m_legendHeight);
91 91 m_legendSettings = new QGroupBox("Detached legend");
92 92 m_legendSettings->setLayout(legendLayout);
93 93 m_buttonLayout->addWidget(m_legendSettings);
94 94 m_legendSettings->setVisible(false);
95 95
96 96 // Create chart view with the chart
97 97 m_chart = new QChart();
98 98 m_chartView = new QChartView(m_chart, this);
99 99
100 100 // Create spinbox to modify font size
101 101 m_fontSize = new QDoubleSpinBox();
102 102 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
103 103 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
104 104
105 105 QFormLayout* fontLayout = new QFormLayout();
106 106 fontLayout->addRow("Legend font size", m_fontSize);
107 107
108 108 // Create layout for grid and detached legend
109 109 m_mainLayout = new QGridLayout();
110 110 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
111 111 m_mainLayout->addLayout(fontLayout,1,0);
112 112 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
113 113 setLayout(m_mainLayout);
114 114
115 115 createSeries();
116 116 }
117 117
118 118 void MainWidget::createSeries()
119 119 {
120 120 m_series = new QPieSeries();
121 121 addSlice();
122 122 addSlice();
123 123 addSlice();
124 124 addSlice();
125 125
126 126 m_chart->addSeries(m_series);
127 127 m_chart->setTitle("Legend detach example");
128 128 m_chart->createDefaultAxes();
129 129 //![1]
130 130 m_chart->legend()->setVisible(true);
131 131 m_chart->legend()->setAlignment(Qt::AlignBottom);
132 132 //![1]
133 133
134 134 m_chartView->setRenderHint(QPainter::Antialiasing);
135 135 }
136 136
137 137 void MainWidget::showLegendSpinbox()
138 138 {
139 139 m_legendSettings->setVisible(true);
140 140 QRectF chartViewRect = m_chartView->rect();
141 141
142 142 m_legendPosX->setMinimum(0);
143 143 m_legendPosX->setMaximum(chartViewRect.width());
144 144 m_legendPosX->setValue(150);
145 145
146 146 m_legendPosY->setMinimum(0);
147 147 m_legendPosY->setMaximum(chartViewRect.height());
148 148 m_legendPosY->setValue(150);
149 149
150 150 m_legendWidth->setMinimum(0);
151 151 m_legendWidth->setMaximum(chartViewRect.width());
152 152 m_legendWidth->setValue(150);
153 153
154 154 m_legendHeight->setMinimum(0);
155 155 m_legendHeight->setMaximum(chartViewRect.height());
156 156 m_legendHeight->setValue(75);
157 157 }
158 158
159 159 void MainWidget::hideLegendSpinbox()
160 160 {
161 161 m_legendSettings->setVisible(false);
162 162 }
163 163
164 164
165 165 void MainWidget::toggleAttached()
166 166 {
167 167 QLegend *legend = m_chart->legend();
168 168 if (legend->isAttachedToChart()) {
169 169 //![2]
170 170 legend->detachFromChart();
171 171 m_chart->legend()->setBackgroundVisible(true);
172 172 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
173 173 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
174 174 //![2]
175 175 showLegendSpinbox();
176 176 updateLegendLayout();
177 177 } else {
178 178 //![3]
179 179 legend->attachToChart();
180 180 legend->setBackgroundVisible(false);
181 181 //![3]
182 182 hideLegendSpinbox();
183 183 }
184 184 update();
185 185 }
186 186
187 187 void MainWidget::addSlice()
188 188 {
189 189 QPieSlice* slice = new QPieSlice(QString("slice " + QString::number(m_series->count())), m_series->count()+1);
190 190 m_series->append(slice);
191 191 }
192 192
193 193 void MainWidget::removeSlice()
194 194 {
195 195 QList<QPieSlice*> slices = m_series->slices();
196 196 if (slices.count() > 0) {
197 197 m_series->remove(slices.at(slices.count()-1));
198 198 }
199 199 }
200 200
201 201 void MainWidget::connectMarkers()
202 202 {
203 203 // Example use case.
204 204 // Explode slice via marker.
205 205 // Should be doable via public api.
206 206
207 207 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
208 208 // Disconnect possible existing connection to avoid multiple connections
209 209 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
210 210 QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
211 211 }
212 212 }
213 213
214 214 void MainWidget::disconnectMarkers()
215 215 {
216 216 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
217 217 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
218 218 }
219 219 }
220 220
221 221 void MainWidget::setLegendAlignment()
222 222 {
223 223 QPushButton *button = qobject_cast<QPushButton *>(sender());
224 224
225 225 switch (m_chart->legend()->alignment()) {
226 226 case Qt::AlignTop:
227 227 m_chart->legend()->setAlignment(Qt::AlignLeft);
228 228 if (button)
229 229 button->setText("Align (Left)");
230 230 break;
231 231 case Qt::AlignLeft:
232 232 m_chart->legend()->setAlignment(Qt::AlignBottom);
233 233 if (button)
234 234 button->setText("Align (Bottom)");
235 235 break;
236 236 case Qt::AlignBottom:
237 237 m_chart->legend()->setAlignment(Qt::AlignRight);
238 238 if (button)
239 239 button->setText("Align (Right)");
240 240 break;
241 241 default:
242 242 if (button)
243 243 button->setText("Align (Top)");
244 244 m_chart->legend()->setAlignment(Qt::AlignTop);
245 245 break;
246 246 }
247 247 }
248 248
249 249 void MainWidget::toggleBold()
250 250 {
251 251 QFont font = m_chart->legend()->font();
252 252 font.setBold(!font.bold());
253 253 m_chart->legend()->setFont(font);
254 254 }
255 255
256 256 void MainWidget::toggleItalic()
257 257 {
258 258 QFont font = m_chart->legend()->font();
259 259 font.setItalic(!font.italic());
260 260 m_chart->legend()->setFont(font);
261 261 }
262 262
263 263 void MainWidget::showDebugInfo()
264 264 {
265 265 qDebug() << "marker count:" << m_chart->legend()->markers().count();
266 266 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
267 267 qDebug() << "marker series type:" << marker->series()->type();
268 268 qDebug() << "peer object:" << marker->peerObject();
269 269 qDebug() << "label:" << marker->label();
270 270 }
271 271 }
272 272
273 273 void MainWidget::fontSizeChanged()
274 274 {
275 275 QFont font = m_chart->legend()->font();
276 276 font.setPointSizeF(m_fontSize->value());
277 277 m_chart->legend()->setFont(font);
278 278 }
279 279
280 280 void MainWidget::updateLegendLayout()
281 281 {
282 282 //![4]
283 283 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
284 284 ,m_legendPosY->value()
285 285 ,m_legendWidth->value()
286 286 ,m_legendHeight->value()));
287 287 m_chart->legend()->update();
288 288 //![4]
289 289 }
290 290
291 291 void MainWidget::handleMarkerClicked()
292 292 {
293 293 QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
294 294
295 295 qDebug() << "marker clicked:" << marker;
296 296
297 297 QPieSlice* slice = qobject_cast<QPieSlice*> (marker->peerObject());
298 298 Q_ASSERT(slice);
299 299
300 300 slice->setExploded(!slice->isExploded());
301 301 }
@@ -1,177 +1,177
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 "legendmarkeritem_p.h"
22 22 #include <QPainter>
23 23 #include <QGraphicsSceneEvent>
24 24 #include <QGraphicsSimpleTextItem>
25 25 #include <QDebug>
26 26
27 27 #include "qlegendmarker_p.h"
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 //LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) :
32 32 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
33 33 QGraphicsObject(parent),
34 34 m_marker(marker),
35 35 m_markerRect(0,0,10.0,10.0),
36 36 m_boundingRect(0,0,10,10),
37 37 m_textItem(new QGraphicsSimpleTextItem(this)),
38 38 m_rectItem(new QGraphicsRectItem(this)),
39 39 m_margin(4),
40 40 m_space(4)
41 41 {
42 42 // qDebug() << "LegendMarkerItem created for marker:" << m_marker;
43 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
43 // setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
44 44 m_rectItem->setRect(m_markerRect);
45 45 // setZValue(zValue() + 20);
46 46 // qDebug() << "z:" << this->zValue();
47 47 }
48 48
49 49 void LegendMarkerItem::setPen(const QPen &pen)
50 50 {
51 51 m_rectItem->setPen(pen);
52 52 }
53 53
54 54 QPen LegendMarkerItem::pen() const
55 55 {
56 56 return m_rectItem->pen();
57 57 }
58 58
59 59 void LegendMarkerItem::setBrush(const QBrush &brush)
60 60 {
61 61 m_rectItem->setBrush(brush);
62 62 }
63 63
64 64 QBrush LegendMarkerItem::brush() const
65 65 {
66 66 return m_rectItem->brush();
67 67 }
68 68
69 69 void LegendMarkerItem::setFont(const QFont &font)
70 70 {
71 71 m_textItem->setFont(font);
72 72 QFontMetrics fn(font);
73 73 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
74 74 updateGeometry();
75 75 }
76 76
77 77 QFont LegendMarkerItem::font() const
78 78 {
79 79 return m_textItem->font();
80 80 }
81 81
82 82 void LegendMarkerItem::setLabel(const QString label)
83 83 {
84 84 qDebug() << "LegendMarkerItem::setlabel" << label;
85 85 m_text = label;
86 86 updateGeometry();
87 87 }
88 88
89 89 QString LegendMarkerItem::label() const
90 90 {
91 91 return m_text;
92 92 }
93 93
94 94 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
95 95 {
96 96 m_textItem->setBrush(brush);
97 97 }
98 98
99 99 QBrush LegendMarkerItem::labelBrush() const
100 100 {
101 101 return m_textItem->brush();
102 102 }
103 103
104 104 void LegendMarkerItem::setGeometry(const QRectF& rect)
105 105 {
106 106 QFontMetrics fn (m_font);
107 107
108 108 int width = rect.width();
109 109 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
110 110 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
111 111
112 112 if (fn.boundingRect(m_text).width() + x > width)
113 113 {
114 114 QString string = m_text + "...";
115 115 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
116 116 string.remove(string.length() - 4, 1);
117 117 m_textItem->setText(string);
118 118 }
119 119 else
120 120 m_textItem->setText(m_text);
121 121
122 122 const QRectF& textRect = m_textItem->boundingRect();
123 123
124 124
125 125 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
126 126 m_rectItem->setRect(m_markerRect);
127 127 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
128 128
129 129 prepareGeometryChange();
130 130 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
131 131 }
132 132
133 133 QRectF LegendMarkerItem::boundingRect() const
134 134 {
135 135 return m_boundingRect;
136 136 }
137 137
138 138 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
139 139 {
140 140 Q_UNUSED(option)
141 141 Q_UNUSED(widget)
142 142 Q_UNUSED(painter)
143 143 }
144 144
145 145 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
146 146 {
147 147 Q_UNUSED(constraint)
148 148
149 149 QFontMetrics fn(m_textItem->font());
150 150 QSizeF sh;
151 151
152 152 switch (which) {
153 153 case Qt::MinimumSize:
154 154 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));
155 155 break;
156 156 case Qt::PreferredSize:
157 157 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));
158 158 break;
159 159 default:
160 160 break;
161 161 }
162 162
163 163 return sh;
164 164 }
165 165
166 166 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
167 167 {
168 168 qDebug() << "LegendMarkerItem::mousePressEvent";
169 169 // QGraphicsObject::mousePressEvent(event);
170 170 //TODO: selected signal removed for now
171 emit m_marker->handleMousePressEvent(event);
171 m_marker->handleMousePressEvent(event);
172 172 QGraphicsItem::mousePressEvent(event);
173 173 }
174 174
175 175 #include "moc_legendmarkeritem_p.cpp"
176 176
177 177 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,597 +1,597
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 "qlegend.h"
22 22 #include "qlegend_p.h"
23 23 #include "qabstractseries.h"
24 24 #include "qabstractseries_p.h"
25 25 #include "qchart_p.h"
26 26 #include "legendlayout_p.h"
27 27 #include "legendmarker_p.h" // TODO: deprecated
28 28 #include "qxyseries.h"
29 29 #include "qlineseries.h"
30 30 #include "qareaseries.h"
31 31 #include "qscatterseries.h"
32 32 #include "qsplineseries.h"
33 33 #include "qabstractbarseries.h"
34 34 #include "qstackedbarseries.h"
35 35 #include "qpercentbarseries.h"
36 36 #include "qbarset.h"
37 37 #include "qpieseries.h"
38 38 #include "qpieseries_p.h"
39 39 #include "qpieslice.h"
40 40 #include "chartpresenter_p.h"
41 41 #include "chartlayout_p.h"
42 42 #include <QPainter>
43 43 #include <QPen>
44 44 #include <QTimer>
45 45 #include <QGraphicsSceneEvent>
46 46
47 47 #include <QLegendMarker>
48 48 #include "qlegendmarker_p.h"
49 49 #include "legendmarkeritem_p.h"
50 50
51 51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52 52
53 53 /*!
54 54 \class QLegend
55 55 \brief Legend object
56 56 \mainclass
57 57
58 58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
59 59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
60 60 handle the drawing manually.
61 61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
62 62
63 63 \image examples_percentbarchart_legend.png
64 64
65 65 \sa QChart
66 66 */
67 67 /*!
68 68 \qmlclass Legend QLegend
69 69 \brief Legend is part of QtCommercial Chart QML API.
70 70
71 71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
72 72 series have been changed. Legend is used via ChartView class. For example:
73 73 \code
74 74 ChartView {
75 75 legend.visible: true
76 76 legend.alignment: Qt.AlignBottom
77 77 // Add a few series...
78 78 }
79 79 \endcode
80 80
81 81 \image examples_percentbarchart_legend.png
82 82 */
83 83
84 84 /*!
85 85 \property QLegend::alignment
86 86 \brief The alignment of the legend.
87 87
88 88 Legend paints on the defined position in the chart. The following alignments are supported:
89 89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
90 90 */
91 91 /*!
92 92 \qmlproperty Qt.Alignment Legend::alignment
93 93 \brief The alignment of the legend.
94 94
95 95 Legend paints on the defined position in the chart. The following alignments are supported:
96 96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
97 97 */
98 98
99 99 /*!
100 100 \property QLegend::backgroundVisible
101 101 Whether the legend background is visible or not.
102 102 */
103 103 /*!
104 104 \qmlproperty bool Legend::backgroundVisible
105 105 Whether the legend background is visible or not.
106 106 */
107 107
108 108 /*!
109 109 \property QLegend::color
110 110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
111 111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
112 112 */
113 113 /*!
114 114 \qmlproperty color Legend::color
115 115 The color of the legend, i.e. the background (brush) color.
116 116 */
117 117
118 118 /*!
119 119 \property QLegend::borderColor
120 120 The border color of the legend, i.e. the line color.
121 121 */
122 122 /*!
123 123 \qmlproperty color Legend::borderColor
124 124 The border color of the legend, i.e. the line color.
125 125 */
126 126
127 127 /*!
128 128 \property QLegend::font
129 129 The font of markers used by legend
130 130 */
131 131 /*!
132 132 \qmlproperty Font Legend::font
133 133 The font of markers used by legend
134 134 */
135 135
136 136 /*!
137 137 \property QLegend::labelColor
138 138 The color of brush used to draw labels.
139 139 */
140 140 /*!
141 141 \qmlproperty color QLegend::labelColor
142 142 The color of brush used to draw labels.
143 143 */
144 144
145 145 /*!
146 146 \fn void QLegend::backgroundVisibleChanged(bool)
147 147 The visibility of the legend background changed to \a visible.
148 148 */
149 149
150 150 /*!
151 151 \fn void QLegend::colorChanged(QColor)
152 152 The color of the legend background changed to \a color.
153 153 */
154 154
155 155 /*!
156 156 \fn void QLegend::borderColorChanged(QColor)
157 157 The border color of the legend background changed to \a color.
158 158 */
159 159
160 160 /*!
161 161 \fn void QLegend::fontChanged(QFont)
162 162 The font of markers of the legend changed to \a font.
163 163 */
164 164
165 165 /*!
166 166 \fn void QLegend::labelColorChanged(QColor color)
167 167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
168 168 */
169 169
170 170 /*!
171 171 Constructs the legend object and sets the parent to \a parent
172 172 */
173 173
174 174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
175 175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
176 176 {
177 177 setZValue(ChartPresenter::LegendZValue);
178 178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
179 179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
180 180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
181 181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
182 182 setLayout(d_ptr->m_layout);
183 183 }
184 184
185 185 /*!
186 186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
187 187 */
188 188 QLegend::~QLegend()
189 189 {
190 190 }
191 191
192 192 /*!
193 193 \internal
194 194 */
195 195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
196 196 {
197 197 Q_UNUSED(option)
198 198 Q_UNUSED(widget)
199 199
200 200 if (!d_ptr->m_backgroundVisible)
201 201 return;
202 202
203 203 painter->setOpacity(opacity());
204 204 painter->setPen(d_ptr->m_pen);
205 205 painter->setBrush(d_ptr->m_brush);
206 206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
207 207 }
208 208
209 209
210 210 /*!
211 211 Sets the \a brush of legend. Brush affects the background of legend.
212 212 */
213 213 void QLegend::setBrush(const QBrush &brush)
214 214 {
215 215 if (d_ptr->m_brush != brush) {
216 216 d_ptr->m_brush = brush;
217 217 update();
218 218 emit colorChanged(brush.color());
219 219 }
220 220 }
221 221
222 222 /*!
223 223 Returns the brush used by legend.
224 224 */
225 225 QBrush QLegend::brush() const
226 226 {
227 227 return d_ptr->m_brush;
228 228 }
229 229
230 230 void QLegend::setColor(QColor color)
231 231 {
232 232 QBrush b = d_ptr->m_brush;
233 233 if (b.style() != Qt::SolidPattern || b.color() != color) {
234 234 b.setStyle(Qt::SolidPattern);
235 235 b.setColor(color);
236 236 setBrush(b);
237 237 }
238 238 }
239 239
240 240 QColor QLegend::color()
241 241 {
242 242 return d_ptr->m_brush.color();
243 243 }
244 244
245 245 /*!
246 246 Sets the \a pen of legend. Pen affects the legend borders.
247 247 */
248 248 void QLegend::setPen(const QPen &pen)
249 249 {
250 250 if (d_ptr->m_pen != pen) {
251 251 d_ptr->m_pen = pen;
252 252 update();
253 253 emit borderColorChanged(pen.color());
254 254 }
255 255 }
256 256
257 257 /*!
258 258 Returns the pen used by legend
259 259 */
260 260
261 261 QPen QLegend::pen() const
262 262 {
263 263 return d_ptr->m_pen;
264 264 }
265 265
266 266 void QLegend::setFont(const QFont &font)
267 267 {
268 268 if (d_ptr->m_font != font) {
269 269 d_ptr->m_font = font;
270 270 foreach (LegendMarker *marker, d_ptr->markers())
271 271 marker->setFont(d_ptr->m_font);
272 272 layout()->invalidate();
273 273 emit fontChanged(font);
274 274 }
275 275 }
276 276
277 277 QFont QLegend::font() const
278 278 {
279 279 return d_ptr->m_font;
280 280 }
281 281
282 282 void QLegend::setBorderColor(QColor color)
283 283 {
284 284 QPen p = d_ptr->m_pen;
285 285 if (p.color() != color) {
286 286 p.setColor(color);
287 287 setPen(p);
288 288 }
289 289 }
290 290
291 291 QColor QLegend::borderColor()
292 292 {
293 293 return d_ptr->m_pen.color();
294 294 }
295 295
296 296 /*!
297 297 Set brush used to draw labels to \a brush.
298 298 */
299 299 void QLegend::setLabelBrush(const QBrush &brush)
300 300 {
301 301 if (d_ptr->m_labelBrush != brush) {
302 302 d_ptr->m_labelBrush = brush;
303 303 foreach (LegendMarker *marker, d_ptr->markers()) {
304 304 marker->setLabelBrush(d_ptr->m_labelBrush);
305 305 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
306 306 // instead of mapping it from label brush color
307 307 marker->setPen(brush.color());
308 308 }
309 309 emit labelColorChanged(brush.color());
310 310 }
311 311 }
312 312
313 313 /*!
314 314 Brush used to draw labels.
315 315 */
316 316 QBrush QLegend::labelBrush() const
317 317 {
318 318 return d_ptr->m_labelBrush;
319 319 }
320 320
321 321 void QLegend::setLabelColor(QColor color)
322 322 {
323 323 QBrush b = d_ptr->m_labelBrush;
324 324 if (b.style() != Qt::SolidPattern || b.color() != color) {
325 325 b.setStyle(Qt::SolidPattern);
326 326 b.setColor(color);
327 327 setLabelBrush(b);
328 328 }
329 329 }
330 330
331 331 QColor QLegend::labelColor() const
332 332 {
333 333 return d_ptr->m_labelBrush.color();
334 334 }
335 335
336 336
337 337 void QLegend::setAlignment(Qt::Alignment alignment)
338 338 {
339 339 if (d_ptr->m_alignment != alignment) {
340 340 d_ptr->m_alignment = alignment;
341 341 layout()->invalidate();
342 342 }
343 343 }
344 344
345 345 Qt::Alignment QLegend::alignment() const
346 346 {
347 347 return d_ptr->m_alignment;
348 348 }
349 349
350 350 /*!
351 351 Detaches the legend from chart. Chart won't change layout of the legend.
352 352 */
353 353 void QLegend::detachFromChart()
354 354 {
355 355 d_ptr->m_attachedToChart = false;
356 356 layout()->invalidate();
357 357 setParent(0);
358 358
359 359 }
360 360
361 361 /*!
362 362 Attaches the legend to chart. Chart may change layout of the legend.
363 363 */
364 364 void QLegend::attachToChart()
365 365 {
366 366 d_ptr->m_attachedToChart = true;
367 367 layout()->invalidate();
368 368 setParent(d_ptr->m_chart);
369 369 }
370 370
371 371 /*!
372 372 Returns true, if legend is attached to chart.
373 373 */
374 374 bool QLegend::isAttachedToChart()
375 375 {
376 376 return d_ptr->m_attachedToChart;
377 377 }
378 378
379 379 /*!
380 380 Sets the visibility of legend background to \a visible
381 381 */
382 382 void QLegend::setBackgroundVisible(bool visible)
383 383 {
384 384 if (d_ptr->m_backgroundVisible != visible) {
385 385 d_ptr->m_backgroundVisible = visible;
386 386 update();
387 387 emit backgroundVisibleChanged(visible);
388 388 }
389 389 }
390 390
391 391 /*!
392 392 Returns the visibility of legend background
393 393 */
394 394 bool QLegend::isBackgroundVisible() const
395 395 {
396 396 return d_ptr->m_backgroundVisible;
397 397 }
398 398
399 399
400 400 QList<QLegendMarker*> QLegend::markers() const
401 401 {
402 402 // TODO: name of PIMPL method will change.
403 403 return d_ptr->legendMarkers();
404 404 }
405 405
406 406 void QLegend::appendSeries(QAbstractSeries* series)
407 407 {
408 408 d_ptr->appendSeries(series);
409 409 }
410 410
411 411 void QLegend::removeSeries(QAbstractSeries* series)
412 412 {
413 413 d_ptr->removeSeries(series);
414 414 }
415 415
416 416 /*!
417 417 \internal \a event see QGraphicsWidget for details
418 418 */
419 419 void QLegend::hideEvent(QHideEvent *event)
420 420 {
421 421 if (isAttachedToChart())
422 422 d_ptr->m_presenter->layout()->invalidate();
423 423 QGraphicsWidget::hideEvent(event);
424 424 }
425 425 /*!
426 426 \internal \a event see QGraphicsWidget for details
427 427 */
428 428 void QLegend::showEvent(QShowEvent *event)
429 429 {
430 430 if (isAttachedToChart()) {
431 431 d_ptr->items()->setVisible(false);
432 432 layout()->invalidate();
433 433 }
434 434 QGraphicsWidget::showEvent(event);
435 435 //layout activation will show the items
436 436 }
437 437
438 438 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
439 439
440 440 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
441 441 : q_ptr(q),
442 442 m_presenter(presenter),
443 443 m_layout(new LegendLayout(q)),
444 444 m_chart(chart),
445 445 m_items(new QGraphicsItemGroup(q)),
446 446 m_alignment(Qt::AlignTop),
447 447 m_brush(QBrush()),
448 448 m_pen(QPen()),
449 449 m_labelBrush(QBrush()),
450 450 m_diameter(5),
451 451 m_attachedToChart(true),
452 452 m_backgroundVisible(false)
453 453 {
454
454 m_items->setHandlesChildEvents(false);
455 455 }
456 456
457 457 QLegendPrivate::~QLegendPrivate()
458 458 {
459 459
460 460 }
461 461
462 462 void QLegendPrivate::setOffset(qreal x, qreal y)
463 463 {
464 464 m_layout->setOffset(x, y);
465 465 }
466 466
467 467 QPointF QLegendPrivate::offset() const
468 468 {
469 469 return m_layout->offset();
470 470 }
471 471
472 472 int QLegendPrivate::roundness(qreal size)
473 473 {
474 474 return 100 * m_diameter / int(size);
475 475 }
476 476
477 477 void QLegendPrivate::appendSeries(QAbstractSeries* series)
478 478 {
479 479 Q_UNUSED(series);
480 480 // TODO:
481 481 /*
482 482 if (!m_series.contains(series)) {
483 483 m_series.append(series);
484 484 handleSeriesAdded(series,0); // Dummy domain
485 485 }
486 486 */
487 487 }
488 488
489 489 void QLegendPrivate::removeSeries(QAbstractSeries* series)
490 490 {
491 491 Q_UNUSED(series);
492 492 // TODO:
493 493 /*
494 494 if (m_series.contains(series)) {
495 495 m_series.removeOne(series);
496 496 handleSeriesRemoved(series);
497 497 }
498 498 */
499 499 }
500 500
501 501 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
502 502 {
503 503 Q_UNUSED(domain)
504 504
505 505 qDebug() << "QLegendPrivate::handleSeriesAdded";
506 506
507 507 // New markers --->
508 508 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
509 509 foreach (QLegendMarker* marker, newMarkers) {
510 510 marker->setFont(m_font);
511 511 marker->setLabelBrush(m_labelBrush);
512 512 marker->setVisible(series->isVisible());
513 513 m_items->addToGroup(marker->d_ptr.data()->item());
514 514 m_legendMarkers << marker;
515 515 }
516 516
517 517 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
518 518 // <--- New markers
519 519
520 520 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
521 521
522 522 foreach (LegendMarker *marker, markers) {
523 523 marker->setFont(m_font);
524 524 marker->setLabelBrush(m_labelBrush);
525 525 marker->setVisible(series->isVisible());
526 526 m_items->addToGroup(marker);
527 527 m_markers << marker;
528 528 }
529 529
530 530 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
531 531 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
532 532
533 533 m_items->setVisible(false);
534 534 m_layout->invalidate();
535 535 }
536 536
537 537 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
538 538 {
539 539 // New markers --->
540 540 foreach (QLegendMarker *marker, m_legendMarkers) {
541 541 if (marker->series() == series) {
542 542 delete marker;
543 543 m_legendMarkers.removeAll(marker);
544 544 }
545 545 }
546 546
547 547 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
548 548 // <--- New markers
549 549
550 550 foreach (LegendMarker *marker, m_markers) {
551 551 if (marker->series() == series) {
552 552 delete marker;
553 553 m_markers.removeAll(marker);
554 554 }
555 555 }
556 556
557 557 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
558 558 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
559 559 m_layout->invalidate();
560 560 }
561 561
562 562 void QLegendPrivate::handleSeriesVisibleChanged()
563 563 {
564 564 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
565 565 Q_ASSERT(series);
566 566
567 567 // New markers --->
568 568 foreach (QLegendMarker* marker, m_legendMarkers) {
569 569 if (marker->series() == series) {
570 570 marker->setVisible(series->isVisible());
571 571 }
572 572 }
573 573
574 574 // <--- New markers
575 575
576 576 foreach (LegendMarker* marker, m_markers) {
577 577 if (marker->series() == series) {
578 578 marker->setVisible(series->isVisible());
579 579 }
580 580 m_layout->invalidate();
581 581 }
582 582
583 583 void QLegendPrivate::handleCountChanged()
584 584 {
585 585 // With new markers, the series shoud notify markers directly?
586 586
587 587 // Handle new or removed markers
588 588 // Handle changes of marker pen/brush/label. every property that legend is interested
589 589 handleSeriesRemoved(series);
590 590 Domain domain;
591 591 handleSeriesAdded(series, &domain);
592 592 }
593 593
594 594 #include "moc_qlegend.cpp"
595 595 #include "moc_qlegend_p.cpp"
596 596
597 597 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,189 +1,192
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 "qlegendmarker.h"
22 22 #include "qlegendmarker_p.h"
23 23 #include "legendmarkeritem_p.h"
24 24 #include <QDebug>
25 25 #include <QFontMetrics>
26 #include <QGraphicsSceneEvent>
26 27
27 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 29 /*
29 30 QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) :
30 31 QObject(parent),
31 32 d_ptr(new QLegendMarkerPrivate(series, this))
32 33 {
33 34 }
34 35 */
35 36 QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) :
36 37 QObject(parent),
37 38 d_ptr(&d)
38 39 {
39 40 }
40 41
41 42 QLegendMarker::~QLegendMarker()
42 43 {
43 44 }
44 45
45 46 QString QLegendMarker::label() const
46 47 {
47 48 return d_ptr->label();
48 49 }
49 50
50 51 void QLegendMarker::setLabel(const QString &label)
51 52 {
52 53 d_ptr->setLabel(label);
53 54 }
54 55
55 56 QBrush QLegendMarker::labelBrush() const
56 57 {
57 58 return d_ptr->labelBrush();
58 59 }
59 60
60 61 void QLegendMarker::setLabelBrush(const QBrush &brush)
61 62 {
62 63 d_ptr->setLabelBrush(brush);
63 64 }
64 65
65 66 QFont QLegendMarker::font() const
66 67 {
67 68 return d_ptr->font();
68 69 }
69 70
70 71 void QLegendMarker::setFont(const QFont &font)
71 72 {
72 73 d_ptr->setFont(font);
73 74 }
74 75
75 76 QPen QLegendMarker::pen() const
76 77 {
77 78 return d_ptr->pen();
78 79 }
79 80
80 81 void QLegendMarker::setPen(const QPen &pen)
81 82 {
82 83 d_ptr->setPen(pen);
83 84 }
84 85
85 86 QBrush QLegendMarker::brush() const
86 87 {
87 88 return d_ptr->brush();
88 89 }
89 90
90 91 void QLegendMarker::setBrush(const QBrush &brush)
91 92 {
92 93 d_ptr->setBrush(brush);
93 94 }
94 95
95 96 bool QLegendMarker::isVisible() const
96 97 {
97 98 return d_ptr->isVisible();
98 99 }
99 100
100 101 void QLegendMarker::setVisible(bool visible)
101 102 {
102 103 d_ptr->setVisible(visible);
103 104 }
104 105
105 106 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106 107 QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q) :
107 108 q_ptr(q)
108 109 {
109 110 // qDebug() << "QLegendMarkerPrivate created";
110 111 m_item = new LegendMarkerItem(this);
111 112 }
112 113
113 114 QLegendMarkerPrivate::~QLegendMarkerPrivate()
114 115 {
115 116 }
116 117
117 118 void QLegendMarkerPrivate::handleMousePressEvent(QGraphicsSceneEvent *event)
118 119 {
119 // Just emit clicked signal for now
120 Q_UNUSED(event);
120 // Just emit clicked signal for now (our default logic for events)
121 // This could propably be on the LegendMarkerItem?
122 // TODO: how to handle scrolling vs clicking? drag event?
123 event->accept();
121 124 Q_Q(QLegendMarker);
122 125 emit q->clicked();
123 126 }
124 127
125 128 void QLegendMarkerPrivate::setPen(const QPen &pen)
126 129 {
127 130 m_item->setPen(pen);
128 131 }
129 132
130 133 QPen QLegendMarkerPrivate::pen() const
131 134 {
132 135 return m_item->pen();
133 136 }
134 137
135 138 void QLegendMarkerPrivate::setBrush(const QBrush &brush)
136 139 {
137 140 m_item->setBrush(brush);
138 141 }
139 142
140 143 QBrush QLegendMarkerPrivate::brush() const
141 144 {
142 145 return m_item->brush();
143 146 }
144 147
145 148 void QLegendMarkerPrivate::setFont(const QFont &font)
146 149 {
147 150 m_item->setFont(font);
148 151 }
149 152
150 153 QFont QLegendMarkerPrivate::font() const
151 154 {
152 155 return m_item->font();
153 156 }
154 157
155 158 void QLegendMarkerPrivate::setLabel(const QString label)
156 159 {
157 160 m_item->setLabel(label);
158 161 }
159 162
160 163 QString QLegendMarkerPrivate::label() const
161 164 {
162 165 return m_item->label();
163 166 }
164 167
165 168 void QLegendMarkerPrivate::setLabelBrush(const QBrush &brush)
166 169 {
167 170 m_item->setLabelBrush(brush);
168 171 }
169 172
170 173 QBrush QLegendMarkerPrivate::labelBrush() const
171 174 {
172 175 return m_item->labelBrush();
173 176 }
174 177
175 178 bool QLegendMarkerPrivate::isVisible() const
176 179 {
177 180 return m_item->isVisible();
178 181 }
179 182
180 183 void QLegendMarkerPrivate::setVisible(bool visible)
181 184 {
182 185 m_item->setVisible(visible);
183 186 }
184 187
185 188
186 189 #include "moc_qlegendmarker.cpp"
187 190 #include "moc_qlegendmarker_p.cpp"
188 191
189 192 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now