##// END OF EJS Templates
functionnal colorbar
functionnal colorbar

File last commit:

r4:6986421aea60 default
r4:6986421aea60 default
Show More
chartcolorbaraxisy.cpp
156 lines | 5.1 KiB | text/x-c | CppLexer
/ src / charts / axis / colorbaraxis / chartcolorbaraxisy.cpp
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 #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
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 ChartColorBarAxisY::ChartColorBarAxisY(QColorBarAxis *axis, QPoint pos, qreal height, QLinearGradient gradient, QGraphicsItem *item)
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 : VerticalAxis(axis, item),
m_axis(axis),
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 m_gradient(gradient),
m_position(pos),
m_height(height)
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 {
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();
winter
functionnal colorbar
r4
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 }
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());
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 width = boundingRect.width() + labelPadding() + base.width() + 61.0; //TODO : hint changed 1.0 to 61.0
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 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;
}
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 width = labelWidth + labelPadding() + base.width() + 62.0; //two pixels of tolerance //TODO : hint changed 2.0 to 62.0
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 height = qMax(height, firstHeight) / 2.0;
sh = QSizeF(width, height);
break;
}
default:
break;
}
return sh;
}
void ChartColorBarAxisY::createColorBar()
{
winter
functionnal colorbar
r4 m_axis->setLabelsColor(QColor(0,0,255,0)); //make automatic labels disappear without changing layout (as it would be the case with setlabelVisible(false))
winter
more work on colorbar axis, stuck with qtchart mecanism
r2
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 //gradient
QGradientStops stops = m_gradient.stops();
winter
functionnal colorbar
r4 QLinearGradient gradient(0,m_position.y(),1,m_height);
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 foreach(QGradientStop stop, stops)
{
gradient.setColorAt(1-stop.first,stop.second);
}
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 //colorbar
winter
functionnal colorbar
r4 QRectF rect(m_position.x()+10,m_position.y(),m_height/20,m_height); //remove 10 if needed
winter
more work on colorbar axis, stuck with qtchart mecanism
r2
QGraphicsRectItem *colorbar = new QGraphicsRectItem(rect, this);
winter
improvements in execution time, colorbar ok, more work to do on axes,
r3 colorbar->setZValue(ChartPresenter::AxisZValue);
colorbar->setBrush(gradient);
winter
functionnal colorbar
r4
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
}
winter
more work on colorbar axis, stuck with qtchart mecanism
r2 }
#include "moc_chartcolorbaraxisy_p.cpp"
winter
functionnal colorbar
r4 QT_CHARTS_END_NAMESPACE