barchartitem.cpp
212 lines
| 6.2 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r666 | #include "barchartitem_p.h" | ||
|
r126 | #include "bar_p.h" | ||
|
r240 | #include "qbarset.h" | ||
|
r962 | #include "qbarset_p.h" | ||
|
r338 | #include "qbarseries.h" | ||
|
r962 | #include "qbarseries_p.h" | ||
|
r487 | #include "qchart.h" | ||
|
r594 | #include "chartpresenter_p.h" | ||
|
r671 | #include "chartanimator_p.h" | ||
|
r679 | #include "chartdataset_p.h" | ||
|
r839 | #include <QPainter> | ||
|
r126 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r677 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : | ||
ChartItem(presenter), | ||||
|
r763 | m_series(series) | ||
|
r126 | { | ||
|
r852 | setFlag(ItemClipsChildrenToShape); | ||
|
r962 | connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); | ||
|
r1339 | connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool))); | ||
connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); | ||||
|
r1342 | connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged())); | ||
|
r594 | setZValue(ChartPresenter::BarSeriesZValue); | ||
|
r1339 | handleDataStructureChanged(); | ||
|
r126 | } | ||
|
r666 | BarChartItem::~BarChartItem() | ||
|
r126 | { | ||
} | ||||
|
r666 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
|
r126 | { | ||
|
r852 | Q_UNUSED(painter); | ||
Q_UNUSED(option); | ||||
Q_UNUSED(widget); | ||||
|
r126 | } | ||
|
r666 | QRectF BarChartItem::boundingRect() const | ||
|
r126 | { | ||
|
r976 | return m_rect; | ||
|
r126 | } | ||
|
r165 | |||
|
r1339 | void BarChartItem::handleDataStructureChanged() | ||
|
r126 | { | ||
|
r852 | foreach(QGraphicsItem *item, childItems()) { | ||
delete item; | ||||
} | ||||
|
r126 | |||
|
r763 | m_bars.clear(); | ||
|
r820 | m_labels.clear(); | ||
|
r763 | m_layout.clear(); | ||
|
r256 | |||
|
r1246 | bool labelsVisible = m_series->isLabelsVisible(); | ||
|
r126 | // Create new graphic items for bars | ||
|
r1321 | for (int c = 0; c < m_series->d_func()->categoryCount(); c++) { | ||
|
r763 | for (int s = 0; s < m_series->barsetCount(); s++) { | ||
|
r962 | QBarSet *set = m_series->d_func()->barsetAt(s); | ||
|
r1246 | |||
// Bars | ||||
|
r1321 | Bar *bar = new Bar(set,c,this); | ||
|
r763 | m_bars.append(bar); | ||
|
r1321 | connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int))); | ||
|
r975 | connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool))); | ||
|
r763 | m_layout.append(QRectF(0, 0, 0, 0)); | ||
|
r654 | |||
|
r1246 | // Labels | ||
QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this); | ||||
label->setVisible(labelsVisible); | ||||
m_labels.append(label); | ||||
|
r263 | } | ||
|
r1246 | } | ||
|
r1063 | |||
// TODO: Is this the right place to call it? | ||||
|
r1356 | presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | ||
|
r1339 | handleLayoutChanged(); | ||
|
r126 | } | ||
|
r810 | |||
|
r681 | QVector<QRectF> BarChartItem::calculateLayout() | ||
|
r671 | { | ||
|
r681 | QVector<QRectF> layout; | ||
|
r1321 | // Use temporary qreals for accuracy | ||
qreal categoryCount = m_series->d_func()->categoryCount(); | ||||
|
r763 | qreal setCount = m_series->barsetCount(); | ||
|
r1284 | bool barsVisible = m_series->isVisible(); | ||
|
r681 | |||
|
r850 | // Domain: | ||
|
r682 | qreal width = geometry().width(); | ||
qreal height = geometry().height(); | ||||
|
r1167 | qreal rangeY = m_domainMaxY - m_domainMinY; | ||
qreal rangeX = m_domainMaxX - m_domainMinX; | ||||
qreal scaleY = (height / rangeY); | ||||
qreal scaleX = (width / rangeX); | ||||
|
r1326 | qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin(); | ||
|
r681 | |||
int itemIndex(0); | ||||
|
r737 | for (int category = 0; category < categoryCount; category++) { | ||
|
r1167 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); | ||
|
r681 | for (int set = 0; set < setCount; set++) { | ||
|
r962 | QBarSet* barSet = m_series->d_func()->barsetAt(set); | ||
|
r1167 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; | ||
qreal barHeight = barSet->at(category).y() * scaleY; | ||||
|
r817 | |||
|
r763 | Bar* bar = m_bars.at(itemIndex); | ||
|
r737 | QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); | ||
|
r1167 | |||
|
r681 | layout.append(rect); | ||
|
r817 | bar->setPen(barSet->pen()); | ||
bar->setBrush(barSet->brush()); | ||||
|
r1284 | bar->setVisible(barsVisible); | ||
|
r681 | |||
|
r1246 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); | ||
|
r681 | |||
|
r1167 | if (!qFuzzyIsNull(barSet->at(category).y())) { | ||
label->setText(QString::number(barSet->at(category).y())); | ||||
|
r681 | } else { | ||
|
r820 | label->setText(QString("")); | ||
|
r681 | } | ||
|
r820 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) | ||
,yPos - barHeight/2 - label->boundingRect().height()/2); | ||||
|
r839 | label->setFont(barSet->labelFont()); | ||
|
r1306 | label->setBrush(barSet->labelBrush()); | ||
|
r811 | |||
|
r681 | itemIndex++; | ||
} | ||||
} | ||||
|
r1167 | |||
|
r671 | return layout; | ||
} | ||||
|
r681 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) | ||
|
r671 | { | ||
|
r1339 | if (animator()) { | ||
|
r763 | animator()->updateLayout(this, m_layout, layout); | ||
|
r1339 | } else { | ||
|
r671 | setLayout(layout); | ||
|
r1339 | } | ||
|
r671 | } | ||
|
r681 | void BarChartItem::setLayout(const QVector<QRectF> &layout) | ||
|
r671 | { | ||
|
r763 | m_layout = layout; | ||
|
r681 | |||
|
r1339 | for (int i=0; i < m_bars.count(); i++) { | ||
|
r763 | m_bars.at(i)->setRect(layout.at(i)); | ||
|
r1339 | } | ||
|
r681 | |||
|
r671 | update(); | ||
} | ||||
|
r139 | //handlers | ||
|
r666 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | ||
|
r139 | { | ||
|
r763 | m_domainMinX = minX; | ||
m_domainMaxX = maxX; | ||||
m_domainMinY = minY; | ||||
m_domainMaxY = maxY; | ||||
|
r681 | handleLayoutChanged(); | ||
|
r139 | } | ||
|
r737 | void BarChartItem::handleGeometryChanged(const QRectF &rect) | ||
|
r139 | { | ||
|
r850 | prepareGeometryChange(); | ||
|
r737 | m_rect = rect; | ||
|
r682 | handleLayoutChanged(); | ||
|
r139 | } | ||
|
r671 | void BarChartItem::handleLayoutChanged() | ||
{ | ||||
|
r850 | if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) { | ||
// rect size zero. | ||||
return; | ||||
} | ||||
|
r681 | QVector<QRectF> layout = calculateLayout(); | ||
|
r671 | applyLayout(layout); | ||
update(); | ||||
} | ||||
|
r1339 | void BarChartItem::handleLabelsVisibleChanged(bool visible) | ||
|
r1246 | { | ||
foreach (QGraphicsSimpleTextItem* label, m_labels) { | ||||
label->setVisible(visible); | ||||
} | ||||
update(); | ||||
} | ||||
|
r666 | #include "moc_barchartitem_p.cpp" | ||
|
r139 | |||
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||