qskinny/src/nodes/QskScaleRenderer.cpp

395 lines
10 KiB
C++
Raw Normal View History

/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskScaleRenderer.h"
2022-03-24 11:14:46 +01:00
#include "QskScaleTickmarks.h"
#include "QskSkinlet.h"
#include "QskSGNode.h"
#include "QskTickmarksNode.h"
#include "QskTextOptions.h"
2022-03-24 11:14:46 +01:00
#include "QskTextColors.h"
#include "QskGraphic.h"
2022-03-24 11:14:46 +01:00
#include "QskColorFilter.h"
#include "QskControl.h"
2022-03-24 11:14:46 +01:00
#include "QskIntervalF.h"
#include "QskFunctions.h"
#include <qstring.h>
#include <qfontmetrics.h>
static QSGNode* qskRemoveTraillingNodes( QSGNode* node, QSGNode* childNode )
{
QskSGNode::removeAllChildNodesFrom( node, childNode );
return nullptr;
}
static inline void qskInsertRemoveChild( QSGNode* parentNode,
QSGNode* oldNode, QSGNode* newNode, bool append )
{
if ( newNode == oldNode )
return;
if ( oldNode )
2020-12-05 15:09:31 +01:00
{
parentNode->removeChildNode( oldNode );
if ( oldNode->flags() & QSGNode::OwnedByParent )
delete oldNode;
}
2020-12-05 15:09:31 +01:00
if ( newNode )
2020-12-05 15:09:31 +01:00
{
if ( append )
parentNode->appendChildNode( newNode );
else
parentNode->prependChildNode( newNode );
}
}
2022-03-24 11:14:46 +01:00
class QskScaleRenderer::PrivateData
{
public:
QskIntervalF boundaries;
QskScaleTickmarks tickmarks;
QColor tickColor = Qt::black;
qreal tickWidth = 1.0;
QFont font;
QskTextColors textColors;
QskColorFilter colorFilter;
Qt::Orientation orientation = Qt::Horizontal;
};
QskScaleRenderer::QskScaleRenderer()
: m_data( new PrivateData() )
{
}
QskScaleRenderer::~QskScaleRenderer()
{
}
void QskScaleRenderer::setOrientation( Qt::Orientation orientation )
{
2022-03-24 11:14:46 +01:00
m_data->orientation = orientation;
}
void QskScaleRenderer::setBoundaries( const QskIntervalF& boundaries )
{
2022-03-24 11:14:46 +01:00
m_data->boundaries = boundaries;
}
void QskScaleRenderer::setTickmarks( const QskScaleTickmarks& tickmarks )
{
2022-03-24 11:14:46 +01:00
m_data->tickmarks = tickmarks;
}
void QskScaleRenderer::setTickColor( const QColor& color )
{
2022-03-24 11:14:46 +01:00
m_data->tickColor = color;
}
void QskScaleRenderer::setTickWidth( qreal width )
{
2022-03-24 11:14:46 +01:00
m_data->tickWidth = width;
}
void QskScaleRenderer::setFont( const QFont& font )
{
2022-03-24 11:14:46 +01:00
m_data->font = font;
}
void QskScaleRenderer::setTextColors( const QskTextColors& textColors )
{
2022-03-24 11:14:46 +01:00
m_data->textColors = textColors;
}
void QskScaleRenderer::setColorFilter( const QskColorFilter& colorFilter )
{
2022-03-24 11:14:46 +01:00
m_data->colorFilter = colorFilter;
}
QSGNode* QskScaleRenderer::updateScaleNode(
const QskSkinnable* skinnable, const QRectF& tickmarksRect,
const QRectF& labelsRect, QSGNode* node )
{
enum Role
{
Ticks = 1,
Labels = 2
};
if ( node == nullptr )
node = new QSGNode();
{
QSGNode* oldNode = QskSGNode::findChildNode( node, Ticks );
QSGNode* newNode = nullptr;
if ( !tickmarksRect.isEmpty() )
{
newNode = updateTicksNode( skinnable, tickmarksRect, oldNode );
if ( newNode )
QskSGNode::setNodeRole( newNode, Ticks );
}
qskInsertRemoveChild( node, oldNode, newNode, false );
}
{
QSGNode* oldNode = QskSGNode::findChildNode( node, Labels );
QSGNode* newNode = nullptr;
if ( !labelsRect.isEmpty() )
{
2021-02-23 12:03:41 +01:00
newNode = updateLabelsNode( skinnable, tickmarksRect, labelsRect, oldNode );
if ( newNode )
QskSGNode::setNodeRole( newNode, Labels );
}
qskInsertRemoveChild( node, oldNode, newNode, true );
}
return node;
}
QSGNode* QskScaleRenderer::updateTicksNode(
const QskSkinnable*, const QRectF& rect, QSGNode* node ) const
{
if ( rect.isEmpty() )
return nullptr;
auto ticksNode = static_cast< QskTickmarksNode* >( node );
if( ticksNode == nullptr )
ticksNode = new QskTickmarksNode;
2020-12-05 15:09:31 +01:00
2022-03-24 11:14:46 +01:00
ticksNode->update( m_data->tickColor, rect, m_data->boundaries,
m_data->tickmarks, m_data->tickWidth, m_data->orientation );
2020-12-05 15:09:31 +01:00
return ticksNode;
}
QSGNode* QskScaleRenderer::updateLabelsNode(
2021-02-23 12:03:41 +01:00
const QskSkinnable* skinnable, const QRectF& tickmarksRect,
const QRectF& labelsRect, QSGNode* node ) const
{
2021-02-23 12:03:41 +01:00
if ( labelsRect.isEmpty() || tickmarksRect.isEmpty() )
return nullptr;
2022-03-24 11:14:46 +01:00
const auto ticks = m_data->tickmarks.majorTicks();
if ( ticks.isEmpty() )
return nullptr;
if( node == nullptr )
node = new QSGNode;
2022-03-24 11:14:46 +01:00
const QFontMetricsF fm( m_data->font );
2022-03-24 11:14:46 +01:00
const qreal length = ( m_data->orientation == Qt::Horizontal )
2021-02-23 12:03:41 +01:00
? tickmarksRect.width() : tickmarksRect.height();
2022-03-24 11:14:46 +01:00
const qreal ratio = length / m_data->boundaries.width();
auto nextNode = node->firstChild();
2021-04-19 09:28:19 +02:00
QRectF labelRect;
for ( auto tick : ticks )
{
enum LabelNodeRole
{
TextNode = 1,
GraphicNode = 2
};
const auto label = labelAt( tick );
if ( label.isNull() )
continue;
2022-03-24 11:14:46 +01:00
const qreal tickPos = ratio * ( tick - m_data->boundaries.lowerBound() );
2020-12-05 15:09:31 +01:00
if ( label.canConvert< QString >() )
{
2021-04-19 09:28:19 +02:00
auto text = label.toString();
if ( text.isEmpty() )
continue;
QRectF r;
Qt::Alignment alignment;
2022-03-24 11:14:46 +01:00
if( m_data->orientation == Qt::Horizontal )
{
const auto w = qskHorizontalAdvance( fm, text );
2021-02-23 12:03:41 +01:00
auto pos = tickmarksRect.x() + tickPos - 0.5 * w;
pos = qBound( labelsRect.left(), pos, labelsRect.right() - w );
2021-02-23 12:03:41 +01:00
r = QRectF( pos, labelsRect.y(), w, labelsRect.height() );
alignment = Qt::AlignLeft;
}
else
{
const auto h = fm.height();
2021-02-23 12:03:41 +01:00
auto pos = tickmarksRect.bottom() - ( tickPos + 0.5 * h );
/*
when clipping the label we can expand the clip rectangle
by the ascent/descent margins, as nothing gets painted there
anyway.
*/
2021-02-23 12:03:41 +01:00
const qreal min = labelsRect.top() - ( h - fm.ascent() );
const qreal max = labelsRect.bottom() + fm.descent();
pos = qBound( min, pos, max );
2021-02-23 12:03:41 +01:00
r = QRectF( labelsRect.x(), pos, labelsRect.width(), h );
alignment = Qt::AlignRight;
}
if ( nextNode && QskSGNode::nodeRole( nextNode ) != TextNode )
{
nextNode = qskRemoveTraillingNodes( node, nextNode );
}
2021-04-19 09:28:19 +02:00
if ( !labelRect.isEmpty() && labelRect.intersects( r ) )
{
if ( tick != ticks.last() )
{
text = QString();
}
else
{
if ( auto obsoleteNode = nextNode
? nextNode->previousSibling() : node->lastChild() )
{
node->removeChildNode( obsoleteNode );
if ( obsoleteNode->flags() & QSGNode::OwnedByParent )
delete obsoleteNode;
}
labelRect = r;
}
}
else
{
labelRect = r;
}
nextNode = QskSkinlet::updateTextNode( skinnable, nextNode,
r, alignment, text, m_data->font, QskTextOptions(),
m_data->textColors, Qsk::Normal );
if ( nextNode )
{
if ( nextNode->parent() != node )
{
QskSGNode::setNodeRole( nextNode, TextNode );
node->appendChildNode( nextNode );
}
nextNode = nextNode->nextSibling();
}
}
else if ( label.canConvert< QskGraphic >() )
{
const auto graphic = label.value< QskGraphic >();
if ( graphic.isNull() )
continue;
const auto h = fm.height();
const auto w = graphic.widthForHeight( h );
Qt::Alignment alignment;
2022-03-24 11:14:46 +01:00
if( m_data->orientation == Qt::Horizontal )
{
2021-02-23 12:03:41 +01:00
auto pos = tickmarksRect.x() + tickPos - 0.5 * w;
pos = qBound( labelsRect.left(), pos, labelsRect.right() - w );
2021-04-19 09:28:19 +02:00
labelRect = QRectF( pos, labelsRect.y(), w, h );
alignment = Qt::AlignHCenter | Qt::AlignBottom;
}
else
{
2021-02-23 12:03:41 +01:00
auto pos = tickmarksRect.bottom() - ( tickPos + 0.5 * h );
pos = qBound( labelsRect.top(), pos, labelsRect.bottom() - h );
2021-04-19 09:28:19 +02:00
labelRect = QRectF( labelsRect.right() - w, pos, w, h );
alignment = Qt::AlignRight | Qt::AlignVCenter;
}
if ( nextNode && QskSGNode::nodeRole( nextNode ) != GraphicNode )
{
nextNode = qskRemoveTraillingNodes( node, nextNode );
}
nextNode = QskSkinlet::updateGraphicNode(
skinnable, nextNode, graphic, m_data->colorFilter, labelRect, alignment );
if ( nextNode )
{
if ( nextNode->parent() != node )
{
QskSGNode::setNodeRole( nextNode, GraphicNode );
node->appendChildNode( nextNode );
}
nextNode = nextNode->nextSibling();
}
}
}
qskRemoveTraillingNodes( node, nextNode );
return node;
}
QVariant QskScaleRenderer::labelAt( qreal pos ) const
{
return QString::number( pos, 'g' );
}
2021-02-01 10:22:54 +01:00
QSizeF QskScaleRenderer::boundingLabelSize() const
{
2022-03-24 11:14:46 +01:00
const auto ticks = m_data->tickmarks.majorTicks();
if ( ticks.isEmpty() )
2021-02-01 10:22:54 +01:00
return QSizeF( 0.0, 0.0 );
2022-03-24 11:14:46 +01:00
const QFontMetricsF fm( m_data->font );
qreal maxWidth = 0.0;
2021-02-01 10:22:54 +01:00
const qreal h = fm.height();
for ( auto tick : ticks )
{
qreal w = 0.0;
const auto label = labelAt( tick );
if ( label.isNull() )
continue;
if ( label.canConvert< QString >() )
{
w = qskHorizontalAdvance( fm, label.toString() );
}
else if ( label.canConvert< QskGraphic >() )
{
const auto graphic = label.value< QskGraphic >();
if ( !graphic.isNull() )
{
2021-02-01 10:22:54 +01:00
w = graphic.widthForHeight( h );
}
}
maxWidth = qMax( w, maxWidth );
}
2021-02-01 10:22:54 +01:00
return QSizeF( maxWidth, h );
}