qskinny/src/nodes/QskScaleRenderer.cpp

549 lines
13 KiB
C++
Raw Normal View History

/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
2023-04-06 07:23:37 +00:00
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#include "QskScaleRenderer.h"
2023-11-28 09:46:03 +00:00
#include "QskTickmarks.h"
#include "QskSkinlet.h"
#include "QskSGNode.h"
2023-11-28 12:36:47 +00:00
#include "QskAxisScaleNode.h"
#include "QskTextOptions.h"
2022-03-24 10:14:46 +00:00
#include "QskTextColors.h"
#include "QskGraphic.h"
2022-03-24 10:14:46 +00:00
#include "QskColorFilter.h"
#include "QskControl.h"
2022-03-24 10:14:46 +00:00
#include "QskIntervalF.h"
#include "QskFunctions.h"
#include <qstring.h>
#include <qfontmetrics.h>
2023-11-28 12:36:47 +00:00
#include <qquickwindow.h>
namespace
{
class ScaleMap
{
public:
inline ScaleMap( bool isHorizontal, const QTransform& transform )
: t( isHorizontal ? transform.dx() : transform.dy() )
, f( isHorizontal ? transform.m11() : transform.m22() )
{
}
inline qreal map( qreal v ) const { return t + f * v; };
private:
const qreal t;
const qreal f;
};
}
static inline bool qskIsHorizontal( Qt::Edge edge )
{
return edge & ( Qt::TopEdge | Qt::BottomEdge );
}
static QSGNode* qskRemoveTraillingNodes( QSGNode* node, QSGNode* childNode )
{
QskSGNode::removeAllChildNodesFrom( node, childNode );
return nullptr;
}
2023-11-28 12:36:47 +00:00
static inline QTransform qskScaleTransform( Qt::Edge edge,
const QskIntervalF& boundaries, const QskIntervalF& range )
{
2023-11-28 12:36:47 +00:00
using T = QTransform;
2023-11-28 12:36:47 +00:00
if ( qskIsHorizontal( edge ) )
2020-12-05 14:09:31 +00:00
{
2023-11-28 12:36:47 +00:00
auto transform = T::fromTranslate( -boundaries.lowerBound(), 0.0 );
transform *= T::fromScale( range.length() / boundaries.length(), 1.0 );
transform *= T::fromTranslate( range.lowerBound(), 0.0 );
return transform;
}
else
{
auto transform = T::fromTranslate( 0.0, -boundaries.lowerBound() );
transform *= T::fromScale( 1.0, -range.length() / boundaries.length() );
transform *= T::fromTranslate( 0.0, range.upperBound() );
return transform;
}
2023-11-28 12:36:47 +00:00
}
2020-12-05 14:09:31 +00:00
2023-11-28 12:36:47 +00:00
static inline quint8 qskLabelNodeRole( const QVariant& label )
{
if ( !label.isNull() )
2020-12-05 14:09:31 +00:00
{
2023-11-28 12:36:47 +00:00
if ( label.canConvert< QString >() )
return 1;
if ( label.canConvert< QskGraphic >() )
return 2;
}
2023-11-28 12:36:47 +00:00
return QskSGNode::NoRole;
}
2022-03-24 10:14:46 +00:00
class QskScaleRenderer::PrivateData
{
public:
2023-11-28 12:36:47 +00:00
// Coordinates related to the scales
2022-03-24 10:14:46 +00:00
QskIntervalF boundaries;
2023-11-28 09:46:03 +00:00
QskTickmarks tickmarks;
2022-03-24 10:14:46 +00:00
2023-11-28 12:36:47 +00:00
/*
Item cooordinates. In case of an horizontal scale
position is an y coordinate, while range corresponds to x coordinates
( vertical: v.v )
*/
qreal position = 0.0;
QskIntervalF range;
#if 1
QColor tickColor = Qt::black; // rgb value ???
#endif
2022-03-24 10:14:46 +00:00
qreal tickWidth = 1.0;
2023-11-28 12:36:47 +00:00
qreal tickLength = 10.0;
qreal spacing = 5.0;
2022-03-24 10:14:46 +00:00
QFont font;
QskTextColors textColors;
QskColorFilter colorFilter;
2023-11-28 12:36:47 +00:00
Qt::Edge edge = Qt::BottomEdge;
QskScaleRenderer::Flags flags = ClampedLabels;
2022-03-24 10:14:46 +00:00
};
QskScaleRenderer::QskScaleRenderer()
: m_data( new PrivateData() )
{
}
QskScaleRenderer::~QskScaleRenderer()
{
}
2023-11-28 12:36:47 +00:00
void QskScaleRenderer::setEdge( Qt::Edge edge )
{
2023-11-28 12:36:47 +00:00
m_data->edge = edge;
}
2023-11-28 12:36:47 +00:00
Qt::Edge QskScaleRenderer::edge() const
2023-04-24 09:55:36 +00:00
{
2023-11-28 12:36:47 +00:00
return m_data->edge;
2023-04-24 09:55:36 +00:00
}
2023-11-28 12:36:47 +00:00
void QskScaleRenderer::setFlag( Flag flag, bool on )
2023-02-28 10:59:46 +00:00
{
2023-11-28 12:36:47 +00:00
if ( on )
m_data->flags |= flag;
else
m_data->flags &= ~flag;
2023-02-28 10:59:46 +00:00
}
2023-11-28 12:36:47 +00:00
void QskScaleRenderer::setFlags( Flags flags )
2023-04-24 09:55:36 +00:00
{
2023-11-28 12:36:47 +00:00
m_data->flags = flags;
}
QskScaleRenderer::Flags QskScaleRenderer::flags() const
{
return m_data->flags;
2023-04-24 09:55:36 +00:00
}
void QskScaleRenderer::setBoundaries( qreal lowerBound, qreal upperBound )
{
setBoundaries( QskIntervalF( lowerBound, upperBound ) );
}
void QskScaleRenderer::setBoundaries( const QskIntervalF& boundaries )
{
2022-03-24 10:14:46 +00:00
m_data->boundaries = boundaries;
}
2023-04-24 09:55:36 +00:00
QskIntervalF QskScaleRenderer::boundaries() const
{
return m_data->boundaries;
}
2023-11-28 12:36:47 +00:00
qreal QskScaleRenderer::position() const
{
return m_data->position;
}
void QskScaleRenderer::setPosition( qreal pos )
{
m_data->position = pos;
}
void QskScaleRenderer::setRange( qreal from, qreal to )
{
setRange( QskIntervalF( from, to ) );
}
void QskScaleRenderer::setRange( const QskIntervalF& range )
{
m_data->range = range;
}
QskIntervalF QskScaleRenderer::range() const
{
return m_data->range;
}
2023-11-28 09:46:03 +00:00
void QskScaleRenderer::setTickmarks( const QskTickmarks& tickmarks )
{
2022-03-24 10:14:46 +00:00
m_data->tickmarks = tickmarks;
}
2023-11-28 09:46:03 +00:00
const QskTickmarks& QskScaleRenderer::tickmarks() const
2023-04-24 09:55:36 +00:00
{
return m_data->tickmarks;
}
2023-11-28 12:36:47 +00:00
void QskScaleRenderer::setSpacing( qreal spacing )
{
m_data->spacing = qMax( spacing, 0.0 );
}
qreal QskScaleRenderer::spacing() const
{
return m_data->spacing;
}
void QskScaleRenderer::setTickColor( const QColor& color )
{
2022-03-24 10:14:46 +00:00
m_data->tickColor = color;
}
2023-04-24 09:55:36 +00:00
QColor QskScaleRenderer::tickColor() const
{
return m_data->tickColor;
}
2023-11-28 12:36:47 +00:00
void QskScaleRenderer::setTickLength( qreal length )
{
m_data->tickLength = qMax( length, 0.0 );
}
qreal QskScaleRenderer::tickLength() const
{
return m_data->tickLength;
}
void QskScaleRenderer::setTickWidth( qreal width )
{
2023-11-28 12:36:47 +00:00
m_data->tickWidth = qMax( width, 0.0 );
}
2023-04-24 09:55:36 +00:00
qreal QskScaleRenderer::tickWidth() const
{
return m_data->tickWidth;
}
void QskScaleRenderer::setFont( const QFont& font )
{
2022-03-24 10:14:46 +00:00
m_data->font = font;
}
2023-04-24 09:55:36 +00:00
QFont QskScaleRenderer::font() const
{
return m_data->font;
}
void QskScaleRenderer::setTextColors( const QskTextColors& textColors )
{
2022-03-24 10:14:46 +00:00
m_data->textColors = textColors;
}
2023-04-24 09:55:36 +00:00
QskTextColors QskScaleRenderer::textColors() const
{
return m_data->textColors;
}
void QskScaleRenderer::setColorFilter( const QskColorFilter& colorFilter )
{
2022-03-24 10:14:46 +00:00
m_data->colorFilter = colorFilter;
}
2023-04-24 09:55:36 +00:00
const QskColorFilter& QskScaleRenderer::colorFilter() const
{
return m_data->colorFilter;
}
2023-11-28 12:36:47 +00:00
QSGNode* QskScaleRenderer::updateNode(
const QskSkinnable* skinnable, QSGNode* node )
{
2023-11-28 12:36:47 +00:00
enum Role : quint8 { Ticks = 1, Labels = 2 };
static const QVector< quint8 > roles = { Ticks, Labels };
const auto transform = qskScaleTransform(
m_data->edge, m_data->boundaries, m_data->range );
if ( node == nullptr )
node = new QSGNode();
2023-11-28 12:36:47 +00:00
for ( auto role : roles )
{
2023-11-28 12:36:47 +00:00
auto oldNode = QskSGNode::findChildNode( node, role );
2023-11-28 12:36:47 +00:00
auto newNode = ( role == Ticks )
? updateTicksNode( transform, oldNode )
: updateLabelsNode( skinnable, transform, oldNode );
2023-11-28 12:36:47 +00:00
QskSGNode::replaceChildNode( roles, role, node, oldNode, newNode );
}
return node;
}
QSGNode* QskScaleRenderer::updateTicksNode(
2023-11-28 12:36:47 +00:00
const QTransform& transform, QSGNode* node ) const
{
2023-11-28 12:36:47 +00:00
QskIntervalF backbone;
if ( m_data->flags & Backbone )
backbone = m_data->boundaries;
2023-11-28 12:36:47 +00:00
const auto orientation = qskIsHorizontal( m_data->edge )
? Qt::Horizontal : Qt::Vertical;
2023-11-28 12:36:47 +00:00
auto alignment = QskAxisScaleNode::Centered;
2020-12-05 14:09:31 +00:00
2023-11-28 12:36:47 +00:00
if ( !( m_data->flags & CenteredTickmarks ) )
{
switch( m_data->edge )
{
case Qt::LeftEdge:
case Qt::TopEdge:
alignment = QskAxisScaleNode::Leading;
break;
case Qt::BottomEdge:
case Qt::RightEdge:
alignment = QskAxisScaleNode::Trailing;
break;
}
}
auto axisNode = QskSGNode::ensureNode< QskAxisScaleNode >( node );
2023-04-04 07:05:16 +00:00
2023-11-28 12:36:47 +00:00
axisNode->setColor( m_data->tickColor );
axisNode->setAxis( orientation, m_data->position, transform );
axisNode->setTickGeometry( alignment, m_data->tickLength, m_data->tickWidth );
axisNode->setPixelAlignment( Qt::Horizontal | Qt::Vertical );
2020-12-05 14:09:31 +00:00
2023-11-28 12:36:47 +00:00
axisNode->update( m_data->tickmarks, backbone );
return axisNode;
}
2023-11-28 12:36:47 +00:00
QSGNode* QskScaleRenderer::updateLabelsNode( const QskSkinnable* skinnable,
const QTransform& transform, QSGNode* node ) const
{
2022-03-24 10:14:46 +00:00
const auto ticks = m_data->tickmarks.majorTicks();
if ( ticks.isEmpty() )
return nullptr;
if( node == nullptr )
node = new QSGNode;
2022-03-24 10:14:46 +00:00
const QFontMetricsF fm( m_data->font );
auto nextNode = node->firstChild();
2023-11-28 12:36:47 +00:00
QRectF lastRect; // to skip overlapping label
2021-04-19 07:28:19 +00:00
for ( auto tick : ticks )
{
const auto label = labelAt( tick );
2023-11-28 12:36:47 +00:00
const auto role = qskLabelNodeRole( label );
2023-11-28 12:36:47 +00:00
if ( nextNode && QskSGNode::nodeRole( nextNode ) != role )
nextNode = qskRemoveTraillingNodes( node, nextNode );
2023-11-28 12:36:47 +00:00
QSizeF size;
2023-11-28 12:36:47 +00:00
if ( label.canConvert< QString >() )
{
size = qskTextRenderSize( fm, label.toString() );
}
else if ( label.canConvert< QskGraphic >() )
{
const auto graphic = label.value< QskGraphic >();
if ( !graphic.isNull() )
{
2023-11-28 12:36:47 +00:00
size.rheight() = fm.height();
size.rwidth() = graphic.widthForHeight( size.height() );
}
2023-11-28 12:36:47 +00:00
}
2023-11-28 12:36:47 +00:00
if ( size.isEmpty() )
continue;
2023-11-28 12:36:47 +00:00
const auto rect = labelRect( transform, tick, size );
2023-11-28 12:36:47 +00:00
if ( !lastRect.isEmpty() && lastRect.intersects( rect ) )
{
/*
Label do overlap: in case it is the last tick we remove
the precessor - otherwise we simply skip this one
*/
2021-04-19 07:28:19 +00:00
2023-11-28 12:36:47 +00:00
if ( tick != ticks.last() )
continue; // skip this label
2023-11-28 12:36:47 +00:00
if ( auto obsoleteNode = nextNode
? nextNode->previousSibling() : node->lastChild() )
{
2023-11-28 12:36:47 +00:00
node->removeChildNode( obsoleteNode );
if ( obsoleteNode->flags() & QSGNode::OwnedByParent )
delete obsoleteNode;
}
}
2023-11-28 12:36:47 +00:00
nextNode = updateTickLabelNode( skinnable, nextNode, label, rect );
2023-11-28 12:36:47 +00:00
if ( nextNode)
{
lastRect = rect;
2023-11-28 12:36:47 +00:00
if ( nextNode->parent() != node )
{
2023-11-28 12:36:47 +00:00
QskSGNode::setNodeRole( nextNode, role );
node->appendChildNode( nextNode );
}
2023-11-28 12:36:47 +00:00
nextNode = nextNode->nextSibling();
}
}
qskRemoveTraillingNodes( node, nextNode );
return node;
}
QVariant QskScaleRenderer::labelAt( qreal pos ) const
{
return QString::number( pos, 'g' );
}
2023-11-28 12:36:47 +00:00
// should be cached
2021-02-01 09:22:54 +00:00
QSizeF QskScaleRenderer::boundingLabelSize() const
{
2023-11-28 12:36:47 +00:00
QSizeF boundingSize( 0.0, 0.0 );
2022-03-24 10:14:46 +00:00
const auto ticks = m_data->tickmarks.majorTicks();
if ( ticks.isEmpty() )
2023-11-28 12:36:47 +00:00
return boundingSize;
2022-03-24 10:14:46 +00:00
const QFontMetricsF fm( m_data->font );
2021-02-01 09:22:54 +00:00
const qreal h = fm.height();
for ( auto tick : ticks )
{
const auto label = labelAt( tick );
if ( label.isNull() )
continue;
if ( label.canConvert< QString >() )
{
2023-11-28 12:36:47 +00:00
boundingSize = boundingSize.expandedTo(
qskTextRenderSize( fm, label.toString() ) );
}
else if ( label.canConvert< QskGraphic >() )
{
const auto graphic = label.value< QskGraphic >();
if ( !graphic.isNull() )
{
2023-11-28 12:36:47 +00:00
const auto w = graphic.widthForHeight( h );
boundingSize.setWidth( qMax( boundingSize.width(), w ) );
}
}
2023-11-28 12:36:47 +00:00
}
return boundingSize;
}
QRectF QskScaleRenderer::labelRect(
const QTransform& transform, qreal tick, const QSizeF& labelSize ) const
{
const auto isHorizontal = qskIsHorizontal( m_data->edge );
auto offset = m_data->tickLength + m_data->spacing;
if ( m_data->flags & CenteredTickmarks )
offset -= 0.5 * m_data->tickLength;
const bool clampLabels = m_data->flags & ClampedLabels;
const qreal w = labelSize.width();
const qreal h = labelSize.height();
qreal x, y;
const ScaleMap map( isHorizontal, transform );
const auto tickPos = map.map( tick );
qreal min, max;
if ( clampLabels )
{
min = map.map( m_data->boundaries.lowerBound() );
max = map.map( m_data->boundaries.upperBound() );
}
if( isHorizontal )
{
x = tickPos - 0.5 * w;
if ( clampLabels )
x = qBound( min, x, max - w );
y = m_data->position + offset;
}
else
{
const auto tickPos = map.map( tick );
y = tickPos - 0.5 * h;
if ( clampLabels )
y = qBound( max, y, min - h );
x = m_data->position - offset - w;
}
2023-11-28 12:36:47 +00:00
return QRectF( x, y, w, h );
}
QSGNode* QskScaleRenderer::updateTickLabelNode( const QskSkinnable* skinnable,
QSGNode* node, const QVariant& label, const QRectF& rect ) const
{
if ( label.canConvert< QString >() )
{
return QskSkinlet::updateTextNode( skinnable, node,
rect, Qt::AlignCenter, label.toString(), m_data->font,
QskTextOptions(), m_data->textColors, Qsk::Normal );
}
if ( label.canConvert< QskGraphic >() )
{
const auto alignment = qskIsHorizontal( m_data->edge )
? ( Qt::AlignHCenter | Qt::AlignBottom )
: ( Qt::AlignRight | Qt::AlignVCenter );
return QskSkinlet::updateGraphicNode(
skinnable, node, label.value< QskGraphic >(),
m_data->colorFilter, rect, alignment );
}
2023-11-28 12:36:47 +00:00
return nullptr;
}
2023-11-28 12:36:47 +00:00
#include "moc_QskScaleRenderer.cpp"