#include "component_display.h" #include "schema_display.h" #include #include #include #include #include namespace display { Schema::Schema() { this->setScene(&scene); this->setAcceptDrops(true); } void Schema::setSchema(domain::Schema* _schema, domain::Library* _library) { std::map components; std::map buses; scene.clear(); connections.clear(); this->schema = _schema; this->library = _library; if(schema != nullptr) { for(auto &instance: schema->componentInstances) { auto group = new display::ComponentGroup(instance); components.insert(std::make_pair(instance->name, group)); scene.addItem(group); } for(auto &instance: schema->busInstances) { if(instance->bus.getDisplay().has_value()) { 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(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); } } } } 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(); for(auto attr: component.getAttributes()) { attributes.emplace_back(attr.getName(), attr.getDefault(), attr); } auto currentPos = this->mapToScene(event->pos()); auto instance = std::make_shared(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(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(); } } // namespace display