##// END OF EJS Templates
fix painting coordinates and added LGPL
fix painting coordinates and added LGPL

File last commit:

r7:a8e6b5a8e8d0 tip default
r7:a8e6b5a8e8d0 tip default
Show More
chartcolorbaraxisy.cpp
179 lines | 6.3 KiB | text/x-c | CppLexer
/*------------------------------------------------------------------------------
-- This file is a part of the ColorMapChart API
-- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------*/
/*-- Author : Hugo Winter
-- Mail : hugo.winter@lpp.polytechnique.fr
----------------------------------------------------------------------------*/
#include "chartcolorbaraxisy_p.h"
#include <QtCharts/QAbstractAxis>
#include <private/chartpresenter_p.h>
//#include <QtCharts/QColorBarAxis>
#include "colorbaraxis/qcolorbaraxis.h"
#include <private/abstractchartlayout_p.h>
#include <QtWidgets/QGraphicsLayout>
#include <QtCore/QtMath>
#include <QtCore/QDebug>
QT_CHARTS_BEGIN_NAMESPACE
ChartColorBarAxisY::ChartColorBarAxisY(QColorBarAxis *axis, QPoint pos, qreal height, QLinearGradient gradient, QGraphicsItem *item)
: VerticalAxis(axis, item),
m_axis(axis),
m_gradient(gradient),
m_position(pos),
m_height(height)
{
QObject::connect(m_axis, SIGNAL(tickCountChanged(int)), this, SLOT(handleTickCountChanged(int)));
QObject::connect(m_axis, SIGNAL(minorTickCountChanged(int)),
this, SLOT(handleMinorTickCountChanged(int)));
QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
createColorBar();
}
ChartColorBarAxisY::~ChartColorBarAxisY()
{
}
QVector<qreal> ChartColorBarAxisY::calculateLayout() const
{
int tickCount = m_axis->tickCount();
Q_ASSERT(tickCount >= 2);
QVector<qreal> points;
points.resize(tickCount);
const QRectF &gridRect = gridGeometry();
const qreal deltaY = gridRect.height() / (qreal(tickCount) - 1.0);
for (int i = 0; i < tickCount; ++i)
points[i] = qreal(i) * -deltaY + gridRect.bottom();
return points;
}
void ChartColorBarAxisY::updateGeometry()
{
const QVector<qreal> &layout = ChartAxisElement::layout();
if (layout.isEmpty())
return;
setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
VerticalAxis::updateGeometry();
}
void ChartColorBarAxisY::handleTickCountChanged(int tick)
{
Q_UNUSED(tick);
QGraphicsLayoutItem::updateGeometry();
if (presenter()) presenter()->layout()->invalidate();
}
void ChartColorBarAxisY::handleMinorTickCountChanged(int tick)
{
Q_UNUSED(tick);
QGraphicsLayoutItem::updateGeometry();
if (presenter())
presenter()->layout()->invalidate();
}
void ChartColorBarAxisY::handleLabelFormatChanged(const QString &format)
{
Q_UNUSED(format);
QGraphicsLayoutItem::updateGeometry();
if(presenter()) presenter()->layout()->invalidate();
}
QSizeF ChartColorBarAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(constraint)
QSizeF sh;
QSizeF base = VerticalAxis::sizeHint(which, constraint);
QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
qreal width = 0;
// Height of vertical axis sizeHint indicates the maximum distance labels can extend past
// first and last ticks. Base height is irrelevant.
qreal height = 0;
switch (which)
{
case Qt::MinimumSize: {
QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
QStringLiteral("..."),
axis()->labelsAngle());
width = boundingRect.width() + labelPadding() + base.width() + 61.0; //TODO : hint changed 1.0 to 61.0
height = boundingRect.height() / 2.0;
sh = QSizeF(width, height);
break;
}
case Qt::PreferredSize: {
qreal labelWidth = 0.0;
qreal firstHeight = -1.0;
foreach (const QString& s, ticksList) {
QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
labelWidth = qMax(rect.width(), labelWidth);
height = rect.height();
if (firstHeight < 0.0)
firstHeight = height;
}
width = labelWidth + labelPadding() + base.width() + 62.0; //two pixels of tolerance //TODO : hint changed 2.0 to 62.0
height = qMax(height, firstHeight) / 2.0;
sh = QSizeF(width, height);
break;
}
default:
break;
}
return sh;
}
void ChartColorBarAxisY::createColorBar()
{
m_axis->setLabelsColor(QColor(0,0,255,0)); //make automatic labels disappear without changing layout (as it would be the case with setlabelVisible(false))
//gradient
QGradientStops stops = m_gradient.stops();
QLinearGradient gradient(0,m_position.y(),1,m_height);
foreach(QGradientStop stop, stops)
{
gradient.setColorAt(1-stop.first,stop.second);
}
//colorbar
QRectF rect(m_position.x()+10,m_position.y(),m_height/20,m_height); //remove +10 if needed
QGraphicsRectItem *colorbar = new QGraphicsRectItem(rect, this);
colorbar->setZValue(ChartPresenter::AxisZValue);
colorbar->setBrush(gradient);
//colorbar labels
for(int i=0;i<m_axis->tickCount();i++)
{
qreal value = m_axis->max() - (i * (m_axis->max() - m_axis->min()) / (m_axis->tickCount() - 1));
QGraphicsTextItem *label = new QGraphicsTextItem(this);
label->setPlainText(QString::number(value));
label->setPos((m_position.x()+10+m_height/20+1),(m_position.y()*0.85+i*(m_height/(m_axis->tickCount()-1)))); //remove +10 if needed
}
}
#include "moc_chartcolorbaraxisy_p.cpp"
QT_CHARTS_END_NAMESPACE