##// END OF EJS Templates
new legend example for testing new api. Currently using still the old one.
sauimone -
r2162:abb4622b2dab
parent child
Show More
@@ -0,0 +1,37
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 QTCOMMERCIALCHART_USE_NAMESPACE
27
28 int main(int argc, char *argv[])
29 {
30 QApplication a(argc, argv);
31
32 MainWidget w;
33 w.resize(720, 480);
34 w.show();
35
36 return a.exec();
37 }
@@ -0,0 +1,246
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 <QPieSeries>
30 #include <QPieSlice>
31
32 QTCOMMERCIALCHART_USE_NAMESPACE
33
34 MainWidget::MainWidget(QWidget *parent) :
35 QWidget(parent)
36 {
37 // Create buttons for ui
38 m_buttonLayout = new QGridLayout();
39 QPushButton *detachLegendButton = new QPushButton("Toggle attached");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached()));
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42
43 QPushButton *addSliceButton = new QPushButton("add slice");
44 connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSlice()));
45 m_buttonLayout->addWidget(addSliceButton, 2, 0);
46 QPushButton *removeSliceButton = new QPushButton("remove slice");
47 connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSlice()));
48 m_buttonLayout->addWidget(removeSliceButton, 3, 0);
49
50 QPushButton *alignButton = new QPushButton("Align (Bottom)");
51 connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment()));
52 m_buttonLayout->addWidget(alignButton, 4, 0);
53
54 QPushButton *boldButton = new QPushButton("Toggle bold");
55 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
56 m_buttonLayout->addWidget(boldButton, 8, 0);
57
58 QPushButton *italicButton = new QPushButton("Toggle italic");
59 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
60 m_buttonLayout->addWidget(italicButton, 9, 0);
61
62 m_legendPosX = new QDoubleSpinBox();
63 m_legendPosY = new QDoubleSpinBox();
64 m_legendWidth = new QDoubleSpinBox();
65 m_legendHeight = new QDoubleSpinBox();
66
67 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
68 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
69 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
70 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
71
72 QFormLayout* legendLayout = new QFormLayout();
73 legendLayout->addRow("HPos", m_legendPosX);
74 legendLayout->addRow("VPos", m_legendPosY);
75 legendLayout->addRow("Width", m_legendWidth);
76 legendLayout->addRow("Height", m_legendHeight);
77 m_legendSettings = new QGroupBox("Detached legend");
78 m_legendSettings->setLayout(legendLayout);
79 m_buttonLayout->addWidget(m_legendSettings);
80 m_legendSettings->setVisible(false);
81
82 // Create chart view with the chart
83 m_chart = new QChart();
84 m_chartView = new QChartView(m_chart, this);
85
86 // Create spinbox to modify font size
87 m_fontSize = new QDoubleSpinBox();
88 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
89 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
90
91 QFormLayout* fontLayout = new QFormLayout();
92 fontLayout->addRow("Legend font size", m_fontSize);
93
94 // Create layout for grid and detached legend
95 m_mainLayout = new QGridLayout();
96 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
97 m_mainLayout->addLayout(fontLayout,1,0);
98 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
99 setLayout(m_mainLayout);
100
101 createSeries();
102 }
103
104 void MainWidget::createSeries()
105 {
106 m_series = new QPieSeries();
107 addSlice();
108 addSlice();
109 addSlice();
110 addSlice();
111
112 m_chart->addSeries(m_series);
113 m_chart->setTitle("Legend detach example");
114 m_chart->createDefaultAxes();
115 //![1]
116 m_chart->legend()->setVisible(true);
117 m_chart->legend()->setAlignment(Qt::AlignBottom);
118 //![1]
119
120 m_chartView->setRenderHint(QPainter::Antialiasing);
121 }
122
123 void MainWidget::showLegendSpinbox()
124 {
125 m_legendSettings->setVisible(true);
126 QRectF chartViewRect = m_chartView->rect();
127
128 m_legendPosX->setMinimum(0);
129 m_legendPosX->setMaximum(chartViewRect.width());
130 m_legendPosX->setValue(150);
131
132 m_legendPosY->setMinimum(0);
133 m_legendPosY->setMaximum(chartViewRect.height());
134 m_legendPosY->setValue(150);
135
136 m_legendWidth->setMinimum(0);
137 m_legendWidth->setMaximum(chartViewRect.width());
138 m_legendWidth->setValue(150);
139
140 m_legendHeight->setMinimum(0);
141 m_legendHeight->setMaximum(chartViewRect.height());
142 m_legendHeight->setValue(75);
143 }
144
145 void MainWidget::hideLegendSpinbox()
146 {
147 m_legendSettings->setVisible(false);
148 }
149
150
151 void MainWidget::toggleAttached()
152 {
153 QLegend *legend = m_chart->legend();
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 }
170 update();
171 }
172
173 void MainWidget::addSlice()
174 {
175 QPieSlice* slice = new QPieSlice(QString("slice " + QString::number(m_series->count())), m_series->count()+1);
176 // slice->setValue();
177 m_series->append(slice);
178 }
179
180 void MainWidget::removeSlice()
181 {
182 QList<QPieSlice*> slices = m_series->slices();
183 if (slices.count() > 0) {
184 m_series->remove(slices.at(slices.count()-1));
185 }
186 }
187
188 void MainWidget::setLegendAlignment()
189 {
190 QPushButton *button = qobject_cast<QPushButton *>(sender());
191
192 switch (m_chart->legend()->alignment()) {
193 case Qt::AlignTop:
194 m_chart->legend()->setAlignment(Qt::AlignLeft);
195 if (button)
196 button->setText("Align (Left)");
197 break;
198 case Qt::AlignLeft:
199 m_chart->legend()->setAlignment(Qt::AlignBottom);
200 if (button)
201 button->setText("Align (Bottom)");
202 break;
203 case Qt::AlignBottom:
204 m_chart->legend()->setAlignment(Qt::AlignRight);
205 if (button)
206 button->setText("Align (Right)");
207 break;
208 default:
209 if (button)
210 button->setText("Align (Top)");
211 m_chart->legend()->setAlignment(Qt::AlignTop);
212 break;
213 }
214 }
215
216 void MainWidget::toggleBold()
217 {
218 QFont font = m_chart->legend()->font();
219 font.setBold(!font.bold());
220 m_chart->legend()->setFont(font);
221 }
222
223 void MainWidget::toggleItalic()
224 {
225 QFont font = m_chart->legend()->font();
226 font.setItalic(!font.italic());
227 m_chart->legend()->setFont(font);
228 }
229
230 void MainWidget::fontSizeChanged()
231 {
232 QFont font = m_chart->legend()->font();
233 font.setPointSizeF(m_fontSize->value());
234 m_chart->legend()->setFont(font);
235 }
236
237 void MainWidget::updateLegendLayout()
238 {
239 //![4]
240 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
241 ,m_legendPosY->value()
242 ,m_legendWidth->value()
243 ,m_legendHeight->value()));
244 m_chart->legend()->update();
245 //![4]
246 }
@@ -0,0 +1,83
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 <QPieSlice>
34 #include <QPieSeries>
35
36 QTCOMMERCIALCHART_USE_NAMESPACE
37
38 class MainWidget : public QWidget
39 {
40 Q_OBJECT
41 public:
42 explicit MainWidget(QWidget *parent = 0);
43 void createSeries();
44 void showLegendSpinbox();
45 void hideLegendSpinbox();
46
47 signals:
48
49 public slots:
50 void toggleAttached();
51 void addSlice();
52 void removeSlice();
53
54 void setLegendAlignment();
55
56 void toggleBold();
57 void toggleItalic();
58 void fontSizeChanged();
59
60 void updateLegendLayout();
61
62
63 private:
64
65 QChart *m_chart;
66 QPieSeries *m_series;
67
68 QChartView *m_chartView;
69 QGridLayout *m_mainLayout;
70 QGridLayout *m_buttonLayout;
71 QGridLayout *m_fontLayout;
72
73 QDoubleSpinBox *m_fontSize;
74
75 // For detached layout
76 QGroupBox* m_legendSettings;
77 QDoubleSpinBox *m_legendPosX;
78 QDoubleSpinBox *m_legendPosY;
79 QDoubleSpinBox *m_legendWidth;
80 QDoubleSpinBox *m_legendHeight;
81 };
82
83 #endif // MAINWIDGET_H
@@ -0,0 +1,10
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = newlegend
6 SOURCES += main.cpp \
7 mainwidget.cpp
8
9 HEADERS += \
10 mainwidget.h
@@ -0,0 +1,36
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 "qpielegendmarker.h"
22 #include <QPieSeries>
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
26 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
27 QLegendMarker(series, parent),
28 m_slice(slice)
29 {
30 }
31
32
33 #include "moc_qpielegendmarker.cpp"
34 //#include "moc_qpielegendmarker_p.cpp"
35
36 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,53
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 QPIELEGENDMARKER_H
22 #define QPIELEGENDMARKER_H
23
24 #include <QChartGlobal>
25 #include <QLegendMarker>
26 #include <QPieSlice>
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker
31 {
32 Q_OBJECT
33
34 public:
35 explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0);
36
37 virtual QPieSlice* peerObject() { return m_slice; }
38
39 //Q_SIGNALS:
40
41 //public Q_SLOTS:
42
43 private:
44 // TODO:
45 // QScopedPointer<QPieLegendMarkerPrivate> d_ptr;
46 Q_DISABLE_COPY(QPieLegendMarker)
47
48 QPieSlice* m_slice;
49
50 };
51
52 QTCOMMERCIALCHART_END_NAMESPACE
53 #endif // QPIELEGENDMARKER_H
@@ -32,7 +32,8 SUBDIRS += \
32 32 temperaturerecords \
33 33 donutchart \
34 34 multiaxis \
35 callout
35 callout \
36 newlegend
36 37
37 38 !linux-arm*: {
38 39 SUBDIRS += \
@@ -5,7 +5,8 SOURCES += \
5 5 $$PWD/qlegend.cpp \
6 6 $$PWD/legendmarker.cpp \
7 7 $$PWD/legendlayout.cpp \
8 $$PWD/qlegendmarker.cpp
8 $$PWD/qlegendmarker.cpp \
9 $$PWD/qpielegendmarker.cpp
9 10
10 11 PRIVATE_HEADERS += \
11 12 $$PWD/legendmarker_p.h \
@@ -16,4 +17,5 PRIVATE_HEADERS += \
16 17
17 18 PUBLIC_HEADERS += \
18 19 $$PWD/qlegend.h \
19 $$PWD/qlegendmarker.h
20 $$PWD/qlegendmarker.h \
21 $$PWD/qpielegendmarker.h
@@ -24,8 +24,9
24 24
25 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 26
27 QLegendMarker::QLegendMarker(QObject *parent) :
28 QObject(parent)//,
27 QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) :
28 QObject(parent),
29 m_series(series)
29 30 // d_ptr(new QLegendMarkerPrivate(this))
30 31 {
31 32 }
@@ -30,6 +30,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 // TODO:
32 32 //class QLegendMarkerPrivate;
33 class QAbstractSeries;
33 34
34 35 // TODO: should this be abstract?
35 36 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
@@ -42,7 +43,7 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
42 43 // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged);
43 44
44 45 public:
45 explicit QLegendMarker(QObject *parent = 0);
46 explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0);
46 47 virtual ~QLegendMarker();
47 48
48 49 QString label() const;
@@ -57,6 +58,10 public:
57 58 bool isVisible() const;
58 59 void setVisible(bool visible);
59 60
61 // virtual QAbstractSeries::SeriesType type() = 0;
62 virtual QAbstractSeries* series() { return m_series; }
63 virtual QObject* peerObject() = 0;
64
60 65 Q_SIGNALS:
61 66 void clicked();
62 67 void hovered(bool status);
@@ -70,6 +75,7 public:
70 75 Q_DISABLE_COPY(QLegendMarker)
71 76
72 77 // TODO: move to PIMPL
78 QAbstractSeries* m_series;
73 79 QString m_label;
74 80 QPen m_pen;
75 81 QBrush m_brush;
General Comments 0
You need to be logged in to leave comments. Login now