code improved

This commit is contained in:
Uwe Rathmann 2022-09-27 15:06:44 +02:00
parent c8367c8a5e
commit f3acb55170
3 changed files with 40 additions and 32 deletions

View File

@ -65,68 +65,73 @@ void ShapeItem::updateNode( QSGNode* parentNode )
{ {
enum NodeRole enum NodeRole
{ {
ShapeRole, FillRole,
StrokeRole BorderRole
}; };
const auto pathRect = m_path.controlPointRect();
const auto rect = contentsRect(); const auto rect = contentsRect();
#if 1
QTransform transform;
transform.translate( rect.left(), rect.top() );
transform.scale( rect.width() / pathRect.width(),
rect.height() / pathRect.height() );
/* /*
The triangulators in the nodes are able to do transformations The triangulators in the nodes are able to do transformations
on the fly. TODO ... on the fly. TODO ...
*/ */
const auto effectivePath = transform.map( m_path ); const auto path = scaledPath( rect );
#endif
auto shapeNode = static_cast< QskShapeNode* >( auto fillNode = static_cast< QskShapeNode* >(
QskSGNode::findChildNode( parentNode, ShapeRole ) ); QskSGNode::findChildNode( parentNode, FillRole ) );
if ( pathRect.isEmpty() || rect.isEmpty() ) if ( path.isEmpty() || rect.isEmpty() )
{ {
delete shapeNode; delete fillNode;
} }
else else
{ {
if ( shapeNode == nullptr ) if ( fillNode == nullptr )
{ {
shapeNode = new QskShapeNode; fillNode = new QskShapeNode;
QskSGNode::setNodeRole( shapeNode, ShapeRole ); QskSGNode::setNodeRole( fillNode, FillRole );
} }
shapeNode->updateNode( effectivePath, m_fillColor ); fillNode->updateNode( path, m_fillColor );
if ( shapeNode->parent() != parentNode ) if ( fillNode->parent() != parentNode )
parentNode->prependChildNode( shapeNode ); parentNode->prependChildNode( fillNode );
} }
auto strokeNode = static_cast< QskStrokeNode* >( auto borderNode = static_cast< QskStrokeNode* >(
QskSGNode::findChildNode( parentNode, StrokeRole ) ); QskSGNode::findChildNode( parentNode, BorderRole ) );
if ( pathRect.isEmpty() || rect.isEmpty() ) if ( path.isEmpty() || rect.isEmpty() )
{ {
delete strokeNode; delete borderNode;
} }
else else
{ {
if ( strokeNode == nullptr ) if ( borderNode == nullptr )
{ {
strokeNode = new QskStrokeNode; borderNode = new QskStrokeNode;
QskSGNode::setNodeRole( strokeNode, StrokeRole ); QskSGNode::setNodeRole( borderNode, BorderRole );
} }
strokeNode->updateNode( effectivePath, m_pen ); borderNode->updateNode( path, m_pen );
if ( strokeNode->parent() != parentNode ) if ( borderNode->parent() != parentNode )
parentNode->appendChildNode( strokeNode ); parentNode->appendChildNode( borderNode );
} }
} }
QPainterPath ShapeItem::scaledPath( const QRectF& rect ) const
{
// does not center properly. TODO
const auto pw = 2 * m_pen.width();
const auto pathRect = m_path.controlPointRect();
const auto r = rect.adjusted( pw, pw, -pw, -pw );
auto transform = QTransform::fromTranslate( r.left(), r.top() );
transform.scale( r.width() / pathRect.width(), r.height() / pathRect.height() );
return transform.map( m_path );
}
#include "moc_ShapeItem.cpp" #include "moc_ShapeItem.cpp"

View File

@ -30,6 +30,8 @@ class ShapeItem : public QskControl
void updateNode( QSGNode* ) override; void updateNode( QSGNode* ) override;
private: private:
QPainterPath scaledPath( const QRectF& ) const;
QPen m_pen; QPen m_pen;
QColor m_fillColor; QColor m_fillColor;
QPainterPath m_path; QPainterPath m_path;

View File

@ -31,6 +31,7 @@ int main( int argc, char* argv[] )
SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts ); SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts );
QskWindow window; QskWindow window;
window.setColor( Qt::gray );
auto shapeItem = new ShapeItem(); auto shapeItem = new ShapeItem();