##// END OF EJS Templates
updating drilldown example. Needs some more thinking
sauimone -
r438:c46e1ae6bddd
parent child
Show More
@@ -1,129 +1,160
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 <QStringList>
8 #include <QStringList>
9 #include <QDebug>
9 #include <QDebug>
10
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
12
13 //! [1]
13 //! [1]
14 class DrilldownBarSeries : public QStackedBarSeries
14 class DrilldownBarSeries : public QStackedBarSeries
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 DrilldownBarSeries(QStringList categories, QObject* parent=0) : QStackedBarSeries(categories,parent) {}
18 DrilldownBarSeries(QStringList categories, QObject* parent = 0) : QStackedBarSeries(categories,parent) {}
19
19
20 /*
20
21 public Q_SLOTS:
21 public Q_SLOTS:
22 void handleRightClick(QBarSet *barset, QString category)
22 void handleRightClick(QBarSet *barset, QString category)
23 {
23 {
24 qDebug() << "DrilldownBarSeries::handleRightClick" << barset->name() << category;
25 // mChart->changeSeries(this);
24 }
26 }
25 */
27
26 };
28 };
27 //! [1]
29 //! [1]
28
30
29 //! [2]
31 //! [2]
30 class DrilldownBarSet : public QBarSet
32 class DrilldownBarSet : public QBarSet
31 {
33 {
32 Q_OBJECT
34 Q_OBJECT
33 public:
35 public:
34 DrilldownBarSet(QString name, DrilldownBarSeries *parent) : QBarSet(name, parent), mSeries(parent) {}
36 DrilldownBarSet(QString name, DrilldownBarSeries* drilldownSeries) : QBarSet(name) , mSeries(drilldownSeries) {}
35
37
36 DrilldownBarSeries* drilldownSeries() { return mSeries; }
38 DrilldownBarSeries* drilldownSeries(QString category)
39 {
40 return mSeries;
41 }
37
42
38 private:
43 private:
39 DrilldownBarSeries* mSeries;
44 DrilldownBarSeries* mSeries;
40 };
45 };
41 //! [2]
46 //! [2]
42
47
43 //! [3]
48 //! [3]
44 class DrilldownChart : public QChartView
49 class DrilldownChart : public QChartView
45 {
50 {
46 Q_OBJECT
51 Q_OBJECT
47 public:
52 public:
48 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
53 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
49
54
50 void changeSeries(QSeries* series)
55 void changeSeries(QSeries* series)
51 {
56 {
52 if (m_currentSeries)
57 if (m_currentSeries)
53 removeSeries(m_currentSeries);
58 removeSeries(m_currentSeries);
54 m_currentSeries = series;
59 m_currentSeries = series;
55 addSeries(series);
60 addSeries(series);
56 setChartTitle(series->title());
61 setChartTitle(series->title());
57 }
62 }
58
63
59 public Q_SLOTS:
64 public Q_SLOTS:
60 void handleRightClick(QBarSet* barset, QString category)
65 void handleRightClick(QBarSet *barset, QString category)
61 {
66 {
62 qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
67 qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
63 // TODO: continue from here
68 // TODO: continue from here
64 // DrilldownBarSet* drilldownBarSet = static_cast<DrilldownBarSet*>(barset);
69 DrilldownBarSet* drilldownBarSet = static_cast<DrilldownBarSet*>(barset);
65 // changeSeries(drilldownBarSet->drilldownSeries());
70 changeSeries(drilldownBarSet->drilldownSeries(category));
66 }
71 }
67
72
68 private:
73 private:
69 QSeries* m_currentSeries;
74 QSeries* m_currentSeries;
70 };
75 };
71 //! [3]
76 //! [3]
72
77
73
74 int main(int argc, char *argv[])
78 int main(int argc, char *argv[])
75 {
79 {
76 QApplication a(argc, argv);
80 QApplication a(argc, argv);
77 QMainWindow window;
81 QMainWindow window;
78
82
79 DrilldownChart* drilldownChart = new DrilldownChart(&window);
83 DrilldownChart* drilldownChart = new DrilldownChart(&window);
80 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
84 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
81
85
82 //! [1]
86 //! [4]
83 // Define categories
87 // Define categories
84 QStringList months;
88 QStringList months;
85 months << "Jun" << "Jul" << "Aug" << "Oct";
89 months << "Jun" << "Jul" << "Aug" << "Sep";
86 QStringList weeks;
90 QStringList weeks;
87 weeks << "week 1" << "week 2" << "week 3" << "week 4";
91 weeks << "week 1" << "week 2" << "week 3" << "week 4";
88 QStringList plants;
92 QStringList plants;
89 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
93 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
90 //! [1]
94 //! [4]
91
95
92 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
96 DrilldownBarSeries* monthlySeries = new DrilldownBarSeries(months, drilldownChart);
93 seasonSeries->setTitle("Crop by month - Season");
97 monthlySeries->setTitle("Crop by month - Season");
94
98
95 foreach (QString plant, plants) {
99 foreach (QString plant, plants) {
96 DrilldownBarSet *seasonCrop = new DrilldownBarSet(plant, seasonSeries);
100 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
101 DrilldownBarSet* monthlyCrop = new DrilldownBarSet(plant,weeklySeries);
102 weeklySeries->setTitle("Crop by week - Month");
103
97 foreach(QString month, months) {
104 foreach(QString month, months) {
98 DrilldownBarSeries* monthSeries = new DrilldownBarSeries(weeks, drilldownChart);
105 DrilldownBarSet* weeklyCrop = new DrilldownBarSet(plant,monthlySeries);
99 DrilldownBarSet *monthCrop = new DrilldownBarSet(plant, monthSeries);
106
100 foreach(QString week, weeks) {
107 foreach (QString week, weeks ) {
101 *monthCrop << (qrand() % 20);
108 *weeklyCrop << (qrand() % 20);
102 }
103 monthSeries->addBarSet(monthCrop);
104 *seasonCrop << monthCrop->valueAt(plants.indexOf(plant));
105 }
109 }
106
110
107 // We want floating values!
111 weeklySeries->addBarSet(weeklyCrop);
108 QObject::connect(seasonCrop,SIGNAL(clicked(QString)),seasonCrop,SIGNAL(toggleFloatingValues()));
112 weeklySeries->setToolTipEnabled(true);
109 seasonSeries->addBarSet(seasonCrop);
113 *monthlyCrop << weeklyCrop->total();
114
115 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
116 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
110 }
117 }
111
118
112 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
119 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
120 monthlySeries->addBarSet(monthlyCrop);
121 }
113
122
114 seasonSeries->setToolTipEnabled(true);
123 /*
124 foreach (QString plant, plants) {
125 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
126 DrilldownBarSet* monthlyCrop = new DrilldownBarSet(plant,weeklySeries);
127 weeklySeries->setTitle("Crop by week - Month");
128 foreach(QString month, months) {
129 DrilldownBarSet* weeklyCrop = new DrilldownBarSet(plant,monthlySeries);
130 foreach (QString week, weeks ) {
131 *weeklyCrop << (qrand() % 20);
132 }
133 weeklySeries->addBarSet(weeklyCrop);
134 weeklySeries->setToolTipEnabled(true);
135 *monthlyCrop << weeklyCrop->total();
136 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
137 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
138 }
139 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
140 monthlySeries->addBarSet(monthlyCrop);
141 }
142 */
143 QObject::connect(monthlySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
115
144
116 drilldownChart->addSeries(seasonSeries);
145 monthlySeries->setToolTipEnabled(true);
146 drilldownChart->changeSeries(monthlySeries);
147 drilldownChart->setChartTitle(monthlySeries->title());
117
148
118 drilldownChart->axisX()->setAxisVisible(false);
149 drilldownChart->axisX()->setAxisVisible(false);
119 drilldownChart->axisX()->setGridVisible(false);
150 drilldownChart->axisX()->setGridVisible(false);
120 drilldownChart->axisX()->setLabelsVisible(false);
151 drilldownChart->axisX()->setLabelsVisible(false);
121
152
122 window.setCentralWidget(drilldownChart);
153 window.setCentralWidget(drilldownChart);
123 window.resize(400, 300);
154 window.resize(400, 300);
124 window.show();
155 window.show();
125
156
126 return a.exec();
157 return a.exec();
127 }
158 }
128
159
129 #include "main.moc"
160 #include "main.moc"
@@ -1,160 +1,159
1 #include "barpresenterbase_p.h"
1 #include "barpresenterbase_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "barlabel_p.h"
4 #include "barlabel_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include <QDebug>
8 #include <QDebug>
9 #include <QToolTip>
9 #include <QToolTip>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
14 : ChartItem(parent)
14 : ChartItem(parent)
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
16 ,mLayoutSet(false)
16 ,mLayoutSet(false)
17 ,mLayoutDirty(true)
17 ,mLayoutDirty(true)
18 ,mSeparatorsEnabled(false)
18 ,mSeparatorsEnabled(false)
19 ,mSeries(series)
19 ,mSeries(series)
20 {
20 {
21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 // connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
22 // connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 dataChanged();
23 dataChanged();
24 }
24 }
25
25
26 BarPresenterBase::~BarPresenterBase()
26 BarPresenterBase::~BarPresenterBase()
27 {
27 {
28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
29 delete mSeries;
30 }
29 }
31
30
32 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
31 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
33 {
32 {
34 if (!mLayoutSet) {
33 if (!mLayoutSet) {
35 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
34 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
36 return;
35 return;
37 }
36 }
38 // if (mLayoutDirty) {
37 // if (mLayoutDirty) {
39 // Layout or data has changed. Need to redraw.
38 // Layout or data has changed. Need to redraw.
40 foreach(QGraphicsItem* i, childItems()) {
39 foreach(QGraphicsItem* i, childItems()) {
41 i->paint(painter,option,widget);
40 i->paint(painter,option,widget);
42 }
41 }
43 // }
42 // }
44 }
43 }
45
44
46 QRectF BarPresenterBase::boundingRect() const
45 QRectF BarPresenterBase::boundingRect() const
47 {
46 {
48 return QRectF(0,0,mWidth,mHeight);
47 return QRectF(0,0,mWidth,mHeight);
49 }
48 }
50
49
51 void BarPresenterBase::setBarWidth( int w )
50 void BarPresenterBase::setBarWidth( int w )
52 {
51 {
53 mBarDefaultWidth = w;
52 mBarDefaultWidth = w;
54 }
53 }
55
54
56 void BarPresenterBase::dataChanged()
55 void BarPresenterBase::dataChanged()
57 {
56 {
58 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
57 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
59 // qDebug() << "datachanged";
58 // qDebug() << "datachanged";
60 // Delete old bars
59 // Delete old bars
61 foreach (QGraphicsItem* item, childItems()) {
60 foreach (QGraphicsItem* item, childItems()) {
62 delete item;
61 delete item;
63 }
62 }
64
63
65 mBars.clear();
64 mBars.clear();
66 mLabels.clear();
65 mLabels.clear();
67 mSeparators.clear();
66 mSeparators.clear();
68 mFloatingValues.clear();
67 mFloatingValues.clear();
69
68
70 // Create new graphic items for bars
69 // Create new graphic items for bars
71 for (int c=0; c<mSeries->categoryCount(); c++) {
70 for (int c=0; c<mSeries->categoryCount(); c++) {
72 QString category = mSeries->categoryName(c);
71 QString category = mSeries->categoryName(c);
73 for (int s=0; s<mSeries->barsetCount(); s++) {
72 for (int s=0; s<mSeries->barsetCount(); s++) {
74 QBarSet *set = mSeries->barsetAt(s);
73 QBarSet *set = mSeries->barsetAt(s);
75 Bar *bar = new Bar(category,this);
74 Bar *bar = new Bar(category,this);
76 childItems().append(bar);
75 childItems().append(bar);
77 mBars.append(bar);
76 mBars.append(bar);
78 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
77 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
79 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
78 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
80 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
79 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
81 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
80 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
82 }
81 }
83 }
82 }
84
83
85 // Create labels
84 // Create labels
86 int count = mSeries->categoryCount();
85 int count = mSeries->categoryCount();
87 for (int i=0; i<count; i++) {
86 for (int i=0; i<count; i++) {
88 BarLabel* label = new BarLabel(this);
87 BarLabel* label = new BarLabel(this);
89 label->set(mSeries->categoryName(i));
88 label->set(mSeries->categoryName(i));
90 childItems().append(label);
89 childItems().append(label);
91 mLabels.append(label);
90 mLabels.append(label);
92 }
91 }
93
92
94 // Create separators
93 // Create separators
95 count = mSeries->categoryCount() - 1; // There is one less separator than columns
94 count = mSeries->categoryCount() - 1; // There is one less separator than columns
96 for (int i=0; i<count; i++) {
95 for (int i=0; i<count; i++) {
97 Separator* sep = new Separator(this);
96 Separator* sep = new Separator(this);
98 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
97 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
99 sep->setVisible(mSeparatorsEnabled);
98 sep->setVisible(mSeparatorsEnabled);
100 childItems().append(sep);
99 childItems().append(sep);
101 mSeparators.append(sep);
100 mSeparators.append(sep);
102 }
101 }
103
102
104 // Create floating values
103 // Create floating values
105 for (int category=0; category<mSeries->categoryCount(); category++) {
104 for (int category=0; category<mSeries->categoryCount(); category++) {
106 for (int s=0; s<mSeries->barsetCount(); s++) {
105 for (int s=0; s<mSeries->barsetCount(); s++) {
107 QBarSet *set = mSeries->barsetAt(s);
106 QBarSet *set = mSeries->barsetAt(s);
108 BarValue *value = new BarValue(*set, this);
107 BarValue *value = new BarValue(*set, this);
109 childItems().append(value);
108 childItems().append(value);
110 mFloatingValues.append(value);
109 mFloatingValues.append(value);
111 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
110 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
112 }
111 }
113 }
112 }
114
113
115 // TODO: if (autolayout) { layoutChanged() } or something
114 // TODO: if (autolayout) { layoutChanged() } or something
116 mLayoutDirty = true;
115 mLayoutDirty = true;
117 }
116 }
118
117
119 //handlers
118 //handlers
120
119
121 void BarPresenterBase::handleModelChanged(int index)
120 void BarPresenterBase::handleModelChanged(int index)
122 {
121 {
123 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
122 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
124 dataChanged();
123 dataChanged();
125 }
124 }
126
125
127 void BarPresenterBase::handleDomainChanged(const Domain& domain)
126 void BarPresenterBase::handleDomainChanged(const Domain& domain)
128 {
127 {
129 // qDebug() << "BarPresenterBase::handleDomainChanged";
128 // qDebug() << "BarPresenterBase::handleDomainChanged";
130 // TODO: Figure out the use case for this.
129 // TODO: Figure out the use case for this.
131 // Affects the size of visible item, so layout is changed.
130 // Affects the size of visible item, so layout is changed.
132 // layoutChanged();
131 // layoutChanged();
133 }
132 }
134
133
135 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
134 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
136 {
135 {
137 mWidth = rect.width();
136 mWidth = rect.width();
138 mHeight = rect.height();
137 mHeight = rect.height();
139 layoutChanged();
138 layoutChanged();
140 mLayoutSet = true;
139 mLayoutSet = true;
141 setPos(rect.topLeft());
140 setPos(rect.topLeft());
142 }
141 }
143
142
144 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
143 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
145 {
144 {
146 // TODO: cool tooltip instead of default
145 // TODO: cool tooltip instead of default
147 QToolTip::showText(pos,tip);
146 QToolTip::showText(pos,tip);
148 }
147 }
149
148
150 void BarPresenterBase::enableSeparators(bool enabled)
149 void BarPresenterBase::enableSeparators(bool enabled)
151 {
150 {
152 for (int i=0; i<mSeparators.count(); i++) {
151 for (int i=0; i<mSeparators.count(); i++) {
153 mSeparators.at(i)->setVisible(enabled);
152 mSeparators.at(i)->setVisible(enabled);
154 }
153 }
155 mSeparatorsEnabled = enabled;
154 mSeparatorsEnabled = enabled;
156 }
155 }
157
156
158 #include "moc_barpresenterbase_p.cpp"
157 #include "moc_barpresenterbase_p.cpp"
159
158
160 QTCOMMERCIALCHART_END_NAMESPACE
159 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,72
1 #ifndef BARPRESENTERBASE_H
1 #ifndef BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class Bar;
12 class Bar;
13 class BarLabel;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15 class BarValue;
16
16
17 // Common implemantation of different presenters. Not to be instantiated.
17 // Common implemantation of different presenters. Not to be instantiated.
18 // TODO: combine this with BarPresenter and derive other presenters from it?
18 // TODO: combine this with BarPresenter and derive other presenters from it?
19 class BarPresenterBase : public QObject, public ChartItem
19 class BarPresenterBase : public QObject, public ChartItem
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0);
23 BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0);
24 ~BarPresenterBase();
24 virtual ~BarPresenterBase();
25
25
26 public:
26 public:
27 // From QGraphicsItem
27 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
29 QRectF boundingRect() const;
30
30
31 // TODO: these may change with layout awarness.
31 // TODO: these may change with layout awarness.
32 void setBarWidth( int w );
32 void setBarWidth( int w );
33
33
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
37
37
38 public slots:
38 public slots:
39 void handleModelChanged(int index);
39 void handleModelChanged(int index);
40 void handleDomainChanged(const Domain& domain);
40 void handleDomainChanged(const Domain& domain);
41 void handleGeometryChanged(const QRectF& size);
41 void handleGeometryChanged(const QRectF& size);
42
42
43 // Internal slots
43 // Internal slots
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void enableSeparators(bool enabled);
45 void enableSeparators(bool enabled);
46
46
47 protected:
47 protected:
48
48
49 // TODO: consider these.
49 // TODO: consider these.
50 int mHeight; // Layout spesific
50 int mHeight; // Layout spesific
51 int mWidth;
51 int mWidth;
52 int mBarDefaultWidth;
52 int mBarDefaultWidth;
53
53
54 bool mLayoutSet; // True, if component has been laid out.
54 bool mLayoutSet; // True, if component has been laid out.
55 bool mLayoutDirty;
55 bool mLayoutDirty;
56
56
57 bool mSeparatorsEnabled;
57 bool mSeparatorsEnabled;
58
58
59 // Owned
59 // Owned
60 QBarSeries* mSeries;
60 QBarSeries* mSeries;
61
61
62 // Not owned.
62 // Not owned.
63 QList<Bar*> mBars;
63 QList<Bar*> mBars;
64 QList<BarLabel*> mLabels;
64 QList<BarLabel*> mLabels;
65 QList<Separator*> mSeparators;
65 QList<Separator*> mSeparators;
66 QList<BarValue*> mFloatingValues;
66 QList<BarValue*> mFloatingValues;
67
67
68 };
68 };
69
69
70 QTCOMMERCIALCHART_END_NAMESPACE
70 QTCOMMERCIALCHART_END_NAMESPACE
71
71
72 #endif // BARPRESENTERBASE_H
72 #endif // BARPRESENTERBASE_H
@@ -1,163 +1,173
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QToolTip>
3 #include <QToolTip>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 /*!
7 /*!
8 \class QBarSet
8 \class QBarSet
9 \brief part of QtCommercial chart API.
9 \brief part of QtCommercial chart API.
10
10
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 First value of set is assumed to belong to first category, second to second category and so on.
12 First value of set is assumed to belong to first category, second to second category and so on.
13 If set has fewer values than there are categories, then the missing values are assumed to be
13 If set has fewer values than there are categories, then the missing values are assumed to be
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn void QBarSet::clicked(QString category)
22 \fn void QBarSet::clicked(QString category)
23 \brief signals that set has been clicked
23 \brief signals that set has been clicked
24 Parameter \a category describes on which category was clicked
24 Parameter \a category describes on which category was clicked
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSet::rightClicked(QString category)
28 \fn void QBarSet::rightClicked(QString category)
29 \brief signals that set has been clicked with right mouse button
29 \brief signals that set has been clicked with right mouse button
30 Parameter \a category describes on which category was clicked
30 Parameter \a category describes on which category was clicked
31 */
31 */
32
32
33 /*!
33 /*!
34 \fn void QBarSet::hoverEnter(QPoint pos)
34 \fn void QBarSet::hoverEnter(QPoint pos)
35 \brief signals that mouse has entered over the set at position \a pos.
35 \brief signals that mouse has entered over the set at position \a pos.
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn void QBarSet::hoverLeave()
39 \fn void QBarSet::hoverLeave()
40 \brief signals that mouse has left from the set.
40 \brief signals that mouse has left from the set.
41 */
41 */
42
42
43 /*!
43 /*!
44 \fn void QBarSet::toggleFloatingValues()
44 \fn void QBarSet::toggleFloatingValues()
45 \brief \internal
45 \brief \internal
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
50 \brief \internal \a pos \a tip
50 \brief \internal \a pos \a tip
51 */
51 */
52
52
53
53
54 /*!
54 /*!
55 Constructs QBarSet with a name of \a name and with parent of \a parent
55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 */
56 */
57 QBarSet::QBarSet(QString name, QObject *parent)
57 QBarSet::QBarSet(QString name, QObject *parent)
58 : QObject(parent)
58 : QObject(parent)
59 ,mName(name)
59 ,mName(name)
60 {
60 {
61 }
61 }
62
62
63 /*!
63 /*!
64 Sets new \a name for set.
64 Sets new \a name for set.
65 */
65 */
66 void QBarSet::setName(QString name)
66 void QBarSet::setName(QString name)
67 {
67 {
68 mName = name;
68 mName = name;
69 }
69 }
70
70
71 /*!
71 /*!
72 Returns name of the set.
72 Returns name of the set.
73 */
73 */
74 QString QBarSet::name()
74 QString QBarSet::name()
75 {
75 {
76 return mName;
76 return mName;
77 }
77 }
78
78
79 /*!
79 /*!
80 Appends new value \a value to the end of set.
80 Appends new value \a value to the end of set.
81 */
81 */
82 QBarSet& QBarSet::operator << (const qreal &value)
82 QBarSet& QBarSet::operator << (const qreal &value)
83 {
83 {
84 mValues.append(value);
84 mValues.append(value);
85 return *this;
85 return *this;
86 }
86 }
87
87
88 /*!
88 /*!
89 Returns count of values in set.
89 Returns count of values in set.
90 */
90 */
91 int QBarSet::count()
91 int QBarSet::count()
92 {
92 {
93 return mValues.count();
93 return mValues.count();
94 }
94 }
95
95
96 /*!
96 /*!
97 Returns value of set indexed by \a index
97 Returns value of set indexed by \a index
98 */
98 */
99 qreal QBarSet::valueAt(int index)
99 qreal QBarSet::valueAt(int index)
100 {
100 {
101 return mValues.at(index);
101 return mValues.at(index);
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets a new value \a value to set, indexed by \a index
105 Sets a new value \a value to set, indexed by \a index
106 */
106 */
107 void QBarSet::setValue(int index, qreal value)
107 void QBarSet::setValue(int index, qreal value)
108 {
108 {
109 mValues.replace(index,value);
109 mValues.replace(index,value);
110 }
110 }
111
111
112 qreal QBarSet::total()
113 {
114 qreal total(0);
115 for (int i=0; i<mValues.count(); i++) {
116 total += mValues.at(i);
117 }
118 return total;
119 }
120
121
112 /*!
122 /*!
113 Sets pen for set. Bars of this set are drawn using \a pen
123 Sets pen for set. Bars of this set are drawn using \a pen
114 */
124 */
115 void QBarSet::setPen(QPen pen)
125 void QBarSet::setPen(QPen pen)
116 {
126 {
117 mPen = pen;
127 mPen = pen;
118 }
128 }
119
129
120 /*!
130 /*!
121 Returns pen of the set.
131 Returns pen of the set.
122 */
132 */
123 QPen QBarSet::pen()
133 QPen QBarSet::pen()
124 {
134 {
125 return mPen;
135 return mPen;
126 }
136 }
127
137
128 /*!
138 /*!
129 Sets brush for the set. Bars of this set are drawn using \a brush
139 Sets brush for the set. Bars of this set are drawn using \a brush
130 */
140 */
131 void QBarSet::setBrush(QBrush brush)
141 void QBarSet::setBrush(QBrush brush)
132 {
142 {
133 mBrush = brush;
143 mBrush = brush;
134 }
144 }
135
145
136 /*!
146 /*!
137 Returns brush of the set.
147 Returns brush of the set.
138 */
148 */
139 QBrush QBarSet::brush()
149 QBrush QBarSet::brush()
140 {
150 {
141 return mBrush;
151 return mBrush;
142 }
152 }
143
153
144 /*!
154 /*!
145 \internal \a pos
155 \internal \a pos
146 */
156 */
147 void QBarSet::barHoverEnterEvent(QPoint pos)
157 void QBarSet::barHoverEnterEvent(QPoint pos)
148 {
158 {
149 emit showToolTip(pos, mName);
159 emit showToolTip(pos, mName);
150 emit hoverEnter(pos);
160 emit hoverEnter(pos);
151 }
161 }
152
162
153 /*!
163 /*!
154 \internal
164 \internal
155 */
165 */
156 void QBarSet::barHoverLeaveEvent()
166 void QBarSet::barHoverLeaveEvent()
157 {
167 {
158 // Emit signal to user of charts
168 // Emit signal to user of charts
159 emit hoverLeave();
169 emit hoverLeave();
160 }
170 }
161
171
162 #include "moc_qbarset.cpp"
172 #include "moc_qbarset.cpp"
163 QTCOMMERCIALCHART_END_NAMESPACE
173 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,67
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet(QString name, QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15
15
16 void setName(QString name);
16 void setName(QString name);
17 QString name();
17 QString name();
18 QBarSet& operator << (const qreal &value); // appends new value to set
18 QBarSet& operator << (const qreal &value); // appends new value to set
19
19
20 // TODO: remove indices eventually. Use as internal?
20 // TODO: remove indices eventually. Use as internal?
21 int count(); // count of values in set
21 int count(); // count of values in set
22 qreal valueAt(int index); // for modifying individual values
22 qreal valueAt(int index); // for modifying individual values
23 void setValue(int index, qreal value); // setter for individual value
23 void setValue(int index, qreal value); // setter for individual value
24 qreal total(); // total values in the set
24
25
25 // TODO:
26 // TODO:
26 //qreal value(QString category);
27 //qreal value(QString category);
27 //void setValue(QString category, qreal value);
28 //void setValue(QString category, qreal value);
28
29
29 void setPen(QPen pen);
30 void setPen(QPen pen);
30 QPen pen();
31 QPen pen();
31
32
32 void setBrush(QBrush brush);
33 void setBrush(QBrush brush);
33 QBrush brush();
34 QBrush brush();
34
35
35 Q_SIGNALS:
36 Q_SIGNALS:
36 void clicked(QString category); // Clicked and hover signals exposed to user
37 void clicked(QString category); // Clicked and hover signals exposed to user
37 void rightClicked(QString category);
38 void rightClicked(QString category);
38 void toggleFloatingValues();
39 void toggleFloatingValues();
39
40
40 // TODO: Expose this to user or not?
41 // TODO: Expose this to user or not?
41 // TODO: TO PIMPL --->
42 // TODO: TO PIMPL --->
42 void hoverEnter(QPoint pos);
43 void hoverEnter(QPoint pos);
43 void hoverLeave();
44 void hoverLeave();
44 void showToolTip(QPoint pos, QString tip); // Private signal
45 void showToolTip(QPoint pos, QString tip); // Private signal
45 // <--- TO PIMPL
46 // <--- TO PIMPL
46
47
47 public Q_SLOTS:
48 public Q_SLOTS:
48 // These are for internal communication
49 // These are for internal communication
49 // TODO: TO PIMPL --->
50 // TODO: TO PIMPL --->
50 void barHoverEnterEvent(QPoint pos);
51 void barHoverEnterEvent(QPoint pos);
51 void barHoverLeaveEvent();
52 void barHoverLeaveEvent();
52 // <--- TO PIMPL
53 // <--- TO PIMPL
53
54
54 private:
55 private:
55
56
56 QString mName;
57 QString mName;
57 QList<qreal> mValues; // TODO: replace with map (category, value)
58 QList<qreal> mValues; // TODO: replace with map (category, value)
58 QMap<QString,qreal> mMappedValues;
59 QMap<QString,qreal> mMappedValues;
59 QPen mPen;
60 QPen mPen;
60 QBrush mBrush;
61 QBrush mBrush;
61
62
62 };
63 };
63
64
64 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
65
66
66 #endif // QBARSET_H
67 #endif // QBARSET_H
@@ -1,111 +1,117
1 #include "stackedbarpresenter_p.h"
1 #include "stackedbarpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
12 BarPresenterBase(series,parent)
12 BarPresenterBase(series,parent)
13 {
13 {
14 }
14 }
15
15
16 StackedBarPresenter::~StackedBarPresenter()
17 {
18 qDebug() << "StackedBarPresenter deleted";
19 }
20
21
16 void StackedBarPresenter::layoutChanged()
22 void StackedBarPresenter::layoutChanged()
17 {
23 {
18 // Scale bars to new layout
24 // Scale bars to new layout
19 // Layout for bars:
25 // Layout for bars:
20 if (mSeries->barsetCount() <= 0) {
26 if (mSeries->barsetCount() <= 0) {
21 qDebug() << "No sets in model!";
27 qDebug() << "No sets in model!";
22 // Nothing to do.
28 // Nothing to do.
23 return;
29 return;
24 }
30 }
25
31
26 if (mSeries->categoryCount() == 0) {
32 if (mSeries->categoryCount() == 0) {
27 qDebug() << "No categories in model!";
33 qDebug() << "No categories in model!";
28 // Nothing to do
34 // Nothing to do
29 return;
35 return;
30 }
36 }
31
37
32 if (childItems().count() == 0) {
38 if (childItems().count() == 0) {
33 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
39 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
34 return;
40 return;
35 }
41 }
36
42
37 // TODO: better way to auto-layout
43 // TODO: better way to auto-layout
38 // Use reals for accurancy (we might get some compiler warnings... :)
44 // Use reals for accurancy (we might get some compiler warnings... :)
39 // TODO: use temp variable for category count...
45 // TODO: use temp variable for category count...
40 qreal maxSum = mSeries->maxCategorySum();
46 qreal maxSum = mSeries->maxCategorySum();
41 qreal h = mHeight;
47 qreal h = mHeight;
42 qreal scale = (h / maxSum);
48 qreal scale = (h / maxSum);
43
49
44 int itemIndex(0);
50 int itemIndex(0);
45 int labelIndex(0);
51 int labelIndex(0);
46 qreal tW = mWidth;
52 qreal tW = mWidth;
47 qreal tC = mSeries->categoryCount() + 1;
53 qreal tC = mSeries->categoryCount() + 1;
48 qreal xStep = (tW/tC);
54 qreal xStep = (tW/tC);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
55 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
50
56
51 for (int category = 0; category < mSeries->categoryCount(); category++) {
57 for (int category = 0; category < mSeries->categoryCount(); category++) {
52 qreal yPos = h;
58 qreal yPos = h;
53 for (int set=0; set < mSeries->barsetCount(); set++) {
59 for (int set=0; set < mSeries->barsetCount(); set++) {
54 qreal barHeight = mSeries->valueAt(set, category) * scale;
60 qreal barHeight = mSeries->valueAt(set, category) * scale;
55 Bar* bar = mBars.at(itemIndex);
61 Bar* bar = mBars.at(itemIndex);
56
62
57 bar->resize(mBarDefaultWidth, barHeight);
63 bar->resize(mBarDefaultWidth, barHeight);
58 bar->setBrush(mSeries->barsetAt(set)->brush());
64 bar->setBrush(mSeries->barsetAt(set)->brush());
59 bar->setPos(xPos, yPos-barHeight);
65 bar->setPos(xPos, yPos-barHeight);
60 itemIndex++;
66 itemIndex++;
61 yPos -= barHeight;
67 yPos -= barHeight;
62 }
68 }
63
69
64 // TODO: Layout for labels, remove magic number
70 // TODO: Layout for labels, remove magic number
65 BarLabel* label = mLabels.at(labelIndex);
71 BarLabel* label = mLabels.at(labelIndex);
66 label->setPos(xPos, mHeight + 20);
72 label->setPos(xPos, mHeight + 20);
67 labelIndex++;
73 labelIndex++;
68 xPos += xStep;
74 xPos += xStep;
69 }
75 }
70
76
71 // Position separators
77 // Position separators
72 xPos = xStep + xStep/2;
78 xPos = xStep + xStep/2;
73 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
79 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
74 Separator* sep = mSeparators.at(s);
80 Separator* sep = mSeparators.at(s);
75 sep->setPos(xPos,0);
81 sep->setPos(xPos,0);
76 sep->setSize(QSizeF(1,mHeight));
82 sep->setSize(QSizeF(1,mHeight));
77 xPos += xStep;
83 xPos += xStep;
78 }
84 }
79
85
80 // Position floating values
86 // Position floating values
81 itemIndex = 0;
87 itemIndex = 0;
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
88 xPos = ((tW/tC) - mBarDefaultWidth / 2);
83 for (int category=0; category < mSeries->categoryCount(); category++) {
89 for (int category=0; category < mSeries->categoryCount(); category++) {
84 qreal yPos = h;
90 qreal yPos = h;
85 for (int set=0; set < mSeries->barsetCount(); set++) {
91 for (int set=0; set < mSeries->barsetCount(); set++) {
86 qreal barHeight = mSeries->valueAt(set,category) * scale;
92 qreal barHeight = mSeries->valueAt(set,category) * scale;
87 BarValue* value = mFloatingValues.at(itemIndex);
93 BarValue* value = mFloatingValues.at(itemIndex);
88
94
89 // TODO: remove hard coding, apply layout
95 // TODO: remove hard coding, apply layout
90 value->resize(100,50);
96 value->resize(100,50);
91 value->setPos(xPos, yPos-barHeight/2);
97 value->setPos(xPos, yPos-barHeight/2);
92 value->setPen(QPen(QColor(255,255,255,255)));
98 value->setPen(QPen(QColor(255,255,255,255)));
93
99
94 if (mSeries->valueAt(set,category) != 0) {
100 if (mSeries->valueAt(set,category) != 0) {
95 value->setValueString(QString::number(mSeries->valueAt(set,category)));
101 value->setValueString(QString::number(mSeries->valueAt(set,category)));
96 } else {
102 } else {
97 value->setValueString(QString(""));
103 value->setValueString(QString(""));
98 }
104 }
99
105
100 itemIndex++;
106 itemIndex++;
101 yPos -= barHeight;
107 yPos -= barHeight;
102 }
108 }
103 xPos += xStep;
109 xPos += xStep;
104 }
110 }
105
111
106 mLayoutDirty = true;
112 mLayoutDirty = true;
107 }
113 }
108
114
109 #include "moc_stackedbarpresenter_p.cpp"
115 #include "moc_stackedbarpresenter_p.cpp"
110
116
111 QTCOMMERCIALCHART_END_NAMESPACE
117 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,27 +1,28
1 #ifndef STACKEDBARPRESENTER_H
1 #ifndef STACKEDBARPRESENTER_H
2 #define STACKEDBARPRESENTER_H
2 #define STACKEDBARPRESENTER_H
3
3
4 #include "barpresenterbase_p.h"
4 #include "barpresenterbase_p.h"
5 #include "qstackedbarseries.h"
5 #include "qstackedbarseries.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class StackedBarPresenter : public BarPresenterBase
10 class StackedBarPresenter : public BarPresenterBase
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
14 StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
15 ~StackedBarPresenter();
15
16
16 private:
17 private:
17 // From BarPresenterBase
18 // From BarPresenterBase
18 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
19 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
19
20
20 private:
21 private:
21
22
22 // Data
23 // Data
23 };
24 };
24
25
25 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
26
27
27 #endif // STACKEDBARPRESENTER_H
28 #endif // STACKEDBARPRESENTER_H
General Comments 0
You need to be logged in to leave comments. Login now