From 14e346f15b06d443d13ef2326c0c35301c204e3d 2012-06-25 13:13:52 From: sauimone Date: 2012-06-25 13:13:52 Subject: [PATCH] legend theme fix --- diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 4cd7d9e..f3ee4c1 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -117,12 +117,20 @@ void ChartTheme::decorate(QLegend *legend) { QPen pen; QBrush brush; + QFont font; if (pen == legend->pen() || m_force) legend->setPen(m_axisLinePen); if (brush == legend->brush() || m_force) legend->setBrush(m_chartBackgroundGradient); + + // TODO: should legend have own brush & font defined by theme? + if (font == legend->font() || m_force) + legend->setFont(m_labelFont); + + if (brush == legend->labelBrush() || m_force) + legend->setLabelBrush(m_axisLabelBrush); } void ChartTheme::decorate(QAreaSeries *series, int index) diff --git a/src/legend/legendmarker.cpp b/src/legend/legendmarker.cpp index 9962a06..b88c366 100644 --- a/src/legend/legendmarker.cpp +++ b/src/legend/legendmarker.cpp @@ -98,6 +98,28 @@ QString LegendMarker::label() const return m_textItem->text(); } +void LegendMarker::setLabelBrush(const QBrush &brush) +{ + m_textItem->setBrush(brush); + updateLayout(); +} + +QBrush LegendMarker::labelBrush() const +{ + return m_textItem->brush(); +} + +void LegendMarker::setLabelPen(const QPen &pen) +{ + m_textItem->setPen(pen); + updateLayout(); +} + +QPen LegendMarker::labelPen() const +{ + return m_textItem->pen(); +} + void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) diff --git a/src/legend/legendmarker_p.h b/src/legend/legendmarker_p.h index 611d5cb..c105e7e 100644 --- a/src/legend/legendmarker_p.h +++ b/src/legend/legendmarker_p.h @@ -66,6 +66,10 @@ public: void setLabel(const QString label); QString label() const; + void setLabelBrush(const QBrush &brush); + QBrush labelBrush() const; + void setLabelPen(const QPen &pen); + QPen labelPen() const; QAbstractSeries *series() const { return m_series;} diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index ec77509..04fe655 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -286,6 +286,50 @@ QFont QLegend::font() const return d_ptr->m_font; } +void QLegend::setLabelPen(const QPen &pen) +{ + if (d_ptr->m_labelPen != pen) { + d_ptr->setLabelPen(pen); + emit labelPenChanged(pen); + } +} + +QPen QLegend::labelPen() const +{ + return d_ptr->m_labelPen; +} + +void QLegend::setLabelBrush(const QBrush &brush) +{ + qDebug() << "setting legend brush 1"; + if (d_ptr->m_labelBrush != brush) { + d_ptr->setLabelBrush(brush); + emit labelBrushChanged(brush); + qDebug() << "setting legend brush 2"; + } +} + +QBrush QLegend::labelBrush() const +{ + return d_ptr->m_labelBrush; +} + +void QLegend::setLabelColor(QColor color) +{ + QBrush b = d_ptr->m_labelBrush; + if (b.style() != Qt::SolidPattern || b.color() != color) { + b.setStyle(Qt::SolidPattern); + b.setColor(color); + setLabelBrush(b); + emit labelColorChanged(color); + } +} + +QColor QLegend::labelColor() const +{ + return d_ptr->m_labelBrush.color(); +} + void QLegend::setAlignment(Qt::Alignment alignment) { if(d_ptr->m_alignment!=alignment) { @@ -396,6 +440,8 @@ QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend m_alignment(Qt::AlignTop), m_brush(QBrush()), m_pen(QPen()), + m_labelPen(QPen(Qt::NoPen)), + m_labelBrush(QBrush()), m_offsetX(0), m_offsetY(0), m_minWidth(0), @@ -719,6 +765,30 @@ void QLegendPrivate::setFont(const QFont &font) updateLayout(); } +void QLegendPrivate::setLabelPen(const QPen &pen) +{ + m_labelPen = pen; + QList items = m_markers->childItems(); + + foreach (QGraphicsItem *markers, items) { + LegendMarker *marker = static_cast(markers); + marker->setPen(m_labelPen); + } + updateLayout(); +} + +void QLegendPrivate::setLabelBrush(const QBrush &brush) +{ + m_labelBrush = brush; + QList items = m_markers->childItems(); + + foreach (QGraphicsItem *markers, items) { + LegendMarker *marker = static_cast(markers); + marker->setLabelBrush(m_labelBrush); + } + updateLayout(); +} + void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) { Q_UNUSED(domain) @@ -726,6 +796,8 @@ void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) QList markers = series->d_ptr->createLegendMarker(q_ptr); foreach(LegendMarker* marker, markers) { marker->setFont(m_font); + marker->setLabelPen(m_labelPen); + marker->setLabelBrush(m_labelBrush); m_markers->addToGroup(marker); } diff --git a/src/legend/qlegend.h b/src/legend/qlegend.h index 313469c..d4444b8 100644 --- a/src/legend/qlegend.h +++ b/src/legend/qlegend.h @@ -48,6 +48,9 @@ class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) + Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged) +// TODO? +// Q_PROPERTY(QColor labelBorderColor READ labelBorderColor WRITE setLabelBorderColor NOTIFY labelBorderColorChanged) private: explicit QLegend(QChart *chart); @@ -70,6 +73,13 @@ public: void setFont(const QFont &font); QFont font() const; + void setLabelPen(const QPen &pen); + QPen labelPen() const; + void setLabelBrush(const QBrush &brush); + QBrush labelBrush() const; + + void setLabelColor(QColor color); + QColor labelColor() const; void setAlignment(Qt::Alignment alignment); Qt::Alignment alignment() const; @@ -94,6 +104,11 @@ Q_SIGNALS: void colorChanged(QColor color); void borderColorChanged(QColor color); void fontChanged(QFont font); + void labelPenChanged(QPen pen); + void labelBrushChanged(QBrush brush); + void labelColorChanged(QColor color); +// TODO? +// void labelBorderColorChanged(QColor color); private: QScopedPointer d_ptr; diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index 4969873..c22eb05 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -52,6 +52,8 @@ public: void attachToChart(); int roundness(qreal size); void setFont(const QFont &font); + void setLabelPen(const QPen &pen); + void setLabelBrush(const QBrush &brush); public Q_SLOTS: void handleSeriesAdded(QAbstractSeries *series, Domain *domain); @@ -69,6 +71,8 @@ private: QBrush m_brush; QPen m_pen; QFont m_font; + QPen m_labelPen; + QBrush m_labelBrush; QRectF m_rect; qreal m_offsetX; qreal m_offsetY;