##// END OF EJS Templates
legend font fix
sauimone -
r1522:7ac28ceb0acb
parent child
Show More
@@ -1,224 +1,262
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("detach legend");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
45
45
46 QPushButton *addSetButton = new QPushButton("add barset");
46 QPushButton *addSetButton = new QPushButton("add barset");
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
52
52
53 QPushButton *leftButton = new QPushButton("Align legend left");
53 QPushButton *leftButton = new QPushButton("Align legend left");
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
55 m_buttonLayout->addWidget(leftButton, 4, 0);
55 m_buttonLayout->addWidget(leftButton, 4, 0);
56
56
57 QPushButton *rightButton = new QPushButton("Align legend right");
57 QPushButton *rightButton = new QPushButton("Align legend right");
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
59 m_buttonLayout->addWidget(rightButton, 5, 0);
59 m_buttonLayout->addWidget(rightButton, 5, 0);
60
60
61 QPushButton *topButton = new QPushButton("Align legend top");
61 QPushButton *topButton = new QPushButton("Align legend top");
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
63 m_buttonLayout->addWidget(topButton, 6, 0);
63 m_buttonLayout->addWidget(topButton, 6, 0);
64
64
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
68
68
69 QPushButton *boldButton = new QPushButton("Toggle bold");
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
71 m_buttonLayout->addWidget(boldButton, 8, 0);
72
73 QPushButton *italicButton = new QPushButton("Toggle italic");
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
75 m_buttonLayout->addWidget(italicButton, 9, 0);
76
69 m_legendPosX = new QDoubleSpinBox();
77 m_legendPosX = new QDoubleSpinBox();
70 m_legendPosY = new QDoubleSpinBox();
78 m_legendPosY = new QDoubleSpinBox();
71 m_legendWidth = new QDoubleSpinBox();
79 m_legendWidth = new QDoubleSpinBox();
72 m_legendHeight = new QDoubleSpinBox();
80 m_legendHeight = new QDoubleSpinBox();
73
81
74 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
75 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
76 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
77 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
78
86
79 QFormLayout* legendLayout = new QFormLayout();
87 QFormLayout* legendLayout = new QFormLayout();
80 legendLayout->addRow("Horizontal position", m_legendPosX);
88 legendLayout->addRow("Horizontal position", m_legendPosX);
81 legendLayout->addRow("Vertical position", m_legendPosY);
89 legendLayout->addRow("Vertical position", m_legendPosY);
82 legendLayout->addRow("Width", m_legendWidth);
90 legendLayout->addRow("Width", m_legendWidth);
83 legendLayout->addRow("Height", m_legendHeight);
91 legendLayout->addRow("Height", m_legendHeight);
84 m_legendSettings = new QGroupBox("Detached legend");
92 m_legendSettings = new QGroupBox("Detached legend");
85 m_legendSettings->setLayout(legendLayout);
93 m_legendSettings->setLayout(legendLayout);
86 m_buttonLayout->addWidget(m_legendSettings);
94 m_buttonLayout->addWidget(m_legendSettings);
87 m_legendSettings->setVisible(false);
95 m_legendSettings->setVisible(false);
88
96
89 // Create chart view with the chart
97 // Create chart view with the chart
90 m_chart = new QChart();
98 m_chart = new QChart();
91 m_chartView = new QChartView(m_chart, this);
99 m_chartView = new QChartView(m_chart, this);
92
100
101 // Create spinbox to modify font size
102 m_fontSize = new QDoubleSpinBox();
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
105
106 QFormLayout* fontLayout = new QFormLayout();
107 fontLayout->addRow("Legend font size", m_fontSize);
108
93 // Create layout for grid and detached legend
109 // Create layout for grid and detached legend
94 m_mainLayout = new QGridLayout();
110 m_mainLayout = new QGridLayout();
95 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
112 m_mainLayout->addLayout(fontLayout,1,0);
96 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
97 setLayout(m_mainLayout);
114 setLayout(m_mainLayout);
98
115
99 createSeries();
116 createSeries();
100 }
117 }
101
118
102 void MainWidget::createSeries()
119 void MainWidget::createSeries()
103 {
120 {
104 m_series = new QBarSeries();
121 m_series = new QBarSeries();
105 addBarset();
122 addBarset();
106 addBarset();
123 addBarset();
107 addBarset();
124 addBarset();
108 addBarset();
125 addBarset();
109
126
110 m_chart->addSeries(m_series);
127 m_chart->addSeries(m_series);
111 m_chart->setTitle("Legend detach example");
128 m_chart->setTitle("Legend detach example");
112 //![1]
129 //![1]
113 m_chart->legend()->setVisible(true);
130 m_chart->legend()->setVisible(true);
114 m_chart->legend()->setAlignment(Qt::AlignBottom);
131 m_chart->legend()->setAlignment(Qt::AlignBottom);
115 //![1]
132 //![1]
116
133
117 m_chart->axisY()->setNiceNumbersEnabled(true);
134 m_chart->axisY()->setNiceNumbersEnabled(true);
118 m_chartView->setRenderHint(QPainter::Antialiasing);
135 m_chartView->setRenderHint(QPainter::Antialiasing);
119 }
136 }
120
137
121 void MainWidget::showLegendSpinbox()
138 void MainWidget::showLegendSpinbox()
122 {
139 {
123 m_legendSettings->setVisible(true);
140 m_legendSettings->setVisible(true);
124 QRectF chartViewRect = m_chartView->rect();
141 QRectF chartViewRect = m_chartView->rect();
125 QRectF legendRect = m_chart->legend()->boundingRect();
142 QRectF legendRect = m_chart->legend()->boundingRect();
126
143
127 m_legendPosX->setMinimum(0);
144 m_legendPosX->setMinimum(0);
128 m_legendPosX->setMaximum(chartViewRect.width());
145 m_legendPosX->setMaximum(chartViewRect.width());
129 m_legendPosX->setValue(150);
146 m_legendPosX->setValue(150);
130
147
131 m_legendPosY->setMinimum(0);
148 m_legendPosY->setMinimum(0);
132 m_legendPosY->setMaximum(chartViewRect.height());
149 m_legendPosY->setMaximum(chartViewRect.height());
133 m_legendPosY->setValue(150);
150 m_legendPosY->setValue(150);
134
151
135 m_legendWidth->setMinimum(0);
152 m_legendWidth->setMinimum(0);
136 m_legendWidth->setMaximum(chartViewRect.width());
153 m_legendWidth->setMaximum(chartViewRect.width());
137 m_legendWidth->setValue(150);
154 m_legendWidth->setValue(150);
138
155
139 m_legendHeight->setMinimum(0);
156 m_legendHeight->setMinimum(0);
140 m_legendHeight->setMaximum(chartViewRect.height());
157 m_legendHeight->setMaximum(chartViewRect.height());
141 m_legendHeight->setValue(75);
158 m_legendHeight->setValue(75);
142 }
159 }
143
160
144 void MainWidget::hideLegendSpinbox()
161 void MainWidget::hideLegendSpinbox()
145 {
162 {
146 m_legendSettings->setVisible(false);
163 m_legendSettings->setVisible(false);
147 }
164 }
148
165
149
166
150 void MainWidget::detachLegend()
167 void MainWidget::detachLegend()
151 {
168 {
152 //![2]
169 //![2]
153 QLegend *legend = m_chart->legend();
170 QLegend *legend = m_chart->legend();
154 legend->detachFromChart();
171 legend->detachFromChart();
155
172
156 m_chart->legend()->setBackgroundVisible(true);
173 m_chart->legend()->setBackgroundVisible(true);
157 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
174 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
158 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
175 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
159 //![2]
176 //![2]
160
177
161 showLegendSpinbox();
178 showLegendSpinbox();
162 updateLegendLayout();
179 updateLegendLayout();
163 update();
180 update();
164 }
181 }
165
182
166
183
167 void MainWidget::attachLegend()
184 void MainWidget::attachLegend()
168 {
185 {
169 //![3]
186 //![3]
170 QLegend *legend = m_chart->legend();
187 QLegend *legend = m_chart->legend();
171 legend->attachToChart();
188 legend->attachToChart();
172 m_chart->legend()->setBackgroundVisible(false);
189 m_chart->legend()->setBackgroundVisible(false);
173 //![3]
190 //![3]
174
191
175 hideLegendSpinbox();
192 hideLegendSpinbox();
176 update();
193 update();
177 }
194 }
178
195
179 void MainWidget::addBarset()
196 void MainWidget::addBarset()
180 {
197 {
181 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
198 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
182 qreal delta = m_series->count() * 0.1;
199 qreal delta = m_series->count() * 0.1;
183 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
200 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
184 m_series->append(barSet);
201 m_series->append(barSet);
185 }
202 }
186
203
187 void MainWidget::removeBarset()
204 void MainWidget::removeBarset()
188 {
205 {
189 QList<QBarSet*> sets = m_series->barSets();
206 QList<QBarSet*> sets = m_series->barSets();
190 if (sets.count() > 0) {
207 if (sets.count() > 0) {
191 m_series->remove(sets.at(sets.count()-1));
208 m_series->remove(sets.at(sets.count()-1));
192 }
209 }
193 }
210 }
194
211
195 void MainWidget::setLegendLeft()
212 void MainWidget::setLegendLeft()
196 {
213 {
197 m_chart->legend()->setAlignment(Qt::AlignLeft);
214 m_chart->legend()->setAlignment(Qt::AlignLeft);
198 }
215 }
199
216
200 void MainWidget::setLegendRight()
217 void MainWidget::setLegendRight()
201 {
218 {
202 m_chart->legend()->setAlignment(Qt::AlignRight);
219 m_chart->legend()->setAlignment(Qt::AlignRight);
203 }
220 }
204
221
205 void MainWidget::setLegendTop()
222 void MainWidget::setLegendTop()
206 {
223 {
207 m_chart->legend()->setAlignment(Qt::AlignTop);
224 m_chart->legend()->setAlignment(Qt::AlignTop);
208 }
225 }
209
226
210 void MainWidget::setLegendBottom()
227 void MainWidget::setLegendBottom()
211 {
228 {
212 m_chart->legend()->setAlignment(Qt::AlignBottom);
229 m_chart->legend()->setAlignment(Qt::AlignBottom);
213 }
230 }
214
231
232 void MainWidget::toggleBold()
233 {
234 QFont font = m_chart->legend()->font();
235 font.setBold(!font.bold());
236 m_chart->legend()->setFont(font);
237 }
238
239 void MainWidget::toggleItalic()
240 {
241 QFont font = m_chart->legend()->font();
242 font.setItalic(!font.italic());
243 m_chart->legend()->setFont(font);
244 }
245
246 void MainWidget::fontSizeChanged()
247 {
248 QFont font = m_chart->legend()->font();
249 font.setPointSizeF(m_fontSize->value());
250 m_chart->legend()->setFont(font);
251 }
252
215 void MainWidget::updateLegendLayout()
253 void MainWidget::updateLegendLayout()
216 {
254 {
217 //![4]
255 //![4]
218 m_chart->legend()->setGeometry(m_legendPosX->value()
256 m_chart->legend()->setGeometry(m_legendPosX->value()
219 ,m_legendPosY->value()
257 ,m_legendPosY->value()
220 ,m_legendWidth->value()
258 ,m_legendWidth->value()
221 ,m_legendHeight->value());
259 ,m_legendHeight->value());
222 m_chart->legend()->update();
260 m_chart->legend()->update();
223 //![4]
261 //![4]
224 }
262 }
@@ -1,78 +1,86
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 detachLegend();
50 void attachLegend();
50 void attachLegend();
51 void addBarset();
51 void addBarset();
52 void removeBarset();
52 void removeBarset();
53
53
54 void setLegendLeft();
54 void setLegendLeft();
55 void setLegendRight();
55 void setLegendRight();
56 void setLegendTop();
56 void setLegendTop();
57 void setLegendBottom();
57 void setLegendBottom();
58
58
59 void toggleBold();
60 void toggleItalic();
61 void fontSizeChanged();
62
59 void updateLegendLayout();
63 void updateLegendLayout();
60
64
65
61 private:
66 private:
62
67
63 QChart *m_chart;
68 QChart *m_chart;
64 QBarSeries *m_series;
69 QBarSeries *m_series;
65
70
66 QChartView *m_chartView;
71 QChartView *m_chartView;
67 QGridLayout *m_mainLayout;
72 QGridLayout *m_mainLayout;
68 QGridLayout *m_buttonLayout;
73 QGridLayout *m_buttonLayout;
74 QGridLayout *m_fontLayout;
75
76 QDoubleSpinBox *m_fontSize;
69
77
70 // For detached layout
78 // For detached layout
71 QGroupBox* m_legendSettings;
79 QGroupBox* m_legendSettings;
72 QDoubleSpinBox *m_legendPosX;
80 QDoubleSpinBox *m_legendPosX;
73 QDoubleSpinBox *m_legendPosY;
81 QDoubleSpinBox *m_legendPosY;
74 QDoubleSpinBox *m_legendWidth;
82 QDoubleSpinBox *m_legendWidth;
75 QDoubleSpinBox *m_legendHeight;
83 QDoubleSpinBox *m_legendHeight;
76 };
84 };
77
85
78 #endif // MAINWIDGET_H
86 #endif // MAINWIDGET_H
@@ -1,198 +1,209
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 "legendmarker_p.h"
21 #include "legendmarker_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "qlegend.h"
24 #include "qlegend.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include "qpieslice.h"
27 #include "qpieslice.h"
28 #include "qbarset.h"
28 #include "qbarset.h"
29 #include "qbarset_p.h"
29 #include "qbarset_p.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qareaseries_p.h"
31 #include "qareaseries_p.h"
32 #include <QPainter>
32 #include <QPainter>
33 #include <QGraphicsSceneEvent>
33 #include <QGraphicsSceneEvent>
34 #include <QGraphicsSimpleTextItem>
34 #include <QGraphicsSimpleTextItem>
35 #include <QDebug>
35 #include <QDebug>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
40 QGraphicsObject(legend),
40 QGraphicsObject(legend),
41 m_series(series),
41 m_series(series),
42 m_markerRect(0,0,10.0,10.0),
42 m_markerRect(0,0,10.0,10.0),
43 m_boundingRect(0,0,0,0),
43 m_boundingRect(0,0,0,0),
44 m_legend(legend),
44 m_legend(legend),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
46 m_rectItem(new QGraphicsRectItem(this))
46 m_rectItem(new QGraphicsRectItem(this))
47 {
47 {
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
49 m_rectItem->setRect(m_markerRect);
49 m_rectItem->setRect(m_markerRect);
50 updateLayout();
50 updateLayout();
51 }
51 }
52
52
53 void LegendMarker::setPen(const QPen &pen)
53 void LegendMarker::setPen(const QPen &pen)
54 {
54 {
55 m_textItem->setPen(pen);
55 m_textItem->setPen(pen);
56 updateLayout();
56 updateLayout();
57 }
57 }
58
58
59 QPen LegendMarker::pen() const
59 QPen LegendMarker::pen() const
60 {
60 {
61 return m_textItem->pen();
61 return m_textItem->pen();
62 }
62 }
63
63
64 void LegendMarker::setBrush(const QBrush &brush)
64 void LegendMarker::setBrush(const QBrush &brush)
65 {
65 {
66 m_rectItem->setBrush(brush);
66 m_rectItem->setBrush(brush);
67 }
67 }
68
68
69 QBrush LegendMarker::brush() const
69 QBrush LegendMarker::brush() const
70 {
70 {
71 return m_rectItem->brush();
71 return m_rectItem->brush();
72 }
72 }
73
73
74 void LegendMarker::setFont(const QFont &font)
75 {
76 m_textItem->setFont(font);
77 updateLayout();
78 }
79
80 QFont LegendMarker::font() const
81 {
82 return m_textItem->font();
83 }
84
74 void LegendMarker::setLabel(const QString label)
85 void LegendMarker::setLabel(const QString label)
75 {
86 {
76 m_textItem->setText(label);
87 m_textItem->setText(label);
77 updateLayout();
88 updateLayout();
78 }
89 }
79
90
80 void LegendMarker::setSize(const QSize& size)
91 void LegendMarker::setSize(const QSize& size)
81 {
92 {
82 m_markerRect = QRectF(0,0,size.width(),size.height());
93 m_markerRect = QRectF(0,0,size.width(),size.height());
83 }
94 }
84
95
85 QString LegendMarker::label() const
96 QString LegendMarker::label() const
86 {
97 {
87 return m_textItem->text();
98 return m_textItem->text();
88 }
99 }
89
100
90 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
101 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
91 {
102 {
92 Q_UNUSED(option)
103 Q_UNUSED(option)
93 Q_UNUSED(widget)
104 Q_UNUSED(widget)
94 Q_UNUSED(painter)
105 Q_UNUSED(painter)
95 }
106 }
96
107
97 QRectF LegendMarker::boundingRect() const
108 QRectF LegendMarker::boundingRect() const
98 {
109 {
99 return m_boundingRect;
110 return m_boundingRect;
100 }
111 }
101
112
102 void LegendMarker::updateLayout()
113 void LegendMarker::updateLayout()
103 {
114 {
104
115
105 static const qreal margin = 2;
116 static const qreal margin = 2;
106 static const qreal space = 4;
117 static const qreal space = 4;
107
118
108 const QRectF& textRect = m_textItem->boundingRect();
119 const QRectF& textRect = m_textItem->boundingRect();
109 prepareGeometryChange();
120 prepareGeometryChange();
110 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
121 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
111 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
122 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
112 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
123 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
113
124
114 }
125 }
115
126
116 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
127 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
117 {
128 {
118 QGraphicsObject::mousePressEvent(event);
129 QGraphicsObject::mousePressEvent(event);
119 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
130 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
120 }
131 }
121
132
122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
133 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123
134
124 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
135 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
125 m_series(series)
136 m_series(series)
126 {
137 {
127 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
138 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
128 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
139 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
129 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
140 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
130 updated();
141 updated();
131 }
142 }
132
143
133 void AreaLegendMarker::updated()
144 void AreaLegendMarker::updated()
134 {
145 {
135 setBrush(m_series->brush());
146 setBrush(m_series->brush());
136 setLabel(m_series->name());
147 setLabel(m_series->name());
137 }
148 }
138
149
139 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140
151
141 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
152 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
142 m_barset(barset)
153 m_barset(barset)
143 {
154 {
144 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
155 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
145 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
156 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
146 updated();
157 updated();
147 }
158 }
148
159
149 void BarLegendMarker::updated()
160 void BarLegendMarker::updated()
150 {
161 {
151 setBrush(m_barset->brush());
162 setBrush(m_barset->brush());
152 setLabel(m_barset->label());
163 setLabel(m_barset->label());
153 }
164 }
154
165
155 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156
167
157 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
168 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
158 m_pieslice(pieslice)
169 m_pieslice(pieslice)
159 {
170 {
160 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
171 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
161 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
172 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
162 updated();
173 updated();
163 }
174 }
164
175
165 void PieLegendMarker::updated()
176 void PieLegendMarker::updated()
166 {
177 {
167 setBrush(m_pieslice->brush());
178 setBrush(m_pieslice->brush());
168 setLabel(m_pieslice->label());
179 setLabel(m_pieslice->label());
169 }
180 }
170
181
171 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
182 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
172
183
173 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
184 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
174 m_series(series)
185 m_series(series)
175 {
186 {
176 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
187 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
177 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
188 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
178 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
189 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
179 updated();
190 updated();
180 }
191 }
181
192
182 void XYLegendMarker::updated()
193 void XYLegendMarker::updated()
183 {
194 {
184 setLabel(m_series->name());
195 setLabel(m_series->name());
185
196
186 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
197 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
187 {
198 {
188 setBrush(m_series->brush());
199 setBrush(m_series->brush());
189
200
190 }
201 }
191 else {
202 else {
192 setBrush(QBrush(m_series->pen().color()));
203 setBrush(QBrush(m_series->pen().color()));
193 }
204 }
194 }
205 }
195
206
196 #include "moc_legendmarker_p.cpp"
207 #include "moc_legendmarker_p.cpp"
197
208
198 QTCOMMERCIALCHART_END_NAMESPACE
209 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,134 +1,137
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef LEGENDMARKER_P_H
30 #ifndef LEGENDMARKER_P_H
31 #define LEGENDMARKER_P_H
31 #define LEGENDMARKER_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QBrush>
35 #include <QBrush>
36 #include <QPen>
36 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
37 #include <QGraphicsSimpleTextItem>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QAbstractSeries;
41 class QAbstractSeries;
42 class QAreaSeries;
42 class QAreaSeries;
43 class QXYSeries;
43 class QXYSeries;
44 class QBarSet;
44 class QBarSet;
45 class QBarSeries;
45 class QBarSeries;
46 class QPieSlice;
46 class QPieSlice;
47 class QLegend;
47 class QLegend;
48 class QPieSeries;
48 class QPieSeries;
49
49
50 class LegendMarker : public QGraphicsObject
50 class LegendMarker : public QGraphicsObject
51 {
51 {
52 Q_OBJECT
52 Q_OBJECT
53
53
54 public:
54 public:
55 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
55 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
56
56
57 void setPen(const QPen &pen);
57 void setPen(const QPen &pen);
58 QPen pen() const;
58 QPen pen() const;
59 void setBrush(const QBrush &brush);
59 void setBrush(const QBrush &brush);
60 QBrush brush() const;
60 QBrush brush() const;
61
61
62 void setFont(const QFont &font);
63 QFont font() const;
64
62 void setSize(const QSize& size);
65 void setSize(const QSize& size);
63
66
64 void setLabel(const QString label);
67 void setLabel(const QString label);
65 QString label() const;
68 QString label() const;
66
69
67 QAbstractSeries *series() const { return m_series;}
70 QAbstractSeries *series() const { return m_series;}
68
71
69 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
72 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
70
73
71 QRectF boundingRect() const;
74 QRectF boundingRect() const;
72
75
73 void updateLayout();
76 void updateLayout();
74
77
75 protected:
78 protected:
76 // From QGraphicsObject
79 // From QGraphicsObject
77 void mousePressEvent(QGraphicsSceneMouseEvent *event);
80 void mousePressEvent(QGraphicsSceneMouseEvent *event);
78
81
79 public Q_SLOTS:
82 public Q_SLOTS:
80 virtual void updated() = 0;
83 virtual void updated() = 0;
81
84
82 protected:
85 protected:
83 QAbstractSeries *m_series;
86 QAbstractSeries *m_series;
84 QRectF m_markerRect;
87 QRectF m_markerRect;
85 QRectF m_boundingRect;
88 QRectF m_boundingRect;
86 QLegend* m_legend;
89 QLegend* m_legend;
87 QGraphicsSimpleTextItem *m_textItem;
90 QGraphicsSimpleTextItem *m_textItem;
88 QGraphicsRectItem *m_rectItem;
91 QGraphicsRectItem *m_rectItem;
89
92
90 };
93 };
91
94
92 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
95 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93 class XYLegendMarker : public LegendMarker
96 class XYLegendMarker : public LegendMarker
94 {
97 {
95 public:
98 public:
96 XYLegendMarker(QXYSeries *series, QLegend *legend);
99 XYLegendMarker(QXYSeries *series, QLegend *legend);
97 protected:
100 protected:
98 void updated();
101 void updated();
99 private:
102 private:
100 QXYSeries *m_series;
103 QXYSeries *m_series;
101 };
104 };
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 class AreaLegendMarker : public LegendMarker
106 class AreaLegendMarker : public LegendMarker
104 {
107 {
105 public:
108 public:
106 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
109 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
107 protected:
110 protected:
108 void updated();
111 void updated();
109 private:
112 private:
110 QAreaSeries *m_series;
113 QAreaSeries *m_series;
111 };
114 };
112 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
115 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113 class BarLegendMarker : public LegendMarker
116 class BarLegendMarker : public LegendMarker
114 {
117 {
115 public:
118 public:
116 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
119 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
117 protected:
120 protected:
118 void updated();
121 void updated();
119 private:
122 private:
120 QBarSet *m_barset;
123 QBarSet *m_barset;
121 };
124 };
122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123 class PieLegendMarker : public LegendMarker
126 class PieLegendMarker : public LegendMarker
124 {
127 {
125 public:
128 public:
126 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
129 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
127 protected:
130 protected:
128 void updated();
131 void updated();
129 private:
132 private:
130 QPieSlice *m_pieslice;
133 QPieSlice *m_pieslice;
131 };
134 };
132
135
133 QTCOMMERCIALCHART_END_NAMESPACE
136 QTCOMMERCIALCHART_END_NAMESPACE
134 #endif // LEGENDMARKER_P_H
137 #endif // LEGENDMARKER_P_H
@@ -1,760 +1,801
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
26
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 "qbarseries.h"
33 #include "qbarseries.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
44
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
125 The font of markers used by legend
126 */
127 /*!
128 \qmlproperty color Legend::font
129 The font of markers used by legend
130 */
131
132 /*!
124 \fn void QLegend::backgroundVisibleChanged(bool)
133 \fn void QLegend::backgroundVisibleChanged(bool)
125 The visibility of the legend background changed to \a visible.
134 The visibility of the legend background changed to \a visible.
126 */
135 */
127
136
128 /*!
137 /*!
129 \fn void QLegend::colorChanged(QColor)
138 \fn void QLegend::colorChanged(QColor)
130 The color of the legend background changed to \a color.
139 The color of the legend background changed to \a color.
131 */
140 */
132
141
133 /*!
142 /*!
134 \fn void QLegend::borderColorChanged(QColor)
143 \fn void QLegend::borderColorChanged(QColor)
135 The border color of the legend background changed to \a color.
144 The border color of the legend background changed to \a color.
136 */
145 */
137
146
138 /*!
147 /*!
148 \fn void QLegend::fontChanged(QFont)
149 The font of markers of the legend changed to \a font.
150 */
151
152 /*!
139 \fn qreal QLegend::minWidth() const
153 \fn qreal QLegend::minWidth() const
140 Returns minimum width of the legend
154 Returns minimum width of the legend
141 */
155 */
142
156
143 /*!
157 /*!
144 \fn qreal QLegend::minHeight() const
158 \fn qreal QLegend::minHeight() const
145 Returns minimum height of the legend
159 Returns minimum height of the legend
146 */
160 */
147
161
148 /*!
162 /*!
149 Constructs the legend object and sets the parent to \a parent
163 Constructs the legend object and sets the parent to \a parent
150 */
164 */
151
165
152 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
166 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
153 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
167 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
154 {
168 {
155 setZValue(ChartPresenter::LegendZValue);
169 setZValue(ChartPresenter::LegendZValue);
156 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
170 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
157 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
171 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
158 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
172 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
159 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
173 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
160 }
174 }
161
175
162 /*!
176 /*!
163 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
177 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
164 */
178 */
165 QLegend::~QLegend()
179 QLegend::~QLegend()
166 {
180 {
167 }
181 }
168
182
169 /*!
183 /*!
170 \internal
184 \internal
171 */
185 */
172 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
186 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
173 {
187 {
174 Q_UNUSED(option)
188 Q_UNUSED(option)
175 Q_UNUSED(widget)
189 Q_UNUSED(widget)
176 if(!d_ptr->m_backgroundVisible) return;
190 if(!d_ptr->m_backgroundVisible) return;
177
191
178 painter->setOpacity(opacity());
192 painter->setOpacity(opacity());
179 painter->setPen(d_ptr->m_pen);
193 painter->setPen(d_ptr->m_pen);
180 painter->setBrush(d_ptr->m_brush);
194 painter->setBrush(d_ptr->m_brush);
181 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
195 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
182 }
196 }
183
197
184 /*!
198 /*!
185 \internal
199 \internal
186 */
200 */
187 QRectF QLegend::boundingRect() const
201 QRectF QLegend::boundingRect() const
188 {
202 {
189 return d_ptr->m_rect;
203 return d_ptr->m_rect;
190 }
204 }
191
205
192 /*!
206 /*!
193 Sets the \a brush of legend. Brush affects the background of legend.
207 Sets the \a brush of legend. Brush affects the background of legend.
194 */
208 */
195 void QLegend::setBrush(const QBrush &brush)
209 void QLegend::setBrush(const QBrush &brush)
196 {
210 {
197 if (d_ptr->m_brush != brush) {
211 if (d_ptr->m_brush != brush) {
198 d_ptr->m_brush = brush;
212 d_ptr->m_brush = brush;
199 update();
213 update();
200 }
214 }
201 }
215 }
202
216
203 /*!
217 /*!
204 Returns the brush used by legend.
218 Returns the brush used by legend.
205 */
219 */
206 QBrush QLegend::brush() const
220 QBrush QLegend::brush() const
207 {
221 {
208 return d_ptr->m_brush;
222 return d_ptr->m_brush;
209 }
223 }
210
224
211 void QLegend::setColor(QColor color)
225 void QLegend::setColor(QColor color)
212 {
226 {
213 QBrush b = d_ptr->m_brush;
227 QBrush b = d_ptr->m_brush;
214 if (b.style() != Qt::SolidPattern || b.color() != color) {
228 if (b.style() != Qt::SolidPattern || b.color() != color) {
215 b.setStyle(Qt::SolidPattern);
229 b.setStyle(Qt::SolidPattern);
216 b.setColor(color);
230 b.setColor(color);
217 setBrush(b);
231 setBrush(b);
218 emit colorChanged(color);
232 emit colorChanged(color);
219 }
233 }
220 }
234 }
221
235
222 QColor QLegend::color()
236 QColor QLegend::color()
223 {
237 {
224 return d_ptr->m_brush.color();
238 return d_ptr->m_brush.color();
225 }
239 }
226
240
227 /*!
241 /*!
228 Sets the \a pen of legend. Pen affects the legend borders.
242 Sets the \a pen of legend. Pen affects the legend borders.
229 */
243 */
230 void QLegend::setPen(const QPen &pen)
244 void QLegend::setPen(const QPen &pen)
231 {
245 {
232 if (d_ptr->m_pen != pen) {
246 if (d_ptr->m_pen != pen) {
233 d_ptr->m_pen = pen;
247 d_ptr->m_pen = pen;
234 update();
248 update();
235 }
249 }
236 }
250 }
237
251
238 /*!
252 /*!
239 Returns the pen used by legend
253 Returns the pen used by legend
240 */
254 */
241
255
242 QPen QLegend::pen() const
256 QPen QLegend::pen() const
243 {
257 {
244 return d_ptr->m_pen;
258 return d_ptr->m_pen;
245 }
259 }
246
260
247 void QLegend::setBorderColor(QColor color)
261 void QLegend::setBorderColor(QColor color)
248 {
262 {
249 QPen p = d_ptr->m_pen;
263 QPen p = d_ptr->m_pen;
250 if (p.color() != color) {
264 if (p.color() != color) {
251 p.setColor(color);
265 p.setColor(color);
252 setPen(p);
266 setPen(p);
253 emit borderColorChanged(color);
267 emit borderColorChanged(color);
254 }
268 }
255 }
269 }
256
270
257 QColor QLegend::borderColor()
271 QColor QLegend::borderColor()
258 {
272 {
259 return d_ptr->m_pen.color();
273 return d_ptr->m_pen.color();
260 }
274 }
261
275
276 void QLegend::setFont(const QFont &font)
277 {
278 if (d_ptr->m_font != font) {
279 d_ptr->setFont(font);
280 emit fontChanged(font);
281 }
282 }
283
284 QFont QLegend::font() const
285 {
286 return d_ptr->m_font;
287 }
288
262 void QLegend::setAlignment(Qt::Alignment alignment)
289 void QLegend::setAlignment(Qt::Alignment alignment)
263 {
290 {
264 if(d_ptr->m_alignment!=alignment) {
291 if(d_ptr->m_alignment!=alignment) {
265 d_ptr->m_alignment = alignment;
292 d_ptr->m_alignment = alignment;
266 d_ptr->updateLayout();
293 d_ptr->updateLayout();
267 }
294 }
268 }
295 }
269
296
270 Qt::Alignment QLegend::alignment() const
297 Qt::Alignment QLegend::alignment() const
271 {
298 {
272 return d_ptr->m_alignment;
299 return d_ptr->m_alignment;
273 }
300 }
274
301
275 /*!
302 /*!
276 Detaches the legend from chart. Chart won't change layout of the legend.
303 Detaches the legend from chart. Chart won't change layout of the legend.
277 */
304 */
278 void QLegend::detachFromChart()
305 void QLegend::detachFromChart()
279 {
306 {
280 d_ptr->m_attachedToChart = false;
307 d_ptr->m_attachedToChart = false;
281 }
308 }
282
309
283 /*!
310 /*!
284 Attaches the legend to chart. Chart may change layout of the legend.
311 Attaches the legend to chart. Chart may change layout of the legend.
285 */
312 */
286 void QLegend::attachToChart()
313 void QLegend::attachToChart()
287 {
314 {
288 d_ptr->attachToChart();
315 d_ptr->attachToChart();
289 }
316 }
290
317
291 /*!
318 /*!
292 Returns true, if legend is attached to chart.
319 Returns true, if legend is attached to chart.
293 */
320 */
294 bool QLegend::isAttachedToChart()
321 bool QLegend::isAttachedToChart()
295 {
322 {
296 return d_ptr->m_attachedToChart;
323 return d_ptr->m_attachedToChart;
297 }
324 }
298
325
299 /*!
326 /*!
300 Sets the visibility of legend background to \a visible
327 Sets the visibility of legend background to \a visible
301 */
328 */
302 void QLegend::setBackgroundVisible(bool visible)
329 void QLegend::setBackgroundVisible(bool visible)
303 {
330 {
304 if(d_ptr->m_backgroundVisible != visible) {
331 if(d_ptr->m_backgroundVisible != visible) {
305 d_ptr->m_backgroundVisible = visible;
332 d_ptr->m_backgroundVisible = visible;
306 update();
333 update();
307 emit backgroundVisibleChanged(visible);
334 emit backgroundVisibleChanged(visible);
308 }
335 }
309 }
336 }
310
337
311 /*!
338 /*!
312 Returns the visibility of legend background
339 Returns the visibility of legend background
313 */
340 */
314 bool QLegend::isBackgroundVisible() const
341 bool QLegend::isBackgroundVisible() const
315 {
342 {
316 return d_ptr->m_backgroundVisible;
343 return d_ptr->m_backgroundVisible;
317 }
344 }
318
345
319 /*!
346 /*!
320 \internal \a event see QGraphicsWidget for details
347 \internal \a event see QGraphicsWidget for details
321 */
348 */
322 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
349 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
323 {
350 {
324 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
351 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
325 QGraphicsWidget::resizeEvent(event);
352 QGraphicsWidget::resizeEvent(event);
326 if(d_ptr->m_rect != rect) {
353 if(d_ptr->m_rect != rect) {
327 d_ptr->m_rect = rect;
354 d_ptr->m_rect = rect;
328 d_ptr->updateLayout();
355 d_ptr->updateLayout();
329 }
356 }
330 }
357 }
331
358
332 /*!
359 /*!
333 \internal \a event see QGraphicsWidget for details
360 \internal \a event see QGraphicsWidget for details
334 */
361 */
335 void QLegend::hideEvent(QHideEvent *event)
362 void QLegend::hideEvent(QHideEvent *event)
336 {
363 {
337 QGraphicsWidget::hideEvent(event);
364 QGraphicsWidget::hideEvent(event);
338 setEnabled(false);
365 setEnabled(false);
339 d_ptr->updateLayout();
366 d_ptr->updateLayout();
340 }
367 }
341
368
342 /*!
369 /*!
343 \internal \a event see QGraphicsWidget for details
370 \internal \a event see QGraphicsWidget for details
344 */
371 */
345 void QLegend::showEvent(QShowEvent *event)
372 void QLegend::showEvent(QShowEvent *event)
346 {
373 {
347 QGraphicsWidget::showEvent(event);
374 QGraphicsWidget::showEvent(event);
348 setEnabled(true);
375 setEnabled(true);
349 d_ptr->updateLayout();
376 d_ptr->updateLayout();
350 }
377 }
351
378
352 qreal QLegend::minWidth() const
379 qreal QLegend::minWidth() const
353 {
380 {
354 return d_ptr->m_minWidth;
381 return d_ptr->m_minWidth;
355 }
382 }
356
383
357 qreal QLegend::minHeight() const
384 qreal QLegend::minHeight() const
358 {
385 {
359 return d_ptr->m_minHeight;
386 return d_ptr->m_minHeight;
360 }
387 }
361
388
362 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
389 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
363
390
364 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
391 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
365 q_ptr(q),
392 q_ptr(q),
366 m_presenter(presenter),
393 m_presenter(presenter),
367 m_chart(chart),
394 m_chart(chart),
368 m_markers(new QGraphicsItemGroup(q)),
395 m_markers(new QGraphicsItemGroup(q)),
369 m_alignment(Qt::AlignTop),
396 m_alignment(Qt::AlignTop),
370 m_brush(QBrush()),
397 m_brush(QBrush()),
371 m_pen(QPen()),
398 m_pen(QPen()),
372 m_offsetX(0),
399 m_offsetX(0),
373 m_offsetY(0),
400 m_offsetY(0),
374 m_minWidth(0),
401 m_minWidth(0),
375 m_minHeight(0),
402 m_minHeight(0),
376 m_width(0),
403 m_width(0),
377 m_height(0),
404 m_height(0),
378 m_diameter(5),
405 m_diameter(5),
379 m_attachedToChart(true),
406 m_attachedToChart(true),
380 m_backgroundVisible(false)
407 m_backgroundVisible(false)
381 {
408 {
382
409
383 }
410 }
384
411
385 QLegendPrivate::~QLegendPrivate()
412 QLegendPrivate::~QLegendPrivate()
386 {
413 {
387
414
388 }
415 }
389
416
390 void QLegendPrivate::setOffset(qreal x, qreal y)
417 void QLegendPrivate::setOffset(qreal x, qreal y)
391 {
418 {
392 bool scrollHorizontal = true;
419 bool scrollHorizontal = true;
393 switch(m_alignment) {
420 switch(m_alignment) {
394 case Qt::AlignTop:
421 case Qt::AlignTop:
395 case Qt::AlignBottom: {
422 case Qt::AlignBottom: {
396 scrollHorizontal = true;
423 scrollHorizontal = true;
397 break;
424 break;
398 }
425 }
399 case Qt::AlignLeft:
426 case Qt::AlignLeft:
400 case Qt::AlignRight: {
427 case Qt::AlignRight: {
401 scrollHorizontal = false;
428 scrollHorizontal = false;
402 break;
429 break;
403 }
430 }
404 }
431 }
405
432
406 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
433 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
407 if (!m_attachedToChart) {
434 if (!m_attachedToChart) {
408 scrollHorizontal = !scrollHorizontal;
435 scrollHorizontal = !scrollHorizontal;
409 }
436 }
410
437
411 // Limit offset between m_minOffset and m_maxOffset
438 // Limit offset between m_minOffset and m_maxOffset
412 if (scrollHorizontal) {
439 if (scrollHorizontal) {
413 if(m_width<=m_rect.width()) return;
440 if(m_width<=m_rect.width()) return;
414
441
415 if (x != m_offsetX) {
442 if (x != m_offsetX) {
416 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
443 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
417 m_markers->setPos(-m_offsetX,m_rect.top());
444 m_markers->setPos(-m_offsetX,m_rect.top());
418 }
445 }
419 } else {
446 } else {
420 if(m_height<=m_rect.height()) return;
447 if(m_height<=m_rect.height()) return;
421
448
422 if (y != m_offsetY) {
449 if (y != m_offsetY) {
423 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
450 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
424 m_markers->setPos(m_rect.left(),-m_offsetY);
451 m_markers->setPos(m_rect.left(),-m_offsetY);
425 }
452 }
426 }
453 }
427 }
454 }
428
455
429 QPointF QLegendPrivate::offset() const
456 QPointF QLegendPrivate::offset() const
430 {
457 {
431 return QPointF(m_offsetX,m_offsetY);
458 return QPointF(m_offsetX,m_offsetY);
432 }
459 }
433
460
434 void QLegendPrivate::updateLayout()
461 void QLegendPrivate::updateLayout()
435 {
462 {
436 if (!m_attachedToChart) {
463 if (!m_attachedToChart) {
437 updateDetachedLayout();
464 updateDetachedLayout();
438 return;
465 return;
439 }
466 }
440
467
441 m_offsetX=0;
468 m_offsetX=0;
442 QList<QGraphicsItem *> items = m_markers->childItems();
469 QList<QGraphicsItem *> items = m_markers->childItems();
443
470
444 if(items.isEmpty()) return;
471 if(items.isEmpty()) return;
445
472
446 m_minWidth=0;
473 m_minWidth=0;
447 m_minHeight=0;
474 m_minHeight=0;
448
475
449 switch(m_alignment) {
476 switch(m_alignment) {
450
477
451 case Qt::AlignTop:
478 case Qt::AlignTop:
452 case Qt::AlignBottom: {
479 case Qt::AlignBottom: {
453 QPointF point = m_rect.topLeft();
480 QPointF point = m_rect.topLeft();
454 m_width = 0;
481 m_width = 0;
455 foreach (QGraphicsItem *item, items) {
482 foreach (QGraphicsItem *item, items) {
456 if (item->isVisible()) {
483 if (item->isVisible()) {
457 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
484 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
458 const QRectF& rect = item->boundingRect();
485 const QRectF& rect = item->boundingRect();
459 qreal w = rect.width();
486 qreal w = rect.width();
460 m_minWidth=qMax(m_minWidth,w);
487 m_minWidth=qMax(m_minWidth,w);
461 m_minHeight=qMax(m_minHeight,rect.height());
488 m_minHeight=qMax(m_minHeight,rect.height());
462 m_width+=w;
489 m_width+=w;
463 point.setX(point.x() + w);
490 point.setX(point.x() + w);
464 }
491 }
465 }
492 }
466 if(m_width<m_rect.width()) {
493 if(m_width<m_rect.width()) {
467 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
494 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
468 }
495 }
469 else {
496 else {
470 m_markers->setPos(m_rect.topLeft());
497 m_markers->setPos(m_rect.topLeft());
471 }
498 }
472 m_height=m_minHeight;
499 m_height=m_minHeight;
473 }
500 }
474 break;
501 break;
475 case Qt::AlignLeft:
502 case Qt::AlignLeft:
476 case Qt::AlignRight: {
503 case Qt::AlignRight: {
477 QPointF point = m_rect.topLeft();
504 QPointF point = m_rect.topLeft();
478 m_height = 0;
505 m_height = 0;
479 foreach (QGraphicsItem *item, items) {
506 foreach (QGraphicsItem *item, items) {
480 if (item->isVisible()) {
507 if (item->isVisible()) {
481 item->setPos(point);
508 item->setPos(point);
482 const QRectF& rect = item->boundingRect();
509 const QRectF& rect = item->boundingRect();
483 qreal h = rect.height();
510 qreal h = rect.height();
484 m_minWidth=qMax(m_minWidth,rect.width());
511 m_minWidth=qMax(m_minWidth,rect.width());
485 m_minHeight=qMax(m_minHeight,h);
512 m_minHeight=qMax(m_minHeight,h);
486 m_height+=h;
513 m_height+=h;
487 point.setY(point.y() + h);
514 point.setY(point.y() + h);
488 }
515 }
489 }
516 }
490 if(m_height<m_rect.height()) {
517 if(m_height<m_rect.height()) {
491 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
518 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
492 }
519 }
493 else {
520 else {
494 m_markers->setPos(m_rect.topLeft());
521 m_markers->setPos(m_rect.topLeft());
495 }
522 }
496 m_width=m_minWidth;
523 m_width=m_minWidth;
497 }
524 }
498 break;
525 break;
499 }
526 }
500
527
501 m_minOffsetX = 0;
528 m_minOffsetX = 0;
502 m_minOffsetY = 0;
529 m_minOffsetY = 0;
503 m_maxOffsetX = m_width - m_rect.width();
530 m_maxOffsetX = m_width - m_rect.width();
504 m_maxOffsetY = m_height - m_rect.height();
531 m_maxOffsetY = m_height - m_rect.height();
505
532
506 m_presenter->updateLayout();
533 m_presenter->updateLayout();
507 }
534 }
508
535
509 void QLegendPrivate::updateDetachedLayout()
536 void QLegendPrivate::updateDetachedLayout()
510 {
537 {
511 // Detached layout is different.
538 // Detached layout is different.
512 // In detached mode legend may have multiple rows and columns, so layout calculations
539 // In detached mode legend may have multiple rows and columns, so layout calculations
513 // differ a log from attached mode.
540 // differ a log from attached mode.
514 // Also the scrolling logic is bit different.
541 // Also the scrolling logic is bit different.
515 m_offsetX=0;
542 m_offsetX=0;
516 m_offsetY=0;
543 m_offsetY=0;
517 QList<QGraphicsItem *> items = m_markers->childItems();
544 QList<QGraphicsItem *> items = m_markers->childItems();
518
545
519 if(items.isEmpty()) return;
546 if(items.isEmpty()) return;
520
547
521 m_minWidth = 0;
548 m_minWidth = 0;
522 m_minHeight = 0;
549 m_minHeight = 0;
523
550
524 switch (m_alignment) {
551 switch (m_alignment) {
525 case Qt::AlignTop: {
552 case Qt::AlignTop: {
526 QPointF point = m_rect.topLeft();
553 QPointF point = m_rect.topLeft();
527 m_width = 0;
554 m_width = 0;
528 m_height = 0;
555 m_height = 0;
529 for (int i=0; i<items.count(); i++) {
556 for (int i=0; i<items.count(); i++) {
530 QGraphicsItem *item = items.at(i);
557 QGraphicsItem *item = items.at(i);
531 if (item->isVisible()) {
558 if (item->isVisible()) {
532 const QRectF& rect = item->boundingRect();
559 const QRectF& rect = item->boundingRect();
533 qreal w = rect.width();
560 qreal w = rect.width();
534 qreal h = rect.height();
561 qreal h = rect.height();
535 m_minWidth = qMax(m_minWidth,w);
562 m_minWidth = qMax(m_minWidth,w);
536 m_minHeight = qMax(m_minHeight,rect.height());
563 m_minHeight = qMax(m_minHeight,rect.height());
537 m_height = qMax(m_height,h);
564 m_height = qMax(m_height,h);
538 item->setPos(point.x(),point.y());
565 item->setPos(point.x(),point.y());
539 point.setX(point.x() + w);
566 point.setX(point.x() + w);
540 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
567 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
541 // Next item would go off rect.
568 // Next item would go off rect.
542 point.setX(m_rect.topLeft().x());
569 point.setX(m_rect.topLeft().x());
543 point.setY(point.y() + h);
570 point.setY(point.y() + h);
544 if (i+1 < items.count()) {
571 if (i+1 < items.count()) {
545 m_height += h;
572 m_height += h;
546 }
573 }
547 }
574 }
548 }
575 }
549 }
576 }
550 m_markers->setPos(m_rect.topLeft());
577 m_markers->setPos(m_rect.topLeft());
551 m_width = m_minWidth;
578 m_width = m_minWidth;
552
579
553 m_minOffsetX = 0;
580 m_minOffsetX = 0;
554 m_minOffsetY = 0;
581 m_minOffsetY = 0;
555 m_maxOffsetX = m_width - m_rect.width();
582 m_maxOffsetX = m_width - m_rect.width();
556 m_maxOffsetY = m_height - m_rect.height();
583 m_maxOffsetY = m_height - m_rect.height();
557 }
584 }
558 break;
585 break;
559 case Qt::AlignBottom: {
586 case Qt::AlignBottom: {
560 QPointF point = m_rect.bottomLeft();
587 QPointF point = m_rect.bottomLeft();
561 m_width = 0;
588 m_width = 0;
562 m_height = 0;
589 m_height = 0;
563 for (int i=0; i<items.count(); i++) {
590 for (int i=0; i<items.count(); i++) {
564 QGraphicsItem *item = items.at(i);
591 QGraphicsItem *item = items.at(i);
565 if (item->isVisible()) {
592 if (item->isVisible()) {
566 const QRectF& rect = item->boundingRect();
593 const QRectF& rect = item->boundingRect();
567 qreal w = rect.width();
594 qreal w = rect.width();
568 qreal h = rect.height();
595 qreal h = rect.height();
569 m_minWidth = qMax(m_minWidth,w);
596 m_minWidth = qMax(m_minWidth,w);
570 m_minHeight = qMax(m_minHeight,rect.height());
597 m_minHeight = qMax(m_minHeight,rect.height());
571 m_height = qMax(m_height,h);
598 m_height = qMax(m_height,h);
572 item->setPos(point.x(),point.y() - h);
599 item->setPos(point.x(),point.y() - h);
573 point.setX(point.x() + w);
600 point.setX(point.x() + w);
574 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
601 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
575 // Next item would go off rect.
602 // Next item would go off rect.
576 point.setX(m_rect.bottomLeft().x());
603 point.setX(m_rect.bottomLeft().x());
577 point.setY(point.y() - h);
604 point.setY(point.y() - h);
578 if (i+1 < items.count()) {
605 if (i+1 < items.count()) {
579 m_height += h;
606 m_height += h;
580 }
607 }
581 }
608 }
582 }
609 }
583 }
610 }
584 m_markers->setPos(m_rect.topLeft());
611 m_markers->setPos(m_rect.topLeft());
585 m_width = m_minWidth;
612 m_width = m_minWidth;
586
613
587 m_minOffsetX = 0;
614 m_minOffsetX = 0;
588 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
615 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
589 m_maxOffsetX = m_width - m_rect.width();
616 m_maxOffsetX = m_width - m_rect.width();
590 m_maxOffsetY = 0;
617 m_maxOffsetY = 0;
591 }
618 }
592 break;
619 break;
593 case Qt::AlignLeft: {
620 case Qt::AlignLeft: {
594 QPointF point = m_rect.topLeft();
621 QPointF point = m_rect.topLeft();
595 m_width = 0;
622 m_width = 0;
596 m_height = 0;
623 m_height = 0;
597 qreal maxWidth = 0;
624 qreal maxWidth = 0;
598 for (int i=0; i<items.count(); i++) {
625 for (int i=0; i<items.count(); i++) {
599 QGraphicsItem *item = items.at(i);
626 QGraphicsItem *item = items.at(i);
600 if (item->isVisible()) {
627 if (item->isVisible()) {
601 const QRectF& rect = item->boundingRect();
628 const QRectF& rect = item->boundingRect();
602 qreal w = rect.width();
629 qreal w = rect.width();
603 qreal h = rect.height();
630 qreal h = rect.height();
604 m_minWidth = qMax(m_minWidth,rect.width());
631 m_minWidth = qMax(m_minWidth,rect.width());
605 m_minHeight = qMax(m_minHeight,h);
632 m_minHeight = qMax(m_minHeight,h);
606 maxWidth = qMax(maxWidth,w);
633 maxWidth = qMax(maxWidth,w);
607 item->setPos(point.x(),point.y());
634 item->setPos(point.x(),point.y());
608 point.setY(point.y() + h);
635 point.setY(point.y() + h);
609 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
636 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
610 // Next item would go off rect.
637 // Next item would go off rect.
611 point.setX(point.x() + maxWidth);
638 point.setX(point.x() + maxWidth);
612 point.setY(m_rect.topLeft().y());
639 point.setY(m_rect.topLeft().y());
613 if (i+1 < items.count()) {
640 if (i+1 < items.count()) {
614 m_width += maxWidth;
641 m_width += maxWidth;
615 maxWidth = 0;
642 maxWidth = 0;
616 }
643 }
617 }
644 }
618 }
645 }
619 }
646 }
620 m_width += maxWidth;
647 m_width += maxWidth;
621 m_markers->setPos(m_rect.topLeft());
648 m_markers->setPos(m_rect.topLeft());
622 m_height = m_minHeight;
649 m_height = m_minHeight;
623
650
624 m_minOffsetX = 0;
651 m_minOffsetX = 0;
625 m_minOffsetY = 0;
652 m_minOffsetY = 0;
626 m_maxOffsetX = m_width - m_rect.width();
653 m_maxOffsetX = m_width - m_rect.width();
627 m_maxOffsetY = m_height - m_rect.height();
654 m_maxOffsetY = m_height - m_rect.height();
628 }
655 }
629 break;
656 break;
630 case Qt::AlignRight: {
657 case Qt::AlignRight: {
631 QPointF point = m_rect.topRight();
658 QPointF point = m_rect.topRight();
632 m_width = 0;
659 m_width = 0;
633 m_height = 0;
660 m_height = 0;
634 qreal maxWidth = 0;
661 qreal maxWidth = 0;
635 for (int i=0; i<items.count(); i++) {
662 for (int i=0; i<items.count(); i++) {
636 QGraphicsItem *item = items.at(i);
663 QGraphicsItem *item = items.at(i);
637 if (item->isVisible()) {
664 if (item->isVisible()) {
638 const QRectF& rect = item->boundingRect();
665 const QRectF& rect = item->boundingRect();
639 qreal w = rect.width();
666 qreal w = rect.width();
640 qreal h = rect.height();
667 qreal h = rect.height();
641 m_minWidth = qMax(m_minWidth,rect.width());
668 m_minWidth = qMax(m_minWidth,rect.width());
642 m_minHeight = qMax(m_minHeight,h);
669 m_minHeight = qMax(m_minHeight,h);
643 maxWidth = qMax(maxWidth,w);
670 maxWidth = qMax(maxWidth,w);
644 item->setPos(point.x() - w,point.y());
671 item->setPos(point.x() - w,point.y());
645 point.setY(point.y() + h);
672 point.setY(point.y() + h);
646 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
673 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
647 // Next item would go off rect.
674 // Next item would go off rect.
648 point.setX(point.x() - maxWidth);
675 point.setX(point.x() - maxWidth);
649 point.setY(m_rect.topLeft().y());
676 point.setY(m_rect.topLeft().y());
650 if (i+1 < items.count()) {
677 if (i+1 < items.count()) {
651 m_width += maxWidth;
678 m_width += maxWidth;
652 maxWidth = 0;
679 maxWidth = 0;
653 }
680 }
654 }
681 }
655 }
682 }
656 }
683 }
657 m_width += maxWidth;
684 m_width += maxWidth;
658 m_markers->setPos(m_rect.topLeft());
685 m_markers->setPos(m_rect.topLeft());
659 m_height = m_minHeight;
686 m_height = m_minHeight;
660
687
661 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
688 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
662 m_minOffsetY = 0;
689 m_minOffsetY = 0;
663 m_maxOffsetX = 0;
690 m_maxOffsetX = 0;
664 m_maxOffsetY = m_height - m_rect.height();
691 m_maxOffsetY = m_height - m_rect.height();
665 }
692 }
666 break;
693 break;
667 default:
694 default:
668 break;
695 break;
669 }
696 }
670 }
697 }
671
698
672 void QLegendPrivate::attachToChart()
699 void QLegendPrivate::attachToChart()
673 {
700 {
674 m_attachedToChart = true;
701 m_attachedToChart = true;
675 q_ptr->setParent(m_chart);
702 q_ptr->setParent(m_chart);
676 }
703 }
677
704
678 int QLegendPrivate::roundness(qreal size)
705 int QLegendPrivate::roundness(qreal size)
679 {
706 {
680 return 100*m_diameter/int(size);
707 return 100*m_diameter/int(size);
681 }
708 }
682
709
710 void QLegendPrivate::setFont(const QFont &font)
711 {
712 m_font = font;
713 QList<QGraphicsItem *> items = m_markers->childItems();
714
715 foreach (QGraphicsItem *markers, items) {
716 LegendMarker *marker = static_cast<LegendMarker*>(markers);
717 marker->setFont(m_font);
718 }
719 updateLayout();
720 }
721
683 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
722 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
684 {
723 {
685 Q_UNUSED(domain)
724 Q_UNUSED(domain)
686
725
687 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
726 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
688 foreach(LegendMarker* marker, markers)
727 foreach(LegendMarker* marker, markers) {
728 marker->setFont(m_font);
689 m_markers->addToGroup(marker);
729 m_markers->addToGroup(marker);
730 }
690
731
691 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
732 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
692
733
693 if(series->type() == QAbstractSeries::SeriesTypePie) {
734 if(series->type() == QAbstractSeries::SeriesTypePie) {
694 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
735 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
695 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
736 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
696 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
737 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
697 }
738 }
698
739
699 updateLayout();
740 updateLayout();
700 }
741 }
701
742
702 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
743 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
703 {
744 {
704 QList<QGraphicsItem *> items = m_markers->childItems();
745 QList<QGraphicsItem *> items = m_markers->childItems();
705
746
706 foreach (QGraphicsItem *markers, items) {
747 foreach (QGraphicsItem *markers, items) {
707 LegendMarker *marker = static_cast<LegendMarker*>(markers);
748 LegendMarker *marker = static_cast<LegendMarker*>(markers);
708 if (marker->series() == series) {
749 if (marker->series() == series) {
709 delete marker;
750 delete marker;
710 }
751 }
711 }
752 }
712
753
713 if(series->type() == QAbstractSeries::SeriesTypePie)
754 if(series->type() == QAbstractSeries::SeriesTypePie)
714 {
755 {
715 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
756 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
716 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
757 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
717 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
758 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
718 }
759 }
719
760
720 updateLayout();
761 updateLayout();
721 }
762 }
722
763
723 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
764 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
724 {
765 {
725 // TODO: find out which markers are are added or removed. Update them
766 // TODO: find out which markers are are added or removed. Update them
726 // TODO: better implementation
767 // TODO: better implementation
727 handleSeriesRemoved(series);
768 handleSeriesRemoved(series);
728 Domain domain;
769 Domain domain;
729 handleSeriesAdded(series, &domain);
770 handleSeriesAdded(series, &domain);
730 }
771 }
731
772
732 void QLegendPrivate::handleUpdatePieSeries()
773 void QLegendPrivate::handleUpdatePieSeries()
733 {
774 {
734 //TODO: reimplement to be optimal
775 //TODO: reimplement to be optimal
735 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
776 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
736 Q_ASSERT(series);
777 Q_ASSERT(series);
737 handleSeriesRemoved(series);
778 handleSeriesRemoved(series);
738 handleSeriesAdded(series, 0);
779 handleSeriesAdded(series, 0);
739 }
780 }
740
781
741 void QLegendPrivate::handleSeriesVisibleChanged()
782 void QLegendPrivate::handleSeriesVisibleChanged()
742 {
783 {
743 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
784 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
744 QList<QGraphicsItem *> items = m_markers->childItems();
785 QList<QGraphicsItem *> items = m_markers->childItems();
745
786
746 foreach (QGraphicsItem *markers, items) {
787 foreach (QGraphicsItem *markers, items) {
747 LegendMarker *marker = static_cast<LegendMarker*>(markers);
788 LegendMarker *marker = static_cast<LegendMarker*>(markers);
748 if (marker->series() == series) {
789 if (marker->series() == series) {
749 marker->setVisible(!marker->isVisible());
790 marker->setVisible(!marker->isVisible());
750 }
791 }
751 }
792 }
752
793
753 updateLayout();
794 updateLayout();
754 }
795 }
755
796
756
797
757 #include "moc_qlegend.cpp"
798 #include "moc_qlegend.cpp"
758 #include "moc_qlegend_p.cpp"
799 #include "moc_qlegend_p.cpp"
759
800
760 QTCOMMERCIALCHART_END_NAMESPACE
801 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,106
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 QLEGEND_H
21 #ifndef QLEGEND_H
22 #define QLEGEND_H
22 #define QLEGEND_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QGraphicsWidget>
25 #include <QGraphicsWidget>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Domain;
31 class Domain;
32 class LegendMarker;
32 class LegendMarker;
33 class QPieSlice;
33 class QPieSlice;
34 class QXYSeries;
34 class QXYSeries;
35 class QBarSet;
35 class QBarSet;
36 class QBarSeries;
36 class QBarSeries;
37 class QPieSeries;
37 class QPieSeries;
38 class QAreaSeries;
38 class QAreaSeries;
39 class LegendScrollButton;
39 class LegendScrollButton;
40 class QChart;
40 class QChart;
41 class QLegendPrivate;
41 class QLegendPrivate;
42
42
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
50
51
51 private:
52 private:
52 explicit QLegend(QChart *chart);
53 explicit QLegend(QChart *chart);
53
54
54 public:
55 public:
55 ~QLegend();
56 ~QLegend();
56
57
57 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
58 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
58 QRectF boundingRect() const;
59 QRectF boundingRect() const;
59
60
60 void setBrush(const QBrush &brush);
61 void setBrush(const QBrush &brush);
61 QBrush brush() const;
62 QBrush brush() const;
62 void setColor(QColor color);
63 void setColor(QColor color);
63 QColor color();
64 QColor color();
64
65
65 void setPen(const QPen &pen);
66 void setPen(const QPen &pen);
66 QPen pen() const;
67 QPen pen() const;
67 void setBorderColor(QColor color);
68 void setBorderColor(QColor color);
68 QColor borderColor();
69 QColor borderColor();
69
70
71 void setFont(const QFont &font);
72 QFont font() const;
73
70 void setAlignment(Qt::Alignment alignment);
74 void setAlignment(Qt::Alignment alignment);
71 Qt::Alignment alignment() const;
75 Qt::Alignment alignment() const;
72
76
73 void detachFromChart();
77 void detachFromChart();
74 void attachToChart();
78 void attachToChart();
75 bool isAttachedToChart();
79 bool isAttachedToChart();
76
80
77 qreal minWidth() const;
81 qreal minWidth() const;
78 qreal minHeight() const;
82 qreal minHeight() const;
79
83
80 void setBackgroundVisible(bool visible = true);
84 void setBackgroundVisible(bool visible = true);
81 bool isBackgroundVisible() const;
85 bool isBackgroundVisible() const;
82
86
83 protected:
87 protected:
84 void resizeEvent(QGraphicsSceneResizeEvent *event);
88 void resizeEvent(QGraphicsSceneResizeEvent *event);
85 void hideEvent(QHideEvent *event);
89 void hideEvent(QHideEvent *event);
86 void showEvent(QShowEvent *event);
90 void showEvent(QShowEvent *event);
87
91
88 Q_SIGNALS:
92 Q_SIGNALS:
89 void backgroundVisibleChanged(bool visible);
93 void backgroundVisibleChanged(bool visible);
90 void colorChanged(QColor color);
94 void colorChanged(QColor color);
91 void borderColorChanged(QColor color);
95 void borderColorChanged(QColor color);
96 void fontChanged(QFont font);
92
97
93 private:
98 private:
94 QScopedPointer<QLegendPrivate> d_ptr;
99 QScopedPointer<QLegendPrivate> d_ptr;
95 Q_DISABLE_COPY(QLegend)
100 Q_DISABLE_COPY(QLegend)
96 friend class LegendScroller;
101 friend class LegendScroller;
97 };
102 };
98
103
99 QTCOMMERCIALCHART_END_NAMESPACE
104 QTCOMMERCIALCHART_END_NAMESPACE
100
105
101 #endif // QLEGEND_H
106 #endif // QLEGEND_H
@@ -1,91 +1,93
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGEND_P_H
30 #ifndef QLEGEND_P_H
31 #define QLEGEND_P_H
31 #define QLEGEND_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QChart;
37 class QChart;
38 class ChartPresenter;
38 class ChartPresenter;
39 class QAbstractSeries;
39 class QAbstractSeries;
40
40
41 class QLegendPrivate : public QObject
41 class QLegendPrivate : public QObject
42 {
42 {
43 Q_OBJECT
43 Q_OBJECT
44 public:
44 public:
45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
46 ~QLegendPrivate();
46 ~QLegendPrivate();
47
47
48 void setOffset(qreal x, qreal y);
48 void setOffset(qreal x, qreal y);
49 QPointF offset() const;
49 QPointF offset() const;
50 void updateLayout();
50 void updateLayout();
51 void updateDetachedLayout();
51 void updateDetachedLayout();
52 void attachToChart();
52 void attachToChart();
53 int roundness(qreal size);
53 int roundness(qreal size);
54 void setFont(const QFont &font);
54
55
55 public Q_SLOTS:
56 public Q_SLOTS:
56 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
57 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
57 void handleSeriesRemoved(QAbstractSeries *series);
58 void handleSeriesRemoved(QAbstractSeries *series);
58 void handleSeriesUpdated(QAbstractSeries *series);
59 void handleSeriesUpdated(QAbstractSeries *series);
59 void handleUpdatePieSeries(); //TODO remove this function
60 void handleUpdatePieSeries(); //TODO remove this function
60 void handleSeriesVisibleChanged();
61 void handleSeriesVisibleChanged();
61
62
62 private:
63 private:
63 QLegend *q_ptr;
64 QLegend *q_ptr;
64 ChartPresenter *m_presenter;
65 ChartPresenter *m_presenter;
65 QChart* m_chart;
66 QChart* m_chart;
66 QGraphicsItemGroup* m_markers;
67 QGraphicsItemGroup* m_markers;
67 Qt::Alignment m_alignment;
68 Qt::Alignment m_alignment;
68 QBrush m_brush;
69 QBrush m_brush;
69 QPen m_pen;
70 QPen m_pen;
71 QFont m_font;
70 QRectF m_rect;
72 QRectF m_rect;
71 qreal m_offsetX;
73 qreal m_offsetX;
72 qreal m_offsetY;
74 qreal m_offsetY;
73 qreal m_minOffsetX;
75 qreal m_minOffsetX;
74 qreal m_minOffsetY;
76 qreal m_minOffsetY;
75 qreal m_maxOffsetX;
77 qreal m_maxOffsetX;
76 qreal m_maxOffsetY;
78 qreal m_maxOffsetY;
77 qreal m_minWidth;
79 qreal m_minWidth;
78 qreal m_minHeight;
80 qreal m_minHeight;
79 qreal m_width;
81 qreal m_width;
80 qreal m_height;
82 qreal m_height;
81 qreal m_diameter;
83 qreal m_diameter;
82 bool m_attachedToChart;
84 bool m_attachedToChart;
83 bool m_backgroundVisible;
85 bool m_backgroundVisible;
84
86
85 friend class QLegend;
87 friend class QLegend;
86
88
87 };
89 };
88
90
89 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
90
92
91 #endif
93 #endif
General Comments 0
You need to be logged in to leave comments. Login now