##// END OF EJS Templates
legend markers example. bugfix to legend scrolling
sauimone -
r2196:aa4c8d495212
parent child
Show More
@@ -0,0 +1,10
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = legendmarkers
6 SOURCES += main.cpp \
7 mainwidget.cpp
8
9 HEADERS += \
10 mainwidget.h
@@ -0,0 +1,35
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "mainwidget.h"
22
23 #include <QApplication>
24 #include <QMainWindow>
25
26 int main(int argc, char *argv[])
27 {
28 QApplication a(argc, argv);
29
30 MainWidget w;
31 w.resize(720, 480);
32 w.show();
33
34 return a.exec();
35 }
@@ -0,0 +1,181
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "mainwidget.h"
22 #include <QChart>
23 #include <QChartView>
24 #include <QPushButton>
25 #include <QLabel>
26 #include <QDebug>
27 #include <QLegend>
28 #include <QFormLayout>
29 #include <QLegendMarker>
30 #include <QLineSeries>
31 #include <QXYLegendMarker>
32 #include <qmath.h>
33
34 QTCOMMERCIALCHART_USE_NAMESPACE
35
36 MainWidget::MainWidget(QWidget *parent) :
37 QWidget(parent)
38 {
39 // Create buttons for ui
40 m_buttonLayout = new QGridLayout();
41
42 QPushButton *addSliceButton = new QPushButton("add series");
43 connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSeries()));
44 m_buttonLayout->addWidget(addSliceButton, 1, 0);
45
46 QPushButton *removeSliceButton = new QPushButton("remove series");
47 connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
48 m_buttonLayout->addWidget(removeSliceButton, 2, 0);
49
50 QPushButton *connectButton = new QPushButton("Connect markers");
51 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectMarkers()));
52 m_buttonLayout->addWidget(connectButton, 3, 0);
53
54 QPushButton *disConnectButton = new QPushButton("Disconnect markers");
55 connect(disConnectButton, SIGNAL(clicked()), this, SLOT(disconnectMarkers()));
56 m_buttonLayout->addWidget(disConnectButton, 4, 0);
57
58 // Create chart view with the chart
59 m_chart = new QChart();
60 m_chartView = new QChartView(m_chart, this);
61
62 // Create layout for grid and detached legend
63 m_mainLayout = new QGridLayout();
64 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
65 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
66 setLayout(m_mainLayout);
67
68 // Add few series
69 addSeries();
70 addSeries();
71 addSeries();
72 addSeries();
73
74 // Set the title and show legend
75 m_chart->setTitle("Legendmarker example");
76 m_chart->legend()->setVisible(true);
77 m_chart->legend()->setAlignment(Qt::AlignBottom);
78
79 m_chartView->setRenderHint(QPainter::Antialiasing);
80 }
81
82 void MainWidget::addSeries()
83 {
84 QLineSeries *series = new QLineSeries();
85 m_series.append(series);
86
87 series->setName(QString("line " + QString::number(m_series.count())));
88
89 // Make some sine wave for data
90 QList<QPointF> data;
91 int offset = m_chart->series().count();
92 for (int i = 0; i < 360; i++) {
93 qreal x = offset * 20 + i;
94 data.append(QPointF(i, qSin(2.0 * 3.141592 * x / 360.0)));
95 }
96
97 series->append(data);
98 m_chart->addSeries(series);
99
100 if (m_series.count() == 1) {
101 m_chart->createDefaultAxes();
102 }
103 }
104
105 void MainWidget::removeSeries()
106 {
107 // Remove last series from chart
108 if (m_series.count() > 0) {
109 QLineSeries *series = m_series.last();
110 m_chart->removeSeries(series);
111 m_series.removeLast();
112 delete series;
113 }
114 }
115
116 void MainWidget::connectMarkers()
117 {
118 //![1]
119 // Connect all markers to handler
120 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
121 // Disconnect possible existing connection to avoid multiple connections
122 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
123 QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
124 }
125 //![1]
126 }
127
128 void MainWidget::disconnectMarkers()
129 {
130 //![2]
131 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
132 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
133 }
134 //![2]
135 }
136
137 void MainWidget::handleMarkerClicked()
138 {
139 //![3]
140 QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
141 Q_ASSERT(marker);
142 //![3]
143
144
145 //![4]
146 switch (marker->type())
147 //![4]
148 {
149 case QLegendMarker::LegendMarkerTypeXY:
150 {
151 //![5]
152 // Toggle visibility of series
153 marker->series()->setVisible(!marker->series()->isVisible());
154
155 // Turn legend marker back to visible, since hiding series also hides the marker
156 // and we don't want it to happen now.
157 marker->setVisible(true);
158 //![5]
159
160 //![6]
161 // Dim the marker, if series is not visible
162 QXYLegendMarker *xymarker = qobject_cast<QXYLegendMarker *> (marker);
163 QColor color = xymarker->labelBrush().color();
164
165 if (marker->series()->isVisible()) {
166 color.setAlphaF(1.0);
167 } else {
168 color.setAlphaF(0.5);
169 }
170
171 xymarker->setLabelBrush(QBrush(color));
172 //![6]
173 break;
174 }
175 default:
176 {
177 qDebug() << "Unknown marker type";
178 break;
179 }
180 }
181 }
@@ -0,0 +1,63
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef MAINWIDGET_H
22 #define MAINWIDGET_H
23
24 #include "qchartglobal.h"
25 #include "qchart.h"
26 #include "qchartview.h"
27 #include <QWidget>
28 #include <QGraphicsWidget>
29 #include <QGridLayout>
30 #include <QGraphicsGridLayout>
31 #include <QDoubleSpinBox>
32 #include <QGroupBox>
33 #include <QLineSeries>
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
37 class MainWidget : public QWidget
38 {
39 Q_OBJECT
40 public:
41 explicit MainWidget(QWidget *parent = 0);
42
43 public slots:
44 void addSeries();
45 void removeSeries();
46 void connectMarkers();
47 void disconnectMarkers();
48
49 void handleMarkerClicked();
50
51 private:
52
53 QChart *m_chart;
54 QList<QLineSeries *> m_series;
55
56 QChartView *m_chartView;
57 QGridLayout *m_mainLayout;
58 QGridLayout *m_buttonLayout;
59 QGridLayout *m_fontLayout;
60
61 };
62
63 #endif // MAINWIDGET_H
@@ -33,7 +33,8 SUBDIRS += \
33 33 donutchart \
34 34 multiaxis \
35 35 callout \
36 newlegend
36 newlegend \
37 legendmarkers
37 38
38 39 !linux-arm*: {
39 40 SUBDIRS += \
@@ -30,7 +30,9
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 LegendLayout::LegendLayout(QLegend *legend)
33 : m_legend(legend)
33 : m_legend(legend),
34 m_offsetX(0),
35 m_offsetY(0)
34 36 {
35 37
36 38 }
@@ -112,6 +114,8 void LegendLayout::setAttachedGeometry(const QRectF &rect)
112 114 if (!rect.isValid())
113 115 return;
114 116
117 qreal oldOffsetX = m_offsetX;
118 qreal oldOffsetY = m_offsetY;
115 119 m_offsetX = 0;
116 120 m_offsetY = 0;
117 121
@@ -181,6 +185,8 void LegendLayout::setAttachedGeometry(const QRectF &rect)
181 185 m_minOffsetY = - top;
182 186 m_maxOffsetX = m_width - geometry.width() - right;
183 187 m_maxOffsetY = m_height - geometry.height() - bottom;
188
189 setOffset(oldOffsetX, oldOffsetY);
184 190 }
185 191
186 192 void LegendLayout::setDettachedGeometry(const QRectF &rect)
@@ -193,6 +199,8 void LegendLayout::setDettachedGeometry(const QRectF &rect)
193 199 // differ a log from attached mode.
194 200 // Also the scrolling logic is bit different.
195 201
202 qreal oldOffsetX = m_offsetX;
203 qreal oldOffsetY = m_offsetY;
196 204 m_offsetX = 0;
197 205 m_offsetY = 0;
198 206
@@ -350,6 +358,7 void LegendLayout::setDettachedGeometry(const QRectF &rect)
350 358 break;
351 359 }
352 360
361 setOffset(oldOffsetX, oldOffsetY);
353 362 }
354 363
355 364 QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
General Comments 0
You need to be logged in to leave comments. Login now