qcategoriesaxis.cpp
297 lines
| 6.6 KiB
| text/x-c
|
CppLexer
|
r1540 | /**************************************************************************** | ||
** | ||||
** 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 "qcategoriesaxis.h" | ||||
#include "qcategoriesaxis_p.h" | ||||
|
r1561 | #include "chartcategoriesaxisx_p.h" | ||
#include "chartcategoriesaxisy_p.h" | ||||
|
r1572 | #include <qmath.h> | ||
|
r1611 | #include <QDebug> | ||
|
r1540 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1612 | QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent): | ||
QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent) | ||||
|
r1540 | { | ||
} | ||||
|
r1612 | QBarCategoriesAxis::~QBarCategoriesAxis() | ||
|
r1540 | { | ||
} | ||||
|
r1612 | QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent) | ||
|
r1540 | { | ||
} | ||||
/*! | ||||
Appends \a categories to axis | ||||
*/ | ||||
|
r1612 | void QBarCategoriesAxis::append(const QStringList &categories) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1605 | if (d->m_categories.isEmpty()) { | ||
|
r1611 | d->m_categories.append(categories); | ||
setRange(categories.first(),categories.last()); | ||||
}else{ | ||||
d->m_categories.append(categories); | ||||
|
r1605 | } | ||
|
r1540 | emit categoriesChanged(); | ||
} | ||||
/*! | ||||
Appends \a category to axis | ||||
*/ | ||||
|
r1612 | void QBarCategoriesAxis::append(const QString &category) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1611 | if (d->m_categories.isEmpty()) { | ||
d->m_categories.append(category); | ||||
setRange(category,category); | ||||
}else{ | ||||
d->m_categories.append(category); | ||||
} | ||||
emit categoriesChanged(); | ||||
|
r1540 | } | ||
/*! | ||||
Removes \a category from axis | ||||
*/ | ||||
|
r1612 | void QBarCategoriesAxis::remove(const QString &category) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1540 | if (d->m_categories.contains(category)) { | ||
d->m_categories.removeAt(d->m_categories.indexOf(category)); | ||||
|
r1611 | setRange(d->m_categories.first(),d->m_categories.last()); | ||
|
r1540 | emit categoriesChanged(); | ||
} | ||||
} | ||||
/*! | ||||
Inserts \a category to axis at \a index | ||||
*/ | ||||
|
r1612 | void QBarCategoriesAxis::insert(int index, const QString &category) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1605 | if (d->m_categories.isEmpty()) { | ||
|
r1611 | d->m_categories.insert(index,category); | ||
setRange(category,category); | ||||
}else{ | ||||
|
r1605 | } | ||
|
r1540 | emit categoriesChanged(); | ||
} | ||||
/*! | ||||
Removes all categories. | ||||
*/ | ||||
|
r1612 | void QBarCategoriesAxis::clear() | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1540 | d->m_categories.clear(); | ||
|
r1611 | setRange(QString::null,QString::null); | ||
|
r1540 | emit categoriesChanged(); | ||
} | ||||
|
r1612 | void QBarCategoriesAxis::setCategories(const QStringList &categories) | ||
|
r1550 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1611 | if(d->m_categories!=categories){ | ||
|
r1550 | d->m_categories = categories; | ||
|
r1611 | setRange(categories.first(),categories.last()); | ||
|
r1550 | emit categoriesChanged(); | ||
|
r1611 | } | ||
|
r1550 | } | ||
|
r1612 | QStringList QBarCategoriesAxis::categories() | ||
|
r1550 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1550 | return d->m_categories; | ||
} | ||||
|
r1540 | /*! | ||
Returns number of categories. | ||||
*/ | ||||
|
r1612 | int QBarCategoriesAxis::count() const | ||
|
r1540 | { | ||
|
r1612 | Q_D(const QBarCategoriesAxis); | ||
|
r1540 | return d->m_categories.count(); | ||
} | ||||
/*! | ||||
Returns category at \a index. Index must be valid. | ||||
*/ | ||||
|
r1612 | QString QBarCategoriesAxis::at(int index) const | ||
|
r1540 | { | ||
|
r1612 | Q_D(const QBarCategoriesAxis); | ||
|
r1540 | return d->m_categories.at(index); | ||
} | ||||
/*! | ||||
|
r1605 | Sets minimum category to \a min. | ||
|
r1540 | */ | ||
|
r1612 | void QBarCategoriesAxis::setMin(const QString& min) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1605 | setRange(min,d->m_maxCategory); | ||
|
r1540 | } | ||
|
r1571 | /*! | ||
Returns minimum category. | ||||
*/ | ||||
|
r1612 | QString QBarCategoriesAxis::min() const | ||
|
r1571 | { | ||
|
r1612 | Q_D(const QBarCategoriesAxis); | ||
|
r1571 | return d->m_minCategory; | ||
} | ||||
|
r1540 | /*! | ||
|
r1605 | Sets maximum category to \a max. | ||
|
r1540 | */ | ||
|
r1612 | void QBarCategoriesAxis::setMax(const QString& max) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1605 | setRange(d->m_minCategory,max); | ||
|
r1540 | } | ||
|
r1571 | /*! | ||
Returns maximum category | ||||
*/ | ||||
|
r1612 | QString QBarCategoriesAxis::max() const | ||
|
r1571 | { | ||
|
r1612 | Q_D(const QBarCategoriesAxis); | ||
|
r1571 | return d->m_maxCategory; | ||
} | ||||
|
r1540 | /*! | ||
Sets range from \a minCategory to \a maxCategory | ||||
*/ | ||||
|
r1612 | void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory) | ||
|
r1540 | { | ||
|
r1612 | Q_D(QBarCategoriesAxis); | ||
|
r1605 | |||
int minIndex = d->m_categories.indexOf(minCategory); | ||||
if (minIndex == -1) { | ||||
return; | ||||
} | ||||
int maxIndex = d->m_categories.indexOf(maxCategory); | ||||
if (maxIndex == -1) { | ||||
return; | ||||
} | ||||
if (maxIndex <= minIndex) { | ||||
// max must be greater than min | ||||
return; | ||||
} | ||||
bool changed = false; | ||||
if (!qFuzzyIsNull(d->m_min - (minIndex))) { | ||||
|
r1611 | d->m_minCategory = minCategory; | ||
|
r1605 | d->m_min = minIndex; | ||
changed = true; | ||||
} | ||||
if (!qFuzzyIsNull(d->m_max - (maxIndex))) { | ||||
d->m_max = maxIndex; | ||||
|
r1611 | d->m_maxCategory = maxCategory; | ||
|
r1605 | changed = true; | ||
} | ||||
if ((changed)) { | ||||
|
r1611 | d->emitRange(); | ||
|
r1605 | emit categoriesChanged(); | ||
} | ||||
|
r1540 | } | ||
/*! | ||||
Returns the type of axis. | ||||
*/ | ||||
|
r1612 | QAbstractAxis::AxisType QBarCategoriesAxis::type() const | ||
|
r1540 | { | ||
return AxisTypeCategories; | ||||
} | ||||
|
r1544 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r1612 | QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q): | ||
|
r1544 | QAbstractAxisPrivate(q) | ||
|
r1541 | { | ||
|
r1544 | |||
|
r1541 | } | ||
|
r1612 | QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate() | ||
|
r1541 | { | ||
|
r1544 | |||
|
r1541 | } | ||
|
r1612 | void QBarCategoriesAxisPrivate::setMin(const QVariant &min) | ||
|
r1540 | { | ||
|
r1605 | setRange(min,m_maxCategory); | ||
|
r1566 | } | ||
|
r1612 | void QBarCategoriesAxisPrivate::setMax(const QVariant &max) | ||
|
r1566 | { | ||
|
r1605 | setRange(m_minCategory,max); | ||
|
r1566 | } | ||
|
r1612 | void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max) | ||
|
r1566 | { | ||
|
r1612 | Q_Q(QBarCategoriesAxis); | ||
|
r1605 | QString value1 = min.toString(); | ||
QString value2 = max.toString(); | ||||
q->setRange(value1,value2); | ||||
|
r1540 | } | ||
|
r1612 | int QBarCategoriesAxisPrivate::ticksCount() const | ||
|
r1540 | { | ||
|
r1544 | return m_categories.count()+1; | ||
|
r1540 | } | ||
|
r1612 | void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count) | ||
|
r1566 | { | ||
m_min = min; | ||||
m_max = max; | ||||
m_ticksCount = count; | ||||
} | ||||
|
r1612 | ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter) | ||
|
r1556 | { | ||
|
r1612 | Q_Q( QBarCategoriesAxis); | ||
|
r1556 | if(m_orientation == Qt::Vertical){ | ||
|
r1561 | return new ChartCategoriesAxisY(q,presenter); | ||
|
r1556 | }else{ | ||
|
r1561 | return new ChartCategoriesAxisX(q,presenter); | ||
|
r1556 | } | ||
} | ||||
|
r1612 | void QBarCategoriesAxisPrivate::emitRange() | ||
|
r1574 | { | ||
|
r1612 | Q_Q( QBarCategoriesAxis); | ||
|
r1611 | if(!q->signalsBlocked()) { | ||
emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false); | ||||
} | ||||
|
r1574 | } | ||
|
r1540 | #include "moc_qcategoriesaxis.cpp" | ||
#include "moc_qcategoriesaxis_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||