##// END OF EJS Templates
Spline chart example added
Spline chart example added

File last commit:

r413:afa09ce7f031
r434:52aa7c3abf86
Show More
piepresenter.cpp
220 lines | 6.8 KiB | text/x-c | CppLexer
Jani Honkonen
Pie chart refactoring
r142
Jani Honkonen
Add _p to pie internal headers
r353 #include "piepresenter_p.h"
#include "pieslice_p.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"
Jani Honkonen
Add _p to pie internal headers
r353 #include "pieslicelabel_p.h"
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 #include "qpieseries.h"
#include <qmath.h>
Jani Honkonen
Pie chart refactoring
r142 #include <QDebug>
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 #include <QFontMetrics>
Jani Honkonen
Pie chart refactoring
r142
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Jani Honkonen
Refactoring piechart API (and internals)
r174 PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series)
:ChartItem(parent),
m_series(series)
Jani Honkonen
Pie chart refactoring
r142 {
Q_ASSERT(series);
Jani Honkonen
Refactoring piechart API (and internals)
r174 connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&)));
Jani Honkonen
Make pie work better with chartwidgettest
r163 connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry()));
connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry()));
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289
if (m_series->count()) {
QPieSeries::ChangeSet changeSet;
changeSet.appendAdded(m_series->m_slices);
handleSeriesChanged(changeSet);
}
Jani Honkonen
Pie chart refactoring
r142 }
Jani Honkonen
Moved pie stuff to own .pri file and rename stuff
r146 PiePresenter::~PiePresenter()
Jani Honkonen
Pie chart refactoring
r142 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 // slices deleted automatically through QGraphicsItem
Jani Honkonen
Pie chart refactoring
r142 }
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 void PiePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
Jani Honkonen
Pie chart refactoring
r142 {
Jani Honkonen
Refactoring piechart API (and internals)
r174 // TODO: paint shadows for all components
// - get paths from items & merge & offset and draw with shadow color?
}
void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet)
{
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 //qDebug() << "PiePresenter::handleSeriesChanged()";
//qDebug() << " added : " << changeSet.added();
//qDebug() << " changed: " << changeSet.changed();
//qDebug() << " removed: " << changeSet.removed();
Jani Honkonen
Refactoring piechart API (and internals)
r174
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 foreach (QPieSlice* s, changeSet.added())
addSlice(s);
foreach (QPieSlice* s, changeSet.changed())
updateSlice(s);
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 foreach (QPieSlice* s, changeSet.removed())
deleteSlice(s);
Jani Honkonen
Pie chart refactoring
r142
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 // every change possibly changes the actual pie size
updateGeometry();
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 }
Jani Honkonen
Refactoring piechart API (and internals)
r174
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 void PiePresenter::handleDomainChanged(const Domain& domain)
{
// TODO
}
void PiePresenter::handleGeometryChanged(const QRectF& rect)
{
m_rect = rect;
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 prepareGeometryChange();
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 updateGeometry();
Jani Honkonen
Pie chart refactoring
r142 }
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157 void PiePresenter::updateGeometry()
Jani Honkonen
Pie chart refactoring
r142 {
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 if (!m_rect.isValid() || m_rect.isEmpty())
return;
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 // calculate maximum rectangle for pie
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 QRectF pieRect = m_rect;
if (pieRect.width() < pieRect.height()) {
pieRect.setWidth(pieRect.width() * m_series->sizeFactor());
pieRect.setHeight(pieRect.width());
pieRect.moveCenter(m_rect.center());
Jani Honkonen
Pie chart refactoring
r142 } else {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 pieRect.setHeight(pieRect.height() * m_series->sizeFactor());
pieRect.setWidth(pieRect.height());
pieRect.moveCenter(m_rect.center());
Jani Honkonen
Pie chart refactoring
r142 }
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 // position the pie rectangle
Jani Honkonen
Refactoring piechart API (and internals)
r174 switch (m_series->position()) {
Jani Honkonen
Pie chart refactoring
r142 case QPieSeries::PiePositionTopLeft: {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 pieRect.setHeight(pieRect.height() / 2);
pieRect.setWidth(pieRect.height());
pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2));
Jani Honkonen
Pie chart refactoring
r142 break;
}
case QPieSeries::PiePositionTopRight: {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 pieRect.setHeight(pieRect.height() / 2);
pieRect.setWidth(pieRect.height());
pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2));
Jani Honkonen
Pie chart refactoring
r142 break;
}
case QPieSeries::PiePositionBottomLeft: {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 pieRect.setHeight(pieRect.height() / 2);
pieRect.setWidth(pieRect.height());
pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3));
Jani Honkonen
Pie chart refactoring
r142 break;
}
case QPieSeries::PiePositionBottomRight: {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 pieRect.setHeight(pieRect.height() / 2);
pieRect.setWidth(pieRect.height());
pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3));
Jani Honkonen
Pie chart refactoring
r142 break;
}
default:
break;
}
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 // calculate how much space we need around the pie rectangle (labels & exploding)
qreal delta = 0;
qreal pieRadius = pieRect.height() / 2;
foreach (QPieSlice* s, m_series->m_slices) {
// calculate the farthest point in the slice from the pie center
Jani Honkonen
Pie now actually withs the given rectangle and does not go over
r413
// the arm
Jani Honkonen
QPieSlice: angle() -> startAngle(), angleSpan() -> endAngle()
r355 qreal centerAngle = s->m_startAngle + (s->m_angleSpan / 2);
Jani Honkonen
Pie now actually withs the given rectangle and does not go over
r413 qreal len = pieRadius + PIESLICE_LABEL_GAP + s->labelArmLength() + s->explodeDistance();
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 QPointF dp(qSin(centerAngle*(PI/180)) * len, -qCos(centerAngle*(PI/180)) * len);
QPointF p = pieRect.center() + dp;
Jani Honkonen
Pie now actually withs the given rectangle and does not go over
r413 // the label text
QFontMetricsF fm(s->labelFont());
QRectF labelRect = fm.boundingRect(s->label());
if (centerAngle < 90 || centerAngle > 270)
p += QPointF(0, -labelRect.height());
if (centerAngle < 180)
p += QPointF(labelRect.width(), 0);
else
p += QPointF(-labelRect.width(), 0);
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289
// calculate how much the radius must get smaller to fit that point in the base rectangle
qreal dt = m_rect.top() - p.y();
if (dt > delta) delta = dt;
qreal dl = m_rect.left() - p.x();
if (dl > delta) delta = dl;
qreal dr = p.x() - m_rect.right();
if (dr > delta) delta = dr;
qreal db = p.y() - m_rect.bottom();
if (db > delta) delta = db;
//if (!m_rect.contains(p)) qDebug() << s->label() << dt << dl << dr << db << "delta" << delta;
}
// shrink the pie rectangle so that everything outside it fits the base rectangle
pieRect.adjust(delta, delta, -delta, -delta);
// update slices
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 if (m_pieRect != pieRect) {
m_pieRect = pieRect;
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 //qDebug() << "PiePresenter::updateGeometry()" << m_rect << m_pieRect;
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 foreach (PieSlice* s, m_slices.values()) {
s->setPieRect(m_pieRect);
s->updateGeometry();
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 s->update();
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 }
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
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 void PiePresenter::addSlice(QPieSlice* sliceData)
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 //qDebug() << "PiePresenter::addSlice()" << sliceData;
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 if (m_slices.keys().contains(sliceData)) {
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 Q_ASSERT(0); // TODO: how to handle this nicely?
Jani Honkonen
Refactoring piechart API (and internals)
r174 return;
}
// create slice
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 PieSlice *slice = new PieSlice(this);
Jani Honkonen
Fix initial pie position bug
r208 slice->setPieRect(m_pieRect);
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 slice->updateData(sliceData);
Jani Honkonen
Fix issue with pie label not drawn correctly when first created
r209 slice->updateGeometry();
slice->update();
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 m_slices.insert(sliceData, slice);
// connect signals
connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked()));
connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter()));
connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave()));
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 void PiePresenter::updateSlice(QPieSlice* sliceData)
{
//qDebug() << "PiePresenter::updateSlice()" << sliceData;
if (!m_slices.contains(sliceData)) {
Q_ASSERT(0); // TODO: how to handle this nicely?
return;
}
m_slices[sliceData]->updateData(sliceData);
}
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 void PiePresenter::deleteSlice(QPieSlice* sliceData)
Jani Honkonen
Refactoring piechart API (and internals)
r174 {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 //qDebug() << "PiePresenter::deleteSlice()" << sliceData;
Jani Honkonen
Refactoring piechart API (and internals)
r174
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289 if (!m_slices.contains(sliceData)) {
Q_ASSERT(0); // TODO: how to handle this nicely?
return;
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
Jani Honkonen
Make pie fit better inside its given rectangle. Label texts still go outside. Needs a bit more work...
r289
delete m_slices.take(sliceData);
Jani Honkonen
Pie chart refactoring
r142 }
Jani Honkonen
Add _p to pie internal headers
r353 #include "moc_piepresenter_p.cpp"
Jani Honkonen
Pie chart refactoring
r142
QTCOMMERCIALCHART_END_NAMESPACE