#include "chartcolorbaraxisy_p.h" #include #include //#include #include "colorbaraxis/qcolorbaraxis.h" #include #include #include #include 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 ChartColorBarAxisY::calculateLayout() const { int tickCount = m_axis->tickCount(); Q_ASSERT(tickCount >= 2); QVector 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 &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); for(int i=0;itickCount();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