diff --git a/docs/faq.rst b/docs/faq.rst index 01c37570f..929a26867 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -156,6 +156,12 @@ consider: - Implementing virtualization (only create graphics for visible nodes) - Disabling shadows and complex styles +**Why is a large node cut off when I zoom in?** + +This is a Qt bug (`QTBUG-77400 `_) +with ``QGraphicsDropShadowEffect``. The default shadow avoids it. If you use the +Qt effect, set ``ShadowEnabled`` to false. See :ref:`Node Shadows `. + **Why is my graph slow to render?** Check for: diff --git a/docs/guide/styling.rst b/docs/guide/styling.rst index 344cb25d0..f641e826b 100644 --- a/docs/guide/styling.rst +++ b/docs/guide/styling.rst @@ -40,6 +40,7 @@ Apply styles to all new objects: "GradientColor3": [45, 45, 45], "ShadowColor": [20, 20, 20], "ShadowEnabled": true, + "PaintedShadowEnabled": true, "FontColor": "white", "PenWidth": 2.0, "Opacity": 0.9 @@ -91,6 +92,7 @@ Controls node appearance: "GradientColor3": [58, 58, 58], "ShadowColor": [20, 20, 20], "ShadowEnabled": true, + "PaintedShadowEnabled": true, "FontColor": "white", "FontColorFaded": "gray", "ConnectionPointColor": [169, 169, 169], @@ -118,7 +120,9 @@ Controls node appearance: * - ``GradientColor0-3`` - Background gradient stops * - ``ShadowEnabled`` - - Drop shadow on/off + - Shadow on or off + * - ``PaintedShadowEnabled`` + - Draw the shadow in the default painter, not with a graphics effect; defaults to true. See `Node Shadows`_ * - ``ConnectionPointColor`` - Empty port circles * - ``FilledConnectionPointColor`` @@ -128,6 +132,50 @@ Controls node appearance: * - ``WarningColor`` - Validation warning indicator + +.. _node-shadows: + +Node Shadows +~~~~~~~~~~~~ + +Two style options control the node shadow: + +* ``ShadowEnabled`` turns the shadow on or off. +* ``PaintedShadowEnabled`` draws the shadow in ``DefaultNodePainter`` (default) + instead of using ``QGraphicsDropShadowEffect``. + +The default shadow is a stack of translucent rounded rectangles under the node. +It looks close to the Qt effect with offset 4 and blur radius 20. It exists +because of a Qt bug: nodes are cached as pixmaps, and together with +``QGraphicsDropShadowEffect`` Qt crops a node that is larger than the view when +you zoom in (`QTBUG-77400 `_, not +fixed). The default shadow uses no graphics effect, so large nodes are drawn +completely. + +It is not a performance feature. On macOS (Apple M1, Qt 6.12) the repaint time was about the +same as with the Qt effect, and higher with many overlapping nodes. The result +can differ with other Qt versions and hardware. Measure your own graphs before +you choose. + +.. code-block:: cpp + + NodeStyle style = StyleCollection::nodeStyle(); + style.PaintedShadowEnabled = false; // use QGraphicsDropShadowEffect + style.ShadowEnabled = false; // or: no shadow at all + StyleCollection::setNodeStyle(style); + +In a graph model, return ``style.toJson().toVariantMap()`` for +``NodeRole::Style``. Limits of the default shadow: + +* It is not pixel-identical to Qt's blur. +* A custom painter must draw its own shadow, or its nodes have none. To keep + the Qt effect, set ``PaintedShadowEnabled`` to false, as ``examples/custom_painter`` + does. +* A style JSON without the ``PaintedShadowEnabled`` key keeps the Qt effect. +* A custom geometry must include the shadow in ``boundingRect()``: it reaches 11 + logical pixels above and left of the node body, and 19 below and right. Both + default geometries have enough margin. + ConnectionStyle --------------- @@ -277,6 +325,7 @@ Complete style file (``my_style.json``): "GradientColor2": [40, 40, 45], "GradientColor3": [35, 35, 40], "ShadowEnabled": true, + "PaintedShadowEnabled": true, "FontColor": [220, 220, 220], "PenWidth": 1.5, "Opacity": 0.95 diff --git a/examples/custom_painter/SimpleGraphModel.cpp b/examples/custom_painter/SimpleGraphModel.cpp index 760cea69c..c6baba925 100644 --- a/examples/custom_painter/SimpleGraphModel.cpp +++ b/examples/custom_painter/SimpleGraphModel.cpp @@ -101,6 +101,8 @@ QVariant SimpleGraphModel::nodeData(NodeId nodeId, NodeRole role) const case NodeRole::Style: { auto style = StyleCollection::nodeStyle(); + // CustomNodePainter draws no shadow, so keep the Qt graphics effect. + style.PaintedShadowEnabled = false; result = style.toJson().toVariantMap(); } break; diff --git a/include/QtNodes/internal/NodeStyle.hpp b/include/QtNodes/internal/NodeStyle.hpp index 01ecd2ea1..16b3e5845 100644 --- a/include/QtNodes/internal/NodeStyle.hpp +++ b/include/QtNodes/internal/NodeStyle.hpp @@ -61,6 +61,9 @@ class NODE_EDITOR_PUBLIC NodeStyle : public Style QColor GradientColor3; QColor ShadowColor; bool ShadowEnabled; + /// Draw an approximate shadow with DefaultNodePainter instead of a graphics effect. + /// Requires ShadowEnabled; custom painters are responsible for their own shadow. + bool PaintedShadowEnabled{false}; QColor FontColor; QColor FontColorFaded; diff --git a/resources/DefaultStyle.json b/resources/DefaultStyle.json index 2df69bfd2..f0e4d3399 100644 --- a/resources/DefaultStyle.json +++ b/resources/DefaultStyle.json @@ -13,6 +13,7 @@ "GradientColor3": [58, 58, 58], "ShadowColor": [20, 20, 20], "ShadowEnabled": true, + "PaintedShadowEnabled": true, "FontColor" : "white", "FontColorFaded" : "gray", "ConnectionPointColor": [169, 169, 169], diff --git a/src/DefaultNodePainter.cpp b/src/DefaultNodePainter.cpp index 2d3dae9ba..f62a5bcab 100644 --- a/src/DefaultNodePainter.cpp +++ b/src/DefaultNodePainter.cpp @@ -44,6 +44,40 @@ void DefaultNodePainter::paint(QPainter *painter, NodeGraphicsObject &ngo) const drawProgressValue(painter, ngo); } +static void drawPaintedShadow(QPainter *painter, + QRectF const &boundary, + double radius, + QColor const &shadowColor) +{ + // Fake a soft shadow without a graphics effect. Draw the node shape many times, + // from the largest and faintest layer to the smallest. The translucent layers add up, + // so the shadow is dark near the node and fades out. The node is drawn on top afterwards. + // The numbers are fitted to QGraphicsDropShadowEffect with offset 4 and blur radius 20. + // The halo must fit into the margins reserved by default geometries (20 px). + int const outerSpread = 15; // the largest layer is 15 px bigger than the shape + int const innerSpread = -4; // the smallest layer is 4 px smaller + double const shadowOffset = 4.0; // the shadow is shifted right and down + double const innerLayerAlpha = 0.32; // opacity of the smallest layer, 1.0 = ShadowColor + int const layerCount = outerSpread - innerSpread + 1; + + painter->save(); + painter->setRenderHint(QPainter::Antialiasing, true); + painter->setPen(Qt::NoPen); + + QRectF const shadowBody = boundary.translated(shadowOffset, shadowOffset); + for (int spread = outerSpread; spread >= innerSpread; --spread) { + double const t = double(outerSpread + 1 - spread) / layerCount; + QColor shade = shadowColor; + shade.setAlphaF(shade.alphaF() * innerLayerAlpha * t * t * t); + painter->setBrush(shade); + double const layerRadius = std::max(0.0, radius + spread); + painter->drawRoundedRect(shadowBody.adjusted(-spread, -spread, spread, spread), + layerRadius, + layerRadius); + } + painter->restore(); +} + void DefaultNodePainter::drawNodeRect(QPainter *painter, NodeGraphicsObject &ngo) const { AbstractGraphModel &model = ngo.graphModel(); @@ -104,6 +138,9 @@ void DefaultNodePainter::drawNodeRect(QPainter *painter, NodeGraphicsObject &ngo double const radius = 3.0; + if (nodeStyle.ShadowEnabled && nodeStyle.PaintedShadowEnabled) + drawPaintedShadow(painter, boundary, radius, nodeStyle.ShadowColor); + painter->drawRoundedRect(boundary, radius, radius); } diff --git a/src/NodeGraphicsObject.cpp b/src/NodeGraphicsObject.cpp index 702b01c4f..e174f67a6 100644 --- a/src/NodeGraphicsObject.cpp +++ b/src/NodeGraphicsObject.cpp @@ -42,7 +42,8 @@ NodeGraphicsObject::NodeGraphicsObject(BasicGraphicsScene &scene, NodeId nodeId) NodeStyle nodeStyle(nodeStyleJson); - if (nodeStyle.ShadowEnabled) { + // The painted shadow is drawn by DefaultNodePainter. + if (nodeStyle.ShadowEnabled && !nodeStyle.PaintedShadowEnabled) { auto effect = new QGraphicsDropShadowEffect; effect->setOffset(4, 4); effect->setBlurRadius(20); diff --git a/src/NodeStyle.cpp b/src/NodeStyle.cpp index 0cfacf51b..9cc0ba3e0 100644 --- a/src/NodeStyle.cpp +++ b/src/NodeStyle.cpp @@ -120,6 +120,7 @@ void NodeStyle::loadJson(QJsonObject const &json) NODE_STYLE_READ_COLOR(obj, GradientColor3); NODE_STYLE_READ_COLOR(obj, ShadowColor); NODE_STYLE_READ_BOOL(obj, ShadowEnabled); + NODE_STYLE_READ_BOOL(obj, PaintedShadowEnabled); NODE_STYLE_READ_COLOR(obj, FontColor); NODE_STYLE_READ_COLOR(obj, FontColorFaded); NODE_STYLE_READ_COLOR(obj, ConnectionPointColor); @@ -146,6 +147,7 @@ QJsonObject NodeStyle::toJson() const NODE_STYLE_WRITE_COLOR(obj, GradientColor3); NODE_STYLE_WRITE_COLOR(obj, ShadowColor); NODE_STYLE_WRITE_BOOL(obj, ShadowEnabled); + NODE_STYLE_WRITE_BOOL(obj, PaintedShadowEnabled); NODE_STYLE_WRITE_COLOR(obj, FontColor); NODE_STYLE_WRITE_COLOR(obj, FontColorFaded); NODE_STYLE_WRITE_COLOR(obj, ConnectionPointColor); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d6ec99d05..319351db8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable(test_nodes src/TestDataFlow.cpp src/TestNodeValidation.cpp src/TestCustomPainters.cpp + src/TestPaintedNodeShadows.cpp src/TestCopyPaste.cpp src/TestZoomFeatures.cpp src/TestLoopDetection.cpp diff --git a/test/src/TestPaintedNodeShadows.cpp b/test/src/TestPaintedNodeShadows.cpp new file mode 100644 index 000000000..d41969af0 --- /dev/null +++ b/test/src/TestPaintedNodeShadows.cpp @@ -0,0 +1,200 @@ +#include "ApplicationSetup.hpp" +#include "TestGraphModel.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +TEST_CASE("Painted shadows are on by default and a style JSON without the key keeps the " + "graphics effect", + "[graphics][shadows]") +{ + auto app = applicationSetup(); + + QtNodes::NodeStyle legacyStyle; + legacyStyle.ShadowEnabled = true; + CHECK(legacyStyle.PaintedShadowEnabled); + + auto legacyJson = legacyStyle.toJson(); + QJsonObject legacyNodeJson = legacyJson.value("NodeStyle").toObject(); + legacyNodeJson.remove("PaintedShadowEnabled"); + legacyJson["NodeStyle"] = legacyNodeJson; + CHECK_FALSE(QtNodes::NodeStyle(legacyJson).PaintedShadowEnabled); + + QtNodes::NodeStyle paintedStyle; + paintedStyle.ShadowEnabled = true; + paintedStyle.PaintedShadowEnabled = true; + CHECK(QtNodes::NodeStyle(paintedStyle.toJson()).PaintedShadowEnabled); + + paintedStyle.loadJson(legacyJson); + CHECK_FALSE(paintedStyle.PaintedShadowEnabled); +} + +TEST_CASE("The graphics effect exists only if the shadow is on and the painted shadow is off", + "[graphics][shadows]") +{ + bool const shadowEnabled = GENERATE(false, true); + bool const paintedShadowEnabled = GENERATE(false, true); + CAPTURE(shadowEnabled, paintedShadowEnabled); + auto app = applicationSetup(); + + QtNodes::NodeStyle style; + style.ShadowEnabled = shadowEnabled; + style.PaintedShadowEnabled = paintedShadowEnabled; + + TestGraphModel model; + auto const id = model.addNode("shadow"); + // Match DataFlowGraphModel and the documented NodeRole::Style representation. + REQUIRE(model.setNodeData(id, QtNodes::NodeRole::Style, style.toJson().toVariantMap())); + + QtNodes::BasicGraphicsScene scene(model); + auto *node = scene.nodeGraphicsObject(id); + REQUIRE(node != nullptr); + CHECK((node->graphicsEffect() != nullptr) == (shadowEnabled && !paintedShadowEnabled)); +} + +TEST_CASE("The painted shadow footprint fits the node margins in both orientations", + "[graphics][shadows]") +{ + bool const vertical = GENERATE(false, true); + CAPTURE(vertical); + auto app = applicationSetup(); + + TestGraphModel model; + auto const id = model.addNode("painted"); + + QtNodes::BasicGraphicsScene scene(model); + scene.setOrientation(vertical ? Qt::Vertical : Qt::Horizontal); + + QSize const size = scene.nodeGeometry().size(id); + REQUIRE(size.width() > 0); + REQUIRE(size.height() > 0); + + // The shadow reaches 11 px above and left, and 19 px below and right of the node body. + QRectF const shadowFootprint(-11, -11, size.width() + 30, size.height() + 30); + CHECK(scene.nodeGeometry().boundingRect(id).contains(shadowFootprint)); +} + +TEST_CASE( + "The painted shadow draws only inside the node margins and only when the shadow is on", + "[graphics][shadows]") +{ + bool const vertical = GENERATE(false, true); + bool const shadowEnabled = GENERATE(false, true); + CAPTURE(vertical, shadowEnabled); + auto app = applicationSetup(); + + TestGraphModel model; + auto const id = model.addNode("painted"); + + QtNodes::NodeStyle style; + style.ShadowEnabled = shadowEnabled; + style.PaintedShadowEnabled = true; + style.ShadowColor = QColor(0, 0, 0); + REQUIRE(model.setNodeData(id, QtNodes::NodeRole::Style, style.toJson().toVariantMap())); + + QtNodes::BasicGraphicsScene scene(model); + scene.setOrientation(vertical ? Qt::Vertical : Qt::Horizontal); + auto *node = scene.nodeGraphicsObject(id); + REQUIRE(node != nullptr); + + QSize const size = scene.nodeGeometry().size(id); + REQUIRE(size.width() > 0); + REQUIRE(size.height() > 0); + + // The default horizontal and vertical geometries reserve 20 px per side. + QImage image(size.width() + 60, size.height() + 60, QImage::Format_ARGB32_Premultiplied); + image.fill(Qt::transparent); + QPainter painter(&image); + painter.translate(25, 25); + QTransform const transform = painter.transform(); + auto const renderHints = painter.renderHints(); + QtNodes::DefaultNodePainter nodePainter; + nodePainter.drawNodeRect(&painter, *node); + CHECK(painter.transform() == transform); + CHECK(painter.renderHints() == renderHints); + CHECK(painter.pen().style() != Qt::NoPen); + CHECK(painter.brush().style() == Qt::LinearGradientPattern); + painter.end(); + + int const midX = 25 + size.width() / 2; + CHECK((qAlpha(image.pixel(midX, 25 + size.height() + 6)) > 0) == shadowEnabled); + CHECK(qAlpha(image.pixel(midX, 25 + size.height() + 19)) == 0); + + QRectF const bounds = scene.nodeGeometry().boundingRect(id).translated(25, 25); + unsigned int pixelsOutsideBounds = 0; + for (int y = 0; y < image.height(); ++y) { + for (int x = 0; x < image.width(); ++x) { + if (qAlpha(image.pixel(x, y)) != 0 && !bounds.contains(QPointF(x, y))) { + ++pixelsOutsideBounds; + } + } + } + CHECK(pixelsOutsideBounds == 0); +} + +namespace { +// Grabs a view that is zoomed in on a node. The node is larger than the view. +// Returns the grabbed image and the part of the view that shows the node body. +QImage grabZoomedNode(bool shadowEnabled, bool paintedShadowEnabled, QRect &bodyInView) +{ + QtNodes::NodeStyle style; + style.ShadowEnabled = shadowEnabled; + style.PaintedShadowEnabled = paintedShadowEnabled; + + TestGraphModel model; + auto const id = model.addNode("zoom"); + REQUIRE(model.setNodeData(id, QtNodes::NodeRole::Style, style.toJson().toVariantMap())); + + QtNodes::BasicGraphicsScene scene(model); + QtNodes::GraphicsView view(&scene); + view.resize(400, 300); + view.show(); + + QSize const size = scene.nodeGeometry().size(id); + view.resetTransform(); + view.scale(8.0, 8.0); + view.centerOn(QPointF(size.width() / 2.0, size.height() / 2.0)); + QApplication::processEvents(); + + QRect const nodeInView = view.mapFromScene(QRectF(QPointF(0, 0), size)).boundingRect(); + // Stay a few pixels away from the node edge, where the shadow and the border are. + bodyInView = nodeInView.adjusted(4, 4, -4, -4).intersected(view.viewport()->rect()); + return view.grab().toImage(); +} +} // namespace + +TEST_CASE("A node larger than the view is drawn completely when the view is zoomed in", + "[graphics][shadows]") +{ + auto app = applicationSetup(); + + QRect body; + QRect bodyWithoutShadow; + QImage const withoutShadow = grabZoomedNode(false, false, bodyWithoutShadow); + QImage const withShadow = grabZoomedNode(true, true, body); + + REQUIRE(body == bodyWithoutShadow); + // The node must fill most of the view, otherwise the test does not prove anything. + REQUIRE(body.width() * body.height() > 100 * 100); + + unsigned int differentPixels = 0; + for (int y = body.top(); y <= body.bottom(); ++y) { + for (int x = body.left(); x <= body.right(); ++x) { + if (withShadow.pixel(x, y) != withoutShadow.pixel(x, y)) { + ++differentPixels; + } + } + } + CHECK(differentPixels == 0); +}