##// END OF EJS Templates

File last commit:

r204:a4ba86638d8d
r269:05d1d227ed47 merge
Show More
qpieseries.cpp
299 lines | 6.3 KiB | text/x-c | CppLexer
Jani Honkonen
Pie chart refactoring
r142 #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"
Jani Honkonen
Moved pie stuff to own .pri file and rename stuff
r146 #include "piepresenter.h"
Jani Honkonen
Pie chart refactoring
r142 #include "pieslice.h"
#include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice)
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_added.contains(slice))
m_added << slice;
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 QPieSeries::ChangeSet::appendChanged(QPieSlice* slice)
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_changed.contains(slice))
m_changed << slice;
}
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 QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice)
{
if (!m_removed.contains(slice))
m_removed << slice;
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 QList<QPieSlice*> QPieSeries::ChangeSet::added() const
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 return m_added;
Jani Honkonen
Make pie work better with chartwidgettest
r163 }
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 QList<QPieSlice*> QPieSeries::ChangeSet::changed() const
Jani Honkonen
Make pie work better with chartwidgettest
r163 {
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 return m_changed;
}
Jani Honkonen
Make pie work better with chartwidgettest
r163
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 QList<QPieSlice*> QPieSeries::ChangeSet::removed() const
{
return m_removed;
}
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 bool QPieSeries::ChangeSet::isEmpty() const
{
if (m_added.count() || m_changed.count() || m_removed.count())
return false;
return true;
}
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
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 QPieSeries::QPieSeries(QObject *parent) :
QChartSeries(parent),
m_sizeFactor(1.0),
m_position(PiePositionMaximized),
m_pieStartAngle(0),
m_pieSpan(360)
{
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 }
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 QPieSeries::~QPieSeries()
{
Jani Honkonen
Fix a major memory leak
r204
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 }
bool QPieSeries::setData(QList<qreal> data)
{
// TODO: remove this function
QList<QPieSlice*> slices;
foreach (int value, data)
slices << new QPieSlice(value, QString::number(value));
set(slices);
Jani Honkonen
Make pie work better with chartwidgettest
r163 return true;
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 QPieSeries::set(QList<QPieSlice*> slices)
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 clear();
add(slices);
}
Jani Honkonen
Make pie work better with chartwidgettest
r163
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 void QPieSeries::add(QList<QPieSlice*> slices)
{
Jani Honkonen
Refactoring piechart API (and internals)
r174 ChangeSet changeSet;
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 foreach (QPieSlice* s, slices) {
s->setParent(this);
m_slices << s;
changeSet.appendAdded(s);
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
Jani Honkonen
Use signals from pieseries, visible hover and exploding slices
r157
Jani Honkonen
Refactoring piechart API (and internals)
r174 updateDerivativeData();
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 foreach (QPieSlice* s, slices) {
connect(s, SIGNAL(changed()), this, SLOT(sliceChanged()));
connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked()));
connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
}
emit changed(changeSet);
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 QPieSeries::add(QPieSlice* slice)
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 add(QList<QPieSlice*>() << slice);
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 QPieSlice* QPieSeries::add(qreal value, QString name)
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 QPieSlice* slice = new QPieSlice(value, name);
add(slice);
return slice;
}
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 QPieSeries::remove(QPieSlice* slice)
{
if (!m_slices.removeOne(slice)) {
Q_ASSERT(0); // TODO: remove before release
return;
}
Jani Honkonen
Refactoring piechart API (and internals)
r174
ChangeSet changeSet;
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 changeSet.appendRemoved(slice);
Jani Honkonen
Refactoring piechart API (and internals)
r174 emit changed(changeSet);
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 delete slice;
slice = NULL;
updateDerivativeData();
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 QPieSeries::clear()
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.count() == 0)
return;
Jani Honkonen
Refactoring piechart API (and internals)
r174
ChangeSet changeSet;
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 foreach (QPieSlice* s, m_slices) {
changeSet.appendRemoved(s);
m_slices.removeOne(s);
delete s;
}
Jani Honkonen
Refactoring piechart API (and internals)
r174 emit changed(changeSet);
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 updateDerivativeData();
Jani Honkonen
Pie chart refactoring
r142 }
void QPieSeries::setSizeFactor(qreal factor)
{
Jani Honkonen
Make pie work better with chartwidgettest
r163 if (factor < 0.0)
return;
if (m_sizeFactor != factor) {
Jani Honkonen
Pie chart refactoring
r142 m_sizeFactor = factor;
Jani Honkonen
Make pie work better with chartwidgettest
r163 emit sizeFactorChanged();
}
Jani Honkonen
Pie chart refactoring
r142 }
void QPieSeries::setPosition(PiePosition position)
{
Jani Honkonen
Make pie work better with chartwidgettest
r163 if (m_position != position) {
m_position = position;
emit positionChanged();
}
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 QPieSeries::setSpan(qreal startAngle, qreal span)
{
if (startAngle >= 0 && startAngle < 360 &&
span > 0 && span <= 360) {
m_pieStartAngle = startAngle;
m_pieSpan = span;
updateDerivativeData();
}
}
void QPieSeries::setLabelsVisible(bool visible)
{
foreach (QPieSlice* s, m_slices)
s->setLabelVisible(visible);
}
void QPieSeries::enableClickExplodes(bool enable)
{
if (enable)
connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*)));
else
disconnect(this, SLOT(toggleExploded(QPieSlice*)));
}
void QPieSeries::enableHoverHighlight(bool enable)
{
if (enable) {
connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*)));
connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*)));
} else {
disconnect(this, SLOT(hoverEnter(QPieSlice*)));
disconnect(this, SLOT(hoverLeave(QPieSlice*)));
}
}
void QPieSeries::sliceChanged()
{
QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
Q_ASSERT(m_slices.contains(slice));
ChangeSet changeSet;
changeSet.appendChanged(slice);
emit changed(changeSet);
updateDerivativeData();
}
void QPieSeries::sliceClicked()
{
QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
Q_ASSERT(m_slices.contains(slice));
emit clicked(slice);
}
void QPieSeries::sliceHoverEnter()
{
QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
Q_ASSERT(m_slices.contains(slice));
emit hoverEnter(slice);
}
void QPieSeries::sliceHoverLeave()
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 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
Q_ASSERT(m_slices.contains(slice));
emit hoverLeave(slice);
}
void QPieSeries::toggleExploded(QPieSlice* slice)
{
Q_ASSERT(slice);
slice->setExploded(!slice->isExploded());
}
void QPieSeries::highlightOn(QPieSlice* slice)
{
Q_ASSERT(slice);
QColor c = slice->brush().color().lighter();
slice->setBrush(c);
}
void QPieSeries::highlightOff(QPieSlice* slice)
{
Q_ASSERT(slice);
QColor c = slice->brush().color().darker(150);
slice->setBrush(c);
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
void QPieSeries::updateDerivativeData()
{
m_total = 0;
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 // nothing to do?
if (m_slices.count() == 0)
return;
// calculate total
foreach (QPieSlice* s, m_slices)
m_total += s->value();
// we must have some values
Q_ASSERT(m_total > 0); // TODO
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 // update slice attributes
qreal sliceAngle = m_pieStartAngle;
foreach (QPieSlice* s, m_slices) {
bool changed = false;
qreal percentage = s->value() / m_total;
if (s->m_percentage != percentage) {
s->m_percentage = percentage;
changed = true;
}
qreal sliceSpan = m_pieSpan * percentage;
if (s->m_span != sliceSpan) {
s->m_span = sliceSpan;
changed = true;
}
if (s->m_angle != sliceAngle) {
s->m_angle = sliceAngle;
changed = true;
}
sliceAngle += sliceSpan;
if (changed)
emit s->changed();
}
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
Jani Honkonen
Pie chart refactoring
r142 #include "moc_qpieseries.cpp"
QTCOMMERCIALCHART_END_NAMESPACE