From db3c1ea5786f07b4cd13586af58f222f947c70e6 2012-09-24 14:39:06 From: Jani Honkonen Date: 2012-09-24 14:39:06 Subject: [PATCH] astyle and manual coding style fixes for src-folder --- diff --git a/src/animations/abstractbaranimation.cpp b/src/animations/abstractbaranimation.cpp index 17fe7f0..2058941 100644 --- a/src/animations/abstractbaranimation.cpp +++ b/src/animations/abstractbaranimation.cpp @@ -28,8 +28,8 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE AbstractBarAnimation::AbstractBarAnimation(AbstractBarChartItem *item) - :ChartAnimation(item), - m_item(item) + : ChartAnimation(item), + m_item(item) { setDuration(ChartAnimationDuration); setEasingCurve(QEasingCurve::OutQuart); diff --git a/src/animations/axisanimation.cpp b/src/animations/axisanimation.cpp index 0d004d6..e3daefa 100644 --- a/src/animations/axisanimation.cpp +++ b/src/animations/axisanimation.cpp @@ -29,9 +29,10 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE -AxisAnimation::AxisAnimation(ChartAxis *axis): ChartAnimation(axis), - m_axis(axis), - m_type(DefaultAnimation) +AxisAnimation::AxisAnimation(ChartAxis *axis) + : ChartAnimation(axis), + m_axis(axis), + m_type(DefaultAnimation) { setDuration(ChartAnimationDuration); setEasingCurve(QEasingCurve::OutQuart); @@ -43,72 +44,76 @@ AxisAnimation::~AxisAnimation() void AxisAnimation::setAnimationType(Animation type) { - if (state() != QAbstractAnimation::Stopped) stop(); - m_type=type; + if (state() != QAbstractAnimation::Stopped) + stop(); + m_type = type; } void AxisAnimation::setAnimationPoint(const QPointF& point) { - if (state() != QAbstractAnimation::Stopped) stop(); - m_point=point; + if (state() != QAbstractAnimation::Stopped) + stop(); + m_point = point; } void AxisAnimation::setValues(QVector &oldLayout, QVector &newLayout) { - if (state() != QAbstractAnimation::Stopped) stop(); - - if (newLayout.count() == 0) - return; - - switch (m_type) { - case ZoomOutAnimation: { - QRectF rect = m_axis->presenter()->chartsGeometry(); - oldLayout.resize(newLayout.count()); - - for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { - oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom(); - oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top(); - } - } - break; - case ZoomInAnimation: { - int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0); - oldLayout.resize(newLayout.count()); - - for(int i = 0; i < oldLayout.count(); i++) - oldLayout[i]= oldLayout[index]; - } - break; - case MoveForwardAnimation: { - oldLayout.resize(newLayout.count()); - - for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) - oldLayout[i]= oldLayout[j]; - } - break; - case MoveBackwordAnimation: { - oldLayout.resize(newLayout.count()); - - for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) - oldLayout[i]= oldLayout[j]; - } - break; - default: { - oldLayout.resize(newLayout.count()); - QRectF rect = m_axis->presenter()->chartsGeometry(); - for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) - oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top(); - } - break; - } - - QVariantAnimation::KeyValues value; - setKeyValues(value); //workaround for wrong interpolation call - setKeyValueAt(0.0, qVariantFromValue(oldLayout)); - setKeyValueAt(1.0, qVariantFromValue(newLayout)); + if (state() != QAbstractAnimation::Stopped) + stop(); + + if (newLayout.count() == 0) + return; + + switch (m_type) { + case ZoomOutAnimation: { + QRectF rect = m_axis->presenter()->chartsGeometry(); + oldLayout.resize(newLayout.count()); + + for (int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { + oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom(); + oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top(); + } + } + break; + case ZoomInAnimation: { + int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), + newLayout.count() - (qreal)1.0); + oldLayout.resize(newLayout.count()); + + for (int i = 0; i < oldLayout.count(); i++) + oldLayout[i] = oldLayout[index]; + } + break; + case MoveForwardAnimation: { + oldLayout.resize(newLayout.count()); + + for (int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) + oldLayout[i] = oldLayout[j]; + } + break; + case MoveBackwordAnimation: { + oldLayout.resize(newLayout.count()); + + for (int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) + oldLayout[i] = oldLayout[j]; + } + break; + default: { + oldLayout.resize(newLayout.count()); + QRectF rect = m_axis->presenter()->chartsGeometry(); + for (int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) + oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top(); + } + break; + } + + QVariantAnimation::KeyValues value; + setKeyValues(value); //workaround for wrong interpolation call + setKeyValueAt(0.0, qVariantFromValue(oldLayout)); + setKeyValueAt(1.0, qVariantFromValue(newLayout)); } -QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const +QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const { QVector startVector = qVariantValue >(start); QVector endVecotr = qVariantValue >(end); @@ -116,18 +121,17 @@ QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, Q_ASSERT(startVector.count() == endVecotr.count()) ; - for(int i = 0; i < startVector.count(); i++){ - qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0)); - result << value; + for (int i = 0; i < startVector.count(); i++) { + qreal value = startVector[i] + ((endVecotr[i] - startVector[i]) * progress); //qBound(0.0, progress, 1.0)); + result << value; } return qVariantFromValue(result); } -void AxisAnimation::updateCurrentValue (const QVariant &value ) +void AxisAnimation::updateCurrentValue(const QVariant &value) { - if (state() != QAbstractAnimation::Stopped)//workaround - { + if (state() != QAbstractAnimation::Stopped) { //workaround QVector vector = qVariantValue >(value); Q_ASSERT(vector.count() != 0); m_axis->setLayout(vector); diff --git a/src/animations/axisanimation_p.h b/src/animations/axisanimation_p.h index 3826ca1..a2d5de6 100644 --- a/src/animations/axisanimation_p.h +++ b/src/animations/axisanimation_p.h @@ -47,8 +47,8 @@ public: void setAnimationPoint(const QPointF& point); void setValues(QVector &oldLayout, QVector &newLayout); protected: - QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress ) const; - void updateCurrentValue(const QVariant &value ); + QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const; + void updateCurrentValue(const QVariant &value); private: ChartAxis *m_axis; Animation m_type; diff --git a/src/animations/baranimation.cpp b/src/animations/baranimation.cpp index 8dc4056..0e334da 100644 --- a/src/animations/baranimation.cpp +++ b/src/animations/baranimation.cpp @@ -27,7 +27,7 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE BarAnimation::BarAnimation(AbstractBarChartItem *item) - :AbstractBarAnimation(item) + : AbstractBarAnimation(item) { } @@ -44,7 +44,7 @@ QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qr Q_ASSERT(startVector.count() == endVector.count()); - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { QRectF start = startVector[i].normalized(); QRectF end = endVector[i].normalized(); @@ -62,7 +62,7 @@ QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qr y = endVector[i].top() + endVector[i].height() - h; } - QRectF value(x,y,w,h); + QRectF value(x, y, w, h); result << value.normalized(); } return qVariantFromValue(result); diff --git a/src/animations/chartanimation_p.h b/src/animations/chartanimation_p.h index 4e52fae..60c3529 100644 --- a/src/animations/chartanimation_p.h +++ b/src/animations/chartanimation_p.h @@ -41,7 +41,7 @@ class ChartAnimation: public QVariantAnimation { Q_OBJECT public: - ChartAnimation(QObject *parent = 0):QVariantAnimation(parent){}; + ChartAnimation(QObject *parent = 0): QVariantAnimation(parent) {}; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/horizontalbaranimation.cpp b/src/animations/horizontalbaranimation.cpp index d03802a..375dc2c 100644 --- a/src/animations/horizontalbaranimation.cpp +++ b/src/animations/horizontalbaranimation.cpp @@ -26,8 +26,8 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE -HorizontalBarAnimation::HorizontalBarAnimation(AbstractBarChartItem *item) : - AbstractBarAnimation(item) +HorizontalBarAnimation::HorizontalBarAnimation(AbstractBarChartItem *item) + : AbstractBarAnimation(item) { } @@ -45,7 +45,7 @@ QVariant HorizontalBarAnimation::interpolated(const QVariant &from, const QVaria Q_ASSERT(startVector.count() == endVector.count()); - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { QRectF start = startVector[i].normalized(); QRectF end = endVector[i].normalized(); @@ -63,7 +63,7 @@ QVariant HorizontalBarAnimation::interpolated(const QVariant &from, const QVaria x = end.left(); } - QRectF value(x,y,w,h); + QRectF value(x, y, w, h); result << value.normalized(); } return qVariantFromValue(result); diff --git a/src/animations/horizontalpercentbaranimation.cpp b/src/animations/horizontalpercentbaranimation.cpp index 8783d43..34e3942 100644 --- a/src/animations/horizontalpercentbaranimation.cpp +++ b/src/animations/horizontalpercentbaranimation.cpp @@ -47,13 +47,13 @@ QVariant HorizontalPercentBarAnimation::interpolated(const QVariant &from, const qreal xAxis = m_item->geometry().x(); - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { qreal h = endVector[i].height(); qreal w = endVector[i].width() * progress; qreal x = xAxis + ((endVector[i].left() - xAxis) * progress); qreal y = endVector[i].top(); - QRectF value(x,y,w,h); + QRectF value(x, y, w, h); result << value.normalized(); } return qVariantFromValue(result); diff --git a/src/animations/horizontalstackedbaranimation.cpp b/src/animations/horizontalstackedbaranimation.cpp index c2034a7..8359393 100644 --- a/src/animations/horizontalstackedbaranimation.cpp +++ b/src/animations/horizontalstackedbaranimation.cpp @@ -47,13 +47,13 @@ QVariant HorizontalStackedBarAnimation::interpolated(const QVariant &from, const qreal xAxis = m_item->geometry().x(); - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { qreal h = endVector[i].height(); qreal w = endVector[i].width() * progress; qreal x = xAxis + ((endVector[i].left() - xAxis) * progress); qreal y = endVector[i].top(); - QRectF value(x,y,w,h); + QRectF value(x, y, w, h); result << value.normalized(); } return qVariantFromValue(result); diff --git a/src/animations/percentbaranimation.cpp b/src/animations/percentbaranimation.cpp index 9266aa0..e0ef87c 100644 --- a/src/animations/percentbaranimation.cpp +++ b/src/animations/percentbaranimation.cpp @@ -27,7 +27,7 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE PercentBarAnimation::PercentBarAnimation(PercentBarChartItem *item) - :AbstractBarAnimation(item) + : AbstractBarAnimation(item) { setDuration(ChartAnimationDuration); setEasingCurve(QEasingCurve::OutQuart); @@ -47,13 +47,13 @@ QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant qreal yAxis = m_item->geometry().height() + m_item->geometry().y(); - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { qreal w = endVector[i].width(); qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress); qreal x = endVector[i].topLeft().x(); qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress); - QRectF value(x,y,w,h); + QRectF value(x, y, w, h); result << value.normalized(); } return qVariantFromValue(result); diff --git a/src/animations/pieanimation.cpp b/src/animations/pieanimation.cpp index 515c33e..759288e 100644 --- a/src/animations/pieanimation.cpp +++ b/src/animations/pieanimation.cpp @@ -26,8 +26,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE PieAnimation::PieAnimation(PieChartItem *item) - :ChartAnimation(item), - m_item(item) + : ChartAnimation(item), + m_item(item) { } diff --git a/src/animations/piesliceanimation.cpp b/src/animations/piesliceanimation.cpp index 415df2f..03ef6dc 100644 --- a/src/animations/piesliceanimation.cpp +++ b/src/animations/piesliceanimation.cpp @@ -58,8 +58,8 @@ QBrush linearPos(QBrush start, QBrush end, qreal pos) } PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem) - :ChartAnimation(sliceItem), - m_sliceItem(sliceItem) + : ChartAnimation(sliceItem), + m_sliceItem(sliceItem) { } diff --git a/src/animations/splineanimation.cpp b/src/animations/splineanimation.cpp index 7e33f3c..be3e9d8 100644 --- a/src/animations/splineanimation.cpp +++ b/src/animations/splineanimation.cpp @@ -27,9 +27,9 @@ Q_DECLARE_METATYPE(SplineVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE -SplineAnimation::SplineAnimation(SplineChartItem* item):XYAnimation(item), -m_item(item), -m_valid(false) +SplineAnimation::SplineAnimation(SplineChartItem* item): XYAnimation(item), + m_item(item), + m_valid(false) { } @@ -40,8 +40,8 @@ SplineAnimation::~SplineAnimation() void SplineAnimation::setup(QVector &oldPoints, QVector &newPoints, QVector &oldControlPoints, QVector &newControlPoints, int index) { if (newPoints.count() * 2 - 2 != newControlPoints.count() || newControlPoints.count() < 2) { - m_valid=false; - m_dirty=false; + m_valid = false; + m_dirty = false; m_item->setGeometryPoints(newPoints); m_item->setControlGeometryPoints(newControlPoints); m_item->setDirty(false); @@ -53,61 +53,58 @@ void SplineAnimation::setup(QVector &oldPoints, QVector &newPo if (state() != QAbstractAnimation::Stopped) { stop(); - m_dirty=false; + m_dirty = false; } - if(!m_dirty) { + if (!m_dirty) { m_dirty = true; m_oldSpline.first = oldPoints; m_oldSpline.second = oldControlPoints; } - m_newSpline.first=newPoints; - m_newSpline.second=newControlPoints; + m_newSpline.first = newPoints; + m_newSpline.second = newControlPoints; int x = m_oldSpline.first.count(); int y = m_newSpline.first.count(); - if(x - y == 1 && index >= 0 && y>0) { + if (x - y == 1 && index >= 0 && y > 0) { //remove point - if(index>0){ - m_newSpline.first.insert(index, newPoints[index-1]); - m_newSpline.second.insert((index -1) * 2, newPoints[index-1] ); - m_newSpline.second.insert((index -1) * 2 + 1, newPoints[index-1]); - }else{ + if (index > 0) { + m_newSpline.first.insert(index, newPoints[index - 1]); + m_newSpline.second.insert((index - 1) * 2, newPoints[index - 1]); + m_newSpline.second.insert((index - 1) * 2 + 1, newPoints[index - 1]); + } else { m_newSpline.first.insert(index, newPoints[index]); - m_newSpline.second.insert(index * 2, newPoints[index] ); + m_newSpline.second.insert(index * 2, newPoints[index]); m_newSpline.second.insert(index * 2 + 1, newPoints[index]); } - m_index=index; + m_index = index; m_type = RemovePointAnimation; } - if(x - y == -1 && index >= 0) { + if (x - y == -1 && index >= 0) { //add point - if(index>0){ - m_oldSpline.first.insert(index, newPoints[index-1]); - m_oldSpline.second.insert((index - 1) * 2, newPoints[index-1]); - m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index-1]); - }else{ + if (index > 0) { + m_oldSpline.first.insert(index, newPoints[index - 1]); + m_oldSpline.second.insert((index - 1) * 2, newPoints[index - 1]); + m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index - 1]); + } else { m_oldSpline.first.insert(index, newPoints[index]); m_oldSpline.second.insert((index - 1) * 2, newPoints[index]); m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index]); } - m_index=index; + m_index = index; m_type = AddPointAnimation; } x = m_oldSpline.first.count(); y = m_newSpline.first.count(); - if(x != y) - { + if (x != y) { m_type = NewAnimation; - } - else if(m_type == NewAnimation) - { + } else if (m_type == NewAnimation) { m_type = ReplacePointAnimation; } @@ -115,11 +112,11 @@ void SplineAnimation::setup(QVector &oldPoints, QVector &newPo setKeyValueAt(0.0, qVariantFromValue(m_oldSpline)); setKeyValueAt(1.0, qVariantFromValue(m_newSpline)); - m_valid=true; + m_valid = true; } -QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const +QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const { SplineVector startPair = qVariantValue< SplineVector >(start); @@ -127,44 +124,41 @@ QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en SplineVector result; switch (animationType()) { - - case RemovePointAnimation: - case AddPointAnimation: - case ReplacePointAnimation: - { + case RemovePointAnimation: + case AddPointAnimation: + case ReplacePointAnimation: { if (startPair.first.count() != endPair.first.count()) - break; + break; Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count()); Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count()); - for(int i = 0; i < endPair.first.count(); i++) { + for (int i = 0; i < endPair.first.count(); i++) { qreal x = startPair.first[i].x() + ((endPair.first[i].x() - startPair.first[i].x()) * progress); qreal y = startPair.first[i].y() + ((endPair.first[i].y() - startPair.first[i].y()) * progress); - result.first << QPointF(x,y); + result.first << QPointF(x, y); if (i + 1 >= endPair.first.count()) - continue; + continue; x = startPair.second[i * 2].x() + ((endPair.second[i * 2].x() - startPair.second[i * 2].x()) * progress); y = startPair.second[i * 2].y() + ((endPair.second[i * 2].y() - startPair.second[i * 2].y()) * progress); - result.second << QPointF(x,y); + result.second << QPointF(x, y); x = startPair.second[i * 2 + 1].x() + ((endPair.second[i * 2 + 1].x() - startPair.second[i * 2 + 1].x()) * progress); y = startPair.second[i * 2 + 1].y() + ((endPair.second[i * 2 + 1].y() - startPair.second[i * 2 + 1].y()) * progress); - result.second << QPointF(x,y); + result.second << QPointF(x, y); } - } break; - case NewAnimation: { + case NewAnimation: { Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count()); - int count = endPair.first.count()* qBound(qreal(0), progress, qreal(1)); - for(int i = 0; i < count; i++) { + int count = endPair.first.count() * qBound(qreal(0), progress, qreal(1)); + for (int i = 0; i < count; i++) { result.first << endPair.first[i]; - if(i + 1 == count) - break; + if (i + 1 == count) + break; result.second << endPair.second[2 * i]; result.second << endPair.second[2 * i + 1]; } } break; - default: + default: qWarning() << "Unknown type of animation"; break; } @@ -172,7 +166,7 @@ QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en return qVariantFromValue(result); } -void SplineAnimation::updateCurrentValue (const QVariant &value ) +void SplineAnimation::updateCurrentValue(const QVariant &value) { if (state() != QAbstractAnimation::Stopped && m_valid) { //workaround QPair, QVector > pair = qVariantValue< QPair< QVector, QVector > >(value); @@ -188,24 +182,21 @@ void SplineAnimation::updateState(QAbstractAnimation::State newState, QAbstractA { XYAnimation::updateState(newState, oldState); - if(oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) - { - if(m_item->isDirty() && m_type==RemovePointAnimation) { - if(!m_newSpline.first.isEmpty()) { + if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) { + if (m_item->isDirty() && m_type == RemovePointAnimation) { + if (!m_newSpline.first.isEmpty()) { m_newSpline.first.remove(m_index); - m_newSpline.second.remove((m_index-1) * 2); - m_newSpline.second.remove((m_index-1) * 2); + m_newSpline.second.remove((m_index - 1) * 2); + m_newSpline.second.remove((m_index - 1) * 2); } m_item->setGeometryPoints(m_newSpline.first); m_item->setControlGeometryPoints(m_newSpline.second); } } - if(oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running) - { - if(!m_valid) { - stop(); - } + if (oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running) { + if (!m_valid) + stop(); } } diff --git a/src/animations/stackedbaranimation.cpp b/src/animations/stackedbaranimation.cpp index a0b4b82..4560332 100644 --- a/src/animations/stackedbaranimation.cpp +++ b/src/animations/stackedbaranimation.cpp @@ -27,7 +27,7 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE StackedBarAnimation::StackedBarAnimation(StackedBarChartItem *item) - :AbstractBarAnimation(item) + : AbstractBarAnimation(item) { setEasingCurve(QEasingCurve::OutQuart); } @@ -46,13 +46,13 @@ QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant qreal yAxis = m_item->geometry().height() + m_item->geometry().y(); - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { qreal w = endVector[i].width(); qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress); qreal x = endVector[i].topLeft().x(); qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress); - QRectF value(x,y,w,h); + QRectF value(x, y, w, h); result << value.normalized(); } return qVariantFromValue(result); diff --git a/src/animations/xyanimation.cpp b/src/animations/xyanimation.cpp index a033d7a..8541cff 100644 --- a/src/animations/xyanimation.cpp +++ b/src/animations/xyanimation.cpp @@ -26,11 +26,12 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE -XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item), - m_type(NewAnimation), - m_dirty(false), - m_index(-1), - m_item(item) +XYAnimation::XYAnimation(XYChart *item) + : ChartAnimation(item), + m_type(NewAnimation), + m_dirty(false), + m_index(-1), + m_item(item) { setDuration(ChartAnimationDuration); setEasingCurve(QEasingCurve::OutQuart); @@ -42,54 +43,50 @@ XYAnimation::~XYAnimation() void XYAnimation::setup(const QVector &oldPoints, const QVector &newPoints, int index) { - m_type = NewAnimation; - - if (state() != QAbstractAnimation::Stopped){ - stop(); - m_dirty=false; - } - - if(!m_dirty){ - m_dirty = true; - m_oldPoints = oldPoints; - } - - m_newPoints = newPoints; - - int x = m_oldPoints.count(); - int y = m_newPoints.count(); - - if(x - y == 1 && index >= 0 && y > 0){ - //remove point - m_newPoints.insert(index, index > 0 ? newPoints[index-1] : newPoints[index]); - m_index=index; - m_type = RemovePointAnimation; - } - - if(x - y == -1 && index >= 0){ - //add point - m_oldPoints.insert(index, index > 0 ? newPoints[index-1] : newPoints[index]); - m_index=index; - m_type = AddPointAnimation; - } - - x = m_oldPoints.count(); - y = m_newPoints.count(); - - if(x != y) - { - m_type = NewAnimation; - } - else if(m_type == NewAnimation) - { - m_type = ReplacePointAnimation; - } + m_type = NewAnimation; + + if (state() != QAbstractAnimation::Stopped) { + stop(); + m_dirty = false; + } + + if (!m_dirty) { + m_dirty = true; + m_oldPoints = oldPoints; + } + + m_newPoints = newPoints; + + int x = m_oldPoints.count(); + int y = m_newPoints.count(); + + if (x - y == 1 && index >= 0 && y > 0) { + //remove point + m_newPoints.insert(index, index > 0 ? newPoints[index - 1] : newPoints[index]); + m_index = index; + m_type = RemovePointAnimation; + } + + if (x - y == -1 && index >= 0) { + //add point + m_oldPoints.insert(index, index > 0 ? newPoints[index - 1] : newPoints[index]); + m_index = index; + m_type = AddPointAnimation; + } + + x = m_oldPoints.count(); + y = m_newPoints.count(); + + if (x != y) + m_type = NewAnimation; + else if (m_type == NewAnimation) + m_type = ReplacePointAnimation; setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); setKeyValueAt(1.0, qVariantFromValue(m_newPoints)); } -QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const +QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const { QVector startVector = qVariantValue >(start); QVector endVector = qVariantValue >(end); @@ -99,25 +96,23 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q case ReplacePointAnimation: case AddPointAnimation: - case RemovePointAnimation: - { - if (startVector.count() != endVector.count()){ + case RemovePointAnimation: { + if (startVector.count() != endVector.count()) break; - } - for(int i = 0; i < startVector.count(); i++) { + for (int i = 0; i < startVector.count(); i++) { qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress); qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress); result << QPointF(x, y); } } - break; + break; case NewAnimation: { - for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++) + for (int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++) result << endVector[i]; } - break; + break; default: qWarning() << "Unknown type of animation"; break; @@ -126,28 +121,28 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q return qVariantFromValue(result); } -void XYAnimation::updateCurrentValue (const QVariant &value) +void XYAnimation::updateCurrentValue(const QVariant &value) { - if(state()!=QAbstractAnimation::Stopped){ //workaround + if (state() != QAbstractAnimation::Stopped) { //workaround QVector vector = qVariantValue >(value); m_item->setGeometryPoints(vector); m_item->updateGeometry(); m_item->setDirty(true); - m_dirty=false; + m_dirty = false; } } -void XYAnimation::updateState( QAbstractAnimation::State newState, QAbstractAnimation::State oldState ) +void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) { - if(oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) - { - if(m_item->isDirty() && m_type==RemovePointAnimation){ - if(!m_newPoints.isEmpty()) m_newPoints.remove(m_index); - m_item->setGeometryPoints(m_newPoints); - } - } + if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) { + if (m_item->isDirty() && m_type == RemovePointAnimation) { + if (!m_newPoints.isEmpty()) + m_newPoints.remove(m_index); + m_item->setGeometryPoints(m_newPoints); + } + } } #include "moc_chartanimation_p.cpp" diff --git a/src/animations/xyanimation_p.h b/src/animations/xyanimation_p.h index 203137b..b1458da 100644 --- a/src/animations/xyanimation_p.h +++ b/src/animations/xyanimation_p.h @@ -44,12 +44,12 @@ protected: public: XYAnimation(XYChart *item); ~XYAnimation(); - void setup(const QVector &oldPoints, const QVector &newPoints,int index = -1); + void setup(const QVector &oldPoints, const QVector &newPoints, int index = -1); Animation animationType() const { return m_type; }; protected: - QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const; - void updateCurrentValue (const QVariant &value ); + QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const; + void updateCurrentValue(const QVariant &value); void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState); protected: Animation m_type; diff --git a/src/areachart/areachartitem.cpp b/src/areachart/areachartitem.cpp index e18abd2..b801923 100644 --- a/src/areachart/areachartitem.cpp +++ b/src/areachart/areachartitem.cpp @@ -35,21 +35,20 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter) : ChartItem(presenter), - m_series(areaSeries), - m_upper(0), - m_lower(0), - m_pointsVisible(false) + m_series(areaSeries), + m_upper(0), + m_lower(0), + m_pointsVisible(false) { setZValue(ChartPresenter::LineChartZValue); - m_upper = new AreaBoundItem(this,m_series->upperSeries(),presenter); - if (m_series->lowerSeries()){ - m_lower = new AreaBoundItem(this,m_series->lowerSeries(),presenter); - } + m_upper = new AreaBoundItem(this, m_series->upperSeries(), presenter); + if (m_series->lowerSeries()) + m_lower = new AreaBoundItem(this, m_series->lowerSeries(), presenter); - QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); + QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); - QObject::connect(this,SIGNAL(clicked(QPointF)),areaSeries,SIGNAL(clicked(QPointF))); + QObject::connect(this, SIGNAL(clicked(QPointF)), areaSeries, SIGNAL(clicked(QPointF))); handleUpdated(); } @@ -81,8 +80,8 @@ void AreaChartItem::updatePath() } else { QPointF first = path.pointAtPercent(0); QPointF last = path.pointAtPercent(1); - path.lineTo(last.x(),m_clipRect.bottom()); - path.lineTo(first.x(),m_clipRect.bottom()); + path.lineTo(last.x(), m_clipRect.bottom()); + path.lineTo(first.x(), m_clipRect.bottom()); } path.closeSubpath(); prepareGeometryChange(); @@ -108,7 +107,7 @@ void AreaChartItem::handleDomainUpdated() { m_upper->setDomain(domain()); m_upper->handleDomainUpdated(); - if (m_lower){ + if (m_lower) { m_lower->setDomain(domain()); m_lower->handleDomainUpdated(); } @@ -116,7 +115,7 @@ void AreaChartItem::handleDomainUpdated() void AreaChartItem::handleGeometryChanged(const QRectF &rect) { - m_clipRect=rect.translated(-rect.topLeft()); + m_clipRect = rect.translated(-rect.topLeft()); setPos(rect.topLeft()); m_upper->handleGeometryChanged(rect); if (m_lower) @@ -134,10 +133,10 @@ void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt painter->setClipRect(m_clipRect); painter->drawPath(m_path); if (m_pointsVisible) { - painter->setPen(m_pointPen); - painter->drawPoints(m_upper->geometryPoints()); - if (m_lower) - painter->drawPoints(m_lower->geometryPoints()); + painter->setPen(m_pointPen); + painter->drawPoints(m_upper->geometryPoints()); + if (m_lower) + painter->drawPoints(m_lower->geometryPoints()); } painter->restore(); } diff --git a/src/areachart/areachartitem_p.h b/src/areachart/areachartitem_p.h index 6e87ca1..4dd1884 100644 --- a/src/areachart/areachartitem_p.h +++ b/src/areachart/areachartitem_p.h @@ -84,7 +84,9 @@ private: class AreaBoundItem : public LineChartItem { public: - AreaBoundItem(AreaChartItem *item,QLineSeries *lineSeries,ChartPresenter* presenter) : LineChartItem(lineSeries, 0), m_item(item) { + AreaBoundItem(AreaChartItem *item, QLineSeries *lineSeries, ChartPresenter* presenter) + : LineChartItem(lineSeries, 0), m_item(item) + { setPresenter(presenter); } ~AreaBoundItem() {} diff --git a/src/areachart/qareaseries.cpp b/src/areachart/qareaseries.cpp index d6a2e06..9f57878 100644 --- a/src/areachart/qareaseries.cpp +++ b/src/areachart/qareaseries.cpp @@ -36,11 +36,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE \mainclass - An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line - is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance, - which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line. + An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line + is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance, + which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line. In that case QAreaSeries should be initiated with two QLineSeries instances. Please note terms "upper" and "lower" boundary can be misleading in cases - where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled. + where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled. See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart. \image examples_areachart.png @@ -162,7 +162,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE When series object is added to QChartView or QChart instance ownerships is transferred. */ QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries) - : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries,lowerSeries,this),upperSeries) + : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries, lowerSeries, this), upperSeries) { } @@ -180,9 +180,8 @@ QAreaSeries::QAreaSeries(QObject *parent) QAreaSeries::~QAreaSeries() { Q_D(QAreaSeries); - if(d->m_dataset){ - d->m_dataset->removeSeries(this); - } + if (d->m_dataset) + d->m_dataset->removeSeries(this); } /*! @@ -315,11 +314,11 @@ bool QAreaSeries::pointsVisible() const //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q) : - QAbstractSeriesPrivate(q), - m_upperSeries(upperSeries), - m_lowerSeries(lowerSeries), - m_pointsVisible(false) +QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries* q) + : QAbstractSeriesPrivate(q), + m_upperSeries(upperSeries), + m_lowerSeries(lowerSeries), + m_pointsVisible(false) { } @@ -337,8 +336,7 @@ void QAreaSeriesPrivate::scaleDomain(Domain& domain) const QList& points = upperSeries->points(); - for (int i = 0; i < points.count(); i++) - { + for (int i = 0; i < points.count(); i++) { qreal x = points[i].x(); qreal y = points[i].y(); minX = qMin(minX, x); @@ -346,33 +344,32 @@ void QAreaSeriesPrivate::scaleDomain(Domain& domain) maxX = qMax(maxX, x); maxY = qMax(maxY, y); } - if(lowerSeries) { + if (lowerSeries) { const QList& points = lowerSeries->points(); - for (int i = 0; i < points.count(); i++) - { + for (int i = 0; i < points.count(); i++) { qreal x = points[i].x(); qreal y = points[i].y(); minX = qMin(minX, x); minY = qMin(minY, y); maxX = qMax(maxX, x); maxY = qMax(maxY, y); - }} + } + } - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } ChartElement* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QAreaSeries); - AreaChartItem* area = new AreaChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + AreaChartItem* area = new AreaChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem())); - if(q->lowerSeries()) { + if (q->lowerSeries()) area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem())); - } } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return area; @@ -382,7 +379,7 @@ QList QAreaSeriesPrivate::createLegendMarker(QLegend* legend) { Q_Q(QAreaSeries); QList list; - return list << new AreaLegendMarker(q,legend); + return list << new AreaLegendMarker(q, legend); } diff --git a/src/areachart/qareaseries_p.h b/src/areachart/qareaseries_p.h index ce4487d..f219deb 100644 --- a/src/areachart/qareaseries_p.h +++ b/src/areachart/qareaseries_p.h @@ -41,7 +41,7 @@ class QAreaSeriesPrivate: public QAbstractSeriesPrivate Q_OBJECT public: - QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q); + QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries* q); void scaleDomain(Domain& domain); ChartElement* createGraphics(ChartPresenter* presenter); diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp index 9290cd4..b113fd2 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp @@ -29,8 +29,9 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), -m_categoriesAxis(axis) +ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter), + m_categoriesAxis(axis) { } @@ -43,31 +44,29 @@ QVector ChartBarCategoryAxisX::calculateLayout() const { int count = m_categoriesAxis->d_ptr->count(); - Q_ASSERT(count>=1); + Q_ASSERT(count >= 1); QVector points; - points.resize(count+2); + points.resize(count + 2); QRectF rect = presenter()->chartsGeometry(); - const qreal delta = rect.width()/(count); - qreal offset =-m_min-0.5; + const qreal delta = rect.width() / (count); + qreal offset = -m_min - 0.5; - if(delta<1) return points; + if (delta < 1) return points; - if(offset<=0) { - offset = int(offset * rect.width()/(m_max - m_min))%int(delta) + delta; - } - else { - offset = int(offset * rect.width()/(m_max - m_min))%int(delta); - } + if (offset <= 0) + offset = int(offset * rect.width() / (m_max - m_min)) % int(delta) + delta; + else + offset = int(offset * rect.width() / (m_max - m_min)) % int(delta); points[0] = rect.left(); - points[count+1] = rect.right(); + points[count + 1] = rect.right(); for (int i = 0; i < count; ++i) { qreal x = offset + i * delta + rect.left(); - points[i+1] = x; + points[i + 1] = x; } return points; } @@ -76,16 +75,13 @@ QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector& la { QStringList result; QRectF rect = presenter()->chartsGeometry(); - qreal d = (m_max - m_min)/rect.width(); - for (int i = 0;i < layout.count()-1; ++i) { - qreal x = qFloor((((layout[i+1] + layout[i])/2-rect.left())*d + m_min+0.5)); - if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { + qreal d = (m_max - m_min) / rect.width(); + for (int i = 0; i < layout.count() - 1; ++i) { + qreal x = qFloor((((layout[i + 1] + layout[i]) / 2 - rect.left()) * d + m_min + 0.5)); + if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) result << m_categoriesAxis->categories().at(x); - } - else { - // No label for x coordinate - result << ""; - } + else + result << ""; // No label for x coordinate } result << ""; return result; @@ -96,7 +92,8 @@ void ChartBarCategoryAxisX::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; QStringList ticksList = createCategoryLabels(layout); @@ -110,10 +107,10 @@ void ChartBarCategoryAxisX::updateGeometry() QRectF chartRect = presenter()->chartsGeometry(); - const qreal delta = chartRect.width()/(m_categoriesAxis->d_ptr->count()); + const qreal delta = chartRect.width() / (m_categoriesAxis->d_ptr->count()); QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine( chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom()); + lineItem->setLine(chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom()); qreal width = chartRect.left(); for (int i = 0; i < layout.size(); ++i) { @@ -125,35 +122,32 @@ void ChartBarCategoryAxisX::updateGeometry() QPointF center = rect.center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - if(i==0){ - labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), chartRect.bottom() + label_padding); - }else{ - labelItem->setPos(layout[i] + (delta)/2 - center.x(), chartRect.bottom() + label_padding); - } + if (i == 0) + labelItem->setPos(layout[i + 1] - (delta) / 2 - center.x(), chartRect.bottom() + label_padding); + else + labelItem->setPos(layout[i] + (delta) / 2 - center.x(), chartRect.bottom() + label_padding); - if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()> chartRect.right()) { + if (labelItem->pos().x() <= width || labelItem->pos().x() + rect.width() > chartRect.right()) { labelItem->setVisible(false); - } - else { + } else { labelItem->setVisible(true); - width=rect.width()+labelItem->pos().x(); + width = rect.width() + labelItem->pos().x(); } - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect(layout[i-1], chartRect.top(),layout[i]-layout[i-1], chartRect.height()); + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(layout[i - 1], chartRect.top(), layout[i] - layout[i - 1], chartRect.height()); } - lineItem = static_cast(axis.at(i+1)); - lineItem->setLine(layout[i], chartRect.bottom(),layout[i], chartRect.bottom()+5); + lineItem = static_cast(axis.at(i + 1)); + lineItem->setLine(layout[i], chartRect.bottom(), layout[i], chartRect.bottom() + 5); } } void ChartBarCategoryAxisX::handleAxisUpdated() { - if(m_categoriesAxis->categories()!=m_categories) - { - m_categories=m_categoriesAxis->categories(); - if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry(); + if (m_categoriesAxis->categories() != m_categories) { + m_categories = m_categoriesAxis->categories(); + if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) updateGeometry(); } ChartAxis::handleAxisUpdated(); } @@ -166,35 +160,32 @@ QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constra QSizeF sh; QSizeF base = ChartAxis::sizeHint(which, constraint); QStringList ticksList = createCategoryLabels(ChartAxis::layout()); - qreal width=0; - qreal height=0; - - switch (which) { - case Qt::MinimumSize: - width = fn.boundingRect("...").width(); - height = fn.height()+label_padding; - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); - break; - case Qt::PreferredSize:{ - - for (int i = 0; i < ticksList.size(); ++i) - { - QRectF rect = fn.boundingRect(ticksList.at(i)); - width+=rect.width(); - height=qMax(rect.height()+label_padding,height); - } - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); - break; + qreal width = 0; + qreal height = 0; + + switch (which) { + case Qt::MinimumSize: + width = fn.boundingRect("...").width(); + height = fn.height() + label_padding; + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); + break; + case Qt::PreferredSize: + for (int i = 0; i < ticksList.size(); ++i) { + QRectF rect = fn.boundingRect(ticksList.at(i)); + width += rect.width(); + height = qMax(rect.height() + label_padding, height); } - default: - break; - } + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); + break; + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h b/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h index 2340ff7..b58d2fe 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h @@ -51,7 +51,7 @@ protected: void updateGeometry(); private: QStringList createCategoryLabels(const QVector& layout) const; -Q_SLOTS +public Q_SLOTS: void handleAxisUpdated(); private: diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp index 1a8071a..2f98578 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp @@ -29,8 +29,9 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), -m_categoriesAxis(axis) +ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter), + m_categoriesAxis(axis) { } @@ -42,31 +43,29 @@ QVector ChartBarCategoryAxisY::calculateLayout() const { int count = m_categoriesAxis->d_ptr->count(); - Q_ASSERT(count>=1); + Q_ASSERT(count >= 1); QVector points; - points.resize(count+2); + points.resize(count + 2); QRectF rect = presenter()->chartsGeometry(); - const qreal delta = rect.height()/(count); + const qreal delta = rect.height() / (count); qreal offset = - m_min - 0.5; - if(delta<1) return points; + if (delta < 1) return points; - if(offset<=0) { - offset = int(offset * rect.height()/(m_max - m_min))%int(delta) + delta; - } - else { - offset = int(offset * rect.height()/(m_max - m_min))%int(delta); - } + if (offset <= 0) + offset = int(offset * rect.height() / (m_max - m_min)) % int(delta) + delta; + else + offset = int(offset * rect.height() / (m_max - m_min)) % int(delta); points[0] = rect.bottom(); - points[count+1] = rect.top(); + points[count + 1] = rect.top(); for (int i = 0; i < count; ++i) { int y = rect.bottom() - i * delta - offset; - points[i+1] = y; + points[i + 1] = y; } return points; } @@ -75,16 +74,13 @@ QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector& la { QStringList result; QRectF rect = presenter()->chartsGeometry(); - qreal d = (m_max - m_min)/rect.height(); - for (int i = 0;i < layout.count()-1; ++i) { - qreal x = qFloor(((rect.height()- (layout[i+1] + layout[i])/2 + rect.top())*d + m_min+0.5)); - if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { + qreal d = (m_max - m_min) / rect.height(); + for (int i = 0; i < layout.count() - 1; ++i) { + qreal x = qFloor(((rect.height() - (layout[i + 1] + layout[i]) / 2 + rect.top()) * d + m_min + 0.5)); + if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) result << m_categoriesAxis->categories().at(x); - } - else { - // No label for x coordinate - result << ""; - } + else + result << ""; // No label for x coordinate } result << ""; return result; @@ -94,7 +90,8 @@ void ChartBarCategoryAxisY::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; QStringList ticksList = createCategoryLabels(layout); @@ -108,7 +105,7 @@ void ChartBarCategoryAxisY::updateGeometry() QRectF chartRect = presenter()->chartsGeometry(); - const qreal delta = chartRect.height()/(m_categoriesAxis->d_ptr->count()); + const qreal delta = chartRect.height() / (m_categoriesAxis->d_ptr->count()); QGraphicsLineItem *lineItem = static_cast(axis.at(0)); lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); @@ -123,39 +120,34 @@ void ChartBarCategoryAxisY::updateGeometry() QPointF center = rect.center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - if(i==0) { - labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y()); - } - else { - labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y()); - } + if (i == 0) + labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i + 1] + (delta) / 2 - center.y()); + else + labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] - (delta) / 2 - center.y()); - if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < chartRect.top()) { + if (labelItem->pos().y() + rect.height() >= height || labelItem->pos().y() < chartRect.top()) { labelItem->setVisible(false); - } - else { + } else { labelItem->setVisible(true); - height=labelItem->pos().y(); + height = labelItem->pos().y(); } - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]); + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]); } - lineItem = static_cast(axis.at(i+1)); - lineItem->setLine(chartRect.left()-5,layout[i],chartRect.left(),layout[i]); + lineItem = static_cast(axis.at(i + 1)); + lineItem->setLine(chartRect.left() - 5, layout[i], chartRect.left(), layout[i]); } } void ChartBarCategoryAxisY::handleAxisUpdated() { - if(m_categoriesAxis->categories()!=m_categories) - { - m_categories=m_categoriesAxis->categories(); - if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) { + if (m_categoriesAxis->categories() != m_categories) { + m_categories = m_categoriesAxis->categories(); + if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) updateGeometry(); - } } ChartAxis::handleAxisUpdated(); } @@ -168,35 +160,32 @@ QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constra QSizeF sh; QSizeF base = ChartAxis::sizeHint(which, constraint); QStringList ticksList = createCategoryLabels(ChartAxis::layout()); - qreal width=0; - qreal height=0; - - switch (which) { - case Qt::MinimumSize: - width = fn.boundingRect("...").width() + label_padding; - height = fn.height(); - width+=base.width(); - height=qMax(height,base.height()); - sh = QSizeF(width,height); - break; - case Qt::PreferredSize:{ - - for (int i = 0; i < ticksList.size(); ++i) - { - QRectF rect = fn.boundingRect(ticksList.at(i)); - height+=rect.height(); - width=qMax(rect.width()+label_padding,width); - } - height=qMax(height,base.height()); - width+=base.width(); - sh = QSizeF(width,height); - break; + qreal width = 0; + qreal height = 0; + + switch (which) { + case Qt::MinimumSize: + width = fn.boundingRect("...").width() + label_padding; + height = fn.height(); + width += base.width(); + height = qMax(height, base.height()); + sh = QSizeF(width, height); + break; + case Qt::PreferredSize: + for (int i = 0; i < ticksList.size(); ++i) { + QRectF rect = fn.boundingRect(ticksList.at(i)); + height += rect.height(); + width = qMax(rect.width() + label_padding, width); } - default: - break; - } + height = qMax(height, base.height()); + width += base.width(); + sh = QSizeF(width, height); + break; + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h b/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h index 1bf399b..3efe505 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h @@ -51,8 +51,8 @@ protected: void updateGeometry(); private: QStringList createCategoryLabels(const QVector& layout) const; -Q_SLOTS - void handleAxisUpdated(); +public Q_SLOTS: + void handleAxisUpdated(); private: QStringList m_categories; QBarCategoryAxis *m_categoriesAxis; diff --git a/src/axis/barcategoryaxis/qbarcategoryaxis.cpp b/src/axis/barcategoryaxis/qbarcategoryaxis.cpp index b61d069..172f926 100644 --- a/src/axis/barcategoryaxis/qbarcategoryaxis.cpp +++ b/src/axis/barcategoryaxis/qbarcategoryaxis.cpp @@ -131,7 +131,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs an axis object which is a child of \a parent. */ QBarCategoryAxis::QBarCategoryAxis(QObject *parent): - QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent) + QAbstractAxis(*new QBarCategoryAxisPrivate(this), parent) { } @@ -141,15 +141,15 @@ QBarCategoryAxis::QBarCategoryAxis(QObject *parent): QBarCategoryAxis::~QBarCategoryAxis() { Q_D(QBarCategoryAxis); - if(d->m_dataset){ - d->m_dataset->removeAxis(this); - } + if (d->m_dataset) + d->m_dataset->removeAxis(this); } /*! \internal */ -QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent) +QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent) + : QAbstractAxis(d, parent) { } @@ -159,13 +159,14 @@ QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):Q */ void QBarCategoryAxis::append(const QStringList &categories) { - if(categories.isEmpty()) return; + if (categories.isEmpty()) + return; Q_D(QBarCategoryAxis); if (d->m_categories.isEmpty()) { d->m_categories.append(categories); - setRange(categories.first(),categories.last()); - }else{ + setRange(categories.first(), categories.last()); + } else { d->m_categories.append(categories); d->emitUpdated(); } @@ -180,8 +181,8 @@ void QBarCategoryAxis::append(const QString &category) Q_D(QBarCategoryAxis); if (d->m_categories.isEmpty()) { d->m_categories.append(category); - setRange(category,category); - }else{ + setRange(category, category); + } else { d->m_categories.append(category); d->emitUpdated(); } @@ -196,10 +197,10 @@ void QBarCategoryAxis::remove(const QString &category) Q_D(QBarCategoryAxis); if (d->m_categories.contains(category)) { d->m_categories.removeAt(d->m_categories.indexOf(category)); - if(!d->m_categories.isEmpty()) - setRange(d->m_categories.first(),d->m_categories.last()); + if (!d->m_categories.isEmpty()) + setRange(d->m_categories.first(), d->m_categories.last()); else - setRange(QString::null,QString::null); + setRange(QString::null, QString::null); emit categoriesChanged(); } } @@ -211,10 +212,10 @@ void QBarCategoryAxis::insert(int index, const QString &category) { Q_D(QBarCategoryAxis); if (d->m_categories.isEmpty()) { - d->m_categories.insert(index,category); - setRange(category,category); - }else{ - d->m_categories.insert(index,category); + d->m_categories.insert(index, category); + setRange(category, category); + } else { + d->m_categories.insert(index, category); d->emitUpdated(); } emit categoriesChanged(); @@ -242,16 +243,16 @@ void QBarCategoryAxis::clear() { Q_D(QBarCategoryAxis); d->m_categories.clear(); - setRange(QString::null,QString::null); + setRange(QString::null, QString::null); emit categoriesChanged(); } void QBarCategoryAxis::setCategories(const QStringList &categories) { Q_D(QBarCategoryAxis); - if(d->m_categories!=categories) { + if (d->m_categories != categories) { d->m_categories = categories; - setRange(categories.first(),categories.last()); + setRange(categories.first(), categories.last()); emit categoriesChanged(); } } @@ -286,7 +287,7 @@ QString QBarCategoryAxis::at(int index) const void QBarCategoryAxis::setMin(const QString& min) { Q_D(QBarCategoryAxis); - setRange(min,d->m_maxCategory); + setRange(min, d->m_maxCategory); } /*! @@ -304,7 +305,7 @@ QString QBarCategoryAxis::min() const void QBarCategoryAxis::setMax(const QString& max) { Q_D(QBarCategoryAxis); - setRange(d->m_minCategory,max); + setRange(d->m_minCategory, max); } /*! @@ -326,28 +327,29 @@ void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCa bool changed = false; //special case - if(minCategory.isNull() && maxCategory.isNull()){ + if (minCategory.isNull() && maxCategory.isNull()) { d->m_minCategory = minCategory; d->m_maxCategory = maxCategory; d->m_min = 0; d->m_max = 0; emit minChanged(minCategory); emit maxChanged(maxCategory); - d->m_count=0; - emit rangeChanged(d->m_minCategory,d->m_maxCategory); + d->m_count = 0; + emit rangeChanged(d->m_minCategory, d->m_maxCategory); d->emitUpdated(); } - if(d->m_categories.indexOf(d->m_maxCategory)m_categories.indexOf(d->m_minCategory)) return; + if (d->m_categories.indexOf(d->m_maxCategory) < d->m_categories.indexOf(d->m_minCategory)) + return; - if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) { + if (!minCategory.isEmpty() && d->m_minCategory != minCategory && d->m_categories.contains(minCategory)) { d->m_minCategory = minCategory; d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5; changed = true; emit minChanged(minCategory); } - if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) { + if (!maxCategory.isEmpty() && d->m_maxCategory != maxCategory && d->m_categories.contains(maxCategory)) { d->m_maxCategory = maxCategory; d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5; changed = true; @@ -355,8 +357,8 @@ void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCa } if (changed) { - d->m_count=d->m_max - d->m_min; - emit rangeChanged(d->m_minCategory,d->m_maxCategory); + d->m_count = d->m_max - d->m_min; + emit rangeChanged(d->m_minCategory, d->m_maxCategory); d->emitUpdated(); } } @@ -387,12 +389,12 @@ QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate() void QBarCategoryAxisPrivate::setMin(const QVariant &min) { - setRange(min,m_maxCategory); + setRange(min, m_maxCategory); } void QBarCategoryAxisPrivate::setMax(const QVariant &max) { - setRange(m_minCategory,max); + setRange(m_minCategory, max); } void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max) @@ -400,7 +402,7 @@ void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max) Q_Q(QBarCategoryAxis); QString value1 = min.toString(); QString value2 = max.toString(); - q->setRange(value1,value2); + q->setRange(value1, value2); } void QBarCategoryAxisPrivate::handleDomainUpdated() @@ -408,11 +410,10 @@ void QBarCategoryAxisPrivate::handleDomainUpdated() Q_Q(QBarCategoryAxis); Domain* domain = qobject_cast(sender()); - if(m_orientation==Qt::Horizontal) { + if (m_orientation == Qt::Horizontal) { m_min = domain->minX(); m_max = domain->maxX(); - } - else if(m_orientation==Qt::Vertical) { + } else if (m_orientation == Qt::Vertical) { m_min = domain->minY(); m_max = domain->maxY(); } @@ -420,64 +421,57 @@ void QBarCategoryAxisPrivate::handleDomainUpdated() bool changed = false; int min = m_min + 0.5; - if(min>=0 && min= 0 && min < m_categories.count()) { QString minCategory = m_categories.at(min); - if(m_minCategory!=minCategory && !minCategory.isEmpty()) { - m_minCategory=minCategory; - changed=true; + if (m_minCategory != minCategory && !minCategory.isEmpty()) { + m_minCategory = minCategory; + changed = true; emit q->minChanged(minCategory); } } int max = m_max - 0.5; - if(max>=0 && max= 0 && max < m_categories.count()) { QString maxCategory = m_categories.at(max); - if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) { - m_maxCategory=maxCategory; + if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) { + m_maxCategory = maxCategory; emit q->maxChanged(maxCategory); } } - if (changed) { - emit q->rangeChanged(m_minCategory,m_maxCategory); - } + if (changed) + emit q->rangeChanged(m_minCategory, m_maxCategory); } ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QBarCategoryAxis); - if(m_orientation == Qt::Vertical){ - return new ChartBarCategoryAxisY(q,presenter); - }else{ - return new ChartBarCategoryAxisX(q,presenter); - } + if (m_orientation == Qt::Vertical) + return new ChartBarCategoryAxisY(q, presenter); + return new ChartBarCategoryAxisX(q, presenter); } void QBarCategoryAxisPrivate::intializeDomain(Domain* domain) { Q_Q(QBarCategoryAxis); - if(m_max==m_min) { + if (m_max == m_min) { int min; int max; - if(m_orientation==Qt::Vertical) { + if (m_orientation == Qt::Vertical) { min = domain->minY() + 0.5; max = domain->maxY() - 0.5; - } - else { + } else { min = domain->minX() + 0.5; max = domain->maxX() - 0.5; } - if(min>0 && min0 && maxsetRange(m_categories.at(min),m_categories.at(max)); - } - else { - if(m_orientation==Qt::Vertical) { + if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count()) + q->setRange(m_categories.at(min), m_categories.at(max)); + } else { + if (m_orientation == Qt::Vertical) domain->setRangeY(m_min, m_max); - } - else { + else domain->setRangeX(m_min, m_max); - } } } diff --git a/src/axis/barcategoryaxis/qbarcategoryaxis.h b/src/axis/barcategoryaxis/qbarcategoryaxis.h index e1c8e0f..9134dae 100644 --- a/src/axis/barcategoryaxis/qbarcategoryaxis.h +++ b/src/axis/barcategoryaxis/qbarcategoryaxis.h @@ -39,7 +39,7 @@ public: ~QBarCategoryAxis(); protected: - QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent = 0); + QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent = 0); public: AxisType type() const; @@ -63,9 +63,9 @@ public: Q_SIGNALS: void categoriesChanged(); - void minChanged(const QString &min); - void maxChanged(const QString &max); - void rangeChanged(const QString &min, const QString &max); + void minChanged(const QString &min); + void maxChanged(const QString &max); + void rangeChanged(const QString &min, const QString &max); private: Q_DECLARE_PRIVATE(QBarCategoryAxis) diff --git a/src/axis/barcategoryaxis/qbarcategoryaxis_p.h b/src/axis/barcategoryaxis/qbarcategoryaxis_p.h index a8bbc38..cb1357b 100644 --- a/src/axis/barcategoryaxis/qbarcategoryaxis_p.h +++ b/src/axis/barcategoryaxis/qbarcategoryaxis_p.h @@ -49,8 +49,8 @@ public: ChartAxis* createGraphics(ChartPresenter* presenter); void intializeDomain(Domain* domain); void handleDomainUpdated(); - qreal min() { return m_min;} - qreal max() { return m_max;} + qreal min() { return m_min; } + qreal max() { return m_max; } int count() const { return m_count; } private: diff --git a/src/axis/categoryaxis/chartcategoryaxisx.cpp b/src/axis/categoryaxis/chartcategoryaxisx.cpp index bd2c27a..b517171 100644 --- a/src/axis/categoryaxis/chartcategoryaxisx.cpp +++ b/src/axis/categoryaxis/chartcategoryaxisx.cpp @@ -30,7 +30,8 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) +ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter) { } @@ -69,7 +70,8 @@ void ChartCategoryAxisX::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; QCategoryAxis *categoryAxis = qobject_cast(m_chartAxis); QStringList ticksList = categoryAxis->categoriesLabels(); @@ -80,9 +82,8 @@ void ChartCategoryAxisX::updateGeometry() QList axis = m_arrow->childItems(); - for (int i = 0; i < labels.count(); i++) { + for (int i = 0; i < labels.count(); i++) labels.at(i)->setVisible(false); - } QRectF chartRect = presenter()->chartsGeometry(); // axis base line @@ -93,18 +94,16 @@ void ChartCategoryAxisX::updateGeometry() for (int i = 0; i < layout.size(); ++i) { QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - if (i < ticksList.count()) { + if (i < ticksList.count()) labelItem->setText(ticksList.at(i)); - } const QRectF& rect = labelItem->boundingRect(); QPointF center = rect.center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - if (i < layout.size() - 1) { + if (i < layout.size() - 1) labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), chartRect.bottom() + label_padding); - } else { + else labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding); - } // check if the label should be shown if (labelItem->pos().x() + center.x() < chartRect.left() || labelItem->pos().x() + center.x() > chartRect.right()) @@ -114,7 +113,7 @@ void ChartCategoryAxisX::updateGeometry() if ((i + 1) % 2 && i > 1) { QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); - rectItem->setRect(layout[i - 1],chartRect.top(),layout[i]-layout[i - 1],chartRect.height()); + rectItem->setRect(layout[i - 1], chartRect.top(), layout[i] - layout[i - 1], chartRect.height()); } // grid lines and axis line ticks @@ -122,7 +121,7 @@ void ChartCategoryAxisX::updateGeometry() lineItem->setPos(layout[i], chartRect.top()); lineItem->setLine(0, 0, 0, chartRect.height()); - QGraphicsLineItem *tickLineItem = static_cast(axis.at(i+1)); + QGraphicsLineItem *tickLineItem = static_cast(axis.at(i + 1)); tickLineItem->setPos(layout[i], chartRect.bottom()); tickLineItem->setLine(0, 0, 0, 5); @@ -134,9 +133,7 @@ void ChartCategoryAxisX::updateGeometry() lineItem->setVisible(true); tickLineItem->setVisible(true); } - } - } void ChartCategoryAxisX::handleAxisUpdated() @@ -153,31 +150,28 @@ QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint QSizeF sh; QSizeF base = ChartAxis::sizeHint(which, constraint); QStringList ticksList ; //TODO: - qreal width=0; - qreal height=0; + qreal width = 0; + qreal height = 0; switch (which) { - case Qt::MinimumSize: + case Qt::MinimumSize: width = fn.boundingRect("...").width(); height = fn.height() + label_padding; - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); break; - case Qt::PreferredSize: { - - for (int i = 0; i < ticksList.size(); ++i) - { - QRectF rect = fn.boundingRect(ticksList.at(i)); - width+=rect.width(); - height=qMax(rect.height()+label_padding,height); - } - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); - break; + case Qt::PreferredSize: + for (int i = 0; i < ticksList.size(); ++i) { + QRectF rect = fn.boundingRect(ticksList.at(i)); + width += rect.width(); + height = qMax(rect.height() + label_padding, height); } - default: + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); + break; + default: break; } diff --git a/src/axis/categoryaxis/chartcategoryaxisx_p.h b/src/axis/categoryaxis/chartcategoryaxisx_p.h index 4b380bc..9cc7618 100644 --- a/src/axis/categoryaxis/chartcategoryaxisx_p.h +++ b/src/axis/categoryaxis/chartcategoryaxisx_p.h @@ -43,7 +43,7 @@ public: ChartCategoryAxisX(QAbstractAxis *axis, ChartPresenter *presenter); ~ChartCategoryAxisX(); - AxisType axisType() const { return X_AXIS;} + AxisType axisType() const { return X_AXIS; } QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const; protected: QVector calculateLayout() const; diff --git a/src/axis/categoryaxis/chartcategoryaxisy.cpp b/src/axis/categoryaxis/chartcategoryaxisy.cpp index 538721a..d05a905 100644 --- a/src/axis/categoryaxis/chartcategoryaxisy.cpp +++ b/src/axis/categoryaxis/chartcategoryaxisy.cpp @@ -30,7 +30,8 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) +ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter) { } @@ -53,7 +54,7 @@ QVector ChartCategoryAxisY::calculateLayout() const if (range > 0) { points.resize(tickCount); qreal scale = rect.height() / range; - for (int i = 0; i < tickCount; ++i) + for (int i = 0; i < tickCount; ++i) { if (i < tickCount - 1) { int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + rect.bottom(); points[i] = y; @@ -61,6 +62,7 @@ QVector ChartCategoryAxisY::calculateLayout() const int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + rect.bottom(); points[i] = y; } + } } return points; @@ -70,9 +72,8 @@ void ChartCategoryAxisY::updateGeometry() { const QVector &layout = ChartAxis::layout(); - if(layout.isEmpty()) { + if (layout.isEmpty()) return; - } QCategoryAxis *categoryAxis = qobject_cast(m_chartAxis); QStringList ticksList = categoryAxis->categoriesLabels(); @@ -108,7 +109,7 @@ void ChartCategoryAxisY::updateGeometry() if (i < layout.size() - 1) labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y()); else - labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i]-center.y()); + labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] - center.y()); // check if the label should be shown if (labelItem->pos().y() + center.y() < chartRect.top() || labelItem->pos().y() + center.y() > chartRect.bottom()) @@ -116,9 +117,9 @@ void ChartCategoryAxisY::updateGeometry() else labelItem->setVisible(true); - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]); + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]); } // grid lines and axis line ticks @@ -126,7 +127,7 @@ void ChartCategoryAxisY::updateGeometry() lineItem->setPos(chartRect.left(), layout[i]); lineItem->setLine(0, 0, chartRect.width(), 0); - QGraphicsLineItem *tickLineItem = static_cast(axis.at(i+1)); + QGraphicsLineItem *tickLineItem = static_cast(axis.at(i + 1)); tickLineItem->setPos(chartRect.left(), layout[i]); tickLineItem->setLine(-5, 0, 0, 0); @@ -138,9 +139,7 @@ void ChartCategoryAxisY::updateGeometry() lineItem->setVisible(true); tickLineItem->setVisible(true); } - } - } void ChartCategoryAxisY::handleAxisUpdated() @@ -157,35 +156,32 @@ QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint QSizeF sh; QSizeF base = ChartAxis::sizeHint(which, constraint); QStringList ticksList; //TODO:: - qreal width=0; - qreal height=0; - - switch (which) { - case Qt::MinimumSize: - width = fn.boundingRect("...").width()+label_padding; - height = fn.height(); - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); - break; - case Qt::PreferredSize:{ - - for (int i = 0; i < ticksList.size(); ++i) - { - QRectF rect = fn.boundingRect(ticksList.at(i)); - height+=rect.height(); - width=qMax(rect.width()+label_padding,width); - } - height=qMax(height,base.height()); - width+=base.width(); - sh = QSizeF(width,height); - break; + qreal width = 0; + qreal height = 0; + + switch (which) { + case Qt::MinimumSize: + width = fn.boundingRect("...").width() + label_padding; + height = fn.height(); + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); + break; + case Qt::PreferredSize: + for (int i = 0; i < ticksList.size(); ++i) { + QRectF rect = fn.boundingRect(ticksList.at(i)); + height += rect.height(); + width = qMax(rect.width() + label_padding, width); } - default: - break; - } + height = qMax(height, base.height()); + width += base.width(); + sh = QSizeF(width, height); + break; + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/categoryaxis/chartcategoryaxisy_p.h b/src/axis/categoryaxis/chartcategoryaxisy_p.h index 41970b9..f63dd08 100644 --- a/src/axis/categoryaxis/chartcategoryaxisy_p.h +++ b/src/axis/categoryaxis/chartcategoryaxisy_p.h @@ -43,13 +43,13 @@ public: ChartCategoryAxisY(QAbstractAxis *axis, ChartPresenter *presenter); ~ChartCategoryAxisY(); - AxisType axisType() const { return Y_AXIS;} + AxisType axisType() const { return Y_AXIS; } QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const; protected: QVector calculateLayout() const; void updateGeometry(); -Q_SLOTS +public Q_SLOTS: void handleAxisUpdated(); }; diff --git a/src/axis/categoryaxis/qcategoryaxis.cpp b/src/axis/categoryaxis/qcategoryaxis.cpp index de382be..572d6a0 100644 --- a/src/axis/categoryaxis/qcategoryaxis.cpp +++ b/src/axis/categoryaxis/qcategoryaxis.cpp @@ -78,7 +78,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs an axis object which is a child of \a parent. */ QCategoryAxis::QCategoryAxis(QObject *parent): - QValueAxis(*new QCategoryAxisPrivate(this),parent) + QValueAxis(*new QCategoryAxisPrivate(this), parent) { } @@ -92,7 +92,7 @@ QCategoryAxis::~QCategoryAxis() /*! \internal */ -QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent):QValueAxis(d,parent) +QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d, QObject *parent): QValueAxis(d, parent) { } @@ -115,13 +115,12 @@ void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue) { Q_D(QCategoryAxis); - if (!d->m_categories.contains(categoryLabel)) - { - if(d->m_categories.isEmpty()){ + if (!d->m_categories.contains(categoryLabel)) { + if (d->m_categories.isEmpty()) { Range range(d->m_categoryMinimum, categoryEndValue); d->m_categoriesMap.insert(categoryLabel, range); d->m_categories.append(categoryLabel); - }else if (categoryEndValue > endValue(d->m_categories.last())){ + } else if (categoryEndValue > endValue(d->m_categories.last())) { Range previousRange = d->m_categoriesMap.value(d->m_categories.last()); d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue)); d->m_categories.append(categoryLabel); @@ -137,9 +136,9 @@ void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue) void QCategoryAxis::setStartValue(qreal min) { Q_D(QCategoryAxis); - if(d->m_categories.isEmpty()){ + if (d->m_categories.isEmpty()) { d->m_categoryMinimum = min; - }else{ + } else { Range range = d->m_categoriesMap.value(d->m_categories.first()); if (min < range.second) d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second)); @@ -154,8 +153,7 @@ qreal QCategoryAxis::startValue(const QString& categoryLabel) const Q_D(const QCategoryAxis); if (categoryLabel.isEmpty()) return d->m_categoryMinimum; - else - return d->m_categoriesMap.value(categoryLabel).first; + return d->m_categoriesMap.value(categoryLabel).first; } /*! @@ -224,7 +222,6 @@ void QCategoryAxis::replaceLabel(const QString& oldLabel, const QString& newLabe d->m_categoriesMap.insert(newLabel, range); d->emitUpdated(); } - } /*! @@ -272,7 +269,7 @@ int QCategoryAxisPrivate::ticksCount() const return m_categories.count() + 1; } -void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count) +void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max, int count) { Q_UNUSED(count); Q_UNUSED(min); @@ -282,11 +279,9 @@ void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QCategoryAxis); - if(m_orientation == Qt::Vertical){ - return new ChartCategoryAxisY(q,presenter); - }else{ - return new ChartCategoryAxisX(q,presenter); - } + if (m_orientation == Qt::Vertical) + return new ChartCategoryAxisY(q, presenter); + return new ChartCategoryAxisX(q, presenter); } #include "moc_qcategoryaxis.cpp" diff --git a/src/axis/categoryaxis/qcategoryaxis.h b/src/axis/categoryaxis/qcategoryaxis.h index 28ea4ba..259a0e0 100644 --- a/src/axis/categoryaxis/qcategoryaxis.h +++ b/src/axis/categoryaxis/qcategoryaxis.h @@ -38,7 +38,7 @@ public: ~QCategoryAxis(); protected: - QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent = 0); + QCategoryAxis(QCategoryAxisPrivate &d, QObject *parent = 0); public: AxisType type() const; diff --git a/src/axis/categoryaxis/qcategoryaxis_p.h b/src/axis/categoryaxis/qcategoryaxis_p.h index 7a9918c..f6fb787 100644 --- a/src/axis/categoryaxis/qcategoryaxis_p.h +++ b/src/axis/categoryaxis/qcategoryaxis_p.h @@ -49,10 +49,10 @@ public: int ticksCount() const; Q_SIGNALS: - void changed(qreal min, qreal max, int tickCount,bool niceNumbers); + void changed(qreal min, qreal max, int tickCount, bool niceNumbers); public Q_SLOTS: - void handleAxisRangeChanged(qreal min, qreal max,int count); + void handleAxisRangeChanged(qreal min, qreal max, int count); private: QMap m_categoriesMap; diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index bdf9b16..85dafc5 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -31,17 +31,18 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter), - m_chartAxis(axis), - m_labelsAngle(0), - m_grid(new QGraphicsItemGroup(presenter->rootItem())), - m_shades(new QGraphicsItemGroup(presenter->rootItem())), - m_labels(new QGraphicsItemGroup(presenter->rootItem())), - m_arrow(new QGraphicsItemGroup(presenter->rootItem())), - m_title(new QGraphicsSimpleTextItem(presenter->rootItem())), - m_min(0), - m_max(0), - m_animation(0) +ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartElement(presenter), + m_chartAxis(axis), + m_labelsAngle(0), + m_grid(new QGraphicsItemGroup(presenter->rootItem())), + m_shades(new QGraphicsItemGroup(presenter->rootItem())), + m_labels(new QGraphicsItemGroup(presenter->rootItem())), + m_arrow(new QGraphicsItemGroup(presenter->rootItem())), + m_title(new QGraphicsSimpleTextItem(presenter->rootItem())), + m_min(0), + m_max(0), + m_animation(0) { //initial initialization m_arrow->setZValue(ChartPresenter::AxisZValue); @@ -50,7 +51,7 @@ ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartEleme m_shades->setZValue(ChartPresenter::ShadesZValue); m_grid->setZValue(ChartPresenter::GridZValue); - QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated())); + QObject::connect(m_chartAxis->d_ptr.data(), SIGNAL(updated()), this, SLOT(handleAxisUpdated())); QGraphicsSimpleTextItem item; m_font = item.font(); @@ -63,23 +64,25 @@ ChartAxis::~ChartAxis() void ChartAxis::setAnimation(AxisAnimation* animation) { - m_animation=animation; + m_animation = animation; } void ChartAxis::setLayout(QVector &layout) { - m_layoutVector=layout; + m_layoutVector = layout; } void ChartAxis::createItems(int count) { if (m_arrow->children().size() == 0) - m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem())); + m_arrow->addToGroup(new AxisItem(this, presenter()->rootItem())); + for (int i = 0; i < count; ++i) { m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem())); m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); - if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem())); + if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2) + m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem())); } } @@ -91,7 +94,8 @@ void ChartAxis::deleteItems(int count) QList axis = m_arrow->childItems(); for (int i = 0; i < count; ++i) { - if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast()); + if (lines.size() % 2 && lines.size() > 1) + delete(shades.takeLast()); delete(lines.takeLast()); delete(labels.takeLast()); delete(axis.takeLast()); @@ -102,41 +106,39 @@ void ChartAxis::updateLayout(QVector &layout) { int diff = m_layoutVector.size() - layout.size(); - if (diff>0) { + if (diff > 0) deleteItems(diff); - } - else if (diff<0) { + else if (diff < 0) createItems(-diff); - } - if(diff<0) handleAxisUpdated(); + 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 { + 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 { setLayout(layout); updateGeometry(); } @@ -204,74 +206,67 @@ void ChartAxis::setShadesVisibility(bool visible) void ChartAxis::setLabelsAngle(int angle) { - foreach(QGraphicsItem* item , m_labels->childItems()) { - item->setRotation(angle); - } + foreach (QGraphicsItem *item , m_labels->childItems()) + item->setRotation(angle); - m_labelsAngle=angle; + m_labelsAngle = angle; } void ChartAxis::setLabelsPen(const QPen &pen) { - foreach(QGraphicsItem* item , m_labels->childItems()) { + foreach (QGraphicsItem *item , m_labels->childItems()) static_cast(item)->setPen(pen); - } } void ChartAxis::setLabelsBrush(const QBrush &brush) { - foreach(QGraphicsItem* item , m_labels->childItems()) { + foreach (QGraphicsItem *item , m_labels->childItems()) static_cast(item)->setBrush(brush); - } } void ChartAxis::setLabelsFont(const QFont &font) { - foreach(QGraphicsItem* item , m_labels->childItems()) { + foreach (QGraphicsItem *item , m_labels->childItems()) static_cast(item)->setFont(font); - } - if(m_font!=font) { + + if (m_font != font) { m_font = font; - foreach(QGraphicsItem* item , m_labels->childItems()) { + foreach (QGraphicsItem *item , m_labels->childItems()) static_cast(item)->setFont(font); - } QGraphicsLayoutItem::updateGeometry(); presenter()->layout()->invalidate(); - } } void ChartAxis::setShadesBrush(const QBrush &brush) { - foreach(QGraphicsItem* item , m_shades->childItems()) { + foreach (QGraphicsItem *item , m_shades->childItems()) static_cast(item)->setBrush(brush); - } } void ChartAxis::setShadesPen(const QPen &pen) { - foreach(QGraphicsItem* item , m_shades->childItems()) { + foreach (QGraphicsItem *item , m_shades->childItems()) static_cast(item)->setPen(pen); - } } void ChartAxis::setArrowPen(const QPen &pen) { - foreach(QGraphicsItem* item , m_arrow->childItems()) { - static_cast(item)->setPen(pen); - } + foreach (QGraphicsItem *item , m_arrow->childItems()) + static_cast(item)->setPen(pen); } void ChartAxis::setGridPen(const QPen &pen) { - foreach(QGraphicsItem* item , m_grid->childItems()) { + foreach (QGraphicsItem *item , m_grid->childItems()) static_cast(item)->setPen(pen); - } } bool ChartAxis::isEmpty() { - return !m_rect.isValid() || presenter()->chartsGeometry().isEmpty() || qFuzzyIsNull(m_min - m_max); + return !m_rect.isValid() || + presenter()->chartsGeometry().isEmpty() || + qFuzzyIsNull(m_min - m_max); } void ChartAxis::handleDomainUpdated() @@ -280,18 +275,15 @@ void ChartAxis::handleDomainUpdated() qreal min(0); qreal max(0); - if(m_chartAxis->orientation()==Qt::Horizontal) { + if (m_chartAxis->orientation() == Qt::Horizontal) { min = domain->minX(); max = domain->maxX(); - } - else if (m_chartAxis->orientation()==Qt::Vertical) - { + } else if (m_chartAxis->orientation() == Qt::Vertical) { min = domain->minY(); max = domain->maxY(); } - if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max)) - { + if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max)) { m_min = min; m_max = max; @@ -301,9 +293,9 @@ void ChartAxis::handleDomainUpdated() updateLayout(layout); QSizeF before = effectiveSizeHint(Qt::MinimumSize); - QSizeF after= sizeHint(Qt::MinimumSize); + QSizeF after = sizeHint(Qt::MinimumSize); - if(before!=after) { + if (before != after) { QGraphicsLayoutItem::updateGeometry(); //we don't want to call invalidate on layout, since it will change minimum size of component, //which we would like to avoid since it causes nasty flips when scrolling or zooming, @@ -316,8 +308,8 @@ void ChartAxis::handleDomainUpdated() void ChartAxis::handleAxisUpdated() { - if(isEmpty()) return; - + if (isEmpty()) + return; bool visible = m_chartAxis->isVisible(); @@ -338,7 +330,7 @@ void ChartAxis::handleAxisUpdated() void ChartAxis::setTitleText(const QString& title) { - if(m_titleText!=title) { + if (m_titleText != title) { m_titleText = title; m_rect = QRect(); QGraphicsLayoutItem::updateGeometry(); @@ -356,47 +348,44 @@ void ChartAxis::hide() void ChartAxis::setGeometry(const QRectF &rect) { + m_rect = rect; - m_rect = rect; + if (isEmpty()) + return; - if (isEmpty()) return; + if (!m_titleText.isNull()) { + QFontMetrics fn(m_title->font()); - if(!m_titleText.isNull()) { - QFontMetrics fn(m_title->font()); + int size(0); - int size(0); + QRectF chartRect = presenter()->chartsGeometry(); - QRectF chartRect = presenter()->chartsGeometry(); - - if(orientation()==Qt::Horizontal) + if (orientation() == Qt::Horizontal) size = chartRect.width(); - else if(orientation()==Qt::Vertical) + else if (orientation() == Qt::Vertical) size = chartRect.height(); - if (fn.boundingRect(m_titleText).width() > size) - { - QString string = m_titleText + "..."; - while (fn.boundingRect(string).width() > size && string.length() > 3) + if (fn.boundingRect(m_titleText).width() > size) { + QString string = m_titleText + "..."; + while (fn.boundingRect(string).width() > size && string.length() > 3) string.remove(string.length() - 4, 1); - m_title->setText(string); - } - else + m_title->setText(string); + } else { m_title->setText(m_titleText); - - QPointF center = chartRect.center() - m_title->boundingRect().center(); - if(orientation()==Qt::Horizontal) { - m_title->setPos(center.x(),m_rect.bottom()-m_title->boundingRect().height()); - } - else if(orientation()==Qt::Vertical) { - m_title->setTransformOriginPoint(m_title->boundingRect().center()); - m_title->setRotation(270); - m_title->setPos(m_rect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y()); - } } - QVector layout = calculateLayout(); - updateLayout(layout); + QPointF center = chartRect.center() - m_title->boundingRect().center(); + if (orientation() == Qt::Horizontal) { + m_title->setPos(center.x(), m_rect.bottom() - m_title->boundingRect().height()); + } else if (orientation() == Qt::Vertical) { + m_title->setTransformOriginPoint(m_title->boundingRect().center()); + m_title->setRotation(270); + m_title->setPos(m_rect.left() - m_title->boundingRect().width() / 2 + m_title->boundingRect().height() / 2, center.y()); + } + } + QVector layout = calculateLayout(); + updateLayout(layout); } void ChartAxis::setInternalRect(const QRectF &size) @@ -412,28 +401,27 @@ void ChartAxis::axisSelected() QStringList ChartAxis::createNumberLabels(qreal min, qreal max, int ticks) const { - Q_ASSERT(max>min); - Q_ASSERT(ticks>1); + Q_ASSERT(max > min); + Q_ASSERT(ticks > 1); QStringList labels; - int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0); + int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0); n++; QValueAxis *axis = qobject_cast(m_chartAxis); QString format = axis->labelFormat(); - if(format.isNull()) { - for (int i=0; i< ticks; i++) { - qreal value = min + (i * (max - min)/ (ticks-1)); - labels << QString::number(value,'f',n); + if (format.isNull()) { + for (int i = 0; i < ticks; i++) { + qreal value = min + (i * (max - min) / (ticks - 1)); + labels << QString::number(value, 'f', n); } - } - else { + } else { QByteArray array = format.toAscii(); - for (int i=0; i< ticks; i++) { - qreal value = min + (i * (max - min)/ (ticks-1)); + for (int i = 0; i < ticks; i++) { + qreal value = min + (i * (max - min) / (ticks - 1)); labels << QString().sprintf(array, value); } } @@ -463,29 +451,24 @@ QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const QFontMetrics fn(m_title->font()); QSizeF sh; - if(m_titleText.isNull()) return sh; - - switch(which) { - case Qt::MinimumSize: - if(orientation()==Qt::Horizontal) { - sh = QSizeF(fn.boundingRect ("...").width(),fn.height()); - } - else if(orientation()==Qt::Vertical) { - sh = QSizeF(fn.height(),fn.boundingRect ("...").width()); - } + if (m_titleText.isNull()) + return sh; + switch (which) { + case Qt::MinimumSize: + if (orientation() == Qt::Horizontal) + sh = QSizeF(fn.boundingRect("...").width(), fn.height()); + else if (orientation() == Qt::Vertical) + sh = QSizeF(fn.height(), fn.boundingRect("...").width()); break; - case Qt::MaximumSize: - case Qt::PreferredSize: - if(orientation()==Qt::Horizontal) { - sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(),fn.height()); - } - else if(orientation()==Qt::Vertical) { - sh = QSizeF(fn.height(),fn.boundingRect(m_chartAxis->title()).width()); - } - + case Qt::MaximumSize: + case Qt::PreferredSize: + if (orientation() == Qt::Horizontal) + sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(), fn.height()); + else if (orientation() == Qt::Vertical) + sh = QSizeF(fn.height(), fn.boundingRect(m_chartAxis->title()).width()); break; - default: + default: break; } diff --git a/src/axis/chartaxis_p.h b/src/axis/chartaxis_p.h index c88c695..69ad826 100644 --- a/src/axis/chartaxis_p.h +++ b/src/axis/chartaxis_p.h @@ -47,7 +47,7 @@ class ChartAxis : public ChartElement, public QGraphicsLayoutItem Q_OBJECT Q_INTERFACES(QGraphicsLayoutItem) public: - enum AxisType{ X_AXIS,Y_AXIS }; + enum AxisType { X_AXIS, Y_AXIS }; ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter); ~ChartAxis(); @@ -111,7 +111,7 @@ public: protected: virtual void updateGeometry() = 0; virtual QVector calculateLayout() const = 0; - QStringList createNumberLabels(qreal min, qreal max,int ticks) const; + QStringList createNumberLabels(qreal min, qreal max, int ticks) const; public Q_SLOTS: @@ -156,28 +156,24 @@ public: explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {} protected: - void mousePressEvent(QGraphicsSceneMouseEvent *event) - { - Q_UNUSED(event) - m_axis->axisSelected(); - } - - QRectF boundingRect() const - { - return shape().boundingRect(); - } - - QPainterPath shape() const - { - QPainterPath path = QGraphicsLineItem::shape(); - QRectF rect = path.boundingRect(); - path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0)); - return path; - } + void mousePressEvent(QGraphicsSceneMouseEvent *event) { + Q_UNUSED(event) + m_axis->axisSelected(); + } + + QRectF boundingRect() const { + return shape().boundingRect(); + } + + QPainterPath shape() const { + QPainterPath path = QGraphicsLineItem::shape(); + QRectF rect = path.boundingRect(); + path.addRect(rect.adjusted(0, 0, m_axis->axisType() != ChartAxis::X_AXIS ? 8 : 0, m_axis->axisType() != ChartAxis::Y_AXIS ? 8 : 0)); + return path; + } private: - ChartAxis* m_axis; - + ChartAxis* m_axis; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp index a6e441e..3f2f649 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp +++ b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp @@ -31,8 +31,9 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartDateTimeAxisX::ChartDateTimeAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), -m_tickCount(0) +ChartDateTimeAxisX::ChartDateTimeAxisX(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter), + m_tickCount(0) { } @@ -40,29 +41,29 @@ ChartDateTimeAxisX::~ChartDateTimeAxisX() { } -void ChartDateTimeAxisX::createLabels(QStringList &labels,qreal min, qreal max, int ticks) +void ChartDateTimeAxisX::createLabels(QStringList &labels, qreal min, qreal max, int ticks) { - Q_ASSERT(max>min); - Q_ASSERT(ticks>1); + Q_ASSERT(max > min); + Q_ASSERT(ticks > 1); QDateTimeAxis *axis = qobject_cast(m_chartAxis); - int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); + int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0); n++; - for (int i=0; i< ticks; i++) { - qreal value = min + (i * (max - min)/ (ticks-1)); + for (int i = 0; i < ticks; i++) { + qreal value = min + (i * (max - min) / (ticks - 1)); labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format()); } } QVector ChartDateTimeAxisX::calculateLayout() const { - Q_ASSERT(m_tickCount>=2); + Q_ASSERT(m_tickCount >= 2); QVector points; points.resize(m_tickCount); QRectF rect = presenter()->chartsGeometry(); - const qreal deltaX = rect.width()/(m_tickCount-1); + const qreal deltaX = rect.width() / (m_tickCount - 1); for (int i = 0; i < m_tickCount; ++i) { int x = i * deltaX + rect.left(); points[i] = x; @@ -74,11 +75,12 @@ void ChartDateTimeAxisX::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; QStringList ticksList; - createLabels(ticksList,m_min,m_max,layout.size()); + createLabels(ticksList, m_min, m_max, layout.size()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -98,27 +100,27 @@ void ChartDateTimeAxisX::updateGeometry() QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(layout[i], chartRect.top(), layout[i], chartRect.bottom()); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - labelItem->setText(ticksList.at(i)); - const QRectF& rect = labelItem->boundingRect(); - QPointF center = rect.center(); - labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding); - - if(labelItem->pos().x()<=width){ - labelItem->setVisible(false); - lineItem->setVisible(false); - }else{ - labelItem->setVisible(true); - lineItem->setVisible(true); - width=rect.width()+labelItem->pos().x(); - } - - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect(layout[i-1],chartRect.top(),layout[i]-layout[i-1],chartRect.height()); + labelItem->setText(ticksList.at(i)); + const QRectF& rect = labelItem->boundingRect(); + QPointF center = rect.center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding); + + if (labelItem->pos().x() <= width) { + labelItem->setVisible(false); + lineItem->setVisible(false); + } else { + labelItem->setVisible(true); + lineItem->setVisible(true); + width = rect.width() + labelItem->pos().x(); } - lineItem = static_cast(axis.at(i+1)); - lineItem->setLine(layout[i],chartRect.bottom(),layout[i],chartRect.bottom()+5); + + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(layout[i - 1], chartRect.top(), layout[i] - layout[i - 1], chartRect.height()); + } + lineItem = static_cast(axis.at(i + 1)); + lineItem->setLine(layout[i], chartRect.bottom(), layout[i], chartRect.bottom() + 5); } } @@ -137,34 +139,30 @@ QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint QFontMetrics fn(m_font); QSizeF sh; - switch (which) { - case Qt::MinimumSize: - sh = QSizeF(fn.boundingRect("...").width(),fn.height()); - break; - case Qt::PreferredSize:{ - - const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) break; - QStringList ticksList; - - - qreal width=0; - qreal height=0; - - for (int i = 0; i < ticksList.size(); ++i) - { - QRectF rect = fn.boundingRect(ticksList.at(i)); - width+=rect.width(); - height+=qMax(rect.height()+label_padding,height); - } - sh = QSizeF(width,height); + switch (which) { + case Qt::MinimumSize: + sh = QSizeF(fn.boundingRect("...").width(), fn.height()); + break; + case Qt::PreferredSize: { + const QVector& layout = ChartAxis::layout(); + if (layout.isEmpty()) break; + QStringList ticksList; + qreal width = 0; + qreal height = 0; + for (int i = 0; i < ticksList.size(); ++i) { + QRectF rect = fn.boundingRect(ticksList.at(i)); + width += rect.width(); + height += qMax(rect.height() + label_padding, height); } - default: - break; - } + sh = QSizeF(width, height); + break; + } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx_p.h b/src/axis/datetimeaxis/chartdatetimeaxisx_p.h index 0e4f170..6afa9fe 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisx_p.h +++ b/src/axis/datetimeaxis/chartdatetimeaxisx_p.h @@ -46,14 +46,13 @@ public: AxisType axisType() const { return X_AXIS;} QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const; protected: - void createLabels(QStringList &labels,qreal min, qreal max, int ticks); + void createLabels(QStringList &labels, qreal min, qreal max, int ticks); void handleAxisUpdated(); QVector calculateLayout() const; void updateGeometry(); private: int m_tickCount; - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp index d92efcc..8164b8d 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp +++ b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp @@ -31,8 +31,9 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartDateTimeAxisY::ChartDateTimeAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), -m_tickCount(0) +ChartDateTimeAxisY::ChartDateTimeAxisY(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter), + m_tickCount(0) { } @@ -40,29 +41,29 @@ ChartDateTimeAxisY::~ChartDateTimeAxisY() { } -void ChartDateTimeAxisY::createLabels(QStringList &labels,qreal min, qreal max, int ticks) +void ChartDateTimeAxisY::createLabels(QStringList &labels, qreal min, qreal max, int ticks) { - Q_ASSERT(max>min); - Q_ASSERT(ticks>1); + Q_ASSERT(max > min); + Q_ASSERT(ticks > 1); QDateTimeAxis *axis = qobject_cast(m_chartAxis); - int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); + int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0); n++; - for (int i=0; i< ticks; i++) { - qreal value = min + (i * (max - min)/ (ticks-1)); + for (int i = 0; i < ticks; i++) { + qreal value = min + (i * (max - min) / (ticks - 1)); labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format()); } } QVector ChartDateTimeAxisY::calculateLayout() const { - Q_ASSERT(m_tickCount>=2); + Q_ASSERT(m_tickCount >= 2); QVector points; points.resize(m_tickCount); QRectF rect = presenter()->chartsGeometry(); - const qreal deltaY = rect.height()/(m_tickCount-1); + const qreal deltaY = rect.height() / (m_tickCount - 1); for (int i = 0; i < m_tickCount; ++i) { int y = i * -deltaY + rect.bottom(); points[i] = y; @@ -75,11 +76,12 @@ void ChartDateTimeAxisY::updateGeometry() { const QVector &layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; QStringList ticksList; - createLabels(ticksList,m_min,m_max,layout.size()); + createLabels(ticksList, m_min, m_max, layout.size()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -101,29 +103,28 @@ void ChartDateTimeAxisY::updateGeometry() lineItem->setLine(chartRect.left() , layout[i], chartRect.right(), layout[i]); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - labelItem->setText(ticksList.at(i)); - const QRectF& rect = labelItem->boundingRect(); - - QPointF center = rect.center(); - labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i]-center.y()); - - if(labelItem->pos().y()+rect.height()>height) { - labelItem->setVisible(false); - lineItem->setVisible(false); - } - else { - labelItem->setVisible(true); - lineItem->setVisible(true); - height=labelItem->pos().y(); - } - - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]); + labelItem->setText(ticksList.at(i)); + const QRectF& rect = labelItem->boundingRect(); + + QPointF center = rect.center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] - center.y()); + + if (labelItem->pos().y() + rect.height() > height) { + labelItem->setVisible(false); + lineItem->setVisible(false); + } else { + labelItem->setVisible(true); + lineItem->setVisible(true); + height = labelItem->pos().y(); + } + + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]); } - lineItem = static_cast(axis.at(i+1)); - lineItem->setLine(chartRect.left()-5,layout[i],chartRect.left(),layout[i]); + lineItem = static_cast(axis.at(i + 1)); + lineItem->setLine(chartRect.left() - 5, layout[i], chartRect.left(), layout[i]); } } @@ -142,34 +143,30 @@ QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint QFontMetrics fn(m_font); QSizeF sh; - switch (which) { - case Qt::MinimumSize: - sh = QSizeF(fn.boundingRect("...").width(),fn.height()); - break; - case Qt::PreferredSize:{ - - const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) break; - QStringList ticksList; - - - qreal width=0; - qreal height=0; - - for (int i = 0; i < ticksList.size(); ++i) - { - QRectF rect = fn.boundingRect(ticksList.at(i)); - width+=rect.width(); - height+=qMax(rect.height()+label_padding,height); - } - sh = QSizeF(width,height); + switch (which) { + case Qt::MinimumSize: + sh = QSizeF(fn.boundingRect("...").width(), fn.height()); + break; + case Qt::PreferredSize: { + const QVector& layout = ChartAxis::layout(); + if (layout.isEmpty()) break; + QStringList ticksList; + qreal width = 0; + qreal height = 0; + for (int i = 0; i < ticksList.size(); ++i) { + QRectF rect = fn.boundingRect(ticksList.at(i)); + width += rect.width(); + height += qMax(rect.height() + label_padding, height); } - default: - break; - } + sh = QSizeF(width, height); + break; + } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy_p.h b/src/axis/datetimeaxis/chartdatetimeaxisy_p.h index 4e3d594..ea61247 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisy_p.h +++ b/src/axis/datetimeaxis/chartdatetimeaxisy_p.h @@ -46,7 +46,7 @@ public: AxisType axisType() const { return Y_AXIS;} QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const; protected: - void createLabels(QStringList &labels,qreal min, qreal max, int ticks); + void createLabels(QStringList &labels, qreal min, qreal max, int ticks); QVector calculateLayout() const; void updateGeometry(); void handleAxisUpdated(); diff --git a/src/axis/datetimeaxis/qdatetimeaxis.cpp b/src/axis/datetimeaxis/qdatetimeaxis.cpp index 46eef54..754a241 100644 --- a/src/axis/datetimeaxis/qdatetimeaxis.cpp +++ b/src/axis/datetimeaxis/qdatetimeaxis.cpp @@ -59,7 +59,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Adding the series to the chart and setting up the QDateTimeAxis. \code - QChartView *chartView = new QChartView; + QChartView *chartView = new QChartView; chartView->chart()->addSeries(series); // ... @@ -158,7 +158,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs an axis object which is a child of \a parent. */ QDateTimeAxis::QDateTimeAxis(QObject *parent) : - QAbstractAxis(*new QDateTimeAxisPrivate(this),parent) + QAbstractAxis(*new QDateTimeAxisPrivate(this), parent) { } @@ -166,7 +166,7 @@ QDateTimeAxis::QDateTimeAxis(QObject *parent) : /*! \internal */ -QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent) +QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d, QObject *parent) : QAbstractAxis(d, parent) { } @@ -229,7 +229,7 @@ void QDateTimeAxis::setRange(QDateTime min, QDateTime max) } if (changed) { - emit rangeChanged(d->m_min,d->m_max); + emit rangeChanged(d->m_min, d->m_max); d->emitUpdated(); } } @@ -253,9 +253,9 @@ QString QDateTimeAxis::format() const Sets \a count for ticks on the axis. */ void QDateTimeAxis::setTickCount(int count) -{ +{ Q_D(QDateTimeAxis); - if (d->m_tickCount != count && count >=2) { + if (d->m_tickCount != count && count >= 2) { d->m_tickCount = count; d->emitUpdated(); } @@ -301,11 +301,10 @@ void QDateTimeAxisPrivate::handleDomainUpdated() Domain* domain = qobject_cast(sender()); Q_ASSERT(domain); - if(orientation()==Qt::Horizontal){ + if (orientation() == Qt::Horizontal) q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX())); - }else if(orientation()==Qt::Vertical){ + else if (orientation() == Qt::Vertical) q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY())); - } } @@ -334,29 +333,24 @@ void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max) ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QDateTimeAxis); - if(m_orientation == Qt::Vertical){ - return new ChartDateTimeAxisY(q,presenter); - }else{ - return new ChartDateTimeAxisX(q,presenter); - } - + if (m_orientation == Qt::Vertical) + return new ChartDateTimeAxisY(q, presenter); + return new ChartDateTimeAxisX(q, presenter); } void QDateTimeAxisPrivate::intializeDomain(Domain* domain) { Q_Q(QDateTimeAxis); - if(m_max == m_min) { - if(m_orientation==Qt::Vertical){ + if (m_max == m_min) { + if (m_orientation == Qt::Vertical) q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY())); - }else{ + else q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX())); - } } else { - if(m_orientation==Qt::Vertical){ + if (m_orientation == Qt::Vertical) domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch()); - }else{ + else domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch()); - } } } diff --git a/src/axis/datetimeaxis/qdatetimeaxis.h b/src/axis/datetimeaxis/qdatetimeaxis.h index fbf4557..3124950 100644 --- a/src/axis/datetimeaxis/qdatetimeaxis.h +++ b/src/axis/datetimeaxis/qdatetimeaxis.h @@ -42,7 +42,7 @@ public: ~QDateTimeAxis(); protected: - QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent = 0); + QDateTimeAxis(QDateTimeAxisPrivate &d, QObject *parent = 0); public: AxisType type() const; diff --git a/src/axis/datetimeaxis/qdatetimeaxis_p.h b/src/axis/datetimeaxis/qdatetimeaxis_p.h index 491c580..a8d5298 100644 --- a/src/axis/datetimeaxis/qdatetimeaxis_p.h +++ b/src/axis/datetimeaxis/qdatetimeaxis_p.h @@ -39,11 +39,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QDateTimeAxisPrivate : public QAbstractAxisPrivate { Q_OBJECT - public: +public: QDateTimeAxisPrivate(QDateTimeAxis *q); ~QDateTimeAxisPrivate(); - public: +public: ChartAxis* createGraphics(ChartPresenter* presenter); void intializeDomain(Domain* domain); void handleDomainUpdated(); @@ -51,13 +51,13 @@ class QDateTimeAxisPrivate : public QAbstractAxisPrivate qreal max(){ return m_max.toMSecsSinceEpoch(); } int count() const { /*TODO:*/ return 0;} - protected: +protected: void setMin(const QVariant &min); void setMax(const QVariant &max); void setRange(const QVariant &min, const QVariant &max); int tickCount() const; - protected: +protected: QDateTime m_min; QDateTime m_max; int m_tickCount; diff --git a/src/axis/qabstractaxis.cpp b/src/axis/qabstractaxis.cpp index e613322..98cf9e4 100644 --- a/src/axis/qabstractaxis.cpp +++ b/src/axis/qabstractaxis.cpp @@ -260,9 +260,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChart when axis added. */ -QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) : -QObject(parent), -d_ptr(&d) +QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) + : QObject(parent), + d_ptr(&d) { } @@ -272,7 +272,8 @@ d_ptr(&d) QAbstractAxis::~QAbstractAxis() { - if(d_ptr->m_dataset) qFatal("Still binded axis detected !"); + if (d_ptr->m_dataset) + qFatal("Still binded axis detected !"); } /*! @@ -280,10 +281,10 @@ QAbstractAxis::~QAbstractAxis() */ void QAbstractAxis::setLinePen(const QPen &pen) { - if (d_ptr->m_axisPen!=pen) { - d_ptr->m_axisPen = pen; - d_ptr->emitUpdated(); - } + if (d_ptr->m_axisPen != pen) { + d_ptr->m_axisPen = pen; + d_ptr->emitUpdated(); + } } /*! @@ -318,7 +319,7 @@ void QAbstractAxis::setLineVisible(bool visible) d_ptr->m_arrowVisible = visible; d_ptr->emitUpdated(); emit lineVisibleChanged(visible); - } + } } bool QAbstractAxis::isLineVisible() const @@ -332,7 +333,7 @@ void QAbstractAxis::setGridLineVisible(bool visible) d_ptr->m_gridLineVisible = visible; d_ptr->emitUpdated(); emit gridVisibleChanged(visible); - } + } } bool QAbstractAxis::isGridLineVisible() const @@ -348,7 +349,7 @@ void QAbstractAxis::setGridLinePen(const QPen &pen) if (d_ptr->m_gridLinePen != pen) { d_ptr->m_gridLinePen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -365,7 +366,7 @@ void QAbstractAxis::setLabelsVisible(bool visible) d_ptr->m_labelsVisible = visible; d_ptr->emitUpdated(); emit labelsVisibleChanged(visible); - } + } } bool QAbstractAxis::labelsVisible() const @@ -381,7 +382,7 @@ void QAbstractAxis::setLabelsPen(const QPen &pen) if (d_ptr->m_labelsPen != pen) { d_ptr->m_labelsPen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -400,7 +401,7 @@ void QAbstractAxis::setLabelsBrush(const QBrush &brush) if (d_ptr->m_labelsBrush != brush) { d_ptr->m_labelsBrush = brush; d_ptr->emitUpdated(); - } + } } /*! @@ -419,7 +420,7 @@ void QAbstractAxis::setLabelsFont(const QFont &font) if (d_ptr->m_labelsFont != font) { d_ptr->m_labelsFont = font; d_ptr->emitUpdated(); - } + } } /*! @@ -435,7 +436,7 @@ void QAbstractAxis::setLabelsAngle(int angle) if (d_ptr->m_labelsAngle != angle) { d_ptr->m_labelsAngle = angle; d_ptr->emitUpdated(); - } + } } int QAbstractAxis::labelsAngle() const @@ -463,7 +464,7 @@ void QAbstractAxis::setTitleVisible(bool visible) if (d_ptr->m_titleVisible != visible) { d_ptr->m_titleVisible = visible; d_ptr->emitUpdated(); - } + } } bool QAbstractAxis::titleVisible() const @@ -479,7 +480,7 @@ void QAbstractAxis::setTitlePen(const QPen &pen) if (d_ptr->m_titlePen != pen) { d_ptr->m_titlePen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -498,7 +499,7 @@ void QAbstractAxis::setTitleBrush(const QBrush &brush) if (d_ptr->m_titleBrush != brush) { d_ptr->m_titleBrush = brush; d_ptr->emitUpdated(); - } + } } /*! @@ -517,7 +518,7 @@ void QAbstractAxis::setTitleFont(const QFont &font) if (d_ptr->m_titleFont != font) { d_ptr->m_titleFont = font; d_ptr->emitUpdated(); - } + } } /*! @@ -530,15 +531,15 @@ QFont QAbstractAxis::titleFont() const void QAbstractAxis::setTitle(const QString& title) { - if (d_ptr->m_title != title) { - d_ptr->m_title = title; - d_ptr->emitUpdated(); - } + if (d_ptr->m_title != title) { + d_ptr->m_title = title; + d_ptr->emitUpdated(); + } } QString QAbstractAxis::title() const { - return d_ptr->m_title; + return d_ptr->m_title; } @@ -548,7 +549,7 @@ void QAbstractAxis::setShadesVisible(bool visible) d_ptr->m_shadesVisible = visible; d_ptr->emitUpdated(); emit shadesVisibleChanged(visible); - } + } } bool QAbstractAxis::shadesVisible() const @@ -564,7 +565,7 @@ void QAbstractAxis::setShadesPen(const QPen &pen) if (d_ptr->m_shadesPen != pen) { d_ptr->m_shadesPen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -630,8 +631,8 @@ bool QAbstractAxis::isVisible() const */ void QAbstractAxis::setVisible(bool visible) { - if(d_ptr->m_visible!=visible){ - d_ptr->m_visible=visible; + if (d_ptr->m_visible != visible) { + d_ptr->m_visible = visible; d_ptr->emitUpdated(); emit visibleChanged(visible); } @@ -681,7 +682,7 @@ void QAbstractAxis::setMax(const QVariant &max) */ void QAbstractAxis::setRange(const QVariant &min, const QVariant &max) { - d_ptr->setRange(min,max); + d_ptr->setRange(min, max); } @@ -730,20 +731,20 @@ QAbstractAxisPrivate::~QAbstractAxisPrivate() void QAbstractAxisPrivate::emitUpdated() { - if(!m_dirty){ - m_dirty=true; + if (!m_dirty) { + m_dirty = true; emit updated(); } } void QAbstractAxisPrivate::setDirty(bool dirty) { - m_dirty=dirty; + m_dirty = dirty; } void QAbstractAxisPrivate::setOrientation(Qt::Orientation orientation) { - m_orientation=orientation; + m_orientation = orientation; } diff --git a/src/axis/qabstractaxis.h b/src/axis/qabstractaxis.h index fe166c9..24b8894 100644 --- a/src/axis/qabstractaxis.h +++ b/src/axis/qabstractaxis.h @@ -58,7 +58,7 @@ public: Q_DECLARE_FLAGS(AxisTypes, AxisType) protected: - explicit QAbstractAxis(QAbstractAxisPrivate &d,QObject *parent = 0); + explicit QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent = 0); public: ~QAbstractAxis(); diff --git a/src/axis/valueaxis/chartvalueaxisx.cpp b/src/axis/valueaxis/chartvalueaxisx.cpp index fe2a916..16c29d9 100644 --- a/src/axis/valueaxis/chartvalueaxisx.cpp +++ b/src/axis/valueaxis/chartvalueaxisx.cpp @@ -31,8 +31,9 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), - m_tickCount(0) +ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter), + m_tickCount(0) { } @@ -42,14 +43,14 @@ ChartValueAxisX::~ChartValueAxisX() QVector ChartValueAxisX::calculateLayout() const { - Q_ASSERT(m_tickCount>=2); + Q_ASSERT(m_tickCount >= 2); QVector points; points.resize(m_tickCount); QRectF rect = presenter()->chartsGeometry(); - const qreal deltaX = rect.width()/(m_tickCount-1); + const qreal deltaX = rect.width() / (m_tickCount - 1); for (int i = 0; i < m_tickCount; ++i) { int x = i * deltaX + rect.left(); points[i] = x; @@ -61,9 +62,10 @@ void ChartValueAxisX::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; - QStringList ticksList = createNumberLabels(m_min,m_max,layout.size()); + QStringList ticksList = createNumberLabels(m_min, m_max, layout.size()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -96,27 +98,27 @@ void ChartValueAxisX::updateGeometry() labelItem->setPos(layout[i] - center.x(), m_internalRect.top() - rect.height() - label_padding); else labelItem->setPos(layout[i] - center.x(), m_internalRect.bottom() + label_padding); - if(labelItem->pos().x() <= width || - labelItem->pos().x() < m_rect.left() || - labelItem->pos().x() + rect.width() > m_rect.right()){ + if (labelItem->pos().x() <= width || + labelItem->pos().x() < m_rect.left() || + labelItem->pos().x() + rect.width() > m_rect.right()){ labelItem->setVisible(false); lineItem->setVisible(false); - }else{ + } else { labelItem->setVisible(true); lineItem->setVisible(true); - width=rect.width()+labelItem->pos().x(); + width = rect.width() + labelItem->pos().x(); } - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect(layout[i-1],chartRrect.top(),layout[i]-layout[i-1],chartRrect.height()); + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(layout[i - 1], chartRrect.top(), layout[i] - layout[i - 1], chartRrect.height()); } - lineItem = static_cast(axis.at(i+1)); + lineItem = static_cast(axis.at(i + 1)); // lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5); if (m_chartAxis->alternativePlacement()) - lineItem->setLine(layout[i],m_internalRect.top(),layout[i],m_internalRect.top()-5); + lineItem->setLine(layout[i], m_internalRect.top(), layout[i], m_internalRect.top() - 5); else - lineItem->setLine(layout[i],m_internalRect.bottom(),layout[i],m_internalRect.bottom()+5); + lineItem->setLine(layout[i], m_internalRect.bottom(), layout[i], m_internalRect.bottom() + 5); } } @@ -136,32 +138,28 @@ QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c QSizeF sh; QSizeF base = ChartAxis::sizeHint(which, constraint); - QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount); - qreal width=0; - qreal height=0; + QStringList ticksList = createNumberLabels(m_min, m_max, m_tickCount); + qreal width = 0; + qreal height = 0; switch (which) { - case Qt::MinimumSize:{ - int count = qMax(ticksList.last().count(),ticksList.first().count()); - width=fn.averageCharWidth()*count; - height=fn.height()+label_padding; - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); + case Qt::MinimumSize: { + int count = qMax(ticksList.last().count(), ticksList.first().count()); + width = fn.averageCharWidth() * count; + height = fn.height() + label_padding; + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); break; - } - case Qt::PreferredSize:{ - for (int i = 0; i < ticksList.size(); ++i) - { - width+=fn.averageCharWidth()*ticksList.at(i).count(); - } - height=fn.height()+label_padding; - width=qMax(width,base.width()); - height+=base.height(); - sh = QSizeF(width,height); + case Qt::PreferredSize: + for (int i = 0; i < ticksList.size(); ++i) + width += fn.averageCharWidth() * ticksList.at(i).count(); + height = fn.height() + label_padding; + width = qMax(width, base.width()); + height += base.height(); + sh = QSizeF(width, height); break; - } default: break; } diff --git a/src/axis/valueaxis/chartvalueaxisy.cpp b/src/axis/valueaxis/chartvalueaxisy.cpp index bf78459..83e04bd 100644 --- a/src/axis/valueaxis/chartvalueaxisy.cpp +++ b/src/axis/valueaxis/chartvalueaxisy.cpp @@ -31,8 +31,9 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), - m_tickCount(0) +ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis, ChartPresenter *presenter) + : ChartAxis(axis, presenter), + m_tickCount(0) { } @@ -42,14 +43,14 @@ ChartValueAxisY::~ChartValueAxisY() QVector ChartValueAxisY::calculateLayout() const { - Q_ASSERT(m_tickCount>=2); + Q_ASSERT(m_tickCount >= 2); QVector points; points.resize(m_tickCount); QRectF rect = presenter()->chartsGeometry(); - const qreal deltaY = rect.height()/(m_tickCount-1); + const qreal deltaY = rect.height() / (m_tickCount - 1); for (int i = 0; i < m_tickCount; ++i) { int y = i * -deltaY + rect.bottom(); points[i] = y; @@ -62,9 +63,10 @@ void ChartValueAxisY::updateGeometry() { const QVector &layout = ChartAxis::layout(); - if(layout.isEmpty()) return; + if (layout.isEmpty()) + return; - QStringList ticksList = createNumberLabels(m_min,m_max,layout.size()); + QStringList ticksList = createNumberLabels(m_min, m_max, layout.size()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -81,26 +83,25 @@ void ChartValueAxisY::updateGeometry() QGraphicsLineItem *lineItem = static_cast(axis.at(0)); // lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); if (m_chartAxis->alternativePlacement()) - lineItem->setLine( m_internalRect.right() , chartRect.top(), m_internalRect.right(), chartRect.bottom()); + lineItem->setLine(m_internalRect.right(), chartRect.top(), m_internalRect.right(), chartRect.bottom()); else - lineItem->setLine( m_internalRect.left() , chartRect.top(), m_internalRect.left(), chartRect.bottom()); + lineItem->setLine(m_internalRect.left(), chartRect.top(), m_internalRect.left(), chartRect.bottom()); QFontMetrics fn(m_font); for (int i = 0; i < layout.size(); ++i) { QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setLine( chartRect.left() , layout[i], chartRect.right(), layout[i]); + lineItem->setLine(chartRect.left(), layout[i], chartRect.right(), layout[i]); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); QString text = ticksList.at(i); - if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding ) - { + if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding) { QString label = text + "..."; while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3) label.remove(label.length() - 4, 1); labelItem->setText(label); - }else{ + } else { labelItem->setText(text); } @@ -110,30 +111,30 @@ void ChartValueAxisY::updateGeometry() labelItem->setTransformOriginPoint(center.x(), center.y()); // labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y()); if (m_chartAxis->alternativePlacement()) - labelItem->setPos( m_internalRect.right() + label_padding , layout[i]-center.y()); + labelItem->setPos(m_internalRect.right() + label_padding , layout[i] - center.y()); else - labelItem->setPos( m_internalRect.left() - rect.width() - label_padding , layout[i]-center.y()); + labelItem->setPos(m_internalRect.left() - rect.width() - label_padding , layout[i] - center.y()); - if(labelItem->pos().y() + rect.height() > height || - labelItem->pos().y() < m_rect.top()) { + if (labelItem->pos().y() + rect.height() > height || + labelItem->pos().y() < m_rect.top()) { labelItem->setVisible(false); lineItem->setVisible(false); - }else{ + } else { labelItem->setVisible(true); lineItem->setVisible(true); - height=labelItem->pos().y(); + height = labelItem->pos().y(); } - if ((i+1)%2 && i>1) { - QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); - rectItem->setRect( chartRect.left(),layout[i], chartRect.width(),layout[i-1]-layout[i]); + if ((i + 1) % 2 && i > 1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(chartRect.left(), layout[i], chartRect.width(), layout[i - 1] - layout[i]); } - lineItem = static_cast(axis.at(i+1)); + lineItem = static_cast(axis.at(i + 1)); // lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]); if (m_chartAxis->alternativePlacement()) - lineItem->setLine( m_internalRect.right()+5,layout[i], m_internalRect.right(),layout[i]); + lineItem->setLine(m_internalRect.right() + 5, layout[i], m_internalRect.right(), layout[i]); else - lineItem->setLine( m_internalRect.left()-5,layout[i], m_internalRect.left(),layout[i]); + lineItem->setLine(m_internalRect.left() - 5, layout[i], m_internalRect.left(), layout[i]); } } @@ -152,32 +153,29 @@ QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c QFontMetrics fn(m_font); QSizeF sh; QSizeF base = ChartAxis::sizeHint(which, constraint); - QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount); - qreal width=0; - qreal height=0; + QStringList ticksList = createNumberLabels(m_min, m_max, m_tickCount); + qreal width = 0; + qreal height = 0; switch (which) { case Qt::MinimumSize: { int count = qMax(ticksList.first().count() , ticksList.last().count()); - width=fn.averageCharWidth()*count+label_padding; - height=fn.height(); - height=qMax(height,base.height()); - width+=base.width(); - sh = QSizeF(width,height); + width = fn.averageCharWidth() * count + label_padding; + height = fn.height(); + height = qMax(height, base.height()); + width += base.width(); + sh = QSizeF(width, height); break; - } + } case Qt::PreferredSize: - { - for (int i = 0; i < ticksList.size(); ++i) - { - width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+label_padding,width); - height+=fn.height(); + for (int i = 0; i < ticksList.size(); ++i) { + width = qMax(qreal(fn.averageCharWidth() * ticksList.at(i).count()) + label_padding, width); + height += fn.height(); } - height=qMax(height,base.height()); - width+=base.width(); - sh = QSizeF(width,height); + height = qMax(height, base.height()); + width += base.width(); + sh = QSizeF(width, height); break; - } default: break; } diff --git a/src/axis/valueaxis/qvalueaxis.cpp b/src/axis/valueaxis/qvalueaxis.cpp index 9d04784..5435a70 100644 --- a/src/axis/valueaxis/qvalueaxis.cpp +++ b/src/axis/valueaxis/qvalueaxis.cpp @@ -152,7 +152,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs an axis object which is a child of \a parent. */ QValueAxis::QValueAxis(QObject *parent) : - QAbstractAxis(*new QValueAxisPrivate(this),parent) + QAbstractAxis(*new QValueAxisPrivate(this), parent) { } @@ -160,7 +160,8 @@ QValueAxis::QValueAxis(QObject *parent) : /*! \internal */ -QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent) +QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent) + : QAbstractAxis(d, parent) { } @@ -171,9 +172,8 @@ QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,p QValueAxis::~QValueAxis() { Q_D(QValueAxis); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeAxis(this); - } } void QValueAxis::setMin(qreal min) @@ -209,12 +209,14 @@ void QValueAxis::setRange(qreal min, qreal max) Q_D(QValueAxis); bool changed = false; - if (min > max) return; + if (min > max) + return; - if(d->m_niceNumbers) { + if (d->m_niceNumbers) { int ticks = d->m_tickCount; d->looseNiceNumbers(min, max, ticks); - if(ticks!=d->m_tickCount) setTickCount(ticks); + if (ticks != d->m_tickCount) + setTickCount(ticks); } if (!qFuzzyIsNull(d->m_min - min)) { @@ -230,7 +232,7 @@ void QValueAxis::setRange(qreal min, qreal max) } if (changed) { - emit rangeChanged(min,max); + emit rangeChanged(min, max); d->emitUpdated(); } } @@ -241,7 +243,7 @@ void QValueAxis::setRange(qreal min, qreal max) void QValueAxis::setTickCount(int count) { Q_D(QValueAxis); - if (d->m_tickCount != count && count >=2) { + if (d->m_tickCount != count && count >= 2) { d->m_tickCount = count; d->emitUpdated(); } @@ -260,11 +262,10 @@ int QValueAxis::tickCount() const void QValueAxis::setNiceNumbersEnabled(bool enable) { Q_D(QValueAxis); - if (d->m_niceNumbers != enable){ + if (d->m_niceNumbers != enable) { d->m_niceNumbers = enable; - if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) { - setRange(d->m_min,d->m_max); - } + if (enable && !qFuzzyIsNull(d->m_max - d->m_min)) + setRange(d->m_min, d->m_max); } } @@ -314,15 +315,14 @@ QValueAxisPrivate::~QValueAxisPrivate() void QValueAxisPrivate::handleDomainUpdated() { - Q_Q(QValueAxis); - Domain* domain = qobject_cast(sender()); - Q_ASSERT(domain); - - if(orientation()==Qt::Horizontal){ - q->setRange(domain->minX(),domain->maxX()); - }else if(orientation()==Qt::Vertical){ - q->setRange(domain->minY(),domain->maxY()); - } + Q_Q(QValueAxis); + Domain* domain = qobject_cast(sender()); + Q_ASSERT(domain); + + if (orientation() == Qt::Horizontal) + q->setRange(domain->minX(), domain->maxX()); + else if (orientation() == Qt::Vertical) + q->setRange(domain->minY(), domain->maxY()); } @@ -331,7 +331,8 @@ void QValueAxisPrivate::setMin(const QVariant &min) Q_Q(QValueAxis); bool ok; qreal value = min.toReal(&ok); - if(ok) q->setMin(value); + if (ok) + q->setMin(value); } void QValueAxisPrivate::setMax(const QVariant &max) @@ -340,7 +341,8 @@ void QValueAxisPrivate::setMax(const QVariant &max) Q_Q(QValueAxis); bool ok; qreal value = max.toReal(&ok); - if(ok) q->setMax(value); + if (ok) + q->setMax(value); } void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) @@ -350,35 +352,31 @@ void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) bool ok2; qreal value1 = min.toReal(&ok1); qreal value2 = max.toReal(&ok2); - if(ok1&&ok2) q->setRange(value1,value2); + if (ok1 && ok2) + q->setRange(value1, value2); } ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QValueAxis); - if(m_orientation == Qt::Vertical){ - return new ChartValueAxisY(q,presenter); - }else{ - return new ChartValueAxisX(q,presenter); - } - + if (m_orientation == Qt::Vertical) + return new ChartValueAxisY(q, presenter); + return new ChartValueAxisX(q, presenter); } void QValueAxisPrivate::intializeDomain(Domain* domain) { Q_Q(QValueAxis); - if(qFuzzyCompare(m_max,m_min)) { - if(m_orientation==Qt::Vertical){ - q->setRange(domain->minY(),domain->maxY()); - }else{ + if (qFuzzyCompare(m_max, m_min)) { + if (m_orientation == Qt::Vertical) + q->setRange(domain->minY(), domain->maxY()); + else q->setRange(domain->minX(), domain->maxX()); - } } else { - if(m_orientation==Qt::Vertical){ + if (m_orientation == Qt::Vertical) domain->setRangeY(m_min, m_max); - }else{ + else domain->setRangeX(m_min, m_max); - } } } @@ -386,35 +384,34 @@ void QValueAxisPrivate::intializeDomain(Domain* domain) void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const { - qreal range = niceNumber(max-min,true); //range with ceiling - qreal step = niceNumber(range/(ticksCount-1),false); - min = qFloor(min/step); - max = qCeil(max/step); - ticksCount = int(max-min) +1; - min*=step; - max*=step; + qreal range = niceNumber(max - min, true); //range with ceiling + qreal step = niceNumber(range / (ticksCount - 1), false); + min = qFloor(min / step); + max = qCeil(max / step); + ticksCount = int(max - min) + 1; + min *= step; + max *= step; } //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n -qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const +qreal QValueAxisPrivate::niceNumber(qreal x, bool ceiling) const { - qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x - qreal q = x/z;//q<10 && q>=1; - - if(ceiling) { - if(q <= 1.0) q=1; - else if(q <= 2.0) q=2; - else if(q <= 5.0) q=5; - else q=10; - } - else { - if(q < 1.5) q=1; - else if(q < 3.0) q=2; - else if(q < 7.0) q=5; - else q=10; + qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x + qreal q = x / z; //q<10 && q>=1; + + if (ceiling) { + if (q <= 1.0) q = 1; + else if (q <= 2.0) q = 2; + else if (q <= 5.0) q = 5; + else q = 10; + } else { + if (q < 1.5) q = 1; + else if (q < 3.0) q = 2; + else if (q < 7.0) q = 5; + else q = 10; } - return q*z; + return q * z; } #include "moc_qvalueaxis.cpp" diff --git a/src/axis/valueaxis/qvalueaxis.h b/src/axis/valueaxis/qvalueaxis.h index be3d054..6bc8636 100644 --- a/src/axis/valueaxis/qvalueaxis.h +++ b/src/axis/valueaxis/qvalueaxis.h @@ -41,7 +41,7 @@ public: ~QValueAxis(); protected: - QValueAxis(QValueAxisPrivate &d,QObject *parent = 0); + QValueAxis(QValueAxisPrivate &d, QObject *parent = 0); public: AxisType type() const; diff --git a/src/axis/valueaxis/qvalueaxis_p.h b/src/axis/valueaxis/qvalueaxis_p.h index 2321ebc..58a0d4c 100644 --- a/src/axis/valueaxis/qvalueaxis_p.h +++ b/src/axis/valueaxis/qvalueaxis_p.h @@ -48,7 +48,7 @@ public: void handleDomainUpdated(); qreal min(){ return m_min; }; qreal max(){ return m_max; }; - int count() const { return m_tickCount;} + int count() const { return m_tickCount; } protected: void setMin(const QVariant &min); @@ -57,7 +57,7 @@ protected: private: void looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const; - qreal niceNumber(qreal x,bool ceiling) const; + qreal niceNumber(qreal x, bool ceiling) const; private: qreal m_min; diff --git a/src/barchart/abstractbarchartitem.cpp b/src/barchart/abstractbarchartitem.cpp index 3686357..2baf3ec 100644 --- a/src/barchart/abstractbarchartitem.cpp +++ b/src/barchart/abstractbarchartitem.cpp @@ -69,9 +69,8 @@ QRectF AbstractBarChartItem::boundingRect() const void AbstractBarChartItem::applyLayout(const QVector &layout) { if (m_animation) { - m_animation->setup(m_layout,layout); + m_animation->setup(m_layout, layout); presenter()->startAnimation(m_animation); - } else { setLayout(layout); update(); @@ -90,9 +89,8 @@ void AbstractBarChartItem::setLayout(const QVector &layout) m_layout = layout; - for (int i=0; i < m_bars.count(); i++) { + for (int i = 0; i < m_bars.count(); i++) m_bars.at(i)->setRect(layout.at(i)); - } } //handlers @@ -114,27 +112,23 @@ void AbstractBarChartItem::handleGeometryChanged(const QRectF &rect) void AbstractBarChartItem::handleLayoutChanged() { - if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) { - // rect size zero. - return; - } + if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) + return; // rect size zero. QVector layout = calculateLayout(); applyLayout(layout); } void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible) { - foreach (QGraphicsSimpleTextItem* label, m_labels) { + foreach (QGraphicsSimpleTextItem *label, m_labels) label->setVisible(visible); - } update(); } void AbstractBarChartItem::handleDataStructureChanged() { - foreach(QGraphicsItem *item, childItems()) { + foreach (QGraphicsItem *item, childItems()) delete item; - } m_bars.clear(); m_labels.clear(); @@ -148,12 +142,12 @@ void AbstractBarChartItem::handleDataStructureChanged() QBarSet *set = m_series->d_func()->barsetAt(s); // Bars - Bar *bar = new Bar(set,c,this); + Bar *bar = new Bar(set, c, this); m_bars.append(bar); - connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*))); - connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*))); - connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int))); - connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool))); + connect(bar, SIGNAL(clicked(int, QBarSet*)), m_series, SIGNAL(clicked(int, QBarSet*))); + connect(bar, SIGNAL(hovered(bool, QBarSet*)), m_series, SIGNAL(hovered(bool, QBarSet*))); + connect(bar, SIGNAL(clicked(int, QBarSet*)), set, SIGNAL(clicked(int))); + connect(bar, SIGNAL(hovered(bool, QBarSet*)), set, SIGNAL(hovered(bool))); m_layout.append(QRectF(0, 0, 0, 0)); // Labels @@ -171,13 +165,13 @@ void AbstractBarChartItem::handleVisibleChanged() { bool visible = m_series->isVisible(); handleLabelsVisibleChanged(visible); - foreach(QGraphicsItem *item, childItems()) + foreach (QGraphicsItem *item, childItems()) item->setVisible(visible); } void AbstractBarChartItem::handleOpacityChanged() { - foreach(QGraphicsItem *item, childItems()) + foreach (QGraphicsItem *item, childItems()) item->setOpacity(m_series->opacity()); } diff --git a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp index 092c1a7..6a3be3f 100644 --- a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp +++ b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp @@ -25,8 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : - AbstractBarChartItem(series, presenter) +HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) + : AbstractBarChartItem(series, presenter) { } @@ -57,8 +57,8 @@ QVector HorizontalBarChartItem::calculateLayout() QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); qreal yPos = geometry().bottom() + (m_domainMinY - barSet->pos(category)) * scaleY; - yPos += setCount*rectHeight/2; - yPos -= set*rectHeight; + yPos += setCount * rectHeight / 2; + yPos -= set * rectHeight; qreal rectWidth = barSet->value(category) * scaleX; Bar* bar = m_bars.at(itemIndex); @@ -67,22 +67,20 @@ QVector HorizontalBarChartItem::calculateLayout() layout.append(rect); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - if (qFuzzyIsNull(rectHeight)) { + if (qFuzzyIsNull(rectHeight)) bar->setVisible(false); - } else { + else bar->setVisible(barsVisible); - } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->value(category))) { + if (!qFuzzyIsNull(barSet->value(category))) label->setText(QString::number(barSet->value(category))); - } else { + else label->setText(QString("")); - } - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), + yPos - rectHeight / 2 - label->boundingRect().height() / 2); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); diff --git a/src/barchart/horizontal/bar/qhorizontalbarseries.cpp b/src/barchart/horizontal/bar/qhorizontalbarseries.cpp index 402b7c3..c7b6ed7 100644 --- a/src/barchart/horizontal/bar/qhorizontalbarseries.cpp +++ b/src/barchart/horizontal/bar/qhorizontalbarseries.cpp @@ -72,9 +72,8 @@ QHorizontalBarSeries::QHorizontalBarSeries(QObject *parent) : QHorizontalBarSeries::~QHorizontalBarSeries() { Q_D(QHorizontalBarSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } /*! @@ -105,17 +104,16 @@ void QHorizontalBarSeriesPrivate::scaleDomain(Domain& domain) maxX = qMax(maxX, max()); maxY = qMax(maxY, y - (qreal)0.5); - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } ChartElement* QHorizontalBarSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QHorizontalBarSeries); - HorizontalBarChartItem* bar = new HorizontalBarChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + HorizontalBarChartItem* bar = new HorizontalBarChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) bar->setAnimation(new HorizontalBarAnimation(bar)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return bar; } diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp index a7a5e13..c78bda3 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -56,7 +56,7 @@ QVector HorizontalPercentBarChartItem::calculateLayout() for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal yPos = (m_domainMinY +0.5 -barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight/2; + qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2; qreal rectWidth = barSet->value(category) * percentage * scaleX; Bar* bar = m_bars.at(itemIndex); @@ -65,16 +65,15 @@ QVector HorizontalPercentBarChartItem::calculateLayout() layout.append(rect); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - if (qFuzzyIsNull(rectHeight)) { + if (qFuzzyIsNull(rectHeight)) bar->setVisible(false); - } else { + else bar->setVisible(barsVisible); - } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) { - int p = m_series->d_func()->percentageAt(set,category) * 100; + if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) { + int p = m_series->d_func()->percentageAt(set, category) * 100; QString vString(QString::number(p)); vString.truncate(3); vString.append("%"); @@ -83,8 +82,8 @@ QVector HorizontalPercentBarChartItem::calculateLayout() label->setText(QString("")); } - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), + yPos - rectHeight / 2 - label->boundingRect().height() / 2); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); diff --git a/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp b/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp index aedfb50..3ecb9fa 100644 --- a/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp +++ b/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp @@ -77,9 +77,8 @@ QAbstractSeries::SeriesType QHorizontalPercentBarSeries::type() const QHorizontalPercentBarSeries::~QHorizontalPercentBarSeries() { Q_D(QHorizontalPercentBarSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -102,17 +101,16 @@ void QHorizontalPercentBarSeriesPrivate::scaleDomain(Domain& domain) minY = qMin(minY, - (qreal)0.5); maxY = qMax(maxY, y - (qreal)0.5); - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } ChartElement* QHorizontalPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QHorizontalPercentBarSeries); - HorizontalPercentBarChartItem* bar = new HorizontalPercentBarChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + HorizontalPercentBarChartItem* bar = new HorizontalPercentBarChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) bar->setAnimation(new HorizontalPercentBarAnimation(bar)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return bar; } diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp index 5454d08..c9936e3 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp @@ -55,40 +55,38 @@ QVector HorizontalStackedBarChartItem::calculateLayout() for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal yPos = (m_domainMinY +0.5 -barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight/2; + qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2; qreal rectWidth = barSet->value(category) * scaleX; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - if (qFuzzyIsNull(rectHeight)) { + if (qFuzzyIsNull(rectHeight)) bar->setVisible(false); - } else { + else bar->setVisible(barsVisible); - } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->value(category))) { + if (!qFuzzyIsNull(barSet->value(category))) label->setText(QString::number(barSet->value(category))); - } else { + else label->setText(QString("")); - } label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); if (rectWidth > 0) { QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight); layout.append(rect); - label->setPos(xMax + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xMax + (rect.width() / 2 - label->boundingRect().width() / 2), + yPos - rectHeight / 2 - label->boundingRect().height() / 2); xMax += rectWidth; } else { QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight); layout.append(rect); - label->setPos(xMin + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xMin + (rect.width() / 2 - label->boundingRect().width() / 2), + yPos - rectHeight / 2 - label->boundingRect().height() / 2); xMin += rectWidth; } itemIndex++; diff --git a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp index 0361420..be01a1a 100644 --- a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp +++ b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp @@ -68,9 +68,8 @@ QHorizontalStackedBarSeries::QHorizontalStackedBarSeries(QObject *parent) : QHorizontalStackedBarSeries::~QHorizontalStackedBarSeries() { Q_D(QHorizontalStackedBarSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } /*! @@ -101,17 +100,16 @@ void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain& domain) maxX = qMax(maxX, top()); maxY = qMax(maxY, y - (qreal)0.5); - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } ChartElement* QHorizontalStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QHorizontalStackedBarSeries); - HorizontalStackedBarChartItem* bar = new HorizontalStackedBarChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + HorizontalStackedBarChartItem* bar = new HorizontalStackedBarChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) bar->setAnimation(new HorizontalStackedBarAnimation(bar)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return bar; } diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 346b149..58bbad4 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -198,11 +198,11 @@ QAbstractBarSeries::~QAbstractBarSeries() /*! \internal */ -QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent) : - QAbstractSeries(o,parent) +QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent) + : QAbstractSeries(o, parent) { Q_D(QAbstractSeries); - QObject::connect(this,SIGNAL(countChanged()),d,SIGNAL(countChanged())); + QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged())); } /*! @@ -333,9 +333,8 @@ void QAbstractBarSeries::clear() if (success) { emit barsetsRemoved(sets); emit countChanged(); - foreach (QBarSet* set, sets) { + foreach (QBarSet *set, sets) delete set; - } } } @@ -392,10 +391,9 @@ int QAbstractBarSeriesPrivate::categoryCount() const { // No categories defined. return count of longest set. int count = 0; - for (int i=0; icount() > count) { + for (int i = 0; i < m_barSets.count(); i++) { + if (m_barSets.at(i)->count() > count) count = m_barSets.at(i)->count(); - } } return count; @@ -403,9 +401,8 @@ int QAbstractBarSeriesPrivate::categoryCount() const void QAbstractBarSeriesPrivate::setBarWidth(qreal width) { - if (width < 0.0) { + if (width < 0.0) width = 0.0; - } m_barWidth = width; emit updatedLayout(); } @@ -434,9 +431,9 @@ void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) qreal QAbstractBarSeriesPrivate::min() { - if (m_barSets.count() <= 0) { + if (m_barSets.count() <= 0) return 0; - } + qreal min = INT_MAX; for (int i = 0; i < m_barSets.count(); i++) { @@ -452,9 +449,9 @@ qreal QAbstractBarSeriesPrivate::min() qreal QAbstractBarSeriesPrivate::max() { - if (m_barSets.count() <= 0) { + if (m_barSets.count() <= 0) return 0; - } + qreal max = INT_MIN; for (int i = 0; i < m_barSets.count(); i++) { @@ -471,32 +468,25 @@ qreal QAbstractBarSeriesPrivate::max() qreal QAbstractBarSeriesPrivate::valueAt(int set, int category) { - if ((set < 0) || (set >= m_barSets.count())) { - // No set, no value. - return 0; - } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { - // No category, no value. - return 0; - } + if ((set < 0) || (set >= m_barSets.count())) + return 0; // No set, no value. + else if ((category < 0) || (category >= m_barSets.at(set)->count())) + return 0; // No category, no value. return m_barSets.at(set)->at(category); } qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category) { - if ((set < 0) || (set >= m_barSets.count())) { - // No set, no value. - return 0; - } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { - // No category, no value. - return 0; - } + if ((set < 0) || (set >= m_barSets.count())) + return 0; // No set, no value. + else if ((category < 0) || (category >= m_barSets.at(set)->count())) + return 0; // No category, no value. qreal value = m_barSets.at(set)->at(category); qreal sum = categorySum(category); - if ( qFuzzyIsNull(sum) ) { + if (qFuzzyIsNull(sum)) return 0; - } return value / sum; } @@ -537,9 +527,9 @@ qreal QAbstractBarSeriesPrivate::maxCategorySum() qreal QAbstractBarSeriesPrivate::minX() { - if (m_barSets.count() <= 0) { + if (m_barSets.count() <= 0) return 0; - } + qreal min = INT_MAX; for (int i = 0; i < m_barSets.count(); i++) { @@ -555,9 +545,9 @@ qreal QAbstractBarSeriesPrivate::minX() qreal QAbstractBarSeriesPrivate::maxX() { - if (m_barSets.count() <= 0) { + if (m_barSets.count() <= 0) return 0; - } + qreal max = INT_MIN; for (int i = 0; i < m_barSets.count(); i++) { @@ -611,11 +601,10 @@ qreal QAbstractBarSeriesPrivate::top() // Returns top of all categories qreal top(0); int count = categoryCount(); - for (int i=0; i top) { + if (temp > top) top = temp; - } } return top; } @@ -625,11 +614,10 @@ qreal QAbstractBarSeriesPrivate::bottom() // Returns bottom of all categories qreal bottom(0); int count = categoryCount(); - for (int i=0; i QAbstractBarSeriesPrivate::createLegendMarker(QLegend* lege { Q_Q(QAbstractBarSeries); QList markers; - foreach(QBarSet* set, q->barSets()) { - BarLegendMarker* marker = new BarLegendMarker(q,set,legend); + foreach (QBarSet *set, q->barSets()) { + BarLegendMarker* marker = new BarLegendMarker(q, set, legend); markers << marker; } @@ -674,77 +662,65 @@ QList QAbstractBarSeriesPrivate::createLegendMarker(QLegend* lege bool QAbstractBarSeriesPrivate::append(QBarSet *set) { - if ((m_barSets.contains(set)) || (set == 0)) { - // Fail if set is already in list or set is null. - return false; - } + if ((m_barSets.contains(set)) || (set == 0)) + return false; // Fail if set is already in list or set is null. m_barSets.append(set); QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); - emit restructuredBars(); // this notifies barchartitem + emit restructuredBars(); // this notifies barchartitem return true; } bool QAbstractBarSeriesPrivate::remove(QBarSet *set) { - if (!m_barSets.contains(set)) { - // Fail if set is not in list - return false; - } + if (!m_barSets.contains(set)) + return false; // Fail if set is not in list m_barSets.removeOne(set); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); - emit restructuredBars(); // this notifies barchartitem + emit restructuredBars(); // this notifies barchartitem return true; } bool QAbstractBarSeriesPrivate::append(QList sets) { - foreach (QBarSet* set, sets) { - if ((set == 0) || (m_barSets.contains(set))) { - // Fail if any of the sets is null or is already appended. - return false; - } - if (sets.count(set) != 1) { - // Also fail if same set is more than once in given list. - return false; - } + foreach (QBarSet *set, sets) { + if ((set == 0) || (m_barSets.contains(set))) + return false; // Fail if any of the sets is null or is already appended. + if (sets.count(set) != 1) + return false; // Also fail if same set is more than once in given list. } - foreach (QBarSet* set, sets) { + foreach (QBarSet *set, sets) { m_barSets.append(set); QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); } - emit restructuredBars(); // this notifies barchartitem + emit restructuredBars(); // this notifies barchartitem return true; } bool QAbstractBarSeriesPrivate::remove(QList sets) { - if (sets.count() == 0) { + if (sets.count() == 0) return false; - } - foreach (QBarSet* set, sets) { - if ((set == 0) || (!m_barSets.contains(set))) { - // Fail if any of the sets is null or is not in series - return false; - } - if (sets.count(set) != 1) { - // Also fail if same set is more than once in given list. - return false; - } + + foreach (QBarSet *set, sets) { + if ((set == 0) || (!m_barSets.contains(set))) + return false; // Fail if any of the sets is null or is not in series + if (sets.count(set) != 1) + return false; // Also fail if same set is more than once in given list. } - foreach (QBarSet* set, sets) { + foreach (QBarSet *set, sets) { m_barSets.removeOne(set); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); @@ -757,10 +733,8 @@ bool QAbstractBarSeriesPrivate::remove(QList sets) bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) { - if ((m_barSets.contains(set)) || (set == 0)) { - // Fail if set is already in list or set is null. - return false; - } + if ((m_barSets.contains(set)) || (set == 0)) + return false; // Fail if set is already in list or set is null. m_barSets.insert(index, set); QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); @@ -775,34 +749,23 @@ void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis) { Q_Q(QAbstractBarSeries); - if(axis->type()==QAbstractAxis::AxisTypeBarCategory) { - - switch(q->type()) { - - case QAbstractSeries::SeriesTypeHorizontalBar: - case QAbstractSeries::SeriesTypeHorizontalPercentBar: - case QAbstractSeries::SeriesTypeHorizontalStackedBar: { - - if(axis->orientation()==Qt::Vertical) - { - populateCategories(qobject_cast(axis)); - } - break; - } - case QAbstractSeries::SeriesTypeBar: - case QAbstractSeries::SeriesTypePercentBar: - case QAbstractSeries::SeriesTypeStackedBar: { - - if(axis->orientation()==Qt::Horizontal) - { - populateCategories(qobject_cast(axis)); - } - break; - } - default: - qWarning()<<"Unexpected series type"; + if (axis->type() == QAbstractAxis::AxisTypeBarCategory) { + switch (q->type()) { + case QAbstractSeries::SeriesTypeHorizontalBar: + case QAbstractSeries::SeriesTypeHorizontalPercentBar: + case QAbstractSeries::SeriesTypeHorizontalStackedBar: + if (axis->orientation() == Qt::Vertical) + populateCategories(qobject_cast(axis)); + break; + case QAbstractSeries::SeriesTypeBar: + case QAbstractSeries::SeriesTypePercentBar: + case QAbstractSeries::SeriesTypeStackedBar: + if (axis->orientation() == Qt::Horizontal) + populateCategories(qobject_cast(axis)); + break; + default: + qWarning() << "Unexpected series type"; break; - } } } @@ -811,32 +774,22 @@ QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientati { Q_Q(const QAbstractBarSeries); - switch(q->type()) { - - case QAbstractSeries::SeriesTypeHorizontalBar: - case QAbstractSeries::SeriesTypeHorizontalPercentBar: - case QAbstractSeries::SeriesTypeHorizontalStackedBar: { - - if(orientation==Qt::Vertical) - { - return QAbstractAxis::AxisTypeBarCategory; - } - break; - } - case QAbstractSeries::SeriesTypeBar: - case QAbstractSeries::SeriesTypePercentBar: - case QAbstractSeries::SeriesTypeStackedBar: { - - if(orientation==Qt::Horizontal) - { - return QAbstractAxis::AxisTypeBarCategory; - } - break; - } - default: - qWarning()<<"Unexpected series type"; + switch (q->type()) { + case QAbstractSeries::SeriesTypeHorizontalBar: + case QAbstractSeries::SeriesTypeHorizontalPercentBar: + case QAbstractSeries::SeriesTypeHorizontalStackedBar: + if (orientation == Qt::Vertical) + return QAbstractAxis::AxisTypeBarCategory; + break; + case QAbstractSeries::SeriesTypeBar: + case QAbstractSeries::SeriesTypePercentBar: + case QAbstractSeries::SeriesTypeStackedBar: + if (orientation == Qt::Horizontal) + return QAbstractAxis::AxisTypeBarCategory; + break; + default: + qWarning() << "Unexpected series type"; break; - } return QAbstractAxis::AxisTypeValue; @@ -845,9 +798,9 @@ QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientati void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis) { QStringList categories; - if(axis->categories().isEmpty()) { - for (int i(1); i < categoryCount()+1; i++) - categories << QString::number(i); + if (axis->categories().isEmpty()) { + for (int i(1); i < categoryCount() + 1; i++) + categories << QString::number(i); axis->append(categories); } } @@ -855,5 +808,4 @@ void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis) #include "moc_qabstractbarseries.cpp" #include "moc_qabstractbarseries_p.cpp" - QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qabstractbarseries.h b/src/barchart/qabstractbarseries.h index a0161c8..87ab4f2 100644 --- a/src/barchart/qabstractbarseries.h +++ b/src/barchart/qabstractbarseries.h @@ -56,7 +56,7 @@ public: bool isLabelsVisible() const; protected: - explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0); + explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = 0); Q_SIGNALS: void clicked(int index, QBarSet *barset); diff --git a/src/barchart/qbarmodelmapper.cpp b/src/barchart/qbarmodelmapper.cpp index 8524c71..cc865b5 100644 --- a/src/barchart/qbarmodelmapper.cpp +++ b/src/barchart/qbarmodelmapper.cpp @@ -45,13 +45,12 @@ void QBarModelMapper::setModel(QAbstractItemModel *model) return; Q_D(QBarModelMapper); - if (d->m_model) { + if (d->m_model) disconnect(d->m_model, 0, d, 0); - } d->m_model = model; d->initializeBarFromModel(); - // connect signals from the model + // connect signals from the model connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int))); connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); @@ -70,9 +69,8 @@ QAbstractBarSeries* QBarModelMapper::series() const void QBarModelMapper::setSeries(QAbstractBarSeries *series) { Q_D(QBarModelMapper); - if (d->m_series) { + if (d->m_series) disconnect(d->m_series, 0, d, 0); - } if (series == 0) return; @@ -402,9 +400,10 @@ void QBarModelMapperPrivate::barSetsAdded(QList sets) return; int maxCount = 0; - for(int i = 0; i < sets.count(); i++) + for (int i = 0; i < sets.count(); i++) { if (sets.at(i)->count() > m_count) maxCount = sets.at(i)->count(); + } if (m_count != -1 && m_count < maxCount) m_count = maxCount; @@ -426,7 +425,7 @@ void QBarModelMapperPrivate::barSetsAdded(QList sets) m_model->insertRows(firstIndex + m_firstBarSetSection, sets.count()); - for(int i = firstIndex + m_firstBarSetSection; i < firstIndex + m_firstBarSetSection + sets.count(); i++) { + for (int i = firstIndex + m_firstBarSetSection; i < firstIndex + m_firstBarSetSection + sets.count(); i++) { m_model->setHeaderData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, sets.at(i - firstIndex - m_firstBarSetSection)->label()); for (int j = 0; j < sets.at(i - firstIndex - m_firstBarSetSection)->count(); j++) m_model->setData(barModelIndex(i, j), sets.at(i - firstIndex - m_firstBarSetSection)->at(j)); diff --git a/src/barchart/qbarmodelmapper.h b/src/barchart/qbarmodelmapper.h index c9b5f2d..805eaf5 100644 --- a/src/barchart/qbarmodelmapper.h +++ b/src/barchart/qbarmodelmapper.h @@ -57,7 +57,7 @@ protected: void setLastBarSetSection(int lastBarSetSection); Qt::Orientation orientation() const; - void setOrientation(Qt::Orientation orientation); + void setOrientation(Qt::Orientation orientation); protected: QBarModelMapperPrivate * const d_ptr; diff --git a/src/barchart/qbarmodelmapper_p.h b/src/barchart/qbarmodelmapper_p.h index e163ae5..5a59f1f 100644 --- a/src/barchart/qbarmodelmapper_p.h +++ b/src/barchart/qbarmodelmapper_p.h @@ -75,7 +75,7 @@ private: void blockSeriesSignals(bool block = true); private: - QAbstractBarSeries *m_series; + QAbstractBarSeries *m_series; QList m_barSets; QAbstractItemModel *m_model; int m_first; diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index e531c84..933587d 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -250,8 +250,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs QBarSet with a label of \a label and with parent of \a parent */ QBarSet::QBarSet(const QString label, QObject *parent) - : QObject(parent) - ,d_ptr(new QBarSetPrivate(label,this)) + : QObject(parent), + d_ptr(new QBarSetPrivate(label, this)) { } @@ -321,7 +321,7 @@ QBarSet& QBarSet::operator << (const qreal &value) void QBarSet::insert(const int index, const qreal value) { d_ptr->insert(index, value); - emit valuesAdded(index,1); + emit valuesAdded(index, 1); } /*! @@ -330,10 +330,9 @@ void QBarSet::insert(const int index, const qreal value) */ void QBarSet::remove(const int index, const int count) { - int removedCount = d_ptr->remove(index,count); - if (removedCount > 0) { - emit valuesRemoved(index,removedCount); - } + int removedCount = d_ptr->remove(index, count); + if (removedCount > 0) + emit valuesRemoved(index, removedCount); return; } @@ -343,7 +342,7 @@ void QBarSet::remove(const int index, const int count) void QBarSet::replace(const int index, const qreal value) { if (index >= 0 && index < d_ptr->m_values.count()) { - d_ptr->replace(index,value); + d_ptr->replace(index, value); emit valueChanged(index); } } @@ -355,10 +354,8 @@ void QBarSet::replace(const int index, const qreal value) */ qreal QBarSet::at(const int index) const { - if (index < 0 || index >= d_ptr->m_values.count()) { + if (index < 0 || index >= d_ptr->m_values.count()) return 0; - } - return d_ptr->m_values.at(index).y(); } @@ -385,9 +382,8 @@ int QBarSet::count() const qreal QBarSet::sum() const { qreal total(0); - for (int i=0; i < d_ptr->m_values.count(); i++) { + for (int i = 0; i < d_ptr->m_values.count(); i++) total += d_ptr->m_values.at(i).y(); - } return total; } @@ -396,11 +392,11 @@ qreal QBarSet::sum() const */ void QBarSet::setPen(const QPen &pen) { - if(d_ptr->m_pen!=pen){ - d_ptr->m_pen = pen; - emit d_ptr->updatedBars(); - emit penChanged(); - } + if (d_ptr->m_pen != pen) { + d_ptr->m_pen = pen; + emit d_ptr->updatedBars(); + emit penChanged(); + } } /*! @@ -416,10 +412,10 @@ QPen QBarSet::pen() const */ void QBarSet::setBrush(const QBrush &brush) { - if(d_ptr->m_brush!=brush){ - d_ptr->m_brush = brush; - emit d_ptr->updatedBars(); - emit brushChanged(); + if (d_ptr->m_brush != brush) { + d_ptr->m_brush = brush; + emit d_ptr->updatedBars(); + emit brushChanged(); } } @@ -436,7 +432,7 @@ QBrush QBarSet::brush() const */ void QBarSet::setLabelBrush(const QBrush &brush) { - if(d_ptr->m_labelBrush!=brush){ + if (d_ptr->m_labelBrush != brush) { d_ptr->m_labelBrush = brush; emit d_ptr->updatedBars(); emit labelBrushChanged(); @@ -456,7 +452,7 @@ QBrush QBarSet::labelBrush() const */ void QBarSet::setLabelFont(const QFont &font) { - if(d_ptr->m_labelFont!=font) { + if (d_ptr->m_labelFont != font) { d_ptr->m_labelFont = font; emit d_ptr->updatedBars(); emit labelFontChanged(); @@ -533,7 +529,7 @@ QColor QBarSet::labelColor() Sets the color of labels for this barset */ void QBarSet::setLabelColor(QColor color) -{ +{ QBrush b = labelBrush(); if (b == QBrush()) b.setStyle(Qt::SolidPattern); @@ -565,17 +561,16 @@ void QBarSetPrivate::append(QPointF value) void QBarSetPrivate::append(QList values) { - for (int i=0; i values) { int index = m_values.count(); - for (int i=0; i m_values.count()) { - // Trying to remove more items than list has. Limit amount to be removed. - removeCount = m_values.count() - index; - } + if ((index < 0) || (m_values.count() == 0)) + return 0; // Invalid index or not values in list, remove nothing. + else if ((index + count) > m_values.count()) + removeCount = m_values.count() - index; // Trying to remove more items than list has. Limit amount to be removed. int c = 0; while (c < removeCount) { @@ -616,31 +608,27 @@ int QBarSetPrivate::remove(const int index, const int count) void QBarSetPrivate::replace(const int index, const qreal value) { - m_values.replace(index,QPointF(index,value)); + m_values.replace(index, QPointF(index, value)); emit updatedLayout(); } void QBarSetPrivate::replace(const int index, const QPointF value) { - m_values.replace(index,value); + m_values.replace(index, value); emit updatedLayout(); } qreal QBarSetPrivate::pos(const int index) { - if (index < 0 || index >= m_values.count()) { + if (index < 0 || index >= m_values.count()) return 0; - } - return m_values.at(index).x(); } qreal QBarSetPrivate::value(const int index) { - if (index < 0 || index >= m_values.count()) { + if (index < 0 || index >= m_values.count()) return 0; - } - return m_values.at(index).y(); } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 1ef6032..e16fbda 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -57,7 +57,7 @@ public: void remove(const int index, const int count = 1); void replace(const int index, const qreal value); qreal at(const int index) const; - qreal operator [] (const int index) const; + qreal operator [](const int index) const; int count() const; qreal sum() const; diff --git a/src/barchart/vertical/bar/barchartitem.cpp b/src/barchart/vertical/bar/barchartitem.cpp index d80e1f6..769f512 100644 --- a/src/barchart/vertical/bar/barchartitem.cpp +++ b/src/barchart/vertical/bar/barchartitem.cpp @@ -56,8 +56,8 @@ QVector BarChartItem::calculateLayout() QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left(); - xPos -= setCount*rectWidth/2; - xPos += set*rectWidth; + xPos -= setCount * rectWidth / 2; + xPos += set * rectWidth; qreal rectHeight = barSet->value(category) * scaleY; Bar* bar = m_bars.at(itemIndex); @@ -66,22 +66,20 @@ QVector BarChartItem::calculateLayout() layout.append(rect); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - if (qFuzzyIsNull(rectHeight)) { + if (qFuzzyIsNull(rectHeight)) bar->setVisible(false); - } else { + else bar->setVisible(barsVisible); - } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->value(category))) { + if (!qFuzzyIsNull(barSet->value(category))) label->setText(QString::number(barSet->value(category))); - } else { + else label->setText(QString("")); - } - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), + yPos - rectHeight / 2 - label->boundingRect().height() / 2); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); diff --git a/src/barchart/vertical/bar/qbarseries.cpp b/src/barchart/vertical/bar/qbarseries.cpp index 41b2115..7032c4e 100644 --- a/src/barchart/vertical/bar/qbarseries.cpp +++ b/src/barchart/vertical/bar/qbarseries.cpp @@ -79,9 +79,8 @@ QAbstractSeries::SeriesType QBarSeries::type() const QBarSeries::~QBarSeries() { Q_D(QBarSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -103,7 +102,7 @@ void QBarSeriesPrivate::scaleDomain(Domain& domain) maxX = qMax(maxX, x - (qreal)0.5); maxY = qMax(maxY, max()); - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } @@ -111,10 +110,9 @@ ChartElement* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QBarSeries); - BarChartItem* bar = new BarChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + BarChartItem* bar = new BarChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) bar->setAnimation(new BarAnimation(bar)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return bar; } diff --git a/src/barchart/vertical/percent/percentbarchartitem.cpp b/src/barchart/vertical/percent/percentbarchartitem.cpp index 4d6283e..b43206d 100644 --- a/src/barchart/vertical/percent/percentbarchartitem.cpp +++ b/src/barchart/vertical/percent/percentbarchartitem.cpp @@ -54,28 +54,27 @@ QVector PercentBarChartItem::calculateLayout() qreal colSum = m_series->d_func()->categorySum(category); qreal percentage = (100 / colSum); qreal yPos = height + scaleY * m_domainMinY + geometry().top(); - for (int set=0; set < setCount; set++) { + for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth/2; + qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2; qreal rectHeight = barSet->value(category) * percentage * scaleY; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - if (qFuzzyIsNull(rectHeight)) { + if (qFuzzyIsNull(rectHeight)) bar->setVisible(false); - } else { + else bar->setVisible(barsVisible); - } - QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight); + QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); layout.append(rect); QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) { - int p = m_series->d_func()->percentageAt(set,category) * 100; + if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) { + int p = m_series->d_func()->percentageAt(set, category) * 100; QString vString(QString::number(p)); vString.truncate(3); vString.append("%"); @@ -84,8 +83,8 @@ QVector PercentBarChartItem::calculateLayout() label->setText(QString("")); } - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), + yPos - rectHeight / 2 - label->boundingRect().height() / 2); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); diff --git a/src/barchart/vertical/percent/qpercentbarseries.cpp b/src/barchart/vertical/percent/qpercentbarseries.cpp index 9ff0da9..2d3f588 100644 --- a/src/barchart/vertical/percent/qpercentbarseries.cpp +++ b/src/barchart/vertical/percent/qpercentbarseries.cpp @@ -69,9 +69,8 @@ QPercentBarSeries::QPercentBarSeries(QObject *parent) QPercentBarSeries::~QPercentBarSeries() { Q_D(QPercentBarSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } /*! @@ -102,7 +101,7 @@ void QPercentBarSeriesPrivate::scaleDomain(Domain& domain) minY = 0; maxY = 100; - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } @@ -110,10 +109,9 @@ ChartElement* QPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter { Q_Q(QPercentBarSeries); - PercentBarChartItem* bar = new PercentBarChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + PercentBarChartItem* bar = new PercentBarChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) bar->setAnimation(new PercentBarAnimation(bar)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return bar; } diff --git a/src/barchart/vertical/stacked/qstackedbarseries.cpp b/src/barchart/vertical/stacked/qstackedbarseries.cpp index b2034d2..085f9d2 100644 --- a/src/barchart/vertical/stacked/qstackedbarseries.cpp +++ b/src/barchart/vertical/stacked/qstackedbarseries.cpp @@ -70,9 +70,8 @@ QStackedBarSeries::QStackedBarSeries(QObject *parent) QStackedBarSeries::~QStackedBarSeries() { Q_D(QStackedBarSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } /*! Returns QChartSeries::SeriesTypeStackedBar. @@ -102,17 +101,16 @@ void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) maxX = qMax(maxX, x - (qreal)0.5); maxY = qMax(maxY, top()); - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QStackedBarSeries); - StackedBarChartItem* bar = new StackedBarChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + StackedBarChartItem* bar = new StackedBarChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) bar->setAnimation(new StackedBarAnimation(bar)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return bar; } diff --git a/src/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/barchart/vertical/stacked/stackedbarchartitem.cpp index 1790a38..4fd1d9d 100644 --- a/src/barchart/vertical/stacked/stackedbarchartitem.cpp +++ b/src/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -52,42 +52,41 @@ QVector StackedBarChartItem::calculateLayout() for (int category = 0; category < categoryCount; category++) { qreal yMax = height + scaleY * m_domainMinY + geometry().top(); qreal yMin = height + scaleY * m_domainMinY + geometry().top(); - for (int set=0; set < setCount; set++) { + for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth/2; + qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2; qreal rectHeight = barSet->value(category) * scaleY; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - if (qFuzzyIsNull(rectHeight)) { + if (qFuzzyIsNull(rectHeight)) bar->setVisible(false); - } else { + else bar->setVisible(barsVisible); - } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->value(category))) { + if (!qFuzzyIsNull(barSet->value(category))) label->setText(QString::number(barSet->value(category))); - } else { + else label->setText(QString("")); - } + label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); if (rectHeight < 0) { - QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight); + QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight); layout.append(rect); - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yMax - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), + yMax - rectHeight / 2 - label->boundingRect().height() / 2); yMax -= rectHeight; } else { - QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight); + QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight); layout.append(rect); - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yMin - rectHeight/2 - label->boundingRect().height()/2); + label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), + yMin - rectHeight / 2 - label->boundingRect().height() / 2); yMin -= rectHeight; } diff --git a/src/chartbackground.cpp b/src/chartbackground.cpp index e1e8396..bf24b88 100644 --- a/src/chartbackground.cpp +++ b/src/chartbackground.cpp @@ -64,27 +64,28 @@ void ChartBackground::setDropShadowEnabled(bool enabled) void ChartBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - Q_UNUSED(option); - Q_UNUSED(widget); - painter->save(); - painter->setPen(pen()); - painter->setBrush(brush()); - painter->drawRoundRect(rect(),roundness(rect().width()),roundness(rect().height())); + Q_UNUSED(option); + Q_UNUSED(widget); + painter->save(); + painter->setPen(pen()); + painter->setBrush(brush()); + painter->drawRoundRect(rect(), roundness(rect().width()), roundness(rect().height())); #ifndef QT_NO_DEBUG - painter->setPen(Qt::black); - QFont font; - QString build("build %1"); - font.setPointSize(6); - painter->setFont(font); - painter->drawText(rect().bottomLeft(),build.arg(ChartConfig::instance()->compilationTime())); + painter->setPen(Qt::black); + QFont font; + QString build("build %1"); + font.setPointSize(6); + painter->setFont(font); + painter->drawText(rect().bottomLeft(), build.arg(ChartConfig::instance()->compilationTime())); #endif - painter->restore(); + painter->restore(); } int ChartBackground::roundness(qreal size) const { - if(qFuzzyIsNull(size)) return 0; - return 100*m_diameter/int(size); + if (qFuzzyIsNull(size)) + return 0; + return 100 * m_diameter / int(size); } int ChartBackground::diameter() const @@ -92,9 +93,9 @@ int ChartBackground::diameter() const return m_diameter; } -void ChartBackground::setDimeter(int dimater) +void ChartBackground::setDiameter(int diameter) { - m_diameter=dimater; + m_diameter = diameter; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartbackground_p.h b/src/chartbackground_p.h index 0a51753..3031f98 100644 --- a/src/chartbackground_p.h +++ b/src/chartbackground_p.h @@ -39,13 +39,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartBackground: public QGraphicsRectItem { public: - ChartBackground(QGraphicsItem *parent =0); + ChartBackground(QGraphicsItem *parent = 0); ~ChartBackground(); - void setDimeter(int dimater); + void setDiameter(int diameter); int diameter() const; void setDropShadowEnabled(bool enabled); - bool isDropShadowEnabled() {return m_dropShadow != 0;} + bool isDropShadowEnabled() { return m_dropShadow != 0; } protected: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); diff --git a/src/chartconfig_p.h b/src/chartconfig_p.h index 3f4c70a..12f0e76 100644 --- a/src/chartconfig_p.h +++ b/src/chartconfig_p.h @@ -48,29 +48,29 @@ const char *gitHead = "unknown"; QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartConfig { - +class ChartConfig +{ private: - ChartConfig(){ - #if defined(DEVELOPMENT_BUILD) && !defined(QT_NO_DEBUG) - qDebug()<<"buildTime" << buildTime; - qDebug()<<"gitHead" << gitHead; - #endif + ChartConfig() { +#if defined(DEVELOPMENT_BUILD) && !defined(QT_NO_DEBUG) + qDebug() << "buildTime" << buildTime; + qDebug() << "gitHead" << gitHead; +#endif m_instance = this; } public: - static ChartConfig* instance(){ - if(!m_instance){ - m_instance= new ChartConfig(); + static ChartConfig* instance() { + if (!m_instance) { + m_instance = new ChartConfig(); } return m_instance; } - QString compilationTime(){ + QString compilationTime() { return buildTime; } - QString compilationHead(){ + QString compilationHead() { return gitHead; } @@ -79,7 +79,7 @@ private: }; -ChartConfig* ChartConfig::m_instance=0; +ChartConfig* ChartConfig::m_instance = 0; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 79fb89c..1e8b8f7 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -31,12 +31,12 @@ #include "qpieseries.h" #ifndef QT_ON_ARM - #include "qdatetimeaxis.h" +#include "qdatetimeaxis.h" #endif QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartDataSet::ChartDataSet(QChart *parent):QObject(parent) +ChartDataSet::ChartDataSet(QChart *parent): QObject(parent) { } @@ -50,13 +50,13 @@ void ChartDataSet::addSeries(QAbstractSeries* series) { Domain* domain = m_seriesDomainMap.value(series); - if(domain) { + if (domain) { qWarning() << "Can not add series. Series already on the chart"; return; } domain = new Domain(series); - m_seriesDomainMap.insert(series,domain); + m_seriesDomainMap.insert(series, domain); series->d_ptr->scaleDomain(*domain); createSeriesIndex(series); @@ -65,15 +65,15 @@ void ChartDataSet::addSeries(QAbstractSeries* series) series->d_ptr->m_chart = qobject_cast(parent()); series->d_ptr->m_dataset = this; - emit seriesAdded(series,domain); + emit seriesAdded(series, domain); } void ChartDataSet::removeSeries(QAbstractSeries* series) { - if(!m_seriesDomainMap.contains(series)) { - qWarning()<<"Can not remove series. Series not found on the chart."; + if (!m_seriesDomainMap.contains(series)) { + qWarning() << "Can not remove series. Series not found on the chart."; return; } @@ -98,22 +98,21 @@ void ChartDataSet::createSeriesIndex(QAbstractSeries* series) { QMapIterator i(m_indexSeriesMap); - int key=0; + int key = 0; while (i.hasNext()) { i.next(); - if(i.key()!=key) { + if (i.key() != key) break; - } key++; } - m_indexSeriesMap.insert(key,series); + m_indexSeriesMap.insert(key, series); } void ChartDataSet::removeSeriesIndex(QAbstractSeries* series) { int key = seriesIndex(series); - Q_ASSERT(key!=-1); + Q_ASSERT(key != -1); m_indexSeriesMap.remove(key); } @@ -137,13 +136,16 @@ void ChartDataSet::createDefaultAxes() // Select the required axis x and axis y types based on the types of the current series while (i.hasNext()) { i.next(); - QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); - if(axisX) typeX&=axisX->type(); - else typeX|=i.key()->d_ptr->defaultAxisType(Qt::Horizontal); - if(axisY) typeY&=axisY->type(); - else typeY|=i.key()->d_ptr->defaultAxisType(Qt::Vertical); + if (axisX) + typeX &= axisX->type(); + else + typeX |= i.key()->d_ptr->defaultAxisType(Qt::Horizontal); + if (axisY) + typeY &= axisY->type(); + else + typeY |= i.key()->d_ptr->defaultAxisType(Qt::Vertical); } // Create the axes of the types selected @@ -170,9 +172,9 @@ void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation ori i.toFront(); while (i.hasNext()) { i.next(); - initializeAxis(axis,i.key()); + initializeAxis(axis, i.key()); } - emit axisAdded(axis,i.value()); + emit axisAdded(axis, i.value()); } } @@ -180,7 +182,7 @@ QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type, Qt::Orient { QAbstractAxis* axis = 0; - switch(type) { + switch (type) { case QAbstractAxis::AxisTypeValue: axis = new QValueAxis(this); break; @@ -200,27 +202,26 @@ QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type, Qt::Orient break; } - if(axis) + if (axis) axis->d_ptr->setOrientation(orientation); return axis; } -void ChartDataSet::initializeAxis(QAbstractAxis* axis,QAbstractSeries* series) +void ChartDataSet::initializeAxis(QAbstractAxis* axis, QAbstractSeries* series) { Domain* domain = m_seriesDomainMap.value(series); axis->d_ptr->m_dataset = this; series->d_ptr->initializeAxis(axis); axis->d_ptr->intializeDomain(domain); - if(axis->orientation()==Qt::Horizontal) { - QObject::connect(axis->d_ptr.data(),SIGNAL(updated()),domain,SLOT(handleAxisUpdated())); - QObject::connect(domain,SIGNAL(updated()),axis->d_ptr.data(),SLOT(handleDomainUpdated())); - m_seriesAxisXMap.insert(series,axis); - } - else { - QObject::connect(axis->d_ptr.data(),SIGNAL(updated()),domain,SLOT(handleAxisUpdated())); - QObject::connect(domain,SIGNAL(updated()),axis->d_ptr.data(),SLOT(handleDomainUpdated())); - m_seriesAxisYMap.insert(series,axis); + if (axis->orientation() == Qt::Horizontal) { + QObject::connect(axis->d_ptr.data(), SIGNAL(updated()), domain, SLOT(handleAxisUpdated())); + QObject::connect(domain, SIGNAL(updated()), axis->d_ptr.data(), SLOT(handleDomainUpdated())); + m_seriesAxisXMap.insert(series, axis); + } else { + QObject::connect(axis->d_ptr.data(), SIGNAL(updated()), domain, SLOT(handleAxisUpdated())); + QObject::connect(domain, SIGNAL(updated()), axis->d_ptr.data(), SLOT(handleDomainUpdated())); + m_seriesAxisYMap.insert(series, axis); } axis->d_ptr->emitUpdated(); } @@ -229,27 +230,27 @@ void ChartDataSet::removeAxes(QAbstractSeries* series) { QAbstractAxis* axisX = m_seriesAxisXMap.take(series); - if(axisX) { + if (axisX) { QList axesX = m_seriesAxisXMap.values(); int x = axesX.indexOf(axisX); - if(x==-1) { + if (x == -1) { emit axisRemoved(axisX); - axisX->d_ptr->m_dataset=0; + axisX->d_ptr->m_dataset = 0; axisX->deleteLater(); } } QAbstractAxis* axisY = m_seriesAxisYMap.take(series); - if(axisY) { + if (axisY) { QList axesY = m_seriesAxisYMap.values(); int y = axesY.indexOf(axisY); - if(y==-1) { + if (y == -1) { emit axisRemoved(axisY); - axisY->d_ptr->m_dataset=0; + axisY->d_ptr->m_dataset = 0; axisY->deleteLater(); } } @@ -257,40 +258,36 @@ void ChartDataSet::removeAxes(QAbstractSeries* series) void ChartDataSet::removeAxis(QAbstractAxis* axis) { - if(!axis->d_ptr->m_dataset) { - qWarning()<<"UnBound axis found !"; + if (!axis->d_ptr->m_dataset) { + qWarning() << "UnBound axis found !"; return; } QMap *seriesAxisMap; - if(axis->orientation()==Qt::Vertical) { - seriesAxisMap= &m_seriesAxisYMap; - } - else { - seriesAxisMap= &m_seriesAxisXMap; - } + if (axis->orientation() == Qt::Vertical) + seriesAxisMap = &m_seriesAxisYMap; + else + seriesAxisMap = &m_seriesAxisXMap; QMapIterator i(*seriesAxisMap); while (i.hasNext()) { i.next(); - if(i.value()==axis) { + if (i.value() == axis) removeSeries(i.key()); - } } } void ChartDataSet::removeAllSeries() { QList series = m_seriesDomainMap.keys(); - foreach(QAbstractSeries *s , series) { + foreach (QAbstractSeries *s , series) removeSeries(s); - } - Q_ASSERT(m_seriesAxisXMap.count()==0); - Q_ASSERT(m_seriesAxisXMap.count()==0); - Q_ASSERT(m_seriesDomainMap.count()==0); + Q_ASSERT(m_seriesAxisXMap.count() == 0); + Q_ASSERT(m_seriesAxisXMap.count() == 0); + Q_ASSERT(m_seriesDomainMap.count() == 0); qDeleteAll(series); } @@ -306,7 +303,7 @@ void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) while (i.hasNext()) { i.next(); - i.value()->zoomIn(rect,size); + i.value()->zoomIn(rect, size); } blockAxisSignals(false); @@ -323,7 +320,7 @@ void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) while (i.hasNext()) { i.next(); - i.value()->zoomOut(rect,size); + i.value()->zoomOut(rect, size); } blockAxisSignals(false); @@ -336,16 +333,16 @@ void ChartDataSet::blockAxisSignals(bool enabled) i.next(); QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); - if(axisX) { + if (axisX) { axisX->d_ptr->blockSignals(enabled); - if(!enabled) { + if (!enabled) { axisX->d_ptr->setDirty(false); axisX->d_ptr->emitUpdated(); } } - if(axisY) { + if (axisY) { axisY->d_ptr->blockSignals(enabled); - if(!enabled) { + if (!enabled) { axisY->d_ptr->setDirty(false); axisY->d_ptr->emitUpdated(); } @@ -355,11 +352,12 @@ void ChartDataSet::blockAxisSignals(bool enabled) int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) { - int count=0; + int count = 0; QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { i.next(); - if(i.key()->type()==type) count++; + if (i.key()->type() == type) + count++; } return count; } @@ -377,13 +375,14 @@ int ChartDataSet::seriesIndex(QAbstractSeries *series) QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const { - if(series == 0) { + if (series == 0) { QMapIterator i(m_seriesAxisXMap); while (i.hasNext()) { i.next(); - if(i.value()->isVisible()) return i.value(); + if (i.value()->isVisible()) + return i.value(); } return 0; } @@ -392,12 +391,12 @@ QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const { - if(series == 0) { + if (series == 0) { QMapIterator i(m_seriesAxisYMap); while (i.hasNext()) { i.next(); - if(i.value()->isVisible()) return i.value(); + if (i.value()->isVisible()) return i.value(); } return 0; } @@ -408,25 +407,25 @@ void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Ori { Q_ASSERT(axis); - if(!series) { + if (!series) { qWarning() << "Series not found on the chart."; return; } Domain* domain = m_seriesDomainMap.value(series); - if(!domain) { + if (!domain) { qWarning() << "Series not found on the chart."; return; } - if(orientation==Qt::Horizontal && axis->orientation()==Qt::Vertical) { - qWarning()<<"Axis already defined as axis Y"; + if (orientation == Qt::Horizontal && axis->orientation() == Qt::Vertical) { + qWarning() << "Axis already defined as axis Y"; return; } - if(orientation==Qt::Vertical && axis->orientation()==Qt::Horizontal) { - qWarning()<<"Axis already defined as axis X"; + if (orientation == Qt::Vertical && axis->orientation() == Qt::Horizontal) { + qWarning() << "Axis already defined as axis X"; return; } @@ -434,11 +433,10 @@ void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Ori QMap *seriesAxisMap; - if(orientation==Qt::Vertical) { - seriesAxisMap= &m_seriesAxisYMap; - }else{ - seriesAxisMap= &m_seriesAxisXMap; - } + if (orientation == Qt::Vertical) + seriesAxisMap = &m_seriesAxisYMap; + else + seriesAxisMap = &m_seriesAxisXMap; if (seriesAxisMap->value(series) == axis) { qWarning() << "The axis already set for the series"; @@ -447,21 +445,21 @@ void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Ori QAbstractAxis *oldAxis = seriesAxisMap->take(series); QList axes = seriesAxisMap->values(); - if(oldAxis) { - if(axes.indexOf(oldAxis)==-1) { + if (oldAxis) { + if (axes.indexOf(oldAxis) == -1) { emit axisRemoved(oldAxis); oldAxis->disconnect(); - QObject::disconnect(domain,0,oldAxis,0); - oldAxis->d_ptr->m_dataset=0; + QObject::disconnect(domain, 0, oldAxis, 0); + oldAxis->d_ptr->m_dataset = 0; oldAxis->deleteLater(); } } - if(axes.indexOf(axis)==-1) { - initializeAxis(axis,series); - emit axisAdded(axis,domain); - }else{ - initializeAxis(axis,series); + if (axes.indexOf(axis) == -1) { + initializeAxis(axis, series); + emit axisAdded(axis, domain); + } else { + initializeAxis(axis, series); } } @@ -470,13 +468,13 @@ Domain* ChartDataSet::domain(QAbstractSeries *series) const return m_seriesDomainMap.value(series); } -void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size) +void ChartDataSet::scrollDomain(qreal dx, qreal dy, const QSizeF& size) { blockAxisSignals(true); QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { i.next(); - i.value()->move(dx,dy,size); + i.value()->move(dx, dy, size); } blockAxisSignals(false); } diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 1996209..514b626 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -43,7 +43,7 @@ class QTCOMMERCIALCHART_AUTOTEST_EXPORT ChartDataSet : public QObject { Q_OBJECT public: - ChartDataSet(QChart* parent=0); + ChartDataSet(QChart* parent = 0); virtual ~ChartDataSet(); void addSeries(QAbstractSeries* series); @@ -53,7 +53,7 @@ public: void zoomInDomain(const QRectF& rect, const QSizeF& size); void zoomOutDomain(const QRectF& rect, const QSizeF& size); - void scrollDomain(qreal dx,qreal dy,const QSizeF& size); + void scrollDomain(qreal dx, qreal dy, const QSizeF& size); int seriesCount(QAbstractSeries::SeriesType type); int seriesIndex(QAbstractSeries *series); @@ -73,14 +73,14 @@ Q_SIGNALS: void seriesAdded(QAbstractSeries* series, Domain* domain); void seriesRemoved(QAbstractSeries* series); // void seriesUpdated(QAbstractSeries* series); - void axisAdded(QAbstractAxis* axis,Domain* domain); + void axisAdded(QAbstractAxis* axis, Domain* domain); void axisRemoved(QAbstractAxis* axis); private: - void calculateDomain(QAbstractSeries* series,Domain* domain); - void createAxes(QAbstractAxis::AxisTypes type,Qt::Orientation orientation); - QAbstractAxis* createAxis(QAbstractAxis::AxisType type,Qt::Orientation orientation); - void initializeAxis(QAbstractAxis* axis,QAbstractSeries* series); + void calculateDomain(QAbstractSeries* series, Domain* domain); + void createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation orientation); + QAbstractAxis* createAxis(QAbstractAxis::AxisType type, Qt::Orientation orientation); + void initializeAxis(QAbstractAxis* axis, QAbstractSeries* series); void removeAxes(QAbstractSeries* series); void blockAxisSignals(bool enabled); void createSeriesIndex(QAbstractSeries* series); diff --git a/src/chartelement.cpp b/src/chartelement.cpp index 577403f..3eaf654 100644 --- a/src/chartelement.cpp +++ b/src/chartelement.cpp @@ -24,15 +24,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartElement::ChartElement(ChartPresenter *presenter):QObject(presenter), - m_presenter(presenter), - m_domain(0) +ChartElement::ChartElement(ChartPresenter *presenter) + : QObject(presenter), + m_presenter(presenter), + m_domain(0) { } void ChartElement::setPresenter(ChartPresenter *presenter) { - m_presenter=presenter; + m_presenter = presenter; } ChartPresenter* ChartElement::presenter() const @@ -42,7 +43,7 @@ ChartPresenter* ChartElement::presenter() const void ChartElement::setDomain(Domain *domain) { - m_domain=domain; + m_domain = domain; } Domain* ChartElement::domain() const @@ -53,21 +54,21 @@ Domain* ChartElement::domain() const void ChartElement::handleGeometryChanged(const QRectF& rect) { Q_UNUSED(rect); - qWarning()<<"Slot not implemented"; + qWarning() << "Slot not implemented"; } -void ChartElement::handleDomainChanged(qreal minX,qreal maxX,qreal minY,qreal maxY) +void ChartElement::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) { Q_UNUSED(minX); Q_UNUSED(maxX); Q_UNUSED(minY); Q_UNUSED(maxY); - qWarning()<<"Slot not implemented"; + qWarning() << "Slot not implemented"; } void ChartElement::handleDomainUpdated() { - qWarning()<<"Slot not implemented"; + qWarning() << "Slot not implemented"; } diff --git a/src/chartelement_p.h b/src/chartelement_p.h index a17870b..b7fad64 100644 --- a/src/chartelement_p.h +++ b/src/chartelement_p.h @@ -49,7 +49,7 @@ public: public Q_SLOTS: virtual void handleGeometryChanged(const QRectF& rect); - virtual void handleDomainChanged(qreal minX,qreal maxX,qreal minY,qreal maxY); + virtual void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); virtual void handleDomainUpdated(); virtual ChartAnimation* animation() const { return 0; }; diff --git a/src/chartitem_p.h b/src/chartitem_p.h index f6b85d9..fcdb6b4 100644 --- a/src/chartitem_p.h +++ b/src/chartitem_p.h @@ -38,7 +38,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartItem : public QGraphicsItem, public ChartElement { - enum ChartItemTypes{ AXIS_ITEM = UserType + 1, XYLINE_ITEM }; + enum ChartItemTypes { AXIS_ITEM = UserType + 1, XYLINE_ITEM }; public: ChartItem(ChartPresenter *presenter) : QGraphicsItem(presenter ? presenter->rootItem() : 0), ChartElement(presenter) {} }; diff --git a/src/chartlayout.cpp b/src/chartlayout.cpp index 79f1914..e24cb8c 100644 --- a/src/chartlayout.cpp +++ b/src/chartlayout.cpp @@ -30,10 +30,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE static const qreal golden_ratio = 0.25; -ChartLayout::ChartLayout(ChartPresenter* presenter): - m_presenter(presenter), - m_margins(20,20,20,20), - m_minChartRect(0,0,200,200) +ChartLayout::ChartLayout(ChartPresenter* presenter) + : m_presenter(presenter), + m_margins(20, 20, 20, 20), + m_minChartRect(0, 0, 200, 200) { } @@ -53,33 +53,30 @@ void ChartLayout::setGeometry(const QRectF& rect) QLegend* legend = m_presenter->legend(); ChartBackground* background = m_presenter->backgroundElement(); - QRectF contentGeometry = calculateBackgroundGeometry(rect,background); + QRectF contentGeometry = calculateBackgroundGeometry(rect, background); contentGeometry = calculateContentGeometry(contentGeometry); - if (title && title->isVisible()) { - contentGeometry = calculateTitleGeometry(contentGeometry,title); - } + if (title && title->isVisible()) + contentGeometry = calculateTitleGeometry(contentGeometry, title); - if (legend->isAttachedToChart() && legend->isVisible()) { - contentGeometry = calculateLegendGeometry(contentGeometry,legend); - } + if (legend->isAttachedToChart() && legend->isVisible()) + contentGeometry = calculateLegendGeometry(contentGeometry, legend); - calculateChartGeometry(contentGeometry,axes); + calculateChartGeometry(contentGeometry, axes); //TODO remove me #ifdef SHOW_LAYOUT LayoutDebuger* debuger = LayoutDebuger::instance(); debuger->reset(); debuger->setPen(QPen(Qt::red)); - debuger->add(backgroundGeometry,m_presenter->rootItem()); - debuger->add(titleGeometry,m_presenter->rootItem()); - debuger->add(legendGeometry ,m_presenter->rootItem()); - debuger->add(axisGeometry ,m_presenter->rootItem()); - debuger->add(geometry,m_presenter->rootItem()); - foreach(LegendMarker* marker,legend->d_ptr->markers()){ - debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem()); - } + debuger->add(backgroundGeometry, m_presenter->rootItem()); + debuger->add(titleGeometry, m_presenter->rootItem()); + debuger->add(legendGeometry , m_presenter->rootItem()); + debuger->add(axisGeometry , m_presenter->rootItem()); + debuger->add(geometry, m_presenter->rootItem()); + foreach (LegendMarker *marker, legend->d_ptr->markers()) + debuger->add(marker->mapRectToScene(marker->boundingRect()), m_presenter->rootItem()); #endif QGraphicsLayout::setGeometry(rect); @@ -87,21 +84,22 @@ void ChartLayout::setGeometry(const QRectF& rect) QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const { - return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom()); + return geometry.adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()); } QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const { - return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom()); + return minimum.adjusted(0, 0, m_margins.left() + m_margins.right(), m_margins.top() + m_margins.bottom()); } -QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const +QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry, ChartBackground* background) const { qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); - QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom); - if(background) background->setRect(backgroundGeometry); + QRectF backgroundGeometry = geometry.adjusted(left, top, -right, -bottom); + if (background) + background->setRect(backgroundGeometry); return backgroundGeometry; } @@ -109,14 +107,14 @@ QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const { qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); - return minimum.adjusted(0,0,left + right,top+bottom); + return minimum.adjusted(0, 0, left + right, top + bottom); } QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList& axes) const { - QSizeF vertical(0,0); - QSizeF horizontal(0,0); + QSizeF vertical(0, 0); + QSizeF horizontal(0, 0); int topAxisCount = 0; int bottomAxisCount = 0; @@ -125,26 +123,24 @@ QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QListorientation()==Qt::Vertical && axis->isVisible()) { + if (axis->orientation() == Qt::Vertical && axis->isVisible()) { // vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); if (axis->alternativePlacement()) rightAxisCount++; else leftAxisCount++; - } - else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) { + } else if (axis->orientation() == Qt::Horizontal && axis->isVisible()) { // horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); if (axis->alternativePlacement()) topAxisCount++; else bottomAxisCount++; } - } - qreal width = qMin(vertical.width(),geometry.width() * golden_ratio); + qreal width = qMin(vertical.width(), geometry.width() * golden_ratio); - QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height()); + QRectF rect = geometry.adjusted(width, vertical.height() / 2, -horizontal.width() / 2, -horizontal.height()); // axis area width // TODO: replase with dynamic size code @@ -160,16 +156,15 @@ QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QListorientation()==Qt::Vertical) { + foreach (ChartAxis *axis , axes) { + if (axis->orientation() == Qt::Vertical) { axis->setInternalRect(rect.adjusted(-leftAxisCount * axisWidth, 0, rightAxisCount * axisWidth, 0)); axis->setGeometry(rect.adjusted(-(leftAxisCount + 1) * axisWidth, 0, (rightAxisCount + 1) * axisWidth, 0)); if (axis->alternativePlacement()) rightAxisCount++; else leftAxisCount++; - } - else if(axis->orientation()==Qt::Horizontal) { + } else if (axis->orientation() == Qt::Horizontal) { axis->setInternalRect(rect.adjusted(0, -topAxisCount * axisWidth, 0, bottomAxisCount * axisWidth)); axis->setGeometry(rect.adjusted(0, -(topAxisCount + 1) * axisWidth, 0, (bottomAxisCount + 1) * axisWidth)); if (axis->alternativePlacement()) @@ -184,103 +179,98 @@ QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList& axes) const { - QSizeF vertical(0,0); - QSizeF horizontal(0,0); + QSizeF vertical(0, 0); + QSizeF horizontal(0, 0); // check axis size - foreach(ChartAxis* axis , axes) { - if(axis->orientation()==Qt::Vertical && axis->isVisible()){ + foreach (ChartAxis *axis , axes) { + if (axis->orientation() == Qt::Vertical && axis->isVisible()) vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); - }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) { + else if (axis->orientation() == Qt::Horizontal && axis->isVisible()) horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); - } } - return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height()); + return minimum.adjusted(0, 0, horizontal.width() + vertical.width(), horizontal.height() + vertical.height()); } -QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const +QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry, QLegend* legend) const { - QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1)); + QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1)); QRectF legendRect; QRectF result; switch (legend->alignment()) { - case Qt::AlignTop: { - legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height())); - result = geometry.adjusted(0,legendRect.height(),0,0); + case Qt::AlignTop: + legendRect = QRectF(geometry.topLeft(), QSizeF(geometry.width(), size.height())); + result = geometry.adjusted(0, legendRect.height(), 0, 0); break; - } - case Qt::AlignBottom: { - legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height())); - result = geometry.adjusted(0,0,0,-legendRect.height()); + case Qt::AlignBottom: + legendRect = QRectF(QPointF(geometry.left(), geometry.bottom() - size.height()), QSizeF(geometry.width(), size.height())); + result = geometry.adjusted(0, 0, 0, -legendRect.height()); break; - } case Qt::AlignLeft: { - qreal width = qMin(size.width(),geometry.width()*golden_ratio); - legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height())); - result = geometry.adjusted(width,0,0,0); + qreal width = qMin(size.width(), geometry.width() * golden_ratio); + legendRect = QRectF(geometry.topLeft(), QSizeF(width, geometry.height())); + result = geometry.adjusted(width, 0, 0, 0); break; } case Qt::AlignRight: { - qreal width = qMin(size.width(),geometry.width()*golden_ratio); - legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height())); - result = geometry.adjusted(0,0,-width,0); + qreal width = qMin(size.width(), geometry.width() * golden_ratio); + legendRect = QRectF(QPointF(geometry.right() - width, geometry.top()), QSizeF(width, geometry.height())); + result = geometry.adjusted(0, 0, -width, 0); break; } - default: { - legendRect = QRectF(0,0,0,0); + default: + legendRect = QRectF(0, 0, 0, 0); result = geometry; break; } - } legend->setGeometry(legendRect); return result; } -QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const +QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry, QLegend* legend) const { - QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1)); - return geometry.adjusted(0,0,minSize.width(),minSize.height()); + QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)); + return geometry.adjusted(0, 0, minSize.width(), minSize.height()); } -QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const +QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry, ChartTitle* title) const { title->setGeometry(geometry); QPointF center = geometry.center() - title->boundingRect().center(); - title->setPos(center.x(),title->pos().y()); - return geometry.adjusted(0,title->boundingRect().height(),0,0); + title->setPos(center.x(), title->pos().y()); + return geometry.adjusted(0, title->boundingRect().height(), 0, 0); } -QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const +QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum, ChartTitle* title) const { QSizeF min = title->sizeHint(Qt::MinimumSize); - return minimum.adjusted(0,0,min.width(),min.height()); + return minimum.adjusted(0, 0, min.width(), min.height()); } -QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const +QSizeF ChartLayout::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const { Q_UNUSED(constraint); - if(which == Qt::MinimumSize){ + if (which == Qt::MinimumSize) { QList axes = m_presenter->axisItems(); ChartTitle* title = m_presenter->titleElement(); QLegend* legend = m_presenter->legend(); - QRectF minimumRect(0,0,0,0); + QRectF minimumRect(0, 0, 0, 0); minimumRect = calculateBackgroundMinimum(minimumRect); minimumRect = calculateContentMinimum(minimumRect); - minimumRect = calculateTitleMinimum(minimumRect,title); - minimumRect = calculateLegendMinimum(minimumRect,legend); - minimumRect = calculateAxisMinimum(minimumRect,axes); - return minimumRect.united(m_minChartRect).size().toSize(); - }else - return QSize(-1,-1); + minimumRect = calculateTitleMinimum(minimumRect, title); + minimumRect = calculateLegendMinimum(minimumRect, legend); + minimumRect = calculateAxisMinimum(minimumRect, axes); + return minimumRect.united(m_minChartRect).size().toSize(); + } + return QSize(-1, -1); } void ChartLayout::setMargins(const QMargins& margins) { - - if(m_margins != margins){ + if (m_margins != margins) { m_margins = margins; updateGeometry(); } diff --git a/src/chartlayout_p.h b/src/chartlayout_p.h index ff3c634..bbd0d99 100644 --- a/src/chartlayout_p.h +++ b/src/chartlayout_p.h @@ -46,22 +46,22 @@ public: void adjustChartGeometry(); protected: - QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const; + QSizeF sizeHint ( Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; int count() const { return 0; } QGraphicsLayoutItem* itemAt(int) const { return 0; }; void removeAt(int){}; private: - QRectF calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const; + QRectF calculateBackgroundGeometry(const QRectF& geometry, ChartBackground* background) const; QRectF calculateContentGeometry(const QRectF& geometry) const; QRectF calculateTitleGeometry(const QRectF& geometry, ChartTitle* title) const; - QRectF calculateChartGeometry(const QRectF& geometry,const QList& axes) const; + QRectF calculateChartGeometry(const QRectF& geometry, const QList& axes) const; QRectF calculateLegendGeometry(const QRectF& geometry, QLegend* legend) const; QRectF calculateBackgroundMinimum(const QRectF& minimum) const; QRectF calculateContentMinimum(const QRectF& minimum) const; - QRectF calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const; - QRectF calculateAxisMinimum(const QRectF& minimum,const QList& axes) const; - QRectF calculateLegendMinimum(const QRectF& minimum,QLegend* legend) const; + QRectF calculateTitleMinimum(const QRectF& minimum, ChartTitle* title) const; + QRectF calculateAxisMinimum(const QRectF& minimum, const QList& axes) const; + QRectF calculateLegendMinimum(const QRectF& minimum, QLegend* legend) const; private: ChartPresenter* m_presenter; diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 3a42ed9..1962655 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -35,15 +35,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), -m_chart(chart), -m_dataset(dataset), -m_chartTheme(0), -m_options(QChart::NoAnimation), -m_state(ShowState), -m_layout(new ChartLayout(this)), -m_background(0), -m_title(0) +ChartPresenter::ChartPresenter(QChart* chart, ChartDataSet* dataset) + : QObject(chart), + m_chart(chart), + m_dataset(dataset), + m_chartTheme(0), + m_options(QChart::NoAnimation), + m_state(ShowState), + m_layout(new ChartLayout(this)), + m_background(0), + m_title(0) { } @@ -57,12 +58,10 @@ void ChartPresenter::setChartsGeometry(const QRectF& rect) { Q_ASSERT(rect.isValid()); - if(m_chartsRect!=rect) { - m_chartsRect=rect; - foreach(ChartElement* chart, m_chartItems) - { + if (m_chartsRect != rect) { + m_chartsRect = rect; + foreach (ChartElement *chart, m_chartItems) chart->handleGeometryChanged(rect); - } } } @@ -71,25 +70,25 @@ QRectF ChartPresenter::chartsGeometry() const return m_chartsRect; } -void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain) +void ChartPresenter::handleAxisAdded(QAbstractAxis* axis, Domain* domain) { ChartAxis* item = axis->d_ptr->createGraphics(this); item->setDomain(domain); - if(m_options.testFlag(QChart::GridAxisAnimations)){ + if (m_options.testFlag(QChart::GridAxisAnimations)) item->setAnimation(new AxisAnimation(item)); - } - QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); - QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated())); - QObject::connect(axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleAxisVisibleChanged(bool))); + QObject::connect(this, SIGNAL(geometryChanged(QRectF)), item, SLOT(handleGeometryChanged(QRectF))); + QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated())); + QObject::connect(axis, SIGNAL(visibleChanged(bool)), this, SLOT(handleAxisVisibleChanged(bool))); //initialize domain->emitUpdated(); m_chartTheme->decorate(axis); axis->d_ptr->setDirty(false); axis->d_ptr->emitUpdated(); - if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect); + if (m_chartsRect.isValid()) + item->handleGeometryChanged(m_chartsRect); m_axisItems.insert(axis, item); selectVisibleAxis(); @@ -102,24 +101,25 @@ void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis) selectVisibleAxis(); item->hide(); item->disconnect(); - QObject::disconnect(this,0,item,0); + QObject::disconnect(this, 0, item, 0); item->deleteLater(); } -void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain) +void ChartPresenter::handleSeriesAdded(QAbstractSeries* series, Domain* domain) { ChartElement *item = series->d_ptr->createGraphics(this); Q_ASSERT(item); item->setDomain(domain); - QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); - QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated())); + QObject::connect(this, SIGNAL(geometryChanged(QRectF)), item, SLOT(handleGeometryChanged(QRectF))); + QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated())); //initialize item->handleDomainUpdated(); - if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect); - m_chartItems.insert(series,item); + if (m_chartsRect.isValid()) + item->handleGeometryChanged(m_chartsRect); + m_chartItems.insert(series, item); } void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series) @@ -161,27 +161,26 @@ void ChartPresenter::selectVisibleAxis() void ChartPresenter::handleAxisVisibleChanged(bool visible) { - QAbstractAxis* axis = static_cast (sender()); + QAbstractAxis* axis = static_cast(sender()); Q_ASSERT(axis); - if(visible){ + if (visible) { QMapIterator i(m_axisItems); while (i.hasNext()) { - i.next(); - if(i.key()==axis) { - continue; - } - if(i.key()->orientation()==axis->orientation()) { - i.key()->setVisible(false); - } + i.next(); + if (i.key() == axis) + continue; + if (i.key()->orientation() == axis->orientation()) + i.key()->setVisible(false); } } } -void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) +void ChartPresenter::setTheme(QChart::ChartTheme theme, bool force) { - if(m_chartTheme && m_chartTheme->id() == theme) return; + if (m_chartTheme && m_chartTheme->id() == theme) + return; delete m_chartTheme; m_chartTheme = ChartTheme::createTheme(theme); m_chartTheme->setForced(force); @@ -202,8 +201,8 @@ QChart::ChartTheme ChartPresenter::theme() void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) { - if(m_options!=options) { - m_options=options; + if (m_options != options) { + m_options = options; resetAllElements(); } @@ -213,30 +212,30 @@ void ChartPresenter::resetAllElements() { QMapIterator i(m_axisItems); while (i.hasNext()) { - i.next(); - Domain* domain = i.value()->domain(); - QAbstractAxis* axis = i.key(); - handleAxisRemoved(axis); - handleAxisAdded(axis,domain); + i.next(); + Domain* domain = i.value()->domain(); + QAbstractAxis* axis = i.key(); + handleAxisRemoved(axis); + handleAxisAdded(axis, domain); } QMapIterator j(m_chartItems); - while (j.hasNext()) { - j.next(); - Domain* domain = j.value()->domain(); - QAbstractSeries* series = j.key(); - handleSeriesRemoved(series); - handleSeriesAdded(series,domain); - } + while (j.hasNext()) { + j.next(); + Domain* domain = j.value()->domain(); + QAbstractSeries* series = j.key(); + handleSeriesRemoved(series); + handleSeriesAdded(series, domain); + } - layout()->invalidate(); + layout()->invalidate(); } void ChartPresenter::zoomIn(qreal factor) { QRectF rect = chartsGeometry(); - rect.setWidth(rect.width()/factor); - rect.setHeight(rect.height()/factor); + rect.setWidth(rect.width() / factor); + rect.setHeight(rect.height() / factor); rect.moveCenter(chartsGeometry().center()); zoomIn(rect); } @@ -249,8 +248,8 @@ void ChartPresenter::zoomIn(const QRectF& rect) return; m_state = ZoomInState; - m_statePoint = QPointF(r.center().x()/chartsGeometry().width(),r.center().y()/chartsGeometry().height()); - m_dataset->zoomInDomain(r,chartsGeometry().size()); + m_statePoint = QPointF(r.center().x() / chartsGeometry().width(), r.center().y() / chartsGeometry().height()); + m_dataset->zoomInDomain(r, chartsGeometry().size()); m_state = ShowState; } @@ -262,23 +261,23 @@ void ChartPresenter::zoomOut(qreal factor) chartRect.setSize(chartsGeometry().size()); QRectF rect; - rect.setSize(chartRect.size()/factor); + rect.setSize(chartRect.size() / factor); rect.moveCenter(chartRect.center()); if (!rect.isValid()) return; - m_statePoint = QPointF(rect.center().x()/chartsGeometry().width(),rect.center().y()/chartsGeometry().height()); + m_statePoint = QPointF(rect.center().x() / chartsGeometry().width(), rect.center().y() / chartsGeometry().height()); m_dataset->zoomOutDomain(rect, chartRect.size()); m_state = ShowState; } -void ChartPresenter::scroll(qreal dx,qreal dy) +void ChartPresenter::scroll(qreal dx, qreal dy) { - if(dx<0) m_state=ScrollLeftState; - if(dx>0) m_state=ScrollRightState; - if(dy<0) m_state=ScrollUpState; - if(dy>0) m_state=ScrollDownState; + if (dx < 0) m_state = ScrollLeftState; + if (dx > 0) m_state = ScrollRightState; + if (dy < 0) m_state = ScrollUpState; + if (dy > 0) m_state = ScrollDownState; - m_dataset->scrollDomain(dx,dy,chartsGeometry().size()); + m_dataset->scrollDomain(dx, dy, chartsGeometry().size()); m_state = ShowState; } @@ -307,17 +306,18 @@ void ChartPresenter::createTitleItem() void ChartPresenter::handleAnimationFinished() { - m_animations.removeAll(qobject_cast(sender())); - if(m_animations.empty()) emit animationsFinished(); + m_animations.removeAll(qobject_cast(sender())); + if (m_animations.empty()) + emit animationsFinished(); } void ChartPresenter::startAnimation(ChartAnimation* animation) { - if (animation->state() != QAbstractAnimation::Stopped) animation->stop(); - QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection); - if(!m_animations.isEmpty()){ + if (animation->state() != QAbstractAnimation::Stopped) + animation->stop(); + QObject::connect(animation, SIGNAL(finished()), this, SLOT(handleAnimationFinished()), Qt::UniqueConnection); + if (!m_animations.isEmpty()) m_animations.append(animation); - } QTimer::singleShot(0, animation, SLOT(start())); } @@ -330,7 +330,8 @@ void ChartPresenter::setBackgroundBrush(const QBrush& brush) QBrush ChartPresenter::backgroundBrush() const { - if (!m_background) return QBrush(); + if (!m_background) + return QBrush(); return m_background->brush(); } @@ -343,7 +344,8 @@ void ChartPresenter::setBackgroundPen(const QPen& pen) QPen ChartPresenter::backgroundPen() const { - if (!m_background) return QPen(); + if (!m_background) + return QPen(); return m_background->pen(); } @@ -356,7 +358,8 @@ void ChartPresenter::setTitle(const QString& title) QString ChartPresenter::title() const { - if (!m_title) return QString(); + if (!m_title) + return QString(); return m_title->text(); } @@ -369,7 +372,8 @@ void ChartPresenter::setTitleFont(const QFont& font) QFont ChartPresenter::titleFont() const { - if (!m_title) return QFont(); + if (!m_title) + return QFont(); return m_title->font(); } @@ -382,7 +386,8 @@ void ChartPresenter::setTitleBrush(const QBrush &brush) QBrush ChartPresenter::titleBrush() const { - if (!m_title) return QBrush(); + if (!m_title) + return QBrush(); return m_title->brush(); } @@ -395,7 +400,8 @@ void ChartPresenter::setBackgroundVisible(bool visible) bool ChartPresenter::isBackgroundVisible() const { - if (!m_background) return false; + if (!m_background) + return false; return m_background->isVisible(); } @@ -407,7 +413,8 @@ void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled) bool ChartPresenter::isBackgroundDropShadowEnabled() const { - if (!m_background) return false; + if (!m_background) + return false; return m_background->isDropShadowEnabled(); } diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index a17264b..234f246 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -78,7 +78,7 @@ public: ZoomOutState }; - ChartPresenter(QChart* chart,ChartDataSet *dataset); + ChartPresenter(QChart* chart, ChartDataSet *dataset); virtual ~ChartPresenter(); ChartTheme *chartTheme() const { return m_chartTheme; } @@ -113,7 +113,7 @@ public: void setVisible(bool visible); - void setTheme(QChart::ChartTheme theme,bool force = true); + void setTheme(QChart::ChartTheme theme, bool force = true); QChart::ChartTheme theme(); void setAnimationOptions(QChart::AnimationOptions options); @@ -122,7 +122,7 @@ public: void zoomIn(qreal factor); void zoomIn(const QRectF& rect); void zoomOut(qreal factor); - void scroll(qreal dx,qreal dy); + void scroll(qreal dx, qreal dy); void setChartsGeometry(const QRectF& rect); QRectF chartsGeometry() const; @@ -143,9 +143,9 @@ private: void selectVisibleAxis(); public Q_SLOTS: - void handleSeriesAdded(QAbstractSeries* series,Domain* domain); + void handleSeriesAdded(QAbstractSeries* series, Domain* domain); void handleSeriesRemoved(QAbstractSeries* series); - void handleAxisAdded(QAbstractAxis* axis,Domain* domain); + void handleAxisAdded(QAbstractAxis* axis, Domain* domain); void handleAxisRemoved(QAbstractAxis* axis); void handleAxisVisibleChanged(bool visible); diff --git a/src/charttheme.cpp b/src/charttheme.cpp index a655d6d..a8fee6e 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -75,13 +75,13 @@ ChartTheme::ChartTheme(QChart::ChartTheme id) : m_gridLinePen(QPen(QRgb(0x000000))), m_force(false) { - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); } ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) { - switch(theme) { + switch (theme) { case QChart::ChartThemeLight: return new ChartThemeLight(); case QChart::ChartThemeBlueCerulean: @@ -105,7 +105,7 @@ void ChartTheme::decorate(QChart *chart) { QBrush brush; - if(m_force || brush == chart->backgroundBrush()) + if (m_force || brush == chart->backgroundBrush()) chart->setBackgroundBrush(m_chartBackgroundGradient); chart->setTitleFont(m_masterFont); chart->setTitleBrush(m_labelBrush); @@ -136,7 +136,7 @@ void ChartTheme::decorate(QAreaSeries *series, int index) QPen pen; QBrush brush; - if (m_force || pen == series->pen()){ + if (m_force || pen == series->pen()) { pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); pen.setWidthF(2); series->setPen(pen); @@ -149,11 +149,11 @@ void ChartTheme::decorate(QAreaSeries *series, int index) } -void ChartTheme::decorate(QLineSeries *series,int index) +void ChartTheme::decorate(QLineSeries *series, int index) { QPen pen; - if(m_force || pen == series->pen()){ - pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); + if (m_force || pen == series->pen()) { + pen.setColor(m_seriesColors.at(index % m_seriesColors.size())); pen.setWidthF(2); series->setPen(pen); } @@ -167,7 +167,7 @@ void ChartTheme::decorate(QAbstractBarSeries *series, int index) qreal takeAtPos = 0.5; qreal step = 0.2; - if (sets.count() > 1 ) { + if (sets.count() > 1) { step = 1.0 / (qreal) sets.count(); if (sets.count() % m_seriesGradients.count()) step *= m_seriesGradients.count(); @@ -228,7 +228,7 @@ void ChartTheme::decorate(QPieSeries *series, int index) QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); // Get color for a slice from a gradient linearly, beginning from the start of the gradient - qreal pos = (qreal) (i + 1) / (qreal) series->count(); + qreal pos = (qreal)(i + 1) / (qreal) series->count(); QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); QPieSlice *s = series->slices().at(i); @@ -251,8 +251,8 @@ void ChartTheme::decorate(QPieSeries *series, int index) void ChartTheme::decorate(QSplineSeries *series, int index) { QPen pen; - if(m_force || pen == series->pen()){ - pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); + if (m_force || pen == series->pen()) { + pen.setColor(m_seriesColors.at(index % m_seriesColors.size())); pen.setWidthF(2); series->setPen(pen); } @@ -264,54 +264,46 @@ void ChartTheme::decorate(QAbstractAxis *axis) QBrush brush; QFont font; - bool axisX = axis->orientation()== Qt::Horizontal; + bool axisX = axis->orientation() == Qt::Horizontal; if (axis->isLineVisible()) { - if(m_force || brush == axis->labelsBrush()){ + if (m_force || brush == axis->labelsBrush()) axis->setLabelsBrush(m_labelBrush); - } - if(m_force || pen == axis->labelsPen()){ - axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons - } + if (m_force || pen == axis->labelsPen()) + axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons if (m_force || axis->shadesVisible()) { - if(m_force || brush == axis->shadesBrush()){ + if (m_force || brush == axis->shadesBrush()) axis->setShadesBrush(m_backgroundShadesBrush); - } - if(m_force || pen == axis->shadesPen()){ + if (m_force || pen == axis->shadesPen()) axis->setShadesPen(m_backgroundShadesPen); - } - if( m_force && (m_backgroundShades == BackgroundShadesBoth + if (m_force && (m_backgroundShades == BackgroundShadesBoth || (m_backgroundShades == BackgroundShadesVertical && axisX) - || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ + || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))) { axis->setShadesVisible(true); - } } - if(m_force || pen == axis->linePen()){ + if (m_force || pen == axis->linePen()) axis->setLinePen(m_axisLinePen); - } - if(m_force || pen == axis->gridLinePen()){ + if (m_force || pen == axis->gridLinePen()) axis->setGridLinePen(m_gridLinePen); - } - if(m_force || font == axis->labelsFont()){ + if (m_force || font == axis->labelsFont()) axis->setLabelsFont(m_labelFont); - } } } void ChartTheme::generateSeriesGradients() { // Generate gradients in HSV color space - foreach (const QColor& color, m_seriesColors) { + foreach (const QColor &color, m_seriesColors) { QLinearGradient g; qreal h = color.hsvHueF(); qreal s = color.hsvSaturationF(); diff --git a/src/charttheme_p.h b/src/charttheme_p.h index cdc6a20..08c8f6b 100644 --- a/src/charttheme_p.h +++ b/src/charttheme_p.h @@ -69,7 +69,7 @@ protected: explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight); public: static ChartTheme *createTheme(QChart::ChartTheme theme); - QChart::ChartTheme id() const {return m_id;} + QChart::ChartTheme id() const { return m_id; } void decorate(QChart *chart); void decorate(QLegend *legend); void decorate(QAbstractBarSeries *series, int index); diff --git a/src/charttitle.cpp b/src/charttitle.cpp index 816bdbf..341db7c 100644 --- a/src/charttitle.cpp +++ b/src/charttitle.cpp @@ -25,7 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartTitle::ChartTitle(QGraphicsItem* parent):QGraphicsSimpleTextItem(parent) +ChartTitle::ChartTitle(QGraphicsItem* parent) + : QGraphicsSimpleTextItem(parent) { } @@ -37,7 +38,7 @@ ChartTitle::~ChartTitle() void ChartTitle::setText(const QString &text) { - m_text=text; + m_text = text; } QString ChartTitle::text() const @@ -47,48 +48,47 @@ QString ChartTitle::text() const void ChartTitle::setGeometry(const QRectF &rect) { - QFontMetrics fn(font()); + QFontMetrics fn(font()); - int width = rect.width(); + int width = rect.width(); - if (fn.boundingRect(m_text).width() > width) - { - QString string = m_text + "..."; - while (fn.boundingRect(string).width() > width && string.length() > 3) - string.remove(string.length() - 4, 1); - QGraphicsSimpleTextItem::setText(string); - } - else - QGraphicsSimpleTextItem::setText(m_text); + if (fn.boundingRect(m_text).width() > width) { + QString string = m_text + "..."; + while (fn.boundingRect(string).width() > width && string.length() > 3) + string.remove(string.length() - 4, 1); + QGraphicsSimpleTextItem::setText(string); + } else { + QGraphicsSimpleTextItem::setText(m_text); + } - setPos(rect.topLeft()); + setPos(rect.topLeft()); } QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { - Q_UNUSED(constraint); - QFontMetrics fn (font ()); - QSizeF sh; + Q_UNUSED(constraint); + QFontMetrics fn(font()); + QSizeF sh; - switch(which) { + switch (which) { case Qt::MinimumSize: - sh = QSizeF(fn.boundingRect ("...").width(),fn.height()); + sh = QSizeF(fn.boundingRect("...").width(), fn.height()); break; case Qt::PreferredSize: - sh = QSizeF(fn.boundingRect(m_text).width(),fn.height()); + sh = QSizeF(fn.boundingRect(m_text).width(), fn.height()); break; case Qt::MaximumSize: - sh = QSizeF(fn.boundingRect(m_text).width(),fn.height()); + sh = QSizeF(fn.boundingRect(m_text).width(), fn.height()); break; case Qt::MinimumDescent: - sh = QSizeF(0, fn.descent ()); + sh = QSizeF(0, fn.descent()); break; default: - break; - } + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/domain.cpp b/src/domain.cpp index 84d6183..939cd90 100644 --- a/src/domain.cpp +++ b/src/domain.cpp @@ -23,11 +23,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -Domain::Domain(QObject* parent) : QObject(parent), - m_minX(0), - m_maxX(0), - m_minY(0), - m_maxY(0) +Domain::Domain(QObject* parent) + : QObject(parent), + m_minX(0), + m_maxX(0), + m_minY(0), + m_maxY(0) { } @@ -41,27 +42,26 @@ void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) bool axisYChanged = false; if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) { - m_minX=minX; - m_maxX=maxX; - axisXChanged=true; - emit rangeXChanged(m_minX,m_maxX); + m_minX = minX; + m_maxX = maxX; + axisXChanged = true; + emit rangeXChanged(m_minX, m_maxX); } if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) { - m_minY=minY; - m_maxY=maxY; - axisYChanged=true; - emit rangeYChanged(m_minY,m_maxY); + m_minY = minY; + m_maxY = maxY; + axisYChanged = true; + emit rangeYChanged(m_minY, m_maxY); } - if(axisXChanged || axisYChanged) { + if (axisXChanged || axisYChanged) emit updated(); - } } void Domain::setRangeX(qreal min, qreal max) { - setRange(min,max,m_minY, m_maxY); + setRange(min, max, m_minY, m_maxY); } void Domain::setRangeY(qreal min, qreal max) @@ -121,7 +121,7 @@ void Domain::zoomIn(const QRectF& rect, const QSizeF& size) minY = maxY - dy * rect.bottom(); maxY = maxY - dy * rect.top(); - setRange(minX,maxX,minY,maxY); + setRange(minX, maxX, minY, maxY); } void Domain::zoomOut(const QRectF& rect, const QSizeF& size) @@ -139,10 +139,10 @@ void Domain::zoomOut(const QRectF& rect, const QSizeF& size) maxY = minY + dy * rect.bottom(); minY = maxY - dy * size.height(); - setRange(minX,maxX,minY,maxY); + setRange(minX, maxX, minY, maxY); } -void Domain::move(qreal dx,qreal dy,const QSizeF& size) +void Domain::move(qreal dx, qreal dy, const QSizeF& size) { qreal x = spanX() / size.width(); qreal y = spanY() / size.height(); @@ -152,15 +152,15 @@ void Domain::move(qreal dx,qreal dy,const QSizeF& size) qreal minY = m_minY; qreal maxY = m_maxY; - if(dx!=0) { + if (dx != 0) { minX = minX + x * dx; maxX = maxX + x * dx; } - if(dy!=0) { + if (dy != 0) { minY = minY + y * dy; maxY = maxY + y * dy; } - setRange(minX,maxX,minY,maxY); + setRange(minX, maxX, minY, maxY); } void Domain::emitUpdated() @@ -173,19 +173,18 @@ void Domain::handleAxisUpdated() QAbstractAxisPrivate* axis = qobject_cast(sender()); Q_ASSERT(axis); axis->setDirty(false); - if(axis->orientation()==Qt::Horizontal){ - setRangeX(axis->min(),axis->max()); - }else if(axis->orientation()==Qt::Vertical){ - setRangeY(axis->min(),axis->max()); - } + if (axis->orientation() == Qt::Horizontal) + setRangeX(axis->min(), axis->max()); + else if (axis->orientation() == Qt::Vertical) + setRangeY(axis->min(), axis->max()); } bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2) { return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) && - qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) && - qFuzzyIsNull(domain1.m_minX - domain2.m_minX) && - qFuzzyIsNull(domain1.m_minY - domain2.m_minY)); + qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) && + qFuzzyIsNull(domain1.m_minX - domain2.m_minX) && + qFuzzyIsNull(domain1.m_minY - domain2.m_minY)); } @@ -197,7 +196,7 @@ bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const Domain &domain1, const QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain) { - dbg.nospace() << "Domain("<alignment()) { - case Qt::AlignTop: - case Qt::AlignBottom: { - scrollHorizontal = true; - break; - } - case Qt::AlignLeft: - case Qt::AlignRight: { - scrollHorizontal = false; - break; - } + switch (m_legend->alignment()) { + case Qt::AlignTop: + case Qt::AlignBottom: + scrollHorizontal = true; + break; + case Qt::AlignLeft: + case Qt::AlignRight: + scrollHorizontal = false; + break; } // If detached, the scrolling direction is vertical instead of horizontal and vice versa. - if (!m_legend->isAttachedToChart()) { + if (!m_legend->isAttachedToChart()) scrollHorizontal = !scrollHorizontal; - } QRectF boundingRect = geometry(); qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); - boundingRect.adjust(left,top,-right,-bottom); + boundingRect.adjust(left, top, -right, -bottom); // Limit offset between m_minOffset and m_maxOffset if (scrollHorizontal) { - if(m_width<=boundingRect.width()) return; + if (m_width <= boundingRect.width()) + return; if (x != m_offsetX) { m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX); - m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top()); + m_legend->d_ptr->items()->setPos(-m_offsetX, boundingRect.top()); } - } - else { - if(m_height<=boundingRect.height()) return; + } else { + if (m_height <= boundingRect.height()) + return; if (y != m_offsetY) { m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY); - m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY); + m_legend->d_ptr->items()->setPos(boundingRect.left(), -m_offsetY); } } } QPointF LegendLayout::offset() const { - return QPointF(m_offsetX,m_offsetY); + return QPointF(m_offsetX, m_offsetY); } void LegendLayout::invalidate() { QGraphicsLayout::invalidate(); - if(m_legend->isAttachedToChart()){ + if (m_legend->isAttachedToChart()) m_legend->d_ptr->m_presenter->layout()->invalidate(); - } } void LegendLayout::setGeometry(const QRectF& rect) @@ -101,84 +98,79 @@ void LegendLayout::setGeometry(const QRectF& rect) QGraphicsLayout::setGeometry(rect); - if(m_legend->isAttachedToChart()) { + if (m_legend->isAttachedToChart()) setAttachedGeometry(rect); - } else { + else setDettachedGeometry(rect); - } - } void LegendLayout::setAttachedGeometry(const QRectF& rect) { - if (!rect.isValid()) return; - m_offsetX=0; - m_offsetY=0; + if (!rect.isValid()) + return; - QSizeF size(0,0); + m_offsetX = 0; + m_offsetY = 0; - if( m_legend->d_ptr->markers().isEmpty()) return; + QSizeF size(0, 0); - m_width=0; - m_height=0; + if (m_legend->d_ptr->markers().isEmpty()) + return; + + m_width = 0; + m_height = 0; qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); - QRectF geometry = rect.adjusted(left,top,-right,-bottom); - - switch(m_legend->alignment()) { + QRectF geometry = rect.adjusted(left, top, -right, -bottom); - case Qt::AlignTop: - case Qt::AlignBottom: { - QPointF point(0,0); - foreach (LegendMarker* marker, m_legend->d_ptr->markers()) { + switch (m_legend->alignment()) { + case Qt::AlignTop: + case Qt::AlignBottom: { + QPointF point(0, 0); + foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { if (marker->isVisible()) { marker->setGeometry(geometry); - marker->setPos(point.x(),geometry.height()/2 - marker->boundingRect().height()/2); + marker->setPos(point.x(), geometry.height() / 2 - marker->boundingRect().height() / 2); const QRectF& rect = marker->boundingRect(); size = size.expandedTo(rect.size()); qreal w = rect.width(); - m_width+=w; + m_width += w; point.setX(point.x() + w); } } - if(m_widthd_ptr->items()->setPos(geometry.width()/2-m_width/2,geometry.top()); - - } - else { + if (m_width < geometry.width()) + m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top()); + else m_legend->d_ptr->items()->setPos(geometry.topLeft()); - } - m_height=size.height(); + m_height = size.height(); } break; - case Qt::AlignLeft: - case Qt::AlignRight: { - QPointF point(0,0); - foreach (LegendMarker* marker, m_legend->d_ptr->markers()) { - if (marker->isVisible()) { - marker->setGeometry(geometry); - marker->setPos(point); - const QRectF& rect = marker->boundingRect(); - qreal h = rect.height(); - size = size.expandedTo(rect.size()); - m_height+=h; - point.setY(point.y() + h); - } + case Qt::AlignLeft: + case Qt::AlignRight: { + QPointF point(0, 0); + foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { + if (marker->isVisible()) { + marker->setGeometry(geometry); + marker->setPos(point); + const QRectF& rect = marker->boundingRect(); + qreal h = rect.height(); + size = size.expandedTo(rect.size()); + m_height += h; + point.setY(point.y() + h); } - if(m_heightd_ptr->items()->setPos(geometry.left(),geometry.height()/2-m_height/2); - } - else { - m_legend->d_ptr->items()->setPos(geometry.topLeft()); - } - m_width=size.width(); } + + if (m_height < geometry.height()) + m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2); + else + m_legend->d_ptr->items()->setPos(geometry.topLeft()); + m_width = size.width(); break; + } } - m_minOffsetX = -left; m_minOffsetY = - top; m_maxOffsetX = m_width - geometry.width() - right; @@ -187,47 +179,49 @@ void LegendLayout::setAttachedGeometry(const QRectF& rect) void LegendLayout::setDettachedGeometry(const QRectF& rect) { - if (!rect.isValid()) return; + if (!rect.isValid()) + return; // Detached layout is different. // In detached mode legend may have multiple rows and columns, so layout calculations // differ a log from attached mode. // Also the scrolling logic is bit different. - m_offsetX=0; - m_offsetY=0; + m_offsetX = 0; + m_offsetY = 0; qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); - QRectF geometry = rect.adjusted(left,top,-right,-bottom); + QRectF geometry = rect.adjusted(left, top, -right, -bottom); - QSizeF size(0,0); + QSizeF size(0, 0); QList markers = m_legend->d_ptr->markers(); - if(markers.isEmpty()) return; + if (markers.isEmpty()) + return; switch (m_legend->alignment()) { - case Qt::AlignTop: { - QPointF point(0,0); + case Qt::AlignTop: { + QPointF point(0, 0); m_width = 0; m_height = 0; - for (int i=0; iisVisible()) { marker->setGeometry(geometry); - marker->setPos(point.x(),point.y()); + marker->setPos(point.x(), point.y()); const QRectF& boundingRect = marker->boundingRect(); qreal w = boundingRect.width(); qreal h = boundingRect.height(); - m_width = qMax(m_width,w); - m_height = qMax(m_height,h); + m_width = qMax(m_width, w); + m_height = qMax(m_height, h); point.setX(point.x() + w); if (point.x() + w > geometry.left() + geometry.width() - right) { // Next item would go off rect. point.setX(0); point.setY(point.y() + h); - if (i+1 < markers.count()) { + if (i + 1 < markers.count()) { m_height += h; } } @@ -241,26 +235,26 @@ void LegendLayout::setDettachedGeometry(const QRectF& rect) m_maxOffsetY = m_height - geometry.height() - bottom; } break; - case Qt::AlignBottom: { - QPointF point(0,geometry.height()); + case Qt::AlignBottom: { + QPointF point(0, geometry.height()); m_width = 0; m_height = 0; - for (int i=0; iisVisible()) { marker->setGeometry(geometry); const QRectF& boundingRect = marker->boundingRect(); qreal w = boundingRect.width(); qreal h = boundingRect.height(); - m_width = qMax(m_width,w); - m_height = qMax(m_height,h); - marker->setPos(point.x(),point.y() - h); + m_width = qMax(m_width, w); + m_height = qMax(m_height, h); + marker->setPos(point.x(), point.y() - h); point.setX(point.x() + w); if (point.x() + w > geometry.left() + geometry.width() - right) { // Next item would go off rect. point.setX(0); point.setY(point.y() - h); - if (i+1 < markers.count()) { + if (i + 1 < markers.count()) { m_height += h; } } @@ -274,27 +268,27 @@ void LegendLayout::setDettachedGeometry(const QRectF& rect) m_maxOffsetY = -bottom; } break; - case Qt::AlignLeft: { - QPointF point(0,0); + case Qt::AlignLeft: { + QPointF point(0, 0); m_width = 0; m_height = 0; qreal maxWidth = 0; - for (int i=0; iisVisible()) { marker->setGeometry(geometry); const QRectF& boundingRect = marker->boundingRect(); qreal w = boundingRect.width(); qreal h = boundingRect.height(); - m_height = qMax(m_height,h); - maxWidth = qMax(maxWidth,w); - marker->setPos(point.x(),point.y()); + m_height = qMax(m_height, h); + maxWidth = qMax(maxWidth, w); + marker->setPos(point.x(), point.y()); point.setY(point.y() + h); if (point.y() + h > geometry.bottom() - bottom) { // Next item would go off rect. point.setX(point.x() + maxWidth); point.setY(0); - if (i+1 < markers.count()) { + if (i + 1 < markers.count()) { m_width += maxWidth; maxWidth = 0; } @@ -310,27 +304,27 @@ void LegendLayout::setDettachedGeometry(const QRectF& rect) m_maxOffsetY = m_height - geometry.height() - bottom; } break; - case Qt::AlignRight: { - QPointF point(geometry.width(),0); + case Qt::AlignRight: { + QPointF point(geometry.width(), 0); m_width = 0; m_height = 0; qreal maxWidth = 0; - for (int i=0; iisVisible()) { marker->setGeometry(geometry); const QRectF& boundingRect = marker->boundingRect(); qreal w = boundingRect.width(); qreal h = boundingRect.height(); - m_height = qMax(m_height,h); - maxWidth = qMax(maxWidth,w); - marker->setPos(point.x() - w,point.y()); + m_height = qMax(m_height, h); + maxWidth = qMax(maxWidth, w); + marker->setPos(point.x() - w, point.y()); point.setY(point.y() + h); - if (point.y() + h > geometry.bottom()-bottom) { + if (point.y() + h > geometry.bottom() - bottom) { // Next item would go off rect. point.setX(point.x() - maxWidth); point.setY(0); - if (i+1 < markers.count()) { + if (i + 1 < markers.count()) { m_width += maxWidth; maxWidth = 0; } @@ -346,47 +340,42 @@ void LegendLayout::setDettachedGeometry(const QRectF& rect) m_maxOffsetY = m_height - geometry.height() - bottom; } break; - default: + default: break; } } -QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const +QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const { QSizeF size(0, 0); qreal left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); - if(constraint.isValid()) { - foreach(LegendMarker* marker, m_legend->d_ptr->markers()) { + if (constraint.isValid()) { + foreach (LegendMarker *marker, m_legend->d_ptr->markers()) size = size.expandedTo(marker->effectiveSizeHint(which)); - } size = size.boundedTo(constraint); - } - else if (constraint.width() >= 0) { + } else if (constraint.width() >= 0) { qreal width = 0; qreal height = 0; - foreach(LegendMarker* marker, m_legend->d_ptr->markers()) { - width+=marker->effectiveSizeHint(which).width(); - height=qMax(height,marker->effectiveSizeHint(which).height()); + foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { + width += marker->effectiveSizeHint(which).width(); + height = qMax(height, marker->effectiveSizeHint(which).height()); } - size = QSizeF(qMin(constraint.width(),width), height); - } - else if (constraint.height() >= 0) { + size = QSizeF(qMin(constraint.width(), width), height); + } else if (constraint.height() >= 0) { qreal width = 0; qreal height = 0; - foreach(LegendMarker* marker, m_legend->d_ptr->markers()) { - width=qMax(width,marker->effectiveSizeHint(which).width()); - height+=height,marker->effectiveSizeHint(which).height(); + foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { + width = qMax(width, marker->effectiveSizeHint(which).width()); + height += height, marker->effectiveSizeHint(which).height(); } - size = QSizeF(width,qMin(constraint.height(),height)); - } - else { - foreach(LegendMarker* marker, m_legend->d_ptr->markers()) { + size = QSizeF(width, qMin(constraint.height(), height)); + } else { + foreach (LegendMarker *marker, m_legend->d_ptr->markers()) size = size.expandedTo(marker->effectiveSizeHint(which)); - } } size += QSize(left + right, top + bottom); return size; diff --git a/src/legend/legendlayout_p.h b/src/legend/legendlayout_p.h index 9f97d7f..a869679 100644 --- a/src/legend/legendlayout_p.h +++ b/src/legend/legendlayout_p.h @@ -41,7 +41,7 @@ public: void invalidate(); protected: - QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const; + QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF()) const; int count() const { return 0; } QGraphicsLayoutItem* itemAt(int) const { return 0; }; void removeAt(int){}; @@ -60,7 +60,6 @@ private: qreal m_maxOffsetY; qreal m_width; qreal m_height; - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/legendmarker.cpp b/src/legend/legendmarker.cpp index 82a5638..4d78dac 100644 --- a/src/legend/legendmarker.cpp +++ b/src/legend/legendmarker.cpp @@ -39,8 +39,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : QGraphicsObject(legend), m_series(series), - m_markerRect(0,0,10.0,10.0), - m_boundingRect(0,0,0,0), + m_markerRect(0, 0, 10.0, 10.0), + m_boundingRect(0, 0, 0, 0), m_legend(legend), m_textItem(new QGraphicsSimpleTextItem(this)), m_rectItem(new QGraphicsRectItem(this)), @@ -75,7 +75,7 @@ void LegendMarker::setFont(const QFont &font) { m_textItem->setFont(font); QFontMetrics fn(font); - m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); + m_markerRect = QRectF(0, 0, fn.height() / 2, fn.height() / 2); updateGeometry(); m_legend->layout()->invalidate(); } @@ -115,31 +115,28 @@ QBrush LegendMarker::labelBrush() const void LegendMarker::setGeometry(const QRectF& rect) { - QFontMetrics fn (font()); + QFontMetrics fn(font()); int width = rect.width(); qreal x = m_margin + m_markerRect.width() + m_space + m_margin; - qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin); + qreal y = qMax(m_markerRect.height() + 2 * m_margin, fn.height() + 2 * m_margin); - if (fn.boundingRect(m_text).width() + x > width) - { + if (fn.boundingRect(m_text).width() + x > width) { QString string = m_text + "..."; - while(fn.boundingRect(string).width() + x > width && string.length() > 3) + while (fn.boundingRect(string).width() + x > width && string.length() > 3) string.remove(string.length() - 4, 1); m_textItem->setText(string); - } - else + } else { m_textItem->setText(m_text); + } const QRectF& textRect = m_textItem->boundingRect(); - - - m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2); + m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2); m_rectItem->setRect(m_markerRect); - m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2); + m_rectItem->setPos(m_margin, y / 2 - m_markerRect.height() / 2); prepareGeometryChange(); - m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y); + m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y); } void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) @@ -156,17 +153,17 @@ QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) cons QFontMetrics fn(m_textItem->font()); QSizeF sh; - switch (which) { - case Qt::MinimumSize: - sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); - break; - case Qt::PreferredSize: - sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); - break; - default: - break; - } - return sh; + switch (which) { + case Qt::MinimumSize: + sh = QSizeF(fn.boundingRect("...").width() + 2 * m_margin + m_space + m_markerRect.width(), qMax(m_markerRect.height() + 2 * m_margin, fn.height() + 2 * m_margin)); + break; + case Qt::PreferredSize: + sh = QSizeF(fn.boundingRect(m_text).width() + 2 * m_margin + m_space + m_markerRect.width(), qMax(m_markerRect.height() + 2 * m_margin, fn.height() + 2 * m_margin)); + break; + default: + break; + } + return sh; } void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) @@ -177,11 +174,12 @@ void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), -m_series(series) +AreaLegendMarker::AreaLegendMarker(QAreaSeries *series, QLegend *legend) + : LegendMarker(series, legend), + m_series(series) { //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); - QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); + QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(updated())); QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); updated(); } @@ -194,8 +192,9 @@ void AreaLegendMarker::updated() ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), -m_barset(barset) +BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries, QBarSet *barset, QLegend *legend) + : LegendMarker(barseries, legend), + m_barset(barset) { //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); @@ -211,8 +210,9 @@ void BarLegendMarker::updated() ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), -m_pieslice(pieslice) +PieLegendMarker::PieLegendMarker(QPieSeries* series, QPieSlice *pieslice, QLegend *legend) + : LegendMarker(series, legend), + m_pieslice(pieslice) { QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); @@ -227,11 +227,12 @@ void PieLegendMarker::updated() ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), -m_series(series) +XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) + : LegendMarker(series, legend), + m_series(series) { //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); - QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); + QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(updated())); QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); updated(); } @@ -240,14 +241,10 @@ void XYLegendMarker::updated() { setLabel(m_series->name()); - if(m_series->type()== QAbstractSeries::SeriesTypeScatter) - { + if (m_series->type() == QAbstractSeries::SeriesTypeScatter) setBrush(m_series->brush()); - - } - else { + else setBrush(QBrush(m_series->pen().color())); - } } #include "moc_legendmarker_p.cpp" diff --git a/src/legend/legendmarker_p.h b/src/legend/legendmarker_p.h index 462370f..e95c386 100644 --- a/src/legend/legendmarker_p.h +++ b/src/legend/legendmarker_p.h @@ -78,7 +78,7 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); - QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const; + QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const; protected: // From QGraphicsObject @@ -124,7 +124,7 @@ private: class BarLegendMarker : public LegendMarker { public: - BarLegendMarker(QAbstractBarSeries *barseries, QBarSet *barset,QLegend *legend); + BarLegendMarker(QAbstractBarSeries *barseries, QBarSet *barset, QLegend *legend); protected: void updated(); private: diff --git a/src/legend/legendscroller_p.h b/src/legend/legendscroller_p.h index dd5fd2c..f40ecd3 100644 --- a/src/legend/legendscroller_p.h +++ b/src/legend/legendscroller_p.h @@ -41,28 +41,17 @@ class LegendScroller: public QLegend, public Scroller { public: - LegendScroller(QChart *chart):QLegend(chart) - { - } + LegendScroller(QChart *chart): QLegend(chart) { } - void setOffset(const QPointF& point) - { - d_ptr->setOffset(point.x(), point.y()); - } - QPointF offset() const - { - return d_ptr->offset(); - } + void setOffset(const QPointF& point) { d_ptr->setOffset(point.x(), point.y()); } - void mousePressEvent(QGraphicsSceneMouseEvent* event){ - Scroller::mousePressEvent(event); - } - void mouseMoveEvent(QGraphicsSceneMouseEvent* event){ - Scroller::mouseMoveEvent(event); - } - void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){ - Scroller::mouseReleaseEvent(event); - } + QPointF offset() const { return d_ptr->offset(); } + + void mousePressEvent(QGraphicsSceneMouseEvent* event) { Scroller::mousePressEvent(event); } + + void mouseMoveEvent(QGraphicsSceneMouseEvent* event) { Scroller::mouseMoveEvent(event); } + + void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { Scroller::mouseReleaseEvent(event); } }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 3d7c632..a00a935 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -167,13 +167,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs the legend object and sets the parent to \a parent */ -QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), -d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) +QLegend::QLegend(QChart *chart): QGraphicsWidget(chart), + d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this)) { setZValue(ChartPresenter::LegendZValue); setFlags(QGraphicsItem::ItemClipsChildrenToShape); - QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); - QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); + QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); + QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*))); // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); setLayout(d_ptr->m_layout); } @@ -193,13 +193,13 @@ void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q Q_UNUSED(option) Q_UNUSED(widget) - if(!d_ptr->m_backgroundVisible) return; + if (!d_ptr->m_backgroundVisible) + return; painter->setOpacity(opacity()); painter->setPen(d_ptr->m_pen); painter->setBrush(d_ptr->m_brush); - painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height())); - + painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height())); } @@ -263,10 +263,8 @@ void QLegend::setFont(const QFont &font) { if (d_ptr->m_font != font) { d_ptr->m_font = font; - - foreach (LegendMarker *marker, d_ptr->markers()) { + foreach (LegendMarker *marker, d_ptr->markers()) marker->setFont(d_ptr->m_font); - } layout()->invalidate(); emit fontChanged(font); } @@ -274,7 +272,7 @@ void QLegend::setFont(const QFont &font) QFont QLegend::font() const { - return d_ptr->m_font; + return d_ptr->m_font; } void QLegend::setBorderColor(QColor color) @@ -334,7 +332,7 @@ QColor QLegend::labelColor() const void QLegend::setAlignment(Qt::Alignment alignment) { - if(d_ptr->m_alignment!=alignment) { + if (d_ptr->m_alignment != alignment) { d_ptr->m_alignment = alignment; layout()->invalidate(); } @@ -379,7 +377,7 @@ bool QLegend::isAttachedToChart() */ void QLegend::setBackgroundVisible(bool visible) { - if(d_ptr->m_backgroundVisible != visible) { + if (d_ptr->m_backgroundVisible != visible) { d_ptr->m_backgroundVisible = visible; update(); emit backgroundVisibleChanged(visible); @@ -399,37 +397,38 @@ bool QLegend::isBackgroundVisible() const */ void QLegend::hideEvent(QHideEvent *event) { - if(isAttachedToChart()) d_ptr->m_presenter->layout()->invalidate(); - QGraphicsWidget::hideEvent(event); + if (isAttachedToChart()) + d_ptr->m_presenter->layout()->invalidate(); + QGraphicsWidget::hideEvent(event); } /*! \internal \a event see QGraphicsWidget for details */ void QLegend::showEvent(QShowEvent *event) { - if(isAttachedToChart()) { - d_ptr->items()->setVisible(false); - layout()->invalidate(); - } - QGraphicsWidget::showEvent(event); - //layout activation will show the items + if (isAttachedToChart()) { + d_ptr->items()->setVisible(false); + layout()->invalidate(); + } + QGraphicsWidget::showEvent(event); + //layout activation will show the items } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): - q_ptr(q), - m_presenter(presenter), - m_layout(new LegendLayout(q)), - m_chart(chart), - m_items(new QGraphicsItemGroup(q)), - m_alignment(Qt::AlignTop), - m_brush(QBrush()), - m_pen(QPen()), - m_labelBrush(QBrush()), - m_diameter(5), - m_attachedToChart(true), - m_backgroundVisible(false) +QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q) + : q_ptr(q), + m_presenter(presenter), + m_layout(new LegendLayout(q)), + m_chart(chart), + m_items(new QGraphicsItemGroup(q)), + m_alignment(Qt::AlignTop), + m_brush(QBrush()), + m_pen(QPen()), + m_labelBrush(QBrush()), + m_diameter(5), + m_attachedToChart(true), + m_backgroundVisible(false) { } @@ -441,7 +440,7 @@ QLegendPrivate::~QLegendPrivate() void QLegendPrivate::setOffset(qreal x, qreal y) { - m_layout->setOffset(x,y); + m_layout->setOffset(x, y); } QPointF QLegendPrivate::offset() const @@ -451,7 +450,7 @@ QPointF QLegendPrivate::offset() const int QLegendPrivate::roundness(qreal size) { - return 100*m_diameter/int(size); + return 100 * m_diameter / int(size); } void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) @@ -460,12 +459,12 @@ void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) QList markers = series->d_ptr->createLegendMarker(q_ptr); - foreach (LegendMarker* marker, markers) { + foreach (LegendMarker *marker, markers) { marker->setFont(m_font); marker->setLabelBrush(m_labelBrush); marker->setVisible(series->isVisible()); m_items->addToGroup(marker); - m_markers< (sender()); Q_ASSERT(series); - foreach (LegendMarker* marker, m_markers) { - if (marker->series() == series) { + foreach (LegendMarker *marker, m_markers) { + if (marker->series() == series) marker->setVisible(series->isVisible()); - } } m_layout->invalidate(); } diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index 17b0825..9bb1e70 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -78,9 +78,8 @@ private: bool m_attachedToChart; bool m_backgroundVisible; -friend class QLegend; -friend class LegendLayout; - + friend class QLegend; + friend class LegendLayout; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 629f1df..416dc38 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -29,14 +29,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE const qreal mouseEventMinWidth(12); -LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): - XYChart(series, presenter), - QGraphicsItem(presenter ? presenter->rootItem() : 0), - m_series(series), - m_pointsVisible(false) +LineChartItem::LineChartItem(QLineSeries* series, ChartPresenter *presenter) + : XYChart(series, presenter), + QGraphicsItem(presenter ? presenter->rootItem() : 0), + m_series(series), + m_pointsVisible(false) { setZValue(ChartPresenter::LineChartZValue); - QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); + QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); handleUpdated(); @@ -44,7 +44,7 @@ LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): QRectF LineChartItem::boundingRect() const { - return m_rect; + return m_rect; } QPainterPath LineChartItem::shape() const @@ -56,8 +56,7 @@ void LineChartItem::updateGeometry() { m_points = geometryPoints(); - if(m_points.size()==0) - { + if (m_points.size() == 0) { prepareGeometryChange(); m_path = QPainterPath(); m_rect = QRect(); @@ -66,20 +65,18 @@ void LineChartItem::updateGeometry() QPainterPath linePath(m_points.at(0)); - if(m_pointsVisible) { + if (m_pointsVisible) { int size = m_linePen.width(); - linePath.addEllipse(m_points.at(0),size,size); - for(int i=1; i< m_points.size();i++) { + linePath.addEllipse(m_points.at(0), size, size); + for (int i = 1; i < m_points.size(); i++) { linePath.lineTo(m_points.at(i)); - linePath.addEllipse(m_points.at(i),size,size); + linePath.addEllipse(m_points.at(i), size, size); } } else { - - for(int i=1; i< m_points.size();i++) { + for (int i = 1; i < m_points.size(); i++) linePath.lineTo(m_points.at(i)); - } } m_linePath = linePath; @@ -122,10 +119,9 @@ void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt if (m_pointsVisible) { painter->drawPath(m_linePath); - } - else { - for (int i(1); i < m_points.size();i++) - painter->drawLine(m_points.at(i-1), m_points.at(i)); + } else { + for (int i(1); i < m_points.size(); i++) + painter->drawLine(m_points.at(i - 1), m_points.at(i)); } painter->restore(); } diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h index 3afe700..3b07c0f 100644 --- a/src/linechart/linechartitem_p.h +++ b/src/linechart/linechartitem_p.h @@ -41,10 +41,10 @@ class ChartPresenter; class LineChartItem : public XYChart , public QGraphicsItem { - Q_OBJECT - Q_INTERFACES(QGraphicsItem) + Q_OBJECT + Q_INTERFACES(QGraphicsItem) public: - explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter); + explicit LineChartItem(QLineSeries *series, ChartPresenter *presenter); ~LineChartItem() {} //from QGraphicsItem diff --git a/src/linechart/qlineseries.cpp b/src/linechart/qlineseries.cpp index a0cd51b..2bd7a0b 100644 --- a/src/linechart/qlineseries.cpp +++ b/src/linechart/qlineseries.cpp @@ -32,13 +32,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE \mainclass - A line chart is used to show information as a series of data points - connected by straight lines. + A line chart is used to show information as a series of data points + connected by straight lines. \image examples_linechart.png - Creating basic line chart is simple: - \code + Creating basic line chart is simple: + \code QLineSeries* series = new QLineSeries(); series->append(0, 6); series->append(2, 4); @@ -98,7 +98,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs empty series object which is a child of \a parent. When series object is added to QChartView or QChart instance ownerships is transferred. */ -QLineSeries::QLineSeries(QObject *parent) : QXYSeries(*new QLineSeriesPrivate(this),parent) +QLineSeries::QLineSeries(QObject *parent) + : QXYSeries(*new QLineSeriesPrivate(this), parent) { } @@ -106,7 +107,8 @@ QLineSeries::QLineSeries(QObject *parent) : QXYSeries(*new QLineSeriesPrivate(th /*! \internal */ -QLineSeries::QLineSeries(QLineSeriesPrivate &d,QObject *parent) : QXYSeries (d,parent) +QLineSeries::QLineSeries(QLineSeriesPrivate &d, QObject *parent) + : QXYSeries(d, parent) { } @@ -117,9 +119,8 @@ QLineSeries::QLineSeries(QLineSeriesPrivate &d,QObject *parent) : QXYSeries (d,p QLineSeries::~QLineSeries() { Q_D(QLineSeries); - if(d->m_dataset){ - d->m_dataset->removeSeries(this); - } + if (d->m_dataset) + d->m_dataset->removeSeries(this); } QAbstractSeries::SeriesType QLineSeries::type() const @@ -141,7 +142,7 @@ QDebug operator<< (QDebug debug, const QLineSeries series) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries* q):QXYSeriesPrivate(q) +QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries* q): QXYSeriesPrivate(q) { }; @@ -149,10 +150,9 @@ QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries* q):QXYSeriesPrivate(q) ChartElement* QLineSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QLineSeries); - LineChartItem* line = new LineChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + LineChartItem* line = new LineChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) line->setAnimation(new XYAnimation(line)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return line; } diff --git a/src/linechart/qlineseries.h b/src/linechart/qlineseries.h index 2553606..ad6c811 100644 --- a/src/linechart/qlineseries.h +++ b/src/linechart/qlineseries.h @@ -39,7 +39,7 @@ public: QAbstractSeries::SeriesType type() const; protected: - QLineSeries(QLineSeriesPrivate &d,QObject *parent = 0); + QLineSeries(QLineSeriesPrivate &d, QObject *parent = 0); private: Q_DECLARE_PRIVATE(QLineSeries) diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index 5d1ccfe..b149914 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -33,9 +33,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter) - :ChartItem(presenter), - m_series(series), - m_animation(0) + : ChartItem(presenter), + m_series(series), + m_animation(0) { Q_ASSERT(series); @@ -62,7 +62,7 @@ PieChartItem::~PieChartItem() void PieChartItem::setAnimation(PieAnimation* animation) { - m_animation=animation; + m_animation = animation; } ChartAnimation* PieChartItem::animation() const @@ -129,13 +129,12 @@ void PieChartItem::updateLayout() m_holeSize *= m_series->holeSize(); // set layouts for existing slice items - foreach (QPieSlice* slice, m_series->slices()) { + foreach (QPieSlice *slice, m_series->slices()) { PieSliceItem *sliceItem = m_sliceItems.value(slice); if (sliceItem) { PieSliceData sliceData = updateSliceGeometry(slice); - if (m_animation){ + if (m_animation) presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData)); - } else sliceItem->setLayout(sliceData); } diff --git a/src/piechart/piesliceitem.cpp b/src/piechart/piesliceitem.cpp index 507ec59..c181084 100644 --- a/src/piechart/piesliceitem.cpp +++ b/src/piechart/piesliceitem.cpp @@ -33,13 +33,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QPointF offset(qreal angle, qreal length) { - qreal dx = qSin(angle*(M_PI/180)) * length; - qreal dy = qCos(angle*(M_PI/180)) * length; + qreal dx = qSin(angle * (M_PI / 180)) * length; + qreal dy = qCos(angle * (M_PI / 180)) * length; return QPointF(dx, -dy); } PieSliceItem::PieSliceItem(QGraphicsItem* parent) - :QGraphicsObject(parent), + : QGraphicsObject(parent), m_hovered(false) { setAcceptHoverEvents(true); @@ -85,8 +85,7 @@ void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*op painter->setBrush(m_data.m_labelBrush); painter->setFont(m_data.m_labelFont); - switch (m_data.m_labelPosition) - { + switch (m_data.m_labelPosition) { case QPieSlice::LabelOutside: painter->setClipRect(parentItem()->boundingRect()); painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); @@ -163,13 +162,12 @@ void PieSliceItem::updateGeometry() m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); // text position - switch (m_data.m_labelPosition) - { + switch (m_data.m_labelPosition) { case QPieSlice::LabelOutside: m_labelTextRect.moveBottomLeft(labelTextStart); break; case QPieSlice::LabelInsideHorizontal: - case QPieSlice::LabelInsideTangential:{ + case QPieSlice::LabelInsideTangential: { QPointF textCenter; if (m_data.m_holeRadius > 0) textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); @@ -177,8 +175,8 @@ void PieSliceItem::updateGeometry() textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); m_labelTextRect.moveCenter(textCenter); break; - } - case QPieSlice::LabelInsideNormal:{ + } + case QPieSlice::LabelInsideNormal: { QPointF textCenter; if (m_data.m_holeRadius > 0) textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); @@ -186,7 +184,7 @@ void PieSliceItem::updateGeometry() textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); m_labelTextRect.moveCenter(textCenter); break; - } + } } // bounding rect @@ -199,7 +197,7 @@ void PieSliceItem::updateGeometry() QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) { if (slice->isExploded()) { - qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2); + qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2); qreal len = radius * slice->explodeDistanceFactor(); point += offset(centerAngle, len); } @@ -209,15 +207,15 @@ QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart) { // calculate center angle - *centerAngle = startAngle + (angleSpan/2); + *centerAngle = startAngle + (angleSpan / 2); // calculate slice rectangle - QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2); + QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2); // slice path QPainterPath path; if (m_data.m_holeRadius > 0) { - QRectF insideRect(center.x() - m_data.m_holeRadius, center.y()-m_data.m_holeRadius, m_data.m_holeRadius*2, m_data.m_holeRadius*2); + QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2); path.arcMoveTo(rect, -startAngle + 90); path.arcTo(rect, -startAngle + 90, -angleSpan); path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); @@ -263,9 +261,8 @@ QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length if (angle < 180) { // arm swings the other way on the left side parm2 += QPointF(textWidth, 0); *textStart = parm1; - } - else { - parm2 += QPointF(-textWidth,0); + } else { + parm2 += QPointF(-textWidth, 0); *textStart = parm2; } diff --git a/src/piechart/qpiemodelmapper.cpp b/src/piechart/qpiemodelmapper.cpp index 5cbac21..c59f73c 100644 --- a/src/piechart/qpiemodelmapper.cpp +++ b/src/piechart/qpiemodelmapper.cpp @@ -308,7 +308,7 @@ void QPieModelMapperPrivate::slicesAdded(QList slices) else m_model->insertColumns(firstIndex + m_first, slices.count()); - for(int i = firstIndex; i < firstIndex + slices.count(); i++) { + for (int i = firstIndex; i < firstIndex + slices.count(); i++) { m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value()); m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label()); } diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 7ce5730..cbd511b 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -347,11 +347,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! Constructs a series object which is a child of \a parent. */ -QPieSeries::QPieSeries(QObject *parent) : - QAbstractSeries(*new QPieSeriesPrivate(this),parent) +QPieSeries::QPieSeries(QObject *parent) + : QAbstractSeries(*new QPieSeriesPrivate(this), parent) { Q_D(QPieSeries); - QObject::connect(this,SIGNAL(countChanged()),d,SIGNAL(countChanged())); + QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged())); } /*! @@ -394,14 +394,14 @@ bool QPieSeries::append(QList slices) if (slices.count() == 0) return false; - foreach (QPieSlice* s, slices) { + foreach (QPieSlice *s, slices) { if (!s || d->m_slices.contains(s)) return false; if (s->series()) // already added to some series return false; } - foreach (QPieSlice* s, slices) { + foreach (QPieSlice *s, slices) { s->setParent(this); QPieSlicePrivate::fromSlice(s)->m_series = this; d->m_slices << s; @@ -409,7 +409,7 @@ bool QPieSeries::append(QList slices) d->updateDerivativeData(); - foreach (QPieSlice* s, slices) { + foreach (QPieSlice *s, slices) { connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged())); connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked())); connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); @@ -540,7 +540,7 @@ void QPieSeries::clear() return; QList slices = d->m_slices; - foreach (QPieSlice* s, d->m_slices) + foreach (QPieSlice *s, d->m_slices) d->m_slices.removeOne(s); d->updateDerivativeData(); @@ -548,7 +548,7 @@ void QPieSeries::clear() emit removed(slices); emit countChanged(); - foreach (QPieSlice* s, slices) + foreach (QPieSlice *s, slices) delete s; } @@ -719,7 +719,7 @@ qreal QPieSeries::pieEndAngle() const void QPieSeries::setLabelsVisible(bool visible) { Q_D(QPieSeries); - foreach (QPieSlice* s, d->m_slices) + foreach (QPieSlice *s, d->m_slices) s->setLabelVisible(visible); } @@ -734,7 +734,7 @@ void QPieSeries::setLabelsVisible(bool visible) void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position) { Q_D(QPieSeries); - foreach (QPieSlice* s, d->m_slices) + foreach (QPieSlice *s, d->m_slices) s->setLabelPosition(position); } @@ -761,7 +761,7 @@ void QPieSeriesPrivate::updateDerivativeData() { // calculate sum of all slices qreal sum = 0; - foreach (QPieSlice* s, m_slices) + foreach (QPieSlice *s, m_slices) sum += s->value(); if (!qFuzzyIsNull(m_sum - sum)) { @@ -777,7 +777,7 @@ void QPieSeriesPrivate::updateDerivativeData() qreal sliceAngle = m_pieStartAngle; qreal pieSpan = m_pieEndAngle - m_pieStartAngle; QVector changed; - foreach (QPieSlice* s, m_slices) { + foreach (QPieSlice *s, m_slices) { QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s); d->setPercentage(s->value() / m_sum); d->setStartAngle(sliceAngle); @@ -843,10 +843,9 @@ void QPieSeriesPrivate::scaleDomain(Domain& domain) ChartElement* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QPieSeries); - PieChartItem* pie = new PieChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + PieChartItem* pie = new PieChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) pie->setAnimation(new PieAnimation(pie)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return pie; } @@ -855,8 +854,8 @@ QList QPieSeriesPrivate::createLegendMarker(QLegend* legend) { Q_Q(QPieSeries); QList markers; - foreach(QPieSlice* slice, q->slices()) { - PieLegendMarker* marker = new PieLegendMarker(q,slice,legend); + foreach (QPieSlice *slice, q->slices()) { + PieLegendMarker* marker = new PieLegendMarker(q, slice, legend); markers << marker; } return markers; diff --git a/src/piechart/qpieslice.cpp b/src/piechart/qpieslice.cpp index 08753d2..6f70dcb 100644 --- a/src/piechart/qpieslice.cpp +++ b/src/piechart/qpieslice.cpp @@ -435,8 +435,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE \sa QPieSeries::append(), QPieSeries::insert() */ QPieSlice::QPieSlice(QObject *parent) - :QObject(parent), - d_ptr(new QPieSlicePrivate(this)) + : QObject(parent), + d_ptr(new QPieSlicePrivate(this)) { } @@ -446,8 +446,8 @@ QPieSlice::QPieSlice(QObject *parent) \sa QPieSeries::append(), QPieSeries::insert() */ QPieSlice::QPieSlice(QString label, qreal value, QObject *parent) - :QObject(parent), - d_ptr(new QPieSlicePrivate(this)) + : QObject(parent), + d_ptr(new QPieSlicePrivate(this)) { setValue(value); setLabel(label); @@ -677,9 +677,9 @@ QPieSeries *QPieSlice::series() const } QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent) - :QObject(parent), - q_ptr(parent), - m_series(0) + : QObject(parent), + q_ptr(parent), + m_series(0) { } diff --git a/src/qabstractseries.cpp b/src/qabstractseries.cpp index 50c9a9b..fdd311d 100644 --- a/src/qabstractseries.cpp +++ b/src/qabstractseries.cpp @@ -137,7 +137,8 @@ QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) : */ QAbstractSeries::~QAbstractSeries() { - if(d_ptr->m_dataset) qFatal("Still binded series detected !"); + if (d_ptr->m_dataset) + qFatal("Still binded series detected !"); } void QAbstractSeries::setName(const QString& name) @@ -223,12 +224,12 @@ void QAbstractSeries::hide() /////////////////////////////////////////////////////////////////////////////////////////////////// -QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q): - q_ptr(q), - m_chart(0), - m_dataset(0), - m_visible(true), - m_opacity(1.0) +QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q) + : q_ptr(q), + m_chart(0), + m_dataset(0), + m_visible(true), + m_opacity(1.0) { } diff --git a/src/qchart.cpp b/src/qchart.cpp index 8d69cae..45a68b1 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -104,11 +104,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. */ -QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), - d_ptr(new QChartPrivate()) +QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) + : QGraphicsWidget(parent, wFlags), + d_ptr(new QChartPrivate()) { d_ptr->m_dataset = new ChartDataSet(this); - d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); + d_ptr->m_presenter = new ChartPresenter(this, d_ptr->m_dataset); d_ptr->createConnections(); d_ptr->m_legend = new LegendScroller(this); d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); @@ -124,7 +125,7 @@ QChart::~QChart() //delete first presenter , since this is a root of all the graphical items setLayout(0); delete d_ptr->m_presenter; - d_ptr->m_presenter=0; + d_ptr->m_presenter = 0; } /*! @@ -353,7 +354,7 @@ QAbstractAxis* QChart::axisY(QAbstractSeries *series) const */ void QChart::createDefaultAxes() { - d_ptr->m_dataset->createDefaultAxes(); + d_ptr->m_dataset->createDefaultAxes(); } /*! @@ -457,7 +458,7 @@ QList QChart::series() const */ void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series) { - d_ptr->m_dataset->setAxis(series,axis,Qt::Horizontal); + d_ptr->m_dataset->setAxis(series, axis, Qt::Horizontal); } /*! @@ -465,9 +466,9 @@ void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series) \sa axisX(), axisY(), setAxisX(), createDefaultAxes() */ -void QChart::setAxisY( QAbstractAxis* axis , QAbstractSeries *series) +void QChart::setAxisY(QAbstractAxis *axis , QAbstractSeries *series) { - d_ptr->m_dataset->setAxis(series,axis,Qt::Vertical); + d_ptr->m_dataset->setAxis(series, axis, Qt::Vertical); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -487,10 +488,10 @@ QChartPrivate::~QChartPrivate() void QChartPrivate::createConnections() { - QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); - QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*))); - QObject::connect(m_dataset,SIGNAL(axisAdded(QAbstractAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAbstractAxis*,Domain*))); - QObject::connect(m_dataset,SIGNAL(axisRemoved(QAbstractAxis*)),m_presenter,SLOT(handleAxisRemoved(QAbstractAxis*))); + QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); + QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*))); + QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*,Domain*))); + QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)),m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*))); //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF))); } diff --git a/src/qchart.h b/src/qchart.h index 0fe6bff..9a1d7de 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -61,7 +61,7 @@ public: enum AnimationOption { NoAnimation = 0x0, GridAxisAnimations = 0x1, - SeriesAnimations =0x2, + SeriesAnimations = 0x2, AllAnimations = 0x3 }; diff --git a/src/qchart_p.h b/src/qchart_p.h index 59ee1a0..ecb572d 100644 --- a/src/qchart_p.h +++ b/src/qchart_p.h @@ -47,7 +47,6 @@ struct QChartPrivate ChartPresenter *m_presenter; void createConnections(); - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartglobal.h b/src/qchartglobal.h index e5144eb..7a4b127 100644 --- a/src/qchartglobal.h +++ b/src/qchartglobal.h @@ -74,14 +74,14 @@ static inline QDebug chartDebug(int numargs,...) { - va_list valist; - va_start(valist,numargs); - //for( int i = 0 ; i < numargs; i++ ) - int line=va_arg(valist,int); - char* file=va_arg(valist,char*); - char* function=va_arg(valist,char*); - va_end(valist); - return qDebug()<m_rubberBandFlags=rubberBand; + d_ptr->m_rubberBandFlags = rubberBand; if (!d_ptr->m_rubberBandFlags) { delete d_ptr->m_rubberBand; - d_ptr->m_rubberBand=0; + d_ptr->m_rubberBand = 0; return; } @@ -132,7 +132,7 @@ QChartView::RubberBands QChartView::rubberBand() const */ void QChartView::mousePressEvent(QMouseEvent *event) { - if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { + if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { QRectF plotArea = d_ptr->m_chart->plotArea(); @@ -142,8 +142,7 @@ void QChartView::mousePressEvent(QMouseEvent *event) d_ptr->m_rubberBand->show(); event->accept(); } - } - else { + } else { QGraphicsView::mousePressEvent(event); } } @@ -154,7 +153,7 @@ void QChartView::mousePressEvent(QMouseEvent *event) */ void QChartView::mouseMoveEvent(QMouseEvent *event) { - if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { + if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { QRect rect = d_ptr->m_chart->plotArea().toRect(); int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x(); int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y(); @@ -164,11 +163,10 @@ void QChartView::mouseMoveEvent(QMouseEvent *event) } if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) { d_ptr->m_rubberBandOrigin.setX(rect.left()); - width= rect.width(); + width = rect.width(); } - d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized()); - } - else { + d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized()); + } else { QGraphicsView::mouseMoveEvent(event); } } @@ -179,7 +177,7 @@ void QChartView::mouseMoveEvent(QMouseEvent *event) */ void QChartView::mouseReleaseEvent(QMouseEvent *event) { - if(d_ptr->m_rubberBand) { + if (d_ptr->m_rubberBand) { if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) { d_ptr->m_rubberBand->hide(); QRect rect = d_ptr->m_rubberBand->geometry(); @@ -187,12 +185,11 @@ void QChartView::mouseReleaseEvent(QMouseEvent *event) event->accept(); } - if(event->button()==Qt::RightButton){ + if (event->button() == Qt::RightButton) { d_ptr->m_chart->zoomOut(); event->accept(); } - } - else { + } else { QGraphicsView::mouseReleaseEvent(event); } } @@ -208,12 +205,12 @@ void QChartView::resizeEvent(QResizeEvent *event) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QChartViewPrivate::QChartViewPrivate(QChartView *q,QChart* chart): -q_ptr(q), -m_scene(new QGraphicsScene(q)), -m_chart(chart), -m_rubberBand(0), -m_rubberBandFlags(QChartView::NoRubberBand) +QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart* chart) + : q_ptr(q), + m_scene(new QGraphicsScene(q)), + m_chart(chart), + m_rubberBand(0), + m_rubberBandFlags(QChartView::NoRubberBand) { q_ptr->setFrameShape(QFrame::NoFrame); q_ptr->setBackgroundRole(QPalette::Window); @@ -221,7 +218,8 @@ m_rubberBandFlags(QChartView::NoRubberBand) q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); q_ptr->setScene(m_scene); q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - if(!m_chart) m_chart = new QChart(); + if (!m_chart) + m_chart = new QChart(); m_scene->addItem(m_chart); } diff --git a/src/qchartview.h b/src/qchartview.h index ec124e0..e08a52a 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -39,7 +39,7 @@ class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView Q_ENUMS(RubberBand) public: - enum RubberBand{ + enum RubberBand { NoRubberBand = 0x0, VerticalRubberBand = 0x1, HorizonalRubberBand = 0x2, @@ -49,7 +49,7 @@ public: Q_DECLARE_FLAGS(RubberBands, RubberBand) explicit QChartView(QWidget *parent = 0); - explicit QChartView(QChart *chart,QWidget *parent = 0); + explicit QChartView(QChart *chart, QWidget *parent = 0); ~QChartView(); void setRubberBand(const RubberBands& rubberBands); diff --git a/src/qchartview_p.h b/src/qchartview_p.h index 6a6dbac..67f8042 100644 --- a/src/qchartview_p.h +++ b/src/qchartview_p.h @@ -40,8 +40,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; class QChartView; -class QChartViewPrivate { - +class QChartViewPrivate +{ public: explicit QChartViewPrivate(QChartView *q, QChart *chart = 0); ~QChartViewPrivate(); diff --git a/src/scatterchart/qscatterseries.cpp b/src/scatterchart/qscatterseries.cpp index 2a0386b..6c1ba34 100644 --- a/src/scatterchart/qscatterseries.cpp +++ b/src/scatterchart/qscatterseries.cpp @@ -128,7 +128,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! Constructs a series object which is a child of \a parent. */ -QScatterSeries::QScatterSeries(QObject *parent) : QXYSeries(*new QScatterSeriesPrivate(this),parent) +QScatterSeries::QScatterSeries(QObject *parent) + : QXYSeries(*new QScatterSeriesPrivate(this), parent) { } @@ -138,9 +139,8 @@ QScatterSeries::QScatterSeries(QObject *parent) : QXYSeries(*new QScatterSeriesP QScatterSeries::~QScatterSeries() { Q_D(QScatterSeries); - if(d->m_dataset) { + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } QAbstractSeries::SeriesType QScatterSeries::type() const @@ -253,10 +253,9 @@ QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries* q) : ChartElement* QScatterSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QScatterSeries); - ScatterChartItem *scatter = new ScatterChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + ScatterChartItem *scatter = new ScatterChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) scatter->setAnimation(new XYAnimation(scatter)); - } presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return scatter; } diff --git a/src/scatterchart/scatterchartitem.cpp b/src/scatterchart/scatterchartitem.cpp index 7de66de..20d55f4 100644 --- a/src/scatterchart/scatterchartitem.cpp +++ b/src/scatterchart/scatterchartitem.cpp @@ -29,16 +29,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) : - XYChart(series,presenter), - QGraphicsItem(presenter ? presenter->rootItem() : 0), - m_series(series), - m_items(this), - m_visible(true), - m_shape(QScatterSeries::MarkerShapeRectangle), - m_size(15) +ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) + : XYChart(series, presenter), + QGraphicsItem(presenter ? presenter->rootItem() : 0), + m_series(series), + m_items(this), + m_visible(true), + m_shape(QScatterSeries::MarkerShapeRectangle), + m_size(15) { - QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated())); + QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); @@ -62,21 +62,20 @@ void ScatterChartItem::createPoints(int count) QGraphicsItem *item = 0; switch (m_shape) { - case QScatterSeries::MarkerShapeCircle: { - item = new CircleMarker(0,0,m_size,m_size,this); - const QRectF& rect = item->boundingRect(); - item->setPos(-rect.width()/2,-rect.height()/2); - break; + case QScatterSeries::MarkerShapeCircle: { + item = new CircleMarker(0, 0, m_size, m_size, this); + const QRectF& rect = item->boundingRect(); + item->setPos(-rect.width() / 2, -rect.height() / 2); + break; } - case QScatterSeries::MarkerShapeRectangle: { - item = new RectangleMarker(0,0,m_size,m_size,this); - item->setPos(-m_size/2,-m_size/2); - break; + case QScatterSeries::MarkerShapeRectangle: { + item = new RectangleMarker(0, 0, m_size, m_size, this); + item->setPos(-m_size / 2, -m_size / 2); + break; } - default: - qWarning()<<"Unsupported marker type"; + default: + qWarning() << "Unsupported marker type"; break; - } m_items.addToGroup(item); } @@ -103,22 +102,20 @@ void ScatterChartItem::updateGeometry() const QVector& points = geometryPoints(); - if(points.size()==0) - { + if (points.size() == 0) { deletePoints(m_items.childItems().count()); return; } int diff = m_items.childItems().size() - points.size(); - if(diff>0) { + if (diff > 0) deletePoints(diff); - } - else if(diff<0) { + else if (diff < 0) createPoints(-diff); - } - if(diff!=0) handleUpdated(); + if (diff != 0) + handleUpdated(); QList items = m_items.childItems(); @@ -126,14 +123,12 @@ void ScatterChartItem::updateGeometry() QGraphicsItem* item = items.at(i); const QPointF& point = points.at(i); const QRectF& rect = item->boundingRect(); - m_markerMap[item]=point; - item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); - if(!m_visible || !clipRect().contains(point)) { + m_markerMap[item] = point; + item->setPos(point.x() - rect.width() / 2, point.y() - rect.height() / 2); + if (!m_visible || !clipRect().contains(point)) item->setVisible(false); - } - else { + else item->setVisible(true); - } } prepareGeometryChange(); @@ -150,34 +145,33 @@ void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * void ScatterChartItem::setPen(const QPen& pen) { - foreach(QGraphicsItem* item , m_items.childItems()) { + foreach (QGraphicsItem *item , m_items.childItems()) static_cast(item)->setPen(pen); - } } void ScatterChartItem::setBrush(const QBrush& brush) { - foreach(QGraphicsItem* item , m_items.childItems()) { + foreach (QGraphicsItem *item , m_items.childItems()) static_cast(item)->setBrush(brush); - } } void ScatterChartItem::handleUpdated() { int count = m_items.childItems().count(); - if(count==0) return; + if (count == 0) + return; bool recreate = m_visible != m_series->isVisible() - || m_size != m_series->markerSize() - || m_shape != m_series->markerShape(); + || m_size != m_series->markerSize() + || m_shape != m_series->markerShape(); m_visible = m_series->isVisible(); m_size = m_series->markerSize(); m_shape = m_series->markerShape(); setOpacity(m_series->opacity()); - if(recreate) { + if (recreate) { // TODO: optimize handleUpdate to recreate points only in case shape changed deletePoints(count); createPoints(count); diff --git a/src/scatterchart/scatterchartitem_p.h b/src/scatterchart/scatterchartitem_p.h index d00fabe..b9630f5 100644 --- a/src/scatterchart/scatterchartitem_p.h +++ b/src/scatterchart/scatterchartitem_p.h @@ -80,40 +80,42 @@ class CircleMarker: public QGraphicsEllipseItem { public: - CircleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) : QGraphicsEllipseItem(x,y,w,h,parent), - m_parent(parent) + CircleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) + : QGraphicsEllipseItem(x, y, w, h, parent), + m_parent(parent) { } protected: - void mousePressEvent(QGraphicsSceneMouseEvent *event) - { - m_parent->markerSelected(this); - QGraphicsEllipseItem::mousePressEvent(event); - } + void mousePressEvent(QGraphicsSceneMouseEvent *event) + { + m_parent->markerSelected(this); + QGraphicsEllipseItem::mousePressEvent(event); + } private: - ScatterChartItem* m_parent; + ScatterChartItem* m_parent; }; class RectangleMarker: public QGraphicsRectItem { public: - RectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) : QGraphicsRectItem(x,y,w,h,parent), - m_parent(parent) - { - } + RectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) + : QGraphicsRectItem(x, y, w, h, parent), + m_parent(parent) + { + } protected: - void mousePressEvent(QGraphicsSceneMouseEvent *event) - { - m_parent->markerSelected(this); - QGraphicsRectItem::mousePressEvent(event); - } + void mousePressEvent(QGraphicsSceneMouseEvent *event) + { + m_parent->markerSelected(this); + QGraphicsRectItem::mousePressEvent(event); + } private: - ScatterChartItem* m_parent; + ScatterChartItem* m_parent; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/scroller.cpp b/src/scroller.cpp index a0c8f2b..c396f56 100644 --- a/src/scroller.cpp +++ b/src/scroller.cpp @@ -25,11 +25,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -Scroller::Scroller(): -m_ticker(this), -m_state(Idle), -m_moveThreshold(10), -m_timeTreshold(50) +Scroller::Scroller() + : m_ticker(this), + m_state(Idle), + m_moveThreshold(10), + m_timeTreshold(50) { } @@ -41,30 +41,25 @@ Scroller::~Scroller() void Scroller::mousePressEvent(QGraphicsSceneMouseEvent* event) { if (event->button() == Qt::LeftButton) { - switch (m_state) { case Idle: - { m_state = Pressed; m_offset = offset(); m_press = event->pos(); m_timeStamp = QTime::currentTime(); event->accept(); break; - } case Scroll: - { m_state = Stop; m_speed = QPointF(0, 0); m_offset = offset(); m_press = event->pos(); event->accept(); break; - } case Pressed: case Move: case Stop: - qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; + qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; event->ignore(); break; } @@ -78,34 +73,27 @@ void Scroller::mouseMoveEvent(QGraphicsSceneMouseEvent* event) switch (m_state) { case Pressed: case Stop: - { if (qAbs(delta.x()) > m_moveThreshold || qAbs(delta.y()) > m_moveThreshold) { m_state = Move; m_timeStamp = QTime::currentTime(); m_distance = QPointF(0, 0); m_press = event->pos(); event->accept(); - break; - } - else { + } else { event->ignore(); - break; } - } + break; case Move: - { setOffset(m_offset - delta); calculateSpeed(event->pos()); event->accept(); break; - } case Idle: case Scroll: - qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; + qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; event->ignore(); break; } - } void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) @@ -121,7 +109,6 @@ void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) event->accept(); break; case Pressed: - { m_state = Idle; //if (m_timeStamp.elapsed() < m_clickedPressDelay) { @@ -129,30 +116,24 @@ void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) //} event->accept(); break; - } case Move: - { calculateSpeed(event->pos()); m_offset = offset(); m_press = event->pos(); if (m_speed == QPointF(0, 0)) { m_state = Idle; - } - else { + } else { m_speed /= 3.75; m_state = Scroll; m_ticker.start(25); } event->accept(); break; - } - case Stop: case Idle: - qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; + qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; event->ignore(); break; - } } } @@ -160,25 +141,23 @@ void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) void Scroller::scrollTick() { switch (m_state) { - case Scroll: - { - lowerSpeed(m_speed); - setOffset(m_offset - m_speed); - m_offset = offset(); - if (m_speed == QPointF(0, 0)) { - m_state = Idle; - m_ticker.stop(); - } - break; - } - case Stop: - m_ticker.stop(); - break; - case Idle: - case Move: - case Pressed: - qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; + case Scroll: + lowerSpeed(m_speed); + setOffset(m_offset - m_speed); + m_offset = offset(); + if (m_speed == QPointF(0, 0)) { + m_state = Idle; m_ticker.stop(); + } + break; + case Stop: + m_ticker.stop(); + break; + case Idle: + case Move: + case Pressed: + qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; + m_ticker.stop(); break; } @@ -212,8 +191,7 @@ void Scroller::calculateSpeed(const QPointF& position) if (fraction != 0) { m_fraction.setX(qAbs(m_speed.x() / fraction)); m_fraction.setY(qAbs(m_speed.y() / fraction)); - } - else { + } else { m_fraction.setX(1); m_fraction.setY(1); } @@ -222,17 +200,17 @@ void Scroller::calculateSpeed(const QPointF& position) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -ScrollTicker::ScrollTicker(Scroller *scroller,QObject* parent):QObject(parent), - m_scroller(scroller) +ScrollTicker::ScrollTicker(Scroller *scroller, QObject* parent) + : QObject(parent), + m_scroller(scroller) { } void ScrollTicker::start(int interval) { - if (!m_timer.isActive()){ + if (!m_timer.isActive()) m_timer.start(interval, this); - } } void ScrollTicker::stop() diff --git a/src/scroller_p.h b/src/scroller_p.h index 5459650..40f19e7 100644 --- a/src/scroller_p.h +++ b/src/scroller_p.h @@ -46,7 +46,7 @@ class ScrollTicker : public QObject { Q_OBJECT public: - explicit ScrollTicker(Scroller *scroller,QObject *parent = 0); + explicit ScrollTicker(Scroller *scroller, QObject *parent = 0); void start(int interval); void stop(); protected: @@ -77,7 +77,6 @@ public: public: void scrollTick(); - public: void mousePressEvent(QGraphicsSceneMouseEvent* event); void mouseMoveEvent(QGraphicsSceneMouseEvent* event); @@ -85,7 +84,7 @@ public: private: void calculateSpeed(const QPointF& position); - void lowerSpeed(QPointF& speed,qreal maxSpeed=100); + void lowerSpeed(QPointF& speed, qreal maxSpeed = 100); private: ScrollTicker m_ticker; @@ -98,8 +97,6 @@ private: QPointF m_fraction; int m_moveThreshold; int m_timeTreshold; - - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/splinechart/qsplineseries.cpp b/src/splinechart/qsplineseries.cpp index fc90dda..806a759 100644 --- a/src/splinechart/qsplineseries.cpp +++ b/src/splinechart/qsplineseries.cpp @@ -98,14 +98,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE When series object is added to QChartView or QChart instance then the ownerships is transferred. */ -QSplineSeries::QSplineSeries(QObject *parent) : - QLineSeries(*new QSplineSeriesPrivate(this),parent) +QSplineSeries::QSplineSeries(QObject *parent) + : QLineSeries(*new QSplineSeriesPrivate(this), parent) { Q_D(QSplineSeries); - QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints())); - QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints())); - QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints())); - QObject::connect(this,SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints())); + QObject::connect(this, SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints())); + QObject::connect(this, SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints())); + QObject::connect(this, SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints())); + QObject::connect(this, SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints())); } /*! @@ -114,9 +114,8 @@ QSplineSeries::QSplineSeries(QObject *parent) : QSplineSeries::~QSplineSeries() { Q_D(QSplineSeries); - if(d->m_dataset){ + if (d->m_dataset) d->m_dataset->removeSeries(this); - } } QAbstractSeries::SeriesType QSplineSeries::type() const @@ -126,7 +125,8 @@ QAbstractSeries::SeriesType QSplineSeries::type() const //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q) +QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q) + : QLineSeriesPrivate(q) { } @@ -141,8 +141,7 @@ void QSplineSeriesPrivate::calculateControlPoints() int n = points.count() - 1; - if (n == 1) - { + if (n == 1) { //for n==1 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3); m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3); @@ -169,35 +168,33 @@ void QSplineSeriesPrivate::calculateControlPoints() vector[0] = points[0].x() + 2 * points[1].x(); - for (int i = 1; i < n - 1; ++i){ + for (int i = 1; i < n - 1; ++i) vector[i] = 4 * points[i].x() + 2 * points[i + 1].x(); - } - vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0; + vector[n - 1] = (8 * points[n - 1].x() + points[n].x()) / 2.0; QVector xControl = firstControlPoints(vector); vector[0] = points[0].y() + 2 * points[1].y(); - for (int i = 1; i < n - 1; ++i) { + for (int i = 1; i < n - 1; ++i) vector[i] = 4 * points[i].y() + 2 * points[i + 1].y(); - } - vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0; + vector[n - 1] = (8 * points[n - 1].y() + points[n].y()) / 2.0; QVector yControl = firstControlPoints(vector); - for (int i = 0,j =0; i < n; ++i, ++j) { + for (int i = 0, j = 0; i < n; ++i, ++j) { m_controlPoints[j].setX(xControl[i]); m_controlPoints[j].setY(yControl[i]); j++; - if (i < n - 1){ - m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]); - m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]); - }else{ + if (i < n - 1) { + m_controlPoints[j].setX(2 * points[i + 1].x() - xControl[i + 1]); + m_controlPoints[j].setY(2 * points[i + 1].y() - yControl[i + 1]); + } else { m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2); m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2); } @@ -221,8 +218,9 @@ QVector QSplineSeriesPrivate::firstControlPoints(const QVector& ve for (int i = 1; i < count; i++) { temp[i] = 1 / b; b = (i < count - 1 ? 4.0 : 3.5) - temp[i]; - result[i]=(vector[i] - result[i - 1]) / b; + result[i] = (vector[i] - result[i - 1]) / b; } + for (int i = 1; i < count; i++) result[count - i - 1] -= temp[count - i] * result[count - i]; @@ -241,7 +239,7 @@ void QSplineSeriesPrivate::updateControlPoints() { Q_Q(QSplineSeries); if (q->count() > 1) { - m_controlPoints.resize(2*q->count()-2); + m_controlPoints.resize(2 * q->count() - 2); calculateControlPoints(); } } @@ -249,11 +247,9 @@ void QSplineSeriesPrivate::updateControlPoints() ChartElement* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QSplineSeries); - SplineChartItem* spline = new SplineChartItem(q,presenter); - if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + SplineChartItem *spline = new SplineChartItem(q, presenter); + if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) spline->setAnimation(new SplineAnimation(spline)); - } - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); return spline; } diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index 7cf4e19..0df1335 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -27,15 +27,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) : - XYChart(series, presenter), - QGraphicsItem(presenter ? presenter->rootItem() : 0), - m_series(series), - m_pointsVisible(false), - m_animation(0) +SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) + : XYChart(series, presenter), + QGraphicsItem(presenter ? presenter->rootItem() : 0), + m_series(series), + m_pointsVisible(false), + m_animation(0) { setZValue(ChartPresenter::SplineChartZValue); - QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); + QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); handleUpdated(); @@ -53,7 +53,7 @@ QPainterPath SplineChartItem::shape() const void SplineChartItem::setAnimation(SplineAnimation* animation) { - m_animation=animation; + m_animation = animation; XYChart::setAnimation(animation); } @@ -64,7 +64,7 @@ ChartAnimation* SplineChartItem::animation() const void SplineChartItem::setControlGeometryPoints(QVector& points) { - m_controlPoints=points; + m_controlPoints = points; } QVector SplineChartItem::controlGeometryPoints() const @@ -72,16 +72,15 @@ QVector SplineChartItem::controlGeometryPoints() const return m_controlPoints; } -void SplineChartItem::updateChart(QVector &oldPoints, QVector &newPoints,int index) +void SplineChartItem::updateChart(QVector &oldPoints, QVector &newPoints, int index) { QVector controlPoints; - if(newPoints.count()>=2) { - controlPoints.resize(newPoints.count()*2-2); - } + if (newPoints.count() >= 2) + controlPoints.resize(newPoints.count() * 2 - 2); for (int i = 0; i < newPoints.size() - 1; i++) { - controlPoints[2*i] = calculateGeometryControlPoint(2 * i); + controlPoints[2 * i] = calculateGeometryControlPoint(2 * i); controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); } @@ -92,11 +91,10 @@ void SplineChartItem::updateChart(QVector &oldPoints, QVector m_controlPoints = controlPoints; setDirty(false); - if (m_animation) { + if (m_animation) presenter()->startAnimation(m_animation); - } else { + else updateGeometry(); - } } QPointF SplineChartItem::calculateGeometryControlPoint(int index) const @@ -109,20 +107,20 @@ void SplineChartItem::updateGeometry() const QVector &points = m_points; const QVector &controlPoints = m_controlPoints; - if ((points.size()<2) || (controlPoints.size()<2)) { - prepareGeometryChange(); + if ((points.size() < 2) || (controlPoints.size() < 2)) { + prepareGeometryChange(); m_path = QPainterPath(); m_rect = QRect(); return; } - Q_ASSERT(points.count()*2-2 == controlPoints.count()); + Q_ASSERT(points.count() * 2 - 2 == controlPoints.count()); QPainterPath splinePath(points.at(0)); for (int i = 0; i < points.size() - 1; i++) { const QPointF& point = points.at(i + 1); - splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); + splinePath.cubicTo(controlPoints[2 * i], controlPoints[2 * i + 1], point); } prepareGeometryChange(); @@ -140,7 +138,7 @@ void SplineChartItem::handleUpdated() m_pointsVisible = m_series->pointsVisible(); m_linePen = m_series->pen(); m_pointPen = m_series->pen(); - m_pointPen.setWidthF(2*m_pointPen.width()); + m_pointPen.setWidthF(2 * m_pointPen.width()); update(); } diff --git a/src/splinechart/splinechartitem_p.h b/src/splinechart/splinechartitem_p.h index e588025..df58a7a 100644 --- a/src/splinechart/splinechartitem_p.h +++ b/src/splinechart/splinechartitem_p.h @@ -60,7 +60,7 @@ public Q_SLOTS: protected: void updateGeometry(); - void updateChart(QVector &oldPoints, QVector &newPoints,int index); + void updateChart(QVector &oldPoints, QVector &newPoints, int index); void mousePressEvent(QGraphicsSceneMouseEvent *event); private: diff --git a/src/themes/chartthemebluecerulean_p.h b/src/themes/chartthemebluecerulean_p.h index bdf2caf..d2a1516 100644 --- a/src/themes/chartthemebluecerulean_p.h +++ b/src/themes/chartthemebluecerulean_p.h @@ -37,8 +37,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartThemeBlueCerulean: public ChartTheme { public: - ChartThemeBlueCerulean() : ChartTheme(QChart::ChartThemeBlueCerulean) - { + ChartThemeBlueCerulean() : ChartTheme(QChart::ChartThemeBlueCerulean) { // Series colors m_seriesColors << QRgb(0xc7e85b); m_seriesColors << QRgb(0x1cb54f); diff --git a/src/themes/chartthemesystem_p.h b/src/themes/chartthemesystem_p.h index c131b1c..c624590 100644 --- a/src/themes/chartthemesystem_p.h +++ b/src/themes/chartthemesystem_p.h @@ -74,8 +74,8 @@ public: GetBValue(colorGradientActiveCaptionLeft)); g.setColorAt(0.0, start); QColor end = QColor(GetRValue(colorGradientActiveCaptionRight), - GetGValue(colorGradientActiveCaptionRight), - GetBValue(colorGradientActiveCaptionRight)); + GetGValue(colorGradientActiveCaptionRight), + GetBValue(colorGradientActiveCaptionRight)); g.setColorAt(1.0, end); m_seriesColors.append(colorAt(g, 0.5)); @@ -87,11 +87,11 @@ public: DWORD colorWindow; colorWindow = GetSysColor(COLOR_WINDOW); backgroundGradient.setColorAt(0.0, QColor(GetRValue(colorWindow), - GetGValue(colorWindow), - GetBValue(colorWindow))); + GetGValue(colorWindow), + GetBValue(colorWindow))); backgroundGradient.setColorAt(1.0, QColor(GetRValue(colorWindow), - GetGValue(colorWindow), - GetBValue(colorWindow))); + GetGValue(colorWindow), + GetBValue(colorWindow))); // Axes and other m_axisLinePen = QPen(0xd6d6d6); m_axisLinePen.setWidth(1); diff --git a/src/xychart/qhxymodelmapper.h b/src/xychart/qhxymodelmapper.h index b6e5116..6d5b652 100644 --- a/src/xychart/qhxymodelmapper.h +++ b/src/xychart/qhxymodelmapper.h @@ -43,7 +43,7 @@ public: QXYSeries* series() const; void setSeries(QXYSeries *series); - + int xRow() const; void setXRow(int xRow); diff --git a/src/xychart/qvxymodelmapper.h b/src/xychart/qvxymodelmapper.h index ead5e05..649f5bc 100644 --- a/src/xychart/qvxymodelmapper.h +++ b/src/xychart/qvxymodelmapper.h @@ -43,12 +43,12 @@ public: QXYSeries* series() const; void setSeries(QXYSeries *series); - + int xColumn() const; void setXColumn(int xColumn); int yColumn() const; - void setYColumn(int yColumn); + void setYColumn(int yColumn); int firstRow() const; void setFirstRow(int firstRow); diff --git a/src/xychart/qxymodelmapper.cpp b/src/xychart/qxymodelmapper.cpp index e1624c6..1ad404f 100644 --- a/src/xychart/qxymodelmapper.cpp +++ b/src/xychart/qxymodelmapper.cpp @@ -52,9 +52,8 @@ void QXYModelMapper::setModel(QAbstractItemModel *model) return; Q_D(QXYModelMapper); - if (d->m_model) { + if (d->m_model) disconnect(d->m_model, 0, d, 0); - } d->m_model = model; d->initializeXYFromModel(); @@ -82,9 +81,8 @@ QXYSeries* QXYModelMapper::series() const void QXYModelMapper::setSeries(QXYSeries *series) { Q_D(QXYModelMapper); - if (d->m_series) { + if (d->m_series) disconnect(d->m_series, 0, d, 0); - } if (series == 0) return; diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 1c3bd92..90c3a4f 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -173,7 +173,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs empty series object which is a child of \a parent. When series object is added to QChartView or QChart instance ownerships is transferred. */ -QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent) +QXYSeries::QXYSeries(QXYSeriesPrivate &d, QObject *parent) + : QAbstractSeries(d, parent) { } @@ -188,9 +189,9 @@ QXYSeries::~QXYSeries() /*! Adds data point \a x \a y to the series. Points are connected with lines on the chart. */ -void QXYSeries::append(qreal x,qreal y) +void QXYSeries::append(qreal x, qreal y) { - append(QPointF(x,y)); + append(QPointF(x, y)); } /*! @@ -200,8 +201,8 @@ void QXYSeries::append(qreal x,qreal y) void QXYSeries::append(const QPointF &point) { Q_D(QXYSeries); - d->m_points<m_points.count()-1); + d->m_points << point; + emit pointAdded(d->m_points.count() - 1); } /*! @@ -210,29 +211,29 @@ void QXYSeries::append(const QPointF &point) */ void QXYSeries::append(const QList &points) { - foreach(const QPointF& point , points) { + foreach (const QPointF &point , points) append(point); - } } /*! Replaces data point \a oldX \a oldY with data point \a newX \a newY. \sa QXYSeries::pointReplaced() */ -void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY) +void QXYSeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY) { - replace(QPointF(oldX,oldY),QPointF(newX,newY)); + replace(QPointF(oldX, oldY), QPointF(newX, newY)); } /*! Replaces \a oldPoint with \a newPoint. \sa QXYSeries::pointReplaced() */ -void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint) +void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint) { Q_D(QXYSeries); int index = d->m_points.indexOf(oldPoint); - if(index==-1) return; + if (index == -1) + return; d->m_points[index] = newPoint; emit pointReplaced(index); } @@ -253,9 +254,9 @@ void QXYSeries::replace(QList points) /*! Removes current \a x and \a y value. */ -void QXYSeries::remove(qreal x,qreal y) +void QXYSeries::remove(qreal x, qreal y) { - remove(QPointF(x,y)); + remove(QPointF(x, y)); } /*! @@ -267,7 +268,8 @@ void QXYSeries::remove(const QPointF &point) { Q_D(QXYSeries); int index = d->m_points.indexOf(point); - if(index==-1) return; + if (index == -1) + return; d->m_points.remove(index); emit pointRemoved(index); } @@ -342,7 +344,7 @@ QPen QXYSeries::pen() const void QXYSeries::setBrush(const QBrush &brush) { Q_D(QXYSeries); - if (d->m_brush!=brush) { + if (d->m_brush != brush) { d->m_brush = brush; emit d->updated(); } @@ -371,7 +373,7 @@ QColor QXYSeries::color() const void QXYSeries::setPointsVisible(bool visible) { Q_D(QXYSeries); - if (d->m_pointsVisible != visible){ + if (d->m_pointsVisible != visible) { d->m_pointsVisible = visible; emit d->updated(); } @@ -426,7 +428,7 @@ void QXYSeriesPrivate::scaleDomain(Domain& domain) const QList& points = q->points(); - if (!points.isEmpty()){ + if (!points.isEmpty()) { minX = points[0].x(); minY = points[0].y(); maxX = minX; @@ -442,14 +444,14 @@ void QXYSeriesPrivate::scaleDomain(Domain& domain) } } - domain.setRange(minX,maxX,minY,maxY); + domain.setRange(minX, maxX, minY, maxY); } QList QXYSeriesPrivate::createLegendMarker(QLegend* legend) { Q_Q(QXYSeries); QList list; - return list << new XYLegendMarker(q,legend); + return list << new XYLegendMarker(q, legend); } void QXYSeriesPrivate::initializeAxis(QAbstractAxis* axis) diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index f8a2887..deb2a46 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -40,15 +40,15 @@ class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) protected: - explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0); + explicit QXYSeries(QXYSeriesPrivate &d, QObject *parent = 0); public: ~QXYSeries(); void append(qreal x, qreal y); void append(const QPointF &point); void append(const QList &points); - void replace(qreal oldX,qreal oldY,qreal newX,qreal newY); - void replace(const QPointF &oldPoint,const QPointF &newPoint); + void replace(qreal oldX, qreal oldY, qreal newX, qreal newY); + void replace(const QPointF &oldPoint, const QPointF &newPoint); void remove(qreal x, qreal y); void remove(const QPointF &point); void insert(int index, const QPointF &point); diff --git a/src/xychart/xychart.cpp b/src/xychart/xychart.cpp index e97a360..4d40228 100644 --- a/src/xychart/xychart.cpp +++ b/src/xychart/xychart.cpp @@ -32,14 +32,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //TODO: optimize : remove points which are not visible -XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):ChartElement(presenter), -m_minX(0), -m_maxX(0), -m_minY(0), -m_maxY(0), -m_series(series), -m_animation(0), -m_dirty(true) +XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter) + : ChartElement(presenter), + m_minX(0), + m_maxX(0), + m_minY(0), + m_maxY(0), + m_series(series), + m_animation(0), + m_dirty(true) { QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int))); QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced())); @@ -60,44 +61,44 @@ void XYChart::setClipRect(const QRectF &rect) void XYChart::setAnimation(XYAnimation* animation) { - m_animation=animation; + m_animation = animation; } void XYChart::setDirty(bool dirty) { - m_dirty=dirty; + m_dirty = dirty; } QPointF XYChart::calculateGeometryPoint(const QPointF &point) const { - const qreal deltaX = m_size.width()/(m_maxX-m_minX); - const qreal deltaY = m_size.height()/(m_maxY-m_minY); - qreal x = (point.x() - m_minX)* deltaX; - qreal y = (point.y() - m_minY)*-deltaY + m_size.height(); - return QPointF(x,y); + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); + qreal x = (point.x() - m_minX) * deltaX; + qreal y = (point.y() - m_minY) * -deltaY + m_size.height(); + return QPointF(x, y); } QPointF XYChart::calculateGeometryPoint(int index) const { - const qreal deltaX = m_size.width()/(m_maxX-m_minX); - const qreal deltaY = m_size.height()/(m_maxY-m_minY); + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); const QList& vector = m_series->points(); - qreal x = (vector[index].x() - m_minX)* deltaX; - qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height(); - return QPointF(x,y); + qreal x = (vector[index].x() - m_minX) * deltaX; + qreal y = (vector[index].y() - m_minY) * -deltaY + m_size.height(); + return QPointF(x, y); } QVector XYChart::calculateGeometryPoints() const { - const qreal deltaX = m_size.width()/(m_maxX-m_minX); - const qreal deltaY = m_size.height()/(m_maxY-m_minY); + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); QVector result; result.resize(m_series->count()); const QList& vector = m_series->points(); for (int i = 0; i < m_series->count(); ++i) { - qreal x = (vector[i].x() - m_minX)* deltaX; - qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height(); + qreal x = (vector[i].x() - m_minX) * deltaX; + qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height(); result[i].setX(x); result[i].setY(y); } @@ -106,14 +107,14 @@ QVector XYChart::calculateGeometryPoints() const QPointF XYChart::calculateDomainPoint(const QPointF &point) const { - const qreal deltaX = m_size.width()/(m_maxX-m_minX); - const qreal deltaY = m_size.height()/(m_maxY-m_minY); - qreal x = point.x()/deltaX +m_minX; - qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY; - return QPointF(x,y); + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); + qreal x = point.x() / deltaX + m_minX; + qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY; + return QPointF(x, y); } -void XYChart::updateChart(QVector &oldPoints, QVector &newPoints,int index) +void XYChart::updateChart(QVector &oldPoints, QVector &newPoints, int index) { if (m_animation) { @@ -121,8 +122,7 @@ void XYChart::updateChart(QVector &oldPoints, QVector &newPoin m_points = newPoints; setDirty(false); presenter()->startAnimation(m_animation); - } - else { + } else { m_points = newPoints; updateGeometry(); } @@ -132,55 +132,55 @@ void XYChart::updateChart(QVector &oldPoints, QVector &newPoin void XYChart::handlePointAdded(int index) { - Q_ASSERT(indexcount()); - Q_ASSERT(index>=0); + Q_ASSERT(index < m_series->count()); + Q_ASSERT(index >= 0); QVector points; - if(m_dirty) { - points = calculateGeometryPoints(); - } else { - points = m_points; - QPointF point = calculateGeometryPoint(index); - points.insert(index, point); - } + if (m_dirty) { + points = calculateGeometryPoints(); + } else { + points = m_points; + QPointF point = calculateGeometryPoint(index); + points.insert(index, point); + } - updateChart(m_points,points,index); + updateChart(m_points, points, index); } void XYChart::handlePointRemoved(int index) { - Q_ASSERT(index<=m_series->count()); - Q_ASSERT(index>=0); + Q_ASSERT(index <= m_series->count()); + Q_ASSERT(index >= 0); - QVector points; + QVector points; - if(m_dirty) { - points = calculateGeometryPoints(); - } else { - points = m_points; - points.remove(index); - } + if (m_dirty) { + points = calculateGeometryPoints(); + } else { + points = m_points; + points.remove(index); + } - updateChart(m_points,points,index); + updateChart(m_points, points, index); } void XYChart::handlePointReplaced(int index) { - Q_ASSERT(indexcount()); - Q_ASSERT(index>=0); + Q_ASSERT(index < m_series->count()); + Q_ASSERT(index >= 0); - QVector points; + QVector points; - if(m_dirty) { - points = calculateGeometryPoints(); - } else { - QPointF point = calculateGeometryPoint(index); - points = m_points; - points.replace(index,point); - } + if (m_dirty) { + points = calculateGeometryPoints(); + } else { + QPointF point = calculateGeometryPoint(index); + points = m_points; + points.replace(index, point); + } - updateChart(m_points,points,index); + updateChart(m_points, points, index); } void XYChart::handlePointsReplaced() @@ -192,29 +192,29 @@ void XYChart::handlePointsReplaced() void XYChart::handleDomainUpdated() { - m_minX=domain()->minX(); - m_maxX=domain()->maxX(); - m_minY=domain()->minY(); - m_maxY=domain()->maxY(); + m_minX = domain()->minX(); + m_maxX = domain()->maxX(); + m_minY = domain()->minY(); + m_maxY = domain()->maxY(); if (isEmpty()) return; QVector points = calculateGeometryPoints(); - updateChart(m_points,points); + updateChart(m_points, points); } void XYChart::handleGeometryChanged(const QRectF &rect) { Q_ASSERT(rect.isValid()); - m_size=rect.size(); - m_clipRect=rect.translated(-rect.topLeft()); - m_origin=rect.topLeft(); + m_size = rect.size(); + m_clipRect = rect.translated(-rect.topLeft()); + m_origin = rect.topLeft(); if (isEmpty()) return; QVector points = calculateGeometryPoints(); - updateChart(m_points,points); + updateChart(m_points, points); } bool XYChart::isEmpty() diff --git a/src/xychart/xychart_p.h b/src/xychart/xychart_p.h index eed7006..029523b 100644 --- a/src/xychart/xychart_p.h +++ b/src/xychart/xychart_p.h @@ -43,9 +43,9 @@ class QXYSeries; class XYChart : public ChartElement { - Q_OBJECT + Q_OBJECT public: - explicit XYChart(QXYSeries *series, ChartPresenter *presenter); + explicit XYChart(QXYSeries *series, ChartPresenter *presenter); ~XYChart() {} void setGeometryPoints(const QVector& points); @@ -76,7 +76,7 @@ Q_SIGNALS: void clicked(const QPointF& point); protected: - virtual void updateChart(QVector &oldPoints,QVector &newPoints,int index = -1); + virtual void updateChart(QVector &oldPoints, QVector &newPoints, int index = -1); QPointF calculateGeometryPoint(const QPointF &point) const; QPointF calculateGeometryPoint(int index) const; QPointF calculateDomainPoint(const QPointF &point) const; @@ -99,7 +99,6 @@ protected: bool m_dirty; friend class AreaChartItem; - }; QTCOMMERCIALCHART_END_NAMESPACE