abstractbarchartitem.cpp
221 lines
| 6.6 KiB
| text/x-c
|
CppLexer
|
r1674 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r1674 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "abstractbarchartitem_p.h" | ||||
#include "bar_p.h" | ||||
#include "qbarset.h" | ||||
#include "qbarset_p.h" | ||||
#include "qabstractbarseries.h" | ||||
#include "qabstractbarseries_p.h" | ||||
#include "qchart.h" | ||||
#include "chartpresenter_p.h" | ||||
|
r1735 | #include "charttheme_p.h" | ||
|
r2316 | #include "baranimation_p.h" | ||
|
r1674 | #include "chartdataset_p.h" | ||
#include <QPainter> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2273 | AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : | ||
ChartItem(series->d_func(),item), | ||||
|
r1674 | m_animation(0), | ||
m_series(series) | ||||
{ | ||||
|
r1917 | |||
|
r1674 | setFlag(ItemClipsChildrenToShape); | ||
|
r1917 | connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged())); | ||
connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars())); | ||||
|
r1674 | connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool))); | ||
connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); | ||||
connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged())); | ||||
|
r2067 | connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged())); | ||
|
r2316 | setZValue(ChartPresenter::BarSeriesZValue); | ||
|
r1674 | handleDataStructureChanged(); | ||
|
r2305 | handleVisibleChanged(); | ||
handleUpdatedBars(); | ||||
|
r1674 | } | ||
AbstractBarChartItem::~AbstractBarChartItem() | ||||
{ | ||||
} | ||||
void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
Q_UNUSED(painter); | ||||
Q_UNUSED(option); | ||||
Q_UNUSED(widget); | ||||
} | ||||
QRectF AbstractBarChartItem::boundingRect() const | ||||
{ | ||||
return m_rect; | ||||
} | ||||
void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout) | ||||
{ | ||||
|
r2316 | QSizeF size = geometry().size(); | ||
if (geometry().size().isValid()) { | ||||
if (m_animation) { | ||||
if (m_layout.count() == 0 || m_oldSize != size) { | ||||
initializeLayout(); | ||||
m_oldSize = size; | ||||
} | ||||
m_animation->setup(m_layout, layout); | ||||
presenter()->startAnimation(m_animation); | ||||
} else { | ||||
setLayout(layout); | ||||
update(); | ||||
} | ||||
|
r1674 | } | ||
} | ||||
|
r2316 | void AbstractBarChartItem::setAnimation(BarAnimation *animation) | ||
|
r1674 | { | ||
m_animation = animation; | ||||
} | ||||
void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout) | ||||
{ | ||||
if (layout.count() != m_bars.count()) | ||||
return; | ||||
|
r1698 | m_layout = layout; | ||
|
r1674 | |||
|
r2305 | for (int i = 0; i < m_bars.count(); i++) { | ||
|
r1674 | m_bars.at(i)->setRect(layout.at(i)); | ||
|
r2539 | QGraphicsTextItem *label = m_labels.at(i); | ||
|
r2305 | label->setPos(layout.at(i).center() - label->boundingRect().center()); | ||
} | ||||
|
r1674 | } | ||
//handlers | ||||
|
r1698 | void AbstractBarChartItem::handleDomainUpdated() | ||
|
r1674 | { | ||
|
r1698 | m_domainMinX = domain()->minX(); | ||
m_domainMaxX = domain()->maxX(); | ||||
m_domainMinY = domain()->minY(); | ||||
m_domainMaxY = domain()->maxY(); | ||||
|
r1674 | |||
|
r2273 | QRectF rect(QPointF(0,0),domain()->size()); | ||
if(m_rect != rect){ | ||||
prepareGeometryChange(); | ||||
m_rect = rect; | ||||
} | ||||
|
r1674 | handleLayoutChanged(); | ||
} | ||||
void AbstractBarChartItem::handleLayoutChanged() | ||||
{ | ||||
|
r2097 | if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) | ||
return; // rect size zero. | ||||
|
r1674 | QVector<QRectF> layout = calculateLayout(); | ||
applyLayout(layout); | ||||
|
r2311 | handleUpdatedBars(); | ||
|
r1674 | } | ||
void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible) | ||||
{ | ||||
|
r2539 | foreach (QGraphicsTextItem *label, m_labels) | ||
|
r1674 | label->setVisible(visible); | ||
update(); | ||||
} | ||||
void AbstractBarChartItem::handleDataStructureChanged() | ||||
{ | ||||
|
r2097 | foreach (QGraphicsItem *item, childItems()) | ||
|
r1674 | delete item; | ||
m_bars.clear(); | ||||
m_labels.clear(); | ||||
m_layout.clear(); | ||||
// Create new graphic items for bars | ||||
for (int c = 0; c < m_series->d_func()->categoryCount(); c++) { | ||||
for (int s = 0; s < m_series->count(); s++) { | ||||
QBarSet *set = m_series->d_func()->barsetAt(s); | ||||
// Bars | ||||
|
r2097 | Bar *bar = new Bar(set, c, this); | ||
|
r1674 | m_bars.append(bar); | ||
|
r2110 | connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*))); | ||
connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*))); | ||||
connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int))); | ||||
connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool))); | ||||
|
r2316 | // m_layout.append(QRectF(0, 0, 1, 1)); | ||
|
r1674 | |||
// Labels | ||||
|
r2539 | m_labels.append(new QGraphicsTextItem(this)); | ||
|
r1674 | } | ||
} | ||||
|
r2273 | if(themeManager()) themeManager()->updateSeries(m_series); | ||
|
r1674 | handleLayoutChanged(); | ||
|
r2311 | handleVisibleChanged(); | ||
|
r1674 | } | ||
void AbstractBarChartItem::handleVisibleChanged() | ||||
{ | ||||
bool visible = m_series->isVisible(); | ||||
|
r2309 | if (visible) | ||
handleLabelsVisibleChanged(m_series->isLabelsVisible()); | ||||
else | ||||
handleLabelsVisibleChanged(visible); | ||||
foreach (QGraphicsItem *bar, m_bars) | ||||
bar->setVisible(visible); | ||||
|
r2067 | } | ||
void AbstractBarChartItem::handleOpacityChanged() | ||||
{ | ||||
|
r2097 | foreach (QGraphicsItem *item, childItems()) | ||
|
r2067 | item->setOpacity(m_series->opacity()); | ||
|
r1674 | } | ||
|
r1917 | void AbstractBarChartItem::handleUpdatedBars() | ||
{ | ||||
// Handle changes in pen, brush, labels etc. | ||||
int categoryCount = m_series->d_func()->categoryCount(); | ||||
int setCount = m_series->count(); | ||||
int itemIndex(0); | ||||
for (int category = 0; category < categoryCount; category++) { | ||||
for (int set = 0; set < setCount; set++) { | ||||
|
r2104 | QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); | ||
Bar *bar = m_bars.at(itemIndex); | ||||
|
r1917 | bar->setPen(barSet->m_pen); | ||
bar->setBrush(barSet->m_brush); | ||||
bar->update(); | ||||
|
r2539 | QGraphicsTextItem *label = m_labels.at(itemIndex); | ||
label->setHtml(QString("%1").arg(barSet->value(category))); | ||||
|
r1917 | label->setFont(barSet->m_labelFont); | ||
|
r2539 | label->setDefaultTextColor(barSet->m_labelBrush.color()); | ||
|
r1917 | label->update(); | ||
itemIndex++; | ||||
} | ||||
} | ||||
} | ||||
|
r1674 | #include "moc_abstractbarchartitem_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||