qskinny/src/controls/QskListViewSkinlet.cpp

499 lines
15 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
2023-04-06 09:23:37 +02:00
* SPDX-License-Identifier: BSD-3-Clause
2017-07-21 18:21:34 +02:00
*****************************************************************************/
#include "QskListViewSkinlet.h"
2018-08-03 08:30:23 +02:00
#include "QskListView.h"
2017-07-21 18:21:34 +02:00
#include "QskColorFilter.h"
2018-07-19 14:10:48 +02:00
#include "QskGraphic.h"
#include "QskBoxHints.h"
2020-11-22 15:27:58 +01:00
#include "QskSGNode.h"
2021-12-23 18:15:54 +01:00
#include "QskSkinStateChanger.h"
#include "QskQuick.h"
2017-07-21 18:21:34 +02:00
2018-08-03 08:15:28 +02:00
#include <qmath.h>
2018-07-19 14:10:48 +02:00
#include <qsgnode.h>
#include <qtransform.h>
2017-07-21 18:21:34 +02:00
namespace
2017-07-21 18:21:34 +02:00
{
class ForegroundNode : public QSGNode
2017-07-21 18:21:34 +02:00
{
public:
void invalidate()
{
removeAllChildNodes();
m_columnCount = m_oldRowMin = m_oldRowMax = -1;
}
2017-07-21 18:21:34 +02:00
void rearrangeNodes( int rowMin, int rowMax, int columnCount )
{
const bool doReorder = ( columnCount == m_columnCount )
&& ( rowMin <= m_oldRowMax ) && ( rowMax >= m_oldRowMin );
2017-07-21 18:21:34 +02:00
if ( doReorder )
{
/*
We have nodes that will be at a different position in the
list of child nodes - however their content might be unchanged.
2017-07-21 18:21:34 +02:00
This is a common situation, when resizing or scrolling the list.
2017-07-21 18:21:34 +02:00
To avoid that the text/graphic nodes have to be updated we
try to rearrange the nodes, so that they are in increasing
row order for further processing.
2017-10-18 20:00:06 +02:00
To avoid delete/new calls we append the leading and prepend
the trailing nodes, so that they might be used later.
*/
2017-07-21 18:21:34 +02:00
if ( rowMin >= m_oldRowMin )
{
for ( int row = m_oldRowMin; row < rowMin; row++ )
{
for ( int col = 0; col < columnCount; col++ )
{
auto childNode = firstChild();
removeChildNode( childNode );
appendChildNode( childNode );
}
}
}
else
{
for ( int row = rowMin; row < m_oldRowMin; row++ )
{
for ( int col = 0; col < columnCount; col++ )
{
auto childNode = lastChild();
removeChildNode( childNode );
prependChildNode( childNode );
}
}
}
}
2017-07-21 18:21:34 +02:00
m_oldRowMin = rowMin;
m_oldRowMax = rowMax;
m_columnCount = columnCount;
}
2017-07-21 18:21:34 +02:00
private:
/*
When scrolling the majority of the child nodes are simply translated
while only few rows appear/disappear. To implement this in an efficient
way we store how the nodes were related to the rows in the previous update,
*/
2019-09-12 10:44:41 +02:00
int m_oldRowMin = -1;
int m_oldRowMax = -1;
int m_columnCount = -1;
};
class ListViewNode final : public QSGTransformNode
2019-09-12 10:44:41 +02:00
{
public:
inline ListViewNode()
{
m_backgroundNode.setFlag( QSGNode::OwnedByParent, false );
appendChildNode( &m_backgroundNode );
m_foregroundNode.setFlag( QSGNode::OwnedByParent, false );
appendChildNode( &m_foregroundNode );
}
void initialize( const QskListView* listView )
{
const auto scrollPos = listView->scrollPos();
setMatrix( QTransform::fromTranslate( -scrollPos.x(), -scrollPos.y() ) );
m_clipRect = listView->viewContentsRect();
m_rowHeight = listView->rowHeight();
m_rowMin = qFloor( scrollPos.y() / m_rowHeight );
const auto rowMax = ( scrollPos.y() + m_clipRect.height() ) / m_rowHeight;
m_rowMax = qFloor( rowMax - 10e-6 );
if ( m_rowMax >= listView->rowCount() )
m_rowMax = listView->rowCount() - 1;
}
QRectF clipRect() const { return m_clipRect; }
int rowMin() const { return m_rowMin; }
int rowMax() const { return m_rowMax; }
int rowCount() const { return m_rowMax - m_rowMin + 1; }
int rowHeight() const { return m_rowHeight; }
QSGNode* backgroundNode() { return &m_backgroundNode; }
ForegroundNode* foregroundNode() { return &m_foregroundNode; }
private:
// caching some calculations to speed things up
QRectF m_clipRect;
qreal m_rowHeight;
int m_rowMin, m_rowMax;
2017-07-21 18:21:34 +02:00
QSGNode m_backgroundNode;
ForegroundNode m_foregroundNode;
};
}
static inline ListViewNode* qskListViewNode( const QskListView* listView )
{
if ( auto node = const_cast< QSGNode* >( qskPaintNode( listView ) ) )
2017-07-21 18:21:34 +02:00
{
using namespace QskSGNode;
node = findChildNode( node, QskScrollViewSkinlet::ContentsRootRole );
if ( node )
{
node = node->firstChild();
if ( node )
{
Q_ASSERT( node->type() == QSGNode::TransformNodeType );
return static_cast< ListViewNode* >( node );
}
}
2017-07-21 18:21:34 +02:00
}
return nullptr;
}
2017-07-21 18:21:34 +02:00
static inline ListViewNode* qskListViewNode( const QskSkinnable* skinnable )
{
return qskListViewNode( static_cast< const QskListView* >( skinnable ) );
}
2017-07-21 18:21:34 +02:00
2018-08-03 08:15:28 +02:00
QskListViewSkinlet::QskListViewSkinlet( QskSkin* skin )
: Inherited( skin )
2017-07-21 18:21:34 +02:00
{
}
QskListViewSkinlet::~QskListViewSkinlet() = default;
QSGNode* QskListViewSkinlet::updateContentsNode(
const QskScrollView* scrollView, QSGNode* node ) const
{
const auto listView = static_cast< const QskListView* >( scrollView );
2017-07-21 18:21:34 +02:00
auto listViewNode = QskSGNode::ensureNode< ListViewNode >( node );
listViewNode->initialize( listView );
2017-07-21 18:21:34 +02:00
updateBackgroundNodes( listView, listViewNode->backgroundNode() );
updateForegroundNodes( listView, listViewNode->foregroundNode() );
2017-07-21 18:21:34 +02:00
return listViewNode;
}
void QskListViewSkinlet::updateBackgroundNodes(
const QskListView* listView, QSGNode* backgroundNode ) const
2017-07-21 18:21:34 +02:00
{
using Q = QskListView;
auto listViewNode = static_cast< const ListViewNode* >( backgroundNode->parent() );
2017-07-21 18:21:34 +02:00
auto rowNode = backgroundNode->firstChild();
2017-07-21 18:21:34 +02:00
for ( int row = listViewNode->rowMin(); row <= listViewNode->rowMax(); row++ )
2017-07-21 18:21:34 +02:00
{
QskSkinStateChanger stateChanger( listView );
stateChanger.setStates( sampleStates( listView, Q::Cell, row ), row );
const auto rect = sampleRect( listView, listView->contentsRect(), Q::Cell, row );
auto newNode = updateBoxNode( listView, rowNode, rect, Q::Cell );
if ( newNode )
{
if ( newNode->parent() != backgroundNode )
backgroundNode->appendChildNode( newNode );
else
rowNode = newNode->nextSibling();
2017-07-21 18:21:34 +02:00
}
}
QskSGNode::removeAllChildNodesFrom( backgroundNode, rowNode );
2017-07-21 18:21:34 +02:00
}
void QskListViewSkinlet::updateForegroundNodes(
const QskListView* listView, QSGNode* parentNode ) const
2017-07-21 18:21:34 +02:00
{
auto foregroundNode = static_cast< ForegroundNode* >( parentNode );
2017-07-21 18:21:34 +02:00
if ( listView->rowCount() <= 0 || listView->columnCount() <= 0 )
{
foregroundNode->invalidate();
2017-07-21 18:21:34 +02:00
return;
}
auto listViewNode = static_cast< const ListViewNode* >( parentNode->parent() );
2017-07-21 18:21:34 +02:00
const auto clipRect = listViewNode->clipRect();
2017-07-21 18:21:34 +02:00
const int rowMin = listViewNode->rowMin();
const int rowMax = listViewNode->rowMax();
2017-07-21 18:21:34 +02:00
foregroundNode->rearrangeNodes( rowMin, rowMax, listView->columnCount() );
2017-07-21 18:21:34 +02:00
#if 1
// should be optimized for visible columns only
const int colMin = 0;
const int colMax = listView->columnCount() - 1;
#endif
const auto margins = listView->paddingHint( QskListView::Cell );
2017-07-21 18:21:34 +02:00
updateVisibleForegroundNodes(
listView, foregroundNode, rowMin, rowMax, margins );
2017-07-21 18:21:34 +02:00
// finally putting the nodes into their position
auto node = foregroundNode->firstChild();
2017-07-21 18:21:34 +02:00
const auto rowHeight = listView->rowHeight();
auto y = clipRect.top() + rowMin * rowHeight;
2017-07-21 18:21:34 +02:00
for ( int row = rowMin; row <= rowMax; row++ )
{
qreal x = clipRect.left();
2017-07-21 18:21:34 +02:00
for ( int col = colMin; col <= colMax; col++ )
{
Q_ASSERT( node->type() == QSGNode::TransformNodeType );
auto transformNode = static_cast< QSGTransformNode* >( node );
transformNode->setMatrix(
QTransform::fromTranslate( x + margins.left(), y + margins.top() ) );
2017-07-21 18:21:34 +02:00
node = node->nextSibling();
x += listView->columnWidth( col );
}
2017-10-18 20:00:06 +02:00
2017-07-21 18:21:34 +02:00
y += rowHeight;
}
}
2017-07-30 14:30:50 +02:00
void QskListViewSkinlet::updateVisibleForegroundNodes(
const QskListView* listView, QSGNode* parentNode,
int rowMin, int rowMax, const QMarginsF& margins ) const
2017-07-21 18:21:34 +02:00
{
auto node = parentNode->firstChild();
2017-07-21 18:21:34 +02:00
for ( int row = rowMin; row <= rowMax; row++ )
2017-07-21 18:21:34 +02:00
{
const auto h = listView->rowHeight() - ( margins.top() + margins.bottom() );
2017-07-21 18:21:34 +02:00
for ( int col = 0; col < listView->columnCount(); col++ )
2017-07-21 18:21:34 +02:00
{
const auto w = listView->columnWidth( col ) - ( margins.left() + margins.right() );
2017-07-21 18:21:34 +02:00
node = updateForegroundNode( listView,
parentNode, static_cast< QSGTransformNode* >( node ),
row, col, QSizeF( w, h ) );
2017-07-21 18:21:34 +02:00
node = node->nextSibling();
2017-07-21 18:21:34 +02:00
}
}
QskSGNode::removeAllChildNodesFrom( parentNode, node );
2017-07-21 18:21:34 +02:00
}
QSGTransformNode* QskListViewSkinlet::updateForegroundNode(
const QskListView* listView, QSGNode* parentNode, QSGTransformNode* cellNode,
int row, int col, const QSizeF& size ) const
2017-07-21 18:21:34 +02:00
{
const QRectF cellRect( 0.0, 0.0, size.width(), size.height() );
/*
Text nodes already have a transform root node - to avoid inserting extra
transform nodes, the code below becomes a bit more complicated.
*/
QSGTransformNode* newCellNode = nullptr;
if ( cellNode && ( cellNode->type() == QSGNode::TransformNodeType ) )
{
QSGNode* oldNode = cellNode;
auto newNode = updateCellNode( listView, oldNode, cellRect, row, col );
if ( newNode )
{
if ( newNode->type() == QSGNode::TransformNodeType )
{
newCellNode = static_cast< QSGTransformNode* >( newNode );
}
else
{
newCellNode = new QSGTransformNode();
newCellNode->appendChildNode( newNode );
}
}
}
else
{
QSGNode* oldNode = cellNode ? cellNode->firstChild() : nullptr;
auto newNode = updateCellNode( listView, oldNode, cellRect, row, col );
if ( newNode )
{
if ( newNode->type() == QSGNode::TransformNodeType )
{
newCellNode = static_cast< QSGTransformNode* >( newNode );
}
else
{
if ( cellNode == nullptr )
{
newCellNode = new QSGTransformNode();
newCellNode->appendChildNode( newNode );
}
else
{
if ( newNode != oldNode )
{
delete cellNode->firstChild();
cellNode->appendChildNode( newNode );
newCellNode = cellNode;
}
}
}
}
}
if ( newCellNode == nullptr )
newCellNode = new QSGTransformNode();
if ( cellNode != newCellNode )
{
if ( cellNode )
{
parentNode->insertChildNodeAfter( newCellNode, cellNode );
delete cellNode;
}
else
{
parentNode->appendChildNode( newCellNode );
2017-07-21 18:21:34 +02:00
}
}
return newCellNode;
}
QSGNode* QskListViewSkinlet::updateCellNode( const QskListView* listView,
QSGNode* contentNode, const QRectF& rect, int row, int col ) const
{
using Q = QskListView;
2020-11-22 15:27:58 +01:00
using namespace QskSGNode;
2021-12-27 17:33:06 +01:00
QskSkinStateChanger stateChanger( listView );
stateChanger.setStates( sampleStates( listView, Q::Cell, row ), row );
2021-12-22 15:08:27 +01:00
2017-07-21 18:21:34 +02:00
QSGNode* newNode = nullptr;
#if 1
/*
Alignments, text options etc are user definable attributes and need
to be adjustable - at least individually for each column - from the
public API of QskListView TODO ...
*/
#endif
const auto alignment = listView->alignmentHint(
Q::Cell, Qt::AlignVCenter | Qt::AlignLeft );
2017-07-21 18:21:34 +02:00
2020-12-18 18:26:32 +01:00
const auto value = listView->valueAt( row, col );
2017-07-21 18:21:34 +02:00
if ( value.canConvert< QskGraphic >() )
{
if ( nodeRole( contentNode ) == GraphicRole )
newNode = contentNode;
const auto colorFilter = listView->effectiveGraphicFilter( Q::Graphic );
2017-07-21 18:21:34 +02:00
2018-08-03 08:15:28 +02:00
newNode = updateGraphicNode( listView, newNode,
value.value< QskGraphic >(), colorFilter, rect, alignment );
2017-07-21 18:21:34 +02:00
if ( newNode )
setNodeRole( newNode, GraphicRole );
}
else if ( value.canConvert< QString >() )
{
if ( nodeRole( contentNode ) == TextRole )
newNode = contentNode;
newNode = updateTextNode( listView, newNode, rect, alignment,
value.toString(), Q::Text );
2017-07-21 18:21:34 +02:00
if ( newNode )
setNodeRole( newNode, TextRole );
}
else
{
2020-11-21 20:36:47 +01:00
qWarning() << "QskListViewSkinlet: got unsupported QVariant type" << value.typeName();
2017-07-21 18:21:34 +02:00
}
return newNode;
}
QSizeF QskListViewSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& ) const
{
const auto listView = static_cast< const QskListView* >( skinnable );
qreal w = -1.0; // shouldn't we return something ???
if ( which != Qt::MaximumSize )
{
if ( listView->preferredWidthFromColumns() )
{
w = listView->scrollableSize().width();
w += listView->metric( QskScrollView::VerticalScrollBar | QskAspect::Size );
}
}
return QSizeF( w, -1.0 );
}
QskAspect::States QskListViewSkinlet::sampleStates( const QskSkinnable* skinnable,
QskAspect::Subcontrol subControl, int index ) const
{
using Q = QskListView;
if ( subControl == Q::Cell || subControl == Q::Text || subControl == Q::Graphic )
{
const auto listView = static_cast< const QskListView* >( skinnable );
return listView->rowStates( index );
}
return Inherited::sampleStates( skinnable, subControl, index );
}
2023-07-18 16:18:36 +02:00
QRectF QskListViewSkinlet::sampleRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl, int index ) const
{
using Q = QskListView;
2023-07-18 16:18:36 +02:00
const auto listView = static_cast< const QskListView* >( skinnable );
if ( subControl == Q::Cell )
{
auto node = qskListViewNode( listView );
const auto clipRect = node ? node->clipRect() : listView->viewContentsRect();
2023-07-18 16:18:36 +02:00
const auto w = clipRect.width();
const auto h = listView->rowHeight();
const auto x = clipRect.left() + listView->scrollPos().x();
const auto y = clipRect.top() + index * h;
2023-07-18 16:18:36 +02:00
return QRectF( x, y, w, h );
2023-07-18 16:18:36 +02:00
}
return Inherited::sampleRect( skinnable, contentsRect, subControl, index );
}
2017-07-21 18:21:34 +02:00
#include "moc_QskListViewSkinlet.cpp"