@@ -1,151 +1,149 | |||
|
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 | |
|
31 | 31 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | MainWidget::MainWidget(QWidget *parent) : |
|
34 | 34 | QWidget(parent) |
|
35 | 35 | { |
|
36 | 36 | // Create buttons for ui |
|
37 | 37 | m_buttonLayout = new QGridLayout(); |
|
38 | 38 | QPushButton *detachLegendButton = new QPushButton("detach legend"); |
|
39 | 39 | connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend())); |
|
40 | 40 | m_buttonLayout->addWidget(detachLegendButton, 0, 0); |
|
41 | 41 | QPushButton *attachLegendButton = new QPushButton("attach legend"); |
|
42 | 42 | connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend())); |
|
43 | 43 | m_buttonLayout->addWidget(attachLegendButton, 1, 0); |
|
44 | 44 | |
|
45 | 45 | QPushButton *addSetButton = new QPushButton("add barset"); |
|
46 | 46 | connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset())); |
|
47 | 47 | m_buttonLayout->addWidget(addSetButton, 2, 0); |
|
48 | 48 | QPushButton *removeBarsetButton = new QPushButton("remove barset"); |
|
49 | 49 | connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset())); |
|
50 | 50 | m_buttonLayout->addWidget(removeBarsetButton, 3, 0); |
|
51 | 51 | |
|
52 | 52 | // Create chart view with the chart |
|
53 | 53 | //![1] |
|
54 | 54 | m_chart = new QChart(); |
|
55 | 55 | m_chartView = new QChartView(m_chart, this); |
|
56 | 56 | m_chartView->setRubberBand(QChartView::HorizonalRubberBand); |
|
57 | m_chart->setAnimationOptions(QChart::SeriesAnimations); | |
|
58 | 57 | //![1] |
|
59 | 58 | |
|
60 | 59 | // Create custom scene and view, where detached legend will be drawn |
|
61 | 60 | //![2] |
|
62 | 61 | m_customView = new QGraphicsView(this); |
|
63 | 62 | m_customScene = new QGraphicsScene(this); |
|
64 | 63 | m_customView->setScene(m_customScene); |
|
65 | 64 | //![2] |
|
66 | 65 | |
|
67 | 66 | // Create layout for grid and detached legend |
|
68 | 67 | m_mainLayout = new QGridLayout(); |
|
69 | 68 | m_mainLayout->addLayout(m_buttonLayout, 0, 0); |
|
70 | 69 | m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1); |
|
71 | 70 | m_mainLayout->addWidget(m_customView, 0, 2, 3, 1); |
|
72 | 71 | setLayout(m_mainLayout); |
|
73 | 72 | |
|
74 | 73 | createSeries(); |
|
75 | 74 | } |
|
76 | 75 | |
|
77 | 76 | void MainWidget::createSeries() |
|
78 | 77 | { |
|
79 | 78 | //![3] |
|
80 | 79 | m_series = new QBarSeries(); |
|
81 | 80 | addBarset(); |
|
82 | 81 | addBarset(); |
|
83 | 82 | addBarset(); |
|
84 | 83 | addBarset(); |
|
85 | 84 | |
|
86 | 85 | m_chart->addSeries(m_series); |
|
87 | 86 | m_chart->setTitle("Legend detach example"); |
|
88 | 87 | |
|
89 | 88 | m_chart->legend()->setVisible(true); |
|
90 | 89 | m_chart->legend()->setAlignment(Qt::AlignBottom); |
|
91 | 90 | m_chart->axisY()->setNiceNumbersEnabled(true); |
|
92 | 91 | |
|
93 | 92 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
94 | 93 | //![3] |
|
95 | 94 | } |
|
96 | 95 | |
|
97 | 96 | void MainWidget::detachLegend() |
|
98 | 97 | { |
|
99 | 98 | // Detach legend from chart and |
|
100 | 99 | // put legend to our custom scene |
|
101 | 100 | //![4] |
|
102 | 101 | QLegend *legend = m_chart->legend(); |
|
103 | 102 | legend->detachFromChart(); |
|
104 | 103 | legend->setGeometry(m_customView->rect()); |
|
105 | 104 | m_customScene->addItem(legend); |
|
106 | 105 | //![4] |
|
107 | 106 | |
|
108 | 107 | // This forces redraw |
|
109 | 108 | QSize delta(1,1); |
|
110 | 109 | resize(size() + delta); |
|
111 | 110 | resize(size() - delta); |
|
112 | 111 | } |
|
113 | 112 | |
|
114 | 113 | |
|
115 | 114 | void MainWidget::attachLegend() |
|
116 | 115 | { |
|
117 | 116 | // Remove legend from custom scene and put it back to chartview scene. |
|
118 | 117 | // Attach legend back to chart, so that layout works. |
|
119 | 118 | |
|
120 | 119 | //![5] |
|
121 | 120 | QLegend *legend = m_chart->legend(); |
|
122 | 121 | |
|
123 | 122 | if (m_customScene->items().contains(legend)) { |
|
124 | 123 | m_customScene->removeItem(legend); |
|
125 | 124 | m_chartView->scene()->addItem(legend); |
|
126 | 125 | legend->attachToChart(); |
|
127 | 126 | } |
|
128 | 127 | //![5] |
|
129 | 128 | |
|
130 | 129 | // This forces redraw |
|
131 | 130 | QSize delta(1,1); |
|
132 | 131 | resize(size() + delta); |
|
133 | 132 | resize(size() - delta); |
|
134 | 133 | } |
|
135 | 134 | |
|
136 | 135 | void MainWidget::addBarset() |
|
137 | 136 | { |
|
138 | 137 | QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount())); |
|
139 | 138 | qreal delta = m_series->barsetCount() * 0.1; |
|
140 | 139 | *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); |
|
141 | 140 | m_series->append(barSet); |
|
142 | 141 | } |
|
143 | 142 | |
|
144 | 143 | void MainWidget::removeBarset() |
|
145 | 144 | { |
|
146 | m_series->clear(); | |
|
147 | // QList<QBarSet*> sets = m_series->barSets(); | |
|
148 | // if (sets.count() > 0) { | |
|
149 | // m_series->remove(sets.at(sets.count()-1)); | |
|
150 | // } | |
|
145 | QList<QBarSet*> sets = m_series->barSets(); | |
|
146 | if (sets.count() > 0) { | |
|
147 | m_series->remove(sets.at(sets.count()-1)); | |
|
148 | } | |
|
151 | 149 | } |
General Comments 0
You need to be logged in to leave comments.
Login now