From f52f5b858a82890784b9460e55949405c778b31e 2012-02-15 10:41:49 From: Jani Honkonen Date: 2012-02-15 10:41:49 Subject: [PATCH] Implementing slice label --- diff --git a/src/piechart/pieslice.cpp b/src/piechart/pieslice.cpp index 9ae8a90..321890d 100644 --- a/src/piechart/pieslice.cpp +++ b/src/piechart/pieslice.cpp @@ -109,7 +109,11 @@ void PieSlice::updateGeometry(QRectF rect, qreal startAngle, qreal span) // update label position qreal radius = rect.height() / 2; QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); - m_slicelabel->updateGeometry(edgeCenter, centerAngle, 50); + + m_slicelabel->setArmStartPoint(edgeCenter); + m_slicelabel->setArmAngle(centerAngle); + m_slicelabel->setArmLength(50); + m_slicelabel->updateGeometry(); //qDebug() << "PieSlice::updateGeometry" << m_rect; } @@ -125,8 +129,11 @@ void PieSlice::updateData() update(); - m_slicelabel->setLabel(m_data.label()); m_slicelabel->setVisible(m_data.isLabelVisible()); + m_slicelabel->setText(m_data.label()); + //m_slicelabel->setPen(m_data.labelPen()); + //m_slicelabel->setFont(m_data.labelFont()); + m_slicelabel->updateGeometry(); // text size & font modifies the geometry m_slicelabel->update(); } diff --git a/src/piechart/pieslicelabel.cpp b/src/piechart/pieslicelabel.cpp index 8d6b4ba..198b1bc 100644 --- a/src/piechart/pieslicelabel.cpp +++ b/src/piechart/pieslicelabel.cpp @@ -1,6 +1,7 @@ #include "pieslicelabel.h" #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -9,68 +10,55 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE PieSliceLabel::PieSliceLabel(QGraphicsItem* parent) :QGraphicsItem(parent) { + // set defaults m_pen = QPen(Qt::black); } -PieSliceLabel::~PieSliceLabel() -{ - -} - -QRectF PieSliceLabel::boundingRect() const -{ - return m_rect; -} - -QPainterPath PieSliceLabel::shape() const -{ - return m_path; -} - void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) { painter->setRenderHint(QPainter::Antialiasing); + painter->setPen(m_pen); - painter->drawPath(m_path); + painter->drawPath(m_armPath); + + // TODO: do we need a pen for text? + painter->setFont(m_font); + painter->drawText(m_textRect, m_text); } -void PieSliceLabel::updateGeometry(const QPointF& startPoint, qreal armAngle, qreal armLength) +void PieSliceLabel::updateGeometry() { prepareGeometryChange(); - QPainterPath path; - path.moveTo(startPoint); + // calculate text size + QFontMetricsF fm(m_font); + QRectF textRect = fm.boundingRect(m_text); - // draw arm - qreal dx = qSin(armAngle*(PI/180)) * armLength; - qreal dy = -qCos(armAngle*(PI/180)) * armLength; - QPointF p1 = startPoint + QPointF(dx, dy); - path.lineTo(p1); + // calculate path for arm and text start point + qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; + qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; + QPointF parm1 = m_armStartPoint + QPointF(dx, dy); - QPointF p2 = p1; - QPointF pt = p1; - if (armAngle < 180) { - p2 += QPointF(50, 0); + // calculate horizontal arm and text position + QPointF parm2 = parm1; + textRect.moveBottomLeft(parm1); + if (m_armAngle < 180) { // arm swings the other way on the left side + parm2 += QPointF(m_textRect.width(), 0); } else { - p2 += QPointF(-50,0); - pt = p2; + parm2 += QPointF(-m_textRect.width(),0); + textRect.moveBottomLeft(parm2); } - path.lineTo(p2); - - QFont font; - pt += QPointF(0,-2); - path.addText(pt, font, m_label); - - m_path = path; - m_rect = path.boundingRect(); -} -void PieSliceLabel::setLabel(QString label) -{ - m_label = label; - // TODO: animation? + // update arm path + QPainterPath path; + path.moveTo(m_armStartPoint); + path.lineTo(parm1); + path.lineTo(parm2); + + // update paths & rects + m_armPath = path; + m_textRect = textRect; + m_rect = path.boundingRect().united(m_textRect); } - - QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/pieslicelabel.h b/src/piechart/pieslicelabel.h index 7b39f6e..0ce3bea 100644 --- a/src/piechart/pieslicelabel.h +++ b/src/piechart/pieslicelabel.h @@ -4,31 +4,52 @@ #include "qchartglobal.h" #include #include +#include +class QGraphicsTextItem; QTCOMMERCIALCHART_BEGIN_NAMESPACE class PieSliceLabel : public QGraphicsItem { public: PieSliceLabel(QGraphicsItem* parent = 0); - ~PieSliceLabel(); + ~PieSliceLabel() {}; public: // from QGraphicsItem - QRectF boundingRect() const; - QPainterPath shape() const; + QRectF boundingRect() const { return m_rect; } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); public: - void updateGeometry(const QPointF& startPoint, qreal armAngle, qreal armLength); - void setLabel(QString label); - QString label() const {return m_label;} - void setPen(QPen pen); + void updateGeometry(); + + void setArmStartPoint(QPointF point) { m_armStartPoint = point; } + QPointF armStartPoint() const { return m_armStartPoint; } + + void setArmAngle(qreal angle) { m_armAngle = angle; } + qreal armAngle() const { return m_armAngle; } + + void setArmLength(qreal len) { m_armLength = len; } + qreal armLength() const { return m_armLength; } + + void setText(QString text) { m_text = text; } + QString text() const { return m_text; } + + void setPen(QPen pen) { m_pen = pen; } + QPen pen() const { return m_pen; } + + void setFont(QFont font) { m_font = font; } + QFont font() const { return m_font; } private: - QString m_label; - QPainterPath m_path; + QPointF m_armStartPoint; + qreal m_armAngle; + qreal m_armLength; + QString m_text; QRectF m_rect; + QPainterPath m_armPath; + QRectF m_textRect; QPen m_pen; + QFont m_font; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index 34120ad..d3400b7 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -35,6 +35,14 @@ public: void setLabelVisible(bool visible) { m_isLabelVisible = visible; } bool isLabelVisible() const { return m_isLabelVisible; } + // TODO: + //void setLabelPen(QPen pen) {}; + //QPen labelPen() const {}; + //void setLabelFont(QFont font); + //QFont labelFont() const; + //void setLabelArmLenght(qreal len) {}; + //qreal labelArmLenght() const {}; + void setExploded(bool exploded) { m_isExploded = exploded; } bool isExploded() const { return m_isExploded; } @@ -109,16 +117,12 @@ public: // TODO: sorting? - // TODO: convenience functions - //void updateValue(int sliceIndex, qreal value); - //void updateLabel(int sliceIndex, QString label); - //void updateColor(int sliceIndex, QColor color); - //void updateExploded(int slizeIndex, bool exploded); - - // TODO: customization - // set/get pen/brush - // - for label - // - for whole pie/slice + // TODO: convenience functions? + //void setValue(QPieSliceId id, qreal value); + //void setLabel(QPieSliceId id, QString label); + //void setPen(QPieSliceId id, QPen pen); + //void setBrush(QPieSliceId id, QBrush brush); + //void setExploded(QPieSliceId id, bool exploded); void setSizeFactor(qreal sizeFactor); qreal sizeFactor() const { return m_sizeFactor; } @@ -130,9 +134,11 @@ Q_SIGNALS: void changed(const QPieSeries::ChangeSet& changeSet); void sizeFactorChanged(); void positionChanged(); + + // TODO: //void sliceClicked(QPieSliceId id); - //void sliceHoverEnter(QPieSliceId id); - //void sliceHoverLeave(QPieSliceId id); + // ?? void sliceHoverEnter(QPieSliceId id); + // ?? void sliceHoverLeave(QPieSliceId id); private: QPieSliceId generateSliceId();