chartaxis.cpp
381 lines
| 9.6 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r1006 | #include "chartaxis_p.h" | ||
|
r1541 | #include "qabstractaxis.h" | ||
#include "qabstractaxis_p.h" | ||||
|
r1032 | #include "qaxiscategories_p.h" | ||
|
r262 | #include "chartpresenter_p.h" | ||
|
r530 | #include "chartanimator_p.h" | ||
|
r67 | #include <QPainter> | ||
|
r996 | #include <QDebug> | ||
|
r678 | #include <cmath> | ||
|
r67 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1541 | ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : Chart(presenter), | ||
|
r745 | m_chartAxis(axis), | ||
m_labelsAngle(0), | ||||
|
r784 | m_grid(new QGraphicsItemGroup(presenter->rootItem())), | ||
m_shades(new QGraphicsItemGroup(presenter->rootItem())), | ||||
m_labels(new QGraphicsItemGroup(presenter->rootItem())), | ||||
m_axis(new QGraphicsItemGroup(presenter->rootItem())), | ||||
|
r745 | m_min(0), | ||
m_max(0), | ||||
|
r1241 | m_ticksCount(0), | ||
|
r1534 | m_animation(0), | ||
m_minWidth(0), | ||||
m_minHeight(0) | ||||
|
r67 | { | ||
|
r145 | //initial initialization | ||
|
r784 | m_axis->setZValue(ChartPresenter::AxisZValue); | ||
m_axis->setHandlesChildEvents(false); | ||||
|
r677 | |||
|
r784 | m_shades->setZValue(ChartPresenter::ShadesZValue); | ||
m_grid->setZValue(ChartPresenter::GridZValue); | ||||
|
r439 | |||
|
r963 | QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated())); | ||
|
r502 | |||
|
r1534 | QGraphicsSimpleTextItem item; | ||
m_font = item.font(); | ||||
|
r502 | handleAxisUpdated(); | ||
|
r67 | } | ||
|
r1006 | ChartAxis::~ChartAxis() | ||
|
r67 | { | ||
} | ||||
|
r1241 | void ChartAxis::setAnimation(AxisAnimation* animation) | ||
|
r176 | { | ||
|
r1241 | m_animation=animation; | ||
} | ||||
|
r530 | |||
|
r1241 | void ChartAxis::setLayout(QVector<qreal> &layout) | ||
{ | ||||
m_layoutVector=layout; | ||||
} | ||||
void ChartAxis::createItems(int count) | ||||
{ | ||||
|
r784 | if (m_axis->children().size() == 0) | ||
|
r1241 | m_axis->addToGroup(new AxisItem(this)); | ||
|
r223 | for (int i = 0; i < count; ++i) { | ||
|
r784 | m_grid->addToGroup(new QGraphicsLineItem()); | ||
m_labels->addToGroup(new QGraphicsSimpleTextItem()); | ||||
m_axis->addToGroup(new QGraphicsLineItem()); | ||||
if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem()); | ||||
|
r1241 | } | ||
|
r176 | } | ||
|
r1006 | void ChartAxis::deleteItems(int count) | ||
|
r176 | { | ||
|
r784 | QList<QGraphicsItem *> lines = m_grid->childItems(); | ||
QList<QGraphicsItem *> labels = m_labels->childItems(); | ||||
QList<QGraphicsItem *> shades = m_shades->childItems(); | ||||
QList<QGraphicsItem *> axis = m_axis->childItems(); | ||||
|
r176 | |||
|
r291 | for (int i = 0; i < count; ++i) { | ||
|
r745 | if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast()); | ||
|
r291 | delete(lines.takeLast()); | ||
delete(labels.takeLast()); | ||||
delete(axis.takeLast()); | ||||
|
r272 | } | ||
|
r176 | } | ||
|
r1006 | void ChartAxis::updateLayout(QVector<qreal> &layout) | ||
|
r176 | { | ||
|
r1241 | int diff = m_layoutVector.size() - layout.size(); | ||
if (diff>0) { | ||||
deleteItems(diff); | ||||
} | ||||
else if (diff<0) { | ||||
createItems(-diff); | ||||
} | ||||
if( diff!=0) handleAxisUpdated(); | ||||
if (m_animation) { | ||||
switch(presenter()->state()){ | ||||
case ChartPresenter::ZoomInState: | ||||
m_animation->setAnimationType(AxisAnimation::ZoomInAnimation); | ||||
m_animation->setAnimationPoint(presenter()->statePoint()); | ||||
break; | ||||
case ChartPresenter::ZoomOutState: | ||||
m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation); | ||||
m_animation->setAnimationPoint(presenter()->statePoint()); | ||||
break; | ||||
case ChartPresenter::ScrollUpState: | ||||
case ChartPresenter::ScrollLeftState: | ||||
m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation); | ||||
break; | ||||
case ChartPresenter::ScrollDownState: | ||||
case ChartPresenter::ScrollRightState: | ||||
m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation); | ||||
break; | ||||
case ChartPresenter::ShowState: | ||||
m_animation->setAnimationType(AxisAnimation::DefaultAnimation); | ||||
break; | ||||
} | ||||
m_animation->setValues(m_layoutVector,layout); | ||||
presenter()->startAnimation(m_animation); | ||||
} | ||||
else { | ||||
|
r745 | setLayout(layout); | ||
|
r1241 | updateGeometry(); | ||
|
r530 | } | ||
|
r85 | } | ||
|
r176 | |||
|
r1006 | bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const | ||
|
r85 | { | ||
|
r1241 | Q_ASSERT(max>min); | ||
|
r678 | Q_ASSERT(ticks>1); | ||
|
r85 | |||
|
r1541 | //TODO:: QAxisCategories* categories = m_chartAxis->categories(); | ||
QAxisCategories* categories = new QAxisCategories(); | ||||
|
r291 | |||
|
r701 | bool category = categories->count()>0; | ||
|
r745 | if (!category) { | ||
|
r701 | int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); | ||
|
r801 | n++; | ||
|
r759 | for (int i=0; i< ticks; i++) { | ||
|
r701 | qreal value = min + (i * (max - min)/ (ticks-1)); | ||
|
r1241 | Q_UNUSED(value); | ||
|
r678 | labels << QString::number(value,'f',n); | ||
|
r439 | } | ||
|
r745 | } else { | ||
|
r706 | QList<qreal> values = categories->values(); | ||
|
r745 | for (int i=0; i< ticks; i++) { | ||
|
r706 | qreal value = (min + (i * (max - min)/ (ticks-1))); | ||
int j=0; | ||||
|
r745 | for (; j<values.count(); j++) { | ||
|
r706 | if (values.at(j) > value) break; | ||
} | ||||
|
r745 | if (j!=0) value=values.at(j-1); | ||
|
r706 | |||
|
r497 | QString label = categories->label(value); | ||
|
r439 | labels << label; | ||
} | ||||
|
r223 | } | ||
|
r701 | |||
return category; | ||||
|
r176 | } | ||
|
r85 | |||
|
r1006 | void ChartAxis::setAxisOpacity(qreal opacity) | ||
|
r184 | { | ||
|
r784 | m_axis->setOpacity(opacity); | ||
|
r184 | } | ||
|
r1006 | qreal ChartAxis::axisOpacity() const | ||
|
r184 | { | ||
|
r784 | return m_axis->opacity(); | ||
|
r184 | } | ||
|
r1006 | void ChartAxis::setGridOpacity(qreal opacity) | ||
|
r176 | { | ||
|
r784 | m_grid->setOpacity(opacity); | ||
|
r176 | } | ||
|
r85 | |||
|
r1006 | qreal ChartAxis::gridOpacity() const | ||
|
r176 | { | ||
|
r784 | return m_grid->opacity(); | ||
|
r176 | } | ||
|
r85 | |||
|
r1006 | void ChartAxis::setLabelsOpacity(qreal opacity) | ||
|
r176 | { | ||
|
r784 | m_labels->setOpacity(opacity); | ||
|
r176 | } | ||
|
r85 | |||
|
r1006 | qreal ChartAxis::labelsOpacity() const | ||
|
r176 | { | ||
|
r784 | return m_labels->opacity(); | ||
|
r176 | } | ||
|
r85 | |||
|
r1006 | void ChartAxis::setShadesOpacity(qreal opacity) | ||
|
r176 | { | ||
|
r784 | m_shades->setOpacity(opacity); | ||
|
r176 | } | ||
|
r85 | |||
|
r1006 | qreal ChartAxis::shadesOpacity() const | ||
|
r176 | { | ||
|
r784 | return m_shades->opacity(); | ||
|
r176 | } | ||
|
r67 | |||
|
r1006 | void ChartAxis::setLabelsAngle(int angle) | ||
|
r176 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_labels->childItems()) { | ||
|
r176 | item->setRotation(angle); | ||
|
r145 | } | ||
|
r176 | |||
m_labelsAngle=angle; | ||||
|
r67 | } | ||
|
r1006 | void ChartAxis::setLabelsPen(const QPen &pen) | ||
|
r140 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_labels->childItems()) { | ||
|
r176 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); | ||
} | ||||
|
r140 | } | ||
|
r1006 | void ChartAxis::setLabelsBrush(const QBrush &brush) | ||
|
r140 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_labels->childItems()) { | ||
|
r176 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); | ||
} | ||||
|
r140 | } | ||
|
r1006 | void ChartAxis::setLabelsFont(const QFont &font) | ||
|
r140 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_labels->childItems()) { | ||
|
r176 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); | ||
} | ||||
|
r1534 | m_font = font; | ||
|
r140 | } | ||
|
r1006 | void ChartAxis::setShadesBrush(const QBrush &brush) | ||
|
r140 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_shades->childItems()) { | ||
|
r176 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); | ||
} | ||||
|
r140 | } | ||
|
r1006 | void ChartAxis::setShadesPen(const QPen &pen) | ||
|
r140 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_shades->childItems()) { | ||
|
r176 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); | ||
} | ||||
} | ||||
|
r1006 | void ChartAxis::setAxisPen(const QPen &pen) | ||
|
r184 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_axis->childItems()) { | ||
|
r272 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | ||
} | ||||
|
r184 | } | ||
|
r1006 | void ChartAxis::setGridPen(const QPen &pen) | ||
|
r176 | { | ||
|
r784 | foreach(QGraphicsItem* item , m_grid->childItems()) { | ||
|
r176 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | ||
} | ||||
} | ||||
|
r1006 | bool ChartAxis::isEmpty() | ||
|
r502 | { | ||
|
r768 | return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0; | ||
|
r291 | } | ||
|
r140 | |||
|
r439 | //handlers | ||
|
r1006 | void ChartAxis::handleAxisCategoriesUpdated() | ||
|
r497 | { | ||
|
r759 | if (isEmpty()) return; | ||
|
r502 | updateLayout(m_layoutVector); | ||
|
r497 | } | ||
|
r1006 | void ChartAxis::handleAxisUpdated() | ||
|
r439 | { | ||
|
r497 | |||
|
r745 | if (isEmpty()) return; | ||
|
r439 | |||
|
r745 | if (m_chartAxis->isAxisVisible()) { | ||
|
r439 | setAxisOpacity(100); | ||
|
r745 | } else { | ||
|
r439 | setAxisOpacity(0); | ||
} | ||||
|
r759 | if (m_chartAxis->isGridLineVisible()) { | ||
|
r439 | setGridOpacity(100); | ||
|
r745 | } else { | ||
|
r439 | setGridOpacity(0); | ||
} | ||||
|
r759 | if (m_chartAxis->labelsVisible()) { | ||
|
r439 | setLabelsOpacity(100); | ||
|
r745 | } else { | ||
|
r439 | setLabelsOpacity(0); | ||
} | ||||
|
r745 | if (m_chartAxis->shadesVisible()) { | ||
|
r1449 | setShadesOpacity(100); | ||
|
r745 | } else { | ||
|
r439 | setShadesOpacity(0); | ||
} | ||||
setLabelsAngle(m_chartAxis->labelsAngle()); | ||||
setAxisPen(m_chartAxis->axisPen()); | ||||
setLabelsPen(m_chartAxis->labelsPen()); | ||||
setLabelsBrush(m_chartAxis->labelsBrush()); | ||||
setLabelsFont(m_chartAxis->labelsFont()); | ||||
|
r535 | setGridPen(m_chartAxis->gridLinePen()); | ||
|
r439 | setShadesPen(m_chartAxis->shadesPen()); | ||
setShadesBrush(m_chartAxis->shadesBrush()); | ||||
|
r452 | |||
|
r439 | } | ||
|
r1006 | void ChartAxis::handleRangeChanged(qreal min, qreal max,int tickCount) | ||
|
r439 | { | ||
|
r768 | if (qFuzzyIsNull(min - max) || tickCount < 2) | ||
return; | ||||
|
r554 | |||
|
r452 | m_min = min; | ||
m_max = max; | ||||
|
r554 | m_ticksCount= tickCount; | ||
|
r513 | |||
|
r745 | if (isEmpty()) return; | ||
|
r502 | QVector<qreal> layout = calculateLayout(); | ||
updateLayout(layout); | ||||
|
r452 | } | ||
|
r439 | |||
|
r1006 | void ChartAxis::handleGeometryChanged(const QRectF &rect) | ||
|
r439 | { | ||
|
r855 | if(m_rect != rect) | ||
{ | ||||
m_rect = rect; | ||||
if (isEmpty()) return; | ||||
QVector<qreal> layout = calculateLayout(); | ||||
updateLayout(layout); | ||||
} | ||||
|
r452 | } | ||
|
r439 | |||
|
r1534 | |||
qreal ChartAxis::minimumWidth() | ||||
{ | ||||
if(m_minWidth == 0) updateGeometry(); | ||||
return m_minWidth; | ||||
} | ||||
qreal ChartAxis::minimumHeight() | ||||
{ | ||||
if(m_minHeight == 0) updateGeometry(); | ||||
return m_minHeight; | ||||
} | ||||
|
r1006 | void ChartAxis::axisSelected() | ||
|
r452 | { | ||
|
r963 | qDebug()<<"TODO: axis clicked"; | ||
|
r439 | } | ||
|
r1006 | #include "moc_chartaxis_p.cpp" | ||
|
r67 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||