##// END OF EJS Templates
Fixed bar label visibility...
Fixed bar label visibility Label for bars with zero value is not shown. This is done to avoid overlapping labels. Change-Id: Ife30c04e8ae4d9ac9df1209967cc3cd3ecb76a4f Task-number: QTRD-3695 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@theqtcompany.com>

File last commit:

r2814:79dc9c599f41
r2814:79dc9c599f41
Show More
abstractbarchartitem.cpp
361 lines | 12.2 KiB | text/x-c | CppLexer
/ src / charts / barchart / abstractbarchartitem.cpp
sauimone
refactoring internal barchart items
r1674 /****************************************************************************
**
Titta Heikkala
Copyright header changes...
r2776 ** Copyright (C) 2015 The Qt Company Ltd
sauimone
refactoring internal barchart items
r1674 ** All rights reserved.
Titta Heikkala
Copyright header changes...
r2776 ** For any questions to The Qt Company, please use contact form at http://qt.io
sauimone
refactoring internal barchart items
r1674 **
Titta Heikkala
Updated license headers...
r2740 ** This file is part of the Qt Charts module.
sauimone
refactoring internal barchart items
r1674 **
Titta Heikkala
Updated license headers...
r2740 ** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
Titta Heikkala
Copyright header changes...
r2776 ** agreement between you and The Qt Company.
sauimone
refactoring internal barchart items
r1674 **
** If you have questions regarding the use of this file, please use
Titta Heikkala
Updated license headers...
r2740 ** contact form at http://qt.io
sauimone
refactoring internal barchart items
r1674 **
****************************************************************************/
Titta Heikkala
Fix include syntax...
r2714 #include <private/abstractbarchartitem_p.h>
#include <private/bar_p.h>
#include <QtCharts/QBarSet>
#include <private/qbarset_p.h>
#include <QtCharts/QAbstractBarSeries>
#include <private/qabstractbarseries_p.h>
#include <QtCharts/QChart>
#include <private/chartpresenter_p.h>
#include <private/charttheme_p.h>
#include <private/baranimation_p.h>
#include <private/chartdataset_p.h>
#include <QtGui/QPainter>
#include <QtGui/QTextDocument>
sauimone
refactoring internal barchart items
r1674
Titta Heikkala
Qt Charts project file structure change...
r2712 QT_CHARTS_BEGIN_NAMESPACE
sauimone
refactoring internal barchart items
r1674
Michal Klocek
Refactors internals...
r2273 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
ChartItem(series->d_func(),item),
sauimone
refactoring internal barchart items
r1674 m_animation(0),
m_series(series)
{
sauimone
fix: changing barset colors no more triggers layout calculations
r1917
sauimone
refactoring internal barchart items
r1674 setFlag(ItemClipsChildrenToShape);
Titta Heikkala
Add pressed, released and doubleClicked signals...
r2739 setFlag(QGraphicsItem::ItemIsSelectable);
sauimone
fix: changing barset colors no more triggers layout calculations
r1917 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
sauimone
refactoring internal barchart items
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()));
Tero Ahola
Added opacity property to QAbstractSeries
r2067 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
Titta Heikkala
Added possibility to show series value...
r2689 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(handleUpdatedBars()));
connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
this, SLOT(handleLabelsPositionChanged()));
Miikka Heikkinen
Added support for bar series value label angle...
r2802 connect(series, SIGNAL(labelsAngleChanged(qreal)), this, SLOT(positionLabels()));
Marek Rosa
Bar animations refactored
r2316 setZValue(ChartPresenter::BarSeriesZValue);
sauimone
refactoring internal barchart items
r1674 handleDataStructureChanged();
Marek Rosa
BarChartItems code cleanup
r2305 handleVisibleChanged();
handleUpdatedBars();
sauimone
refactoring internal barchart items
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)
{
Marek Rosa
Bar animations refactored
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();
}
sauimone
refactoring internal barchart items
r1674 }
}
Marek Rosa
Bar animations refactored
r2316 void AbstractBarChartItem::setAnimation(BarAnimation *animation)
sauimone
refactoring internal barchart items
r1674 {
m_animation = animation;
}
void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
{
if (layout.count() != m_bars.count())
return;
Michal Klocek
Refactors Domain and Axis...
r1698 m_layout = layout;
sauimone
refactoring internal barchart items
r1674
Titta Heikkala
Added possibility to show series value...
r2689 for (int i = 0; i < m_bars.count(); i++)
sauimone
refactoring internal barchart items
r1674 m_bars.at(i)->setRect(layout.at(i));
Marek Rosa
BarChartItems code cleanup
r2305
Titta Heikkala
Added possibility to show series value...
r2689 positionLabels();
sauimone
refactoring internal barchart items
r1674 }
//handlers
Michal Klocek
Refactors Domain and Axis...
r1698 void AbstractBarChartItem::handleDomainUpdated()
sauimone
refactoring internal barchart items
r1674 {
Michal Klocek
Refactors Domain and Axis...
r1698 m_domainMinX = domain()->minX();
m_domainMaxX = domain()->maxX();
m_domainMinY = domain()->minY();
m_domainMaxY = domain()->maxY();
sauimone
refactoring internal barchart items
r1674
Michal Klocek
Refactors internals...
r2273 QRectF rect(QPointF(0,0),domain()->size());
if(m_rect != rect){
prepareGeometryChange();
m_rect = rect;
}
sauimone
refactoring internal barchart items
r1674 handleLayoutChanged();
}
void AbstractBarChartItem::handleLayoutChanged()
{
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
return; // rect size zero.
sauimone
refactoring internal barchart items
r1674 QVector<QRectF> layout = calculateLayout();
applyLayout(layout);
Marek Rosa
BarChart brush and labels fix
r2311 handleUpdatedBars();
sauimone
refactoring internal barchart items
r1674 }
void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
{
Miikka Heikkinen
Added HTML support for various text items...
r2539 foreach (QGraphicsTextItem *label, m_labels)
sauimone
refactoring internal barchart items
r1674 label->setVisible(visible);
update();
}
void AbstractBarChartItem::handleDataStructureChanged()
{
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 foreach (QGraphicsItem *item, childItems())
sauimone
refactoring internal barchart items
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
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 Bar *bar = new Bar(set, c, this);
sauimone
refactoring internal barchart items
r1674 m_bars.append(bar);
Jani Honkonen
normalize signal/slot signatures
r2110 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
Titta Heikkala
Add hovered signal with index for bar charts...
r2600 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), m_series, SIGNAL(hovered(bool, int, QBarSet*)));
Titta Heikkala
Add pressed, released and doubleClicked signals...
r2739 connect(bar, SIGNAL(pressed(int, QBarSet*)), m_series, SIGNAL(pressed(int, QBarSet*)));
connect(bar, SIGNAL(released(int, QBarSet*)),
m_series, SIGNAL(released(int, QBarSet*)));
connect(bar, SIGNAL(doubleClicked(int, QBarSet*)),
m_series, SIGNAL(doubleClicked(int, QBarSet*)));
Jani Honkonen
normalize signal/slot signatures
r2110 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
Titta Heikkala
Add hovered signal with index for bar charts...
r2600 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), set, SIGNAL(hovered(bool, int)));
Titta Heikkala
Add pressed, released and doubleClicked signals...
r2739 connect(bar, SIGNAL(pressed(int, QBarSet*)), set, SIGNAL(pressed(int)));
connect(bar, SIGNAL(released(int, QBarSet*)), set, SIGNAL(released(int)));
connect(bar, SIGNAL(doubleClicked(int, QBarSet*)), set, SIGNAL(doubleClicked(int)));
Marek Rosa
Bar animations refactored
r2316 // m_layout.append(QRectF(0, 0, 1, 1));
sauimone
refactoring internal barchart items
r1674
// Labels
Miikka Heikkinen
Fix text item margins...
r2592 QGraphicsTextItem *newLabel = new QGraphicsTextItem(this);
newLabel->document()->setDocumentMargin(ChartPresenter::textMargin());
m_labels.append(newLabel);
sauimone
refactoring internal barchart items
r1674 }
}
Michal Klocek
Refactors internals...
r2273 if(themeManager()) themeManager()->updateSeries(m_series);
sauimone
refactoring internal barchart items
r1674 handleLayoutChanged();
Marek Rosa
BarChart brush and labels fix
r2311 handleVisibleChanged();
sauimone
refactoring internal barchart items
r1674 }
void AbstractBarChartItem::handleVisibleChanged()
{
bool visible = m_series->isVisible();
sauimone
barlabel visibility fix
r2309 if (visible)
handleLabelsVisibleChanged(m_series->isLabelsVisible());
else
handleLabelsVisibleChanged(visible);
foreach (QGraphicsItem *bar, m_bars)
bar->setVisible(visible);
Tero Ahola
Added opacity property to QAbstractSeries
r2067 }
void AbstractBarChartItem::handleOpacityChanged()
{
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 foreach (QGraphicsItem *item, childItems())
Tero Ahola
Added opacity property to QAbstractSeries
r2067 item->setOpacity(m_series->opacity());
sauimone
refactoring internal barchart items
r1674 }
sauimone
fix: changing barset colors no more triggers layout calculations
r1917 void AbstractBarChartItem::handleUpdatedBars()
{
Titta Heikkala
Improve theme initialization performance for bar series...
r2598 if (!m_series->d_func()->blockBarUpdate()) {
// Handle changes in pen, brush, labels etc.
int categoryCount = m_series->d_func()->categoryCount();
int setCount = m_series->count();
int itemIndex(0);
Titta Heikkala
Added possibility to show series value...
r2689 static const QString valueTag(QLatin1String("@value"));
Titta Heikkala
Improve theme initialization performance for bar series...
r2598
for (int category = 0; category < categoryCount; category++) {
for (int set = 0; set < setCount; set++) {
QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
Bar *bar = m_bars.at(itemIndex);
bar->setPen(barSet->m_pen);
bar->setBrush(barSet->m_brush);
bar->update();
QGraphicsTextItem *label = m_labels.at(itemIndex);
Titta Heikkala
Added possibility to show series value...
r2689 QString valueLabel;
Miikka Heikkinen
Added QChart::localizeNumbers...
r2707 if (presenter()) { // At startup presenter is not yet set, yet somehow update comes
Titta Heikkala
Fixed bar label visibility...
r2814 if (barSet->value(category) == 0) {
label->setVisible(false);
Miikka Heikkinen
Added QChart::localizeNumbers...
r2707 } else {
Titta Heikkala
Fixed bar label visibility...
r2814 label->setVisible(true);
if (m_series->labelsFormat().isEmpty()) {
valueLabel = presenter()->numberToString(barSet->value(category));
} else {
valueLabel = m_series->labelsFormat();
valueLabel.replace(valueTag,
Miikka Heikkinen
Added QChart::localizeNumbers...
r2707 presenter()->numberToString(barSet->value(category)));
Titta Heikkala
Fixed bar label visibility...
r2814 }
Miikka Heikkinen
Added QChart::localizeNumbers...
r2707 }
Titta Heikkala
Added possibility to show series value...
r2689 }
label->setHtml(valueLabel);
Titta Heikkala
Improve theme initialization performance for bar series...
r2598 label->setFont(barSet->m_labelFont);
label->setDefaultTextColor(barSet->m_labelBrush.color());
label->update();
itemIndex++;
}
sauimone
fix: changing barset colors no more triggers layout calculations
r1917 }
}
}
Titta Heikkala
Added possibility to show series value...
r2689 void AbstractBarChartItem::handleLabelsPositionChanged()
{
positionLabels();
}
void AbstractBarChartItem::positionLabels()
{
Miikka Heikkinen
Added support for bar series value label angle...
r2802 // By default position labels on horizontal bar series
// Vertical bar series overload positionLabels() to call positionLabelsVertical()
QTransform transform;
const qreal angle = m_series->d_func()->labelsAngle();
if (angle != 0.0)
transform.rotate(angle);
Titta Heikkala
Added possibility to show series value...
r2689 for (int i = 0; i < m_layout.count(); i++) {
QGraphicsTextItem *label = m_labels.at(i);
Miikka Heikkinen
Added support for bar series value label angle...
r2802
QRectF labelRect = label->boundingRect();
QPointF center = labelRect.center();
Titta Heikkala
Added possibility to show series value...
r2689 qreal xPos = 0;
Miikka Heikkinen
Added support for bar series value label angle...
r2802 qreal yPos = m_layout.at(i).center().y() - center.y();
int xDiff = 0;
if (angle != 0.0) {
label->setTransformOriginPoint(center.x(), center.y());
label->setRotation(m_series->d_func()->labelsAngle());
qreal oldWidth = labelRect.width();
labelRect = transform.mapRect(labelRect);
xDiff = (labelRect.width() - oldWidth) / 2;
}
Titta Heikkala
Added possibility to show series value...
r2689
Titta Heikkala
Fix bar value label position...
r2697 int offset = m_bars.at(i)->pen().width() / 2 + 2;
Miikka Heikkinen
Added support for bar series value label angle...
r2802
switch (m_series->labelsPosition()) {
case QAbstractBarSeries::LabelsCenter:
xPos = m_layout.at(i).center().x() - center.x();
break;
case QAbstractBarSeries::LabelsInsideEnd:
xPos = m_layout.at(i).right() - labelRect.width() - offset + xDiff;
break;
case QAbstractBarSeries::LabelsInsideBase:
xPos = m_layout.at(i).left() + offset + xDiff;
break;
case QAbstractBarSeries::LabelsOutsideEnd:
xPos = m_layout.at(i).right() + offset + xDiff;
break;
default:
// Invalid position, never comes here
break;
}
label->setPos(xPos, yPos);
label->setZValue(zValue() + 1);
}
}
void AbstractBarChartItem::positionLabelsVertical()
{
QTransform transform;
const qreal angle = m_series->d_func()->labelsAngle();
if (angle != 0.0)
transform.rotate(angle);
for (int i = 0; i < m_layout.count(); i++) {
QGraphicsTextItem *label = m_labels.at(i);
QRectF labelRect = label->boundingRect();
QPointF center = labelRect.center();
qreal xPos = m_layout.at(i).center().x() - center.x();
qreal yPos = 0;
int yDiff = 0;
if (angle != 0.0) {
label->setTransformOriginPoint(center.x(), center.y());
label->setRotation(m_series->d_func()->labelsAngle());
qreal oldHeight = labelRect.height();
labelRect = transform.mapRect(labelRect);
yDiff = (labelRect.height() - oldHeight) / 2;
}
int offset = m_bars.at(i)->pen().width() / 2 + 2;
switch (m_series->labelsPosition()) {
case QAbstractBarSeries::LabelsCenter:
yPos = m_layout.at(i).center().y() - center.y();
break;
case QAbstractBarSeries::LabelsInsideEnd:
yPos = m_layout.at(i).top() + offset + yDiff;
break;
case QAbstractBarSeries::LabelsInsideBase:
yPos = m_layout.at(i).bottom() - labelRect.height() - offset + yDiff;
break;
case QAbstractBarSeries::LabelsOutsideEnd:
yPos = m_layout.at(i).top() - labelRect.height() - offset + yDiff;
break;
default:
// Invalid position, never comes here
break;
}
Titta Heikkala
Added possibility to show series value...
r2689
label->setPos(xPos, yPos);
label->setZValue(zValue() + 1);
}
}
sauimone
refactoring internal barchart items
r1674 #include "moc_abstractbarchartitem_p.cpp"
Titta Heikkala
Qt Charts project file structure change...
r2712 QT_CHARTS_END_NAMESPACE