diff --git a/examples/multiaxis/main.cpp b/examples/multiaxis/main.cpp index f25fb91..617e9f0 100644 --- a/examples/multiaxis/main.cpp +++ b/examples/multiaxis/main.cpp @@ -48,13 +48,21 @@ int main(int argc, char *argv[]) axisX = new QValueAxis; axisX->setTickCount(7 + i); axisX->setLinePenColor(series->pen().color()); + if (i % 2) - axisX->setAlternativePlacement(true); + axisX->setAlignment(Qt::AlignTop); + else + axisX->setAlignment(Qt::AlignBottom); + axisY = new QValueAxis; axisY->setTickCount(7 + i); axisY->setLinePenColor(series->pen().color()); + if (i % 2) - axisY->setAlternativePlacement(true); + axisY->setAlignment(Qt::AlignRight); + else + axisY->setAlignment(Qt::AlignLeft); + chart->setAxisX(axisX, series); chart->setAxisY(axisY, series); diff --git a/src/animations/axisanimation.cpp b/src/animations/axisanimation.cpp index 891007c..cb9b917 100644 --- a/src/animations/axisanimation.cpp +++ b/src/animations/axisanimation.cpp @@ -58,51 +58,49 @@ void AxisAnimation::setAnimationPoint(const QPointF &point) void AxisAnimation::setValues(QVector &oldLayout, QVector &newLayout) { - if (state() != QAbstractAnimation::Stopped) - stop(); + if (state() != QAbstractAnimation::Stopped) stop(); if (newLayout.count() == 0) - return; + return; switch (m_type) { - case ZoomOutAnimation: { - QRectF rect = m_axis->presenter()->chartsGeometry(); + case ZoomOutAnimation: { + QRectF rect = m_axis->gridGeometry(); oldLayout.resize(newLayout.count()); - for (int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { + 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); + 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]; + for(int i = 0; i < oldLayout.count(); i++) + oldLayout[i]= oldLayout[index]; } break; - case MoveForwardAnimation: { + case MoveForwardAnimation: { oldLayout.resize(newLayout.count()); - for (int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) - oldLayout[i] = oldLayout[j]; + for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) + oldLayout[i]= oldLayout[j]; } break; - case MoveBackwordAnimation: { + case MoveBackwordAnimation: { oldLayout.resize(newLayout.count()); - for (int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) - oldLayout[i] = oldLayout[j]; + for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) + oldLayout[i]= oldLayout[j]; } break; - default: { + 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(); + QRectF rect = m_axis->gridGeometry(); + 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; } diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp index bf870c7..bec4b4e 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp @@ -29,9 +29,8 @@ 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) { } @@ -44,29 +43,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 = m_gridRect.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 * m_gridRect.width()/(m_max - m_min))%int(delta) + delta; + } + else { + offset = int(offset * m_gridRect.width()/(m_max - m_min))%int(delta); + } - points[0] = rect.left(); - points[count + 1] = rect.right(); + points[0] = m_gridRect.left(); + points[count+1] = m_gridRect.right(); for (int i = 0; i < count; ++i) { - qreal x = offset + i * delta + rect.left(); - points[i + 1] = x; + qreal x = offset + i * delta + m_gridRect.left(); + points[i+1] = x; } return points; } @@ -74,14 +73,17 @@ QVector ChartBarCategoryAxisX::calculateLayout() const QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector& layout) const { 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)/m_gridRect.width(); + for (int i = 0;i < layout.count()-1; ++i) { + qreal x = qFloor((((layout[i+1] + layout[i])/2-m_gridRect.left())*d + m_min+0.5)); + if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { result << m_categoriesAxis->categories().at(x); - else - result << ""; // No label for x coordinate + } + else { + // No label for x coordinate + result << ""; + } } result << ""; return result; @@ -92,8 +94,7 @@ void ChartBarCategoryAxisX::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if (layout.isEmpty()) - return; + if(layout.isEmpty()) return; QStringList ticksList = createCategoryLabels(layout); @@ -105,54 +106,55 @@ void ChartBarCategoryAxisX::updateGeometry() Q_ASSERT(labels.size() == ticksList.size()); Q_ASSERT(layout.size() == ticksList.size()); - QRectF chartRect = presenter()->chartsGeometry(); - - const qreal delta = chartRect.width() / (m_categoriesAxis->d_ptr->count()); + const qreal delta = m_gridRect.width()/(m_categoriesAxis->d_ptr->count()); - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine(chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine( m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom()); - qreal width = chartRect.left(); + qreal width = m_gridRect.left(); for (int i = 0; i < layout.size(); ++i) { - QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setLine(layout[i], chartRect.top(), layout[i], chartRect.bottom()); - QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(layout[i], m_gridRect.top(), layout[i], m_gridRect.bottom()); + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); labelItem->setText(ticksList.at(i)); - const QRectF &rect = labelItem->boundingRect(); + const QRectF& rect = labelItem->boundingRect(); 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(), m_gridRect.bottom() + label_padding); + }else{ + labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_gridRect.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()> m_gridRect.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], m_gridRect.top(),layout[i]-layout[i-1], m_gridRect.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], m_gridRect.bottom(),layout[i], m_gridRect.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(); } -QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(constraint) @@ -160,32 +162,35 @@ 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); + 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; } - width = qMax(width, base.width()); - height += base.height(); - sh = QSizeF(width, height); - break; - default: - break; - } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp index 5c4e5f0..831cd04 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp @@ -29,9 +29,8 @@ 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) { } @@ -43,29 +42,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 = m_gridRect.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 * m_gridRect.height()/(m_max - m_min))%int(delta) + delta; + } + else { + offset = int(offset * m_gridRect.height()/(m_max - m_min))%int(delta); + } - points[0] = rect.bottom(); - points[count + 1] = rect.top(); + points[0] = m_gridRect.bottom(); + points[count+1] = m_gridRect.top(); for (int i = 0; i < count; ++i) { - int y = rect.bottom() - i * delta - offset; - points[i + 1] = y; + int y = m_gridRect.bottom() - i * delta - offset; + points[i+1] = y; } return points; } @@ -73,14 +72,17 @@ QVector ChartBarCategoryAxisY::calculateLayout() const QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector& layout) const { 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)/m_gridRect.height(); + for (int i = 0;i < layout.count()-1; ++i) { + qreal x = qFloor(((m_gridRect.height()- (layout[i+1] + layout[i])/2 + m_gridRect.top())*d + m_min+0.5)); + if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { result << m_categoriesAxis->categories().at(x); - else - result << ""; // No label for x coordinate + } + else { + // No label for x coordinate + result << ""; + } } result << ""; return result; @@ -90,8 +92,7 @@ void ChartBarCategoryAxisY::updateGeometry() { const QVector& layout = ChartAxis::layout(); - if (layout.isEmpty()) - return; + if(layout.isEmpty()) return; QStringList ticksList = createCategoryLabels(layout); @@ -103,56 +104,59 @@ void ChartBarCategoryAxisY::updateGeometry() Q_ASSERT(labels.size() == ticksList.size()); Q_ASSERT(layout.size() == ticksList.size()); - QRectF chartRect = presenter()->chartsGeometry(); + const qreal delta = m_gridRect.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(m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom()); - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); - - qreal height = chartRect.bottom(); + qreal height = m_gridRect.bottom(); 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]); - QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(m_gridRect.left() , layout[i], m_gridRect.right(), layout[i]); + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); labelItem->setText(ticksList.at(i)); - const QRectF &rect = labelItem->boundingRect(); + const QRectF& rect = labelItem->boundingRect(); 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(m_gridRect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y()); + } + else { + labelItem->setPos(m_gridRect.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() < m_gridRect.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(m_gridRect.left(),layout[i],m_gridRect.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(m_gridRect.left()-5,layout[i],m_gridRect.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(); } -QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(constraint) @@ -160,32 +164,35 @@ 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); + 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; } - height = qMax(height, base.height()); - width += base.width(); - sh = QSizeF(width, height); - break; - default: - break; - } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/categoryaxis/chartcategoryaxisx.cpp b/src/axis/categoryaxis/chartcategoryaxisx.cpp index cb57566..cf07892 100644 --- a/src/axis/categoryaxis/chartcategoryaxisx.cpp +++ b/src/axis/categoryaxis/chartcategoryaxisx.cpp @@ -30,8 +30,7 @@ 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) { } @@ -48,18 +47,16 @@ QVector ChartCategoryAxisX::calculateLayout() const if (tickCount < 2) return points; - QRectF rect = presenter()->chartsGeometry(); - qreal range = axis->max() - axis->min(); if (range > 0) { points.resize(tickCount); - qreal scale = rect.width() / range; + qreal scale = m_gridRect.width() / range; for (int i = 0; i < tickCount; ++i) if (i < tickCount - 1) { - int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + rect.left(); + int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_gridRect.left(); points[i] = x; } else { - int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + rect.left(); + int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_gridRect.left(); points[i] = x; } } @@ -70,8 +67,7 @@ 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(); @@ -82,58 +78,61 @@ 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 - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine(chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom()); for (int i = 0; i < layout.size(); ++i) { - QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - if (i < ticksList.count()) + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + if (i < ticksList.count()) { labelItem->setText(ticksList.at(i)); - const QRectF &rect = labelItem->boundingRect(); + } + const QRectF& rect = labelItem->boundingRect(); QPointF center = rect.center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - if (i < layout.size() - 1) - labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), chartRect.bottom() + label_padding); - else - labelItem->setPos(layout[i] - center.x(), chartRect.bottom() + label_padding); + if (i < layout.size() - 1) { + labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_gridRect.bottom() + label_padding); + } else { + labelItem->setPos(layout[i] - center.x(), m_gridRect.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()) + if (labelItem->pos().x() + center.x() < m_gridRect.left() || labelItem->pos().x() + center.x() > m_gridRect.right()) labelItem->setVisible(false); else labelItem->setVisible(true); 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()); + QGraphicsRectItem *rectItem = static_cast(shades.at(i / 2 - 1)); + rectItem->setRect(layout[i - 1],m_gridRect.top(),layout[i]-layout[i - 1],m_gridRect.height()); } // grid lines and axis line ticks - QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setPos(layout[i], chartRect.top()); - lineItem->setLine(0, 0, 0, chartRect.height()); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setPos(layout[i], m_gridRect.top()); + lineItem->setLine(0, 0, 0, m_gridRect.height()); - QGraphicsLineItem *tickLineItem = static_cast(axis.at(i + 1)); - tickLineItem->setPos(layout[i], chartRect.bottom()); + QGraphicsLineItem *tickLineItem = static_cast(axis.at(i+1)); + tickLineItem->setPos(layout[i], m_gridRect.bottom()); tickLineItem->setLine(0, 0, 0, 5); // check if the grid line and the axis tick should be shown - if (lineItem->pos().x() < chartRect.left() || lineItem->pos().x() > chartRect.right()) { + if (lineItem->pos().x() < m_gridRect.left() || lineItem->pos().x() > m_gridRect.right()) { lineItem->setVisible(false); tickLineItem->setVisible(false); } else { lineItem->setVisible(true); tickLineItem->setVisible(true); } + } + } void ChartCategoryAxisX::handleAxisUpdated() @@ -142,7 +141,7 @@ void ChartCategoryAxisX::handleAxisUpdated() ChartAxis::handleAxisUpdated(); } -QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(constraint) @@ -150,28 +149,31 @@ 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); + 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; } - width = qMax(width, base.width()); - height += base.height(); - sh = QSizeF(width, height); - break; - default: + default: break; } diff --git a/src/axis/categoryaxis/chartcategoryaxisy.cpp b/src/axis/categoryaxis/chartcategoryaxisy.cpp index 7cd05c5..03b1091 100644 --- a/src/axis/categoryaxis/chartcategoryaxisy.cpp +++ b/src/axis/categoryaxis/chartcategoryaxisy.cpp @@ -30,8 +30,7 @@ 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) { } @@ -48,21 +47,18 @@ QVector ChartCategoryAxisY::calculateLayout() const if (tickCount < 2) return points; - QRectF rect = presenter()->chartsGeometry(); - qreal range = axis->max() - axis->min(); if (range > 0) { points.resize(tickCount); - qreal scale = rect.height() / range; - for (int i = 0; i < tickCount; ++i) { + qreal scale = m_gridRect.height() / range; + for (int i = 0; i < tickCount; ++i) if (i < tickCount - 1) { - int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + rect.bottom(); + int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_gridRect.bottom(); points[i] = y; } else { - int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + rect.bottom(); + int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_gridRect.bottom(); points[i] = y; } - } } return points; @@ -72,8 +68,9 @@ 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(); @@ -87,59 +84,59 @@ void ChartCategoryAxisY::updateGeometry() labels.at(i)->setVisible(false); } - QRectF chartRect = presenter()->chartsGeometry(); - // axis base line - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom()); for (int i = 0; i < layout.size(); ++i) { // label items - QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); if (i < ticksList.count()) { labelItem->setText(ticksList.at(i)); } - const QRectF &rect = labelItem->boundingRect(); + const QRectF& rect = labelItem->boundingRect(); QPointF center = rect.center(); labelItem->setTransformOriginPoint(center.x(), center.y()); if (i < layout.size() - 1) - labelItem->setPos(chartRect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y()); + labelItem->setPos(m_gridRect.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(m_gridRect.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()) + if (labelItem->pos().y() + center.y() < m_gridRect.top() || labelItem->pos().y() + center.y() > m_gridRect.bottom()) labelItem->setVisible(false); 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(m_gridRect.left(),layout[i],m_gridRect.width(),layout[i-1]-layout[i]); } // grid lines and axis line ticks - QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setPos(chartRect.left(), layout[i]); - lineItem->setLine(0, 0, chartRect.width(), 0); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setPos(m_gridRect.left(), layout[i]); + lineItem->setLine(0, 0, m_gridRect.width(), 0); - QGraphicsLineItem *tickLineItem = static_cast(axis.at(i + 1)); - tickLineItem->setPos(chartRect.left(), layout[i]); + QGraphicsLineItem *tickLineItem = static_cast(axis.at(i+1)); + tickLineItem->setPos(m_gridRect.left(), layout[i]); tickLineItem->setLine(-5, 0, 0, 0); // check if the grid line and the axis tick should be shown - if (lineItem->pos().y() < chartRect.top() || lineItem->pos().y() > chartRect.bottom()) { + if (lineItem->pos().y() < m_gridRect.top() || lineItem->pos().y() > m_gridRect.bottom()) { lineItem->setVisible(false); tickLineItem->setVisible(false); } else { lineItem->setVisible(true); tickLineItem->setVisible(true); } + } + } void ChartCategoryAxisY::handleAxisUpdated() @@ -148,7 +145,7 @@ void ChartCategoryAxisY::handleAxisUpdated() ChartAxis::handleAxisUpdated(); } -QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(constraint) @@ -156,32 +153,35 @@ 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); + 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; } - height = qMax(height, base.height()); - width += base.width(); - sh = QSizeF(width, height); - break; - default: - break; - } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index d8be13c..ade1ee1 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -22,6 +22,7 @@ #include "qabstractaxis.h" #include "qabstractaxis_p.h" #include "chartpresenter_p.h" +#include "chartlayout_p.h" #include "domain_p.h" #include #include @@ -31,18 +32,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), + m_labelPadding(5) { //initial initialization m_arrow->setZValue(ChartPresenter::AxisZValue); @@ -51,7 +52,7 @@ ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter) 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(); @@ -62,27 +63,25 @@ ChartAxis::~ChartAxis() { } -void ChartAxis::setAnimation(AxisAnimation *animation) +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())); } } @@ -94,8 +93,7 @@ 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()); @@ -106,39 +104,41 @@ 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(); } @@ -206,84 +206,99 @@ 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); + } +} + +void ChartAxis::setLabelPadding(int padding) +{ + m_labelPadding=padding; } bool ChartAxis::isEmpty() { - return !m_rect.isValid() || - presenter()->chartsGeometry().isEmpty() || - qFuzzyIsNull(m_min - m_max); + return !m_axisRect.isValid() || m_gridRect.isEmpty() || qFuzzyIsNull(m_min - m_max); } void ChartAxis::handleDomainUpdated() { - Domain *domain = qobject_cast(sender()); + Domain* domain = qobject_cast(sender()); 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; @@ -293,9 +308,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, @@ -308,8 +323,8 @@ void ChartAxis::handleDomainUpdated() void ChartAxis::handleAxisUpdated() { - if (isEmpty()) - return; + if(isEmpty()) return; + bool visible = m_chartAxis->isVisible(); @@ -328,11 +343,11 @@ void ChartAxis::handleAxisUpdated() setTitleText(m_chartAxis->title()); } -void ChartAxis::setTitleText(const QString &title) +void ChartAxis::setTitleText(const QString& title) { - if (m_titleText != title) { + if(m_titleText!=title) { m_titleText = title; - m_rect = QRect(); + m_axisRect = QRect(); QGraphicsLayoutItem::updateGeometry(); presenter()->layout()->invalidate(); } @@ -346,51 +361,47 @@ void ChartAxis::hide() setShadesVisibility(false); } -void ChartAxis::setGeometry(const QRectF &rect) +void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) { - m_rect = rect; + m_gridRect = grid; + m_axisRect = axis; - 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(); + if(orientation()==Qt::Horizontal) + size = grid.width(); + else if(orientation()==Qt::Vertical) + size = grid.height(); - if (orientation() == Qt::Horizontal) - size = chartRect.width(); - 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()); + QPointF center = grid.center() - m_title->boundingRect().center(); + if(orientation()==Qt::Horizontal) { + m_title->setPos(center.x(),m_axisRect.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_axisRect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y()); + } } - } - QVector layout = calculateLayout(); - updateLayout(layout); -} + QVector layout = calculateLayout(); + updateLayout(layout); -void ChartAxis::setInternalRect(const QRectF &size) -{ - m_internalRect = size; } void ChartAxis::axisSelected() @@ -401,27 +412,28 @@ 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); } } @@ -434,9 +446,9 @@ Qt::Orientation ChartAxis::orientation() const return m_chartAxis->orientation(); } -bool ChartAxis::alternativePlacement() const +Qt::Alignment ChartAxis::alignment() const { - return m_chartAxis->alternativePlacement(); + return m_chartAxis->alignment(); } bool ChartAxis::isVisible() @@ -444,30 +456,36 @@ bool ChartAxis::isVisible() return m_chartAxis->isVisible(); } -QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { + Q_UNUSED(constraint); QFontMetrics fn(m_title->font()); QSizeF sh; - if (m_titleText.isNull()) - return 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()); + } - 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 4fdf92d..8beb394 100644 --- a/src/axis/chartaxis_p.h +++ b/src/axis/chartaxis_p.h @@ -82,13 +82,14 @@ public: void setLabelsPen(const QPen &pen); void setLabelsBrush(const QBrush &brush); void setLabelsFont(const QFont &font); + void setLabelPadding(int padding); + int labelPadding() const { return m_labelPadding;}; void setTitlePen(const QPen &pen); void setTitleBrush(const QBrush &brush); void setTitleFont(const QFont &font); void setTitleText(const QString &title); - void setLayout(QVector &layout); QVector layout() const { return m_layoutVector; } @@ -96,19 +97,19 @@ public: ChartAnimation *animation() const { return m_animation; }; Qt::Orientation orientation() const; - bool alternativePlacement() const; + Qt::Alignment alignment() const; bool isVisible(); void hide(); - void setGeometry(const QRectF &size); - QRectF geometry() const { return m_rect; } - - void setInternalRect(const QRectF &size); + void setGeometry(const QRectF &axis, const QRectF &grid); + QRectF axisGeometry() const { return m_axisRect; } + QRectF gridGeometry() const { return m_gridRect; } virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; protected: + void setGeometry(const QRectF &size){ Q_UNUSED(size);}; virtual void updateGeometry() = 0; virtual QVector calculateLayout() const = 0; QStringList createNumberLabels(qreal min, qreal max, int ticks) const; @@ -130,8 +131,8 @@ protected: QAbstractAxis *m_chartAxis; int m_labelsAngle; //TODO: to be removed - QRectF m_rect; - QRectF m_internalRect; + QRectF m_axisRect; + QRectF m_gridRect; QScopedPointer m_grid; QScopedPointer m_shades; QScopedPointer m_labels; @@ -143,6 +144,7 @@ protected: AxisAnimation *m_animation; QFont m_font; QString m_titleText; + int m_labelPadding; friend class AxisAnimation; friend class AxisItem; diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp index e107b20..97357f8 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp +++ b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp @@ -31,9 +31,8 @@ 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) { } @@ -41,31 +40,31 @@ 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 = m_gridRect.width()/(m_tickCount-1); for (int i = 0; i < m_tickCount; ++i) { - int x = i * deltaX + rect.left(); + int x = i * deltaX + m_gridRect.left(); points[i] = x; } return points; @@ -75,12 +74,11 @@ 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(); @@ -90,79 +88,81 @@ void ChartDateTimeAxisX::updateGeometry() Q_ASSERT(labels.size() == ticksList.size()); Q_ASSERT(layout.size() == ticksList.size()); - QRectF chartRect = presenter()->chartsGeometry(); - - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine(chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom()); qreal width = 0; for (int i = 0; i < layout.size(); ++i) { - 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()); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(layout[i], m_gridRect.top(), layout[i], m_gridRect.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(), m_gridRect.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],m_gridRect.top(),layout[i]-layout[i-1],m_gridRect.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],m_gridRect.bottom(),layout[i],m_gridRect.bottom()+5); } } void ChartDateTimeAxisX::handleAxisUpdated() { //TODO:: fix this - QDateTimeAxis *axis = qobject_cast(m_chartAxis); + QDateTimeAxis* axis = qobject_cast(m_chartAxis); m_tickCount = axis->tickCount(); ChartAxis::handleAxisUpdated(); } -QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(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()) + 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); 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); - break; - } - default: - break; - } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp index 820b68c..21ee91b 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp +++ b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp @@ -31,9 +31,8 @@ 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) { } @@ -41,31 +40,31 @@ 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 = m_gridRect.height()/(m_tickCount-1); for (int i = 0; i < m_tickCount; ++i) { - int y = i * -deltaY + rect.bottom(); + int y = i * -deltaY + m_gridRect.bottom(); points[i] = y; } @@ -76,12 +75,11 @@ 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(); @@ -91,82 +89,85 @@ void ChartDateTimeAxisY::updateGeometry() Q_ASSERT(labels.size() == ticksList.size()); Q_ASSERT(layout.size() == ticksList.size()); - QRectF chartRect = presenter()->chartsGeometry(); - - qreal height = chartRect.bottom(); + qreal height = m_gridRect.bottom(); - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom()); 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]); - 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]); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(m_gridRect.left() , layout[i], m_gridRect.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(m_gridRect.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(m_gridRect.left(),layout[i],m_gridRect.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(m_gridRect.left()-5,layout[i],m_gridRect.left(),layout[i]); } } void ChartDateTimeAxisY::handleAxisUpdated() { //TODO:: fix this - QDateTimeAxis *axis = qobject_cast(m_chartAxis); + QDateTimeAxis* axis = qobject_cast(m_chartAxis); m_tickCount = axis->tickCount(); ChartAxis::handleAxisUpdated(); } -QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(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()) + 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); 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); - break; } - default: - break; - } + default: + break; + } - return sh; + return sh; } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/qabstractaxis.cpp b/src/axis/qabstractaxis.cpp index 3a5ec54..d06d0f9 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,8 +272,7 @@ QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) QAbstractAxis::~QAbstractAxis() { - if (d_ptr->m_dataset) - qFatal("Still binded axis detected !"); + if(d_ptr->m_dataset) qFatal("Still binded axis detected !"); } /*! @@ -281,10 +280,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(); + } } /*! @@ -319,7 +318,7 @@ void QAbstractAxis::setLineVisible(bool visible) d_ptr->m_arrowVisible = visible; d_ptr->emitUpdated(); emit lineVisibleChanged(visible); - } + } } bool QAbstractAxis::isLineVisible() const @@ -333,7 +332,7 @@ void QAbstractAxis::setGridLineVisible(bool visible) d_ptr->m_gridLineVisible = visible; d_ptr->emitUpdated(); emit gridVisibleChanged(visible); - } + } } bool QAbstractAxis::isGridLineVisible() const @@ -349,7 +348,7 @@ void QAbstractAxis::setGridLinePen(const QPen &pen) if (d_ptr->m_gridLinePen != pen) { d_ptr->m_gridLinePen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -366,7 +365,7 @@ void QAbstractAxis::setLabelsVisible(bool visible) d_ptr->m_labelsVisible = visible; d_ptr->emitUpdated(); emit labelsVisibleChanged(visible); - } + } } bool QAbstractAxis::labelsVisible() const @@ -382,7 +381,7 @@ void QAbstractAxis::setLabelsPen(const QPen &pen) if (d_ptr->m_labelsPen != pen) { d_ptr->m_labelsPen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -401,7 +400,7 @@ void QAbstractAxis::setLabelsBrush(const QBrush &brush) if (d_ptr->m_labelsBrush != brush) { d_ptr->m_labelsBrush = brush; d_ptr->emitUpdated(); - } + } } /*! @@ -420,7 +419,7 @@ void QAbstractAxis::setLabelsFont(const QFont &font) if (d_ptr->m_labelsFont != font) { d_ptr->m_labelsFont = font; d_ptr->emitUpdated(); - } + } } /*! @@ -436,7 +435,7 @@ void QAbstractAxis::setLabelsAngle(int angle) if (d_ptr->m_labelsAngle != angle) { d_ptr->m_labelsAngle = angle; d_ptr->emitUpdated(); - } + } } int QAbstractAxis::labelsAngle() const @@ -464,7 +463,7 @@ void QAbstractAxis::setTitleVisible(bool visible) if (d_ptr->m_titleVisible != visible) { d_ptr->m_titleVisible = visible; d_ptr->emitUpdated(); - } + } } bool QAbstractAxis::titleVisible() const @@ -480,7 +479,7 @@ void QAbstractAxis::setTitlePen(const QPen &pen) if (d_ptr->m_titlePen != pen) { d_ptr->m_titlePen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -499,7 +498,7 @@ void QAbstractAxis::setTitleBrush(const QBrush &brush) if (d_ptr->m_titleBrush != brush) { d_ptr->m_titleBrush = brush; d_ptr->emitUpdated(); - } + } } /*! @@ -518,7 +517,7 @@ void QAbstractAxis::setTitleFont(const QFont &font) if (d_ptr->m_titleFont != font) { d_ptr->m_titleFont = font; d_ptr->emitUpdated(); - } + } } /*! @@ -529,17 +528,17 @@ QFont QAbstractAxis::titleFont() const return d_ptr->m_titleFont; } -void QAbstractAxis::setTitle(const QString &title) +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; } @@ -549,7 +548,7 @@ void QAbstractAxis::setShadesVisible(bool visible) d_ptr->m_shadesVisible = visible; d_ptr->emitUpdated(); emit shadesVisibleChanged(visible); - } + } } bool QAbstractAxis::shadesVisible() const @@ -565,7 +564,7 @@ void QAbstractAxis::setShadesPen(const QPen &pen) if (d_ptr->m_shadesPen != pen) { d_ptr->m_shadesPen = pen; d_ptr->emitUpdated(); - } + } } /*! @@ -631,8 +630,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); } @@ -682,44 +681,44 @@ 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); } /*! Returns the orientation in which the axis is being used (Vertical or Horizontal) */ -Qt::Orientation QAbstractAxis::orientation() +Qt::Orientation QAbstractAxis::orientation() const { return d_ptr->m_orientation; } -bool QAbstractAxis::alternativePlacement() const +Qt::Alignment QAbstractAxis::alignment() const { - return d_ptr->m_alternativePlacement; + return d_ptr->m_alignment; } -void QAbstractAxis::setAlternativePlacement(bool placement) +void QAbstractAxis::setAlignment(Qt::Alignment alignment) { - d_ptr->m_alternativePlacement = placement; + d_ptr->m_alignment=alignment; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q) - : q_ptr(q), - m_orientation(Qt::Orientation(0)), - m_dataset(0), - m_visible(true), - m_arrowVisible(true), - m_gridLineVisible(true), - m_labelsVisible(true), - m_labelsAngle(0), - m_shadesVisible(false), - m_shadesBrush(Qt::SolidPattern), - m_shadesOpacity(1.0), - m_dirty(false), - m_alternativePlacement(false) +QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q): + q_ptr(q), + m_orientation(Qt::Orientation(0)), + m_alignment(0), + m_dataset(0), + m_visible(true), + m_arrowVisible(true), + m_gridLineVisible(true), + m_labelsVisible(true), + m_labelsAngle(0), + m_shadesVisible(false), + m_shadesBrush(Qt::SolidPattern), + m_shadesOpacity(1.0), + m_dirty(false) { } @@ -731,20 +730,24 @@ 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; + if(m_orientation==Qt::Horizontal && !m_alignment) + m_alignment=Qt::AlignBottom; + else if(m_orientation==Qt::Vertical && !m_alignment) + m_alignment=Qt::AlignLeft; } diff --git a/src/axis/qabstractaxis.h b/src/axis/qabstractaxis.h index 8864da5..c5aa82d 100644 --- a/src/axis/qabstractaxis.h +++ b/src/axis/qabstractaxis.h @@ -123,9 +123,9 @@ public: void setShadesBorderColor(QColor color); QColor shadesBorderColor() const; - Qt::Orientation orientation(); - bool alternativePlacement() const; - void setAlternativePlacement(bool placement); + Qt::Orientation orientation() const; + Qt::Alignment alignment() const; + void setAlignment( Qt::Alignment alignment); //range handling void setMin(const QVariant &min); diff --git a/src/axis/qabstractaxis_p.h b/src/axis/qabstractaxis_p.h index 0d6f23a..06845de 100644 --- a/src/axis/qabstractaxis_p.h +++ b/src/axis/qabstractaxis_p.h @@ -75,6 +75,7 @@ Q_SIGNALS: protected: QAbstractAxis *q_ptr; Qt::Orientation m_orientation; + Qt::Alignment m_alignment; ChartDataSet *m_dataset; private: @@ -106,8 +107,6 @@ private: bool m_dirty; - bool m_alternativePlacement; - friend class QAbstractAxis; friend class ChartDataSet; }; diff --git a/src/axis/valueaxis/chartvalueaxisx.cpp b/src/axis/valueaxis/chartvalueaxisx.cpp index 00820b2..59d9e3f 100644 --- a/src/axis/valueaxis/chartvalueaxisx.cpp +++ b/src/axis/valueaxis/chartvalueaxisx.cpp @@ -25,15 +25,11 @@ #include #include #include -#include - -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) { } @@ -43,16 +39,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 = m_gridRect.width()/(m_tickCount-1); for (int i = 0; i < m_tickCount; ++i) { - int x = i * deltaX + rect.left(); + int x = i * deltaX + m_gridRect.left(); points[i] = x; } return points; @@ -62,10 +56,9 @@ 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(); @@ -75,65 +68,65 @@ void ChartValueAxisX::updateGeometry() Q_ASSERT(labels.size() == ticksList.size()); Q_ASSERT(layout.size() == ticksList.size()); - QRectF chartRrect = presenter()->chartsGeometry(); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + //lineItem->setLine(m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom()); - QGraphicsLineItem *lineItem = static_cast(axis.at(0)); - // lineItem->setLine(chartRrect.left(), chartRrect.bottom(), chartRrect.right(), chartRrect.bottom()); - if (m_chartAxis->alternativePlacement()) - lineItem->setLine(chartRrect.left(), m_internalRect.top(), chartRrect.right(), m_internalRect.top()); - else - lineItem->setLine(chartRrect.left(), m_internalRect.bottom(), chartRrect.right(), m_internalRect.bottom()); + if (m_chartAxis->alignment()==Qt::AlignTop) + lineItem->setLine(m_gridRect.left(), m_axisRect.bottom(), m_gridRect.right(), m_axisRect.bottom()); + else if(m_chartAxis->alignment()==Qt::AlignBottom) + lineItem->setLine(m_gridRect.left(), m_axisRect.top(), m_gridRect.right(), m_axisRect.top()); qreal width = 0; for (int i = 0; i < layout.size(); ++i) { - QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setLine(layout[i], chartRrect.top(), layout[i], chartRrect.bottom()); - QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(layout[i], m_gridRect.top(), layout[i], m_gridRect.bottom()); + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); labelItem->setText(ticksList.at(i)); - const QRectF &rect = labelItem->boundingRect(); + const QRectF& rect = labelItem->boundingRect(); QPointF center = rect.center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - // labelItem->setPos(layout[i] - center.x(), chartRrect.bottom() + label_padding); - if (m_chartAxis->alternativePlacement()) - 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()) { + //labelItem->setPos(layout[i] - center.x(), m_gridRect.bottom() + label_padding); + + if (m_chartAxis->alignment()==Qt::AlignTop) + labelItem->setPos(layout[i] - center.x(), m_axisRect.bottom() - rect.height() - labelPadding()); + else if(m_chartAxis->alignment()==Qt::AlignBottom) + labelItem->setPos(layout[i] - center.x(), m_axisRect.top() + labelPadding()); + + if(labelItem->pos().x() <= width || + labelItem->pos().x() < m_axisRect.left() || + labelItem->pos().x() + rect.width() > m_axisRect.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],m_gridRect.top(),layout[i]-layout[i-1],m_gridRect.height()); } - 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); - else - lineItem->setLine(layout[i], m_internalRect.bottom(), layout[i], m_internalRect.bottom() + 5); + lineItem = static_cast(axis.at(i+1)); + //lineItem->setLine(layout[i],m_gridRect.bottom(),layout[i],m_gridRect.bottom()+5); + + if (m_chartAxis->alignment()==Qt::AlignTop) + lineItem->setLine(layout[i],m_axisRect.bottom(),layout[i],m_axisRect.bottom() - labelPadding()); + else if(m_chartAxis->alignment()==Qt::AlignBottom) + lineItem->setLine(layout[i],m_axisRect.top(),layout[i],m_axisRect.top() + labelPadding()); + } } void ChartValueAxisX::handleAxisUpdated() { //TODO:: fix this - QValueAxis *axis = qobject_cast(m_chartAxis); - if (m_tickCount != axis->tickCount()) { - m_tickCount = axis->tickCount(); - presenter()->layout()->invalidate(); - } + QValueAxis* axis = qobject_cast(m_chartAxis); + m_tickCount = axis->tickCount(); ChartAxis::handleAxisUpdated(); } -QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(constraint) @@ -141,28 +134,31 @@ 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()+labelPadding(); + width=qMax(width,base.width()); + height+=base.height(); + sh = QSizeF(width,height); break; - } - case Qt::PreferredSize: + } + 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); + { + width+=fn.averageCharWidth()*ticksList.at(i).count(); + } + height=fn.height()+labelPadding(); + 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 51d4f43..1a904d1 100644 --- a/src/axis/valueaxis/chartvalueaxisy.cpp +++ b/src/axis/valueaxis/chartvalueaxisy.cpp @@ -27,13 +27,10 @@ #include #include -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) { } @@ -43,16 +40,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 = m_gridRect.height()/(m_tickCount-1); for (int i = 0; i < m_tickCount; ++i) { - int y = i * -deltaY + rect.bottom(); + int y = i * -deltaY + m_gridRect.bottom(); points[i] = y; } @@ -63,10 +58,9 @@ 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(); @@ -76,109 +70,112 @@ void ChartValueAxisY::updateGeometry() Q_ASSERT(labels.size() == ticksList.size()); Q_ASSERT(layout.size() == ticksList.size()); - QRectF chartRect = presenter()->chartsGeometry(); + qreal height = m_axisRect.bottom(); - qreal height = m_rect.bottom(); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + //lineItem->setLine( m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom()); - 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()); - else - lineItem->setLine(m_internalRect.left(), chartRect.top(), m_internalRect.left(), chartRect.bottom()); + if (m_chartAxis->alignment()==Qt::AlignLeft) + lineItem->setLine( m_axisRect.right() , m_gridRect.top(), m_axisRect.right(), m_gridRect.bottom()); + else if(m_chartAxis->alignment()==Qt::AlignRight) + lineItem->setLine( m_axisRect.left() , m_gridRect.top(), m_axisRect.left(), m_gridRect.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]); - QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine( m_gridRect.left() , layout[i], m_gridRect.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() > m_axisRect.right() - m_axisRect.left() - labelPadding() ) + { QString label = text + "..."; - while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3) + while (fn.boundingRect(label).width() > m_axisRect.right() - m_axisRect.left() - labelPadding() && label.length() > 3) label.remove(label.length() - 4, 1); labelItem->setText(label); - } else { + }else{ labelItem->setText(text); } - const QRectF &rect = labelItem->boundingRect(); + 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 (m_chartAxis->alternativePlacement()) - 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()); - - if (labelItem->pos().y() + rect.height() > height || - labelItem->pos().y() < m_rect.top()) { + //labelItem->setPos( m_gridRect.left() - rect.width() - labelPadding() , layout[i]-center.y()); + + if (m_chartAxis->alignment()==Qt::AlignLeft) + labelItem->setPos( m_axisRect.right() - rect.width() - labelPadding() , layout[i]-center.y()); + else if(m_chartAxis->alignment()==Qt::AlignRight) + labelItem->setPos( m_axisRect.left() + labelPadding() , layout[i]-center.y()); + + if(labelItem->pos().y() + rect.height() > height || + labelItem->pos().y() < m_axisRect.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( m_gridRect.left(),layout[i], m_gridRect.width(),layout[i-1]-layout[i]); } - 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]); - else - lineItem->setLine(m_internalRect.left() - 5, layout[i], m_internalRect.left(), layout[i]); + lineItem = static_cast(axis.at(i+1)); + // lineItem->setLine( m_gridRect.left()-5,layout[i], m_gridRect.left(),layout[i]); + + if (m_chartAxis->alignment()==Qt::AlignLeft) + lineItem->setLine( m_axisRect.right()- labelPadding(),layout[i], m_axisRect.right(),layout[i]); + else if(m_chartAxis->alignment()==Qt::AlignRight) + lineItem->setLine( m_axisRect.left(),layout[i], m_axisRect.left()+ labelPadding(),layout[i]); + } } void ChartValueAxisY::handleAxisUpdated() { //TODO:: fix this - QValueAxis *axis = qobject_cast(m_chartAxis); - if (m_tickCount != axis->tickCount()) { - m_tickCount = axis->tickCount(); - presenter()->layout()->invalidate(); - } + QValueAxis* axis = qobject_cast(m_chartAxis); + m_tickCount = axis->tickCount(); ChartAxis::handleAxisUpdated(); } -QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { Q_UNUSED(constraint) 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+labelPadding(); + 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())+labelPadding(),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/chartlayout.cpp b/src/chartlayout.cpp index 217c3e1..edffe5b 100644 --- a/src/chartlayout.cpp +++ b/src/chartlayout.cpp @@ -25,15 +25,16 @@ #include "charttitle_p.h" #include "chartbackground_p.h" #include "legendmarker_p.h" +#include 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) { } @@ -43,234 +44,284 @@ ChartLayout::~ChartLayout() } -void ChartLayout::setGeometry(const QRectF &rect) +void ChartLayout::setGeometry(const QRectF& rect) { if (!rect.isValid()) return; - QList axes = m_presenter->axisItems(); - ChartTitle *title = m_presenter->titleElement(); - QLegend *legend = m_presenter->legend(); - ChartBackground *background = m_presenter->backgroundElement(); + QList axes = m_presenter->axisItems(); + QList charts = m_presenter->chartItems(); + ChartTitle* title = m_presenter->titleElement(); + 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); + contentGeometry = calculateAxisGeometry(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()); -#endif + m_chartsRect = calculateChartsGeometry(contentGeometry,charts); QGraphicsLayout::setGeometry(rect); } -QRectF ChartLayout::calculateContentGeometry(const QRectF &geometry) const +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 +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; } -QRectF ChartLayout::calculateBackgroundMinimum(const QRectF &minimum) const +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 + +QRectF ChartLayout::calculateAxisGeometry(const QRectF& geometry, const QList& axes) const { - QSizeF vertical(0, 0); - QSizeF horizontal(0, 0); - - int topAxisCount = 0; - int bottomAxisCount = 0; - int leftAxisCount = 0; - int rightAxisCount = 0; - - // check axis size - foreach (ChartAxis *axis , axes) { - 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()) { -// horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); - if (axis->alternativePlacement()) - topAxisCount++; - else - bottomAxisCount++; + QSizeF left; + QSizeF right; + QSizeF bottom; + QSizeF top; + int leftCount = 0; + int rightCount = 0; + int topCount = 0; + int bottomCount = 0; + + foreach(ChartAxis* axis , axes) { + + if(!axis->isVisible()) continue; + + QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize); + + switch(axis->alignment()){ + case Qt::AlignLeft: + left.setWidth(left.width()+size.width()); + left.setHeight(qMax(left.height(),size.height())); + leftCount++; + break; + case Qt::AlignRight: + right.setWidth(right.width()+size.width()); + right.setHeight(qMax(right.height(),size.height())); + rightCount++; + break; + case Qt::AlignTop: + top.setWidth(qMax(top.width(),size.width())); + top.setHeight(top.height()+size.height()); + topCount++; + break; + case Qt::AlignBottom: + bottom.setWidth(qMax(bottom.width(),size.width())); + bottom.setHeight(bottom.height() + size.height()); + bottomCount++; + break; + } } - qreal width = qMin(vertical.width(), geometry.width() * golden_ratio); - - QRectF rect = geometry.adjusted(width, vertical.height() / 2, -horizontal.width() / 2, -horizontal.height()); - - // axis area width - // TODO: replase with dynamic size code - int axisWidth = 35; - - // get the final size of the plot rect - rect.adjust(leftAxisCount * axisWidth, topAxisCount * axisWidth, -rightAxisCount * axisWidth, -bottomAxisCount * axisWidth); - m_presenter->setChartsGeometry(rect); - - leftAxisCount = 0; - rightAxisCount = 0; - bottomAxisCount = 0; - topAxisCount = 0; - - // adjust the axes internal and external rects - 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) { - 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()) - topAxisCount++; - else - bottomAxisCount++; + left.setWidth(qMax(qMax(top.width()/2,bottom.width()/2),left.width())); + left.setWidth(qMin(left.width(),golden_ratio*geometry.width())); + right.setWidth(qMax(qMax(top.width()/2,bottom.width()/2),right.width())); + right.setWidth(qMin(right.width(),golden_ratio*geometry.width())); + top.setHeight(qMax(qMax(left.height()/2,right.height()/2),top.height())); + bottom.setHeight(qMax(qMax(left.height()/2,right.height()/2),bottom.height())); + + QRectF chartRect = geometry.adjusted(left.width(),top.height(),-right.width(),-bottom.height()); + + qreal leftOffset=0; + qreal rightOffset=0; + qreal topOffset=0; + qreal bottomOffset=0; + + foreach(ChartAxis* axis , axes) { + + if(!axis->isVisible()) continue; + + QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize); + + switch(axis->alignment()){ + case Qt::AlignLeft:{ + qreal width = qMin(size.width(),left.width()/leftCount); + leftOffset+=width; + axis->setGeometry(QRect(chartRect.left()-leftOffset,chartRect.top()-(size.height()+1)/2,width,chartRect.height()+size.height()+2),chartRect); + break; + } + case Qt::AlignRight:{ + qreal width = qMin(size.width(),right.width()/rightCount); + axis->setGeometry(QRect(chartRect.right()+rightOffset,chartRect.top()-(size.height()+1)/2,width,chartRect.height()+size.height()+2),chartRect); + rightOffset+=width; + break; + } + case Qt::AlignTop: + axis->setGeometry(QRect(geometry.left(),chartRect.top()-topOffset - size.height(),geometry.width(),size.height()),chartRect); + topOffset+=size.height(); + break; + case Qt::AlignBottom: + axis->setGeometry(QRect(geometry.left(),chartRect.bottom()+bottomOffset,geometry.width(),size.height()),chartRect); + bottomOffset+=size.height(); + break; } } - return rect; + return chartRect; } -QRectF ChartLayout::calculateAxisMinimum(const QRectF &minimum, const QList& axes) const +QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList& axes) const { - QSizeF vertical(0, 0); - QSizeF horizontal(0, 0); - - // check axis size - 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()) - horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize)); + QSizeF left; + QSizeF right; + QSizeF bottom; + QSizeF top; + + foreach(ChartAxis* axis , axes) { + + QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize); + + if(!axis->isVisible()) continue; + + switch(axis->alignment()) { + case Qt::AlignLeft: + left.setWidth(left.width()+size.width()); + left.setHeight(qMax(left.height()*2,size.height())); + break; + case Qt::AlignRight: + right.setWidth(right.width()+size.width()); + right.setHeight(qMax(right.height()*2,size.height())); + break; + case Qt::AlignTop: + top.setWidth(qMax(top.width(),size.width())); + top.setHeight(top.height()+size.height()); + break; + case Qt::AlignBottom: + bottom.setWidth(qMax(bottom.width(),size.width())); + bottom.setHeight(bottom.height() + size.height()); + break; + } } - return minimum.adjusted(0, 0, horizontal.width() + vertical.width(), horizontal.height() + vertical.height()); + return minimum.adjusted(0,0,left.width() + right.width() + qMax(top.width(),bottom.width()), top.height() + bottom.height() + qMax(left.height(),right.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::calculateChartsGeometry(const QRectF& geometry, const QList& charts) const { - QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)); - return geometry.adjusted(0, 0, minSize.width(), minSize.height()); + Q_ASSERT(geometry.isValid()); + + foreach(ChartElement* chart, charts) + { + chart->handleGeometryChanged(geometry); + } + + return geometry; } -QRectF ChartLayout::calculateTitleGeometry(const QRectF &geometry, ChartTitle *title) 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()); +} + +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) { - QList axes = m_presenter->axisItems(); - ChartTitle *title = m_presenter->titleElement(); - QLegend *legend = m_presenter->legend(); - QRectF minimumRect(0, 0, 0, 0); + 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); 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(); - } - return QSize(-1, -1); + minimumRect = calculateTitleMinimum(minimumRect,title); + minimumRect = calculateLegendMinimum(minimumRect,legend); + minimumRect = calculateAxisMinimum(minimumRect,axes); + return minimumRect.united(m_minChartRect).size().toSize(); + }else + return QSize(-1,-1); } -void ChartLayout::setMargins(const QMargins &margins) +void ChartLayout::setMargins(const QMargins& margins) { - if (m_margins != margins) { + if(m_margins != margins){ m_margins = margins; updateGeometry(); } @@ -281,9 +332,4 @@ QMargins ChartLayout::margins() const return m_margins; } -void ChartLayout::adjustChartGeometry() -{ - setGeometry(geometry()); -} - QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartlayout_p.h b/src/chartlayout_p.h index da4f5b2..f3552e7 100644 --- a/src/chartlayout_p.h +++ b/src/chartlayout_p.h @@ -31,43 +31,47 @@ class ChartTitle; class QLegend; class ChartAxis; class ChartBackground; +class ChartElement; class ChartLayout : public QGraphicsLayout { public: - ChartLayout(ChartPresenter *presenter); + ChartLayout(ChartPresenter* presenter); virtual ~ChartLayout(); - void setMargins(const QMargins &margins); + void setMargins(const QMargins& margins); QMargins margins() const; - void setGeometry(const QRectF &rect); - void adjustChartGeometry(); + void setGeometry(const QRectF& rect); + QRectF chartsGeometry() const { return m_chartsRect;} 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) {}; + QGraphicsLayoutItem* itemAt(int) const { return 0; }; + void removeAt(int){}; private: - 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 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 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 calculateLegendGeometry(const QRectF& geometry, QLegend* legend) const; + QRectF calculateAxisGeometry(const QRectF& geometry, const QList& axes) const; + QRectF calculateChartsGeometry(const QRectF& geometry, const QList& charts) 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; private: - ChartPresenter *m_presenter; + ChartPresenter* m_presenter; QMargins m_margins; QRectF m_minChartRect; QRectF m_minAxisRect; + QRectF m_chartsRect; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 156ed48..9e0c221 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -35,16 +35,15 @@ 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) { } @@ -54,84 +53,65 @@ ChartPresenter::~ChartPresenter() delete m_chartTheme; } -void ChartPresenter::setChartsGeometry(const QRectF &rect) +void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain) { - Q_ASSERT(rect.isValid()); - - if (m_chartsRect != rect) { - m_chartsRect = rect; - foreach (ChartElement *chart, m_chartItems) - chart->handleGeometryChanged(rect); - } -} - -QRectF ChartPresenter::chartsGeometry() const -{ - return m_chartsRect; -} - -void ChartPresenter::handleAxisAdded(QAbstractAxis *axis, Domain *domain) -{ - ChartAxis *item = axis->d_ptr->createGraphics(this); + 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(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); m_axisItems.insert(axis, item); selectVisibleAxis(); + m_layout->invalidate(); } -void ChartPresenter::handleAxisRemoved(QAbstractAxis *axis) +void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis) { - ChartAxis *item = m_axisItems.take(axis); + ChartAxis* item = m_axisItems.take(axis); Q_ASSERT(item); 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(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated())); //initialize item->handleDomainUpdated(); - if (m_chartsRect.isValid()) - item->handleGeometryChanged(m_chartsRect); - m_chartItems.insert(series, item); + m_chartItems.insert(series,item); + m_layout->invalidate(); } -void ChartPresenter::handleSeriesRemoved(QAbstractSeries *series) +void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series) { - ChartElement *item = m_chartItems.take(series); + ChartElement* item = m_chartItems.take(series); Q_ASSERT(item); item->deleteLater(); } void ChartPresenter::selectVisibleAxis() { - QMapIterator i(m_axisItems); + QMapIterator i(m_axisItems); while (i.hasNext()) { i.next(); @@ -161,26 +141,27 @@ 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); + 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); @@ -201,8 +182,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(); } @@ -210,46 +191,46 @@ void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) void ChartPresenter::resetAllElements() { - QMapIterator i(m_axisItems); + 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); - } + QMapIterator j(m_chartItems); + 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.moveCenter(chartsGeometry().center()); + QRectF rect = m_layout->chartsGeometry(); + rect.setWidth(rect.width()/factor); + rect.setHeight(rect.height()/factor); + rect.moveCenter(m_layout->chartsGeometry().center()); zoomIn(rect); } -void ChartPresenter::zoomIn(const QRectF &rect) +void ChartPresenter::zoomIn(const QRectF& rect) { QRectF r = rect.normalized(); - r.translate(-chartsGeometry().topLeft()); + r.translate(-m_layout->chartsGeometry().topLeft()); if (!r.isValid()) 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()/m_layout->chartsGeometry().width(),r.center().y()/m_layout->chartsGeometry().height()); + m_dataset->zoomInDomain(r,m_layout->chartsGeometry().size()); m_state = ShowState; } @@ -258,26 +239,26 @@ void ChartPresenter::zoomOut(qreal factor) m_state = ZoomOutState; QRectF chartRect; - chartRect.setSize(chartsGeometry().size()); + chartRect.setSize(m_layout->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()/m_layout->chartsGeometry().width(),rect.center().y()/m_layout->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,m_layout->chartsGeometry().size()); m_state = ShowState; } @@ -306,22 +287,21 @@ 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) +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())); } -void ChartPresenter::setBackgroundBrush(const QBrush &brush) +void ChartPresenter::setBackgroundBrush(const QBrush& brush) { createBackgroundItem(); m_background->setBrush(brush); @@ -330,12 +310,11 @@ void ChartPresenter::setBackgroundBrush(const QBrush &brush) QBrush ChartPresenter::backgroundBrush() const { - if (!m_background) - return QBrush(); + if (!m_background) return QBrush(); return m_background->brush(); } -void ChartPresenter::setBackgroundPen(const QPen &pen) +void ChartPresenter::setBackgroundPen(const QPen& pen) { createBackgroundItem(); m_background->setPen(pen); @@ -344,12 +323,11 @@ void ChartPresenter::setBackgroundPen(const QPen &pen) QPen ChartPresenter::backgroundPen() const { - if (!m_background) - return QPen(); + if (!m_background) return QPen(); return m_background->pen(); } -void ChartPresenter::setTitle(const QString &title) +void ChartPresenter::setTitle(const QString& title) { createTitleItem(); m_title->setText(title); @@ -358,12 +336,11 @@ void ChartPresenter::setTitle(const QString &title) QString ChartPresenter::title() const { - if (!m_title) - return QString(); + if (!m_title) return QString(); return m_title->text(); } -void ChartPresenter::setTitleFont(const QFont &font) +void ChartPresenter::setTitleFont(const QFont& font) { createTitleItem(); m_title->setFont(font); @@ -372,8 +349,7 @@ void ChartPresenter::setTitleFont(const QFont &font) QFont ChartPresenter::titleFont() const { - if (!m_title) - return QFont(); + if (!m_title) return QFont(); return m_title->font(); } @@ -386,8 +362,7 @@ void ChartPresenter::setTitleBrush(const QBrush &brush) QBrush ChartPresenter::titleBrush() const { - if (!m_title) - return QBrush(); + if (!m_title) return QBrush(); return m_title->brush(); } @@ -400,8 +375,7 @@ void ChartPresenter::setBackgroundVisible(bool visible) bool ChartPresenter::isBackgroundVisible() const { - if (!m_background) - return false; + if (!m_background) return false; return m_background->isVisible(); } @@ -413,28 +387,17 @@ void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled) bool ChartPresenter::isBackgroundDropShadowEnabled() const { - if (!m_background) - return false; + if (!m_background) return false; return m_background->isDropShadowEnabled(); } -QGraphicsLayout *ChartPresenter::layout() +ChartLayout* ChartPresenter::layout() { return m_layout; } -void ChartPresenter::setMargins(const QMargins &margins) -{ - m_layout->setMargins(margins); -} - -QMargins ChartPresenter::margins() const -{ - return m_layout->margins(); -} - -QLegend *ChartPresenter::legend() +QLegend* ChartPresenter::legend() { return m_chart->legend(); } @@ -444,17 +407,22 @@ void ChartPresenter::setVisible(bool visible) m_chart->setVisible(visible); } -ChartBackground *ChartPresenter::backgroundElement() +ChartBackground* ChartPresenter::backgroundElement() { return m_background; } -QList ChartPresenter::axisItems() const +QList ChartPresenter::axisItems() const { return m_axisItems.values(); } -ChartTitle *ChartPresenter::titleElement() +QList ChartPresenter::chartItems() const +{ + return m_chartItems.values(); +} + +ChartTitle* ChartPresenter::titleElement() { return m_title; } diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index f23f8fd..7466f72 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -78,28 +78,29 @@ public: ZoomOutState }; - ChartPresenter(QChart *chart, ChartDataSet *dataset); + ChartPresenter(QChart* chart,ChartDataSet *dataset); virtual ~ChartPresenter(); ChartTheme *chartTheme() const { return m_chartTheme; } ChartDataSet *dataSet() const { return m_dataset; } - QGraphicsItem *rootItem() const { return m_chart; } - ChartBackground *backgroundElement(); - ChartTitle *titleElement(); - QList axisItems() const; + QGraphicsItem* rootItem() const { return m_chart; } + ChartBackground* backgroundElement(); + ChartTitle* titleElement(); + QList axisItems() const; + QList chartItems() const; - QLegend *legend(); + QLegend* legend(); - void setBackgroundBrush(const QBrush &brush); + void setBackgroundBrush(const QBrush& brush); QBrush backgroundBrush() const; - void setBackgroundPen(const QPen &pen); + void setBackgroundPen(const QPen& pen); QPen backgroundPen() const; - void setTitle(const QString &title); + void setTitle(const QString& title); QString title() const; - void setTitleFont(const QFont &font); + void setTitleFont(const QFont& font); QFont titleFont() const; void setTitleBrush(const QBrush &brush); @@ -113,29 +114,24 @@ 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); QChart::AnimationOptions animationOptions() const; void zoomIn(qreal factor); - void zoomIn(const QRectF &rect); + 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; - - void startAnimation(ChartAnimation *animation); + void startAnimation(ChartAnimation* animation); State state() const { return m_state; } QPointF statePoint() const { return m_statePoint; } void resetAllElements(); - void setMargins(const QMargins &margins); - QMargins margins() const; - QGraphicsLayout *layout(); + ChartLayout* layout(); private: void createBackgroundItem(); @@ -143,34 +139,31 @@ private: void selectVisibleAxis(); public Q_SLOTS: - void handleSeriesAdded(QAbstractSeries *series, Domain *domain); - void handleSeriesRemoved(QAbstractSeries *series); - void handleAxisAdded(QAbstractAxis *axis, Domain *domain); - void handleAxisRemoved(QAbstractAxis *axis); + void handleSeriesAdded(QAbstractSeries* series,Domain* domain); + void handleSeriesRemoved(QAbstractSeries* series); + void handleAxisAdded(QAbstractAxis* axis,Domain* domain); + void handleAxisRemoved(QAbstractAxis* axis); void handleAxisVisibleChanged(bool visible); private Q_SLOTS: void handleAnimationFinished(); Q_SIGNALS: - void geometryChanged(const QRectF &rect); void animationsFinished(); - void marginsChanged(QRectF margins); private: - QChart *m_chart; - ChartDataSet *m_dataset; + QChart* m_chart; + ChartDataSet* m_dataset; ChartTheme *m_chartTheme; - QMap m_chartItems; - QMap m_axisItems; - QRectF m_chartsRect; + QMap m_chartItems; + QMap m_axisItems; QChart::AnimationOptions m_options; State m_state; QPointF m_statePoint; - QList m_animations; - ChartLayout *m_layout; - ChartBackground *m_background; - ChartTitle *m_title; + QList m_animations; + ChartLayout* m_layout; + ChartBackground* m_background; + ChartTitle* m_title; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/legendlayout.cpp b/src/legend/legendlayout.cpp index 222be6d..ac72bf8 100644 --- a/src/legend/legendlayout.cpp +++ b/src/legend/legendlayout.cpp @@ -22,7 +22,7 @@ #include "chartpresenter_p.h" #include "legendmarker_p.h" #include "qlegend_p.h" -#include +#include "chartlayout_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 199e010..beacb7d 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -38,10 +38,10 @@ #include "qpieseries_p.h" #include "qpieslice.h" #include "chartpresenter_p.h" +#include "chartlayout_p.h" #include #include #include -#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE diff --git a/src/qchart.cpp b/src/qchart.cpp index 90804eb..27d195d 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -24,9 +24,9 @@ #include "qlegend_p.h" #include "chartbackground_p.h" #include "qabstractaxis.h" +#include "chartlayout_p.h" #include #include -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -104,12 +104,11 @@ 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); @@ -125,7 +124,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; } /*! @@ -166,7 +165,7 @@ void QChart::removeAllSeries() /*! Sets the \a brush that is used for painting the background of the chart area. */ -void QChart::setBackgroundBrush(const QBrush &brush) +void QChart::setBackgroundBrush(const QBrush& brush) { d_ptr->m_presenter->setBackgroundBrush(brush); } @@ -182,7 +181,7 @@ QBrush QChart::backgroundBrush() const /*! Sets the \a pen that is used for painting the background of the chart area. */ -void QChart::setBackgroundPen(const QPen &pen) +void QChart::setBackgroundPen(const QPen& pen) { d_ptr->m_presenter->setBackgroundPen(pen); } @@ -198,7 +197,7 @@ QPen QChart::backgroundPen() const /*! Sets the chart \a title. The description text that is drawn above the chart. */ -void QChart::setTitle(const QString &title) +void QChart::setTitle(const QString& title) { d_ptr->m_presenter->setTitle(title); } @@ -214,7 +213,7 @@ QString QChart::title() const /*! Sets the \a font that is used for drawing the chart description text that is rendered above the chart. */ -void QChart::setTitleFont(const QFont &font) +void QChart::setTitleFont(const QFont& font) { d_ptr->m_presenter->setTitleFont(font); } @@ -264,10 +263,9 @@ void QChart::zoomIn() /*! Zooms in the view to a maximum level at which \a rect is still fully visible. */ -void QChart::zoomIn(const QRectF &rect) +void QChart::zoomIn(const QRectF& rect) { - if (!rect.isValid()) - return; + if (!rect.isValid()) return; d_ptr->m_presenter->zoomIn(rect); } @@ -305,7 +303,7 @@ void QChart::zoom(qreal factor) Returns the pointer to the x axis object of the chart asociated with the specified \a series If no series is provided then pointer to currently visible axis is provided */ -QAbstractAxis *QChart::axisX(QAbstractSeries *series) const +QAbstractAxis* QChart::axisX(QAbstractSeries* series) const { return d_ptr->m_dataset->axisX(series); } @@ -314,7 +312,7 @@ QAbstractAxis *QChart::axisX(QAbstractSeries *series) const Returns the pointer to the y axis object of the chart asociated with the specified \a series If no series is provided then pointer to currently visible axis is provided */ -QAbstractAxis *QChart::axisY(QAbstractSeries *series) const +QAbstractAxis* QChart::axisY(QAbstractSeries *series) const { return d_ptr->m_dataset->axisY(series); } @@ -355,13 +353,13 @@ QAbstractAxis *QChart::axisY(QAbstractSeries *series) const */ void QChart::createDefaultAxes() { - d_ptr->m_dataset->createDefaultAxes(); + d_ptr->m_dataset->createDefaultAxes(); } /*! Returns the legend object of the chart. Ownership stays in chart. */ -QLegend *QChart::legend() const +QLegend* QChart::legend() const { return d_ptr->m_legend; } @@ -369,9 +367,9 @@ QLegend *QChart::legend() const /*! Sets the minimum \a margins between the plot area (axes) and the edge of the chart widget. */ -void QChart::setMargins(const QMargins &margins) +void QChart::setMargins(const QMargins& margins) { - d_ptr->m_presenter->setMargins(margins); + d_ptr->m_presenter->layout()->setMargins(margins); } /*! @@ -380,7 +378,7 @@ void QChart::setMargins(const QMargins &margins) */ QMargins QChart::margins() const { - return d_ptr->m_presenter->margins(); + return d_ptr->m_presenter->layout()->margins(); } /*! @@ -389,7 +387,7 @@ QMargins QChart::margins() const */ QRectF QChart::plotArea() const { - return d_ptr->m_presenter->chartsGeometry(); + return d_ptr->m_presenter->layout()->chartsGeometry(); } ///*! @@ -447,7 +445,7 @@ bool QChart::isDropShadowEnabled() const \sa addSeries(), removeSeries(), removeAllSeries() */ -QList QChart::series() const +QList QChart::series() const { return d_ptr->m_dataset->series(); } @@ -457,9 +455,10 @@ QList QChart::series() const \sa axisX(), axisY(), setAxisY(), createDefaultAxes() */ -void QChart::setAxisX(QAbstractAxis *axis , QAbstractSeries *series) +void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series) { - d_ptr->m_dataset->setAxis(series, axis, Qt::Horizontal); + if(axis->alignment()==Qt::AlignLeft || axis->alignment()==Qt::AlignRight) return; + d_ptr->m_dataset->setAxis(series,axis,Qt::Horizontal); } /*! @@ -467,9 +466,10 @@ 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); + if(axis->alignment()==Qt::AlignTop || axis->alignment()==Qt::AlignBottom) return; + d_ptr->m_dataset->setAxis(series,axis,Qt::Vertical); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -489,10 +489,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))); }