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
8 changes: 8 additions & 0 deletions src/GraphicsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ void GraphicsView::onPasteObjects()

void GraphicsView::keyPressEvent(QKeyEvent *event)
{
// An embedded widget with the focus gets the key first. Backspace and Delete edit its text.
if (auto *focusItem = scene() ? scene()->focusItem() : nullptr) {
if (qgraphicsitem_cast<QGraphicsProxyWidget *>(focusItem)) {
QGraphicsView::keyPressEvent(event);
return;
}
}

switch (event->key()) {
#ifdef Q_OS_MACOS
case Qt::Key_Backspace:
Expand Down
18 changes: 12 additions & 6 deletions src/UndoCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ static QJsonObject serializeSelectedItems(BasicGraphicsScene *scene)
return serializedScene;
}

static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *scene)
static void insertSerializedItems(QJsonObject const &json,
BasicGraphicsScene *scene,
bool const selectConnections)
{
AbstractGraphModel &graphModel = scene->graphModel();

Expand Down Expand Up @@ -141,8 +143,10 @@ static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *s
// Restore the connection
graphModel.addConnection(connId);

if (auto *cgo = scene->connectionGraphicsObject(connId))
cgo->setSelected(true);
if (selectConnections) {
if (auto *cgo = scene->connectionGraphicsObject(connId))
cgo->setSelected(true);
}
}

if (json.contains("groups")) {
Expand Down Expand Up @@ -236,7 +240,7 @@ void CreateCommand::redo()
if (_sceneJson.empty() || _sceneJson["nodes"].toArray().empty())
return;

insertSerializedItems(_sceneJson, _scene);
insertSerializedItems(_sceneJson, _scene, true);
}

//-------------------------------------
Expand Down Expand Up @@ -324,7 +328,9 @@ DeleteCommand::DeleteCommand(BasicGraphicsScene *scene)

void DeleteCommand::undo()
{
insertSerializedItems(_sceneJson, _scene);
// The deleted nodes were selected, so they are selected again. Selecting a node does not
// select its connections, so the restored connections stay unselected.
insertSerializedItems(_sceneJson, _scene, false);
}

void DeleteCommand::redo()
Expand Down Expand Up @@ -416,7 +422,7 @@ void PasteCommand::redo()

// Ignore if pasted in content does not generate nodes.
try {
insertSerializedItems(_newSceneJson, _scene);
insertSerializedItems(_newSceneJson, _scene, true);
} catch (...) {
// If the paste does not work, delete all selected nodes and connections
// `deleteNode(...)` implicitly removed connections
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_executable(test_nodes
src/TestVersion.cpp
src/TestZoomFeatures.cpp
src/TestLoopDetection.cpp
src/TestEmbeddedWidgetKeys.cpp
include/ApplicationSetup.hpp
include/TestGraphModel.hpp
include/UITestHelper.hpp
Expand Down
103 changes: 103 additions & 0 deletions test/src/TestEmbeddedWidgetKeys.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "ApplicationSetup.hpp"
#include "UITestHelper.hpp"

#include <QtNodes/BasicGraphicsScene>
#include <QtNodes/DataFlowGraphModel>
#include <QtNodes/DataFlowGraphicsScene>
#include <QtNodes/GraphicsView>
#include <QtNodes/NodeDelegateModel>
#include <QtNodes/NodeDelegateModelRegistry>

#include <catch2/catch.hpp>

#include <QGraphicsProxyWidget>
#include <QLineEdit>
#include <QPointer>

#include <memory>

using QtNodes::DataFlowGraphicsScene;
using QtNodes::DataFlowGraphModel;
using QtNodes::GraphicsView;
using QtNodes::NodeData;
using QtNodes::NodeDataType;
using QtNodes::NodeDelegateModel;
using QtNodes::NodeDelegateModelRegistry;
using QtNodes::NodeId;
using QtNodes::NodeRole;
using QtNodes::PortIndex;
using QtNodes::PortType;

namespace {

class LineEditModel : public NodeDelegateModel
{
public:
LineEditModel()
: _edit(new QLineEdit(QStringLiteral("abc")))
{}

~LineEditModel() override { delete _edit; }

QString name() const override { return QStringLiteral("LineEditModel"); }
QString caption() const override { return QStringLiteral("Line"); }

unsigned int nPorts(PortType) const override { return 0; }
NodeDataType dataType(PortType, PortIndex) const override { return {}; }
void setInData(std::shared_ptr<NodeData>, PortIndex) override {}
std::shared_ptr<NodeData> outData(PortIndex) override { return nullptr; }

QWidget *embeddedWidget() override { return _edit; }

private:
QPointer<QLineEdit> _edit;
};

} // namespace

TEST_CASE("Backspace in an embedded line edit erases text and does not delete a selected node",
"[ui][widget-keys]")
{
auto app = applicationSetup();

auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel<LineEditModel>();

DataFlowGraphModel model(registry);
DataFlowGraphicsScene scene(model);
GraphicsView view(&scene);
view.resize(800, 600);
view.show();
REQUIRE(QTest::qWaitForWindowExposed(&view));

NodeId const other = model.addNode(QStringLiteral("LineEditModel"));
NodeId const edited = model.addNode(QStringLiteral("LineEditModel"));
model.setNodeData(other, NodeRole::Position, QPointF(-300, 0));
model.setNodeData(edited, NodeRole::Position, QPointF(100, 0));
UITestHelper::waitForUI();

// No style is loaded in tests, so the default opacity is 0 and Qt would
// skip the nodes when hit-testing the simulated mouse events.
scene.nodeGraphicsObject(other)->setOpacity(1.0);
scene.nodeGraphicsObject(edited)->setOpacity(1.0);

auto *edit = qobject_cast<QLineEdit *>(model.delegateModel<LineEditModel>(edited)
->embeddedWidget());
REQUIRE(edit != nullptr);

scene.nodeGraphicsObject(other)->setSelected(true);

QGraphicsProxyWidget *proxy = edit->graphicsProxyWidget();
REQUIRE(proxy != nullptr);
QPointF const target = proxy->mapToScene(proxy->boundingRect().center());

UITestHelper::simulateMousePress(&view, target);
UITestHelper::simulateMouseRelease(&view, target);
UITestHelper::waitForUI();

QTest::keyClick(&view, Qt::Key_Backspace);
UITestHelper::waitForUI();

CHECK(edit->text() == QStringLiteral("ab"));
CHECK(model.allNodeIds().size() == 2);
}
58 changes: 58 additions & 0 deletions test/src/TestSerializedItemCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QtNodes/BasicGraphicsScene>
#include <QtNodes/Definitions>
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>
#include <QtNodes/internal/NodeGraphicsObject.hpp>
#include <QtNodes/internal/GroupGraphicsObject.hpp>
#include <QtNodes/internal/NodeGroup.hpp>
Expand Down Expand Up @@ -231,6 +232,63 @@ TEST_CASE("Deleting a node removes its connections and undo restores them",
CHECK(model.connectionExists(ac));
}

TEST_CASE("Undo of a delete selects the restored nodes and not their connections",
"[undo][serialized]")
{
auto app = applicationSetup();

RecordingGraphModel model;
BasicGraphicsScene scene(model);

NodeId const a = addNodeAt(model, QPointF(0, 0));
NodeId const b = addNodeAt(model, QPointF(100, 0));
NodeId const c = addNodeAt(model, QPointF(200, 0));

ConnectionId const ab{a, 0, b, 0};
ConnectionId const bc{b, 0, c, 0};
model.addConnection(ab);
model.addConnection(bc);

selectOnly(scene, {b});
scene.undoStack().push(new DeleteCommand(&scene));
scene.undoStack().undo();

CHECK(scene.nodeGraphicsObject(b)->isSelected());
CHECK_FALSE(scene.nodeGraphicsObject(a)->isSelected());
REQUIRE(scene.connectionGraphicsObject(ab) != nullptr);
REQUIRE(scene.connectionGraphicsObject(bc) != nullptr);
CHECK_FALSE(scene.connectionGraphicsObject(ab)->isSelected());
CHECK_FALSE(scene.connectionGraphicsObject(bc)->isSelected());
}

TEST_CASE("Paste selects the pasted nodes and their connections", "[undo][serialized]")
{
auto app = applicationSetup();

RecordingGraphModel model;
BasicGraphicsScene scene(model);

NodeId const a = addNodeAt(model, QPointF(0, 0));
NodeId const b = addNodeAt(model, QPointF(100, 0));
model.addConnection(ConnectionId{a, 0, b, 0});

selectOnly(scene, {a, b});
scene.undoStack().push(new CopyCommand(&scene));
scene.undoStack().push(new PasteCommand(&scene, QPointF(500, 500)));

REQUIRE(model.allNodeIds().size() == 4);

for (NodeId id : model.allNodeIds()) {
bool const pasted = id != a && id != b;
CHECK(scene.nodeGraphicsObject(id)->isSelected() == pasted);

for (ConnectionId const &connectionId : model.allConnectionIds(id)) {
REQUIRE(scene.connectionGraphicsObject(connectionId) != nullptr);
CHECK(scene.connectionGraphicsObject(connectionId)->isSelected() == pasted);
}
}
}

TEST_CASE("CreateCommand adds a node, undo removes it and redo restores it",
"[undo][serialized]")
{
Expand Down
Loading