##// END OF EJS Templates
adds Andy's customer request, first call to createDaultAxis scales whole domain (if on common axis)
adds Andy's customer request, first call to createDaultAxis scales whole domain (if on common axis)

File last commit:

r2353:f0ebc1456fe5
r2408:f065af9daaed
Show More
horizontalaxis.cpp
210 lines | 7.1 KiB | text/x-c | CppLexer
/ src / axis / horizontalaxis.cpp
Michal Klocek
Refactors axis updateGeometry handling...
r2111 /****************************************************************************
**
** Copyright (C) 2012 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Commercial Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/
#include "horizontalaxis_p.h"
#include "qabstractaxis.h"
#include <QFontMetrics>
Michal Klocek
Updates axis drawing code...
r2133 #include <qmath.h>
Michal Klocek
Refactors axis updateGeometry handling...
r2111 #include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Michal Klocek
Refactors internals...
r2273 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
: ChartAxis(axis, item, intervalAxis)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 {
}
HorizontalAxis::~HorizontalAxis()
{
}
void HorizontalAxis::updateGeometry()
{
const QVector<qreal>& layout = ChartAxis::layout();
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (layout.isEmpty())
return;
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Michal Klocek
Updates axis drawing code...
r2133 QStringList labelList = labels();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
QList<QGraphicsItem *> lines = lineItems();
QList<QGraphicsItem *> labels = labelItems();
QList<QGraphicsItem *> shades = shadeItems();
QList<QGraphicsItem *> axis = arrowItems();
Michal Klocek
Adds title support for mulitaxis...
r2139 QGraphicsSimpleTextItem* title = titleItem();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Michal Klocek
Updates axis drawing code...
r2133 Q_ASSERT(labels.size() == labelList.size());
Q_ASSERT(layout.size() == labelList.size());
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Jani Honkonen
src folder: another massive victory for coding style police
r2131 const QRectF &axisRect = axisGeometry();
const QRectF &gridRect = gridGeometry();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
//arrow
Jani Honkonen
src folder: another massive victory for coding style police
r2131 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(axis.at(0));
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (alignment() == Qt::AlignTop)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
Jani Honkonen
src folder: another massive victory for coding style police
r2131 else if (alignment() == Qt::AlignBottom)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
qreal width = 0;
Michal Klocek
Updates axis drawing code...
r2133 QFontMetrics fn(font());
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Michal Klocek
Adds consider title boundary for axis label wrapping
r2148 //title
if (!titleText().isNull()) {
QFontMetrics fn(title->font());
int size(0);
size = gridRect.width();
QString titleText = this->titleText();
if (fn.boundingRect(titleText).width() > size) {
QString string = titleText + "...";
while (fn.boundingRect(string).width() > size && string.length() > 3)
string.remove(string.length() - 4, 1);
title->setText(string);
} else {
title->setText(titleText);
}
QPointF center = gridRect.center() - title->boundingRect().center();
if (alignment() == Qt::AlignTop) {
title->setPos(center.x(), axisRect.top());
} else if (alignment() == Qt::AlignBottom) {
title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height());
}
}
Michal Klocek
Refactors axis updateGeometry handling...
r2111 for (int i = 0; i < layout.size(); ++i) {
Michal Klocek
Updates axis drawing code...
r2133 //items
QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
Jani Honkonen
src folder: another massive victory for coding style police
r2131 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
Michal Klocek
Refactors axis updateGeometry handling...
r2111
//grid line
gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
Michal Klocek
Updates axis drawing code...
r2133 //label text wrapping
if(intervalAxis()&& i+1!=layout.size()) {
//wrapping in case of interval axis
const qreal delta = layout[i+1] - layout[i];
QString text = labelList.at(i);
Michal Klocek
Reimplement zoom/scroll hanling for barcategoryaxis Y
r2145 if (fn.boundingRect(text).width() + 1 > delta )
Michal Klocek
Updates axis drawing code...
r2133 {
QString label = text + "...";
Michal Klocek
Reimplement zoom/scroll hanling for barcategoryaxis Y
r2145 while (fn.boundingRect(label).width() >= delta && label.length() > 3)
Michal Klocek
Updates axis drawing code...
r2133 label.remove(label.length() - 4, 1);
labelItem->setText(label);
}
else {
labelItem->setText(text);
}
}else{
labelItem->setText(labelList.at(i));
}
//label transformation origin point
const QRectF& rect = labelItem->boundingRect();
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QPointF center = rect.center();
labelItem->setTransformOriginPoint(center.x(), center.y());
//ticks and label position
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (alignment() == Qt::AlignTop) {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding());
Jani Honkonen
src folder: another massive victory for coding style police
r2131 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
} else if (alignment() == Qt::AlignBottom) {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding());
Jani Honkonen
src folder: another massive victory for coding style police
r2131 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
Michal Klocek
Updates axis drawing code...
r2133 //label in beetwen
if(intervalAxis()&& i+1!=layout.size()) {
const qreal delta = (layout[i+1] - layout[i])/2;
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
}
Michal Klocek
Updates axis drawing code...
r2133 //label overlap detection
Michal Klocek
Reimplement zoom/scroll hanling for barcategoryaxis Y
r2145 if(labelItem->pos().x() < width ||
Michal Klocek
test better layout calcuation
r2353 labelItem->pos().x() < axisRect.left() ||
labelItem->pos().x() + rect.width() -1 > axisRect.right()){
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setVisible(false);
Michal Klocek
Reimplement zoom/scroll hanling for barcategoryaxis Y
r2145 } else {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 labelItem->setVisible(true);
Michal Klocek
Updates axis drawing code...
r2133 width=rect.width()+labelItem->pos().x();
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
//shades
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if ((i + 1) % 2 && i > 1) {
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height());
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
// check if the grid line and the axis tick should be shown
qreal x = gridItem->line().p1().x();
if (x < gridRect.left() || x > gridRect.right()) {
Jani Honkonen
src folder: another massive victory for coding style police
r2131 gridItem->setVisible(false);
tickItem->setVisible(false);
Michal Klocek
Updates axis drawing code...
r2133 }else{
gridItem->setVisible(true);
tickItem->setVisible(true);
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
}
//begin/end grid line in case labels between
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (intervalAxis()) {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QGraphicsLineItem *gridLine;
Jani Honkonen
src folder: another massive victory for coding style police
r2131 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
Michal Klocek
Refactors axis updateGeometry handling...
r2111 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
Michal Klocek
Updates axis drawing code...
r2133 gridLine->setVisible(true);
gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
Michal Klocek
Refactors axis updateGeometry handling...
r2111 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
Michal Klocek
Updates axis drawing code...
r2133 gridLine->setVisible(true);
Michal Klocek
Refactors axis updateGeometry handling...
r2111 }
}
Michal Klocek
Refactors ChartAxis...
r2138 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(constraint);
Michal Klocek
Adds title support for mulitaxis...
r2139 QFontMetrics fn(titleFont());
Michal Klocek
Fixes layout rounding issue
r2306 QSizeF sh(0,0);
Michal Klocek
Refactors ChartAxis...
r2138
Michal Klocek
Bugfix: axis visibily issues...
r2297 if (titleText().isNull() || !titleItem()->isVisible())
Michal Klocek
Refactors ChartAxis...
r2138 return sh;
switch (which) {
case Qt::MinimumSize:
sh = QSizeF(fn.boundingRect("...").width(), fn.height());
break;
case Qt::MaximumSize:
case Qt::PreferredSize:
Michal Klocek
Refactors internals...
r2273 sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height());
Michal Klocek
Refactors ChartAxis...
r2138 break;
default:
break;
}
return sh;
}
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QTCOMMERCIALCHART_END_NAMESPACE