chartlayout.cpp
179 lines
| 6.3 KiB
| text/x-c
|
CppLexer
/ src / chartlayout.cpp
Michal Klocek
|
r1535 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r1534 | #include "chartlayout_p.h" | ||
#include "chartpresenter_p.h" | ||||
Michal Klocek
|
r1837 | #include "qlegend_p.h" | ||
Michal Klocek
|
r1534 | #include "chartaxis_p.h" | ||
Michal Klocek
|
r1883 | #include <QDebug> | ||
Michal Klocek
|
r1535 | |||
Michal Klocek
|
r1534 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
ChartLayout::ChartLayout(ChartPresenter* presenter): | ||||
m_presenter(presenter), | ||||
m_marginBig(60), | ||||
m_marginSmall(20), | ||||
m_marginTiny(10), | ||||
Michal Klocek
|
r1883 | m_chartMargins(m_marginBig,m_marginBig,m_marginBig,m_marginBig), | ||
Michal Klocek
|
r1837 | m_intialized(false) | ||
Michal Klocek
|
r1534 | { | ||
} | ||||
ChartLayout::~ChartLayout() | ||||
{ | ||||
} | ||||
void ChartLayout::setGeometry(const QRectF& rect) | ||||
{ | ||||
Michal Klocek
|
r1538 | |||
Michal Klocek
|
r1534 | if (!rect.isValid()) return; | ||
QGraphicsLayout::setGeometry(rect); | ||||
Michal Klocek
|
r1837 | if(!m_intialized){ | ||
m_presenter->setGeometry(rect); | ||||
m_intialized=true; | ||||
} | ||||
Michal Klocek
|
r1534 | // check title size | ||
QSize titleSize = QSize(0,0); | ||||
if (m_presenter->titleItem()) { | ||||
titleSize= m_presenter->titleItem()->boundingRect().size().toSize(); | ||||
} | ||||
qreal axisHeight = 0; | ||||
qreal axisWidth = 0; | ||||
// check axis size | ||||
foreach (ChartAxis* axis,m_presenter->axisItems()){ | ||||
if(axis->axisType() == ChartAxis::X_AXIS) | ||||
axisHeight = qMax(axis->minimumHeight(),axisHeight); | ||||
else | ||||
axisWidth = qMax(axis->minimumWidth(),axisWidth); | ||||
} | ||||
QLegend* legend = m_presenter->legend(); | ||||
Michal Klocek
|
r1837 | Q_ASSERT(legend); | ||
Michal Klocek
|
r1534 | qreal titlePadding = m_chartMargins.top()/2; | ||
Michal Klocek
|
r1883 | QMargins chartMargins = m_chartMargins; | ||
chartMargins.setLeft(qMax(m_chartMargins.left(),int(axisWidth + 2*m_marginTiny))); | ||||
chartMargins.setBottom(qMax(m_chartMargins.bottom(),int(axisHeight + 2* m_marginTiny))); | ||||
Michal Klocek
|
r1534 | |||
// recalculate legend position | ||||
Michal Klocek
|
r1837 | if ((legend->isAttachedToChart() && legend->isVisible())) { | ||
Michal Klocek
|
r1534 | |||
// Reserve some space for legend | ||||
switch (legend->alignment()) { | ||||
case Qt::AlignTop: { | ||||
Michal Klocek
|
r1883 | |||
Michal Klocek
|
r1534 | QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1)); | ||
Michal Klocek
|
r2003 | int topMargin = qMax(2*m_marginTiny + titleSize.height() + legendSize.height() + m_marginTiny,qreal(chartMargins.top())); | ||
Michal Klocek
|
r1883 | chartMargins = QMargins(chartMargins.left(),topMargin,chartMargins.right(),chartMargins.bottom()); | ||
m_legendMargins = QMargins(chartMargins.left(),topMargin - (legendSize.height() + m_marginTiny),chartMargins.right(),rect.height()-topMargin + m_marginTiny); | ||||
Michal Klocek
|
r1534 | titlePadding = m_marginTiny + m_marginTiny; | ||
break; | ||||
} | ||||
case Qt::AlignBottom: { | ||||
QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1)); | ||||
Michal Klocek
|
r2003 | int bottomMargin = qMax(m_marginTiny + legendSize.height() + m_marginTiny + axisHeight,qreal(chartMargins.bottom())); | ||
Michal Klocek
|
r1883 | chartMargins = QMargins(chartMargins.left(),chartMargins.top(),chartMargins.right(),bottomMargin); | ||
m_legendMargins = QMargins(chartMargins.left(),rect.height()-bottomMargin + m_marginTiny + axisHeight,chartMargins.right(),m_marginTiny + m_marginSmall); | ||||
Michal Klocek
|
r1534 | titlePadding = chartMargins.top()/2; | ||
break; | ||||
} | ||||
case Qt::AlignLeft: { | ||||
QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height())); | ||||
Michal Klocek
|
r2003 | int leftPadding = qMax(m_marginTiny + legendSize.width() + m_marginTiny + axisWidth,qreal(chartMargins.left())); | ||
Michal Klocek
|
r1883 | chartMargins = QMargins(leftPadding,chartMargins.top(),chartMargins.right(),chartMargins.bottom()); | ||
m_legendMargins = QMargins(m_marginTiny + m_marginSmall,chartMargins.top(),rect.width()-leftPadding + m_marginTiny + axisWidth,chartMargins.bottom()); | ||||
Michal Klocek
|
r1534 | titlePadding = chartMargins.top()/2; | ||
break; | ||||
} | ||||
case Qt::AlignRight: { | ||||
QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height())); | ||||
Michal Klocek
|
r2003 | int rightPadding = qMax(m_marginTiny + legendSize.width() + m_marginTiny,qreal(chartMargins.right())); | ||
Michal Klocek
|
r1883 | chartMargins = QMargins(chartMargins.left(),chartMargins.top(),rightPadding,chartMargins.bottom()); | ||
m_legendMargins = QMargins(rect.width()- rightPadding+ m_marginTiny ,chartMargins.top(),m_marginTiny + m_marginSmall,chartMargins.bottom()); | ||||
Michal Klocek
|
r1534 | titlePadding = chartMargins.top()/2; | ||
break; | ||||
} | ||||
default: { | ||||
break; | ||||
} | ||||
} | ||||
legend->setGeometry(rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); | ||||
} | ||||
// recalculate title position | ||||
if (m_presenter->titleItem()) { | ||||
QPointF center = rect.center() - m_presenter->titleItem()->boundingRect().center(); | ||||
m_presenter->titleItem()->setPos(center.x(),titlePadding); | ||||
} | ||||
//recalculate background gradient | ||||
if (m_presenter->backgroundItem()) { | ||||
m_presenter->backgroundItem()->setRect(rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); | ||||
} | ||||
QRectF chartRect = rect.adjusted(chartMargins.left(),chartMargins.top(),-chartMargins.right(),-chartMargins.bottom()); | ||||
if(m_presenter->geometry()!=chartRect && chartRect.isValid()){ | ||||
m_presenter->setGeometry(chartRect); | ||||
}else if(chartRect.size().isEmpty()){ | ||||
m_presenter->setGeometry(QRect(rect.width()/2,rect.height()/2,1,1)); | ||||
} | ||||
} | ||||
QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const | ||||
{ | ||||
Q_UNUSED(constraint); | ||||
if(which == Qt::MinimumSize) | ||||
return QSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); | ||||
else | ||||
return QSize(-1,-1); | ||||
} | ||||
Michal Klocek
|
r1883 | void ChartLayout::setMinimumMargins(const QMargins& margins) | ||
Michal Klocek
|
r1534 | { | ||
Michal Klocek
|
r1883 | |||
Michal Klocek
|
r1534 | if(m_chartMargins != margins){ | ||
m_chartMargins = margins; | ||||
updateGeometry(); | ||||
} | ||||
} | ||||
Michal Klocek
|
r1883 | QMargins ChartLayout::minimumMargins() const | ||
Michal Klocek
|
r1534 | { | ||
return m_chartMargins; | ||||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||