Skip to content

Commit de6f4f4

Browse files
committed
Add the widget embeddable option and implement it in the resizable_images example.
1 parent 5465ddc commit de6f4f4

File tree

9 files changed

+74
-22
lines changed

9 files changed

+74
-22
lines changed

examples/resizable_images/ImageShowModel.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ ImageShowModel::ImageShowModel()
2424
_label->installEventFilter(this);
2525
}
2626

27+
ImageShowModel::~ImageShowModel()
28+
{
29+
delete _label;
30+
}
31+
2732
unsigned int ImageShowModel::nPorts(PortType portType) const
2833
{
2934
unsigned int result = 1;

examples/resizable_images/ImageShowModel.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ImageShowModel : public NodeDelegateModel
2323
public:
2424
ImageShowModel();
2525

26-
~ImageShowModel() = default;
26+
virtual ~ImageShowModel();
2727

2828
public:
2929
QString caption() const override { return QString("Image Display"); }
@@ -41,6 +41,8 @@ class ImageShowModel : public NodeDelegateModel
4141

4242
void setInData(std::shared_ptr<NodeData> nodeData, PortIndex const port) override;
4343

44+
bool widgetEmbeddable() const override { return false; }
45+
4446
QWidget *embeddedWidget() override { return _label; }
4547

4648
bool resizable() const override { return true; }

examples/resizable_images/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ int main(int argc, char *argv[])
3838

3939
GraphicsView view(&scene);
4040

41+
QObject::connect(&scene,
42+
&DataFlowGraphicsScene::nodeDoubleClicked,
43+
&dataFlowGraphModel,
44+
[&dataFlowGraphModel](QtNodes::NodeId nodeId) {
45+
QString name = dataFlowGraphModel
46+
.nodeData(nodeId, QtNodes::NodeRole::Caption)
47+
.value<QString>();
48+
49+
bool isEmbeded = dataFlowGraphModel
50+
.nodeData(nodeId, QtNodes::NodeRole::WidgetEmbeddable)
51+
.value<bool>();
52+
auto w = dataFlowGraphModel.nodeData(nodeId, QtNodes::NodeRole::Widget)
53+
.value<QWidget *>();
54+
55+
if (!isEmbeded && w) {
56+
w->setWindowTitle(name + "_" + QString::number(nodeId));
57+
w->show();
58+
}
59+
});
60+
4161
view.setWindowTitle("Data Flow: Resizable Images");
4262
view.resize(800, 600);
4363
// Center window.

include/QtNodes/internal/Definitions.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#pragma once
1+
#pragma once
22

33
#include "Export.hpp"
44

@@ -22,16 +22,17 @@ Q_NAMESPACE_EXPORT(NODE_EDITOR_PUBLIC)
2222
* Constants used for fetching QVariant data from GraphModel.
2323
*/
2424
enum class NodeRole {
25-
Type = 0, ///< Type of the current node, usually a string.
26-
Position = 1, ///< `QPointF` positon of the node on the scene.
27-
Size = 2, ///< `QSize` for resizable nodes.
28-
CaptionVisible = 3, ///< `bool` for caption visibility.
29-
Caption = 4, ///< `QString` for node caption.
30-
Style = 5, ///< Custom NodeStyle as QJsonDocument
31-
InternalData = 6, ///< Node-stecific user data as QJsonObject
32-
InPortCount = 7, ///< `unsigned int`
33-
OutPortCount = 9, ///< `unsigned int`
34-
Widget = 10, ///< Optional `QWidget*` or `nullptr`
25+
Type = 0, ///< Type of the current node, usually a string.
26+
Position, ///< `QPointF` positon of the node on the scene.
27+
Size, ///< `QSize` for resizable nodes.
28+
CaptionVisible, ///< `bool` for caption visibility.
29+
Caption, ///< `QString` for node caption.
30+
Style, ///< Custom NodeStyle as QJsonDocument
31+
InternalData, ///< Node-stecific user data as QJsonObject
32+
InPortCount, ///< `unsigned int`
33+
OutPortCount, ///< `unsigned int`
34+
WidgetEmbeddable, ///< `bool` for widget embeddability
35+
Widget, ///< Optional `QWidget*` or `nullptr`
3536
};
3637
Q_ENUM_NS(NodeRole)
3738

include/QtNodes/internal/NodeDelegateModel.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class StyleCollection;
2020
* AbstractGraphModel.
2121
* This class is the same what has been called NodeDataModel before v3.
2222
*/
23-
class NODE_EDITOR_PUBLIC NodeDelegateModel : public QObject, public Serializable
23+
class NODE_EDITOR_PUBLIC NodeDelegateModel
24+
: public QObject
25+
, public Serializable
2426
{
2527
Q_OBJECT
2628

@@ -78,6 +80,8 @@ class NODE_EDITOR_PUBLIC NodeDelegateModel : public QObject, public Serializable
7880
*/
7981
virtual QWidget *embeddedWidget() = 0;
8082

83+
virtual bool widgetEmbeddable() const { return true; }
84+
8185
virtual bool resizable() const { return false; }
8286

8387
public Q_SLOTS:

src/DataFlowGraphModel.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ QVariant DataFlowGraphModel::nodeData(NodeId nodeId, NodeRole role) const
203203
break;
204204

205205
case NodeRole::CaptionVisible:
206-
result = model->captionVisible();
206+
result = model->widgetEmbeddable() ? model->captionVisible() : true;
207207
break;
208208

209209
case NodeRole::Caption:
@@ -232,6 +232,10 @@ QVariant DataFlowGraphModel::nodeData(NodeId nodeId, NodeRole role) const
232232
result = model->nPorts(PortType::Out);
233233
break;
234234

235+
case NodeRole::WidgetEmbeddable:
236+
result = model->widgetEmbeddable();
237+
break;
238+
235239
case NodeRole::Widget: {
236240
auto w = model->embeddedWidget();
237241
result = QVariant::fromValue(w);
@@ -245,7 +249,7 @@ NodeFlags DataFlowGraphModel::nodeFlags(NodeId nodeId) const
245249
{
246250
auto it = _models.find(nodeId);
247251

248-
if (it != _models.end() && it->second->resizable())
252+
if (it != _models.end() && it->second->widgetEmbeddable() && it->second->resizable())
249253
return NodeFlag::Resizable;
250254

251255
return NodeFlag::NoFlags;

src/DefaultHorizontalNodeGeometry.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ void DefaultHorizontalNodeGeometry::recomputeSize(NodeId const nodeId) const
3232
{
3333
unsigned int height = maxVerticalPortsExtent(nodeId);
3434

35-
if (auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget)) {
35+
bool isEmbeded = _graphModel.nodeData(nodeId, NodeRole::WidgetEmbeddable).value<bool>();
36+
auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget);
37+
38+
if (isEmbeded && w) {
3639
height = std::max(height, static_cast<unsigned int>(w->height()));
3740
}
3841

@@ -48,7 +51,7 @@ void DefaultHorizontalNodeGeometry::recomputeSize(NodeId const nodeId) const
4851

4952
unsigned int width = inPortWidth + outPortWidth + 4 * _portSpasing;
5053

51-
if (auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget)) {
54+
if (isEmbeded && w) {
5255
width += w->width();
5356
}
5457

@@ -150,7 +153,10 @@ QPointF DefaultHorizontalNodeGeometry::widgetPosition(NodeId const nodeId) const
150153

151154
unsigned int captionHeight = captionRect(nodeId).height();
152155

153-
if (auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget)) {
156+
bool isEmbeded = _graphModel.nodeData(nodeId, NodeRole::WidgetEmbeddable).value<bool>();
157+
auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget);
158+
159+
if (isEmbeded && w) {
154160
// If the widget wants to use as much vertical space as possible,
155161
// place it immediately after the caption.
156162
if (w->sizePolicy().verticalPolicy() & QSizePolicy::ExpandFlag) {

src/DefaultVerticalNodeGeometry.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ void DefaultVerticalNodeGeometry::recomputeSize(NodeId const nodeId) const
3232
{
3333
unsigned int height = _portSpasing; // maxHorizontalPortsExtent(nodeId);
3434

35-
if (auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget)) {
35+
bool isEmbeded = _graphModel.nodeData(nodeId, NodeRole::WidgetEmbeddable).value<bool>();
36+
auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget);
37+
38+
if (isEmbeded && w) {
3639
height = std::max(height, static_cast<unsigned int>(w->height()));
3740
}
3841

@@ -64,7 +67,7 @@ void DefaultVerticalNodeGeometry::recomputeSize(NodeId const nodeId) const
6467

6568
unsigned int width = std::max(totalInPortsWidth, totalOutPortsWidth);
6669

67-
if (auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget)) {
70+
if (isEmbeded && w) {
6871
width = std::max(width, static_cast<unsigned int>(w->width()));
6972
}
7073

@@ -177,7 +180,10 @@ QPointF DefaultVerticalNodeGeometry::widgetPosition(NodeId const nodeId) const
177180

178181
unsigned int captionHeight = captionRect(nodeId).height();
179182

180-
if (auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget)) {
183+
bool isEmbeded = _graphModel.nodeData(nodeId, NodeRole::WidgetEmbeddable).value<bool>();
184+
auto w = _graphModel.nodeData<QWidget *>(nodeId, NodeRole::Widget);
185+
186+
if (isEmbeded && w) {
181187
// If the widget wants to use as much vertical space as possible,
182188
// place it immediately after the caption.
183189
if (w->sizePolicy().verticalPolicy() & QSizePolicy::ExpandFlag) {

src/NodeGraphicsObject.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ void NodeGraphicsObject::embedQWidget()
8181
AbstractNodeGeometry &geometry = nodeScene()->nodeGeometry();
8282
geometry.recomputeSize(_nodeId);
8383

84+
if (!_graphModel.nodeData(_nodeId, NodeRole::WidgetEmbeddable).value<bool>())
85+
return;
86+
8487
if (auto w = _graphModel.nodeData(_nodeId, NodeRole::Widget).value<QWidget *>()) {
8588
_proxyWidget = new QGraphicsProxyWidget(this);
8689

@@ -245,7 +248,8 @@ void NodeGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
245248
setSelected(true);
246249
}
247250

248-
if (_nodeState.resizing()) {
251+
if (_nodeState.resizing()
252+
&& _graphModel.nodeData(_nodeId, NodeRole::WidgetEmbeddable).value<bool>()) {
249253
auto diff = event->pos() - event->lastPos();
250254

251255
if (auto w = _graphModel.nodeData<QWidget *>(_nodeId, NodeRole::Widget)) {

0 commit comments

Comments
 (0)