##// END OF EJS Templates
Fixed a bug with legend attach requiring a screen resize to update UI
Tero Ahola -
r2034:40ab6eb6f3e9
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,29 +1,33
1 /*!
1 /*!
2 \example examples/legend
2 \example examples/legend
3 \title Legend Example
3 \title Legend Example
4 \subtitle
4 \subtitle
5
5
6 This example shows how to detach legend from chart and how to attach it back. By default the chart
6 This example shows how to detach legend from chart and how to attach it back. By default the chart
7 draws the legend inside same view with the chart. In some cases user may want to draw legend to somewhere else. To make this possible the legend can be detached from the chart. Detaching means that chart doesn't draw the legend or try to change it's layout. Detached legend can then be drawn where user wants, for example in different graphics scene. The behaviour of legend can be inspected by running the legend example.
7 draws the legend inside same view with the chart. In some cases user may want to draw legend to somewhere else. To make this possible the legend can be detached from the chart. Detaching means that chart doesn't draw the legend or try to change it's layout. Detached legend can then be drawn where user wants, for example in different graphics scene. The behaviour of legend can be inspected by running the legend example.
8 In example we use barseries where we add or remove barsets. The legend reflects the changes in series. Legend can be detached or attached back to chart and its alignment can be modified.
8 In example we use barseries where we add or remove barsets. The legend reflects the changes in series. Legend can be detached or attached back to chart and its alignment can be modified.
9 When legend is detached, it can be resized and positioned freely.
9 When legend is detached, it can be resized and positioned freely.
10
10
11 \image examples_legend_detach.png
11 \table
12 \row
13 \o \inlineimage examples_legend_detach.png
14 \o \inlineimage examples_legend_detach2.png
15 \endtable
12
16
13 Here we turn legend visible and set its alignment to the bottom of the chart.
17 Here we turn legend visible and set its alignment to the bottom of the chart.
14
18
15 \snippet ../examples/legend/mainwidget.cpp 1
19 \snippet ../examples/legend/mainwidget.cpp 1
16
20
17 This snippet shows how to detach the legend from chart. After detaching, we turn its background to visible and set a different color to it. This makes it easier to see, how the items inside legend are arranged
21 This snippet shows how to detach the legend from chart. After detaching, we turn its background to visible and set a different color to it. This makes it easier to see, how the items inside legend are arranged
18 in detached mode.
22 in detached mode.
19
23
20 \snippet ../examples/legend/mainwidget.cpp 2
24 \snippet ../examples/legend/mainwidget.cpp 2
21
25
22 Here we attach legend back to chart. The background is turned invisible.
26 Here we attach legend back to chart. The background is turned invisible.
23
27
24 \snippet ../examples/legend/mainwidget.cpp 3
28 \snippet ../examples/legend/mainwidget.cpp 3
25
29
26 This shows how we set the detached legend dimensions. After setting new values, we call update to show changes on screen.
30 This shows how we set the detached legend dimensions. After setting new values, we call update to show changes on screen.
27
31
28 \snippet ../examples/legend/mainwidget.cpp 4
32 \snippet ../examples/legend/mainwidget.cpp 4
29 */
33 */
@@ -1,37 +1,37
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "mainwidget.h"
21 #include "mainwidget.h"
22
22
23 #include <QApplication>
23 #include <QApplication>
24 #include <QMainWindow>
24 #include <QMainWindow>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 int main(int argc, char *argv[])
28 int main(int argc, char *argv[])
29 {
29 {
30 QApplication a(argc, argv);
30 QApplication a(argc, argv);
31
31
32 MainWidget w;
32 MainWidget w;
33 w.resize(1024,768);
33 w.resize(720, 480);
34 w.show();
34 w.show();
35
35
36 return a.exec();
36 return a.exec();
37 }
37 }
@@ -1,261 +1,247
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "mainwidget.h"
21 #include "mainwidget.h"
22 #include <QChart>
22 #include <QChart>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QLabel>
25 #include <QLabel>
26 #include <QDebug>
26 #include <QDebug>
27 #include <QBarSet>
27 #include <QBarSet>
28 #include <QBarSeries>
28 #include <QBarSeries>
29 #include <QLegend>
29 #include <QLegend>
30 #include <QFormLayout>
30 #include <QFormLayout>
31
31
32 QTCOMMERCIALCHART_USE_NAMESPACE
32 QTCOMMERCIALCHART_USE_NAMESPACE
33
33
34 MainWidget::MainWidget(QWidget *parent) :
34 MainWidget::MainWidget(QWidget *parent) :
35 QWidget(parent)
35 QWidget(parent)
36 {
36 {
37 // Create buttons for ui
37 // Create buttons for ui
38 m_buttonLayout = new QGridLayout();
38 m_buttonLayout = new QGridLayout();
39 QPushButton *detachLegendButton = new QPushButton("detach legend");
39 QPushButton *detachLegendButton = new QPushButton("Toggle attached");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached()));
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
45
42
46 QPushButton *addSetButton = new QPushButton("add barset");
43 QPushButton *addSetButton = new QPushButton("add barset");
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
44 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
45 m_buttonLayout->addWidget(addSetButton, 2, 0);
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
46 QPushButton *removeBarsetButton = new QPushButton("remove barset");
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
47 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
48 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
52
49
53 QPushButton *leftButton = new QPushButton("Align legend left");
50 QPushButton *alignButton = new QPushButton("Align (Bottom)");
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
51 connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment()));
55 m_buttonLayout->addWidget(leftButton, 4, 0);
52 m_buttonLayout->addWidget(alignButton, 4, 0);
56
57 QPushButton *rightButton = new QPushButton("Align legend right");
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
59 m_buttonLayout->addWidget(rightButton, 5, 0);
60
61 QPushButton *topButton = new QPushButton("Align legend top");
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
63 m_buttonLayout->addWidget(topButton, 6, 0);
64
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
68
53
69 QPushButton *boldButton = new QPushButton("Toggle bold");
54 QPushButton *boldButton = new QPushButton("Toggle bold");
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
55 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
71 m_buttonLayout->addWidget(boldButton, 8, 0);
56 m_buttonLayout->addWidget(boldButton, 8, 0);
72
57
73 QPushButton *italicButton = new QPushButton("Toggle italic");
58 QPushButton *italicButton = new QPushButton("Toggle italic");
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
59 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
75 m_buttonLayout->addWidget(italicButton, 9, 0);
60 m_buttonLayout->addWidget(italicButton, 9, 0);
76
61
77 m_legendPosX = new QDoubleSpinBox();
62 m_legendPosX = new QDoubleSpinBox();
78 m_legendPosY = new QDoubleSpinBox();
63 m_legendPosY = new QDoubleSpinBox();
79 m_legendWidth = new QDoubleSpinBox();
64 m_legendWidth = new QDoubleSpinBox();
80 m_legendHeight = new QDoubleSpinBox();
65 m_legendHeight = new QDoubleSpinBox();
81
66
82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
67 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
68 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
69 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
70 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
86
71
87 QFormLayout* legendLayout = new QFormLayout();
72 QFormLayout* legendLayout = new QFormLayout();
88 legendLayout->addRow("Horizontal position", m_legendPosX);
73 legendLayout->addRow("HPos", m_legendPosX);
89 legendLayout->addRow("Vertical position", m_legendPosY);
74 legendLayout->addRow("VPos", m_legendPosY);
90 legendLayout->addRow("Width", m_legendWidth);
75 legendLayout->addRow("Width", m_legendWidth);
91 legendLayout->addRow("Height", m_legendHeight);
76 legendLayout->addRow("Height", m_legendHeight);
92 m_legendSettings = new QGroupBox("Detached legend");
77 m_legendSettings = new QGroupBox("Detached legend");
93 m_legendSettings->setLayout(legendLayout);
78 m_legendSettings->setLayout(legendLayout);
94 m_buttonLayout->addWidget(m_legendSettings);
79 m_buttonLayout->addWidget(m_legendSettings);
95 m_legendSettings->setVisible(false);
80 m_legendSettings->setVisible(false);
96
81
97 // Create chart view with the chart
82 // Create chart view with the chart
98 m_chart = new QChart();
83 m_chart = new QChart();
99 m_chartView = new QChartView(m_chart, this);
84 m_chartView = new QChartView(m_chart, this);
100
85
101 // Create spinbox to modify font size
86 // Create spinbox to modify font size
102 m_fontSize = new QDoubleSpinBox();
87 m_fontSize = new QDoubleSpinBox();
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
88 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
89 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
105
90
106 QFormLayout* fontLayout = new QFormLayout();
91 QFormLayout* fontLayout = new QFormLayout();
107 fontLayout->addRow("Legend font size", m_fontSize);
92 fontLayout->addRow("Legend font size", m_fontSize);
108
93
109 // Create layout for grid and detached legend
94 // Create layout for grid and detached legend
110 m_mainLayout = new QGridLayout();
95 m_mainLayout = new QGridLayout();
111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
96 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
112 m_mainLayout->addLayout(fontLayout,1,0);
97 m_mainLayout->addLayout(fontLayout,1,0);
113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
98 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
114 setLayout(m_mainLayout);
99 setLayout(m_mainLayout);
115
100
116 createSeries();
101 createSeries();
117 }
102 }
118
103
119 void MainWidget::createSeries()
104 void MainWidget::createSeries()
120 {
105 {
121 m_series = new QBarSeries();
106 m_series = new QBarSeries();
122 addBarset();
107 addBarset();
123 addBarset();
108 addBarset();
124 addBarset();
109 addBarset();
125 addBarset();
110 addBarset();
126
111
127 m_chart->addSeries(m_series);
112 m_chart->addSeries(m_series);
128 m_chart->setTitle("Legend detach example");
113 m_chart->setTitle("Legend detach example");
129 m_chart->createDefaultAxes();
114 m_chart->createDefaultAxes();
130 //![1]
115 //![1]
131 m_chart->legend()->setVisible(true);
116 m_chart->legend()->setVisible(true);
132 m_chart->legend()->setAlignment(Qt::AlignBottom);
117 m_chart->legend()->setAlignment(Qt::AlignBottom);
133 //![1]
118 //![1]
134
119
135 m_chartView->setRenderHint(QPainter::Antialiasing);
120 m_chartView->setRenderHint(QPainter::Antialiasing);
136 }
121 }
137
122
138 void MainWidget::showLegendSpinbox()
123 void MainWidget::showLegendSpinbox()
139 {
124 {
140 m_legendSettings->setVisible(true);
125 m_legendSettings->setVisible(true);
141 QRectF chartViewRect = m_chartView->rect();
126 QRectF chartViewRect = m_chartView->rect();
142
127
143 m_legendPosX->setMinimum(0);
128 m_legendPosX->setMinimum(0);
144 m_legendPosX->setMaximum(chartViewRect.width());
129 m_legendPosX->setMaximum(chartViewRect.width());
145 m_legendPosX->setValue(150);
130 m_legendPosX->setValue(150);
146
131
147 m_legendPosY->setMinimum(0);
132 m_legendPosY->setMinimum(0);
148 m_legendPosY->setMaximum(chartViewRect.height());
133 m_legendPosY->setMaximum(chartViewRect.height());
149 m_legendPosY->setValue(150);
134 m_legendPosY->setValue(150);
150
135
151 m_legendWidth->setMinimum(0);
136 m_legendWidth->setMinimum(0);
152 m_legendWidth->setMaximum(chartViewRect.width());
137 m_legendWidth->setMaximum(chartViewRect.width());
153 m_legendWidth->setValue(150);
138 m_legendWidth->setValue(150);
154
139
155 m_legendHeight->setMinimum(0);
140 m_legendHeight->setMinimum(0);
156 m_legendHeight->setMaximum(chartViewRect.height());
141 m_legendHeight->setMaximum(chartViewRect.height());
157 m_legendHeight->setValue(75);
142 m_legendHeight->setValue(75);
158 }
143 }
159
144
160 void MainWidget::hideLegendSpinbox()
145 void MainWidget::hideLegendSpinbox()
161 {
146 {
162 m_legendSettings->setVisible(false);
147 m_legendSettings->setVisible(false);
163 }
148 }
164
149
165
150
166 void MainWidget::detachLegend()
151 void MainWidget::toggleAttached()
167 {
152 {
168 //![2]
169 QLegend *legend = m_chart->legend();
153 QLegend *legend = m_chart->legend();
170 legend->detachFromChart();
154 if (legend->isAttachedToChart()) {
171
155 //![2]
172 m_chart->legend()->setBackgroundVisible(true);
156 legend->detachFromChart();
173 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
157 m_chart->legend()->setBackgroundVisible(true);
174 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
158 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
175 //![2]
159 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
176
160 //![2]
177 showLegendSpinbox();
161 showLegendSpinbox();
178 updateLegendLayout();
162 updateLegendLayout();
179 update();
163 } else {
180 }
164 //![3]
181
165 legend->attachToChart();
182
166 legend->setBackgroundVisible(false);
183 void MainWidget::attachLegend()
167 //![3]
184 {
168 hideLegendSpinbox();
185 //![3]
169 }
186 QLegend *legend = m_chart->legend();
187 legend->attachToChart();
188 m_chart->legend()->setBackgroundVisible(false);
189 //![3]
190
191 hideLegendSpinbox();
192 update();
170 update();
193 }
171 }
194
172
195 void MainWidget::addBarset()
173 void MainWidget::addBarset()
196 {
174 {
197 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
175 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
198 qreal delta = m_series->count() * 0.1;
176 qreal delta = m_series->count() * 0.1;
199 *barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta;
177 *barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta;
200 m_series->append(barSet);
178 m_series->append(barSet);
201 }
179 }
202
180
203 void MainWidget::removeBarset()
181 void MainWidget::removeBarset()
204 {
182 {
205 QList<QBarSet*> sets = m_series->barSets();
183 QList<QBarSet*> sets = m_series->barSets();
206 if (sets.count() > 0) {
184 if (sets.count() > 0) {
207 m_series->remove(sets.at(sets.count()-1));
185 m_series->remove(sets.at(sets.count()-1));
208 }
186 }
209 }
187 }
210
188
211 void MainWidget::setLegendLeft()
189 void MainWidget::setLegendAlignment()
212 {
190 {
213 m_chart->legend()->setAlignment(Qt::AlignLeft);
191 QPushButton *button = qobject_cast<QPushButton *>(sender());
214 }
192
215
193 switch (m_chart->legend()->alignment()) {
216 void MainWidget::setLegendRight()
194 case Qt::AlignTop:
217 {
195 m_chart->legend()->setAlignment(Qt::AlignLeft);
218 m_chart->legend()->setAlignment(Qt::AlignRight);
196 if (button)
219 }
197 button->setText("Align (Left)");
220
198 break;
221 void MainWidget::setLegendTop()
199 case Qt::AlignLeft:
222 {
200 m_chart->legend()->setAlignment(Qt::AlignBottom);
223 m_chart->legend()->setAlignment(Qt::AlignTop);
201 if (button)
224 }
202 button->setText("Align (Bottom)");
225
203 break;
226 void MainWidget::setLegendBottom()
204 case Qt::AlignBottom:
227 {
205 m_chart->legend()->setAlignment(Qt::AlignRight);
228 m_chart->legend()->setAlignment(Qt::AlignBottom);
206 if (button)
207 button->setText("Align (Right)");
208 break;
209 default:
210 if (button)
211 button->setText("Align (Top)");
212 m_chart->legend()->setAlignment(Qt::AlignTop);
213 break;
214 }
229 }
215 }
230
216
231 void MainWidget::toggleBold()
217 void MainWidget::toggleBold()
232 {
218 {
233 QFont font = m_chart->legend()->font();
219 QFont font = m_chart->legend()->font();
234 font.setBold(!font.bold());
220 font.setBold(!font.bold());
235 m_chart->legend()->setFont(font);
221 m_chart->legend()->setFont(font);
236 }
222 }
237
223
238 void MainWidget::toggleItalic()
224 void MainWidget::toggleItalic()
239 {
225 {
240 QFont font = m_chart->legend()->font();
226 QFont font = m_chart->legend()->font();
241 font.setItalic(!font.italic());
227 font.setItalic(!font.italic());
242 m_chart->legend()->setFont(font);
228 m_chart->legend()->setFont(font);
243 }
229 }
244
230
245 void MainWidget::fontSizeChanged()
231 void MainWidget::fontSizeChanged()
246 {
232 {
247 QFont font = m_chart->legend()->font();
233 QFont font = m_chart->legend()->font();
248 font.setPointSizeF(m_fontSize->value());
234 font.setPointSizeF(m_fontSize->value());
249 m_chart->legend()->setFont(font);
235 m_chart->legend()->setFont(font);
250 }
236 }
251
237
252 void MainWidget::updateLegendLayout()
238 void MainWidget::updateLegendLayout()
253 {
239 {
254 //![4]
240 //![4]
255 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
241 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
256 ,m_legendPosY->value()
242 ,m_legendPosY->value()
257 ,m_legendWidth->value()
243 ,m_legendWidth->value()
258 ,m_legendHeight->value()));
244 ,m_legendHeight->value()));
259 m_chart->legend()->update();
245 m_chart->legend()->update();
260 //![4]
246 //![4]
261 }
247 }
@@ -1,86 +1,82
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef MAINWIDGET_H
21 #ifndef MAINWIDGET_H
22 #define MAINWIDGET_H
22 #define MAINWIDGET_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "qchart.h"
25 #include "qchart.h"
26 #include "qchartview.h"
26 #include "qchartview.h"
27 #include <QWidget>
27 #include <QWidget>
28 #include <QGraphicsWidget>
28 #include <QGraphicsWidget>
29 #include <QGridLayout>
29 #include <QGridLayout>
30 #include <QGraphicsGridLayout>
30 #include <QGraphicsGridLayout>
31 #include <QDoubleSpinBox>
31 #include <QDoubleSpinBox>
32 #include <QGroupBox>
32 #include <QGroupBox>
33 #include <QBarSeries>
33 #include <QBarSeries>
34
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
36
37 class MainWidget : public QWidget
37 class MainWidget : public QWidget
38 {
38 {
39 Q_OBJECT
39 Q_OBJECT
40 public:
40 public:
41 explicit MainWidget(QWidget *parent = 0);
41 explicit MainWidget(QWidget *parent = 0);
42 void createSeries();
42 void createSeries();
43 void showLegendSpinbox();
43 void showLegendSpinbox();
44 void hideLegendSpinbox();
44 void hideLegendSpinbox();
45
45
46 signals:
46 signals:
47
47
48 public slots:
48 public slots:
49 void detachLegend();
49 void toggleAttached();
50 void attachLegend();
51 void addBarset();
50 void addBarset();
52 void removeBarset();
51 void removeBarset();
53
52
54 void setLegendLeft();
53 void setLegendAlignment();
55 void setLegendRight();
56 void setLegendTop();
57 void setLegendBottom();
58
54
59 void toggleBold();
55 void toggleBold();
60 void toggleItalic();
56 void toggleItalic();
61 void fontSizeChanged();
57 void fontSizeChanged();
62
58
63 void updateLegendLayout();
59 void updateLegendLayout();
64
60
65
61
66 private:
62 private:
67
63
68 QChart *m_chart;
64 QChart *m_chart;
69 QBarSeries *m_series;
65 QBarSeries *m_series;
70
66
71 QChartView *m_chartView;
67 QChartView *m_chartView;
72 QGridLayout *m_mainLayout;
68 QGridLayout *m_mainLayout;
73 QGridLayout *m_buttonLayout;
69 QGridLayout *m_buttonLayout;
74 QGridLayout *m_fontLayout;
70 QGridLayout *m_fontLayout;
75
71
76 QDoubleSpinBox *m_fontSize;
72 QDoubleSpinBox *m_fontSize;
77
73
78 // For detached layout
74 // For detached layout
79 QGroupBox* m_legendSettings;
75 QGroupBox* m_legendSettings;
80 QDoubleSpinBox *m_legendPosX;
76 QDoubleSpinBox *m_legendPosX;
81 QDoubleSpinBox *m_legendPosY;
77 QDoubleSpinBox *m_legendPosY;
82 QDoubleSpinBox *m_legendWidth;
78 QDoubleSpinBox *m_legendWidth;
83 QDoubleSpinBox *m_legendHeight;
79 QDoubleSpinBox *m_legendHeight;
84 };
80 };
85
81
86 #endif // MAINWIDGET_H
82 #endif // MAINWIDGET_H
@@ -1,541 +1,541
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26 #include "legendlayout_p.h"
26 #include "legendlayout_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "qxyseries.h"
28 #include "qxyseries.h"
29 #include "qlineseries.h"
29 #include "qlineseries.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qscatterseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
32 #include "qsplineseries.h"
33 #include "qabstractbarseries.h"
33 #include "qabstractbarseries.h"
34 #include "qstackedbarseries.h"
34 #include "qstackedbarseries.h"
35 #include "qpercentbarseries.h"
35 #include "qpercentbarseries.h"
36 #include "qbarset.h"
36 #include "qbarset.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieseries_p.h"
38 #include "qpieseries_p.h"
39 #include "qpieslice.h"
39 #include "qpieslice.h"
40 #include "chartpresenter_p.h"
40 #include "chartpresenter_p.h"
41 #include <QPainter>
41 #include <QPainter>
42 #include <QPen>
42 #include <QPen>
43 #include <QTimer>
43 #include <QTimer>
44 #include <QGraphicsLayout>
44 #include <QGraphicsLayout>
45 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
46
46
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48
48
49 /*!
49 /*!
50 \class QLegend
50 \class QLegend
51 \brief Legend object
51 \brief Legend object
52 \mainclass
52 \mainclass
53
53
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
56 handle the drawing manually.
56 handle the drawing manually.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
58
58
59 \image examples_percentbarchart_legend.png
59 \image examples_percentbarchart_legend.png
60
60
61 \sa QChart
61 \sa QChart
62 */
62 */
63 /*!
63 /*!
64 \qmlclass Legend QLegend
64 \qmlclass Legend QLegend
65 \brief Legend is part of QtCommercial Chart QML API.
65 \brief Legend is part of QtCommercial Chart QML API.
66
66
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
68 series have been changed. Legend is used via ChartView class. For example:
68 series have been changed. Legend is used via ChartView class. For example:
69 \code
69 \code
70 ChartView {
70 ChartView {
71 legend.visible: true
71 legend.visible: true
72 legend.alignment: Qt.AlignBottom
72 legend.alignment: Qt.AlignBottom
73 // Add a few series...
73 // Add a few series...
74 }
74 }
75 \endcode
75 \endcode
76
76
77 \image examples_percentbarchart_legend.png
77 \image examples_percentbarchart_legend.png
78 */
78 */
79
79
80 /*!
80 /*!
81 \property QLegend::alignment
81 \property QLegend::alignment
82 \brief The alignment of the legend.
82 \brief The alignment of the legend.
83
83
84 Legend paints on the defined position in the chart. The following alignments are supported:
84 Legend paints on the defined position in the chart. The following alignments are supported:
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
86 */
86 */
87 /*!
87 /*!
88 \qmlproperty Qt.Alignment Legend::alignment
88 \qmlproperty Qt.Alignment Legend::alignment
89 \brief The alignment of the legend.
89 \brief The alignment of the legend.
90
90
91 Legend paints on the defined position in the chart. The following alignments are supported:
91 Legend paints on the defined position in the chart. The following alignments are supported:
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
93 */
93 */
94
94
95 /*!
95 /*!
96 \property QLegend::backgroundVisible
96 \property QLegend::backgroundVisible
97 Whether the legend background is visible or not.
97 Whether the legend background is visible or not.
98 */
98 */
99 /*!
99 /*!
100 \qmlproperty bool Legend::backgroundVisible
100 \qmlproperty bool Legend::backgroundVisible
101 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
102 */
102 */
103
103
104 /*!
104 /*!
105 \property QLegend::color
105 \property QLegend::color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
108 */
108 */
109 /*!
109 /*!
110 \qmlproperty color Legend::color
110 \qmlproperty color Legend::color
111 The color of the legend, i.e. the background (brush) color.
111 The color of the legend, i.e. the background (brush) color.
112 */
112 */
113
113
114 /*!
114 /*!
115 \property QLegend::borderColor
115 \property QLegend::borderColor
116 The border color of the legend, i.e. the line color.
116 The border color of the legend, i.e. the line color.
117 */
117 */
118 /*!
118 /*!
119 \qmlproperty color Legend::borderColor
119 \qmlproperty color Legend::borderColor
120 The border color of the legend, i.e. the line color.
120 The border color of the legend, i.e. the line color.
121 */
121 */
122
122
123 /*!
123 /*!
124 \property QLegend::font
124 \property QLegend::font
125 The font of markers used by legend
125 The font of markers used by legend
126 */
126 */
127 /*!
127 /*!
128 \qmlproperty color Legend::font
128 \qmlproperty color Legend::font
129 The font of markers used by legend
129 The font of markers used by legend
130 */
130 */
131
131
132 /*!
132 /*!
133 \property QLegend::labelColor
133 \property QLegend::labelColor
134 The color of brush used to draw labels.
134 The color of brush used to draw labels.
135 */
135 */
136 /*!
136 /*!
137 \qmlproperty color QLegend::labelColor
137 \qmlproperty color QLegend::labelColor
138 The color of brush used to draw labels.
138 The color of brush used to draw labels.
139 */
139 */
140
140
141 /*!
141 /*!
142 \fn void QLegend::backgroundVisibleChanged(bool)
142 \fn void QLegend::backgroundVisibleChanged(bool)
143 The visibility of the legend background changed to \a visible.
143 The visibility of the legend background changed to \a visible.
144 */
144 */
145
145
146 /*!
146 /*!
147 \fn void QLegend::colorChanged(QColor)
147 \fn void QLegend::colorChanged(QColor)
148 The color of the legend background changed to \a color.
148 The color of the legend background changed to \a color.
149 */
149 */
150
150
151 /*!
151 /*!
152 \fn void QLegend::borderColorChanged(QColor)
152 \fn void QLegend::borderColorChanged(QColor)
153 The border color of the legend background changed to \a color.
153 The border color of the legend background changed to \a color.
154 */
154 */
155
155
156 /*!
156 /*!
157 \fn void QLegend::fontChanged(QFont)
157 \fn void QLegend::fontChanged(QFont)
158 The font of markers of the legend changed to \a font.
158 The font of markers of the legend changed to \a font.
159 */
159 */
160
160
161 /*!
161 /*!
162 \fn void QLegend::labelColorChanged(QColor color)
162 \fn void QLegend::labelColorChanged(QColor color)
163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
164 */
164 */
165
165
166 /*!
166 /*!
167 Constructs the legend object and sets the parent to \a parent
167 Constructs the legend object and sets the parent to \a parent
168 */
168 */
169
169
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
172 {
172 {
173 setZValue(ChartPresenter::LegendZValue);
173 setZValue(ChartPresenter::LegendZValue);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
177 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
177 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
178 setLayout(d_ptr->m_layout);
178 setLayout(d_ptr->m_layout);
179 setVisible(false);
179 setVisible(false);
180 }
180 }
181
181
182 /*!
182 /*!
183 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
183 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
184 */
184 */
185 QLegend::~QLegend()
185 QLegend::~QLegend()
186 {
186 {
187 }
187 }
188
188
189 /*!
189 /*!
190 \internal
190 \internal
191 */
191 */
192 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
192 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
193 {
193 {
194 Q_UNUSED(option)
194 Q_UNUSED(option)
195 Q_UNUSED(widget)
195 Q_UNUSED(widget)
196
196
197 if(!d_ptr->m_backgroundVisible) return;
197 if(!d_ptr->m_backgroundVisible) return;
198
198
199 painter->setOpacity(opacity());
199 painter->setOpacity(opacity());
200 painter->setPen(d_ptr->m_pen);
200 painter->setPen(d_ptr->m_pen);
201 painter->setBrush(d_ptr->m_brush);
201 painter->setBrush(d_ptr->m_brush);
202 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
202 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
203
203
204 }
204 }
205
205
206
206
207 /*!
207 /*!
208 Sets the \a brush of legend. Brush affects the background of legend.
208 Sets the \a brush of legend. Brush affects the background of legend.
209 */
209 */
210 void QLegend::setBrush(const QBrush &brush)
210 void QLegend::setBrush(const QBrush &brush)
211 {
211 {
212 if (d_ptr->m_brush != brush) {
212 if (d_ptr->m_brush != brush) {
213 d_ptr->m_brush = brush;
213 d_ptr->m_brush = brush;
214 update();
214 update();
215 emit colorChanged(brush.color());
215 emit colorChanged(brush.color());
216 }
216 }
217 }
217 }
218
218
219 /*!
219 /*!
220 Returns the brush used by legend.
220 Returns the brush used by legend.
221 */
221 */
222 QBrush QLegend::brush() const
222 QBrush QLegend::brush() const
223 {
223 {
224 return d_ptr->m_brush;
224 return d_ptr->m_brush;
225 }
225 }
226
226
227 void QLegend::setColor(QColor color)
227 void QLegend::setColor(QColor color)
228 {
228 {
229 QBrush b = d_ptr->m_brush;
229 QBrush b = d_ptr->m_brush;
230 if (b.style() != Qt::SolidPattern || b.color() != color) {
230 if (b.style() != Qt::SolidPattern || b.color() != color) {
231 b.setStyle(Qt::SolidPattern);
231 b.setStyle(Qt::SolidPattern);
232 b.setColor(color);
232 b.setColor(color);
233 setBrush(b);
233 setBrush(b);
234 }
234 }
235 }
235 }
236
236
237 QColor QLegend::color()
237 QColor QLegend::color()
238 {
238 {
239 return d_ptr->m_brush.color();
239 return d_ptr->m_brush.color();
240 }
240 }
241
241
242 /*!
242 /*!
243 Sets the \a pen of legend. Pen affects the legend borders.
243 Sets the \a pen of legend. Pen affects the legend borders.
244 */
244 */
245 void QLegend::setPen(const QPen &pen)
245 void QLegend::setPen(const QPen &pen)
246 {
246 {
247 if (d_ptr->m_pen != pen) {
247 if (d_ptr->m_pen != pen) {
248 d_ptr->m_pen = pen;
248 d_ptr->m_pen = pen;
249 update();
249 update();
250 emit borderColorChanged(pen.color());
250 emit borderColorChanged(pen.color());
251 }
251 }
252 }
252 }
253
253
254 /*!
254 /*!
255 Returns the pen used by legend
255 Returns the pen used by legend
256 */
256 */
257
257
258 QPen QLegend::pen() const
258 QPen QLegend::pen() const
259 {
259 {
260 return d_ptr->m_pen;
260 return d_ptr->m_pen;
261 }
261 }
262
262
263 void QLegend::setFont(const QFont &font)
263 void QLegend::setFont(const QFont &font)
264 {
264 {
265 if (d_ptr->m_font != font) {
265 if (d_ptr->m_font != font) {
266 d_ptr->m_font = font;
266 d_ptr->m_font = font;
267
267
268 foreach (LegendMarker *marker, d_ptr->markers()) {
268 foreach (LegendMarker *marker, d_ptr->markers()) {
269 marker->setFont(d_ptr->m_font);
269 marker->setFont(d_ptr->m_font);
270 }
270 }
271 layout()->invalidate();
271 layout()->invalidate();
272 emit fontChanged(font);
272 emit fontChanged(font);
273 }
273 }
274 }
274 }
275
275
276 QFont QLegend::font() const
276 QFont QLegend::font() const
277 {
277 {
278 return d_ptr->m_font;
278 return d_ptr->m_font;
279 }
279 }
280
280
281 void QLegend::setBorderColor(QColor color)
281 void QLegend::setBorderColor(QColor color)
282 {
282 {
283 QPen p = d_ptr->m_pen;
283 QPen p = d_ptr->m_pen;
284 if (p.color() != color) {
284 if (p.color() != color) {
285 p.setColor(color);
285 p.setColor(color);
286 setPen(p);
286 setPen(p);
287 }
287 }
288 }
288 }
289
289
290 QColor QLegend::borderColor()
290 QColor QLegend::borderColor()
291 {
291 {
292 return d_ptr->m_pen.color();
292 return d_ptr->m_pen.color();
293 }
293 }
294
294
295 /*!
295 /*!
296 Set brush used to draw labels to \a brush.
296 Set brush used to draw labels to \a brush.
297 */
297 */
298 void QLegend::setLabelBrush(const QBrush &brush)
298 void QLegend::setLabelBrush(const QBrush &brush)
299 {
299 {
300 if (d_ptr->m_labelBrush != brush) {
300 if (d_ptr->m_labelBrush != brush) {
301 d_ptr->m_labelBrush = brush;
301 d_ptr->m_labelBrush = brush;
302 foreach (LegendMarker *marker, d_ptr->markers()) {
302 foreach (LegendMarker *marker, d_ptr->markers()) {
303 marker->setLabelBrush(d_ptr->m_labelBrush);
303 marker->setLabelBrush(d_ptr->m_labelBrush);
304 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
304 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
305 // instead of mapping it from label brush color
305 // instead of mapping it from label brush color
306 marker->setPen(brush.color());
306 marker->setPen(brush.color());
307 }
307 }
308 emit labelColorChanged(brush.color());
308 emit labelColorChanged(brush.color());
309 }
309 }
310 }
310 }
311
311
312 /*!
312 /*!
313 Brush used to draw labels.
313 Brush used to draw labels.
314 */
314 */
315 QBrush QLegend::labelBrush() const
315 QBrush QLegend::labelBrush() const
316 {
316 {
317 return d_ptr->m_labelBrush;
317 return d_ptr->m_labelBrush;
318 }
318 }
319
319
320 void QLegend::setLabelColor(QColor color)
320 void QLegend::setLabelColor(QColor color)
321 {
321 {
322 QBrush b = d_ptr->m_labelBrush;
322 QBrush b = d_ptr->m_labelBrush;
323 if (b.style() != Qt::SolidPattern || b.color() != color) {
323 if (b.style() != Qt::SolidPattern || b.color() != color) {
324 b.setStyle(Qt::SolidPattern);
324 b.setStyle(Qt::SolidPattern);
325 b.setColor(color);
325 b.setColor(color);
326 setLabelBrush(b);
326 setLabelBrush(b);
327 }
327 }
328 }
328 }
329
329
330 QColor QLegend::labelColor() const
330 QColor QLegend::labelColor() const
331 {
331 {
332 return d_ptr->m_labelBrush.color();
332 return d_ptr->m_labelBrush.color();
333 }
333 }
334
334
335
335
336 void QLegend::setAlignment(Qt::Alignment alignment)
336 void QLegend::setAlignment(Qt::Alignment alignment)
337 {
337 {
338 if(d_ptr->m_alignment!=alignment) {
338 if(d_ptr->m_alignment!=alignment) {
339 d_ptr->m_alignment = alignment;
339 d_ptr->m_alignment = alignment;
340 updateGeometry();
340 updateGeometry();
341 if(isAttachedToChart()){
341 if(isAttachedToChart()){
342 d_ptr->m_presenter->layout()->invalidate();
342 d_ptr->m_presenter->layout()->invalidate();
343 }else{
343 }else{
344 layout()->invalidate();
344 layout()->invalidate();
345 }
345 }
346 }
346 }
347 }
347 }
348
348
349 Qt::Alignment QLegend::alignment() const
349 Qt::Alignment QLegend::alignment() const
350 {
350 {
351 return d_ptr->m_alignment;
351 return d_ptr->m_alignment;
352 }
352 }
353
353
354 /*!
354 /*!
355 Detaches the legend from chart. Chart won't change layout of the legend.
355 Detaches the legend from chart. Chart won't change layout of the legend.
356 */
356 */
357 void QLegend::detachFromChart()
357 void QLegend::detachFromChart()
358 {
358 {
359 d_ptr->m_attachedToChart = false;
359 d_ptr->m_attachedToChart = false;
360 d_ptr->m_layout->invalidate();
360 d_ptr->m_layout->invalidate();
361 setParent(0);
361 setParent(0);
362
362
363 }
363 }
364
364
365 /*!
365 /*!
366 Attaches the legend to chart. Chart may change layout of the legend.
366 Attaches the legend to chart. Chart may change layout of the legend.
367 */
367 */
368 void QLegend::attachToChart()
368 void QLegend::attachToChart()
369 {
369 {
370 d_ptr->m_attachedToChart = true;
370 d_ptr->m_attachedToChart = true;
371 d_ptr->m_layout->invalidate();
371 d_ptr->m_presenter->layout()->invalidate();
372 setParent(d_ptr->m_chart);
372 setParent(d_ptr->m_chart);
373 }
373 }
374
374
375 /*!
375 /*!
376 Returns true, if legend is attached to chart.
376 Returns true, if legend is attached to chart.
377 */
377 */
378 bool QLegend::isAttachedToChart()
378 bool QLegend::isAttachedToChart()
379 {
379 {
380 return d_ptr->m_attachedToChart;
380 return d_ptr->m_attachedToChart;
381 }
381 }
382
382
383 /*!
383 /*!
384 Sets the visibility of legend background to \a visible
384 Sets the visibility of legend background to \a visible
385 */
385 */
386 void QLegend::setBackgroundVisible(bool visible)
386 void QLegend::setBackgroundVisible(bool visible)
387 {
387 {
388 if(d_ptr->m_backgroundVisible != visible) {
388 if(d_ptr->m_backgroundVisible != visible) {
389 d_ptr->m_backgroundVisible = visible;
389 d_ptr->m_backgroundVisible = visible;
390 update();
390 update();
391 emit backgroundVisibleChanged(visible);
391 emit backgroundVisibleChanged(visible);
392 }
392 }
393 }
393 }
394
394
395 /*!
395 /*!
396 Returns the visibility of legend background
396 Returns the visibility of legend background
397 */
397 */
398 bool QLegend::isBackgroundVisible() const
398 bool QLegend::isBackgroundVisible() const
399 {
399 {
400 return d_ptr->m_backgroundVisible;
400 return d_ptr->m_backgroundVisible;
401 }
401 }
402
402
403 /*!
403 /*!
404 \internal \a event see QGraphicsWidget for details
404 \internal \a event see QGraphicsWidget for details
405 */
405 */
406 void QLegend::hideEvent(QHideEvent *event)
406 void QLegend::hideEvent(QHideEvent *event)
407 {
407 {
408 d_ptr->m_presenter->layout()->invalidate();
408 d_ptr->m_presenter->layout()->invalidate();
409 QGraphicsWidget::hideEvent(event);
409 QGraphicsWidget::hideEvent(event);
410 }
410 }
411
411
412 /*!
412 /*!
413 \internal \a event see QGraphicsWidget for details
413 \internal \a event see QGraphicsWidget for details
414 */
414 */
415 void QLegend::showEvent(QShowEvent *event)
415 void QLegend::showEvent(QShowEvent *event)
416 {
416 {
417 QGraphicsWidget::showEvent(event);
417 QGraphicsWidget::showEvent(event);
418 d_ptr->m_presenter->layout()->invalidate();
418 d_ptr->m_presenter->layout()->invalidate();
419 d_ptr->items()->setVisible(false);
419 d_ptr->items()->setVisible(false);
420 //layout activation will show the items
420 //layout activation will show the items
421 }
421 }
422
422
423 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
423 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
424
424
425 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
425 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
426 q_ptr(q),
426 q_ptr(q),
427 m_presenter(presenter),
427 m_presenter(presenter),
428 m_layout(new LegendLayout(q)),
428 m_layout(new LegendLayout(q)),
429 m_chart(chart),
429 m_chart(chart),
430 m_items(new QGraphicsItemGroup(q)),
430 m_items(new QGraphicsItemGroup(q)),
431 m_alignment(Qt::AlignTop),
431 m_alignment(Qt::AlignTop),
432 m_brush(QBrush()),
432 m_brush(QBrush()),
433 m_pen(QPen()),
433 m_pen(QPen()),
434 m_labelBrush(QBrush()),
434 m_labelBrush(QBrush()),
435 m_diameter(5),
435 m_diameter(5),
436 m_attachedToChart(true),
436 m_attachedToChart(true),
437 m_backgroundVisible(false)
437 m_backgroundVisible(false)
438 {
438 {
439
439
440 }
440 }
441
441
442 QLegendPrivate::~QLegendPrivate()
442 QLegendPrivate::~QLegendPrivate()
443 {
443 {
444
444
445 }
445 }
446
446
447 void QLegendPrivate::setOffset(qreal x, qreal y)
447 void QLegendPrivate::setOffset(qreal x, qreal y)
448 {
448 {
449 m_layout->setOffset(x,y);
449 m_layout->setOffset(x,y);
450 }
450 }
451
451
452 QPointF QLegendPrivate::offset() const
452 QPointF QLegendPrivate::offset() const
453 {
453 {
454 return m_layout->offset();
454 return m_layout->offset();
455 }
455 }
456
456
457 int QLegendPrivate::roundness(qreal size)
457 int QLegendPrivate::roundness(qreal size)
458 {
458 {
459 return 100*m_diameter/int(size);
459 return 100*m_diameter/int(size);
460 }
460 }
461
461
462 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
462 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
463 {
463 {
464 Q_UNUSED(domain)
464 Q_UNUSED(domain)
465
465
466 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
466 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
467
467
468 foreach(LegendMarker* marker, markers) {
468 foreach(LegendMarker* marker, markers) {
469 marker->setFont(m_font);
469 marker->setFont(m_font);
470 marker->setLabelBrush(m_labelBrush);
470 marker->setLabelBrush(m_labelBrush);
471 marker->setVisible(series->isVisible());
471 marker->setVisible(series->isVisible());
472 m_items->addToGroup(marker);
472 m_items->addToGroup(marker);
473 m_markers<<marker;
473 m_markers<<marker;
474 }
474 }
475
475
476 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
476 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
477
477
478 if(series->type() == QAbstractSeries::SeriesTypePie) {
478 if(series->type() == QAbstractSeries::SeriesTypePie) {
479 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
479 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
480 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
480 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
481 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
481 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
482 }
482 }
483
483
484 q_ptr->layout()->invalidate();
484 q_ptr->layout()->invalidate();
485 q_ptr->layout()->activate();
485 q_ptr->layout()->activate();
486 }
486 }
487
487
488 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
488 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
489 {
489 {
490 foreach (LegendMarker *marker, m_markers) {
490 foreach (LegendMarker *marker, m_markers) {
491 if (marker->series() == series) {
491 if (marker->series() == series) {
492 delete marker;
492 delete marker;
493 m_markers.removeAll(marker);
493 m_markers.removeAll(marker);
494 }
494 }
495 }
495 }
496
496
497 if(series->type() == QAbstractSeries::SeriesTypePie)
497 if(series->type() == QAbstractSeries::SeriesTypePie)
498 {
498 {
499 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
499 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
500 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
500 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
501 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
501 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
502 }
502 }
503
503
504 q_ptr->layout()->invalidate();
504 q_ptr->layout()->invalidate();
505 }
505 }
506
506
507 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
507 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
508 {
508 {
509 // TODO: find out which markers are are added or removed. Update them
509 // TODO: find out which markers are are added or removed. Update them
510 // TODO: better implementation
510 // TODO: better implementation
511 handleSeriesRemoved(series);
511 handleSeriesRemoved(series);
512 Domain domain;
512 Domain domain;
513 handleSeriesAdded(series, &domain);
513 handleSeriesAdded(series, &domain);
514 }
514 }
515
515
516 void QLegendPrivate::handleUpdatePieSeries()
516 void QLegendPrivate::handleUpdatePieSeries()
517 {
517 {
518 //TODO: reimplement to be optimal
518 //TODO: reimplement to be optimal
519 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
519 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
520 Q_ASSERT(series);
520 Q_ASSERT(series);
521 handleSeriesRemoved(series);
521 handleSeriesRemoved(series);
522 handleSeriesAdded(series, 0);
522 handleSeriesAdded(series, 0);
523 }
523 }
524
524
525 void QLegendPrivate::handleSeriesVisibleChanged()
525 void QLegendPrivate::handleSeriesVisibleChanged()
526 {
526 {
527 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
527 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
528
528
529 foreach (LegendMarker* marker, m_markers) {
529 foreach (LegendMarker* marker, m_markers) {
530 if (marker->series() == series) {
530 if (marker->series() == series) {
531 marker->setVisible(series->isVisible());
531 marker->setVisible(series->isVisible());
532 }
532 }
533 }
533 }
534
534
535 q_ptr->layout()->invalidate();
535 q_ptr->layout()->invalidate();
536 }
536 }
537
537
538 #include "moc_qlegend.cpp"
538 #include "moc_qlegend.cpp"
539 #include "moc_qlegend_p.cpp"
539 #include "moc_qlegend_p.cpp"
540
540
541 QTCOMMERCIALCHART_END_NAMESPACE
541 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now