##// END OF EJS Templates
right click feature for bar series. Enables drilldown
sauimone -
r412:917c9ad18551
parent child
Show More
@@ -0,0 +1,17
1 #include "barcategory_p.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 BarCategory::BarCategory(QString name, QObject *parent) :
5 QObject(parent)
6 ,mName(name)
7 {
8 }
9
10 void BarCategory::barRightClickEvent()
11 {
12 // TODO:
13 emit rightClicked(mName);
14 }
15
16 #include "moc_barcategory_p.cpp"
17 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,26
1 #ifndef BARCATEGORY_P_H
2 #define BARCATEGORY_P_H
3
4 #include <QObject>
5 #include <qchartglobal.h>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 // Event handler for bar category
9 class BarCategory : public QObject
10 {
11 Q_OBJECT
12 public:
13 explicit BarCategory(QString name, QObject *parent = 0);
14
15 signals:
16 void rightClicked(QString name); // "We want something to happen that involves this category"
17
18 public slots:
19 void barRightClickEvent();
20
21 private:
22 QString mName;
23 };
24
25 QTCOMMERCIALCHART_END_NAMESPACE
26 #endif // BARCATEGORY_P_H
@@ -8,7 +8,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 Bar::Bar(QGraphicsItem *parent)
8 Bar::Bar(QGraphicsItem *parent)
9 : QGraphicsObject(parent)
9 : QGraphicsObject(parent)
10 {
10 {
11 setAcceptedMouseButtons(Qt::LeftButton);
11 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
12 setAcceptHoverEvents(true);
12 setAcceptHoverEvents(true);
13 }
13 }
14
14
@@ -64,9 +64,13 QRectF Bar::boundingRect() const
64 return r;
64 return r;
65 }
65 }
66
66
67 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
67 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event)
68 {
68 {
69 emit clicked();
69 if (event->button() == Qt::LeftButton) {
70 emit clicked();
71 } else if (event->button() == Qt::RightButton) {
72 emit rightClicked();
73 }
70 }
74 }
71
75
72 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
76 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
@@ -34,6 +34,7 public:
34
34
35 Q_SIGNALS:
35 Q_SIGNALS:
36 void clicked();
36 void clicked();
37 void rightClicked();
37 void hoverEntered(QPoint pos);
38 void hoverEntered(QPoint pos);
38 void hoverLeaved();
39 void hoverLeaved();
39
40
@@ -43,7 +44,6 private:
43 qreal mWidth;
44 qreal mWidth;
44 qreal mXpos;
45 qreal mXpos;
45 qreal mYpos;
46 qreal mYpos;
46 QColor mColor;
47
47
48 QBrush mBrush;
48 QBrush mBrush;
49 QPen mPen;
49 QPen mPen;
@@ -14,7 +14,8 SOURCES += \
14 $$PWD/qstackedbarseries.cpp \
14 $$PWD/qstackedbarseries.cpp \
15 $$PWD/separator.cpp \
15 $$PWD/separator.cpp \
16 $$PWD/stackedbarpresenter.cpp \
16 $$PWD/stackedbarpresenter.cpp \
17 $$PWD/barvalue.cpp
17 $$PWD/barvalue.cpp \
18 $$PWD/barcategory.cpp
18
19
19 PRIVATE_HEADERS += \
20 PRIVATE_HEADERS += \
20 $$PWD/bar_p.h \
21 $$PWD/bar_p.h \
@@ -25,12 +26,12 PRIVATE_HEADERS += \
25 $$PWD/percentbarpresenter_p.h \
26 $$PWD/percentbarpresenter_p.h \
26 $$PWD/separator_p.h \
27 $$PWD/separator_p.h \
27 $$PWD/stackedbarpresenter_p.h \
28 $$PWD/stackedbarpresenter_p.h \
28 $$PWD/barvalue_p.h
29 $$PWD/barvalue_p.h \
30 $$PWD/barcategory_p.h
29
31
30 PUBLIC_HEADERS += \
32 PUBLIC_HEADERS += \
31 $$PWD/qbarseries.h \
33 $$PWD/qbarseries.h \
32 $$PWD/qbarset.h \
34 $$PWD/qbarset.h \
33 $$PWD/qpercentbarseries.h \
35 $$PWD/qpercentbarseries.h \
34 $$PWD/qstackedbarseries.h
36 $$PWD/qstackedbarseries.h
35
36
37
@@ -2,6 +2,7
2 #include <QVector>
2 #include <QVector>
3 #include <QDebug>
3 #include <QDebug>
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5 #include "barcategory_p.h"
5 #include "qbarset.h"
6 #include "qbarset.h"
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -10,6 +11,10 BarChartModel::BarChartModel(QStringList categories, QObject *parent) :
10 QObject(parent)
11 QObject(parent)
11 ,mCategory(categories)
12 ,mCategory(categories)
12 {
13 {
14 for (int i=0; i<mCategory.count(); i++) {
15 BarCategory* cat = new BarCategory(mCategory.at(i), this);
16 mCategoryObjects.append(cat);
17 }
13 }
18 }
14
19
15 QStringList BarChartModel::category()
20 QStringList BarChartModel::category()
@@ -162,11 +167,16 qreal BarChartModel::maxCategorySum()
162 return max;
167 return max;
163 }
168 }
164
169
165 QString BarChartModel::label(int category)
170 QString BarChartModel::categoryName(int category)
166 {
171 {
167 return mCategory.at(category);
172 return mCategory.at(category);
168 }
173 }
169
174
175 BarCategory* BarChartModel::categoryObject(int category)
176 {
177 return mCategoryObjects.at(category);
178 }
179
170 #include "moc_barchartmodel_p.cpp"
180 #include "moc_barchartmodel_p.cpp"
171
181
172 QTCOMMERCIALCHART_END_NAMESPACE
182 QTCOMMERCIALCHART_END_NAMESPACE
@@ -12,6 +12,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 // TODO: Implement as QAbstractItemModel?
12 // TODO: Implement as QAbstractItemModel?
13
13
14 class QBarSet;
14 class QBarSet;
15 class BarCategory;
15
16
16 class BarChartModel : public QObject //, public QAbstractItemModel
17 class BarChartModel : public QObject //, public QAbstractItemModel
17 {
18 {
@@ -38,7 +39,8 public:
38 qreal categorySum(int category);
39 qreal categorySum(int category);
39 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
40 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
40
41
41 QString label(int category);
42 QString categoryName(int category);
43 BarCategory* categoryObject(int category);
42
44
43 signals:
45 signals:
44 void modelUpdated();
46 void modelUpdated();
@@ -49,6 +51,7 private:
49
51
50 QList<QBarSet*> mDataModel;
52 QList<QBarSet*> mDataModel;
51 QStringList mCategory;
53 QStringList mCategory;
54 QList<BarCategory*> mCategoryObjects;
52
55
53 int mCurrentSet;
56 int mCurrentSet;
54
57
@@ -3,6 +3,7
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 "barcategory_p.h"
6 #include "qbarset.h"
7 #include "qbarset.h"
7 #include "qbarseries.h"
8 #include "qbarseries.h"
8 #include <QDebug>
9 #include <QDebug>
@@ -56,7 +57,7 void BarPresenterBase::setBarWidth( int w )
56 void BarPresenterBase::dataChanged()
57 void BarPresenterBase::dataChanged()
57 {
58 {
58 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
59 // 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";
60 // qDebug() << "datachanged";
60 // Delete old bars
61 // Delete old bars
61 foreach (QGraphicsItem* item, childItems()) {
62 foreach (QGraphicsItem* item, childItems()) {
62 delete item;
63 delete item;
@@ -69,14 +70,16 void BarPresenterBase::dataChanged()
69
70
70 // Create new graphic items for bars
71 // Create new graphic items for bars
71 for (int c=0; c<mSeries->categoryCount(); c++) {
72 for (int c=0; c<mSeries->categoryCount(); c++) {
73 BarCategory *category = mSeries->categoryObject(c);
72 for (int s=0; s<mSeries->barsetCount(); s++) {
74 for (int s=0; s<mSeries->barsetCount(); s++) {
73 QBarSet *set = mSeries->barsetAt(s);
75 QBarSet *set = mSeries->barsetAt(s);
74 Bar *bar = new Bar(this);
76 Bar *bar = new Bar(this);
75 childItems().append(bar);
77 childItems().append(bar);
76 mBars.append(bar);
78 mBars.append(bar);
77 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
79 connect(bar,SIGNAL(clicked()),set,SLOT(barClickedEvent()));
78 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint)));
80 connect(bar,SIGNAL(rightClicked()),category,SLOT(barRightClickEvent()));
79 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
81 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
82 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
80 }
83 }
81 }
84 }
82
85
@@ -84,7 +87,7 void BarPresenterBase::dataChanged()
84 int count = mSeries->categoryCount();
87 int count = mSeries->categoryCount();
85 for (int i=0; i<count; i++) {
88 for (int i=0; i<count; i++) {
86 BarLabel* label = new BarLabel(this);
89 BarLabel* label = new BarLabel(this);
87 label->set(mSeries->label(i));
90 label->set(mSeries->categoryName(i));
88 childItems().append(label);
91 childItems().append(label);
89 mLabels.append(label);
92 mLabels.append(label);
90 }
93 }
@@ -2,7 +2,7
2 #include "qbarseries.h"
2 #include "qbarseries.h"
3 #include "qbarset.h"
3 #include "qbarset.h"
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5
5 #include "barcategory_p.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
@@ -53,6 +53,10 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
53 : QSeries(parent)
53 : QSeries(parent)
54 ,mModel(new BarChartModel(categories, this))
54 ,mModel(new BarChartModel(categories, this))
55 {
55 {
56 for (int i=0; i<mModel->categoryCount(); i++) {
57 BarCategory *categoryObject = mModel->categoryObject(i);
58 connect(categoryObject, SIGNAL(rightClicked(QString)), this, SIGNAL(categoryRightClicked(QString)));
59 }
56 }
60 }
57
61
58 /*!
62 /*!
@@ -114,9 +118,9 QList<QSeries::Legend> QBarSeries::legend()
114 /*!
118 /*!
115 \internal \a category
119 \internal \a category
116 */
120 */
117 QString QBarSeries::label(int category)
121 QString QBarSeries::categoryName(int category)
118 {
122 {
119 return mModel->label(category);
123 return mModel->categoryName(category);
120 }
124 }
121
125
122 /*!
126 /*!
@@ -225,6 +229,12 BarChartModel& QBarSeries::model()
225 return *mModel;
229 return *mModel;
226 }
230 }
227
231
232 BarCategory* QBarSeries::categoryObject(int category)
233 {
234 return mModel->categoryObject(category);
235 }
236
237
228 #include "moc_qbarseries.cpp"
238 #include "moc_qbarseries.cpp"
229
239
230 QTCOMMERCIALCHART_END_NAMESPACE
240 QTCOMMERCIALCHART_END_NAMESPACE
@@ -8,6 +8,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QBarSet;
9 class QBarSet;
10 class BarChartModel;
10 class BarChartModel;
11 class BarCategory;
11
12
12 // Container for series
13 // Container for series
13 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
14 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
@@ -29,8 +30,8 public:
29 // TODO: Functions below this are not part of api and will be moved
30 // TODO: Functions below this are not part of api and will be moved
30 // to private implementation, when we start using it
31 // to private implementation, when we start using it
31 // TODO: TO PIMPL --->
32 // TODO: TO PIMPL --->
32 QBarSet *barsetAt(int index);
33 QBarSet* barsetAt(int index);
33 QString label(int category);
34 QString categoryName(int category);
34 qreal min();
35 qreal min();
35 qreal max();
36 qreal max();
36 qreal valueAt(int set, int category);
37 qreal valueAt(int set, int category);
@@ -38,10 +39,12 public:
38 qreal categorySum(int category);
39 qreal categorySum(int category);
39 qreal maxCategorySum();
40 qreal maxCategorySum();
40 BarChartModel& model();
41 BarChartModel& model();
42 BarCategory* categoryObject(int category);
41 // <--- TO PIMPL
43 // <--- TO PIMPL
42
44
43 signals:
45 signals:
44 void changed(int index);
46 void changed(int index);
47 void categoryRightClicked(QString category);
45
48
46 // TODO: internal signals, these to private implementation.
49 // TODO: internal signals, these to private implementation.
47 // TODO: TO PIMPL --->
50 // TODO: TO PIMPL --->
@@ -133,9 +133,8 QBrush QBarSet::brush()
133 /*!
133 /*!
134 \internal
134 \internal
135 */
135 */
136 void QBarSet::barClicked()
136 void QBarSet::barClickedEvent()
137 {
137 {
138 // qDebug() << "QBarset::barClicked" << this;
139 // Some bar of this set has been clicked
138 // Some bar of this set has been clicked
140 // TODO: What happens then?
139 // TODO: What happens then?
141 emit clicked(); // Notify that set has been clicked
140 emit clicked(); // Notify that set has been clicked
@@ -144,7 +143,7 void QBarSet::barClicked()
144 /*!
143 /*!
145 \internal \a pos
144 \internal \a pos
146 */
145 */
147 void QBarSet::barHoverEntered(QPoint pos)
146 void QBarSet::barHoverEnterEvent(QPoint pos)
148 {
147 {
149 emit showToolTip(pos, mName);
148 emit showToolTip(pos, mName);
150 emit hoverEnter(pos);
149 emit hoverEnter(pos);
@@ -153,12 +152,8 void QBarSet::barHoverEntered(QPoint pos)
153 /*!
152 /*!
154 \internal
153 \internal
155 */
154 */
156 void QBarSet::barHoverLeaved()
155 void QBarSet::barHoverLeaveEvent()
157 {
156 {
158 // qDebug() << "QBarset::barHoverLeaved" << this;
159 // if (mToolTipEnabled) {
160 // TODO: do what?
161 // }
162 // Emit signal to user of charts
157 // Emit signal to user of charts
163 emit hoverLeave();
158 emit hoverLeave();
164 }
159 }
@@ -41,9 +41,9 Q_SIGNALS:
41 public Q_SLOTS:
41 public Q_SLOTS:
42 // These are for internal communication
42 // These are for internal communication
43 // TODO: TO PIMPL --->
43 // TODO: TO PIMPL --->
44 void barClicked();
44 void barClickedEvent();
45 void barHoverEntered(QPoint pos);
45 void barHoverEnterEvent(QPoint pos);
46 void barHoverLeaved();
46 void barHoverLeaveEvent();
47 // <--- TO PIMPL
47 // <--- TO PIMPL
48
48
49 private:
49 private:
General Comments 0
You need to be logged in to leave comments. Login now