From 917c9ad1855195c2eff28a27ee7f00cff4bf6fcc 2012-02-29 15:03:39 From: sauimone Date: 2012-02-29 15:03:39 Subject: [PATCH] right click feature for bar series. Enables drilldown --- diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index a26a10c..13fec18 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -8,7 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Bar::Bar(QGraphicsItem *parent) : QGraphicsObject(parent) { - setAcceptedMouseButtons(Qt::LeftButton); + setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); setAcceptHoverEvents(true); } @@ -64,9 +64,13 @@ QRectF Bar::boundingRect() const return r; } -void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) +void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) { - emit clicked(); + if (event->button() == Qt::LeftButton) { + emit clicked(); + } else if (event->button() == Qt::RightButton) { + emit rightClicked(); + } } void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) diff --git a/src/barchart/bar_p.h b/src/barchart/bar_p.h index 12465a2..704f44b 100644 --- a/src/barchart/bar_p.h +++ b/src/barchart/bar_p.h @@ -34,6 +34,7 @@ public: Q_SIGNALS: void clicked(); + void rightClicked(); void hoverEntered(QPoint pos); void hoverLeaved(); @@ -43,7 +44,6 @@ private: qreal mWidth; qreal mXpos; qreal mYpos; - QColor mColor; QBrush mBrush; QPen mPen; diff --git a/src/barchart/barcategory.cpp b/src/barchart/barcategory.cpp new file mode 100644 index 0000000..f405dd9 --- /dev/null +++ b/src/barchart/barcategory.cpp @@ -0,0 +1,17 @@ +#include "barcategory_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +BarCategory::BarCategory(QString name, QObject *parent) : + QObject(parent) + ,mName(name) +{ +} + +void BarCategory::barRightClickEvent() +{ + // TODO: + emit rightClicked(mName); +} + +#include "moc_barcategory_p.cpp" +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barcategory_p.h b/src/barchart/barcategory_p.h new file mode 100644 index 0000000..96df531 --- /dev/null +++ b/src/barchart/barcategory_p.h @@ -0,0 +1,26 @@ +#ifndef BARCATEGORY_P_H +#define BARCATEGORY_P_H + +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +// Event handler for bar category +class BarCategory : public QObject +{ + Q_OBJECT +public: + explicit BarCategory(QString name, QObject *parent = 0); + +signals: + void rightClicked(QString name); // "We want something to happen that involves this category" + +public slots: + void barRightClickEvent(); + +private: + QString mName; +}; + +QTCOMMERCIALCHART_END_NAMESPACE +#endif // BARCATEGORY_P_H diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index 3ae84f5..926999c 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -14,7 +14,8 @@ SOURCES += \ $$PWD/qstackedbarseries.cpp \ $$PWD/separator.cpp \ $$PWD/stackedbarpresenter.cpp \ - $$PWD/barvalue.cpp + $$PWD/barvalue.cpp \ + $$PWD/barcategory.cpp PRIVATE_HEADERS += \ $$PWD/bar_p.h \ @@ -25,12 +26,12 @@ PRIVATE_HEADERS += \ $$PWD/percentbarpresenter_p.h \ $$PWD/separator_p.h \ $$PWD/stackedbarpresenter_p.h \ - $$PWD/barvalue_p.h + $$PWD/barvalue_p.h \ + $$PWD/barcategory_p.h PUBLIC_HEADERS += \ $$PWD/qbarseries.h \ $$PWD/qbarset.h \ $$PWD/qpercentbarseries.h \ $$PWD/qstackedbarseries.h - diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index 1af7615..450408f 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -2,6 +2,7 @@ #include #include #include "barchartmodel_p.h" +#include "barcategory_p.h" #include "qbarset.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -10,6 +11,10 @@ BarChartModel::BarChartModel(QStringList categories, QObject *parent) : QObject(parent) ,mCategory(categories) { + for (int i=0; i mDataModel; QStringList mCategory; + QList mCategoryObjects; int mCurrentSet; diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 48247f9..c3974cb 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -3,6 +3,7 @@ #include "barvalue_p.h" #include "barlabel_p.h" #include "separator_p.h" +#include "barcategory_p.h" #include "qbarset.h" #include "qbarseries.h" #include @@ -56,7 +57,7 @@ void BarPresenterBase::setBarWidth( int w ) void BarPresenterBase::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? - qDebug() << "datachanged"; +// qDebug() << "datachanged"; // Delete old bars foreach (QGraphicsItem* item, childItems()) { delete item; @@ -69,14 +70,16 @@ void BarPresenterBase::dataChanged() // Create new graphic items for bars for (int c=0; ccategoryCount(); c++) { + BarCategory *category = mSeries->categoryObject(c); for (int s=0; sbarsetCount(); s++) { QBarSet *set = mSeries->barsetAt(s); Bar *bar = new Bar(this); childItems().append(bar); mBars.append(bar); - connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); - connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint))); - connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); + connect(bar,SIGNAL(clicked()),set,SLOT(barClickedEvent())); + connect(bar,SIGNAL(rightClicked()),category,SLOT(barRightClickEvent())); + connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); + connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); } } @@ -84,7 +87,7 @@ void BarPresenterBase::dataChanged() int count = mSeries->categoryCount(); for (int i=0; iset(mSeries->label(i)); + label->set(mSeries->categoryName(i)); childItems().append(label); mLabels.append(label); } diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 11a0ffa..b67549f 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -2,7 +2,7 @@ #include "qbarseries.h" #include "qbarset.h" #include "barchartmodel_p.h" - +#include "barcategory_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -53,6 +53,10 @@ QBarSeries::QBarSeries(QStringList categories, QObject *parent) : QSeries(parent) ,mModel(new BarChartModel(categories, this)) { + for (int i=0; icategoryCount(); i++) { + BarCategory *categoryObject = mModel->categoryObject(i); + connect(categoryObject, SIGNAL(rightClicked(QString)), this, SIGNAL(categoryRightClicked(QString))); + } } /*! @@ -114,9 +118,9 @@ QList QBarSeries::legend() /*! \internal \a category */ -QString QBarSeries::label(int category) +QString QBarSeries::categoryName(int category) { - return mModel->label(category); + return mModel->categoryName(category); } /*! @@ -225,6 +229,12 @@ BarChartModel& QBarSeries::model() return *mModel; } +BarCategory* QBarSeries::categoryObject(int category) +{ + return mModel->categoryObject(category); +} + + #include "moc_qbarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index b303b85..5e68d4c 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -8,6 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QBarSet; class BarChartModel; +class BarCategory; // Container for series class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries @@ -29,8 +30,8 @@ public: // TODO: Functions below this are not part of api and will be moved // to private implementation, when we start using it // TODO: TO PIMPL ---> - QBarSet *barsetAt(int index); - QString label(int category); + QBarSet* barsetAt(int index); + QString categoryName(int category); qreal min(); qreal max(); qreal valueAt(int set, int category); @@ -38,10 +39,12 @@ public: qreal categorySum(int category); qreal maxCategorySum(); BarChartModel& model(); + BarCategory* categoryObject(int category); // <--- TO PIMPL signals: void changed(int index); + void categoryRightClicked(QString category); // TODO: internal signals, these to private implementation. // TODO: TO PIMPL ---> diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index caa04bf..4d15a4f 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -133,9 +133,8 @@ QBrush QBarSet::brush() /*! \internal */ -void QBarSet::barClicked() +void QBarSet::barClickedEvent() { -// qDebug() << "QBarset::barClicked" << this; // Some bar of this set has been clicked // TODO: What happens then? emit clicked(); // Notify that set has been clicked @@ -144,7 +143,7 @@ void QBarSet::barClicked() /*! \internal \a pos */ -void QBarSet::barHoverEntered(QPoint pos) +void QBarSet::barHoverEnterEvent(QPoint pos) { emit showToolTip(pos, mName); emit hoverEnter(pos); @@ -153,12 +152,8 @@ void QBarSet::barHoverEntered(QPoint pos) /*! \internal */ -void QBarSet::barHoverLeaved() +void QBarSet::barHoverLeaveEvent() { -// qDebug() << "QBarset::barHoverLeaved" << this; -// if (mToolTipEnabled) { - // TODO: do what? -// } // Emit signal to user of charts emit hoverLeave(); } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 6d89792..a3d3e6b 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -41,9 +41,9 @@ Q_SIGNALS: public Q_SLOTS: // These are for internal communication // TODO: TO PIMPL ---> - void barClicked(); - void barHoverEntered(QPoint pos); - void barHoverLeaved(); + void barClickedEvent(); + void barHoverEnterEvent(QPoint pos); + void barHoverLeaveEvent(); // <--- TO PIMPL private: