From 19fd8e6e26065665a85ca8030acf4d871fbea854 Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 24 Apr 2017 12:24:58 +0100 Subject: [PATCH] WIP on clipping working --- utils/fgqcanvas/canvasitem.cpp | 32 +++++++++++++++++++++++++++++ utils/fgqcanvas/canvasitem.h | 7 +++++++ utils/fgqcanvas/fgcanvaselement.cpp | 2 +- utils/fgqcanvas/fgcanvaspath.cpp | 7 +++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/utils/fgqcanvas/canvasitem.cpp b/utils/fgqcanvas/canvasitem.cpp index fdf6bdd25..8fc6a2aba 100644 --- a/utils/fgqcanvas/canvasitem.cpp +++ b/utils/fgqcanvas/canvasitem.cpp @@ -18,6 +18,7 @@ #include "canvasitem.h" #include +#include class LocalTransform : public QQuickTransform { @@ -48,4 +49,35 @@ void CanvasItem::setTransform(const QMatrix4x4 &mat) m_localTransform->setTransform(mat); } +void CanvasItem::setGlobalClip(const QRectF &clip) +{ + m_hasClip = true; + update(); +} + +void CanvasItem::setScaleToFit(const QSizeF &windowSize) +{ + const double verticalScaling = windowSize.height() / implicitHeight(); + const double horizontalScaling = windowSize.width() / implicitWidth(); + const double usedScale = std::min(verticalScaling, horizontalScaling); + setScale(usedScale); +} + +QSGClipNode *CanvasItem::getClipNode() +{ + if (!m_hasClip) { + return nullptr; + } + + if (!m_clipNode) { + m_clipNode = new QSGClipNode(); + } + + // transform global rect to local + QRectF localRect(mapFromGlobal(m_globalClipRect.topLeft()), + mapFromGlobal(m_globalClipRect.bottomRight())); + m_clipNode->setClipRect(localRect); + return m_clipNode; +} + #include "canvasitem.moc" diff --git a/utils/fgqcanvas/canvasitem.h b/utils/fgqcanvas/canvasitem.h index ebf46ae76..f232765cb 100644 --- a/utils/fgqcanvas/canvasitem.h +++ b/utils/fgqcanvas/canvasitem.h @@ -21,6 +21,7 @@ #include class LocalTransform; +class QSGClipNode; class CanvasItem : public QQuickItem { @@ -33,8 +34,14 @@ signals: public slots: +protected: + QSGClipNode* getClipNode(); + private: LocalTransform* m_localTransform; + QRectF m_globalClipRect; + bool m_hasClip = false; + QSGClipNode* m_clipNode = nullptr; }; #endif // CANVASITEM_H diff --git a/utils/fgqcanvas/fgcanvaselement.cpp b/utils/fgqcanvas/fgcanvaselement.cpp index a120b8b8b..8c4520329 100644 --- a/utils/fgqcanvas/fgcanvaselement.cpp +++ b/utils/fgqcanvas/fgcanvaselement.cpp @@ -148,7 +148,7 @@ void FGCanvasElement::paint(FGCanvasPaintContext *context) const if (_hasClip) { - // clip is definef in the global coordinate system + // clip is defined in the global coordinate system #if 0 p->save(); p->setTransform(context->globalCoordinateTransform()); diff --git a/utils/fgqcanvas/fgcanvaspath.cpp b/utils/fgqcanvas/fgcanvaspath.cpp index 1831ec143..dcdf90f21 100644 --- a/utils/fgqcanvas/fgcanvaspath.cpp +++ b/utils/fgqcanvas/fgcanvaspath.cpp @@ -168,6 +168,13 @@ public: strokeGeom->setFlag(QSGNode::OwnsMaterial); } + QSGClipNode* clip = getClipNode(); + if (clip) { + if (fillGeom) clip->appendChildNode(fillGeom); + if (strokeGeom) clip->appendChildNode(strokeGeom); + return clip; + } + if (fillGeom && strokeGeom) { QSGNode* groupNode = new QSGNode; groupNode->appendChildNode(fillGeom);