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