diff --git a/src/legendscrollbutton.cpp b/src/legendscrollbutton.cpp index 80bcee6..5e4b5f1 100644 --- a/src/legendscrollbutton.cpp +++ b/src/legendscrollbutton.cpp @@ -1,11 +1,13 @@ #include "legendscrollbutton_p.h" +#include "qlegend.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -LegendScrollButton::LegendScrollButton(ScrollButtonId id, QGraphicsItem *parent) - : QGraphicsPolygonItem(parent) - ,m_id(id) +LegendScrollButton::LegendScrollButton(ScrollButtonId id, QLegend *legend) + : QGraphicsPolygonItem(legend), + m_id(id), + m_ledgend(legend) { setAcceptedMouseButtons(Qt::LeftButton); } @@ -17,7 +19,8 @@ LegendScrollButton::ScrollButtonId LegendScrollButton::id() void LegendScrollButton::mousePressEvent(QGraphicsSceneMouseEvent *event) { - emit clicked(event); + Q_UNUSED(event); + m_ledgend->scrollButtonClicked(this); } #include "moc_legendscrollbutton_p.cpp" diff --git a/src/legendscrollbutton_p.h b/src/legendscrollbutton_p.h index 2ebd3dd..8a89b5b 100644 --- a/src/legendscrollbutton_p.h +++ b/src/legendscrollbutton_p.h @@ -7,9 +7,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class LegendScrollButton : public QObject, public QGraphicsPolygonItem +class QLegend; + +class LegendScrollButton : public QGraphicsPolygonItem { - Q_OBJECT public: enum ScrollButtonId { ScrollButtonIdLeft, @@ -18,19 +19,14 @@ public: ScrollButtonIdDown }; - explicit LegendScrollButton(ScrollButtonId id, QGraphicsItem *parent = 0); + explicit LegendScrollButton(ScrollButtonId id, QLegend *legend); ScrollButtonId id(); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); -Q_SIGNALS: - void clicked(QGraphicsSceneMouseEvent* event); - -public Q_SLOTS: - private: - ScrollButtonId m_id; + QLegend *m_ledgend; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchart.h b/src/qchart.h index ea6477f..464a25c 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -105,6 +105,7 @@ protected: protected: QScopedPointer d_ptr; friend class QChartView; + friend class QLegend; Q_DISABLE_COPY(QChart) }; diff --git a/src/qlegend.cpp b/src/qlegend.cpp index dcdbe1c..4ab3620 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -1,5 +1,5 @@ -#include "qchartglobal.h" #include "qlegend.h" +#include "qchart_p.h" #include "qseries.h" #include "legendmarker_p.h" #include "legendscrollbutton_p.h" @@ -66,7 +66,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! Constructs the legend object and sets the parent to \a parent */ -QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent), +QLegend::QLegend(QChart *chart):QGraphicsWidget(chart->d_ptr->m_presenter->rootItem()), m_margin(5), m_pos(0,0), m_minimumSize(50,20), // TODO: magic numbers @@ -80,16 +80,6 @@ QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent), m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this); m_scrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this); m_scrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this); - - connect(m_scrollButtonLeft, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), - this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); - connect(m_scrollButtonRight, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), - this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); - connect(m_scrollButtonUp, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), - this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); - connect(m_scrollButtonDown, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), - this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); - setZValue(ChartPresenter::LegendZValue); } @@ -297,12 +287,9 @@ void QLegend::handleMarkerDestroyed() /*! \internal \a event Handles clicked signals from scroll buttons */ -void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event) +void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton) { - Q_UNUSED(event); // Maybe later something happens with right click... - - LegendScrollButton* scrollButton = static_cast (sender()); - Q_ASSERT(scrollButton); + Q_ASSERT(scrollButton); switch (scrollButton->id()) { case LegendScrollButton::ScrollButtonIdLeft: diff --git a/src/qlegend.h b/src/qlegend.h index bf27ce7..1610d7e 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -1,8 +1,8 @@ #ifndef QLEGEND_H #define QLEGEND_H -#include -#include +#include +#include #include #include @@ -17,9 +17,10 @@ class QBarSeries; class QPieSeries; class LegendScrollButton; class QSeries; +class QChart; // TODO: This as widget -class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject +class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget { Q_OBJECT public: @@ -31,9 +32,10 @@ public: LayoutLeft = Qt::AlignLeft, LayoutRight = Qt::AlignRight }; +private: + explicit QLegend(QChart *chart); - explicit QLegend(QGraphicsItem *parent = 0); - +public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); QRectF boundingRect() const; @@ -53,6 +55,8 @@ public: void setSize(const QSizeF size); void setPos(const QPointF &pos); + void scrollButtonClicked(LegendScrollButton *scrollButton); + Q_SIGNALS: // for interactions. void clicked(QSeries *series, Qt::MouseButton button); @@ -66,7 +70,7 @@ public Q_SLOTS: void handleAdded(QList slices); void handleRemoved(QList slices); void handleMarkerDestroyed(); - void handleScrollButtonClicked(QGraphicsSceneMouseEvent *event); + // PIMPL <--- private: @@ -103,6 +107,7 @@ private: LegendScrollButton *m_scrollButtonUp; LegendScrollButton *m_scrollButtonDown; + friend class QChart; // <--- PIMPL };