##// END OF EJS Templates
Legend disabled by defaut. User can turn in on, by calling setVisible
sauimone -
r652:4ab0d58be448
parent child
Show More
@@ -1,150 +1,149
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qstackedbarseries.h>
5 #include <qstackedbarseries.h>
6 #include <qbarset.h>
6 #include <qbarset.h>
7 #include <qchartaxis.h>
7 #include <qchartaxis.h>
8 #include <qlegend.h>
9 #include <QStringList>
8 #include <QStringList>
10 #include <QDebug>
9 #include <QDebug>
11
10
12 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
13
12
14 //! [1]
13 //! [1]
15 class DrilldownBarSeries : public QStackedBarSeries
14 class DrilldownBarSeries : public QStackedBarSeries
16 {
15 {
17 Q_OBJECT
16 Q_OBJECT
18 public:
17 public:
19 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
18 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
20
19
21 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
20 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
22 {
21 {
23 mDrilldownSeries[category] = drilldownSeries;
22 mDrilldownSeries[category] = drilldownSeries;
24 }
23 }
25
24
26 DrilldownBarSeries* drilldownSeries(QString category)
25 DrilldownBarSeries* drilldownSeries(QString category)
27 {
26 {
28 return mDrilldownSeries[category];
27 return mDrilldownSeries[category];
29 }
28 }
30
29
31 public Q_SLOTS:
30 public Q_SLOTS:
32
31
33 private:
32 private:
34
33
35 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
34 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
36 };
35 };
37 //! [1]
36 //! [1]
38
37
39 //! [2]
38 //! [2]
40 class DrilldownChart : public QChartView
39 class DrilldownChart : public QChartView
41 {
40 {
42 Q_OBJECT
41 Q_OBJECT
43 public:
42 public:
44 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
43 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
45
44
46 void changeSeries(QSeries* series)
45 void changeSeries(QSeries* series)
47 {
46 {
48 if (m_currentSeries)
47 if (m_currentSeries)
49 removeSeries(m_currentSeries);
48 removeSeries(m_currentSeries);
50 m_currentSeries = series;
49 m_currentSeries = series;
51 addSeries(series);
50 addSeries(series);
52 setChartTitle(series->title());
51 setChartTitle(series->title());
53 }
52 }
54
53
55 public Q_SLOTS:
54 public Q_SLOTS:
56 void handleRightClick(QBarSet *barset, QString category)
55 void handleRightClick(QBarSet *barset, QString category)
57 {
56 {
58 Q_UNUSED(barset)
57 Q_UNUSED(barset)
59 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
60 changeSeries(series->drilldownSeries(category));
59 changeSeries(series->drilldownSeries(category));
61 }
60 }
62
61
63 private:
62 private:
64 QSeries* m_currentSeries;
63 QSeries* m_currentSeries;
65 };
64 };
66 //! [2]
65 //! [2]
67
66
68 int main(int argc, char *argv[])
67 int main(int argc, char *argv[])
69 {
68 {
70 QApplication a(argc, argv);
69 QApplication a(argc, argv);
71 QMainWindow window;
70 QMainWindow window;
72
71
73 DrilldownChart* drilldownChart = new DrilldownChart(&window);
72 DrilldownChart* drilldownChart = new DrilldownChart(&window);
74 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
73 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
75
74
76 //! [3]
75 //! [3]
77 // Define categories
76 // Define categories
78 QStringList months;
77 QStringList months;
79 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
80 QStringList weeks;
79 QStringList weeks;
81 weeks << "week 1" << "week 2" << "week 3" << "week 4";
80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
82 QStringList plants;
81 QStringList plants;
83 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
82 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
84 //! [3]
83 //! [3]
85
84
86 //! [4]
85 //! [4]
87 // Create drilldown structure
86 // Create drilldown structure
88 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
87 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
89 seasonSeries->setTitle("Crop by month - Season");
88 seasonSeries->setTitle("Crop by month - Season");
90
89
91 // Each month in season series has drilldown series for weekly data
90 // Each month in season series has drilldown series for weekly data
92 foreach (QString month, months) {
91 foreach (QString month, months) {
93
92
94 // Create drilldown series for every week
93 // Create drilldown series for every week
95 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
96 seasonSeries->mapDrilldownSeries(month, weeklySeries);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
97
96
98 // Drilling down from weekly data brings us back to season data.
97 // Drilling down from weekly data brings us back to season data.
99 foreach (QString week, weeks) {
98 foreach (QString week, weeks) {
100 weeklySeries->mapDrilldownSeries(week, seasonSeries);
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
101 weeklySeries->setTitle(QString("Crop by week - " + month));
100 weeklySeries->setTitle(QString("Crop by week - " + month));
102 }
101 }
103
102
104 // Use right click signal to implement drilldown
103 // Use right click signal to implement drilldown
105 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
104 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
106 }
105 }
107
106
108 // Enable drilldown from season series using right click.
107 // Enable drilldown from season series using right click.
109 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
108 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
110 //! [4]
109 //! [4]
111
110
112 //! [5]
111 //! [5]
113 // Fill monthly and weekly series with data
112 // Fill monthly and weekly series with data
114 foreach (QString plant, plants) {
113 foreach (QString plant, plants) {
115 QBarSet* monthlyCrop = new QBarSet(plant);
114 QBarSet* monthlyCrop = new QBarSet(plant);
116 foreach (QString month, months) {
115 foreach (QString month, months) {
117 QBarSet* weeklyCrop = new QBarSet(plant);
116 QBarSet* weeklyCrop = new QBarSet(plant);
118 foreach (QString week, weeks ) {
117 foreach (QString week, weeks ) {
119 *weeklyCrop << (qrand() % 20);
118 *weeklyCrop << (qrand() % 20);
120 }
119 }
121 // Get the drilldown series from season series and add crop to it.
120 // Get the drilldown series from season series and add crop to it.
122 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
123 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
124 *monthlyCrop << weeklyCrop->total();
123 *monthlyCrop << weeklyCrop->total();
125
124
126 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
125 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
127 }
126 }
128 seasonSeries->addBarSet(monthlyCrop);
127 seasonSeries->addBarSet(monthlyCrop);
129 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
128 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
130 }
129 }
131 //! [5]
130 //! [5]
132
131
133 seasonSeries->setToolTipEnabled(true);
132 seasonSeries->setToolTipEnabled(true);
134
133
135 //! [6]
134 //! [6]
136 // Show season series in initial view
135 // Show season series in initial view
137 drilldownChart->changeSeries(seasonSeries);
136 drilldownChart->changeSeries(seasonSeries);
138 drilldownChart->setChartTitle(seasonSeries->title());
137 drilldownChart->setChartTitle(seasonSeries->title());
139 //! [6]
138 //! [6]
140
139
141 drilldownChart->axisX()->setGridLineVisible(false);
140 drilldownChart->axisX()->setGridLineVisible(false);
142
141
143 window.setCentralWidget(drilldownChart);
142 window.setCentralWidget(drilldownChart);
144 window.resize(400, 300);
143 window.resize(400, 300);
145 window.show();
144 window.show();
146
145
147 return a.exec();
146 return a.exec();
148 }
147 }
149
148
150 #include "main.moc"
149 #include "main.moc"
@@ -1,395 +1,396
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "chartpresenter_p.h"
4 #include "chartpresenter_p.h"
5 #include "chartdataset_p.h"
5 #include "chartdataset_p.h"
6 #include "chartbackground_p.h"
6 #include "chartbackground_p.h"
7 #include <QGraphicsScene>
7 #include <QGraphicsScene>
8 #include <QGraphicsSceneResizeEvent>
8 #include <QGraphicsSceneResizeEvent>
9 #include <QDebug>
9 #include <QDebug>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 /*!
13 /*!
14 \enum QChart::ChartTheme
14 \enum QChart::ChartTheme
15
15
16 This enum describes the theme used by the chart.
16 This enum describes the theme used by the chart.
17
17
18 \value ChartThemeDefault Follows the GUI style of the Operating System
18 \value ChartThemeDefault Follows the GUI style of the Operating System
19 \value ChartThemeVanilla
19 \value ChartThemeVanilla
20 \value ChartThemeIcy
20 \value ChartThemeIcy
21 \value ChartThemeGrayscale
21 \value ChartThemeGrayscale
22 \value ChartThemeScientific
22 \value ChartThemeScientific
23 \value ChartThemeBlueCerulean
23 \value ChartThemeBlueCerulean
24 \value ChartThemeLight
24 \value ChartThemeLight
25 \value ChartThemeCount Not really a theme; the total count of themes.
25 \value ChartThemeCount Not really a theme; the total count of themes.
26 */
26 */
27
27
28 /*!
28 /*!
29 \enum QChart::AnimationOption
29 \enum QChart::AnimationOption
30
30
31 For enabling/disabling animations. Defaults to NoAnimation.
31 For enabling/disabling animations. Defaults to NoAnimation.
32
32
33 \value NoAnimation
33 \value NoAnimation
34 \value GridAxisAnimations
34 \value GridAxisAnimations
35 \value SeriesAnimations
35 \value SeriesAnimations
36 \value AllAnimations
36 \value AllAnimations
37 */
37 */
38
38
39 /*!
39 /*!
40 \class QChart
40 \class QChart
41 \brief QtCommercial chart API.
41 \brief QtCommercial chart API.
42
42
43 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
43 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
44 representation of different types of QChartSeries and other chart related objects like
44 representation of different types of QChartSeries and other chart related objects like
45 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
45 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
46 convenience class QChartView instead of QChart.
46 convenience class QChartView instead of QChart.
47 \sa QChartView
47 \sa QChartView
48 */
48 */
49
49
50 /*!
50 /*!
51 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
51 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
52 */
52 */
53 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
53 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
54 m_backgroundItem(0),
54 m_backgroundItem(0),
55 m_titleItem(0),
55 m_titleItem(0),
56 m_legend(new QLegend(this)),
56 m_legend(new QLegend(this)),
57 m_dataset(new ChartDataSet(this)),
57 m_dataset(new ChartDataSet(this)),
58 m_presenter(new ChartPresenter(this,m_dataset)),
58 m_presenter(new ChartPresenter(this,m_dataset)),
59 m_padding(50),
59 m_padding(50),
60 m_backgroundPadding(10)
60 m_backgroundPadding(10)
61 {
61 {
62 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
62 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
63 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
63 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
64 }
64 }
65
65
66 /*!
66 /*!
67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
68 */
68 */
69 QChart::~QChart()
69 QChart::~QChart()
70 {
70 {
71 }
71 }
72
72
73 /*!
73 /*!
74 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
75 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
76 the y axis).
76 the y axis).
77 */
77 */
78 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
78 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
79 {
79 {
80 m_dataset->addSeries(series, axisY);
80 m_dataset->addSeries(series, axisY);
81 }
81 }
82
82
83 /*!
83 /*!
84 Removes the \a series specified in a perameter from the QChartView.
84 Removes the \a series specified in a perameter from the QChartView.
85 It releses its ownership of the specified QChartSeries object.
85 It releses its ownership of the specified QChartSeries object.
86 It does not delete the pointed QChartSeries data object
86 It does not delete the pointed QChartSeries data object
87 \sa addSeries(), removeAllSeries()
87 \sa addSeries(), removeAllSeries()
88 */
88 */
89 void QChart::removeSeries(QSeries* series)
89 void QChart::removeSeries(QSeries* series)
90 {
90 {
91 m_dataset->removeSeries(series);
91 m_dataset->removeSeries(series);
92 }
92 }
93
93
94 /*!
94 /*!
95 Removes all the QChartSeries that have been added to the QChartView
95 Removes all the QChartSeries that have been added to the QChartView
96 It also deletes the pointed QChartSeries data objects
96 It also deletes the pointed QChartSeries data objects
97 \sa addSeries(), removeSeries()
97 \sa addSeries(), removeSeries()
98 */
98 */
99 void QChart::removeAllSeries()
99 void QChart::removeAllSeries()
100 {
100 {
101 m_dataset->removeAllSeries();
101 m_dataset->removeAllSeries();
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets the \a brush that is used for painting the background of the chart area.
105 Sets the \a brush that is used for painting the background of the chart area.
106 */
106 */
107 void QChart::setBackgroundBrush(const QBrush& brush)
107 void QChart::setBackgroundBrush(const QBrush& brush)
108 {
108 {
109 createChartBackgroundItem();
109 createChartBackgroundItem();
110 m_backgroundItem->setBrush(brush);
110 m_backgroundItem->setBrush(brush);
111 m_backgroundItem->update();
111 m_backgroundItem->update();
112 }
112 }
113
113
114 QBrush QChart::backgroundBrush() const
114 QBrush QChart::backgroundBrush() const
115 {
115 {
116 if(!m_backgroundItem) return QBrush();
116 if(!m_backgroundItem) return QBrush();
117 return m_backgroundItem->brush();
117 return m_backgroundItem->brush();
118 }
118 }
119
119
120 /*!
120 /*!
121 Sets the \a pen that is used for painting the background of the chart area.
121 Sets the \a pen that is used for painting the background of the chart area.
122 */
122 */
123 void QChart::setBackgroundPen(const QPen& pen)
123 void QChart::setBackgroundPen(const QPen& pen)
124 {
124 {
125 createChartBackgroundItem();
125 createChartBackgroundItem();
126 m_backgroundItem->setPen(pen);
126 m_backgroundItem->setPen(pen);
127 m_backgroundItem->update();
127 m_backgroundItem->update();
128 }
128 }
129
129
130 QPen QChart::backgroundPen() const
130 QPen QChart::backgroundPen() const
131 {
131 {
132 if(!m_backgroundItem) return QPen();
132 if(!m_backgroundItem) return QPen();
133 return m_backgroundItem->pen();
133 return m_backgroundItem->pen();
134 }
134 }
135
135
136 /*!
136 /*!
137 Sets the chart \a title. The description text that is drawn above the chart.
137 Sets the chart \a title. The description text that is drawn above the chart.
138 */
138 */
139 void QChart::setTitle(const QString& title)
139 void QChart::setTitle(const QString& title)
140 {
140 {
141 createChartTitleItem();
141 createChartTitleItem();
142 m_titleItem->setText(title);
142 m_titleItem->setText(title);
143 updateLayout();
143 updateLayout();
144 }
144 }
145
145
146 /*!
146 /*!
147 Returns the chart title. The description text that is drawn above the chart.
147 Returns the chart title. The description text that is drawn above the chart.
148 */
148 */
149 QString QChart::title() const
149 QString QChart::title() const
150 {
150 {
151 if(m_titleItem)
151 if(m_titleItem)
152 return m_titleItem->text();
152 return m_titleItem->text();
153 else
153 else
154 return QString();
154 return QString();
155 }
155 }
156
156
157 /*!
157 /*!
158 Sets the \a font that is used for rendering the description text that is rendered above the chart.
158 Sets the \a font that is used for rendering the description text that is rendered above the chart.
159 */
159 */
160 void QChart::setTitleFont(const QFont& font)
160 void QChart::setTitleFont(const QFont& font)
161 {
161 {
162 createChartTitleItem();
162 createChartTitleItem();
163 m_titleItem->setFont(font);
163 m_titleItem->setFont(font);
164 updateLayout();
164 updateLayout();
165 }
165 }
166
166
167 /*!
167 /*!
168 Sets the \a brush used for rendering the title text.
168 Sets the \a brush used for rendering the title text.
169 */
169 */
170 void QChart::setTitleBrush(const QBrush &brush)
170 void QChart::setTitleBrush(const QBrush &brush)
171 {
171 {
172 createChartTitleItem();
172 createChartTitleItem();
173 m_titleItem->setBrush(brush);
173 m_titleItem->setBrush(brush);
174 updateLayout();
174 updateLayout();
175 }
175 }
176
176
177 /*!
177 /*!
178 Returns the brush used for rendering the title text.
178 Returns the brush used for rendering the title text.
179 */
179 */
180 QBrush QChart::titleBrush() const
180 QBrush QChart::titleBrush() const
181 {
181 {
182 if(!m_titleItem) return QBrush();
182 if(!m_titleItem) return QBrush();
183 return m_titleItem->brush();
183 return m_titleItem->brush();
184 }
184 }
185
185
186 void QChart::createChartBackgroundItem()
186 void QChart::createChartBackgroundItem()
187 {
187 {
188 if(!m_backgroundItem) {
188 if(!m_backgroundItem) {
189 m_backgroundItem = new ChartBackground(this);
189 m_backgroundItem = new ChartBackground(this);
190 m_backgroundItem->setPen(Qt::NoPen);
190 m_backgroundItem->setPen(Qt::NoPen);
191 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
191 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
192 }
192 }
193 }
193 }
194
194
195 void QChart::createChartTitleItem()
195 void QChart::createChartTitleItem()
196 {
196 {
197 if(!m_titleItem) {
197 if(!m_titleItem) {
198 m_titleItem = new QGraphicsSimpleTextItem(this);
198 m_titleItem = new QGraphicsSimpleTextItem(this);
199 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
199 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
200 }
200 }
201 }
201 }
202
202
203 /*!
203 /*!
204 Sets the \a theme used by the chart for rendering the graphical representation of the data
204 Sets the \a theme used by the chart for rendering the graphical representation of the data
205 \sa ChartTheme, chartTheme()
205 \sa ChartTheme, chartTheme()
206 */
206 */
207 void QChart::setChartTheme(QChart::ChartTheme theme)
207 void QChart::setChartTheme(QChart::ChartTheme theme)
208 {
208 {
209 m_presenter->setChartTheme(theme);
209 m_presenter->setChartTheme(theme);
210 }
210 }
211
211
212 /*!
212 /*!
213 Returns the theme enum used by the chart.
213 Returns the theme enum used by the chart.
214 \sa ChartTheme, setChartTheme()
214 \sa ChartTheme, setChartTheme()
215 */
215 */
216 QChart::ChartTheme QChart::chartTheme() const
216 QChart::ChartTheme QChart::chartTheme() const
217 {
217 {
218 return m_presenter->chartTheme();
218 return m_presenter->chartTheme();
219 }
219 }
220
220
221 /*!
221 /*!
222 Zooms in the view by a factor of 2
222 Zooms in the view by a factor of 2
223 */
223 */
224 void QChart::zoomIn()
224 void QChart::zoomIn()
225 {
225 {
226 m_presenter->zoomIn();
226 m_presenter->zoomIn();
227 }
227 }
228
228
229 /*!
229 /*!
230 Zooms in the view to a maximum level at which \a rect is still fully visible.
230 Zooms in the view to a maximum level at which \a rect is still fully visible.
231 */
231 */
232 void QChart::zoomIn(const QRectF& rect)
232 void QChart::zoomIn(const QRectF& rect)
233 {
233 {
234
234
235 if(!rect.isValid()) return;
235 if(!rect.isValid()) return;
236 m_presenter->zoomIn(rect);
236 m_presenter->zoomIn(rect);
237 }
237 }
238
238
239 /*!
239 /*!
240 Restores the view zoom level to the previous one.
240 Restores the view zoom level to the previous one.
241 */
241 */
242 void QChart::zoomOut()
242 void QChart::zoomOut()
243 {
243 {
244 m_presenter->zoomOut();
244 m_presenter->zoomOut();
245 }
245 }
246
246
247 /*!
247 /*!
248 Resets to the default view.
248 Resets to the default view.
249 */
249 */
250 void QChart::zoomReset()
250 void QChart::zoomReset()
251 {
251 {
252 m_presenter->zoomReset();
252 m_presenter->zoomReset();
253 }
253 }
254
254
255 /*!
255 /*!
256 Returns the pointer to the x axis object of the chart
256 Returns the pointer to the x axis object of the chart
257 */
257 */
258 QChartAxis* QChart::axisX() const
258 QChartAxis* QChart::axisX() const
259 {
259 {
260 return m_dataset->axisX();
260 return m_dataset->axisX();
261 }
261 }
262
262
263 /*!
263 /*!
264 Returns the pointer to the y axis object of the chart
264 Returns the pointer to the y axis object of the chart
265 */
265 */
266 QChartAxis* QChart::axisY() const
266 QChartAxis* QChart::axisY() const
267 {
267 {
268 return m_dataset->axisY();
268 return m_dataset->axisY();
269 }
269 }
270
270
271 /*!
271 /*!
272 Returns the legend object of the chart
272 Returns the legend object of the chart. Ownership stays in chart.
273 */
273 */
274 QLegend* QChart::legend()
274 QLegend* QChart::legend() const
275 {
275 {
276 return m_legend;
276 return m_legend;
277 }
277 }
278
278
279 /*!
279 /*!
280 Resizes and updates the chart area using the \a event data
280 Resizes and updates the chart area using the \a event data
281 */
281 */
282 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
282 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
283 {
283 {
284
284
285 m_rect = QRectF(QPoint(0,0),event->newSize());
285 m_rect = QRectF(QPoint(0,0),event->newSize());
286 updateLayout();
286 updateLayout();
287 QGraphicsWidget::resizeEvent(event);
287 QGraphicsWidget::resizeEvent(event);
288 update();
288 update();
289 }
289 }
290
290
291 /*!
291 /*!
292 Sets animation \a options for the chart
292 Sets animation \a options for the chart
293 */
293 */
294 void QChart::setAnimationOptions(AnimationOptions options)
294 void QChart::setAnimationOptions(AnimationOptions options)
295 {
295 {
296 m_presenter->setAnimationOptions(options);
296 m_presenter->setAnimationOptions(options);
297 }
297 }
298
298
299 /*!
299 /*!
300 Returns animation options for the chart
300 Returns animation options for the chart
301 */
301 */
302 QChart::AnimationOptions QChart::animationOptions() const
302 QChart::AnimationOptions QChart::animationOptions() const
303 {
303 {
304 return m_presenter->animationOptions();
304 return m_presenter->animationOptions();
305 }
305 }
306
306
307 void QChart::scrollLeft()
307 void QChart::scrollLeft()
308 {
308 {
309 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
309 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
310 }
310 }
311
311
312 void QChart::scrollRight()
312 void QChart::scrollRight()
313 {
313 {
314 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
314 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
315 }
315 }
316 void QChart::scrollUp()
316 void QChart::scrollUp()
317 {
317 {
318 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
318 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
319 }
319 }
320 void QChart::scrollDown()
320 void QChart::scrollDown()
321 {
321 {
322 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
322 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
323 }
323 }
324
324
325 void QChart::updateLayout()
325 void QChart::updateLayout()
326 {
326 {
327 if(!m_rect.isValid()) return;
327 if(!m_rect.isValid()) return;
328
328
329 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
329 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
330
330
331 // recalculate title position
331 // recalculate title position
332 if (m_titleItem) {
332 if (m_titleItem) {
333 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
333 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
334 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
334 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
335 }
335 }
336
336
337 //recalculate background gradient
337 //recalculate background gradient
338 if (m_backgroundItem) {
338 if (m_backgroundItem) {
339 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
339 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
340 }
340 }
341
341
342 // recalculate legend position
342 // recalculate legend position
343 if (m_legend) {
343 if (m_legend) {
344 m_legend->setMaximumSize(rect.size());
344 if (m_legend->parentObject() == this) {
345 m_legend->setPos(rect.topLeft());
345 m_legend->setMaximumSize(rect.size());
346 m_legend->setPreferredLayout(QLegend::PreferredLayoutHorizontal);
346 m_legend->setPos(rect.topLeft());
347 }
347 }
348 }
348 }
349 }
349
350
350
351
351 int QChart::padding() const
352 int QChart::padding() const
352 {
353 {
353 return m_padding;
354 return m_padding;
354 }
355 }
355
356
356 void QChart::setPadding(int padding)
357 void QChart::setPadding(int padding)
357 {
358 {
358 if(m_padding==padding){
359 if(m_padding==padding){
359 m_padding = padding;
360 m_padding = padding;
360 m_presenter->handleGeometryChanged();
361 m_presenter->handleGeometryChanged();
361 updateLayout();
362 updateLayout();
362 }
363 }
363 }
364 }
364
365
365 void QChart::setBackgroundPadding(int padding)
366 void QChart::setBackgroundPadding(int padding)
366 {
367 {
367 if(m_backgroundPadding!=padding){
368 if(m_backgroundPadding!=padding){
368 m_backgroundPadding = padding;
369 m_backgroundPadding = padding;
369 updateLayout();
370 updateLayout();
370 }
371 }
371 }
372 }
372
373
373 void QChart::setBackgroundDiameter(int diameter)
374 void QChart::setBackgroundDiameter(int diameter)
374 {
375 {
375 createChartBackgroundItem();
376 createChartBackgroundItem();
376 m_backgroundItem->setDimeter(diameter);
377 m_backgroundItem->setDimeter(diameter);
377 m_backgroundItem->update();
378 m_backgroundItem->update();
378 }
379 }
379
380
380 void QChart::setBackgroundVisible(bool visible)
381 void QChart::setBackgroundVisible(bool visible)
381 {
382 {
382 createChartBackgroundItem();
383 createChartBackgroundItem();
383 m_backgroundItem->setVisible(visible);
384 m_backgroundItem->setVisible(visible);
384 }
385 }
385
386
386 bool QChart::isBackgroundVisible() const
387 bool QChart::isBackgroundVisible() const
387 {
388 {
388 if(!m_backgroundItem) return false;
389 if(!m_backgroundItem) return false;
389 return m_backgroundItem->isVisible();
390 return m_backgroundItem->isVisible();
390 }
391 }
391
392
392
393
393 #include "moc_qchart.cpp"
394 #include "moc_qchart.cpp"
394
395
395 QTCOMMERCIALCHART_END_NAMESPACE
396 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,126 +1,124
1 #ifndef QCHART_H
1 #ifndef QCHART_H
2 #define QCHART_H
2 #define QCHART_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qseries.h>
5 #include <qseries.h>
6 #include <QGraphicsWidget>
6 #include <QGraphicsWidget>
7 #include <QLinearGradient>
7 #include <QLinearGradient>
8 #include <QFont>
8 #include <QFont>
9
9
10 class QGraphicsSceneResizeEvent;
10 class QGraphicsSceneResizeEvent;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class AxisItem;
14 class AxisItem;
15 class QSeries;
15 class QSeries;
16 class PlotDomain;
16 class PlotDomain;
17 class BarPresenter;
17 class BarPresenter;
18 class QChartAxis;
18 class QChartAxis;
19 class ChartTheme;
19 class ChartTheme;
20 class ChartItem;
20 class ChartItem;
21 class ChartDataSet;
21 class ChartDataSet;
22 class ChartPresenter;
22 class ChartPresenter;
23 class QLegend;
23 class QLegend;
24 class ChartBackground;
24 class ChartBackground;
25
25
26
26
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
28 {
28 {
29 Q_OBJECT
29 Q_OBJECT
30 public:
30 public:
31 enum ChartTheme {
31 enum ChartTheme {
32 ChartThemeDefault,
32 ChartThemeDefault,
33 ChartThemeLight,
33 ChartThemeLight,
34 ChartThemeBlueCerulean,
34 ChartThemeBlueCerulean,
35 ChartThemeDark,
35 ChartThemeDark,
36 ChartThemeBrownSand,
36 ChartThemeBrownSand,
37 ChartThemeBlueNcs,
37 ChartThemeBlueNcs,
38 ChartThemeVanilla,
38 ChartThemeVanilla,
39 ChartThemeIcy,
39 ChartThemeIcy,
40 ChartThemeGrayscale,
40 ChartThemeGrayscale,
41 ChartThemeScientific,
41 ChartThemeScientific,
42 ChartThemeCount
42 ChartThemeCount
43 };
43 };
44
44
45 enum AnimationOption {
45 enum AnimationOption {
46 NoAnimation = 0x0,
46 NoAnimation = 0x0,
47 GridAxisAnimations = 0x1,
47 GridAxisAnimations = 0x1,
48 SeriesAnimations =0x2,
48 SeriesAnimations =0x2,
49 AllAnimations = 0x3
49 AllAnimations = 0x3
50 };
50 };
51 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
51 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
52
52
53 public:
53 public:
54 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
54 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
55 ~QChart();
55 ~QChart();
56
56
57 void addSeries(QSeries* series, QChartAxis* axisY = 0);
57 void addSeries(QSeries* series, QChartAxis* axisY = 0);
58 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
58 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
59 void removeAllSeries(); // deletes series and axis
59 void removeAllSeries(); // deletes series and axis
60
60
61 void setChartTheme(QChart::ChartTheme theme);
61 void setChartTheme(QChart::ChartTheme theme);
62 QChart::ChartTheme chartTheme() const;
62 QChart::ChartTheme chartTheme() const;
63
63
64 void setTitle(const QString& title);
64 void setTitle(const QString& title);
65 QString title() const;
65 QString title() const;
66 void setTitleFont(const QFont& font);
66 void setTitleFont(const QFont& font);
67 QFont titleFont() const;
67 QFont titleFont() const;
68 void setTitleBrush(const QBrush &brush);
68 void setTitleBrush(const QBrush &brush);
69 QBrush titleBrush() const;
69 QBrush titleBrush() const;
70 void setBackgroundBrush(const QBrush& brush);
70 void setBackgroundBrush(const QBrush& brush);
71 QBrush backgroundBrush() const;
71 QBrush backgroundBrush() const;
72 void setBackgroundPen(const QPen& pen);
72 void setBackgroundPen(const QPen& pen);
73 QPen backgroundPen() const;
73 QPen backgroundPen() const;
74
74
75 void setBackgroundVisible(bool visible);
75 void setBackgroundVisible(bool visible);
76 bool isBackgroundVisible() const;
76 bool isBackgroundVisible() const;
77
77
78 void setAnimationOptions(AnimationOptions options);
78 void setAnimationOptions(AnimationOptions options);
79 AnimationOptions animationOptions() const;
79 AnimationOptions animationOptions() const;
80
80
81 void zoomIn();
81 void zoomIn();
82 void zoomIn(const QRectF& rect);
82 void zoomIn(const QRectF& rect);
83 void zoomOut();
83 void zoomOut();
84 void zoomReset();
84 void zoomReset();
85 void scrollLeft();
85 void scrollLeft();
86 void scrollRight();
86 void scrollRight();
87 void scrollUp();
87 void scrollUp();
88 void scrollDown();
88 void scrollDown();
89
89
90 QChartAxis* axisX() const;
90 QChartAxis* axisX() const;
91 QChartAxis* axisY() const;
91 QChartAxis* axisY() const;
92
92
93 // TODO: take (and give) legend instead of this.
93 QLegend* legend() const;
94 QLegend* legend();
95
96
94
97 int padding() const;
95 int padding() const;
98
96
99 protected:
97 protected:
100 void resizeEvent(QGraphicsSceneResizeEvent *event);
98 void resizeEvent(QGraphicsSceneResizeEvent *event);
101
99
102 private:
100 private:
103 inline void createChartBackgroundItem();
101 inline void createChartBackgroundItem();
104 inline void createChartTitleItem();
102 inline void createChartTitleItem();
105 void setPadding(int padding);
103 void setPadding(int padding);
106 void setBackgroundPadding(int padding);
104 void setBackgroundPadding(int padding);
107 void setBackgroundDiameter(int diameter);
105 void setBackgroundDiameter(int diameter);
108 void updateLayout();
106 void updateLayout();
109
107
110 private:
108 private:
111 Q_DISABLE_COPY(QChart)
109 Q_DISABLE_COPY(QChart)
112 ChartBackground* m_backgroundItem;
110 ChartBackground* m_backgroundItem;
113 QGraphicsSimpleTextItem* m_titleItem;
111 QGraphicsSimpleTextItem* m_titleItem;
114 QRectF m_rect;
112 QRectF m_rect;
115 QLegend* m_legend;
113 QLegend* m_legend;
116 ChartDataSet *m_dataset;
114 ChartDataSet *m_dataset;
117 ChartPresenter *m_presenter;
115 ChartPresenter *m_presenter;
118 int m_padding;
116 int m_padding;
119 int m_backgroundPadding;
117 int m_backgroundPadding;
120 };
118 };
121
119
122 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
123
121
124 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
122 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
125
123
126 #endif
124 #endif
@@ -1,492 +1,491
1 #include "qchartglobal.h"
1 #include "qchartglobal.h"
2 #include "qlegend.h"
2 #include "qlegend.h"
3 #include "qseries.h"
3 #include "qseries.h"
4 #include "legendmarker_p.h"
4 #include "legendmarker_p.h"
5 #include "qxyseries.h"
5 #include "qxyseries.h"
6 #include "qlineseries.h"
6 #include "qlineseries.h"
7 #include "qareaseries.h"
7 #include "qareaseries.h"
8 #include "qscatterseries.h"
8 #include "qscatterseries.h"
9 #include "qsplineseries.h"
9 #include "qsplineseries.h"
10 #include "qbarseries.h"
10 #include "qbarseries.h"
11 #include "qstackedbarseries.h"
11 #include "qstackedbarseries.h"
12 #include "qpercentbarseries.h"
12 #include "qpercentbarseries.h"
13 #include "qbarset.h"
13 #include "qbarset.h"
14 #include "qpieseries.h"
14 #include "qpieseries.h"
15 #include "qpieslice.h"
15 #include "qpieslice.h"
16 #include "chartpresenter_p.h"
16 #include "chartpresenter_p.h"
17 #include <QPainter>
17 #include <QPainter>
18 #include <QPen>
18 #include <QPen>
19
19
20 #include <QGraphicsSceneEvent>
20 #include <QGraphicsSceneEvent>
21
21
22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23
23
24 QLegend::QLegend(QGraphicsItem *parent)
24 QLegend::QLegend(QGraphicsItem *parent)
25 : QGraphicsObject(parent)
25 : QGraphicsObject(parent)
26 ,mPos(0,0)
26 ,mPos(0,0)
27 ,mSize(0,0)
27 ,mSize(0,0)
28 ,mMinimumSize(50,20) // TODO: magic numbers
28 ,mMinimumSize(50,20) // TODO: magic numbers
29 ,mMaximumSize(150,100)
29 ,mMaximumSize(150,100)
30 ,m_brush(Qt::darkGray) // TODO: from theme?
30 ,m_brush(Qt::darkGray) // TODO: from theme?
31 ,mPreferredLayout(QLegend::PreferredLayoutVertical)
31 ,mPreferredLayout(QLegend::PreferredLayoutVertical)
32 {
32 {
33 // setVisible(false);
33 setVisible(false);
34 setZValue(ChartPresenter::LegendZValue);
34 setZValue(ChartPresenter::LegendZValue);
35 }
35 }
36
36
37 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
38 {
38 {
39 Q_UNUSED(option)
39 Q_UNUSED(option)
40 Q_UNUSED(widget)
40 Q_UNUSED(widget)
41
41
42 painter->setOpacity(0.5);
42 painter->setOpacity(0.5);
43 painter->setPen(m_pen);
43 painter->setPen(m_pen);
44 painter->setBrush(m_brush);
44 painter->setBrush(m_brush);
45 painter->drawRect(boundingRect());
45 painter->drawRect(boundingRect());
46 }
46 }
47
47
48 QRectF QLegend::boundingRect() const
48 QRectF QLegend::boundingRect() const
49 {
49 {
50 return QRectF(mPos,mSize);
50 return QRectF(mPos,mSize);
51 }
51 }
52
52
53 void QLegend::setBrush(const QBrush& brush)
53 void QLegend::setBrush(const QBrush& brush)
54 {
54 {
55 if(m_brush!=brush){
55 if(m_brush!=brush){
56 m_brush = brush;
56 m_brush = brush;
57 update();
57 update();
58 }
58 }
59 }
59 }
60
60
61 QBrush QLegend::brush() const
61 QBrush QLegend::brush() const
62 {
62 {
63 return m_brush;
63 return m_brush;
64 }
64 }
65
65
66 void QLegend::setPen(const QPen& pen)
66 void QLegend::setPen(const QPen& pen)
67 {
67 {
68 if(m_pen!=pen){
68 if(m_pen!=pen){
69 m_pen = pen;
69 m_pen = pen;
70 update();
70 update();
71 }
71 }
72 }
72 }
73
73
74 QPen QLegend::pen() const
74 QPen QLegend::pen() const
75 {
75 {
76 return m_pen;
76 return m_pen;
77 }
77 }
78
78
79 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
79 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
80 {
80 {
81 mPreferredLayout = preferred;
81 mPreferredLayout = preferred;
82 layoutChanged();
82 layoutChanged();
83 }
83 }
84
84
85 QSizeF QLegend::maximumSize() const
85 QSizeF QLegend::maximumSize() const
86 {
86 {
87 return mMaximumSize;
87 return mMaximumSize;
88 }
88 }
89
89
90 void QLegend::setMaximumSize(const QSizeF size)
90 void QLegend::setMaximumSize(const QSizeF size)
91 {
91 {
92 mMaximumSize = size;
92 mMaximumSize = size;
93 layoutChanged();
93 }
94 }
94
95
95 void QLegend::setSize(const QSizeF size)
96 void QLegend::setSize(const QSizeF size)
96 {
97 {
97 mSize = size;
98 mSize = size;
98 if (mSize.width() > mMaximumSize.width()) {
99 if (mSize.width() > mMaximumSize.width()) {
99 mSize.setWidth(mMaximumSize.width());
100 mSize.setWidth(mMaximumSize.width());
100 }
101 }
101 if (mSize.height() > mMaximumSize.height()) {
102 if (mSize.height() > mMaximumSize.height()) {
102 mSize.setHeight(mMaximumSize.height());
103 mSize.setHeight(mMaximumSize.height());
103 }
104 }
104 }
105 }
105
106
106 void QLegend::setPos(const QPointF &pos)
107 void QLegend::setPos(const QPointF &pos)
107 {
108 {
108 mPos = pos;
109 mPos = pos;
110 layoutChanged();
109 }
111 }
110
112
111 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
113 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
112 {
114 {
113 Q_UNUSED(domain)
115 Q_UNUSED(domain)
114
116
115 // mSeriesList.append(series);
116 createMarkers(series);
117 createMarkers(series);
117 connectSeries(series);
118 connectSeries(series);
118 layoutChanged();
119 layoutChanged();
119 }
120 }
120
121
121 void QLegend::handleSeriesRemoved(QSeries* series)
122 void QLegend::handleSeriesRemoved(QSeries* series)
122 {
123 {
123 disconnectSeries(series);
124 disconnectSeries(series);
124
125
125 if (series->type() == QSeries::SeriesTypeArea)
126 if (series->type() == QSeries::SeriesTypeArea)
126 {
127 {
127 // This is special case. Area series has upper and lower series, which each have markers
128 // This is special case. Area series has upper and lower series, which each have markers
128 QAreaSeries* s = static_cast<QAreaSeries*> (series);
129 QAreaSeries* s = static_cast<QAreaSeries*> (series);
129 deleteMarkers(s->upperSeries());
130 deleteMarkers(s->upperSeries());
130 deleteMarkers(s->lowerSeries());
131 deleteMarkers(s->lowerSeries());
131 } else {
132 } else {
132 deleteMarkers(series);
133 deleteMarkers(series);
133 }
134 }
134
135
135 // mSeriesList.removeOne(series);
136 layoutChanged();
136 layoutChanged();
137 }
137 }
138
138
139 void QLegend::handleAdded(QList<QPieSlice*> slices)
139 void QLegend::handleAdded(QList<QPieSlice*> slices)
140 {
140 {
141 QPieSeries* series = static_cast<QPieSeries*> (sender());
141 QPieSeries* series = static_cast<QPieSeries*> (sender());
142 foreach(QPieSlice* s, slices) {
142 foreach(QPieSlice* s, slices) {
143 LegendMarker* marker = new LegendMarker(series,s,this);
143 LegendMarker* marker = new LegendMarker(series,s,this);
144 marker->setName(s->label());
144 marker->setName(s->label());
145 marker->setBrush(s->sliceBrush());
145 marker->setBrush(s->sliceBrush());
146 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
146 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
147 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
147 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
148 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
148 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
149 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
149 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
150 mMarkers.append(marker);
150 mMarkers.append(marker);
151 childItems().append(marker);
151 childItems().append(marker);
152 }
152 }
153 layoutChanged();
153 layoutChanged();
154 }
154 }
155
155
156 void QLegend::handleRemoved(QList<QPieSlice *> slices)
156 void QLegend::handleRemoved(QList<QPieSlice *> slices)
157 {
157 {
158 Q_UNUSED(slices)
158 Q_UNUSED(slices)
159 // Propably no need to listen for this, since removed slices are deleted and we listen destroyed signal
159 // Propably no need to listen for this, since removed slices are deleted and we listen destroyed signal
160 // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count();
160 // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count();
161 }
161 }
162
162
163
163
164 void QLegend::handleMarkerDestroyed()
164 void QLegend::handleMarkerDestroyed()
165 {
165 {
166 // TODO: what if more than one markers are destroyed and we update layout after first one?
166 // TODO: what if more than one markers are destroyed and we update layout after first one?
167 LegendMarker* m = static_cast<LegendMarker*> (sender());
167 LegendMarker* m = static_cast<LegendMarker*> (sender());
168 mMarkers.removeOne(m);
168 mMarkers.removeOne(m);
169 layoutChanged();
169 layoutChanged();
170 }
170 }
171
171
172 void QLegend::connectSeries(QSeries *series)
172 void QLegend::connectSeries(QSeries *series)
173 {
173 {
174 // Connect relevant signals from series
174 // Connect relevant signals from series
175 switch (series->type())
175 switch (series->type())
176 {
176 {
177 case QSeries::SeriesTypeLine: {
177 case QSeries::SeriesTypeLine: {
178 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
178 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
179 break;
179 break;
180 }
180 }
181 case QSeries::SeriesTypeArea: {
181 case QSeries::SeriesTypeArea: {
182 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
182 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
183 break;
183 break;
184 }
184 }
185 case QSeries::SeriesTypeBar: {
185 case QSeries::SeriesTypeBar: {
186 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
186 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
187 break;
187 break;
188 }
188 }
189 case QSeries::SeriesTypeStackedBar: {
189 case QSeries::SeriesTypeStackedBar: {
190 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
190 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
191 break;
191 break;
192 }
192 }
193 case QSeries::SeriesTypePercentBar: {
193 case QSeries::SeriesTypePercentBar: {
194 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
194 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
195 break;
195 break;
196 }
196 }
197 case QSeries::SeriesTypeScatter: {
197 case QSeries::SeriesTypeScatter: {
198 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
198 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
199 break;
199 break;
200 }
200 }
201 case QSeries::SeriesTypePie: {
201 case QSeries::SeriesTypePie: {
202 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
202 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
203 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
203 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
204 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
204 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
205 break;
205 break;
206 }
206 }
207 case QSeries::SeriesTypeSpline: {
207 case QSeries::SeriesTypeSpline: {
208 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
208 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
209 break;
209 break;
210 }
210 }
211 default: {
211 default: {
212 qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented.";
212 qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented.";
213 break;
213 break;
214 }
214 }
215 }
215 }
216 }
216 }
217
217
218 void QLegend::disconnectSeries(QSeries *series)
218 void QLegend::disconnectSeries(QSeries *series)
219 {
219 {
220 // Connect relevant signals from series
220 // Connect relevant signals from series
221 switch (series->type())
221 switch (series->type())
222 {
222 {
223 case QSeries::SeriesTypeLine: {
223 case QSeries::SeriesTypeLine: {
224 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
224 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
225 break;
225 break;
226 }
226 }
227 case QSeries::SeriesTypeArea: {
227 case QSeries::SeriesTypeArea: {
228 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
228 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
229 break;
229 break;
230 }
230 }
231 case QSeries::SeriesTypeBar: {
231 case QSeries::SeriesTypeBar: {
232 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
232 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
233 break;
233 break;
234 }
234 }
235 case QSeries::SeriesTypeStackedBar: {
235 case QSeries::SeriesTypeStackedBar: {
236 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
236 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
237 break;
237 break;
238 }
238 }
239 case QSeries::SeriesTypePercentBar: {
239 case QSeries::SeriesTypePercentBar: {
240 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
240 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
241 break;
241 break;
242 }
242 }
243 case QSeries::SeriesTypeScatter: {
243 case QSeries::SeriesTypeScatter: {
244 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
244 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
245 break;
245 break;
246 }
246 }
247 case QSeries::SeriesTypePie: {
247 case QSeries::SeriesTypePie: {
248 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
248 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
249 disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
249 disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
250 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
250 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
251 break;
251 break;
252 }
252 }
253 case QSeries::SeriesTypeSpline: {
253 case QSeries::SeriesTypeSpline: {
254 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
254 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
255 break;
255 break;
256 }
256 }
257 default: {
257 default: {
258 qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
258 qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
259 break;
259 break;
260 }
260 }
261 }
261 }
262 }
262 }
263
263
264 void QLegend::createMarkers(QSeries *series)
264 void QLegend::createMarkers(QSeries *series)
265 {
265 {
266 switch (series->type())
266 switch (series->type())
267 {
267 {
268 case QSeries::SeriesTypeLine: {
268 case QSeries::SeriesTypeLine: {
269 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
269 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
270 appendMarkers(lineSeries);
270 appendMarkers(lineSeries);
271 break;
271 break;
272 }
272 }
273 case QSeries::SeriesTypeArea: {
273 case QSeries::SeriesTypeArea: {
274 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
274 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
275 appendMarkers(areaSeries->upperSeries());
275 appendMarkers(areaSeries->upperSeries());
276 if(areaSeries->lowerSeries())
276 if(areaSeries->lowerSeries())
277 appendMarkers(areaSeries->lowerSeries());
277 appendMarkers(areaSeries->lowerSeries());
278 break;
278 break;
279 }
279 }
280
280
281 case QSeries::SeriesTypeBar: {
281 case QSeries::SeriesTypeBar: {
282 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
282 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
283 appendMarkers(barSeries);
283 appendMarkers(barSeries);
284 break;
284 break;
285 }
285 }
286
286
287 case QSeries::SeriesTypeStackedBar: {
287 case QSeries::SeriesTypeStackedBar: {
288 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
288 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
289 appendMarkers(stackedBarSeries);
289 appendMarkers(stackedBarSeries);
290 break;
290 break;
291 }
291 }
292
292
293 case QSeries::SeriesTypePercentBar: {
293 case QSeries::SeriesTypePercentBar: {
294 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
294 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
295 appendMarkers(percentBarSeries);
295 appendMarkers(percentBarSeries);
296 break;
296 break;
297 }
297 }
298
298
299 case QSeries::SeriesTypeScatter: {
299 case QSeries::SeriesTypeScatter: {
300 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
300 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
301 appendMarkers(scatterSeries);
301 appendMarkers(scatterSeries);
302 break;
302 break;
303 }
303 }
304
304
305 case QSeries::SeriesTypePie: {
305 case QSeries::SeriesTypePie: {
306 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
306 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
307 appendMarkers(pieSeries);
307 appendMarkers(pieSeries);
308 break;
308 break;
309 }
309 }
310
310
311 case QSeries::SeriesTypeSpline: {
311 case QSeries::SeriesTypeSpline: {
312 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
312 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
313 appendMarkers(splineSeries);
313 appendMarkers(splineSeries);
314 break;
314 break;
315 }
315 }
316 default: {
316 default: {
317 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
317 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
318 break;
318 break;
319 }
319 }
320 }
320 }
321 }
321 }
322
322
323 void QLegend::appendMarkers(QXYSeries* series)
323 void QLegend::appendMarkers(QXYSeries* series)
324 {
324 {
325 LegendMarker* marker = new LegendMarker(series,this);
325 LegendMarker* marker = new LegendMarker(series,this);
326 marker->setName(series->name());
326 marker->setName(series->name());
327 marker->setBrush(series->brush());
327 marker->setBrush(series->brush());
328 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
328 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
329 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
329 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
330 mMarkers.append(marker);
330 mMarkers.append(marker);
331 childItems().append(marker);
331 childItems().append(marker);
332 }
332 }
333
333
334 void QLegend::appendMarkers(QBarSeries *series)
334 void QLegend::appendMarkers(QBarSeries *series)
335 {
335 {
336 foreach(QBarSet* s, series->barSets()) {
336 foreach(QBarSet* s, series->barSets()) {
337 LegendMarker* marker = new LegendMarker(series,s,this);
337 LegendMarker* marker = new LegendMarker(series,s,this);
338 marker->setName(s->name());
338 marker->setName(s->name());
339 marker->setBrush(s->brush());
339 marker->setBrush(s->brush());
340 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
340 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
341 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
341 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
342 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
342 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
343 mMarkers.append(marker);
343 mMarkers.append(marker);
344 childItems().append(marker);
344 childItems().append(marker);
345 }
345 }
346 }
346 }
347
347
348 void QLegend::appendMarkers(QPieSeries *series)
348 void QLegend::appendMarkers(QPieSeries *series)
349 {
349 {
350 foreach(QPieSlice* s, series->slices()) {
350 foreach(QPieSlice* s, series->slices()) {
351 LegendMarker* marker = new LegendMarker(series,s,this);
351 LegendMarker* marker = new LegendMarker(series,s,this);
352 marker->setName(s->label());
352 marker->setName(s->label());
353 marker->setBrush(s->sliceBrush());
353 marker->setBrush(s->sliceBrush());
354 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
354 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
355 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
355 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
356 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
356 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
357 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
357 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
358 mMarkers.append(marker);
358 mMarkers.append(marker);
359 childItems().append(marker);
359 childItems().append(marker);
360 }
360 }
361 }
361 }
362
362
363 void QLegend::deleteMarkers(QSeries *series)
363 void QLegend::deleteMarkers(QSeries *series)
364 {
364 {
365 // Search all markers that belong to given series and delete them.
365 // Search all markers that belong to given series and delete them.
366 foreach (LegendMarker *m, mMarkers) {
366 foreach (LegendMarker *m, mMarkers) {
367 if (m->series() == series) {
367 if (m->series() == series) {
368 mMarkers.removeOne(m);
368 mMarkers.removeOne(m);
369 delete m;
369 delete m;
370 }
370 }
371 }
371 }
372 }
372 }
373
373
374 void QLegend::layoutChanged()
374 void QLegend::layoutChanged()
375 {
375 {
376 // Calculate layout for markers and text
376 // Calculate layout for markers and text
377 qDebug() << "Marker count:" << mMarkers.count();
378 if (mMarkers.count() <= 0) {
377 if (mMarkers.count() <= 0) {
379 // Nothing to do
378 // Nothing to do
380 return;
379 return;
381 }
380 }
382
381
383 // Find out widest item.
382 // Find out widest item.
384 qreal itemMaxWidth = 0;
383 qreal itemMaxWidth = 0;
385 qreal itemMaxHeight = 0;
384 qreal itemMaxHeight = 0;
386 foreach (LegendMarker* m, mMarkers) {
385 foreach (LegendMarker* m, mMarkers) {
387 if (m->boundingRect().width() > itemMaxWidth) {
386 if (m->boundingRect().width() > itemMaxWidth) {
388 itemMaxWidth = m->boundingRect().width();
387 itemMaxWidth = m->boundingRect().width();
389 }
388 }
390 if (m->boundingRect().height() > itemMaxHeight) {
389 if (m->boundingRect().height() > itemMaxHeight) {
391 itemMaxHeight = m->boundingRect().height();
390 itemMaxHeight = m->boundingRect().height();
392 }
391 }
393 }
392 }
394
393
395 int maxHorizontalItems = boundingRect().width() / itemMaxWidth;
394 int maxHorizontalItems = boundingRect().width() / itemMaxWidth;
396 int maxVerticalItems = boundingRect().height() / itemMaxHeight;
395 int maxVerticalItems = boundingRect().height() / itemMaxHeight;
397
396
398 if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) {
397 if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) {
399 // TODO: overlapping layout
398 // TODO: overlapping layout
400 qDebug() << "Warning. Not enough space to layout all legend items properly.";
399 qDebug() << "Warning. Not enough space to layout all legend items properly.";
401 }
400 }
402
401
403 qreal margin = 5;
402 qreal margin = 5;
404 qreal totalWidth = 0;
403 qreal totalWidth = 0;
405 qreal totalHeight = 0;
404 qreal totalHeight = 0;
406 switch (mPreferredLayout)
405 switch (mPreferredLayout)
407 {
406 {
408 case QLegend::PreferredLayoutHorizontal: {
407 case QLegend::PreferredLayoutHorizontal: {
409 /*
408 /*
410 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
409 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
411 if (xStep > itemMaxWidth) {
410 if (xStep > itemMaxWidth) {
412 xStep = itemMaxWidth;
411 xStep = itemMaxWidth;
413 }
412 }
414 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
413 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
415 if (yStep > itemMaxHeight) {
414 if (yStep > itemMaxHeight) {
416 yStep = itemMaxHeight;
415 yStep = itemMaxHeight;
417 }*/
416 }*/
418 qreal xStep = itemMaxWidth;
417 qreal xStep = itemMaxWidth;
419 qreal yStep = itemMaxHeight;
418 qreal yStep = itemMaxHeight;
420 qreal x = mPos.x() + margin;
419 qreal x = mPos.x() + margin;
421 qreal y = mPos.y() + margin;
420 qreal y = mPos.y() + margin;
422 int row = 1;
421 int row = 1;
423 int column = 0;
422 int column = 0;
424 int maxRows = 1;
423 int maxRows = 1;
425 int maxColumns = 1;
424 int maxColumns = 1;
426 foreach (LegendMarker* m, mMarkers) {
425 foreach (LegendMarker* m, mMarkers) {
427 maxRows = row;
426 maxRows = row;
428 m->setPos(x,y);
427 m->setPos(x,y);
429 x += xStep;
428 x += xStep;
430 column++;
429 column++;
431 if (column > maxColumns) {
430 if (column > maxColumns) {
432 maxColumns = column;
431 maxColumns = column;
433 }
432 }
434 if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) {
433 if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) {
435 x = mPos.x() + margin;
434 x = mPos.x() + margin;
436 y += yStep;
435 y += yStep;
437 row++;
436 row++;
438 column = 0;
437 column = 0;
439 }
438 }
440 }
439 }
441 totalWidth = maxColumns * itemMaxWidth + margin * 2;
440 totalWidth = maxColumns * itemMaxWidth + margin * 2;
442 totalHeight = maxRows * itemMaxHeight + margin * 2;
441 totalHeight = maxRows * itemMaxHeight + margin * 2;
443 break;
442 break;
444 }
443 }
445 case QLegend::PreferredLayoutVertical: {
444 case QLegend::PreferredLayoutVertical: {
446 /*
445 /*
447 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
446 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
448 if (xStep > itemMaxWidth) {
447 if (xStep > itemMaxWidth) {
449 xStep = itemMaxWidth;
448 xStep = itemMaxWidth;
450 }
449 }
451 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
450 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
452 if (yStep > itemMaxHeight) {
451 if (yStep > itemMaxHeight) {
453 yStep = itemMaxHeight;
452 yStep = itemMaxHeight;
454 }*/
453 }*/
455 qreal xStep = itemMaxWidth;
454 qreal xStep = itemMaxWidth;
456 qreal yStep = itemMaxHeight;
455 qreal yStep = itemMaxHeight;
457 qreal x = mPos.x() + margin;
456 qreal x = mPos.x() + margin;
458 qreal y = mPos.y() + margin;
457 qreal y = mPos.y() + margin;
459 int row = 0;
458 int row = 0;
460 int column = 1;
459 int column = 1;
461 int maxRows = 1;
460 int maxRows = 1;
462 int maxColumns = 1;
461 int maxColumns = 1;
463 foreach (LegendMarker* m, mMarkers) {
462 foreach (LegendMarker* m, mMarkers) {
464 maxColumns = column;
463 maxColumns = column;
465 m->setPos(x,y);
464 m->setPos(x,y);
466 y += yStep;
465 y += yStep;
467 row++;
466 row++;
468 if (row > maxRows) {
467 if (row > maxRows) {
469 maxRows = row;
468 maxRows = row;
470 }
469 }
471 if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) {
470 if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) {
472 y = mPos.y() + margin;
471 y = mPos.y() + margin;
473 x += xStep;
472 x += xStep;
474 column++;
473 column++;
475 row = 0;
474 row = 0;
476 }
475 }
477 }
476 }
478 totalWidth = maxColumns * itemMaxWidth + margin * 2;
477 totalWidth = maxColumns * itemMaxWidth + margin * 2;
479 totalHeight = maxRows * itemMaxHeight + margin * 2;
478 totalHeight = maxRows * itemMaxHeight + margin * 2;
480 break;
479 break;
481 }
480 }
482 default: {
481 default: {
483 break;
482 break;
484 }
483 }
485 }
484 }
486
485
487 mSize.setWidth(totalWidth);
486 mSize.setWidth(totalWidth);
488 mSize.setHeight(totalHeight);
487 mSize.setHeight(totalHeight);
489 }
488 }
490
489
491 #include "moc_qlegend.cpp"
490 #include "moc_qlegend.cpp"
492 QTCOMMERCIALCHART_END_NAMESPACE
491 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,86
1 #ifndef QLEGEND_H
1 #ifndef QLEGEND_H
2 #define QLEGEND_H
2 #define QLEGEND_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QGraphicsObject>
6 #include <QGraphicsObject>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class Domain;
10 class Domain;
11 class LegendMarker;
11 class LegendMarker;
12 class QPieSlice;
12 class QPieSlice;
13 class QXYSeries;
13 class QXYSeries;
14 class QBarSet;
14 class QBarSet;
15 class QBarSeries;
15 class QBarSeries;
16 class QPieSeries;
16 class QPieSeries;
17
17
18 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
18 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
19 {
19 {
20 Q_OBJECT
20 Q_OBJECT
21 public:
21 public:
22
22
23 enum PreferredLayout {
23 enum PreferredLayout {
24 PreferredLayoutHorizontal,
24 PreferredLayoutHorizontal,
25 PreferredLayoutVertical
25 PreferredLayoutVertical
26 };
26 };
27
27
28 explicit QLegend(QGraphicsItem *parent = 0);
28 explicit QLegend(QGraphicsItem *parent = 0);
29
29
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
31 QRectF boundingRect() const;
31 QRectF boundingRect() const;
32
32
33 void setBrush(const QBrush& brush);
33 void setBrush(const QBrush& brush);
34 QBrush brush() const;
34 QBrush brush() const;
35
35
36 void setPen(const QPen& pen);
36 void setPen(const QPen& pen);
37 QPen pen() const;
37 QPen pen() const;
38
38
39 void setPreferredLayout(QLegend::PreferredLayout preferred);
39 void setPreferredLayout(QLegend::PreferredLayout preferred);
40
40
41 QSizeF maximumSize() const;
41 QSizeF maximumSize() const;
42 void setMaximumSize(const QSizeF size);
42 void setMaximumSize(const QSizeF size);
43
43
44 void setSize(const QSizeF size);
44 void setSize(const QSizeF size);
45 void setPos(const QPointF &pos);
45 void setPos(const QPointF &pos);
46
46
47 signals:
47 signals:
48 // for interactions.
48 // for interactions.
49 void clicked(QSeries* series, Qt::MouseButton button);
49 void clicked(QSeries* series, Qt::MouseButton button);
50 void clicked(QBarSet* barset, Qt::MouseButton button);
50 void clicked(QBarSet* barset, Qt::MouseButton button);
51 void clicked(QPieSlice* slice, Qt::MouseButton button);
51 void clicked(QPieSlice* slice, Qt::MouseButton button);
52
52
53 public slots:
53 public slots:
54 void handleSeriesAdded(QSeries* series,Domain* domain);
54 void handleSeriesAdded(QSeries* series,Domain* domain);
55 void handleSeriesRemoved(QSeries* series);
55 void handleSeriesRemoved(QSeries* series);
56 void handleAdded(QList<QPieSlice*> slices);
56 void handleAdded(QList<QPieSlice*> slices);
57 void handleRemoved(QList<QPieSlice*> slices);
57 void handleRemoved(QList<QPieSlice*> slices);
58 void handleMarkerDestroyed();
58 void handleMarkerDestroyed();
59
59
60 private:
60 private:
61 // PIMPL --->
61 // PIMPL --->
62 void connectSeries(QSeries* series);
62 void connectSeries(QSeries* series);
63 void disconnectSeries(QSeries* series);
63 void disconnectSeries(QSeries* series);
64 void createMarkers(QSeries* series);
64 void createMarkers(QSeries* series);
65 void appendMarkers(QXYSeries* series); // All line series are derived from QXYSeries, so this works for now
65 void appendMarkers(QXYSeries* series); // All line series are derived from QXYSeries, so this works for now
66 void appendMarkers(QBarSeries* series);
66 void appendMarkers(QBarSeries* series);
67 void appendMarkers(QPieSeries* series);
67 void appendMarkers(QPieSeries* series);
68 void deleteMarkers(QSeries* series);
68 void deleteMarkers(QSeries* series);
69 void layoutChanged();
69 void layoutChanged();
70 // <--- PIMPL
70 // <--- PIMPL
71
71
72 QPointF mPos;
72 QPointF mPos;
73 QSizeF mSize;
73 QSizeF mSize;
74 QSizeF mMinimumSize;
74 QSizeF mMinimumSize;
75 QSizeF mMaximumSize;
75 QSizeF mMaximumSize;
76
76
77 // QList<QSeries*> mSeriesList;
78 QList<LegendMarker*> mMarkers;
77 QList<LegendMarker*> mMarkers;
79
78
80 QBrush m_brush;
79 QBrush m_brush;
81 QPen m_pen;
80 QPen m_pen;
82 QLegend::PreferredLayout mPreferredLayout;
81 QLegend::PreferredLayout mPreferredLayout;
83 };
82 };
84
83
85 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
86
85
87 #endif // QLEGEND_H
86 #endif // QLEGEND_H
@@ -1,71 +1,66
1 #include "qseries.h"
1 #include "qseries.h"
2
2
3 /*!
3 /*!
4 \class QSeries
4 \class QSeries
5 \brief Base class for all QtCommercial Chart series.
5 \brief Base class for all QtCommercial Chart series.
6 \mainclass
6 \mainclass
7
7
8 Usually you use the series type specific inherited classes instead of the base class.
8 Usually you use the series type specific inherited classes instead of the base class.
9 \sa QScatterSeries
9 \sa QScatterSeries
10 */
10 */
11
11
12 /*!
12 /*!
13 \enum QSeries::QSeriesType
13 \enum QSeries::QSeriesType
14
14
15 The type of the series object.
15 The type of the series object.
16
16
17 \value SeriesTypeLine
17 \value SeriesTypeLine
18 \value SeriesTypeArea
18 \value SeriesTypeArea
19 \value SeriesTypeBar
19 \value SeriesTypeBar
20 \value SeriesTypeStackedBar
20 \value SeriesTypeStackedBar
21 \value SeriesTypePercentBar
21 \value SeriesTypePercentBar
22 \value SeriesTypePie
22 \value SeriesTypePie
23 \value SeriesTypeScatter
23 \value SeriesTypeScatter
24 \value SeriesTypeSpline
24 \value SeriesTypeSpline
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn QSeries::QSeries(QObject *parent)
28 \fn QSeries::QSeries(QObject *parent)
29 \brief Constructs ChartSeries object with \a parent.
29 \brief Constructs ChartSeries object with \a parent.
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn QSeries::~QSeries()
33 \fn QSeries::~QSeries()
34 \brief Virtual destructor for the chart series.
34 \brief Virtual destructor for the chart series.
35 */
35 */
36
36
37 /*!
37 /*!
38 \fn QSeriesType QSeries::type() const
38 \fn QSeriesType QSeries::type() const
39 \brief The type of the series.
39 \brief The type of the series.
40 */
40 */
41
41
42 /*!
42 /*!
43 \fn bool QSeries::setModel(QAbstractItemModel *model)
43 \fn bool QSeries::setModel(QAbstractItemModel *model)
44 \brief Use the \a model to provide data for the series. The model overrides possible user data
44 \brief Use the \a model to provide data for the series. The model overrides possible user data
45 set with QChartSeries type specific data setters. For example if you call both
45 set with QChartSeries type specific data setters. For example if you call both
46 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
46 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
47 used by the series. Returns true if the model is valid for the series.
47 used by the series. Returns true if the model is valid for the series.
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn QList<QSeries::Legend> QSeries::legend()
52 \brief Returns the legend of the series. If series is empty, empty list is returned.
53 */
54
55 /*!
56 \fn void QSeries::setTitle(QString title)
51 \fn void QSeries::setTitle(QString title)
57 \brief Sets a \a title for the series.
52 \brief Sets a \a title for the series.
58
53
59 This is not used directly by the chart itself. It is up to the user to use this as for example
54 This is not used directly by the chart itself. It is up to the user to use this as for example
60 chart title.
55 chart title.
61 \sa QChart::setChartTitle()
56 \sa QChart::setChartTitle()
62 */
57 */
63
58
64 /*!
59 /*!
65 \fn QString QSeries::title()
60 \fn QString QSeries::title()
66 \brief Returns the title of the series.
61 \brief Returns the title of the series.
67 */
62 */
68
63
69 QTCOMMERCIALCHART_BEGIN_NAMESPACE
64 QTCOMMERCIALCHART_BEGIN_NAMESPACE
70 #include "moc_qseries.cpp"
65 #include "moc_qseries.cpp"
71 QTCOMMERCIALCHART_END_NAMESPACE
66 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now