From 2c194d26bbea8995e3688cca53af781c3321b518 2012-03-13 10:48:09 From: sauimone Date: 2012-03-13 10:48:09 Subject: [PATCH] Separated legend marker to private header. Added signals for left and right mouse click --- diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 6b3195a..a3807d9 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -374,7 +374,7 @@ void ChartPresenter::scroll(int dx,int dy) m_dataset->scrollDomain(dx,dy,geometry().size()); - if(m_animator){ + if(m_animator){ m_animator->setState(ChartAnimator::ShowState); } } diff --git a/src/legendmarker.cpp b/src/legendmarker.cpp new file mode 100644 index 0000000..e51abee --- /dev/null +++ b/src/legendmarker.cpp @@ -0,0 +1,63 @@ +#include "qchartglobal.h" +#include "legendmarker_p.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent) + : QGraphicsObject(parent) + ,mSeries(series) + ,mBoundingRect(0,0,1,1) + { + setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); + } + +void LegendMarker::setBoundingRect(const QRectF rect) +{ + mBoundingRect = rect; +} + +void LegendMarker::setBrush(const QBrush brush) +{ + mBrush = brush; +} + +void LegendMarker::setName(const QString name) +{ + mName = name; +} + +QString LegendMarker::name() const +{ + return mName; +} + +QColor LegendMarker::color() const +{ + return mBrush.color(); +} + +void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + painter->setBrush(mBrush); + painter->drawRect(mBoundingRect); +} + +QRectF LegendMarker::boundingRect() const +{ + return mBoundingRect; +} + +void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + emit clicked(mSeries, mName); + } else if (event->button() == Qt::RightButton) { + emit rightClicked(mSeries, mName); + } +} + +#include "moc_legendmarker_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legendmarker_p.h b/src/legendmarker_p.h new file mode 100644 index 0000000..c3d82e8 --- /dev/null +++ b/src/legendmarker_p.h @@ -0,0 +1,44 @@ +#ifndef LEGENDMARKER_P_H +#define LEGENDMARKER_P_H + +#include "qchartglobal.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QSeries; + + +class LegendMarker : public QGraphicsObject +{ + Q_OBJECT +public: + LegendMarker(QSeries* series, QGraphicsItem *parent = 0); + void setBoundingRect(const QRectF rect); + void setBrush(const QBrush brush); + void setName(const QString name); + QString name() const; + QColor color() const; + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + + QRectF boundingRect() const; + +public: + // From QGraphicsObject + void mousePressEvent(QGraphicsSceneMouseEvent *event); + +Q_SIGNALS: + void clicked(QSeries* series, QString name); + void rightClicked(QSeries* series, QString name); + +private: + QRectF mBoundingRect; + QBrush mBrush; + QString mName; + QSeries* mSeries; +}; + +QTCOMMERCIALCHART_END_NAMESPACE +#endif // LEGENDMARKER_P_H diff --git a/src/qlegend.cpp b/src/qlegend.cpp index 3194c83..eea8c83 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -1,38 +1,13 @@ #include "qchartglobal.h" #include "qlegend.h" #include "qseries.h" +#include "legendmarker_p.h" #include #include -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -// TODO: this to legendmarker_p.h header -class LegendMarker : public QGraphicsItem -{ -public: - LegendMarker(QGraphicsItem *parent = 0) : QGraphicsItem(parent) - ,mBoundingRect(0,0,1,1) - {} - - void setBoundingRect(const QRectF rect) { mBoundingRect = rect; } - void setBrush(const QBrush brush) { mBrush = brush; } - void setName(const QString name) { mName = name; } - QString name() const { return mName; } - QColor color() const { return mBrush.color(); } - - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) - { - painter->setBrush(mBrush); - painter->drawRect(mBoundingRect); - } - - QRectF boundingRect() const { return mBoundingRect; } +#include -private: - QRectF mBoundingRect; - QBrush mBrush; - QString mName; -}; +QTCOMMERCIALCHART_BEGIN_NAMESPACE QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent) @@ -49,7 +24,7 @@ void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q foreach(LegendMarker* m, mMarkers) { QRectF r = m->boundingRect(); painter->setPen(m->color()); - painter->drawText(r.x() + 20, r.y() + r.height(), m->name()); + painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name()); } } @@ -90,19 +65,23 @@ void QLegend::handleGeometryChanged(const QRectF& size) void QLegend::dataChanged() { - foreach (QGraphicsItem* i, childItems()) { - delete i; + foreach (LegendMarker* marker, mMarkers) { + disconnect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString))); + disconnect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString))); + delete marker; } mMarkers.clear(); foreach (QSeries* s, mSeriesList) { for (int i=0; ilegendEntries().count(); i++) { - LegendMarker *marker = new LegendMarker(this); + LegendMarker *marker = new LegendMarker(s, this); marker->setBrush(s->legendEntries().at(i).mBrush); marker->setName(s->legendEntries().at(i).mName); mMarkers.append(marker); -// childItems().append(marker); + childItems().append(marker); + connect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString))); + connect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString))); } } } diff --git a/src/qlegend.h b/src/qlegend.h index 0612833..b8935e0 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -24,7 +24,10 @@ public: QBrush backgroundBrush() const; signals: - + // for interactions. + void clicked(QSeries* series, QString name); + void rightClicked(QSeries* series, QString name); + public slots: void handleSeriesAdded(QSeries* series,Domain* domain); void handleSeriesRemoved(QSeries* series); diff --git a/src/src.pro b/src/src.pro index 8182d27..c9155ad 100644 --- a/src/src.pro +++ b/src/src.pro @@ -14,14 +14,16 @@ SOURCES += \ qchart.cpp \ qchartview.cpp \ qseries.cpp \ - qlegend.cpp + qlegend.cpp \ + legendmarker.cpp PRIVATE_HEADERS += \ chartdataset_p.h \ chartitem_p.h \ chartpresenter_p.h \ charttheme_p.h \ - domain_p.h -PUBLIC_HEADERS += \ + domain_p.h \ + legendmarker_p.h +PUBLIC_HEADERS += \ qchart.h \ qchartglobal.h \ qseries.h \