schema_editor/comdel/display/schema_display.cpp

108 lines
3.5 KiB
C++

#include "component_display.h"
#include "schema_display.h"
#include <QDrag>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <iostream>
namespace display {
Schema::Schema()
{
this->setScene(&scene);
this->setAcceptDrops(true);
}
void Schema::setSchema(domain::Schema* _schema, domain::Library* _library)
{
std::map<std::string, display::ComponentGroup*> components;
std::map<std::string, display::BusGroup*> 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<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);
}
}
}
}
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();
}
} // namespace display