barchartitem.cpp
217 lines
| 6.2 KiB
| text/x-c
|
CppLexer
sauimone
|
r666 | #include "barchartitem_p.h" | ||
sauimone
|
r126 | #include "bar_p.h" | ||
sauimone
|
r263 | #include "barvalue_p.h" | ||
sauimone
|
r240 | #include "qbarset.h" | ||
sauimone
|
r338 | #include "qbarseries.h" | ||
sauimone
|
r487 | #include "qchart.h" | ||
#include "qchartaxis.h" | ||||
#include "qchartaxiscategories.h" | ||||
sauimone
|
r594 | #include "chartpresenter_p.h" | ||
sauimone
|
r671 | #include "chartanimator_p.h" | ||
Michal Klocek
|
r679 | #include "chartdataset_p.h" | ||
sauimone
|
r126 | #include <QDebug> | ||
sauimone
|
r288 | #include <QToolTip> | ||
sauimone
|
r126 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r677 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : | ||
ChartItem(presenter), | ||||
Tero Ahola
|
r518 | mLayoutSet(false), | ||
Michal Klocek
|
r677 | mSeries(series) | ||
sauimone
|
r126 | { | ||
sauimone
|
r288 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | ||
sauimone
|
r681 | connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); | ||
sauimone
|
r671 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); | ||
connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); | ||||
sauimone
|
r594 | setZValue(ChartPresenter::BarSeriesZValue); | ||
sauimone
|
r167 | dataChanged(); | ||
sauimone
|
r126 | } | ||
sauimone
|
r666 | BarChartItem::~BarChartItem() | ||
sauimone
|
r126 | { | ||
sauimone
|
r288 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | ||
sauimone
|
r126 | } | ||
sauimone
|
r666 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
sauimone
|
r126 | { | ||
if (!mLayoutSet) { | ||||
sauimone
|
r666 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; | ||
sauimone
|
r126 | return; | ||
} | ||||
sauimone
|
r473 | foreach(QGraphicsItem* i, childItems()) { | ||
i->paint(painter,option,widget); | ||||
} | ||||
sauimone
|
r126 | } | ||
sauimone
|
r666 | QRectF BarChartItem::boundingRect() const | ||
sauimone
|
r126 | { | ||
Michal Klocek
|
r679 | return m_rect; | ||
sauimone
|
r126 | } | ||
sauimone
|
r165 | |||
sauimone
|
r666 | void BarChartItem::dataChanged() | ||
sauimone
|
r126 | { | ||
sauimone
|
r172 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | ||
sauimone
|
r126 | // Delete old bars | ||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
sauimone
|
r256 | mBars.clear(); | ||
sauimone
|
r263 | mFloatingValues.clear(); | ||
sauimone
|
r681 | mLayout.clear(); | ||
sauimone
|
r256 | |||
sauimone
|
r126 | // Create new graphic items for bars | ||
sauimone
|
r366 | for (int c=0; c<mSeries->categoryCount(); c++) { | ||
sauimone
|
r425 | QString category = mSeries->categoryName(c); | ||
sauimone
|
r366 | for (int s=0; s<mSeries->barsetCount(); s++) { | ||
sauimone
|
r357 | QBarSet *set = mSeries->barsetAt(s); | ||
sauimone
|
r425 | Bar *bar = new Bar(category,this); | ||
sauimone
|
r239 | childItems().append(bar); | ||
sauimone
|
r256 | mBars.append(bar); | ||
sauimone
|
r425 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | ||
connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | ||||
sauimone
|
r412 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | ||
connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | ||||
sauimone
|
r681 | mLayout.append(QRectF(0,0,0,0)); | ||
sauimone
|
r239 | } | ||
sauimone
|
r126 | } | ||
sauimone
|
r654 | |||
sauimone
|
r263 | // Create floating values | ||
sauimone
|
r366 | for (int category=0; category<mSeries->categoryCount(); category++) { | ||
for (int s=0; s<mSeries->barsetCount(); s++) { | ||||
sauimone
|
r357 | QBarSet *set = mSeries->barsetAt(s); | ||
sauimone
|
r267 | BarValue *value = new BarValue(*set, this); | ||
sauimone
|
r263 | childItems().append(value); | ||
mFloatingValues.append(value); | ||||
sauimone
|
r276 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | ||
sauimone
|
r263 | } | ||
} | ||||
sauimone
|
r126 | } | ||
sauimone
|
r681 | QVector<QRectF> BarChartItem::calculateLayout() | ||
sauimone
|
r671 | { | ||
sauimone
|
r681 | QVector<QRectF> layout; | ||
// Use temporary qreals for accurancy (we might get some compiler warnings... :) | ||||
Michal Klocek
|
r683 | qreal categoryCount = mSeries->categoryCount(); | ||
qreal setCount = mSeries->barsetCount(); | ||||
sauimone
|
r681 | |||
sauimone
|
r682 | qreal width = geometry().width(); | ||
qreal height = geometry().height(); | ||||
Michal Klocek
|
r683 | qreal max = mSeries->max(); | ||
sauimone
|
r681 | |||
// Domain: | ||||
Michal Klocek
|
r683 | if (mDomainMaxY > max) { | ||
max = mDomainMaxY; | ||||
sauimone
|
r681 | } | ||
Michal Klocek
|
r683 | qreal scale = (height/max); | ||
qreal categoryWidth = width/categoryCount; | ||||
sauimone
|
r682 | qreal barWidth = categoryWidth / (setCount+1); | ||
sauimone
|
r681 | |||
int itemIndex(0); | ||||
for (int category=0; category < categoryCount; category++) { | ||||
Michal Klocek
|
r683 | qreal xPos = categoryWidth * category + barWidth/2; | ||
sauimone
|
r682 | qreal yPos = height; | ||
sauimone
|
r681 | for (int set = 0; set < setCount; set++) { | ||
qreal barHeight = mSeries->valueAt(set,category) * scale; | ||||
Bar* bar = mBars.at(itemIndex); | ||||
sauimone
|
r682 | QRectF rect(xPos,yPos-barHeight,barWidth,barHeight); | ||
sauimone
|
r681 | layout.append(rect); | ||
bar->setPen(mSeries->barsetAt(set)->pen()); | ||||
bar->setBrush(mSeries->barsetAt(set)->brush()); | ||||
itemIndex++; | ||||
sauimone
|
r682 | xPos += barWidth; | ||
sauimone
|
r681 | } | ||
} | ||||
// Position floating values | ||||
itemIndex = 0; | ||||
for (int category=0; category < mSeries->categoryCount(); category++) { | ||||
Michal Klocek
|
r683 | qreal xPos = categoryWidth * category + barWidth; | ||
sauimone
|
r682 | qreal yPos = height; | ||
sauimone
|
r681 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
qreal barHeight = mSeries->valueAt(set,category) * scale; | ||||
BarValue* value = mFloatingValues.at(itemIndex); | ||||
QBarSet* barSet = mSeries->barsetAt(set); | ||||
value->resize(100,50); // TODO: proper layout for this. | ||||
value->setPos(xPos, yPos-barHeight/2); | ||||
value->setPen(barSet->floatingValuePen()); | ||||
if (mSeries->valueAt(set,category) != 0) { | ||||
value->setValueString(QString::number(mSeries->valueAt(set,category))); | ||||
} else { | ||||
value->setValueString(QString("")); | ||||
} | ||||
itemIndex++; | ||||
sauimone
|
r682 | xPos += barWidth; | ||
sauimone
|
r681 | } | ||
} | ||||
sauimone
|
r671 | |||
return layout; | ||||
} | ||||
sauimone
|
r681 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) | ||
sauimone
|
r671 | { | ||
Michal Klocek
|
r679 | if (animator()) | ||
sauimone
|
r681 | animator()->updateLayout(this, mLayout, layout); | ||
sauimone
|
r671 | else | ||
setLayout(layout); | ||||
} | ||||
sauimone
|
r681 | void BarChartItem::setLayout(const QVector<QRectF> &layout) | ||
sauimone
|
r671 | { | ||
sauimone
|
r681 | mLayout = layout; | ||
for (int i=0; i<mBars.count(); i++) { | ||||
mBars.at(i)->setRect(layout.at(i)); | ||||
sauimone
|
r671 | } | ||
sauimone
|
r681 | |||
sauimone
|
r671 | update(); | ||
} | ||||
sauimone
|
r666 | |||
Michal Klocek
|
r139 | //handlers | ||
sauimone
|
r666 | void BarChartItem::handleModelChanged(int index) | ||
Michal Klocek
|
r139 | { | ||
Tero Ahola
|
r611 | Q_UNUSED(index) | ||
sauimone
|
r161 | dataChanged(); | ||
Michal Klocek
|
r139 | } | ||
sauimone
|
r666 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | ||
Michal Klocek
|
r139 | { | ||
sauimone
|
r674 | mDomainMinX = minX; | ||
mDomainMaxX = maxX; | ||||
mDomainMinY = minY; | ||||
mDomainMaxY = maxY; | ||||
sauimone
|
r681 | handleLayoutChanged(); | ||
Michal Klocek
|
r139 | } | ||
sauimone
|
r666 | void BarChartItem::handleGeometryChanged(const QRectF& rect) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r679 | m_rect=rect; | ||
sauimone
|
r682 | handleLayoutChanged(); | ||
Michal Klocek
|
r139 | mLayoutSet = true; | ||
setPos(rect.topLeft()); | ||||
} | ||||
sauimone
|
r671 | void BarChartItem::handleLayoutChanged() | ||
{ | ||||
sauimone
|
r681 | QVector<QRectF> layout = calculateLayout(); | ||
sauimone
|
r671 | applyLayout(layout); | ||
update(); | ||||
} | ||||
sauimone
|
r666 | void BarChartItem::showToolTip(QPoint pos, QString tip) | ||
sauimone
|
r288 | { | ||
sauimone
|
r296 | // TODO: cool tooltip instead of default | ||
QToolTip::showText(pos,tip); | ||||
sauimone
|
r288 | } | ||
sauimone
|
r280 | |||
sauimone
|
r666 | #include "moc_barchartitem_p.cpp" | ||
Michal Klocek
|
r139 | |||
sauimone
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||