diff --git a/examples/stackedbarchartdrilldown/main.cpp b/examples/stackedbarchartdrilldown/main.cpp index 0bb409d..fb7c185 100644 --- a/examples/stackedbarchartdrilldown/main.cpp +++ b/examples/stackedbarchartdrilldown/main.cpp @@ -138,13 +138,10 @@ int main(int argc, char *argv[]) drilldownChart->setChartTitle(seasonSeries->title()); //! [6] - // Disable axis, since they don't really apply to bar chart -// drilldownChart->axisX()->setAxisVisible(false); drilldownChart->axisX()->setGridLineVisible(false); -// drilldownChart->axisX()->setLabelsVisible(false); QLegend* l = drilldownChart->legend(); - l->handleGeometryChanged(QRectF(20,20,100,100)); + l->handleGeometryChanged(QRectF(50,270,300,20)); window.setCentralWidget(drilldownChart); window.resize(400, 300); diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index dfd5c26..6b3195a 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -277,6 +277,7 @@ void ChartPresenter::setChartTheme(QChart::ChartTheme theme) delete m_chartTheme; m_chartTheme = ChartTheme::createTheme(theme); m_chartTheme->decorate(m_chart); + m_chartTheme->decorate(m_chart->legend()); resetAllElements(); } diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 0b274b3..0c426f7 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -27,7 +27,8 @@ public: AxisZValue, LineChartZValue, ScatterSeriesZValue, - PieSeriesZValue + PieSeriesZValue, + LegendZValue }; ChartPresenter(QChart* chart,ChartDataSet *dataset); diff --git a/src/charttheme.cpp b/src/charttheme.cpp index e8bd1b2..5844100 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -1,5 +1,6 @@ #include "charttheme_p.h" #include "qchart.h" +#include "qlegend.h" #include "qchartaxis.h" #include @@ -64,6 +65,11 @@ void ChartTheme::decorate(QChart* chart) chart->setChartBackgroundBrush(m_backgroundGradient); } +void ChartTheme::decorate(QLegend* legend) +{ + legend->setBackgroundBrush(m_backgroundGradient); +} + void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index) { QPen pen; diff --git a/src/charttheme_p.h b/src/charttheme_p.h index 91235a8..53d28c5 100644 --- a/src/charttheme_p.h +++ b/src/charttheme_p.h @@ -35,6 +35,7 @@ public: static ChartTheme* createTheme(QChart::ChartTheme theme); QChart::ChartTheme id() const {return m_id;} void decorate(QChart* chart); + void decorate(QLegend* legend); //void decorate(ChartItem* item, QSeries* series,int index); void decorate(BarPresenter* item, QBarSeries* series, int index); void decorate(StackedBarPresenter* item, QStackedBarSeries* series, int index); diff --git a/src/qchart.cpp b/src/qchart.cpp index bdaab78..2aa5a60 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -50,9 +50,9 @@ 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_legend(new QLegend(this)) // TODO: is this the parent we want the legend to have? + m_presenter(new ChartPresenter(this,m_dataset)) { + createLegend(); connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); } @@ -181,6 +181,16 @@ void QChart::createChartTitleItem() } } +void QChart::createLegend() +{ + // TODO: Why we have null pointer, even if this is created in constructor? + if(!m_legend) { + m_legend = new QLegend(this); + m_legend->setZValue(ChartPresenter::LegendZValue); + } +} + + /*! Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. \sa setMargin() @@ -270,12 +280,12 @@ QChartAxis* QChart::axisY() const /*! Returns the legend object of the chart */ -QLegend* QChart::legend() const +QLegend* QChart::legend() { + createLegend(); return m_legend; } - /*! Resizes and updates the chart area using the \a event data */ diff --git a/src/qchart.h b/src/qchart.h index fdd1953..6d4fa78 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -77,7 +77,7 @@ public: QChartAxis* axisX() const; QChartAxis* axisY() const; - QLegend* legend() const; + QLegend* legend(); protected: void resizeEvent(QGraphicsSceneResizeEvent *event); @@ -85,6 +85,7 @@ protected: private: inline void createChartBackgroundItem(); inline void createChartTitleItem(); + inline void createLegend(); private: Q_DISABLE_COPY(QChart) diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 525e9a3..853196c 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -364,7 +364,6 @@ QLegend* QChartView::legend() const return m_chart->legend(); } - /*! Sets animation \a options for the chart */ diff --git a/src/qlegend.cpp b/src/qlegend.cpp index 251a71c..3194c83 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -22,7 +22,6 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) { - qDebug() << "LegendMarker::paint" << mBoundingRect; painter->setBrush(mBrush); painter->drawRect(mBoundingRect); } @@ -38,17 +37,19 @@ private: QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent) ,mBoundingRect(0,0,1,1) + ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? { } void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - // TODO: layout for text. using marker layout + magic for now. + painter->setBrush(mBackgroundBrush); + painter->drawRect(mBoundingRect); + foreach(LegendMarker* m, mMarkers) { QRectF r = m->boundingRect(); painter->setPen(m->color()); - // TODO: r.y + r.height is incorrect. should be r.y. Find the bug, and remove the hack - painter->drawText(r.x() + 20, r.y()+r.height(), m->name()); + painter->drawText(r.x() + 20, r.y() + r.height(), m->name()); } } @@ -57,6 +58,16 @@ QRectF QLegend::boundingRect() const return mBoundingRect; } +void QLegend::setBackgroundBrush(const QBrush& brush) +{ + mBackgroundBrush = brush; +} + +QBrush QLegend::backgroundBrush() const +{ + return mBackgroundBrush; +} + void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) { mSeriesList.append(series); @@ -91,12 +102,11 @@ void QLegend::dataChanged() marker->setBrush(s->legendEntries().at(i).mBrush); marker->setName(s->legendEntries().at(i).mName); mMarkers.append(marker); - childItems().append(marker); +// childItems().append(marker); } } } - void QLegend::layoutChanged() { // Calculate layout for markers and text @@ -110,14 +120,17 @@ void QLegend::layoutChanged() // TODO: better layout, this is just concept. // Leave some space around markers like this: | x x x x | - qreal steps = mMarkers.count() * 2 + 1; + qreal steps = mMarkers.count(); + qreal xStep = mBoundingRect.width() / steps; qreal yStep = mBoundingRect.height() / steps; - qreal x = 0; - qreal y = yStep; // first step is empty + qreal x = mBoundingRect.x() + 5; + qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2; foreach (LegendMarker* m, mMarkers) { + qDebug() << "marker x:" << x; + qDebug() << "marker y:" << y; m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height())); - y += yStep*2; // 2 steps per marker (marker and empty space) + x += xStep; } } diff --git a/src/qlegend.h b/src/qlegend.h index c5a6a31..0612833 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -20,6 +20,9 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); QRectF boundingRect() const; + void setBackgroundBrush(const QBrush& brush); + QBrush backgroundBrush() const; + signals: public slots: @@ -34,6 +37,8 @@ private: QRectF mBoundingRect; QList mSeriesList; QList mMarkers; + + QBrush mBackgroundBrush; }; QTCOMMERCIALCHART_END_NAMESPACE