verticalaxis.cpp
230 lines
| 8.2 KiB
| text/x-c
|
CppLexer
|
r2111 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
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 "verticalaxis_p.h" | ||||
#include "qabstractaxis.h" | ||||
#include <QFontMetrics> | ||||
|
r2139 | #include <QDebug> | ||
|
r2111 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2273 | VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item, bool intervalAxis) | ||
: ChartAxis(axis, item, intervalAxis) | ||||
|
r2111 | { | ||
} | ||||
VerticalAxis::~VerticalAxis() | ||||
{ | ||||
} | ||||
void VerticalAxis::updateGeometry() | ||||
{ | ||||
const QVector<qreal> &layout = ChartAxis::layout(); | ||||
|
r2131 | if (layout.isEmpty()) | ||
return; | ||||
|
r2111 | |||
QStringList labelList = labels(); | ||||
QList<QGraphicsItem *> lines = lineItems(); | ||||
QList<QGraphicsItem *> labels = labelItems(); | ||||
QList<QGraphicsItem *> shades = shadeItems(); | ||||
QList<QGraphicsItem *> axis = arrowItems(); | ||||
|
r2139 | QGraphicsSimpleTextItem* title = titleItem(); | ||
|
r2111 | |||
Q_ASSERT(labels.size() == labelList.size()); | ||||
Q_ASSERT(layout.size() == labelList.size()); | ||||
|
r2131 | const QRectF &axisRect = axisGeometry(); | ||
const QRectF &gridRect = gridGeometry(); | ||||
|
r2111 | |||
qreal height = axisRect.bottom(); | ||||
|
r2133 | //arrow | ||
QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | ||||
//arrow position | ||||
if (alignment()==Qt::AlignLeft) | ||||
arrowItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom()); | ||||
else if(alignment()==Qt::AlignRight) | ||||
arrowItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom()); | ||||
|
r2111 | |||
QFontMetrics fn(font()); | ||||
|
r2148 | //title | ||
|
r2415 | int titlePad = 0; | ||
QRectF titleBoundingRect; | ||||
if (!titleText().isEmpty() && titleItem()->isVisible()) { | ||||
|
r2148 | QFontMetrics fn(title->font()); | ||
int size(0); | ||||
size = gridRect.height(); | ||||
QString titleText = this->titleText(); | ||||
if (fn.boundingRect(titleText).width() > size) { | ||||
QString string = titleText + "..."; | ||||
while (fn.boundingRect(string).width() > size && string.length() > 3) | ||||
string.remove(string.length() - 4, 1); | ||||
title->setText(string); | ||||
} | ||||
else { | ||||
title->setText(titleText); | ||||
} | ||||
|
r2415 | titlePad = titlePadding(); | ||
titleBoundingRect = title->boundingRect(); | ||||
QPointF center = gridRect.center() - titleBoundingRect.center(); | ||||
|
r2148 | if (alignment() == Qt::AlignLeft) { | ||
|
r2415 | title->setPos(axisRect.left() - titleBoundingRect.width() / 2 + titleBoundingRect.height() / 2 + titlePad, center.y()); | ||
|
r2148 | } | ||
else if (alignment() == Qt::AlignRight) { | ||||
|
r2415 | title->setPos(axisRect.right() - titleBoundingRect.width() / 2 - titleBoundingRect.height() / 2 - titlePad, center.y()); | ||
|
r2148 | } | ||
|
r2415 | title->setTransformOriginPoint(titleBoundingRect.center()); | ||
|
r2148 | title->setRotation(270); | ||
} | ||||
|
r2111 | for (int i = 0; i < layout.size(); ++i) { | ||
|
r2133 | //items | ||
|
r2131 | QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i)); | ||
QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1)); | ||||
QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i)); | ||||
|
r2111 | |||
//grid line | ||||
|
r2131 | gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]); | ||
|
r2111 | |||
|
r2133 | //label text wrapping | ||
|
r2111 | QString text = labelList.at(i); | ||
|
r2412 | QRectF boundingRect = labelBoundingRect(fn, text); | ||
|
r2415 | qreal size = axisRect.right() - axisRect.left() - labelPadding() - titleBoundingRect.height() - (titlePad * 2); | ||
|
r2412 | if (boundingRect.width() > size) { | ||
|
r2111 | QString label = text + "..."; | ||
|
r2412 | while (boundingRect.width() > size && label.length() > 3) { | ||
|
r2131 | label.remove(label.length() - 4, 1); | ||
|
r2412 | boundingRect = labelBoundingRect(fn, label); | ||
} | ||||
|
r2111 | labelItem->setText(label); | ||
|
r2131 | } else { | ||
|
r2111 | labelItem->setText(text); | ||
} | ||||
|
r2412 | |||
|
r2133 | //label transformation origin point | ||
|
r2131 | const QRectF &rect = labelItem->boundingRect(); | ||
|
r2133 | |||
|
r2111 | QPointF center = rect.center(); | ||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
|
r2412 | int widthDiff = rect.width() - boundingRect.width(); | ||
|
r2111 | |||
//ticks and label position | ||||
|
r2131 | if (alignment() == Qt::AlignLeft) { | ||
|
r2412 | labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2) - labelPadding(), layout[i] - center.y()); | ||
|
r2131 | tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]); | ||
} else if (alignment() == Qt::AlignRight) { | ||||
|
r2412 | labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2), layout[i] - center.y()); | ||
|
r2131 | tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]); | ||
|
r2111 | } | ||
|
r2133 | |||
|
r2446 | //label in between | ||
bool forceHide = false; | ||||
if (intervalAxis() && (i + 1) != layout.size()) { | ||||
qreal lowerBound = qMin(layout[i], gridRect.bottom()); | ||||
qreal upperBound = qMax(layout[i + 1], gridRect.top()); | ||||
const qreal delta = lowerBound - upperBound; | ||||
// Hide label in case visible part of the category at the grid edge is too narrow | ||||
if (delta < boundingRect.height() | ||||
&& (lowerBound == gridRect.bottom() || upperBound == gridRect.top())) { | ||||
forceHide = true; | ||||
} else { | ||||
labelItem->setPos(labelItem->pos().x() , lowerBound - (delta / 2.0) - center.y()); | ||||
} | ||||
|
r2111 | } | ||
|
r2445 | //label overlap detection - compensate one pixel for rounding errors | ||
|
r2446 | if (labelItem->pos().y() + boundingRect.height() > height || forceHide || | ||
|
r2445 | (labelItem->pos().y() + (boundingRect.height() / 2.0) - 1.0) > axisRect.bottom() || | ||
labelItem->pos().y() + (boundingRect.height() / 2.0) < (axisRect.top() - 1.0)) { | ||||
|
r2111 | labelItem->setVisible(false); | ||
|
r2133 | } | ||
else { | ||||
|
r2111 | labelItem->setVisible(true); | ||
|
r2133 | height=labelItem->pos().y(); | ||
|
r2111 | } | ||
//shades | ||||
|
r2131 | if ((i + 1) % 2 && i > 1) { | ||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); | ||||
rectItem->setRect(gridRect.left(), layout[i], gridRect.width(), layout[i - 1] - layout[i]); | ||||
|
r2111 | } | ||
// check if the grid line and the axis tick should be shown | ||||
qreal y = gridItem->line().p1().y(); | ||||
|
r2133 | if ((y < gridRect.top() || y > gridRect.bottom())) | ||
{ | ||||
|
r2111 | gridItem->setVisible(false); | ||
tickItem->setVisible(false); | ||||
|
r2133 | }else{ | ||
gridItem->setVisible(true); | ||||
tickItem->setVisible(true); | ||||
|
r2111 | } | ||
} | ||||
//begin/end grid line in case labels between | ||||
|
r2131 | if (intervalAxis()) { | ||
|
r2111 | QGraphicsLineItem *gridLine; | ||
|
r2131 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); | ||
|
r2111 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top()); | ||
|
r2133 | gridLine->setVisible(true); | ||
gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); | ||||
|
r2111 | gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom()); | ||
|
r2133 | gridLine->setVisible(true); | ||
|
r2111 | } | ||
} | ||||
|
r2138 | QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
{ | ||||
Q_UNUSED(constraint); | ||||
|
r2139 | QFontMetrics fn(titleFont()); | ||
|
r2306 | QSizeF sh(0,0); | ||
|
r2138 | |||
|
r2415 | if (titleText().isEmpty() || !titleItem()->isVisible()) | ||
|
r2138 | return sh; | ||
switch (which) { | ||||
case Qt::MinimumSize: | ||||
|
r2413 | sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect("...").width()); | ||
|
r2138 | break; | ||
case Qt::MaximumSize: | ||||
case Qt::PreferredSize: | ||||
|
r2413 | sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect(axis()->titleText()).width()); | ||
|
r2138 | break; | ||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
|
r2111 | QTCOMMERCIALCHART_END_NAMESPACE | ||