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