##// END OF EJS Templates
Added HTML support for various text items...
Added HTML support for various text items - HTML tags can now be used in chart titles, legends, and labels - '\n' does no longer cause line break in multi-line text items. HTML tag <br/> can be used for line break now. - Setting pen for axis titles and labels is deprecated - When setting brush for titles and labels, only the color is relevant - Logic for identifying label format specifiers was improved, so adding additional information to labels via label format string is now more viable. Task-number: QTRD-1912 Change-Id: I8fa56df56fd656e2a3e427eff4abf3481435eec7 Reviewed-by: Mika Salmela <mika.salmela@digia.com>

File last commit:

r2539:74f3dbde7a75
r2539:74f3dbde7a75
Show More
horizontalaxis.cpp
206 lines | 7.9 KiB | text/x-c | CppLexer
/ src / axis / horizontalaxis.cpp
Michal Klocek
Refactors axis updateGeometry handling...
r2111 /****************************************************************************
**
Miikka Heikkinen
Fixed the copyright year 2012 -> 2013
r2432 ** Copyright (C) 2013 Digia Plc
Michal Klocek
Refactors axis updateGeometry handling...
r2111 ** 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 "horizontalaxis_p.h"
Miikka Heikkinen
Add Polar chart support...
r2483 #include "qabstractaxis_p.h"
Miikka Heikkinen
Added HTML support for various text items...
r2539 #include "chartpresenter_p.h"
Michal Klocek
Updates axis drawing code...
r2133 #include <qmath.h>
Michal Klocek
Refactors axis updateGeometry handling...
r2111 #include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Miikka Heikkinen
Add Polar chart support...
r2483 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
: CartesianChartAxis(axis, item, intervalAxis)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 {
}
HorizontalAxis::~HorizontalAxis()
{
}
void HorizontalAxis::updateGeometry()
{
Miikka Heikkinen
Add Polar chart support...
r2483 const QVector<qreal> &layout = ChartAxisElement::layout();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (layout.isEmpty())
return;
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Michal Klocek
Updates axis drawing code...
r2133 QStringList labelList = labels();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Miikka Heikkinen
Add Polar chart support...
r2483 QList<QGraphicsItem *> lines = gridItems();
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QList<QGraphicsItem *> labels = labelItems();
QList<QGraphicsItem *> shades = shadeItems();
Miikka Heikkinen
Add Polar chart support...
r2483 QList<QGraphicsItem *> arrow = arrowItems();
Miikka Heikkinen
Added HTML support for various text items...
r2539 QGraphicsTextItem *title = titleItem();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Michal Klocek
Updates axis drawing code...
r2133 Q_ASSERT(labels.size() == labelList.size());
Q_ASSERT(layout.size() == labelList.size());
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Jani Honkonen
src folder: another massive victory for coding style police
r2131 const QRectF &axisRect = axisGeometry();
const QRectF &gridRect = gridGeometry();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
//arrow
Miikka Heikkinen
Add Polar chart support...
r2483 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(arrow.at(0));
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Miikka Heikkinen
Add Polar chart support...
r2483 if (axis()->alignment() == Qt::AlignTop)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
Miikka Heikkinen
Add Polar chart support...
r2483 else if (axis()->alignment() == Qt::AlignBottom)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
qreal width = 0;
Michal Klocek
Adds consider title boundary for axis label wrapping
r2148 //title
Miikka Heikkinen
Fix axis geometry when there is no axis title...
r2415 int titlePad = 0;
QRectF titleBoundingRect;
Miikka Heikkinen
Add Polar chart support...
r2483 QString titleText = axis()->titleText();
if (!titleText.isEmpty() && titleItem()->isVisible()) {
Miikka Heikkinen
Added HTML support for various text items...
r2539 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), gridRect.width(), Qt::Horizontal, QRectF()));
Michal Klocek
Adds consider title boundary for axis label wrapping
r2148
Miikka Heikkinen
Fix axis geometry when there is no axis title...
r2415 titlePad = titlePadding();
titleBoundingRect = title->boundingRect();
QPointF center = gridRect.center() - titleBoundingRect.center();
Miikka Heikkinen
Add Polar chart support...
r2483 if (axis()->alignment() == Qt::AlignTop) {
Miikka Heikkinen
Fix axis geometry when there is no axis title...
r2415 title->setPos(center.x(), axisRect.top() + titlePad);
Miikka Heikkinen
Add Polar chart support...
r2483 } else if (axis()->alignment() == Qt::AlignBottom) {
Miikka Heikkinen
Fix axis geometry when there is no axis title...
r2415 title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePad);
Michal Klocek
Adds consider title boundary for axis label wrapping
r2148 }
}
Michal Klocek
Refactors axis updateGeometry handling...
r2111 for (int i = 0; i < layout.size(); ++i) {
Michal Klocek
Updates axis drawing code...
r2133 //items
QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
Miikka Heikkinen
Add Polar chart support...
r2483 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
Miikka Heikkinen
Added HTML support for various text items...
r2539 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
Michal Klocek
Refactors axis updateGeometry handling...
r2111
//grid line
gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
Michal Klocek
Updates axis drawing code...
r2133 //label text wrapping
Miikka Heikkinen
Fix axis sizehints when labels are angled...
r2412 QString text = labelList.at(i);
Miikka Heikkinen
Fix multiline axis label positioning....
r2534 QRectF boundingRect;
Miikka Heikkinen
Fix axis geometry when there is no axis title...
r2415 qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - titleBoundingRect.height() - (titlePad * 2);
Miikka Heikkinen
Added HTML support for various text items...
r2539 labelItem->setHtml(ChartPresenter::truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
size, Qt::Vertical, boundingRect));
Michal Klocek
Updates axis drawing code...
r2133
//label transformation origin point
const QRectF& rect = labelItem->boundingRect();
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QPointF center = rect.center();
labelItem->setTransformOriginPoint(center.x(), center.y());
Miikka Heikkinen
Fix axis sizehints when labels are angled...
r2412 int heightDiff = rect.height() - boundingRect.height();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
//ticks and label position
Miikka Heikkinen
Add Polar chart support...
r2483 if (axis()->alignment() == Qt::AlignTop) {
Miikka Heikkinen
Fix axis sizehints when labels are angled...
r2412 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2) - labelPadding());
Jani Honkonen
src folder: another massive victory for coding style police
r2131 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
Miikka Heikkinen
Add Polar chart support...
r2483 } else if (axis()->alignment() == Qt::AlignBottom) {
Miikka Heikkinen
Fix axis sizehints when labels are angled...
r2412 labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2) + labelPadding());
Jani Honkonen
src folder: another massive victory for coding style police
r2131 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
Miikka Heikkinen
Fix category axis label positioning....
r2446 //label in between
bool forceHide = false;
if (intervalAxis() && (i + 1) != layout.size()) {
qreal leftBound = qMax(layout[i], gridRect.left());
qreal rightBound = qMin(layout[i + 1], gridRect.right());
const qreal delta = rightBound - leftBound;
// Hide label in case visible part of the category at the grid edge is too narrow
if (delta < boundingRect.width()
&& (leftBound == gridRect.left() || rightBound == gridRect.right())) {
forceHide = true;
} else {
labelItem->setPos(leftBound + (delta / 2.0) - center.x(), labelItem->pos().y());
}
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
Miikka Heikkinen
Compensate for rounding errors when determining label visibility...
r2445 //label overlap detection - compensate one pixel for rounding errors
Miikka Heikkinen
Fix category axis label positioning....
r2446 if (labelItem->pos().x() < width || forceHide ||
Miikka Heikkinen
Compensate for rounding errors when determining label visibility...
r2445 labelItem->pos().x() < (axisRect.left() - 1.0) ||
(labelItem->pos().x() + boundingRect.width() - 1.0) > axisRect.right()){
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setVisible(false);
Michal Klocek
Reimplement zoom/scroll hanling for barcategoryaxis Y
r2145 } else {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setVisible(true);
Miikka Heikkinen
Fix axis sizehints when labels are angled...
r2412 width = boundingRect.width() + labelItem->pos().x();
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
//shades
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if ((i + 1) % 2 && i > 1) {
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
Miikka Heikkinen
Prevent drawing shades off-chart...
r2448 qreal leftBound = qMax(layout[i - 1], gridRect.left());
qreal rightBound = qMin(layout[i], gridRect.right());
rectItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, gridRect.height());
if (rectItem->rect().width() <= 0.0)
rectItem->setVisible(false);
else
rectItem->setVisible(true);
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
// check if the grid line and the axis tick should be shown
qreal x = gridItem->line().p1().x();
if (x < gridRect.left() || x > gridRect.right()) {
Jani Honkonen
src folder: another massive victory for coding style police
r2131 gridItem->setVisible(false);
tickItem->setVisible(false);
Miikka Heikkinen
Add Polar chart support...
r2483 } else {
Michal Klocek
Updates axis drawing code...
r2133 gridItem->setVisible(true);
tickItem->setVisible(true);
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
}
//begin/end grid line in case labels between
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (intervalAxis()) {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QGraphicsLineItem *gridLine;
Jani Honkonen
src folder: another massive victory for coding style police
r2131 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
Michal Klocek
Refactors axis updateGeometry handling...
r2111 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
Michal Klocek
Updates axis drawing code...
r2133 gridLine->setVisible(true);
gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
Michal Klocek
Refactors axis updateGeometry handling...
r2111 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
Michal Klocek
Updates axis drawing code...
r2133 gridLine->setVisible(true);
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
}
Michal Klocek
Refactors ChartAxis...
r2138 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(constraint);
Michal Klocek
Fixes layout rounding issue
r2306 QSizeF sh(0,0);
Michal Klocek
Refactors ChartAxis...
r2138
Miikka Heikkinen
Add Polar chart support...
r2483 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
Michal Klocek
Refactors ChartAxis...
r2138 return sh;
switch (which) {
Miikka Heikkinen
Fix multi-line axis titles....
r2530 case Qt::MinimumSize: {
Miikka Heikkinen
Added HTML support for various text items...
r2539 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
Miikka Heikkinen
Fix multiline axis label positioning....
r2534 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
Michal Klocek
Refactors ChartAxis...
r2138 break;
Miikka Heikkinen
Fix multi-line axis titles....
r2530 }
Michal Klocek
Refactors ChartAxis...
r2138 case Qt::MaximumSize:
Miikka Heikkinen
Fix multi-line axis titles....
r2530 case Qt::PreferredSize: {
Miikka Heikkinen
Added HTML support for various text items...
r2539 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
Miikka Heikkinen
Fix multi-line axis titles....
r2530 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2));
Michal Klocek
Refactors ChartAxis...
r2138 break;
Miikka Heikkinen
Fix multi-line axis titles....
r2530 }
Michal Klocek
Refactors ChartAxis...
r2138 default:
break;
}
return sh;
}
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QTCOMMERCIALCHART_END_NAMESPACE