schema_editor/comdel/display/schema_display.cpp

108 lines
3.5 KiB
C++
Raw Normal View History

2022-04-07 22:21:23 +00:00
#include "component_display.h"
#include "schema_display.h"
#include <QDrag>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <iostream>
2022-04-07 22:21:23 +00:00
namespace display {
Schema::Schema()
{
this->setScene(&scene);
this->setAcceptDrops(true);
2022-04-07 22:21:23 +00:00
}
void Schema::setSchema(domain::Schema* _schema, domain::Library* _library)
2022-04-07 22:21:23 +00:00
{
2022-05-17 22:14:33 +00:00
std::map<std::string, display::ComponentGroup*> components;
std::map<std::string, display::BusGroup*> buses;
2022-04-07 22:21:23 +00:00
scene.clear();
2022-05-17 22:14:33 +00:00
connections.clear();
this->schema = _schema;
this->library = _library;
2022-04-18 09:41:02 +00:00
if(schema != nullptr) {
2022-05-07 11:20:09 +00:00
for(auto &instance: schema->componentInstances) {
2022-05-17 22:14:33 +00:00
auto group = new display::ComponentGroup(instance);
components.insert(std::make_pair(instance->name, group));
scene.addItem(group);
2022-04-07 22:21:23 +00:00
}
2022-05-07 11:20:09 +00:00
for(auto &instance: schema->busInstances) {
if(instance->bus.getDisplay().has_value()) {
2022-05-17 22:14:33 +00:00
auto group = new display::BusGroup(instance);
buses.insert(std::make_pair(instance->name, group));
scene.addItem(group);
}
}
for(auto &connection: schema->connections) {
auto busInstance = dynamic_cast<domain::BusConnectionInstance*>(connection.get());
if(busInstance != nullptr) {
auto con = new display::BusConnection(busInstance, components[busInstance->instance->name], buses[busInstance->bus->name]);
connections.push_back(con);
scene.addItem(con);
}
2022-04-07 22:21:23 +00:00
}
}
}
2022-05-17 22:14:33 +00:00
void Schema::updateConnections() {
if(schema != nullptr) {
for(auto conn: connections) {
conn->updateConnection();
}
}
}
void Schema::dragEnterEvent(QDragEnterEvent *event) {
if(event->mimeData()->hasFormat("comdel/component") ||
event->mimeData()->hasFormat("comdel/bus")) {
event->acceptProposedAction();
} else {
std::cout<<"false"<< std::endl;
}
}
void Schema::dropEvent(QDropEvent *event) {
if(event->mimeData()->hasFormat("comdel/component")) {
auto component = library->getComponent(event->mimeData()->data("comdel/component").toStdString());
auto attributes = std::vector<domain::InstanceAttribute>();
for(auto attr: component.getAttributes()) {
attributes.emplace_back(attr.getName(), attr.getDefault(), attr);
}
auto currentPos = this->mapToScene(event->pos());
auto instance = std::make_shared<domain::ComponentInstance>(component.getInstanceName(), attributes, std::make_pair(currentPos.x(), currentPos.y()), component);
schema->componentInstances.push_back(instance);
auto group = new display::ComponentGroup(instance);
scene.addItem(group);
event->acceptProposedAction();
}
if(event->mimeData()->hasFormat("comdel/bus")) {
auto bus = library->getBus(event->mimeData()->data("comdel/bus").toStdString());
auto currentPos = this->mapToScene(event->pos());
auto instance = std::make_shared<domain::BusInstance>(bus.getName(), std::make_pair(currentPos.x(), currentPos.y()), bus, 50);
schema->busInstances.push_back(instance);
auto group = new display::BusGroup(instance);
scene.addItem(group);
event->acceptProposedAction();
}
}
void Schema::dragMoveEvent(QDragMoveEvent *event) {
event->acceptProposedAction();
}
2022-04-07 22:21:23 +00:00
} // namespace display