##// END OF EJS Templates
barcharts: added legend to model. added signals for hover events (for tooltip). updated examples
sauimone -
r280:9b4c1980ee18
parent child
Show More
@@ -1,7 +1,7
1 1 #include "custombarset.h"
2 2
3 CustomBarSet::CustomBarSet(QObject *parent) :
4 QBarSet(parent)
3 CustomBarSet::CustomBarSet(QString name, QObject *parent) :
4 QBarSet(name, parent)
5 5 {
6 6 // Using signal to signal connection here.
7 7 connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues()));
@@ -9,7 +9,7 class CustomBarSet : public QBarSet
9 9 {
10 10 Q_OBJECT
11 11 public:
12 explicit CustomBarSet(QObject *parent = 0);
12 explicit CustomBarSet(QString name, QObject *parent = 0);
13 13
14 14 signals:
15 15
@@ -20,11 +20,11 int main(int argc, char *argv[])
20 20 QBarChartSeries* series= new QBarChartSeries(category);
21 21
22 22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
23 CustomBarSet *set0 = new CustomBarSet;
24 CustomBarSet *set1 = new CustomBarSet;
25 CustomBarSet *set2 = new CustomBarSet;
26 CustomBarSet *set3 = new CustomBarSet;
27 CustomBarSet *set4 = new CustomBarSet;
23 CustomBarSet *set0 = new CustomBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
28 28
29 29 // Create some test data to chart
30 30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
@@ -1,7 +1,7
1 1 #include "custombarset.h"
2 2
3 CustomBarSet::CustomBarSet(QObject *parent) :
4 QBarSet(parent)
3 CustomBarSet::CustomBarSet(QString name, QObject *parent) :
4 QBarSet(name, parent)
5 5 {
6 6 // Using signal to signal connection here.
7 7 connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues()));
@@ -9,7 +9,7 class CustomBarSet : public QBarSet
9 9 {
10 10 Q_OBJECT
11 11 public:
12 explicit CustomBarSet(QObject *parent = 0);
12 explicit CustomBarSet(QString name, QObject *parent = 0);
13 13
14 14 signals:
15 15
@@ -5,7 +5,6
5 5 #include <qbarcategory.h>
6 6 #include <qchartview.h>
7 7
8 //#include <qbarset.h>
9 8 #include "custombarset.h"
10 9
11 10 QTCOMMERCIALCHART_USE_NAMESPACE
@@ -21,11 +20,11 int main(int argc, char *argv[])
21 20 QPercentBarChartSeries* series = new QPercentBarChartSeries(category);
22 21
23 22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
24 CustomBarSet *set0 = new CustomBarSet;
25 CustomBarSet *set1 = new CustomBarSet;
26 CustomBarSet *set2 = new CustomBarSet;
27 CustomBarSet *set3 = new CustomBarSet;
28 CustomBarSet *set4 = new CustomBarSet;
23 CustomBarSet *set0 = new CustomBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
29 28
30 29 // Create some test data to chart
31 30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
@@ -1,7 +1,7
1 1 #include "custombarset.h"
2 2
3 CustomBarSet::CustomBarSet(QObject *parent) :
4 QBarSet(parent)
3 CustomBarSet::CustomBarSet(QString name, QObject *parent) :
4 QBarSet(name, parent)
5 5 {
6 6 // Using signal to signal connection here.
7 7 connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues()));
@@ -9,7 +9,7 class CustomBarSet : public QBarSet
9 9 {
10 10 Q_OBJECT
11 11 public:
12 explicit CustomBarSet(QObject *parent = 0);
12 explicit CustomBarSet(QString name, QObject *parent = 0);
13 13
14 14 signals:
15 15
@@ -20,11 +20,11 int main(int argc, char *argv[])
20 20 QStackedBarChartSeries* series = new QStackedBarChartSeries(category);
21 21
22 22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
23 CustomBarSet *set0 = new CustomBarSet;
24 CustomBarSet *set1 = new CustomBarSet;
25 CustomBarSet *set2 = new CustomBarSet;
26 CustomBarSet *set3 = new CustomBarSet;
27 CustomBarSet *set4 = new CustomBarSet;
23 CustomBarSet *set0 = new CustomBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
28 28
29 29 // Create some test data to chart
30 30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
@@ -8,6 +8,7 Bar::Bar(QGraphicsItem *parent)
8 8 : QGraphicsObject(parent)
9 9 {
10 10 setAcceptedMouseButtons(Qt::LeftButton);
11 setAcceptHoverEvents(true);
11 12 }
12 13
13 14 void Bar::setSize(const QSizeF& size)
@@ -23,11 +24,6 void Bar::resize( qreal w, qreal h )
23 24 mHeight = h;
24 25 }
25 26
26 void Bar::setColor( QColor col )
27 {
28 mColor = col;
29 }
30
31 27 void Bar::setPos(qreal x, qreal y)
32 28 {
33 29 mXpos = x;
@@ -72,14 +68,14 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
72 68 emit clicked();
73 69 }
74 70
75 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
71 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
76 72 {
77 emit hoverEntered();
73 emit hoverEntered(event);
78 74 }
79 75
80 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
76 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
81 77 {
82 emit hoverLeaved();
78 emit hoverLeaved(event);
83 79 }
84 80
85 81 #include "moc_bar_p.cpp"
@@ -8,7 +8,7
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 // Single bar item of chart
11 // Single visual bar item of chart
12 12 class Bar : public QGraphicsObject
13 13 {
14 14 Q_OBJECT
@@ -19,11 +19,10 public: // from ChartItem
19 19 void setSize(const QSizeF &size);
20 20
21 21 // Layout Stuff
22 void resize(qreal w, qreal h); // Size of bar.
22 void resize(qreal w, qreal h);
23 23 void setPos(qreal x, qreal y);
24 24 void setPen(QPen pen);
25 25 void setBrush(QBrush brush);
26 void setColor( QColor col); // deprecated
27 26
28 27 public:
29 28 // From QGraphicsItem
@@ -35,8 +34,8 public:
35 34
36 35 Q_SIGNALS:
37 36 void clicked();
38 void hoverEntered();
39 void hoverLeaved();
37 void hoverEntered(QGraphicsSceneHoverEvent *event);
38 void hoverLeaved(QGraphicsSceneHoverEvent *event);
40 39
41 40 private:
42 41
@@ -56,6 +56,16 QBarSet* BarChartModel::setAt(int index)
56 56 return mDataModel.at(index);
57 57 }
58 58
59 QList<QString> BarChartModel::legend()
60 {
61 QList<QString> legend;
62
63 for (int i=0; i<mDataModel.count(); i++) {
64 legend.append(mDataModel.at(i)->name());
65 }
66 return legend;
67 }
68
59 69 int BarChartModel::countSets()
60 70 {
61 71 return mDataModel.count();
@@ -24,6 +24,7 public:
24 24 void removeBarSet(QBarSet *set);
25 25 QBarSet* nextSet(bool getFirst);
26 26 QBarSet *setAt(int index);
27 QList<QString> legend();
27 28
28 29 int countSets(); // Number of sets in model
29 30 int countCategories(); // Number of categories
@@ -70,8 +70,9 void BarPresenterBase::dataChanged()
70 70 childItems().append(bar);
71 71 mBars.append(bar);
72 72 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
73 connect(bar,SIGNAL(hoverEntered()),set,SLOT(barHoverEntered()));
74 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
73 // TODO: should the event be passed to set or not?
74 //connect(bar,SIGNAL(hoverEntered(QGraphicsSceneHoverEvent* event)),set,SLOT(barHoverEntered(QGraphicsSceneHoverEvent* event)));
75 //connect(bar,SIGNAL(hoverLeaved(QGraphicsSceneHoverEvent* event)),set,SLOT(barHoverLeaved(QGraphicsSceneHoverEvent *event)));
75 76 }
76 77 }
77 78
@@ -133,6 +134,17 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
133 134 setPos(rect.topLeft());
134 135 }
135 136
137
138 void BarPresenterBase::barHoverEntered(QGraphicsSceneHoverEvent *event)
139 {
140 //TODO: show tooltip (name of series, where bar belongs...)
141 }
142
143 void BarPresenterBase::barHoverLeaved(QGraphicsSceneHoverEvent *event)
144 {
145 //TODO: hide tooltip (name of series, where bar belongs...)
146 }
147
136 148 #include "moc_barpresenterbase.cpp"
137 149
138 150 QTCOMMERCIALCHART_END_NAMESPACE
@@ -46,6 +46,9 protected slots:
46 46 void handleDomainChanged(const Domain& domain);
47 47 void handleGeometryChanged(const QRectF& size);
48 48
49 void barHoverEntered(QGraphicsSceneHoverEvent *event); // Internal.
50 void barHoverLeaved(QGraphicsSceneHoverEvent *event);
51
49 52 protected:
50 53
51 54 // TODO: consider these.
@@ -43,10 +43,12 void BarValue::setPos(qreal x, qreal y)
43 43 mYpos = y;
44 44 }
45 45
46 /*
46 47 bool BarValue::belongsToSet(QBarSet *set)
47 48 {
48 49 return (&mBarSet == set);
49 50 }
51 */
50 52
51 53 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 54 {
@@ -27,7 +27,8 public:
27 27 void resize(qreal w, qreal h);
28 28 void setPos(qreal x, qreal y);
29 29
30 bool belongsToSet(QBarSet *set);
30 // Propably not needed.
31 // bool belongsToSet(QBarSet *set);
31 32
32 33 // From QGraphicsItem
33 34 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
@@ -84,11 +84,14 void PercentBarPresenter::layoutChanged()
84 84 BarValue* value = mFloatingValues.at(itemIndex);
85 85
86 86 // TODO: remove hard coding, apply layout
87 value->resize(100,50);
87 88 value->setPos(xPos, yPos-barHeight/2);
88 89 value->setPen(QPen(QColor(255,255,255,255)));
89 90
90 91 if (mModel.valueAt(set,category) != 0) {
91 QString vString(QString::number(mModel.percentageAt(set,category) * 100));
92 int p = mModel.percentageAt(set,category) * 100;
93 QString vString(QString::number(p));
94 vString.truncate(3);
92 95 vString.append("%");
93 96 value->setValueString(vString);
94 97 } else {
@@ -15,11 +15,15 QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent)
15 15
16 16 void QBarChartSeries::addBarSet(QBarSet *set)
17 17 {
18 // connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
19 // connect(this,SIGNAL(hoverNamesEnabled(bool)),set,SLOT(enableHoverNames(bool)));
18 20 mModel->addBarSet(set);
19 21 }
20 22
21 23 void QBarChartSeries::removeBarSet(QBarSet *set)
22 24 {
25 // disconnect(set,SLOT(enableFloatingValues(bool)));
26 // disconnect(set,SLOT(enableHoverNames(bool)));
23 27 mModel->removeBarSet(set);
24 28 }
25 29
@@ -33,6 +37,21 QBarSet* QBarChartSeries::nextSet(bool getFirst)
33 37 return mModel->nextSet(getFirst);
34 38 }
35 39
40 QList<QString> QBarChartSeries::legend()
41 {
42 return mModel->legend();
43 }
44
45 void QBarChartSeries::enableFloatingValues(bool enabled)
46 {
47 emit floatingValuesEnabled(enabled);
48 }
49
50 void QBarChartSeries::enableHoverNames(bool enabled)
51 {
52 emit hoverNamesEnabled(enabled);
53 }
54
36 55 int QBarChartSeries::countCategories()
37 56 {
38 57 return mModel->countCategories();
@@ -12,6 +12,7 class QBarSet;
12 12 class BarChartModel;
13 13
14 14 // Container for series
15 // TODO: derive Stacked and percent bar charts from this?
15 16 class QTCOMMERCIALCHART_EXPORT QBarChartSeries : public QChartSeries
16 17 {
17 18 Q_OBJECT
@@ -20,13 +21,16 public:
20 21
21 22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
22 23
23 void addBarSet(QBarSet *set); // Takes ownership of set
24 void removeBarSet(QBarSet *set); // Removes set, releases ownership.
24 void addBarSet(QBarSet *set); // Takes ownership of set
25 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
25 26 int countSets();
26 27 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
27 28
28 //TODO:
29 //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
29 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
30
31 // Disabled by default. Call these to change behavior.
32 void enableFloatingValues(bool enabled=true);
33 void enableHoverNames(bool enabled=true);
30 34
31 35 // TODO: Functions below this are not part of api and will be moved
32 36 // to private implementation, when we start using it (not part of api)
@@ -41,10 +45,16 public:
41 45 signals:
42 46 void changed(int index);
43 47
44 public Q_SLOTS:
48 // TODO: these to private implementation.
49 void floatingValuesEnabled(bool enabled);
50 void hoverNamesEnabled(bool enabled);
51
52
53 //public Q_SLOTS:
45 54
46 55 private:
47 56 BarChartModel* mModel;
57
48 58 };
49 59
50 60 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,10 +3,12
3 3
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 QBarSet::QBarSet(QObject *parent)
6 QBarSet::QBarSet(QString name, QObject *parent)
7 7 : QObject(parent)
8 ,mName(name)
9 ,mHoverNamesEnabled(true) // TODO: these 2 as false by default, when implementation is ready
10 ,mFloatingValuesEnabled(true)
8 11 {
9 mFloatingValuesVisible = false;
10 12 }
11 13
12 14 void QBarSet::setName(QString name)
@@ -59,9 +61,16 const QBrush& QBarSet::brush() const
59 61 return mBrush;
60 62 }
61 63
62 bool QBarSet::isFloatingValuesVisible()
64 void QBarSet::enableFloatingValues(bool enabled)
63 65 {
64 return mFloatingValuesVisible;
66 qDebug() << "QBarSet::enableFloatingValues" << enabled;
67 mFloatingValuesEnabled = enabled;
68 }
69
70 void QBarSet::enableHoverNames(bool enabled)
71 {
72 qDebug() << "QBarSet::enableHoverNames" << enabled;
73 mHoverNamesEnabled = enabled;
65 74 }
66 75
67 76 void QBarSet::barClicked()
@@ -75,13 +84,17 void QBarSet::barClicked()
75 84 void QBarSet::barHoverEntered()
76 85 {
77 86 qDebug() << "QBarset::barHoverEntered" << this;
78 emit hoverEnter();
87 if (mHoverNamesEnabled) {
88 emit hoverEnter();
89 }
79 90 }
80 91
81 92 void QBarSet::barHoverLeaved()
82 93 {
83 94 qDebug() << "QBarset::barHoverLeaved" << this;
84 emit hoverLeave();
95 if (mHoverNamesEnabled) {
96 emit hoverLeave();
97 }
85 98 }
86 99
87 100 #include "moc_qbarset.cpp"
@@ -11,7 +11,7 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 11 {
12 12 Q_OBJECT
13 13 public:
14 QBarSet(QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15 15
16 16 void setName(QString name);
17 17 QString name();
@@ -19,7 +19,7 public:
19 19
20 20 int count(); // count of values in set
21 21 qreal valueAt(int index); // for modifying individual values
22 void setValue(int index, qreal value); //
22 void setValue(int index, qreal value); // setter for individual value
23 23
24 24 void setPen(const QPen& pen);
25 25 const QPen& pen() const;
@@ -27,15 +27,18 public:
27 27 void setBrush(const QBrush& brush);
28 28 const QBrush& brush() const;
29 29
30 bool isFloatingValuesVisible();
31
32 30 Q_SIGNALS:
33 void clicked();
31 void clicked(); // Clicked and hover signals exposed to user
34 32 void hoverEnter();
35 33 void hoverLeave();
36 void toggleFloatingValues();
34 void toggleFloatingValues(); // Private signal, TODO: move to private impl
37 35
38 36 public Q_SLOTS:
37 void enableFloatingValues(bool enabled); // enables floating values on top of bars
38 void enableHoverNames(bool enabled); // enableHoverEvents would be better name?
39
40 // TODO: these slots belong to private implementation.
41 // These are for single bars to notify set about internal events
39 42 void barClicked();
40 43 void barHoverEntered();
41 44 void barHoverLeaved();
@@ -47,7 +50,9 private:
47 50 QPen mPen;
48 51 QBrush mBrush;
49 52
50 bool mFloatingValuesVisible;
53 // TODO: to pimpl
54 bool mFloatingValuesEnabled;
55 bool mHoverNamesEnabled;
51 56 };
52 57
53 58 QTCOMMERCIALCHART_END_NAMESPACE
@@ -15,11 +15,15 QPercentBarChartSeries::QPercentBarChartSeries(QBarCategory *category, QObject *
15 15
16 16 void QPercentBarChartSeries::addBarSet(QBarSet *set)
17 17 {
18 connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
19 connect(this,SIGNAL(hoverNamesEnabled(bool)),set,SLOT(enableHoverNames(bool)));
18 20 mModel->addBarSet(set);
19 21 }
20 22
21 23 void QPercentBarChartSeries::removeBarSet(QBarSet *set)
22 24 {
25 disconnect(set,SLOT(enableFloatingValues(bool)));
26 disconnect(set,SLOT(enableHoverNames(bool)));
23 27 mModel->removeBarSet(set);
24 28 }
25 29
@@ -33,6 +37,11 QBarSet* QPercentBarChartSeries::nextSet(bool getFirst)
33 37 return mModel->nextSet(getFirst);
34 38 }
35 39
40 QList<QString> QPercentBarChartSeries::legend()
41 {
42 return mModel->legend();
43 }
44
36 45 int QPercentBarChartSeries::countCategories()
37 46 {
38 47 return mModel->countCategories();
@@ -21,12 +21,15 public:
21 21 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
22 22
23 23 void addBarSet(QBarSet *set); // Takes ownership
24 void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned.
24 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
25 25 int countSets();
26 26 QBarSet* nextSet(bool first=false); // Returns first set, if called with true
27 27
28 //TODO:
29 //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
28 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
29
30 // Disabled by default. Call these to change behavior.
31 void enableFloatingValues(bool enabled=true);
32 void enableHoverNames(bool enabled=true);
30 33
31 34 // TODO: Functions below this are not part of api and will be moved
32 35 // to private implementation, when we start using it (not part of api)
@@ -15,11 +15,15 QStackedBarChartSeries::QStackedBarChartSeries(QBarCategory *category, QObject *
15 15
16 16 void QStackedBarChartSeries::addBarSet(QBarSet *set)
17 17 {
18 connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
19 connect(this,SIGNAL(hoverNamesEnabled(bool)),set,SLOT(enableHoverNames(bool)));
18 20 mModel->addBarSet(set);
19 21 }
20 22
21 23 void QStackedBarChartSeries::removeBarSet(QBarSet *set)
22 24 {
25 disconnect(set,SLOT(enableFloatingValues(bool)));
26 disconnect(set,SLOT(enableHoverNames(bool)));
23 27 mModel->removeBarSet(set);
24 28 }
25 29
@@ -33,6 +37,11 QBarSet* QStackedBarChartSeries::nextSet(bool getFirst)
33 37 return mModel->nextSet(getFirst);
34 38 }
35 39
40 QList<QString> QStackedBarChartSeries::legend()
41 {
42 return mModel->legend();
43 }
44
36 45 int QStackedBarChartSeries::countCategories()
37 46 {
38 47 return mModel->countCategories();
@@ -22,11 +22,15 public:
22 22
23 23 // Set handling
24 24 void addBarSet(QBarSet *set); // Takes ownership
25 void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned.
25 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
26 26 int countSets();
27 27 QBarSet* nextSet(bool first=false); // Returns first set, if called with true
28 28
29 //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
29 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
30
31 // Disabled by default. Call these to change behavior.
32 void enableFloatingValues(bool enabled=true);
33 void enableHoverNames(bool enabled=true);
30 34
31 35 // TODO: Functions below this are not part of api and will be moved
32 36 // to private implementation, when we start using it (not part of api)
General Comments 0
You need to be logged in to leave comments. Login now