From c8367c8a5e7b966c4518ab8f2015d0eabd031a20 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 26 Sep 2022 17:51:28 +0200 Subject: [PATCH] shapes playground example added --- playground/playground.pro | 6 +- playground/shapes/ShapeItem.cpp | 132 ++++++++++++++++++++++++++++++++ playground/shapes/ShapeItem.h | 36 +++++++++ playground/shapes/main.cpp | 46 +++++++++++ playground/shapes/shapes.pro | 8 ++ 5 files changed, 225 insertions(+), 3 deletions(-) create mode 100644 playground/shapes/ShapeItem.cpp create mode 100644 playground/shapes/ShapeItem.h create mode 100644 playground/shapes/main.cpp create mode 100644 playground/shapes/shapes.pro diff --git a/playground/playground.pro b/playground/playground.pro index fedb7191..1cdf0540 100644 --- a/playground/playground.pro +++ b/playground/playground.pro @@ -6,9 +6,9 @@ SUBDIRS += \ dialogbuttons \ invoker \ inputpanel \ - images - -SUBDIRS += shadows + images \ + shadows \ + shapes qtHaveModule(webengine) { diff --git a/playground/shapes/ShapeItem.cpp b/playground/shapes/ShapeItem.cpp new file mode 100644 index 00000000..767cd229 --- /dev/null +++ b/playground/shapes/ShapeItem.cpp @@ -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 +#include +#include + +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" diff --git a/playground/shapes/ShapeItem.h b/playground/shapes/ShapeItem.h new file mode 100644 index 00000000..6a99e325 --- /dev/null +++ b/playground/shapes/ShapeItem.h @@ -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 +#include +#include + +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; +}; diff --git a/playground/shapes/main.cpp b/playground/shapes/main.cpp new file mode 100644 index 00000000..81ba35d7 --- /dev/null +++ b/playground/shapes/main.cpp @@ -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 +#include + +#include +#include +#include + +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(); +} diff --git a/playground/shapes/shapes.pro b/playground/shapes/shapes.pro new file mode 100644 index 00000000..bbf1741a --- /dev/null +++ b/playground/shapes/shapes.pro @@ -0,0 +1,8 @@ +CONFIG += qskexample + +HEADERS += \ + ShapeItem.h \ + +SOURCES += \ + ShapeItem.cpp \ + main.cpp