Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embed/dembed widget posibility #216

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion include/nodes/internal/FlowScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private Q_SLOTS:

void sendConnectionCreatedToNodes(Connection const& c);
void sendConnectionDeletedToNodes(Connection const& c);

void sceneContextMenuEvent(Node &node, const QPointF &pos);
};

Node*
Expand Down
5 changes: 5 additions & 0 deletions include/nodes/internal/NodeDataModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class NODE_EDITOR_PUBLIC NodeDataModel
virtual
NodePainterDelegate* painterDelegate() const { return nullptr; }

bool wembed() const;
void setWembed(bool wembed);

public Q_SLOTS:

virtual void
Expand Down Expand Up @@ -162,6 +165,8 @@ public Q_SLOTS:

void embeddedWidgetSizeUpdated();

protected:
bool m_wembed;
private:

NodeStyle _nodeStyle;
Expand Down
8 changes: 4 additions & 4 deletions include/nodes/internal/NodeGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class NodeGraphicsObject : public QGraphicsObject
void
lock(bool locked);

void
embedQWidget(bool embed=true);

protected:
void
paint(QPainter* painter,
Expand Down Expand Up @@ -87,10 +90,6 @@ class NodeGraphicsObject : public QGraphicsObject
void
contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;

private:
void
embedQWidget();

private:

FlowScene & _scene;
Expand All @@ -101,5 +100,6 @@ class NodeGraphicsObject : public QGraphicsObject

// either nullptr or owned by parent QGraphicsItem
QGraphicsProxyWidget * _proxyWidget;

};
}
21 changes: 21 additions & 0 deletions src/FlowScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdexcept>
#include <utility>

#include <QMenu>
#include <QtWidgets/QGraphicsSceneMoveEvent>
#include <QtWidgets/QFileDialog>
#include <QtCore/QByteArray>
Expand Down Expand Up @@ -50,6 +51,8 @@ FlowScene(std::shared_ptr<DataModelRegistry> registry,
connect(this, &FlowScene::connectionCreated, this, &FlowScene::setupConnectionSignals);
connect(this, &FlowScene::connectionCreated, this, &FlowScene::sendConnectionCreatedToNodes);
connect(this, &FlowScene::connectionDeleted, this, &FlowScene::sendConnectionDeletedToNodes);
connect(this, &FlowScene::nodeContextMenu , this, &FlowScene::sceneContextMenuEvent);

}

FlowScene::
Expand Down Expand Up @@ -607,6 +610,24 @@ sendConnectionDeletedToNodes(Connection const& c)
to->nodeDataModel()->inputConnectionDeleted(c);
}

void
FlowScene::
sceneContextMenuEvent(Node &node, const QPointF &pos)
{
QMenu menu;
QAction *embedAction = menu.addAction("Embed");
QAction *deembedAction = menu.addAction("Deembed");
QGraphicsView *v = node.nodeGraphicsObject().scene()->views().first();
QPoint viewP = v->mapFromScene(pos);
QAction *selectedAction = menu.exec( v->viewport()->mapToGlobal(viewP) );
if( selectedAction == embedAction ){
node.nodeGraphicsObject().embedQWidget(true);
}
else if(selectedAction == deembedAction){
node.nodeGraphicsObject().embedQWidget(false);
}
}


//------------------------------------------------------------------------------
namespace QtNodes
Expand Down
13 changes: 12 additions & 1 deletion src/NodeDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ using QtNodes::NodeStyle;

NodeDataModel::
NodeDataModel()
: _nodeStyle(StyleCollection::nodeStyle())
: m_wembed(false), _nodeStyle(StyleCollection::nodeStyle())

{
// Derived classes can initialize specific style here
}
Expand Down Expand Up @@ -39,3 +40,13 @@ setNodeStyle(NodeStyle const& style)
{
_nodeStyle = style;
}

bool NodeDataModel::wembed() const
{
return m_wembed;
}

void NodeDataModel::setWembed(bool wembed)
{
m_wembed = wembed;
}
2 changes: 2 additions & 0 deletions src/NodeGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ recalculateSize() const
_height = step * maxNumOfEntries;
}

if(_dataModel->wembed())
if (auto w = _dataModel->embeddedWidget())
{
_height = std::max(_height, static_cast<unsigned>(w->height()));
Expand All @@ -104,6 +105,7 @@ recalculateSize() const
_outputPortWidth +
2 * _spacing;

if(_dataModel->wembed())
if (auto w = _dataModel->embeddedWidget())
{
_width += w->width();
Expand Down
74 changes: 53 additions & 21 deletions src/NodeGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ NodeGraphicsObject(FlowScene &scene,
, _node(node)
, _locked(false)
, _proxyWidget(nullptr)

{
_scene.addItem(this);

Expand Down Expand Up @@ -57,7 +58,7 @@ NodeGraphicsObject(FlowScene &scene,

setZValue(0);

embedQWidget();
embedQWidget( true );

// connect to the move signals to emit the move signals in FlowScene
auto onMoveSlot = [this] {
Expand Down Expand Up @@ -93,27 +94,51 @@ node() const

void
NodeGraphicsObject::
embedQWidget()
embedQWidget( bool embed )
{
NodeGeometry & geom = _node.nodeGeometry();

if (auto w = _node.nodeDataModel()->embeddedWidget())
{
_proxyWidget = new QGraphicsProxyWidget(this);

_proxyWidget->setWidget(w);

_proxyWidget->setPreferredWidth(5);

geom.recalculateSize();

_proxyWidget->setPos(geom.widgetPosition());

update();
NodeGeometry & geom = _node.nodeGeometry();
_node.nodeDataModel()->setWembed( embed );
if (auto w = _node.nodeDataModel()->embeddedWidget())
{
if ( embed ){
if ( nullptr == _proxyWidget ) {
_proxyWidget = new QGraphicsProxyWidget(this);
w->setParent(nullptr);

_proxyWidget->setWidget(w);

_proxyWidget->setPreferredWidth(5);
geom.recalculateSize();

_proxyWidget->setPos(geom.widgetPosition());

update();

_proxyWidget->setOpacity(1.0);
_proxyWidget->setFlag(QGraphicsItem::ItemIgnoresParentOpacity);
}

}else{
if ( nullptr != _proxyWidget ){
_proxyWidget->setWidget(nullptr);
QPoint pos = QCursor::pos();
_proxyWidget->deleteLater();
_proxyWidget = nullptr;
connect(this,SIGNAL(destroyed()), w, SLOT(deleteLater()));
geom.recalculateSize();
update();
w->setWindowTitle(_node.nodeDataModel()->caption());
w->setWindowFlags(Qt::Widget);
w->move(pos.x(),pos.y());
w->show();
w->raise();

}
}
}

_proxyWidget->setOpacity(1.0);
_proxyWidget->setFlag(QGraphicsItem::ItemIgnoresParentOpacity);
}
moveConnections();
scene()->update();
}


Expand Down Expand Up @@ -345,6 +370,11 @@ hoverEnterEvent(QGraphicsSceneHoverEvent * event)
// bring this node forward
setZValue(1.0);

if (auto w = _node.nodeDataModel()->embeddedWidget())
{
w->raise();
}

_node.nodeGeometry().setHovered(true);
update();
_scene.nodeHovered(node(), event->screenPos());
Expand Down Expand Up @@ -398,5 +428,7 @@ void
NodeGraphicsObject::
contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
_scene.nodeContextMenu(node(), mapToScene(event->pos()));

_scene.nodeContextMenu(node(), mapToScene(event->pos()));

}