shapes playground example added

This commit is contained in:
Uwe Rathmann 2022-09-26 17:51:28 +02:00
parent a13dcf8428
commit c8367c8a5e
5 changed files with 225 additions and 3 deletions

View File

@ -6,9 +6,9 @@ SUBDIRS += \
dialogbuttons \
invoker \
inputpanel \
images
SUBDIRS += shadows
images \
shadows \
shapes
qtHaveModule(webengine) {

View File

@ -0,0 +1,132 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "ShapeItem.h"
#include <QskStrokeNode.h>
#include <QskShapeNode.h>
#include <QskSGNode.h>
ShapeItem::ShapeItem( QQuickItem* parent )
: QskControl( parent )
{
setMargins( 20 );
}
ShapeItem::~ShapeItem()
{
}
void ShapeItem::setPen( const QPen& pen )
{
if ( pen != m_pen )
{
m_pen = pen;
update();
}
}
QPen ShapeItem::pen() const
{
return m_pen;
}
void ShapeItem::setFillColor( const QColor& color )
{
if ( color != m_fillColor )
{
m_fillColor = color;
update();
}
}
QColor ShapeItem::fillColor() const
{
return m_fillColor;
}
void ShapeItem::setPath( const QPainterPath& path )
{
if ( path != m_path )
{
m_path = path;
update();
}
}
QPainterPath ShapeItem::path() const
{
return m_path;
}
void ShapeItem::updateNode( QSGNode* parentNode )
{
enum NodeRole
{
ShapeRole,
StrokeRole
};
const auto pathRect = m_path.controlPointRect();
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
on the fly. TODO ...
*/
const auto effectivePath = transform.map( m_path );
#endif
auto shapeNode = static_cast< QskShapeNode* >(
QskSGNode::findChildNode( parentNode, ShapeRole ) );
if ( pathRect.isEmpty() || rect.isEmpty() )
{
delete shapeNode;
}
else
{
if ( shapeNode == nullptr )
{
shapeNode = new QskShapeNode;
QskSGNode::setNodeRole( shapeNode, ShapeRole );
}
shapeNode->updateNode( effectivePath, m_fillColor );
if ( shapeNode->parent() != parentNode )
parentNode->prependChildNode( shapeNode );
}
auto strokeNode = static_cast< QskStrokeNode* >(
QskSGNode::findChildNode( parentNode, StrokeRole ) );
if ( pathRect.isEmpty() || rect.isEmpty() )
{
delete strokeNode;
}
else
{
if ( strokeNode == nullptr )
{
strokeNode = new QskStrokeNode;
QskSGNode::setNodeRole( strokeNode, StrokeRole );
}
strokeNode->updateNode( effectivePath, m_pen );
if ( strokeNode->parent() != parentNode )
parentNode->appendChildNode( strokeNode );
}
}
#include "moc_ShapeItem.cpp"

View File

@ -0,0 +1,36 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#pragma once
#include <QskControl.h>
#include <QPen>
#include <QPainterPath>
class ShapeItem : public QskControl
{
Q_OBJECT
public:
ShapeItem( QQuickItem* parent = nullptr );
~ShapeItem() override;
void setPen( const QPen& );
QPen pen() const;
void setFillColor( const QColor& );
QColor fillColor() const;
void setPath( const QPainterPath& );
QPainterPath path() const;
protected:
void updateNode( QSGNode* ) override;
private:
QPen m_pen;
QColor m_fillColor;
QPainterPath m_path;
};

View File

@ -0,0 +1,46 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "ShapeItem.h"
#include <QskObjectCounter.h>
#include <QskWindow.h>
#include <SkinnyShortcut.h>
#include <SkinnyShapeFactory.h>
#include <QGuiApplication>
namespace
{
QPainterPath path( SkinnyShapeFactory::Shape shape )
{
return SkinnyShapeFactory::shapePath( shape, QSizeF( 50, 50 ) );
}
}
int main( int argc, char* argv[] )
{
#ifdef ITEM_STATISTICS
QskObjectCounter counter( true );
#endif
QGuiApplication app( argc, argv );
SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts );
QskWindow window;
auto shapeItem = new ShapeItem();
shapeItem->setPath( path( SkinnyShapeFactory::Hexagon ) );
shapeItem->setPen( QPen( Qt::darkBlue, 20 ) );
shapeItem->setFillColor( Qt::darkYellow );
window.addItem( shapeItem );
window.resize( 600, 600 );
window.show();
return app.exec();
}

View File

@ -0,0 +1,8 @@
CONFIG += qskexample
HEADERS += \
ShapeItem.h \
SOURCES += \
ShapeItem.cpp \
main.cpp