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