chartvalueaxisy.cpp
116 lines
| 3.1 KiB
| text/x-c
|
CppLexer
|
r1555 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r1805 | #include "chartvalueaxisy_p.h" | ||
|
r1555 | #include "qabstractaxis.h" | ||
#include "chartpresenter_p.h" | ||||
|
r1805 | #include "qvalueaxis.h" | ||
|
r2106 | #include "chartlayout_p.h" | ||
|
r1555 | #include <QGraphicsLayout> | ||
#include <QFontMetrics> | ||||
|
r1735 | #include <qmath.h> | ||
|
r1555 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2111 | ChartValueAxisY::ChartValueAxisY(QValueAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter), | ||
m_tickCount(0),m_axis(axis) | ||||
|
r1555 | { | ||
} | ||||
|
r1805 | ChartValueAxisY::~ChartValueAxisY() | ||
|
r1555 | { | ||
} | ||||
|
r1805 | QVector<qreal> ChartValueAxisY::calculateLayout() const | ||
|
r1555 | { | ||
|
r2105 | Q_ASSERT(m_tickCount>=2); | ||
|
r1555 | |||
QVector<qreal> points; | ||||
|
r1698 | points.resize(m_tickCount); | ||
|
r1555 | |||
|
r2111 | const QRectF& gridRect = gridGeometry(); | ||
const qreal deltaY = gridRect.height()/(m_tickCount-1); | ||||
|
r1698 | for (int i = 0; i < m_tickCount; ++i) { | ||
|
r2111 | int y = i * -deltaY + gridRect.bottom(); | ||
|
r1561 | points[i] = y; | ||
|
r1555 | } | ||
|
r1561 | |||
|
r1555 | return points; | ||
} | ||||
|
r1805 | void ChartValueAxisY::updateGeometry() | ||
|
r1555 | { | ||
|
r1561 | const QVector<qreal> &layout = ChartAxis::layout(); | ||
|
r2105 | if(layout.isEmpty()) return; | ||
|
r2111 | setLabels(createValueLabels(layout.size())); | ||
VerticalAxis::updateGeometry(); | ||||
|
r1555 | } | ||
|
r1805 | void ChartValueAxisY::handleAxisUpdated() | ||
|
r1698 | { | ||
|
r2111 | if (m_tickCount != m_axis->tickCount()) { | ||
m_tickCount = m_axis->tickCount(); | ||||
|
r2106 | presenter()->layout()->invalidate(); | ||
} | ||||
|
r1698 | ChartAxis::handleAxisUpdated(); | ||
} | ||||
|
r2105 | QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const | ||
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
|
r2111 | QFontMetrics fn(font()); | ||
|
r1965 | QSizeF sh; | ||
QSizeF base = ChartAxis::sizeHint(which, constraint); | ||||
|
r2111 | QStringList ticksList = createValueLabels(m_tickCount); | ||
|
r2105 | qreal width=0; | ||
qreal height=0; | ||||
|
r1965 | |||
switch (which) { | ||||
|
r2093 | case Qt::MinimumSize: { | ||
int count = qMax(ticksList.first().count() , ticksList.last().count()); | ||||
|
r2105 | width=fn.averageCharWidth()*count+labelPadding(); | ||
height=fn.height(); | ||||
height=qMax(height,base.height()); | ||||
width+=base.width(); | ||||
sh = QSizeF(width,height); | ||||
|
r2093 | break; | ||
|
r2105 | } | ||
|
r2093 | case Qt::PreferredSize: | ||
|
r2105 | { | ||
for (int i = 0; i < ticksList.size(); ++i) | ||||
{ | ||||
width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+labelPadding(),width); | ||||
height+=fn.height(); | ||||
|
r1965 | } | ||
|
r2105 | height=qMax(height,base.height()); | ||
width+=base.width(); | ||||
sh = QSizeF(width,height); | ||||
|
r2093 | break; | ||
|
r2105 | } | ||
|
r2093 | default: | ||
|
r1965 | break; | ||
} | ||||
return sh; | ||||
} | ||||
|
r1698 | |||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||