From 280ce33a2f47a6915c69c2a9eb45a2c0a445e4cd 2012-03-09 13:09:03 From: sauimone Date: 2012-03-09 13:09:03 Subject: [PATCH] framework for legend --- diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index 0bb3e5b..d4913ad 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -39,12 +39,12 @@ QList BarChartModel::barSets() return mDataModel; } -QList BarChartModel::legend() +QList BarChartModel::legendEntries() { - QList legend; + QList legend; for (int i=0; iname(); l.mPen = mDataModel.at(i)->pen(); legend.append(l); diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index d926bfa..be5764f 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -25,7 +25,7 @@ public: QBarSet *setAt(int index); QList barSets(); - QList legend(); + QList legendEntries(); int barsetCount(); // Number of sets in model int categoryCount(); // Number of categories diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index eaa0572..f95410c 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -98,9 +98,9 @@ QBarSet* QBarSeries::barsetAt(int index) /*! Returns legend of series. */ -QList QBarSeries::legend() +QList QBarSeries::legendEntries() { - return mModel->legend(); + return mModel->legendEntries(); } /*! diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index 61b6575..938f4ae 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -24,7 +24,7 @@ public: int barsetCount(); int categoryCount(); QList barSets(); - QList legend(); + QList legendEntries(); public: // TODO: Functions below this are not part of api and will be moved diff --git a/src/qchart.cpp b/src/qchart.cpp index 3633ace..ad9d3ea 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -1,5 +1,6 @@ #include "qchart.h" #include "qchartaxis.h" +#include "qlegend.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" #include @@ -49,8 +50,11 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( m_backgroundItem(0), m_titleItem(0), m_dataset(new ChartDataSet(this)), - m_presenter(new ChartPresenter(this,m_dataset)) + m_presenter(new ChartPresenter(this,m_dataset)), + m_legend(new QLegend(this)) // TODO: is this the parent we want the legend to have? { + connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); + connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); } /*! @@ -58,6 +62,8 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( */ QChart::~QChart() { + disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); + disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); } /*! @@ -262,6 +268,15 @@ QChartAxis* QChart::axisY() const } /*! + Returns the legend object of the chart +*/ +QLegend* QChart::legend() const +{ + return m_legend; +} + + +/*! Resizes and updates the chart area using the \a event data */ void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) diff --git a/src/qchart.h b/src/qchart.h index 843c9c2..b63fc16 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -20,6 +20,7 @@ class ChartTheme; class ChartItem; class ChartDataSet; class ChartPresenter; +class QLegend; class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget { @@ -75,6 +76,8 @@ public: QChartAxis* axisX() const; QChartAxis* axisY() const; + QLegend* legend() const; + protected: void resizeEvent(QGraphicsSceneResizeEvent *event); @@ -89,6 +92,7 @@ private: QRectF m_rect; ChartDataSet *m_dataset; ChartPresenter *m_presenter; + QLegend* m_legend; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qlegend.cpp b/src/qlegend.cpp new file mode 100644 index 0000000..08338bf --- /dev/null +++ b/src/qlegend.cpp @@ -0,0 +1,44 @@ +#include "qchartglobal.h" +#include "qlegend.h" +#include "qseries.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +QLegend::QLegend(QGraphicsItem *parent) + : QGraphicsObject(parent) + ,mBoundingRect(0,0,0,0) +{ +} + + +void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + // TODO: + qDebug() << "QLegend::paint"; + foreach(QSeries* s, mSeriesList) { + for (int i=0; ilegendEntries().count(); i++) { + // Paint it... + //qDebug() << s->legendEntries().at(i).mName; + } + } +} + +QRectF QLegend::boundingRect() const +{ + return mBoundingRect; +} + +void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) +{ + // TODO: append entries + mSeriesList.append(series); +} + +void QLegend::handleSeriesRemoved(QSeries* series) +{ + // TODO: remove entries + mSeriesList.removeOne(series); +} + + +#include "moc_qlegend.cpp" +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qlegend.h b/src/qlegend.h new file mode 100644 index 0000000..8b90557 --- /dev/null +++ b/src/qlegend.h @@ -0,0 +1,36 @@ +#ifndef QLEGEND_H +#define QLEGEND_H + +#include "qchartglobal.h" +#include "qseries.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class Domain; + +class QLegend : public QGraphicsObject +{ + Q_OBJECT +public: + + explicit QLegend(QGraphicsItem *parent = 0); + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + QRectF boundingRect() const; + +signals: + +public slots: + void handleSeriesAdded(QSeries* series,Domain* domain); + void handleSeriesRemoved(QSeries* series); + +private: + + QRectF mBoundingRect; + QList mSeriesList; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QLEGEND_H diff --git a/src/qseries.h b/src/qseries.h index a8f5e6a..1cb9ece 100644 --- a/src/qseries.h +++ b/src/qseries.h @@ -25,8 +25,8 @@ public: // Helper class to contain legend and color for it // TODO: This as private class? Or should we expose this to user of API - class Legend { - public: + class LegendEntry { + public: QString mName; QPen mPen; }; @@ -40,8 +40,7 @@ public: // TODO virtual bool setModel(QAbstractItemModel* /*model*/) { return false; } - // TODO: should this be internal? - virtual QList legend() { QList l; return l; } + virtual QList legendEntries() { QList l; return l; } void setTitle(QString title) { m_title = title; } QString title() { return m_title; } diff --git a/src/src.pro b/src/src.pro index 747415b..413d92a 100644 --- a/src/src.pro +++ b/src/src.pro @@ -13,7 +13,8 @@ SOURCES += \ domain.cpp \ qchart.cpp \ qchartview.cpp \ - qseries.cpp + qseries.cpp \ + qlegend.cpp PRIVATE_HEADERS += \ chartdataset_p.h \ chartitem_p.h \ @@ -24,8 +25,9 @@ PUBLIC_HEADERS += \ qchart.h \ qchartglobal.h \ qseries.h \ - qchartview.h - + qchartview.h \ + qlegend.h + include(axis/axis.pri) include(xychart/xychart.pri) include(linechart/linechart.pri)