diff --git a/src/legend/legendlayout.cpp b/src/legend/legendlayout.cpp index 8486840..4319c64 100644 --- a/src/legend/legendlayout.cpp +++ b/src/legend/legendlayout.cpp @@ -24,6 +24,10 @@ #include "qlegend_p.h" #include "chartlayout_p.h" +#include "qlegendmarker_p.h" +#include "legendmarkeritem_p.h" +#include "qlegendmarker.h" + QTCOMMERCIALCHART_BEGIN_NAMESPACE LegendLayout::LegendLayout(QLegend *legend) @@ -125,52 +129,84 @@ void LegendLayout::setAttachedGeometry(const QRectF &rect) QRectF geometry = rect.adjusted(left, top, -right, -bottom); - switch (m_legend->alignment()) { + switch(m_legend->alignment()) { case Qt::AlignTop: case Qt::AlignBottom: { - QPointF point(0, 0); - foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { - if (marker->isVisible()) { - marker->setGeometry(geometry); - marker->setPos(point.x(), geometry.height() / 2 - marker->boundingRect().height() / 2); - const QRectF &rect = marker->boundingRect(); - size = size.expandedTo(rect.size()); - qreal w = rect.width(); - m_width += w; - point.setX(point.x() + w); + QPointF point(0,0); + /* + foreach (LegendMarker* marker, m_legend->d_ptr->markers()) { + if (marker->isVisible()) { + marker->setGeometry(geometry); + marker->setPos(point.x(),geometry.height()/2 - marker->boundingRect().height()/2); + const QRectF& rect = marker->boundingRect(); + size = size.expandedTo(rect.size()); + qreal w = rect.width(); + m_width+=w; + point.setX(point.x() + w); + } + } + */ +// New markers -->> + foreach (QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) { + if (marker->isVisible()) { + LegendMarkerItem* item = marker->d_ptr.data()->item(); + item->setGeometry(geometry); + item->setPos(point.x(),geometry.height()/2 - item->boundingRect().height()/2); + const QRectF& rect = item->boundingRect(); + size = size.expandedTo(rect.size()); + qreal w = rect.width(); + m_width+=w; + point.setX(point.x() + w); + } } +// <<-- New markers + if (m_width < geometry.width()) + m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top()); + else + m_legend->d_ptr->items()->setPos(geometry.topLeft()); + m_height = size.height(); } - if (m_width < geometry.width()) - m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top()); - else - m_legend->d_ptr->items()->setPos(geometry.topLeft()); - m_height = size.height(); - } - break; + break; case Qt::AlignLeft: case Qt::AlignRight: { - QPointF point(0, 0); - foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { - if (marker->isVisible()) { - marker->setGeometry(geometry); - marker->setPos(point); - const QRectF &rect = marker->boundingRect(); - qreal h = rect.height(); - size = size.expandedTo(rect.size()); - m_height += h; - point.setY(point.y() + h); + QPointF point(0,0); +/* + foreach (LegendMarker* marker, m_legend->d_ptr->markers()) { + if (marker->isVisible()) { + marker->setGeometry(geometry); + marker->setPos(point); + const QRectF& rect = marker->boundingRect(); + qreal h = rect.height(); + size = size.expandedTo(rect.size()); + m_height+=h; + point.setY(point.y() + h); + } + } +*/ +// New markers -->> + foreach (QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) { + if (marker->isVisible()) { + LegendMarkerItem* item = marker->d_ptr.data()->item(); + item->setGeometry(geometry); + item->setPos(point); + const QRectF& rect = item->boundingRect(); + qreal h = rect.height(); + size = size.expandedTo(rect.size()); + m_height+=h; + point.setY(point.y() + h); + } + } +// <<--- New markers + + if (m_height < geometry.height()) + m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2); + else + m_legend->d_ptr->items()->setPos(geometry.topLeft()); + m_width = size.width(); + break; } } - if (m_height < geometry.height()) - m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2); - else - m_legend->d_ptr->items()->setPos(geometry.topLeft()); - m_width = size.width(); - break; - } - } - m_minOffsetX = -left; m_minOffsetY = - top; m_maxOffsetX = m_width - geometry.width() - right; @@ -348,6 +384,7 @@ void LegendLayout::setDettachedGeometry(const QRectF &rect) QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { +/* QSizeF size(0, 0); qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); @@ -379,6 +416,49 @@ QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) cons } size += QSize(left + right, top + bottom); return size; +*/ +// New markers -->> + QSizeF size(0, 0); + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + + if(constraint.isValid()) { + foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) { + LegendMarkerItem *item = marker->d_ptr.data()->item(); + size = size.expandedTo(item->effectiveSizeHint(which)); + } + size = size.boundedTo(constraint); + } + else if (constraint.width() >= 0) { + qreal width = 0; + qreal height = 0; + foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) { + LegendMarkerItem *item = marker->d_ptr.data()->item(); + width+=item->effectiveSizeHint(which).width(); + height=qMax(height,item->effectiveSizeHint(which).height()); + } + + size = QSizeF(qMin(constraint.width(),width), height); + } + else if (constraint.height() >= 0) { + qreal width = 0; + qreal height = 0; + foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) { + LegendMarkerItem *item = marker->d_ptr.data()->item(); + width=qMax(width,item->effectiveSizeHint(which).width()); + height+=height,item->effectiveSizeHint(which).height(); + } + size = QSizeF(width,qMin(constraint.height(),height)); + } + else { + foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) { + LegendMarkerItem *item = marker->d_ptr.data()->item(); + size = size.expandedTo(item->effectiveSizeHint(which)); + } + } + size += QSize(left + right, top + bottom); + return size; +// <<-- New markers } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/legendmarkeritem.cpp b/src/legend/legendmarkeritem.cpp index 1c3d0f2..bf1e590 100644 --- a/src/legend/legendmarkeritem.cpp +++ b/src/legend/legendmarkeritem.cpp @@ -33,15 +33,17 @@ LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject QGraphicsObject(parent), m_marker(marker), m_markerRect(0,0,10.0,10.0), - m_boundingRect(0,0,0,0), + m_boundingRect(0,0,10,10), m_textItem(new QGraphicsSimpleTextItem(this)), m_rectItem(new QGraphicsRectItem(this)), m_margin(4), m_space(4) { - qDebug() << "LegendMarkerItem created for marker:" << m_marker; +// qDebug() << "LegendMarkerItem created for marker:" << m_marker; setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); m_rectItem->setRect(m_markerRect); +// setZValue(zValue() + 20); +// qDebug() << "z:" << this->zValue(); } void LegendMarkerItem::setPen(const QPen &pen) @@ -79,6 +81,7 @@ QFont LegendMarkerItem::font() const void LegendMarkerItem::setLabel(const QString label) { + qDebug() << "LegendMarkerItem::setlabel" << label; m_text = label; updateGeometry(); } @@ -88,11 +91,6 @@ QString LegendMarkerItem::label() const return m_text; } -QRectF LegendMarkerItem::boundingRect() const -{ - return m_boundingRect; -} - void LegendMarkerItem::setLabelBrush(const QBrush &brush) { m_textItem->setBrush(brush); @@ -103,10 +101,9 @@ QBrush LegendMarkerItem::labelBrush() const return m_textItem->brush(); } - void LegendMarkerItem::setGeometry(const QRectF& rect) { - QFontMetrics fn (font()); + QFontMetrics fn (m_font); int width = rect.width(); qreal x = m_margin + m_markerRect.width() + m_space + m_margin; @@ -133,6 +130,11 @@ void LegendMarkerItem::setGeometry(const QRectF& rect) m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y); } +QRectF LegendMarkerItem::boundingRect() const +{ + return m_boundingRect; +} + void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) diff --git a/src/legend/legendmarkeritem_p.h b/src/legend/legendmarkeritem_p.h index 8c18a10..65c319f 100644 --- a/src/legend/legendmarkeritem_p.h +++ b/src/legend/legendmarkeritem_p.h @@ -32,6 +32,7 @@ #include "qchartglobal.h" #include +#include #include #include #include @@ -46,7 +47,6 @@ class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem Q_OBJECT Q_INTERFACES(QGraphicsLayoutItem) public: -// explicit LegendMarkerItem(QAbstractSeries *m_series, QGraphicsObject *parent = 0); explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0); void setPen(const QPen &pen); @@ -77,7 +77,7 @@ protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); protected: - QLegendMarkerPrivate *m_marker; + QLegendMarkerPrivate *m_marker; // Knows QRectF m_markerRect; QRectF m_boundingRect; QGraphicsSimpleTextItem *m_textItem; @@ -86,6 +86,15 @@ protected: qreal m_space; QString m_text; + QString m_label; + QBrush m_labelBrush; + QFont m_font; + QPen m_pen; + QBrush m_brush; + bool m_visible; + + friend class QLegendMarkerPrivate; + friend class LegendLayout; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index c674926..757ebee 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -510,9 +510,7 @@ void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) marker->setFont(m_font); marker->setLabelBrush(m_labelBrush); marker->setVisible(series->isVisible()); -// TODO: possible hazard. What if marker is deleted and group still has pointer? m_items->addToGroup(marker->d_ptr.data()->item()); -// qDebug() << "item:" << marker->d_ptr.data()->item(); m_legendMarkers << marker; } diff --git a/src/legend/qlegendmarker.cpp b/src/legend/qlegendmarker.cpp index 3ba48b1..3c08b21 100644 --- a/src/legend/qlegendmarker.cpp +++ b/src/legend/qlegendmarker.cpp @@ -44,77 +44,69 @@ QLegendMarker::~QLegendMarker() QString QLegendMarker::label() const { - return d_ptr->m_label; + return d_ptr->label(); } void QLegendMarker::setLabel(const QString &label) { - d_ptr->m_label = label; + d_ptr->setLabel(label); } QBrush QLegendMarker::labelBrush() const { - return d_ptr->m_labelBrush; + return d_ptr->labelBrush(); } void QLegendMarker::setLabelBrush(const QBrush &brush) { - d_ptr->m_labelBrush = brush; + d_ptr->setLabelBrush(brush); } QFont QLegendMarker::font() const { - return d_ptr->m_font; + return d_ptr->font(); } void QLegendMarker::setFont(const QFont &font) { - d_ptr->m_font = font; + d_ptr->setFont(font); } QPen QLegendMarker::pen() const { - return d_ptr->m_pen; + return d_ptr->pen(); } void QLegendMarker::setPen(const QPen &pen) { - d_ptr->m_pen = pen; + d_ptr->setPen(pen); } QBrush QLegendMarker::brush() const { - return d_ptr->m_brush; + return d_ptr->brush(); } void QLegendMarker::setBrush(const QBrush &brush) { - d_ptr->m_brush = brush; + d_ptr->setBrush(brush); } bool QLegendMarker::isVisible() const { - return d_ptr->m_visible; + return d_ptr->isVisible(); } void QLegendMarker::setVisible(bool visible) { - d_ptr->m_visible = visible; + d_ptr->setVisible(visible); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -/* -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"; +// qDebug() << "QLegendMarkerPrivate created"; m_item = new LegendMarkerItem(this); } @@ -130,6 +122,66 @@ void QLegendMarkerPrivate::handleMousePressEvent(QGraphicsSceneEvent *event) emit q->clicked(); } +void QLegendMarkerPrivate::setPen(const QPen &pen) +{ + m_item->setPen(pen); +} + +QPen QLegendMarkerPrivate::pen() const +{ + return m_item->pen(); +} + +void QLegendMarkerPrivate::setBrush(const QBrush &brush) +{ + m_item->setBrush(brush); +} + +QBrush QLegendMarkerPrivate::brush() const +{ + return m_item->brush(); +} + +void QLegendMarkerPrivate::setFont(const QFont &font) +{ + m_item->setFont(font); +} + +QFont QLegendMarkerPrivate::font() const +{ + return m_item->font(); +} + +void QLegendMarkerPrivate::setLabel(const QString label) +{ + m_item->setLabel(label); +} + +QString QLegendMarkerPrivate::label() const +{ + return m_item->label(); +} + +void QLegendMarkerPrivate::setLabelBrush(const QBrush &brush) +{ + m_item->setLabelBrush(brush); +} + +QBrush QLegendMarkerPrivate::labelBrush() const +{ + return m_item->labelBrush(); +} + +bool QLegendMarkerPrivate::isVisible() const +{ + return m_item->isVisible(); +} + +void QLegendMarkerPrivate::setVisible(bool visible) +{ + m_item->setVisible(visible); +} + #include "moc_qlegendmarker.cpp" #include "moc_qlegendmarker_p.cpp" diff --git a/src/legend/qlegendmarker.h b/src/legend/qlegendmarker.h index 3e405c6..aa2463a 100644 --- a/src/legend/qlegendmarker.h +++ b/src/legend/qlegendmarker.h @@ -75,14 +75,12 @@ Q_SIGNALS: void clicked(); void hovered(bool status); -public Q_SLOTS: - virtual void updated() = 0; // TODO: private. Idea is that series signals, when some property has changed - protected: QScopedPointer d_ptr; Q_DISABLE_COPY(QLegendMarker) friend class QLegendPrivate; friend class QLegendMarkerPrivate; + friend class LegendLayout; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qlegendmarker_p.h b/src/legend/qlegendmarker_p.h index 51dde66..f5f2ab0 100644 --- a/src/legend/qlegendmarker_p.h +++ b/src/legend/qlegendmarker_p.h @@ -60,7 +60,7 @@ public: // explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q); explicit QLegendMarkerPrivate(QLegendMarker *q); virtual ~QLegendMarkerPrivate(); -/* + void setPen(const QPen &pen); QPen pen() const; @@ -75,7 +75,10 @@ public: void setLabelBrush(const QBrush &brush); QBrush labelBrush() const; -*/ + + bool isVisible() const; + void setVisible(bool visible); + // Helper for now. LegendMarkerItem* item() const { return m_item; } @@ -95,13 +98,14 @@ private: */ // New legend marker properties +/* QString m_label; QBrush m_labelBrush; QFont m_font; QPen m_pen; QBrush m_brush; bool m_visible; - +*/ friend class QLegendPrivate; // TODO: Is this needed? Q_DECLARE_PUBLIC(QLegendMarker) }; diff --git a/src/legend/qpielegendmarker.cpp b/src/legend/qpielegendmarker.cpp index 64f65a5..94671ea 100644 --- a/src/legend/qpielegendmarker.cpp +++ b/src/legend/qpielegendmarker.cpp @@ -24,23 +24,14 @@ #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -/* + QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) : - QLegendMarker(series, parent), - d_ptr(new QPieLegendMarkerPrivate(series,slice,this)) + QLegendMarker(*new QPieLegendMarkerPrivate(series,slice,this), parent) { - QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated())); - QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated())); - updated(); } -*/ -QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) : - QLegendMarker(*new QPieLegendMarkerPrivate(series,slice,this), parent) +QPieLegendMarker::~QPieLegendMarker() { - QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated())); - QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated())); -// updated(); } /*! @@ -65,24 +56,30 @@ QPieSlice* QPieLegendMarker::peerObject() //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -//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"; + QObject::connect(m_slice, SIGNAL(labelChanged()), this, SLOT(updated())); + QObject::connect(m_slice, SIGNAL(brushChanged()), this, SLOT(updated())); updated(); } QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate() { + QObject::disconnect(m_slice, SIGNAL(labelChanged()), this, SLOT(updated())); + QObject::disconnect(m_slice, SIGNAL(brushChanged()), this, SLOT(updated())); } void QPieLegendMarkerPrivate::updated() { + qDebug() << "QPieLegendMarkerPrivate::updated"; m_item->setBrush(m_slice->brush()); m_item->setLabel(m_slice->label()); + m_item->setPen(m_slice->pen()); + m_item->setBrush(m_slice->brush()); } #include "moc_qpielegendmarker.cpp" diff --git a/src/legend/qpielegendmarker.h b/src/legend/qpielegendmarker.h index 84427f8..38f3ebc 100644 --- a/src/legend/qpielegendmarker.h +++ b/src/legend/qpielegendmarker.h @@ -34,6 +34,7 @@ class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker public: explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0); + virtual ~QPieLegendMarker(); virtual QAbstractSeries* series(); virtual QPieSlice* peerObject();