horizontalaxis.cpp
137 lines
| 5.1 KiB
| text/x-c
|
CppLexer
Michal Klocek
|
r2111 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "horizontalaxis_p.h" | ||||
#include "qabstractaxis.h" | ||||
#include <QFontMetrics> | ||||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r2131 | HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) | ||
: ChartAxis(axis, presenter, intervalAxis) | ||||
Michal Klocek
|
r2111 | { | ||
} | ||||
HorizontalAxis::~HorizontalAxis() | ||||
{ | ||||
} | ||||
void HorizontalAxis::updateGeometry() | ||||
{ | ||||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
Jani Honkonen
|
r2131 | if (layout.isEmpty()) | ||
return; | ||||
Michal Klocek
|
r2111 | |||
QStringList ticksList = labels(); | ||||
QList<QGraphicsItem *> lines = lineItems(); | ||||
QList<QGraphicsItem *> labels = labelItems(); | ||||
QList<QGraphicsItem *> shades = shadeItems(); | ||||
QList<QGraphicsItem *> axis = arrowItems(); | ||||
Q_ASSERT(labels.size() == ticksList.size()); | ||||
Q_ASSERT(layout.size() == ticksList.size()); | ||||
Jani Honkonen
|
r2131 | const QRectF &axisRect = axisGeometry(); | ||
const QRectF &gridRect = gridGeometry(); | ||||
Michal Klocek
|
r2111 | |||
//arrow | ||||
Jani Honkonen
|
r2131 | QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(axis.at(0)); | ||
Michal Klocek
|
r2111 | |||
Jani Honkonen
|
r2131 | if (alignment() == Qt::AlignTop) | ||
Michal Klocek
|
r2111 | arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom()); | ||
Jani Honkonen
|
r2131 | else if (alignment() == Qt::AlignBottom) | ||
Michal Klocek
|
r2111 | arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top()); | ||
qreal width = 0; | ||||
for (int i = 0; i < layout.size(); ++i) { | ||||
Jani Honkonen
|
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)); | ||||
Michal Klocek
|
r2111 | |||
//grid line | ||||
gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom()); | ||||
//label text | ||||
labelItem->setText(ticksList.at(i)); | ||||
Jani Honkonen
|
r2131 | const QRectF &rect = labelItem->boundingRect(); | ||
Michal Klocek
|
r2111 | QPointF center = rect.center(); | ||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
//ticks and label position | ||||
Jani Honkonen
|
r2131 | if (alignment() == Qt::AlignTop) { | ||
Michal Klocek
|
r2111 | labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding()); | ||
Jani Honkonen
|
r2131 | tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding()); | ||
} else if (alignment() == Qt::AlignBottom) { | ||||
Michal Klocek
|
r2111 | labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding()); | ||
Jani Honkonen
|
r2131 | tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding()); | ||
Michal Klocek
|
r2111 | } | ||
Jani Honkonen
|
r2131 | if (intervalAxis() && i + 1 != layout.size()) { | ||
const qreal delta = (layout[i + 1] - layout[i]) / 2; | ||||
Michal Klocek
|
r2111 | labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y()); | ||
} | ||||
//overlap detection | ||||
Jani Honkonen
|
r2131 | if (labelItem->pos().x() <= width || | ||
Michal Klocek
|
r2111 | labelItem->pos().x() < axisRect.left() || | ||
labelItem->pos().x() + rect.width() > axisRect.right()) { | ||||
labelItem->setVisible(false); | ||||
gridItem->setVisible(false); | ||||
tickItem->setVisible(false); | ||||
Jani Honkonen
|
r2131 | } else { | ||
Michal Klocek
|
r2111 | labelItem->setVisible(true); | ||
gridItem->setVisible(true); | ||||
tickItem->setVisible(true); | ||||
Jani Honkonen
|
r2131 | width = rect.width() + labelItem->pos().x(); | ||
Michal Klocek
|
r2111 | } | ||
//shades | ||||
Jani Honkonen
|
r2131 | if ((i + 1) % 2 && i > 1) { | ||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); | ||||
rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height()); | ||||
Michal Klocek
|
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
|
r2131 | gridItem->setVisible(false); | ||
tickItem->setVisible(false); | ||||
if (intervalAxis() && (labelItem->pos().x() < gridRect.left() || labelItem->pos().x() + rect.width() > gridRect.right())) | ||||
labelItem->setVisible(false); | ||||
Michal Klocek
|
r2111 | } | ||
} | ||||
//begin/end grid line in case labels between | ||||
Jani Honkonen
|
r2131 | if (intervalAxis()) { | ||
Michal Klocek
|
r2111 | QGraphicsLineItem *gridLine; | ||
Jani Honkonen
|
r2131 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); | ||
Michal Klocek
|
r2111 | gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom()); | ||
Jani Honkonen
|
r2131 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size() + 1)); | ||
Michal Klocek
|
r2111 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom()); | ||
} | ||||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||