##// END OF EJS Templates
Fix compilation issue with scatterpresenter.cpp
Fix compilation issue with scatterpresenter.cpp

File last commit:

r181:f52f5b858a82
r202:384e4e17c6c1
Show More
pieslice.cpp
142 lines | 3.6 KiB | text/x-c | CppLexer
Tero Ahola
Integrated draft version of pie series
r51 #include "pieslice.h"
Jani Honkonen
Refactoring piechart API (and internals)
r174 #include "pieslicelabel.h"
Jani Honkonen
Moved pie stuff to own .pri file and rename stuff
r146 #include "piepresenter.h"
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 #include "qpieseries.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
Use signals from pieseries, visible hover and exploding slices
r157 #define PI 3.14159265
#define EXPLODE_OFFSET 20
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
Refactoring piechart API (and internals)
r174 PieSlice::PieSlice(QPieSliceId id, QPieSeries *series, QGraphicsItem* parent)
:QGraphicsObject(parent),
m_id(id),
m_series(series),
m_slicelabel(new PieSliceLabel(this)),
m_isHovering(false)
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 Q_ASSERT(series);
Tero Ahola
Integrated draft version of pie series
r51 setAcceptHoverEvents(true);
Jani Honkonen
Pie chart refactoring
r142 setAcceptedMouseButtons(Qt::LeftButton);
Jani Honkonen
Refactoring piechart API (and internals)
r174 updateData();
Tero Ahola
Integrated draft version of pie series
r51 }
PieSlice::~PieSlice()
{
Jani Honkonen
Refactoring piechart API (and internals)
r174 qDebug() << "~PieSlice()" << m_id;
Tero Ahola
Integrated draft version of pie series
r51 }
QRectF PieSlice::boundingRect() const
{
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 return m_rect;
Tero Ahola
Integrated draft version of pie series
r51 }
QPainterPath PieSlice::shape() const
{
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 return m_path;
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
Pie chart refactoring
r142 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 // set hover brush
// TODO: what if we are using gradients...
QBrush brush = m_data.brush();
if (m_isHovering)
brush.setColor(brush.color().lighter());
Jani Honkonen
Pie chart refactoring
r142
Jani Honkonen
Refactoring piechart API (and internals)
r174 painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(m_data.pen());
painter->setBrush(brush);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 painter->drawPath(m_path);
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
Refactoring piechart API (and internals)
r174 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
Tero Ahola
Integrated draft version of pie series
r51 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 m_isHovering = true;
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 update();
Jani Honkonen
Refactoring piechart API (and internals)
r174 // TODO: emit hoverEnter()
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 }
Jani Honkonen
Make pie work better with chartwidgettest
r163 void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 m_isHovering = false;
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 update();
Jani Honkonen
Refactoring piechart API (and internals)
r174 // TODO: emit hoverLeave()
Tero Ahola
Integrated draft version of pie series
r51 }
Jani Honkonen
Make pie work better with chartwidgettest
r163 void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
Jani Honkonen
Pie chart refactoring
r142 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 // TODO: emit clicked
// TODO: should we let the user decide if this can be exploded?
m_data.setExploded(!m_data.isExploded());
m_series->update(m_data);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 }
Jani Honkonen
Refactoring piechart API (and internals)
r174 void PieSlice::updateGeometry(QRectF rect, qreal startAngle, qreal span)
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 {
prepareGeometryChange();
Jani Honkonen
Refactoring piechart API (and internals)
r174 // calculate center angle
qreal centerAngle = startAngle + (span/2);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Refactoring piechart API (and internals)
r174 // adjust rect for exploding
rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET);
if (m_data.isExploded()) {
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 QPointF d = offset((centerAngle), EXPLODE_OFFSET);
rect.translate(d.x(), d.y());
}
Jani Honkonen
Refactoring piechart API (and internals)
r174 // update slice path
// TODO: draw the shape so that it might have a hole in the center
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 QPainterPath path;
path.moveTo(rect.center());
Jani Honkonen
Refactoring piechart API (and internals)
r174 path.arcTo(rect, -startAngle + 90, -span);
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 m_path = path;
m_rect = path.boundingRect();
Jani Honkonen
Refactoring piechart API (and internals)
r174 // update label position
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 qreal radius = rect.height() / 2;
Jani Honkonen
Refactoring piechart API (and internals)
r174 QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5);
Jani Honkonen
Implementing slice label
r181
m_slicelabel->setArmStartPoint(edgeCenter);
m_slicelabel->setArmAngle(centerAngle);
m_slicelabel->setArmLength(50);
m_slicelabel->updateGeometry();
Jani Honkonen
Refactoring piechart API (and internals)
r174
//qDebug() << "PieSlice::updateGeometry" << m_rect;
Jani Honkonen
Pie chart refactoring
r142 }
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Refactoring piechart API (and internals)
r174 void PieSlice::updateData()
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 if (!m_series->m_slices.contains(m_id))
qWarning() << "PieSlice::updateData(): cannot find slice data!" << m_id;
QPieSlice data = m_series->slice(m_id);
// TODO: find out what has changed and trigger some animation
m_data = data;
update();
m_slicelabel->setVisible(m_data.isLabelVisible());
Jani Honkonen
Implementing slice label
r181 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
Jani Honkonen
Refactoring piechart API (and internals)
r174 m_slicelabel->update();
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 }
Jani Honkonen
Refactoring piechart API (and internals)
r174 #include "moc_pieslice.cpp"
Tero Ahola
Integrated draft version of pie series
r51 QTCOMMERCIALCHART_END_NAMESPACE