diff --git a/agui2/src/Graphics/Connection/Connection.cpp b/agui2/src/Graphics/Connection/Connection.cpp index 01d85ef37a57bb20c070f9bfeb391174d2ad646e..a22dd2cea34f2191ddb20a1bd734c8c2cc40bbcc 100644 --- a/agui2/src/Graphics/Connection/Connection.cpp +++ b/agui2/src/Graphics/Connection/Connection.cpp @@ -24,8 +24,7 @@ Connection::Connection(OutputConnectionBox* origin, InputConnectionBox* target) auto* originModel = origin->getParent()->getModelBox(); auto* targetModel = target->getParent()->getModelBox(); - originModel->addOutput(targetModel, target->getSlot()); - targetModel->setInput(target->getSlot(), originModel); + ModelBox::connect(originModel, targetModel, target->getSlot()); origin->addConnection(this); target->setConnection(this); @@ -104,8 +103,7 @@ void Connection::destroy() { auto* originModel = originConnectionBox->getParent()->getModelBox(); auto* targetModel = targetConnectionBox->getParent()->getModelBox(); - originModel->removeOutput(targetModel, targetConnectionBox->getSlot()); - targetModel->setInput(targetConnectionBox->getSlot(), nullptr); + ModelBox::disconnect(originModel, targetModel, targetConnectionBox->getSlot()); auto removed = this->originConnectionBox->connections.erase(this); Q_ASSERT(removed > 0); diff --git a/agui2/src/Models/ModelBox.cpp b/agui2/src/Models/ModelBox.cpp index ccd6acfa166fd8bfc8ca6df8bac4a3e9c0bccb7e..a4d3e23475671fa72cca9bb13cd2ce1c6d04d758 100644 --- a/agui2/src/Models/ModelBox.cpp +++ b/agui2/src/Models/ModelBox.cpp @@ -28,14 +28,14 @@ void ModelBox::setInput(size_t slot, ModelBox* model) { this->inputs[slot] = model; } -void ModelBox::addOutput(ModelBox* model, size_t slot) { +void ModelBox::addOutput(ModelBox* target, size_t targetSlot) { Q_ASSERT(this->canHaveOutput()); - this->outputs.emplace(model, slot); + this->outputs.emplace(target, targetSlot); } -void ModelBox::removeOutput(ModelBox* model, size_t slot) { +void ModelBox::removeOutput(ModelBox* target, size_t targetSlot) { Q_ASSERT(this->canHaveOutput()); - this->outputs.erase({model, slot}); + this->outputs.erase({target, targetSlot}); } std::shared_ptr<abstraction::OperationAbstraction> ModelBox::getCachedResultOrEvaluate() { @@ -49,3 +49,13 @@ void ModelBox::clearCachedResults() { box->clearCachedResult(); } } + +void ModelBox::connect(ModelBox* origin, ModelBox* target, size_t targetSlot) { + origin->addOutput(target, targetSlot); + target->setInput(targetSlot, origin); +} + +void ModelBox::disconnect(ModelBox* origin, ModelBox* target, size_t targetSlot) { + origin->removeOutput(target, targetSlot); + target->setInput(targetSlot, nullptr); +} diff --git a/agui2/src/Models/ModelBox.hpp b/agui2/src/Models/ModelBox.hpp index 71560388e8f98887e8a9f8d02f191c79efa22ba1..40fe9b36e16e0e3a068fcd2b7aa5a6fe760c5d69 100644 --- a/agui2/src/Models/ModelBox.hpp +++ b/agui2/src/Models/ModelBox.hpp @@ -26,10 +26,6 @@ public: ModelType getType() const { return this->type; } virtual std::string getName() const = 0; - void setInput(size_t slot, ModelBox* model); - void addOutput(ModelBox* model, size_t slot); - void removeOutput(ModelBox* model, size_t slot); - size_t getMaxInputCount() const { return this->maxInputCount; } virtual bool canHaveOutput() const { return true; } @@ -38,9 +34,17 @@ public: virtual void clearCachedResult() { this->result.reset(); } virtual std::shared_ptr<abstraction::OperationAbstraction> evaluate() = 0; + static void connect(ModelBox* origin, ModelBox* target, size_t targetSlot); + static void disconnect(ModelBox* origin, ModelBox* target, size_t targetSlot); + static const std::vector<ModelBox*>& getAllModelBoxes() { return ModelBox::allModelBoxes; } static void clearCachedResults(); +private: + void setInput(size_t slot, ModelBox* model); + void addOutput(ModelBox* model, size_t slot); + void removeOutput(ModelBox* model, size_t slot); + protected: ModelType type;