chartcolorbaraxisy.cpp
156 lines
| 5.1 KiB
| text/x-c
|
CppLexer
winter
|
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
|
r3 | ChartColorBarAxisY::ChartColorBarAxisY(QColorBarAxis *axis, QPoint pos, qreal height, QLinearGradient gradient, QGraphicsItem *item) | ||
winter
|
r2 | : VerticalAxis(axis, item), | ||
m_axis(axis), | ||||
winter
|
r3 | m_gradient(gradient), | ||
m_position(pos), | ||||
m_height(height) | ||||
winter
|
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
|
r4 | |||
winter
|
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
|
r3 | width = boundingRect.width() + labelPadding() + base.width() + 61.0; //TODO : hint changed 1.0 to 61.0 | ||
winter
|
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
|
r3 | width = labelWidth + labelPadding() + base.width() + 62.0; //two pixels of tolerance //TODO : hint changed 2.0 to 62.0 | ||
winter
|
r2 | height = qMax(height, firstHeight) / 2.0; | ||
sh = QSizeF(width, height); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
void ChartColorBarAxisY::createColorBar() | ||||
{ | ||||
winter
|
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
|
r2 | |||
winter
|
r3 | //gradient | ||
QGradientStops stops = m_gradient.stops(); | ||||
winter
|
r4 | QLinearGradient gradient(0,m_position.y(),1,m_height); | ||
winter
|
r2 | foreach(QGradientStop stop, stops) | ||
{ | ||||
gradient.setColorAt(1-stop.first,stop.second); | ||||
} | ||||
winter
|
r3 | //colorbar | ||
winter
|
r4 | QRectF rect(m_position.x()+10,m_position.y(),m_height/20,m_height); //remove 10 if needed | ||
winter
|
r2 | |||
QGraphicsRectItem *colorbar = new QGraphicsRectItem(rect, this); | ||||
winter
|
r3 | colorbar->setZValue(ChartPresenter::AxisZValue); | ||
colorbar->setBrush(gradient); | ||||
winter
|
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
|
r2 | } | ||
#include "moc_chartcolorbaraxisy_p.cpp" | ||||
winter
|
r4 | QT_CHARTS_END_NAMESPACE | ||