Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://bugreports.qt.io/browse/QTBUG-77400>`_)
with ``QGraphicsDropShadowEffect``. The default shadow avoids it. If you use the
Qt effect, set ``ShadowEnabled`` to false. See :ref:`Node Shadows <node-shadows>`.

**Why is my graph slow to render?**

Check for:
Expand Down
51 changes: 50 additions & 1 deletion docs/guide/styling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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``
Expand All @@ -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 <https://bugreports.qt.io/browse/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
---------------

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/custom_painter/SimpleGraphModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions include/QtNodes/internal/NodeStyle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions resources/DefaultStyle.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"GradientColor3": [58, 58, 58],
"ShadowColor": [20, 20, 20],
"ShadowEnabled": true,
"PaintedShadowEnabled": true,
"FontColor" : "white",
"FontColorFaded" : "gray",
"ConnectionPointColor": [169, 169, 169],
Expand Down
37 changes: 37 additions & 0 deletions src/DefaultNodePainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/NodeGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/NodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading