schema_editor/comdel/display/component_display.cpp

357 lines
17 KiB
C++
Raw Normal View History

2022-04-07 22:21:23 +00:00
#include "component_display.h"
2022-06-13 22:53:46 +00:00
#include "comdel/display/dialog/attribute_dialog.h"
#include "comdel/display/dialog/name_dialog.h"
2022-05-15 21:55:03 +00:00
#include "mainwindow.h"
#include "application.h"
2022-06-13 22:53:46 +00:00
#include "comdel/display/dialog/single_automatic_dialog.h"
#include "comdel/display/dialog/memory_dialog.h"
2022-06-19 18:10:44 +00:00
#include "message_source.h"
2022-04-07 22:21:23 +00:00
2022-04-10 15:24:15 +00:00
#include <QMenu>
2022-05-25 05:39:45 +00:00
#include <QLine>
2022-06-09 18:24:27 +00:00
#include <QPen>
2022-04-10 15:24:15 +00:00
#include <QGraphicsSceneContextMenuEvent>
#include <set>
2022-04-10 15:24:15 +00:00
2022-06-09 18:24:27 +00:00
2022-04-07 22:21:23 +00:00
namespace display {
2022-06-09 18:24:27 +00:00
QPen connectionPen(QColor::fromRgb(150, 150, 250));
2022-06-09 21:42:45 +00:00
Component::Component(const std::shared_ptr<domain::ComponentInstance> &instance): instance(instance) {
setFlag(ItemSendsGeometryChanges, true);
2022-06-14 19:27:40 +00:00
setToolTip(QString::fromStdString(instance->name + "::" + instance->component.getDisplayName()));
instance->component.getDisplay().render(this, domain::ui::DisplayContext(instance.get()));
2022-06-09 21:42:45 +00:00
}
2022-05-27 06:18:17 +00:00
void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
2022-06-19 18:10:44 +00:00
menu.addAction(QMESSAGE("msg_dialog_name_update"), [this]() {
std::set<std::string> names;
for(const auto &component: Application::instance()->getSchema()->componentInstances) {
names.insert(component->name);
}
auto dialog = new NameDialog(this->instance->name, names);
2022-05-27 06:18:17 +00:00
dialog->exec();
auto currentName = this->instance->name;
auto newName = dialog->getName();
Application::instance()->renameComponent(currentName, newName);
2022-06-14 19:27:40 +00:00
setToolTip(QString::fromStdString(instance->name + "::" + instance->component.getDisplayName()));
2022-05-15 14:13:29 +00:00
});
2022-05-27 06:18:17 +00:00
menu.addSeparator();
for (int i = 0; i < this->instance->attributes.size(); i++) {
auto *attr = &this->instance->attributes[i];
bool enabled = attr->attribute.getPopup().has_value();
2022-06-09 18:24:27 +00:00
if(attr->value.getType() == domain::Value::MEMORY_REFERENCE) {
2022-06-19 18:10:44 +00:00
menu.addAction(QMESSAGE("msg_dialog_memory_update"), [attr]() {
auto dialog = new MemoryDialog(MESSAGE("msg_dialog_memory_update"),
MESSAGE("msg_dialog_actions_update"),
attr,
2022-06-09 18:24:27 +00:00
Application::instance()->getSchema()->componentInstances);
dialog->exec();
});
} else {
2022-06-19 18:10:44 +00:00
std::map<std::string, std::string> params = {{"attribute", attr->attribute.getDisplayName()}};
auto action = menu.addAction(QMESSAGE_PARAM("msg_dialog_attribute_update", params),
[attr]() {
std::map<std::string, std::string> params = {{"attribute", attr->attribute.getDisplayName()}};
auto dialog = new AttributeDialog(MESSAGE_PARAM("msg_dialog_attribute_update", params),
"msg_dialog_actions_update",
attr);
dialog->exec();
});
2022-06-09 18:24:27 +00:00
action->setEnabled(enabled);
}
2022-05-27 06:18:17 +00:00
}
menu.addSeparator();
2022-06-19 18:10:44 +00:00
std::map<std::string, std::string> params = {{"name", instance->name}};
menu.addAction(QMESSAGE_PARAM("msg_dialog_actions_remove_named", params), [this]() {
Application::instance()->removeComponent(this->instance->name);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
2022-05-27 06:18:17 +00:00
menu.exec(event->screenPos());
}
void Pin::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
2022-06-09 21:42:45 +00:00
auto pinConnections = Application::instance()->getSchema()->getConnections(componentInstance->name, pin.getName());
if(isSingleAutomatic(pinConnections)) {
2022-06-19 18:10:44 +00:00
auto *update = menu.addMenu(QMESSAGE("msg_pin_update"));
auto *remove = menu.addMenu(QMESSAGE("msg_pin_remove"));
2022-06-09 21:42:45 +00:00
for (auto pinConnection: pinConnections) {
// this always must be true as only directConnections can be connected multiple times
if (auto directConnection = dynamic_cast<domain::DirectConnectionInstance *>(pinConnection)) {
if (directConnection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC) {
2022-06-19 18:10:44 +00:00
auto params = MessageSource::instance()->map(
{{"wire1", directConnection->attributes[0].value},
{"wire2", directConnection->attributes[1].value}}
);
update->addAction(QMESSAGE_PARAM("msg_sa_pin_update_title", params),
[directConnection]() {
auto params = MessageSource::instance()->map(
{{"wire1", directConnection->attributes[0].value},
{"wire2", directConnection->attributes[1].value}}
);
auto dialog = new SingleAutomaticDialog(MESSAGE_PARAM("msg_sa_pin_update_title", params),
MESSAGE("msg_pin_update_action"),
directConnection->attributes);
2022-06-09 21:42:45 +00:00
dialog->exec();
});
2022-06-19 18:10:44 +00:00
remove->addAction(QMESSAGE_PARAM("msg_sa_pin_remove_title", params),
2022-06-09 21:42:45 +00:00
[this, directConnection]() {
Application::instance()->removeConnection(directConnection);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
}
}
}
2022-06-12 11:43:35 +00:00
} else if(!pinConnections.empty()) {
2022-06-09 21:42:45 +00:00
auto pinConnection = pinConnections[0];
if(auto busConnection = dynamic_cast<domain::BusConnectionInstance*>(pinConnection)) {
menu.addSection(QString::fromStdString(busConnection->bus->name));
} else if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(pinConnection)) {
if(directConnection->instance == componentInstance.get()) {
menu.addSection(QString::fromStdString(directConnection->secondInstance->name + "." + directConnection->connection.getSecondComponent()->pin));
} else {
menu.addSection(QString::fromStdString(directConnection->instance->name + "." + directConnection->connection.getComponent().pin));
}
}
for (int i = 0; i < pinConnection->attributes.size(); i++) {
auto *attr = &pinConnection->attributes[i];
2022-06-19 18:10:44 +00:00
std::map<std::string, std::string> params = {{"name", attr->attribute.getDisplayName()}};
menu.addAction(QMESSAGE_PARAM("msg_dialog_actions_update_named", params),
[attr]() {
std::map<std::string, std::string> params = {{"name", attr->attribute.getDisplayName()}};
auto dialog = new AttributeDialog(MESSAGE_PARAM("msg_dialog_actions_update_named", params), "msg_pin_update_action", attr);
dialog->exec();
2022-06-09 21:42:45 +00:00
});
}
2022-06-19 18:10:44 +00:00
menu.addAction(QMESSAGE("msg_pin_remove_action"), [this, pinConnection]() {
2022-06-09 21:42:45 +00:00
Application::instance()->removeConnection(pinConnection);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
}
menu.exec(event->screenPos());
}
void Pin::mousePressEvent(QGraphicsSceneMouseEvent *event) {
2022-05-27 06:18:17 +00:00
if (event->button() == Qt::MouseButton::LeftButton) {
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->state = Schema::CREATING_CONNECTION;
view->context.pin = this;
2022-05-27 06:18:17 +00:00
view->context.startingPoint = dynamic_cast<ComponentGroup *>(this->parentItem())->pos() +
QPointF(pin.getDisplayPin().getConnectionX(),
pin.getDisplayPin().getConnectionY());
2022-05-25 05:39:45 +00:00
view->context.line = new QGraphicsLineItem(QLineF(view->context.startingPoint, event->scenePos()));
2022-06-09 18:24:27 +00:00
view->context.line->setPen(QPen(QColor::fromRgb(100, 100, 250), 1, Qt::PenStyle::DashLine));
view->context.line->setZValue(1000);
2022-05-25 05:39:45 +00:00
this->scene()->addItem(view->context.line);
view->showConnectable(this);
}
}
void Pin::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
2022-05-27 06:18:17 +00:00
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
if (view->state == Schema::CREATING_CONNECTION) {
2022-05-25 05:39:45 +00:00
auto line = view->context.line->line();
line.setP2(event->scenePos());
view->context.line->setLine(line);
}
}
void Pin::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
2022-05-27 06:18:17 +00:00
if (event->button() == Qt::MouseButton::LeftButton) {
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->state = Schema::DEFAULT;
2022-05-25 05:39:45 +00:00
this->scene()->removeItem(view->context.line);
delete view->context.line;
view->removeConnectable(event->scenePos());
}
}
2022-05-27 06:18:17 +00:00
domain::Pin &Pin::getPin() {
return pin;
}
domain::ComponentInstance *Pin::getComponentInstance() {
return componentInstance.get();
}
2022-06-09 21:42:45 +00:00
bool Pin::isSingleAutomatic(std::vector<domain::ConnectionInstance *> pinConnections) {
if(pinConnections.empty()) {
return false;
}
auto pinConnection = pinConnections[0];
if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(pinConnection)) {
return directConnection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC;
} else {
return false;
}
}
Bus::Bus(const std::shared_ptr<domain::BusInstance> &instance): busInstance(instance) {
instance->bus.getDisplayBus()->render(this, instance->size);
setToolTip(QString::fromStdString(busInstance->name + "::" + busInstance->bus.getName()));
}
void Bus::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
2022-05-27 06:18:17 +00:00
QMenu menu;
2022-06-19 18:10:44 +00:00
menu.addAction(QMESSAGE("msg_dialog_name_update"), [this]() {
std::set<std::string> names;
for(const auto &component: Application::instance()->getSchema()->busInstances) {
names.insert(component->name);
}
auto dialog = new NameDialog(this->busInstance->name, names);
2022-05-27 06:18:17 +00:00
dialog->exec();
auto currentName = this->busInstance->name;
auto newName = dialog->getName();
Application::instance()->renameBus(currentName, newName);
2022-06-14 19:27:40 +00:00
setToolTip(QString::fromStdString(busInstance->name + "::" + busInstance->bus.getDisplayName()));
});
menu.addSeparator();
2022-06-19 18:10:44 +00:00
std::map<std::string, std::string> params = {{"name", busInstance->name}};
menu.addAction(QMESSAGE_PARAM("msg_dialog_actions_remove_named", params), [this]() {
Application::instance()->removeBus(this->busInstance->name);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
2022-05-27 06:18:17 +00:00
});
menu.exec(event->screenPos());
}
2022-05-17 19:31:32 +00:00
QVariant BusGroup::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) {
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPoint newPos = value.toPointF().toPoint();
2022-05-17 19:31:32 +00:00
busInstance->position.first = newPos.x();
busInstance->position.second = newPos.y();
2022-05-17 22:14:33 +00:00
2022-05-27 06:18:17 +00:00
auto view = dynamic_cast<Schema *>(scene()->views()[0]);
2022-05-17 22:14:33 +00:00
view->updateConnections();
2022-05-17 19:31:32 +00:00
}
return QGraphicsItem::itemChange(change, value);
}
2022-05-27 06:18:17 +00:00
BusGroup::BusGroup(const std::shared_ptr<domain::BusInstance> &instance) : busInstance(instance) {
2022-05-26 17:21:53 +00:00
setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true);
setHandlesChildEvents(false);
bus = new display::Bus(instance);
addToGroup(bus);
setPos(instance->position.first, instance->position.second);
2022-05-27 06:18:17 +00:00
}
2022-05-26 17:21:53 +00:00
2022-05-17 19:31:32 +00:00
QVariant ComponentGroup::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) {
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPoint newPos = value.toPointF().toPoint();
2022-05-17 19:31:32 +00:00
componentInstance->position.first = newPos.x();
componentInstance->position.second = newPos.y();
2022-05-17 22:14:33 +00:00
2022-05-27 06:18:17 +00:00
auto view = dynamic_cast<Schema *>(scene()->views()[0]);
2022-05-17 22:14:33 +00:00
view->updateConnections();
2022-05-17 19:31:32 +00:00
}
return QGraphicsItem::itemChange(change, value);
}
2022-05-17 22:14:33 +00:00
2022-05-27 06:18:17 +00:00
std::shared_ptr<domain::ComponentInstance> ComponentGroup::getComponentInstance() {
return componentInstance;
}
std::vector<display::Pin *> &ComponentGroup::getPins() {
return pins;
}
ComponentGroup::ComponentGroup(const std::shared_ptr<domain::ComponentInstance> &instance): componentInstance(
instance) {
setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true);
setHandlesChildEvents(false);
addToGroup(new display::Component(instance));
for (auto &pin: instance->component.getPins()) {
pins.push_back(new display::Pin(pin, componentInstance));
addToGroup(pins.back());
}
setPos(instance->position.first, instance->position.second);
}
BusConnection::BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus): connection(
connection), component(component), bus(bus) {
updateConnection();
2022-06-09 18:24:27 +00:00
setPen(connectionPen);
setZValue(-1);
2022-05-27 06:18:17 +00:00
setHandlesChildEvents(false);
}
void BusConnection::updateConnection() {
auto busPosition = bus->boundingRect();
auto pin = connection->instance->component.getPin(
connection->connection.getComponent().pin).getDisplayPin();
setLine(connection->instance->position.first + pin.getConnectionX(),
connection->instance->position.second + pin.getConnectionY(),
connection->bus->position.first + busPosition.width() / 2,
connection->bus->position.second + busPosition.height() / 2);
connection->start.first = connection->instance->position.first + pin.getConnectionX();
connection->start.second = connection->instance->position.second + pin.getConnectionY();
connection->end.first = connection->bus->position.first + busPosition.width() / 2;
connection->end.second = connection->bus->position.second + busPosition.height() / 2;
}
DirectConnection::DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first,
ComponentGroup *second): connection(connection), first(first), second(second) {
updateConnection();
2022-06-09 18:24:27 +00:00
setPen(connectionPen);
setZValue(-1);
2022-05-27 06:18:17 +00:00
setHandlesChildEvents(false);
}
void DirectConnection::updateConnection() {
2022-06-19 18:10:44 +00:00
domain::ui::Pin pin1, pin2;
if(connection->connection.getComponent().component == connection->instance->component.getName()) {
pin1 = connection->instance->component.getPin(
connection->connection.getComponent().pin).getDisplayPin();
pin2 = connection->secondInstance->component.getPin(
connection->connection.getSecondComponent()->pin).getDisplayPin();
} else {
pin1 = connection->instance->component.getPin(
connection->connection.getSecondComponent()->pin).getDisplayPin();
pin2 = connection->secondInstance->component.getPin(
connection->connection.getComponent().pin).getDisplayPin();
}
2022-05-27 06:18:17 +00:00
setLine(connection->instance->position.first + pin1.getConnectionX(),
connection->instance->position.second + pin1.getConnectionY(),
connection->secondInstance->position.first + pin2.getConnectionX(),
connection->secondInstance->position.second + pin2.getConnectionY());
connection->start.first = connection->instance->position.first + pin1.getConnectionX();
connection->start.second = connection->instance->position.second + pin1.getConnectionY();
connection->end.first = connection->secondInstance->position.first + pin2.getConnectionX();
connection->end.second = connection->secondInstance->position.second + pin2.getConnectionY();
}
2022-04-07 22:21:23 +00:00
} // namespace display