From 39840e10ebaad56182364b18071158e8aeb432fb 2012-10-17 07:46:16 From: sauimone Date: 2012-10-17 07:46:16 Subject: [PATCH] refactoring --- diff --git a/src/legend/legendmarkeritem.cpp b/src/legend/legendmarkeritem.cpp index 14f2f6d..1c3d0f2 100644 --- a/src/legend/legendmarkeritem.cpp +++ b/src/legend/legendmarkeritem.cpp @@ -19,26 +19,19 @@ ****************************************************************************/ #include "legendmarkeritem_p.h" -#include "qxyseries.h" -#include "qxyseries_p.h" -#include "qlegend.h" -#include "qabstractbarseries.h" -#include "qpieseries.h" -#include "qpieslice.h" -#include "qbarset.h" -#include "qbarset_p.h" -#include "qareaseries.h" -#include "qareaseries_p.h" #include #include #include #include +#include "qlegendmarker_p.h" + QTCOMMERCIALCHART_BEGIN_NAMESPACE -LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) : +//LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) : +LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) : QGraphicsObject(parent), - m_series(series), + m_marker(marker), m_markerRect(0,0,10.0,10.0), m_boundingRect(0,0,0,0), m_textItem(new QGraphicsSimpleTextItem(this)), @@ -46,7 +39,8 @@ LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *par m_margin(4), m_space(4) { - //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); + qDebug() << "LegendMarkerItem created for marker:" << m_marker; + setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); m_rectItem->setRect(m_markerRect); } @@ -170,82 +164,10 @@ QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug() << "LegendMarkerItem::mousePressEvent"; - QGraphicsObject::mousePressEvent(event); +// QGraphicsObject::mousePressEvent(event); //TODO: selected signal removed for now -} - -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -AreaLegendMarkerItem::AreaLegendMarkerItem(QAreaSeries *series,QLegend *legend) : LegendMarkerItem(series,legend), -m_series(series) -{ - //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); -// QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); -// QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); - updated(); -} - -void AreaLegendMarkerItem::updated() -{ - setBrush(m_series->brush()); - setLabel(m_series->name()); -} - -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -BarLegendMarkerItem::BarLegendMarkerItem(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarkerItem(barseries,legend), -m_barset(barset) -{ - //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); -// QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); - updated(); -} - -void BarLegendMarkerItem::updated() -{ - setBrush(m_barset->brush()); - setLabel(m_barset->label()); -} - -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -PieLegendMarkerItem::PieLegendMarkerItem(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarkerItem(series,legend), -m_pieslice(pieslice) -{ -// QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); -// QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); - updated(); -} - -void PieLegendMarkerItem::updated() -{ - setBrush(m_pieslice->brush()); - setLabel(m_pieslice->label()); -} - -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -XYLegendMarkerItem::XYLegendMarkerItem(QXYSeries *series, QLegend *legend) : LegendMarkerItem(series,legend), -m_series(series) -{ - //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); -// QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); -// QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); - updated(); -} - -void XYLegendMarkerItem::updated() -{ - setLabel(m_series->name()); - - if(m_series->type()== QAbstractSeries::SeriesTypeScatter) - { - setBrush(m_series->brush()); - - } - else { - setBrush(QBrush(m_series->pen().color())); - } + emit m_marker->handleMousePressEvent(event); + QGraphicsItem::mousePressEvent(event); } #include "moc_legendmarkeritem_p.cpp" diff --git a/src/legend/legendmarkeritem_p.h b/src/legend/legendmarkeritem_p.h index 3c5db80..8c18a10 100644 --- a/src/legend/legendmarkeritem_p.h +++ b/src/legend/legendmarkeritem_p.h @@ -39,21 +39,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QAbstractSeries; -class QAreaSeries; -class QXYSeries; -class QBarSet; -class QAbstractBarSeries; -class QPieSlice; -class QLegend; -class QPieSeries; +class QLegendMarkerPrivate; class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem { Q_OBJECT Q_INTERFACES(QGraphicsLayoutItem) public: - explicit LegendMarkerItem(QAbstractSeries *m_series, QGraphicsObject *parent = 0); +// explicit LegendMarkerItem(QAbstractSeries *m_series, QGraphicsObject *parent = 0); + explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0); void setPen(const QPen &pen); QPen pen() const; @@ -70,8 +64,6 @@ public: void setLabelBrush(const QBrush &brush); QBrush labelBrush() const; - QAbstractSeries *series() const { return m_series;} - void setGeometry(const QRectF& rect); QRectF boundingRect() const; @@ -84,14 +76,10 @@ protected: // From QGraphicsObject void mousePressEvent(QGraphicsSceneMouseEvent *event); -//public Q_SLOTS: - //virtual void updated() = 0; - protected: - QAbstractSeries *m_series; + QLegendMarkerPrivate *m_marker; QRectF m_markerRect; QRectF m_boundingRect; -// QLegend* m_legend; QGraphicsSimpleTextItem *m_textItem; QGraphicsRectItem *m_rectItem; qreal m_margin; @@ -100,47 +88,6 @@ protected: }; -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -class XYLegendMarkerItem : public LegendMarkerItem -{ -public: - XYLegendMarkerItem(QXYSeries *series, QLegend *legend); -protected: - void updated(); -private: - QXYSeries *m_series; -}; -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -class AreaLegendMarkerItem : public LegendMarkerItem -{ -public: - AreaLegendMarkerItem(QAreaSeries *series, QLegend *legend); -protected: - void updated(); -private: - QAreaSeries *m_series; -}; -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -class BarLegendMarkerItem : public LegendMarkerItem -{ -public: - BarLegendMarkerItem(QAbstractBarSeries *barseries, QBarSet *barset,QLegend *legend); -protected: - void updated(); -private: - QBarSet *m_barset; -}; -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -class PieLegendMarkerItem : public LegendMarkerItem -{ -public: - PieLegendMarkerItem(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend); -protected: - void updated(); -private: - QPieSlice *m_pieslice; -}; - QTCOMMERCIALCHART_END_NAMESPACE #endif // LEGENDMARKERITEM_P_H diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 9588004..c674926 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -24,7 +24,7 @@ #include "qabstractseries_p.h" #include "qchart_p.h" #include "legendlayout_p.h" -#include "legendmarker_p.h" +#include "legendmarker_p.h" // TODO: deprecated #include "qxyseries.h" #include "qlineseries.h" #include "qareaseries.h" @@ -45,6 +45,8 @@ #include #include +#include "qlegendmarker_p.h" +#include "legendmarkeritem_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -401,6 +403,15 @@ QList QLegend::markers() const return d_ptr->legendMarkers(); } +void QLegend::appendSeries(QAbstractSeries* series) +{ + d_ptr->appendSeries(series); +} + +void QLegend::removeSeries(QAbstractSeries* series) +{ + d_ptr->removeSeries(series); +} /*! \internal \a event see QGraphicsWidget for details @@ -463,6 +474,30 @@ int QLegendPrivate::roundness(qreal size) return 100 * m_diameter / int(size); } +void QLegendPrivate::appendSeries(QAbstractSeries* series) +{ + Q_UNUSED(series); + // TODO: + /* + if (!m_series.contains(series)) { + m_series.append(series); + handleSeriesAdded(series,0); // Dummy domain + } + */ +} + +void QLegendPrivate::removeSeries(QAbstractSeries* series) +{ + Q_UNUSED(series); + // TODO: + /* + if (m_series.contains(series)) { + m_series.removeOne(series); + handleSeriesRemoved(series); + } + */ +} + void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) { Q_UNUSED(domain) @@ -475,9 +510,9 @@ void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) marker->setFont(m_font); marker->setLabelBrush(m_labelBrush); marker->setVisible(series->isVisible()); -// TODO: QLegendMarker has QGraphicsItem vs QLegendMarker is QGraphicsItem // TODO: possible hazard. What if marker is deleted and group still has pointer? -// m_items->addToGroup(marker->d_ptr.data()); + m_items->addToGroup(marker->d_ptr.data()->item()); +// qDebug() << "item:" << marker->d_ptr.data()->item(); m_legendMarkers << marker; } diff --git a/src/legend/qlegend.h b/src/legend/qlegend.h index 93aa4b9..1a824c7 100644 --- a/src/legend/qlegend.h +++ b/src/legend/qlegend.h @@ -28,15 +28,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QPieSlice; -class QXYSeries; -class QBarSet; -class QAbstractBarSeries; -class QPieSeries; -class QAreaSeries; class QChart; class QLegendPrivate; class QLegendMarker; +class QAbstractSeries; class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget { @@ -86,6 +81,8 @@ public: // New stuff: QList markers() const; + void appendSeries(QAbstractSeries* series); + void removeSeries(QAbstractSeries* series); protected: void hideEvent(QHideEvent *event); diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index 8d76008..bb2d4e1 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -58,6 +58,8 @@ public: // New stuff: QList legendMarkers() { return m_legendMarkers; } // TODO: function name will change + void appendSeries(QAbstractSeries* series); + void removeSeries(QAbstractSeries* series); public Q_SLOTS: void handleSeriesAdded(QAbstractSeries *series, Domain *domain); @@ -85,6 +87,7 @@ private: friend class QLegend; friend class LegendLayout; QList m_legendMarkers; // TODO: rename to m_markers eventually. + QList m_series; }; diff --git a/src/legend/qlegendmarker.cpp b/src/legend/qlegendmarker.cpp index ccc0336..3ba48b1 100644 --- a/src/legend/qlegendmarker.cpp +++ b/src/legend/qlegendmarker.cpp @@ -25,12 +25,18 @@ #include QTCOMMERCIALCHART_BEGIN_NAMESPACE - +/* QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) : QObject(parent), d_ptr(new QLegendMarkerPrivate(series, this)) { } +*/ +QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) : + QObject(parent), + d_ptr(&d) +{ +} QLegendMarker::~QLegendMarker() { @@ -96,24 +102,34 @@ void QLegendMarker::setVisible(bool visible) d_ptr->m_visible = visible; } -QAbstractSeries* QLegendMarker::series() -{ - return d_ptr->m_series; -} - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - +/* QLegendMarkerPrivate::QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q) : q_ptr(q), m_series(series) { m_item = new LegendMarkerItem(m_series); } +*/ +QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q) : + q_ptr(q) +{ + qDebug() << "QLegendMarkerPrivate created"; + m_item = new LegendMarkerItem(this); +} QLegendMarkerPrivate::~QLegendMarkerPrivate() { } +void QLegendMarkerPrivate::handleMousePressEvent(QGraphicsSceneEvent *event) +{ + // Just emit clicked signal for now + Q_UNUSED(event); + Q_Q(QLegendMarker); + emit q->clicked(); +} + #include "moc_qlegendmarker.cpp" #include "moc_qlegendmarker_p.cpp" diff --git a/src/legend/qlegendmarker.h b/src/legend/qlegendmarker.h index b743d0b..3e405c6 100644 --- a/src/legend/qlegendmarker.h +++ b/src/legend/qlegendmarker.h @@ -43,7 +43,7 @@ class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged); public: - explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0); +// explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0); virtual ~QLegendMarker(); QString label() const; @@ -64,10 +64,13 @@ public: bool isVisible() const; void setVisible(bool visible); -// virtual QAbstractSeries::SeriesType type() = 0; - virtual QAbstractSeries* series(); +// virtual QAbstractSeries::SeriesType type() = 0; // TODO? + virtual QAbstractSeries* series() = 0; virtual QObject* peerObject() = 0; +protected: + explicit QLegendMarker(QLegendMarkerPrivate &d, QObject *parent = 0); + Q_SIGNALS: void clicked(); void hovered(bool status); @@ -75,10 +78,11 @@ Q_SIGNALS: public Q_SLOTS: virtual void updated() = 0; // TODO: private. Idea is that series signals, when some property has changed -public: +protected: QScopedPointer d_ptr; Q_DISABLE_COPY(QLegendMarker) - + friend class QLegendPrivate; + friend class QLegendMarkerPrivate; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qlegendmarker_p.h b/src/legend/qlegendmarker_p.h index 23f1528..51dde66 100644 --- a/src/legend/qlegendmarker_p.h +++ b/src/legend/qlegendmarker_p.h @@ -36,6 +36,7 @@ #include #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -56,7 +57,8 @@ class QLegendMarkerPrivate : public QObject { Q_OBJECT public: - explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q); +// explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q); + explicit QLegendMarkerPrivate(QLegendMarker *q); virtual ~QLegendMarkerPrivate(); /* void setPen(const QPen &pen); @@ -74,21 +76,25 @@ public: void setLabelBrush(const QBrush &brush); QBrush labelBrush() const; */ + // Helper for now. + LegendMarkerItem* item() const { return m_item; } + + // Item gets the event, logic for event is here + void handleMousePressEvent(QGraphicsSceneEvent *event); public Q_SLOTS: virtual void updated() {}; -private: - QLegendMarker *q_ptr; - +protected: LegendMarkerItem *m_item; +private: + QLegendMarker *q_ptr; /* QLegend* m_legend; */ // New legend marker properties - QAbstractSeries* m_series; QString m_label; QBrush m_labelBrush; QFont m_font; @@ -96,14 +102,6 @@ private: QBrush m_brush; bool m_visible; -// Implementation details of new marker - QRectF m_markerRect; - QRectF m_boundingRect; - QGraphicsSimpleTextItem *m_textItem; - QGraphicsRectItem *m_rectItem; - qreal m_margin; - qreal m_space; - friend class QLegendPrivate; // TODO: Is this needed? Q_DECLARE_PUBLIC(QLegendMarker) }; diff --git a/src/legend/qpielegendmarker.cpp b/src/legend/qpielegendmarker.cpp index cb7d144..64f65a5 100644 --- a/src/legend/qpielegendmarker.cpp +++ b/src/legend/qpielegendmarker.cpp @@ -21,41 +21,70 @@ #include "qpielegendmarker.h" #include "qpielegendmarker_p.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE - +/* QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) : QLegendMarker(series, parent), - m_slice(slice) + d_ptr(new QPieLegendMarkerPrivate(series,slice,this)) { QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated())); QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated())); updated(); } -QPieSlice* QPieLegendMarker::peerObject() +*/ +QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) : + QLegendMarker(*new QPieLegendMarkerPrivate(series,slice,this), parent) +{ + QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated())); + QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated())); +// updated(); +} + +/*! + \internal +*/ +QPieLegendMarker::QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent) : + QLegendMarker(d, parent) { - return m_slice; } -void QPieLegendMarker::updated() +QAbstractSeries* QPieLegendMarker::series() { - // TODO: to PIMPL. - setBrush(m_slice->brush()); - setLabel(m_slice->label()); + Q_D(QPieLegendMarker); + return d->m_series; +} + +QPieSlice* QPieLegendMarker::peerObject() +{ + Q_D(QPieLegendMarker); + return d->m_slice; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q) : - QLegendMarkerPrivate(series, q) +//QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q) : +QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QPieSeries *series, QPieSlice *slice, QPieLegendMarker *q) : + QLegendMarkerPrivate(q), + m_series(series), + m_slice(slice) { + qDebug() << "QPieLegendMarkerPrivate created"; + updated(); } QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate() { } +void QPieLegendMarkerPrivate::updated() +{ + m_item->setBrush(m_slice->brush()); + m_item->setLabel(m_slice->label()); +} + #include "moc_qpielegendmarker.cpp" #include "moc_qpielegendmarker_p.cpp" diff --git a/src/legend/qpielegendmarker.h b/src/legend/qpielegendmarker.h index a97c312..84427f8 100644 --- a/src/legend/qpielegendmarker.h +++ b/src/legend/qpielegendmarker.h @@ -35,22 +35,20 @@ class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker public: explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0); + virtual QAbstractSeries* series(); virtual QPieSlice* peerObject(); -// TODO: to pimpl. - void updated(); +protected: + QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent = 0); //Q_SIGNALS: //public Q_SLOTS: private: - QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(QPieLegendMarker) Q_DISABLE_COPY(QPieLegendMarker) -// TODO: PIMPL - QPieSlice* m_slice; - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qpielegendmarker_p.h b/src/legend/qpielegendmarker_p.h index 72cfb2d..8226f99 100644 --- a/src/legend/qpielegendmarker_p.h +++ b/src/legend/qpielegendmarker_p.h @@ -33,6 +33,10 @@ #include "qchartglobal.h" #include "qlegendmarker_p.h" #include "legendmarkeritem_p.h" +#include +#include + +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -42,15 +46,18 @@ class QPieLegendMarkerPrivate : public QLegendMarkerPrivate { Q_OBJECT public: - explicit QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q); +// explicit QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q); + explicit QPieLegendMarkerPrivate(QPieSeries *series, QPieSlice *slice, QPieLegendMarker *q); virtual ~QPieLegendMarkerPrivate(); public Q_SLOTS: - virtual void updated() {}; + virtual void updated(); private: QPieLegendMarker *q_ptr; - PieLegendMarkerItem *m_item; + + QPieSeries* m_series; + QPieSlice* m_slice; friend class QLegendPrivate; // TODO: Is this needed? Q_DECLARE_PUBLIC(QPieLegendMarker) diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 9a03a47..3d1ae1a 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -865,11 +865,11 @@ QList QPieSeriesPrivate::createLegendMarker(QLegend *legend) QList QPieSeriesPrivate::createLegendMarkers(QLegend* legend) { - Q_UNUSED(legend); +// Q_UNUSED(legend); Q_Q(QPieSeries); QList markers; foreach(QPieSlice* slice, q->slices()) { - QPieLegendMarker* marker = new QPieLegendMarker(q,slice); + QPieLegendMarker* marker = new QPieLegendMarker(q,slice,legend); markers << marker; } return markers;