##// END OF EJS Templates
Fix setting custom color to pie. Now the pie knows if the color is set by the user.
Fix setting custom color to pie. Now the pie knows if the color is set by the user.

File last commit:

r691:02b456949de5
r691:02b456949de5
Show More
piesliceitem.cpp
184 lines | 4.9 KiB | text/x-c | CppLexer
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 #include "piesliceitem_p.h"
Jani Honkonen
Rename piepresenter -> piechartitem
r568 #include "piechartitem_p.h"
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 #include "qpieseries.h"
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 #include "qpieslice.h"
Tero Ahola
Z order for pie
r490 #include "chartpresenter_p.h"
Tero Ahola
Integrated draft version of pie series
r51 #include <QPainter>
#include <QDebug>
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 #include <qmath.h>
Jani Honkonen
Make pie work better with chartwidgettest
r163 #include <QGraphicsSceneEvent>
Jani Honkonen
Refactoring piechart API (and internals)
r174 #include <QTime>
Tero Ahola
Integrated draft version of pie series
r51
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 #define PI 3.14159265 // TODO: is this defined in some header?
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 QPointF offset(qreal angle, qreal length)
{
qreal dx = qSin(angle*(PI/180)) * length;
qreal dy = qCos(angle*(PI/180)) * length;
return QPointF(dx, -dy);
}
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 :QGraphicsObject(parent)
Tero Ahola
Integrated draft version of pie series
r51 {
setAcceptHoverEvents(true);
Jani Honkonen
Pie chart refactoring
r142 setAcceptedMouseButtons(Qt::LeftButton);
Tero Ahola
Z order for pie
r490 setZValue(ChartPresenter::PieSeriesZValue);
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 PieSliceItem::~PieSliceItem()
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 QRectF PieSliceItem::boundingRect() const
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Refactoring pie series and animations.
r621 return m_boundingRect;
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 QPainterPath PieSliceItem::shape() const
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 // Don't include the label and label arm.
// This is used to detect a mouse clicks. We do not want clicks from label.
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 return m_slicePath;
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 painter->setClipRect(parentItem()->boundingRect());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 painter->save();
Jani Honkonen
Adding PIMPL to pie
r669 painter->setPen(m_data.m_slicePen);
painter->setBrush(m_data.m_sliceBrush);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 painter->drawPath(m_slicePath);
painter->restore();
Jani Honkonen
Fix setting custom color to pie. Now the pie knows if the color is set by the user.
r691 if (m_data.m_isLabelVisible) {
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 painter->save();
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 painter->setPen(m_data.m_labelArmPen);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 painter->drawPath(m_labelArmPath);
painter->restore();
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 painter->setFont(m_data.m_labelFont);
painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 }
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 emit hoverEnter();
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 emit hoverLeave();
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
Jani Honkonen
Pie chart refactoring
r142 {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 emit clicked();
}
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 void PieSliceItem::setSliceData(PieSliceData sliceData)
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 {
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 m_data = sliceData;
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 void PieSliceItem::updateGeometry()
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 {
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 if (m_data.m_radius <= 0)
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 return;
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 prepareGeometryChange();
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 // update slice path
qreal centerAngle;
QPointF armStart;
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 // update text rect
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 // update label arm path
QPointF labelTextStart;
Jani Honkonen
Rename PieSliceLayout -> PieSliceData. A "layout" is a bad name for this.
r668 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 // update text position
m_labelTextRect.moveBottomLeft(labelTextStart);
Jani Honkonen
Refactoring piechart API (and internals)
r174
Jani Honkonen
Refactoring pie series and animations.
r621 // update bounding rect
m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
Jani Honkonen
Refactoring the pie for animations (no actual animations yet)
r566 {
if (slice->isExploded()) {
Jani Honkonen
Adding PIMPL to pie
r669 qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2);
Jani Honkonen
Refactoring the pie for animations (no actual animations yet)
r566 qreal len = radius * slice->explodeDistanceFactor();
qreal dx = qSin(centerAngle*(PI/180)) * len;
qreal dy = -qCos(centerAngle*(PI/180)) * len;
point += QPointF(dx, dy);
}
return point;
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 }
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal* centerAngle, QPointF* armStart)
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 {
// calculate center angle
*centerAngle = startAngle + (angleSpan/2);
// calculate slice rectangle
QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
// slice path
// TODO: draw the shape so that it might have a hole in the center
QPainterPath path;
path.moveTo(rect.center());
path.arcTo(rect, -startAngle + 90, -angleSpan);
path.closeSubpath();
// calculate label arm start point
*armStart = center;
Jani Honkonen
Refactoring the pie for animations (no actual animations yet)
r566 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
return path;
}
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart)
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 {
qreal dx = qSin(angle*(PI/180)) * length;
qreal dy = -qCos(angle*(PI/180)) * length;
QPointF parm1 = start + QPointF(dx, dy);
QPointF parm2 = parm1;
if (angle < 180) { // arm swings the other way on the left side
parm2 += QPointF(textWidth, 0);
*textStart = parm1;
}
else {
parm2 += QPointF(-textWidth,0);
*textStart = parm2;
}
// elevate the text position a bit so that it does not hit the line
*textStart += QPointF(0, -5);
QPainterPath path;
path.moveTo(start);
path.lineTo(parm1);
path.lineTo(parm2);
return path;
}
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 QRectF PieSliceItem::labelTextRect(QFont font, QString text)
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 {
QFontMetricsF fm(font);
return fm.boundingRect(text);
}
Jani Honkonen
rename PieSlice -> PieSliceItem
r673 #include "moc_piesliceitem_p.cpp"
Jani Honkonen
Refactoring piechart API (and internals)
r174
Tero Ahola
Integrated draft version of pie series
r51 QTCOMMERCIALCHART_END_NAMESPACE