Added connection generation

This commit is contained in:
Borna Rajković 2022-05-25 07:39:45 +02:00
parent cbff8ff5f2
commit 5cf8235608
16 changed files with 440 additions and 35 deletions

View File

@ -4,9 +4,9 @@
#include "mainwindow.h"
#include <QMenu>
#include <QLine>
#include <QGraphicsSceneContextMenuEvent>
#include <iostream>
#include <QDebug>
namespace display {
@ -47,14 +47,20 @@ void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
auto view = dynamic_cast<Schema*>(this->scene()->views()[0]);
view->state = Schema::CREATING_CONNECTION;
view->context.pin = this;
view->context.startingPoint = view->mapToScene(event->pos().toPoint());
view->context.startingPoint = dynamic_cast<ComponentGroup*>(this->parentItem())->pos() + QPointF(pin.getDisplayPin().getConnectionX(), pin.getDisplayPin().getConnectionY());
view->context.line = new QGraphicsLineItem(QLineF(view->context.startingPoint, event->scenePos()));
this->scene()->addItem(view->context.line);
view->showConnectable(this);
}
}
void Pin::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
auto view = dynamic_cast<Schema*>(this->scene()->views()[0]);
if(view->state == Schema::CREATING_CONNECTION) {
auto line = view->context.line->line();
line.setP2(event->scenePos());
view->context.line->setLine(line);
}
}
@ -62,6 +68,9 @@ void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
if(event->button() == Qt::MouseButton::LeftButton) {
auto view = dynamic_cast<Schema*>(this->scene()->views()[0]);
view->state = Schema::DEFAULT;
this->scene()->removeItem(view->context.line);
delete view->context.line;
view->removeConnectable(event->scenePos());
}
}

View File

@ -15,8 +15,10 @@ class Pin: public QGraphicsItemGroup
{
private:
domain::Pin pin;
std::shared_ptr<domain::ComponentInstance> componentInstance;
public:
Pin(domain::Pin pin): pin(pin) {
Pin(domain::Pin pin, std::shared_ptr<domain::ComponentInstance> componentInstance): pin(pin), componentInstance(componentInstance) {
pin.getDisplayPin().render(this);
}
@ -25,6 +27,13 @@ public:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
domain::Pin& getPin() {
return pin;
}
domain::ComponentInstance* getComponentInstance() {
return componentInstance.get();
}
};
class Component: public QGraphicsItemGroup
@ -56,8 +65,13 @@ class ComponentGroup: public QGraphicsItemGroup
{
private:
std::shared_ptr<domain::ComponentInstance> componentInstance;
std::vector<display::Pin*> pins;
public:
std::shared_ptr<domain::ComponentInstance> getComponentInstance() { return componentInstance; }
std::vector<display::Pin*>& getPins() { return pins; }
explicit ComponentGroup(const std::shared_ptr<domain::ComponentInstance>& instance): componentInstance(instance) {
setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true);
@ -67,7 +81,8 @@ public:
addToGroup(new display::Component(instance));
for(auto &pin: instance->component.getPins()) {
addToGroup(new display::Pin(pin));
pins.push_back(new display::Pin(pin, componentInstance));
addToGroup(pins.back());
}
setPos(instance->position.first, instance->position.second);
@ -118,6 +133,12 @@ public:
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;
}
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
@ -130,10 +151,9 @@ public:
domain::DirectConnectionInstance* connection;
ComponentGroup* first;
ComponentGroup* second;
BusGroup* bus;
public:
DirectConnection(domain::DirectConnectionInstance* connection, ComponentGroup *first, ComponentGroup *second, BusGroup *bus): connection(connection), first(first), second(second), bus(bus) {
DirectConnection(domain::DirectConnectionInstance* connection, ComponentGroup *first, ComponentGroup *second): connection(connection), first(first), second(second) {
updateConnection();
setHandlesChildEvents(false);
}
@ -144,6 +164,12 @@ public:
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();
}
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;

View File

@ -3,14 +3,15 @@
#include <QDrag>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <iostream>
namespace display {
Schema::Schema()
{
this->selectedBrush.setColor(QColor::fromRgb(20,20,125));
this->selectedPen.setColor(QColor::fromRgb(20, 20, 125));
this->setScene(&scene);
this->setAcceptDrops(true);
}
@ -18,16 +19,21 @@ Schema::Schema()
void Schema::setSchema(domain::Schema* _schema, domain::Library* _library)
{
std::map<std::string, display::ComponentGroup*> components;
std::map<std::string, display::BusGroup*> buses;
components.clear();
buses.clear();
scene.clear();
connections.clear();
busConnections.clear();
this->schema = _schema;
this->library = _library;
if(schema != nullptr) {
for(auto &instance: schema->componentInstances) {
auto group = new display::ComponentGroup(instance);
for(auto pin: group->getPins()) {
display::Pin* p = pin;
domain::ConnectionComponent connection{instance->name, p->getPin().getName()};
pins.insert(std::make_pair(connection, p));
}
components.insert(std::make_pair(instance->name, group));
scene.addItem(group);
}
@ -42,7 +48,13 @@ void Schema::setSchema(domain::Schema* _schema, domain::Library* _library)
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);
busConnections.push_back(con);
scene.addItem(con);
}
auto directInstance = dynamic_cast<domain::DirectConnectionInstance*>(connection.get());
if(directInstance != nullptr) {
auto con = new display::DirectConnection(directInstance, components[directInstance->instance->name], components[directInstance->secondInstance->name]);
directConnections.push_back(con);
scene.addItem(con);
}
}
@ -51,7 +63,10 @@ void Schema::setSchema(domain::Schema* _schema, domain::Library* _library)
void Schema::updateConnections() {
if(schema != nullptr) {
for(auto conn: connections) {
for(auto conn: busConnections) {
conn->updateConnection();
}
for(auto conn: directConnections) {
conn->updateConnection();
}
}
@ -61,8 +76,6 @@ void Schema::updateConnections() {
if(event->mimeData()->hasFormat("comdel/component") ||
event->mimeData()->hasFormat("comdel/bus")) {
event->acceptProposedAction();
} else {
std::cout<<"false"<< std::endl;
}
}
@ -82,6 +95,13 @@ void Schema::updateConnections() {
auto group = new display::ComponentGroup(instance);
scene.addItem(group);
for(auto pin: group->getPins()) {
display::Pin* p = pin;
domain::ConnectionComponent connection{instance->name, p->getPin().getName()};
pins.insert(std::make_pair(connection, p));
}
components[instance->name] = group;
event->acceptProposedAction();
}
@ -95,6 +115,7 @@ void Schema::updateConnections() {
auto group = new display::BusGroup(instance);
scene.addItem(group);
buses[instance->name] = group;
event->acceptProposedAction();
}
@ -104,4 +125,140 @@ void Schema::updateConnections() {
event->acceptProposedAction();
}
void Schema::removeConnectable(QPointF endPoint) {
auto instance = context.pin->getComponentInstance();
auto& pin = context.pin->getPin();
auto busInstances = getAvailableConnectionBusses(instance, pin);
for(auto &bus: busInstances) {
auto rect = buses[bus->name]->boundingRect();
rect = QRectF(buses[bus->name]->x(), buses[bus->name]->y(), rect.width(), rect.height());
if(rect.contains(endPoint)) {
auto con = library->getConnection({instance->component.getName(), pin.getName()}, bus->bus.getName());
if(con.has_value()) {
std::vector<domain::InstanceAttribute> attributes;
for(auto attr: con->getAttributes()) {
attributes.emplace_back(attr.getName(), attr.getDefault(), attr);
}
auto conInstance = std::make_shared<domain::BusConnectionInstance>(instance, attributes, bus, *con);
schema->connections.push_back(conInstance);
if (conInstance != nullptr) {
auto c = new display::BusConnection(conInstance.get(), components[conInstance->instance->name],
buses[conInstance->bus->name]);
busConnections.push_back(c);
scene.addItem(c);
}
break;
}
}
}
auto pinInstances = getAvailableConnectionPins(instance, pin);
for(auto &pinInstance: pinInstances) {
auto rect = pins[pinInstance]->boundingRect();
rect.setX(pins[pinInstance]->scenePos().x());
rect.setY(pins[pinInstance]->scenePos().y());
if(rect.contains(endPoint)) {
auto name = components[pinInstance.component]->getComponentInstance()->component.getName();
auto con = library->getConnection({instance->component.getName(), pin.getName()}, {name, pinInstance.pin});
if(con.has_value()) {
auto bus = library->getBus(con->getBus());
auto busInstance = std::make_shared<domain::BusInstance>(bus.getName(), bus);
schema->busInstances.push_back(busInstance);
std::vector<domain::InstanceAttribute> attributes;
for(auto attr: con->getAttributes()) {
attributes.emplace_back(attr.getName(), attr.getDefault(), attr);
}
auto conInstance = std::make_shared<domain::DirectConnectionInstance>(instance, components[pinInstance.component]->getComponentInstance().get(), attributes, busInstance.get(), *con);
schema->connections.push_back(conInstance);
if (conInstance != nullptr) {
auto c = new display::DirectConnection(conInstance.get(), components[conInstance->instance->name], components[conInstance->secondInstance->name]);
directConnections.push_back(c);
scene.addItem(c);
}
break;
}
}
}
updateConnections();
for(auto& item: this->context.selectable) {
this->scene.removeItem(item);
delete item;
}
this->context.selectable.clear();
}
void Schema::showConnectable(Pin *domainPin) {
auto& pin = domainPin->getPin();
auto busInstances = getAvailableConnectionBusses(domainPin->getComponentInstance(), domainPin->getPin());
for(auto bus: busInstances) {
auto& group = buses[bus->name];
auto rect = new QGraphicsRectItem(group->boundingRect());
rect->setPen(selectedPen);
rect->setPos(group->scenePos());
auto _rect = rect->rect();
_rect.setWidth(_rect.width() + 1);
_rect.setHeight(_rect.height() + 1);
rect->setRect(_rect);
context.selectable.push_back(rect);
scene.addItem(rect);
}
std::vector<domain::ConnectionComponent> pinInstances = getAvailableConnectionPins(domainPin->getComponentInstance(), domainPin->getPin());
for(auto& pinInstance: pinInstances) {
auto& instance = pins[pinInstance];
auto rect = new QGraphicsRectItem(instance->boundingRect());
rect->setPen(selectedPen);
rect->setPos(instance->scenePos());
auto _rect = rect->rect();
_rect.setWidth(_rect.width() + 1);
_rect.setHeight(_rect.height() + 1);
rect->setRect(_rect);
context.selectable.push_back(rect);
scene.addItem(rect);
}
}
std::vector<domain::BusInstance*> Schema::getAvailableConnectionBusses(domain::ComponentInstance *instance, domain::Pin &pin) {
std::vector<domain::BusInstance*> instances;
for(const auto& bus: schema->busInstances) {
if(library->hasConnection(domain::ConnectionComponent{instance->component.getName(), pin.getName()}, bus->bus.getName())) {
instances.push_back(bus.get());
}
}
return instances;
}
std::vector<domain::ConnectionComponent> Schema::getAvailableConnectionPins(domain::ComponentInstance *instance, domain::Pin &pin) {
std::vector<domain::ConnectionComponent> instances;
domain::ConnectionComponent source{instance->component.getName(), pin.getName()};
for(const auto& entry: pins) {
std::string name = components[entry.first.component]->getComponentInstance()->component.getName();
if(library->hasConnection(source, domain::ConnectionComponent{name, entry.first.pin})) {
instances.push_back(entry.first);
}
}
return instances;
}
} // namespace display

View File

@ -4,9 +4,11 @@
#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsLineItem>
#include <comdel/domain/schema.h>
#include <comdel/domain/library.h>
#include "component_display.h"
namespace display {
@ -18,27 +20,41 @@ class Schema: public QGraphicsView
public:
QBrush selectedBrush;
QPen selectedPen;
enum State {
DEFAULT,
CREATING_CONNECTION
};
struct Context {
display::Pin *pin;
display::Pin *pin = nullptr;
QPointF startingPoint;
QGraphicsLineItem *line = nullptr;
std::vector<QGraphicsRectItem*> selectable;
};
std::map<std::string, display::ComponentGroup*> components;
std::map<std::string, display::BusGroup*> buses;
std::map<domain::ConnectionComponent, display::Pin*> pins;
State state = DEFAULT;
Context context;
Schema();
std::vector<BusConnection*> connections;
std::vector<BusConnection*> busConnections;
std::vector<DirectConnection*> directConnections;
void setSchema(domain::Schema* schema, domain::Library* library);
void updateConnections();
void removeConnectable(QPointF f);
void showConnectable(Pin *pin);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
@ -50,6 +66,10 @@ private:
domain::Schema* schema;
domain::Library* library;
std::vector<domain::BusInstance*> getAvailableConnectionBusses(domain::ComponentInstance *instance, domain::Pin &pin);
std::vector<domain::ConnectionComponent> getAvailableConnectionPins(domain::ComponentInstance *instance, domain::Pin &pin);
};
} // namespace display

View File

@ -94,6 +94,7 @@ void generateComdelFile(Schema *schema, Library &library, std::ostream &buffer)
}
void generateBus(BusInstance *bus, ostream &buffer);
void generateConnection(ConnectionInstance *connection, ostream &buffer);
void generateDisplay(Schema *schema, ostream &buffer) {
buffer << "\n\tdisplay {\n";
@ -106,9 +107,20 @@ void generateDisplay(Schema *schema, ostream &buffer) {
generateBus(bus.get(), buffer);
}
buffer << "\n";
for(auto &connection: schema->connections) {
generateConnection(connection.get(), buffer);
}
buffer << "\t}\n";
}
void generateConnection(ConnectionInstance *connection, ostream &buffer) {
buffer << "\t\tline {x1:" << connection->start.first << "; y1:" << connection->start.second << "; " <<
"x2:" << connection->end.first << "; y2:" << connection->end.second << ";}" << "\n";
}
void generateBus(BusInstance *bus, ostream &buffer) {
buffer << "\n";
buffer << "\t\t// " << bus->name << " bus\n\n";

View File

@ -63,4 +63,14 @@ bool Connection::isConnecting(ConnectionComponent component, std::string bus, Co
(this->first == secondComponent && this->bus == bus && this->second == component);
}
bool Connection::isConnecting(ConnectionComponent component, ConnectionComponent secondComponent) {
if(!second.has_value()) {
return false;
}
return (this->first == component && this->second == secondComponent) ||
(this->first == secondComponent && this->second == component);
}
} // namespace domain

View File

@ -21,6 +21,17 @@ struct ConnectionComponent
{
return !operator==(rhs);
}
bool operator<(const ConnectionComponent& rhs) const
{
if(component < rhs.component) {
return true;
} else if(component == rhs.component) {
if(pin < rhs.pin) {
return true;
}
}
return false;
}
};
class Connection
@ -40,6 +51,7 @@ public:
bool isConnecting(ConnectionComponent first);
bool isConnecting(ConnectionComponent first, std::string bus);
bool isConnecting(ConnectionComponent first, std::string bus, ConnectionComponent second);
bool isConnecting(ConnectionComponent first, ConnectionComponent second);
ConnectionComponent getComponent();

View File

@ -5,8 +5,6 @@
#include "instance.h"
#include "wireinstance.h"
namespace domain {
class ConnectionInstance
@ -19,6 +17,9 @@ public:
std::vector<InstanceAttribute> attributes;
std::pair<int, int> start;
std::pair<int, int> end;
ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, Connection connection);
InstanceAttribute getAttribute(string attribute);

View File

@ -5,7 +5,10 @@ namespace domain {
BusInstance::BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size)
: name(name), position(position), bus(bus), size(size)
{}
ComponentInstance::ComponentInstance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position, Component component)
BusInstance::BusInstance(std::string name, Bus bus): name(name), bus(bus), position(0,0), size(0) {}
ComponentInstance::ComponentInstance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position, Component component)
: name(name), attributes(std::move(attributes)), position(position), component(component)
{}

View File

@ -21,6 +21,8 @@ public:
Bus bus;
int size;
BusInstance(std::string name, Bus bus);
BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size);
virtual ~BusInstance() = default;

View File

@ -113,4 +113,18 @@ std::string Library::getMessage(std::string key) {
return messages[key];
}
bool Library::hasConnection(ConnectionComponent component, ConnectionComponent secondComponent) {
return getConnection(component, secondComponent).has_value();
}
std::optional<Connection>
Library::getConnection(ConnectionComponent component, ConnectionComponent secondComponent) {
for(auto & connection : connections) {
if(connection.isConnecting(component, secondComponent)) {
return connection;
}
}
return nullopt;
}
} // namespace domain

View File

@ -60,6 +60,10 @@ public:
bool hasConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent);
std::optional<Connection> getConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent);
bool hasConnection(ConnectionComponent component, ConnectionComponent secondComponent);
std::optional<Connection> getConnection(ConnectionComponent component, ConnectionComponent secondComponent);
};
} // namespace domain

View File

@ -9,8 +9,7 @@
namespace domain {
class Schema
{
class Schema {
public:
Schema();
@ -19,16 +18,8 @@ public:
std::vector<shared_ptr<ConnectionInstance>> connections;
BusInstance *getBusInstance(std::string& name) {
for(auto& instance: busInstances) {
if (instance->name == name) {
return instance.get();
}
}
return nullptr;
}
ComponentInstance *getComponentInstance(std::string& name) {
for(auto& instance: componentInstances) {
BusInstance *getBusInstance(std::string &name) {
for (auto &instance: busInstances) {
if (instance->name == name) {
return instance.get();
}
@ -36,8 +27,18 @@ public:
return nullptr;
}
bool hasConnection(string& component, string& pin);
ConnectionInstance* getConnection(string& component, string& pin);
ComponentInstance *getComponentInstance(std::string &name) {
for (auto &instance: componentInstances) {
if (instance->name == name) {
return instance.get();
}
}
return nullptr;
}
bool hasConnection(string &component, string &pin);
ConnectionInstance *getConnection(string &component, string &pin);
};
} // namespace domain

View File

@ -0,0 +1,53 @@
// Version 0.0.1
#include "libraries\frisc\vjezba1\FRISC.cdl"
#include "libraries\frisc\vjezba1\memory.cdl"
component System
{
clock 100MHz;
//directRam
wire INT;
//glavnaSabirnica
wire<32> ADR;
wire<32> DATA;
wire READ;
wire WRITE;
wired_and WAIT;
wired_and INT0;
wired_and INT1;
wired_and INT2;
wired_and INT3;
wire<3> SIZE;
wire --IACK;
wire --BREQ;
wire --BACK;
// components --------------------------------------------
subcomponent Memorija memorija<false, 1, 65536, 8, 0>(ADR, DATA, READ, WRITE, SIZE, WAIT, INT);
subcomponent FRISC procesor(ADR, DATA, READ, WRITE, SIZE, WAIT, INT0, INT1, INT2, INT3, --IACK, 1, *, INT) uses memorija;
display {
component { x: -12; y: 68; ref: "memorija"; }
component { x: -206; y: -76; ref: "procesor"; }
// directRam bus
// glavnaSabirnica bus
rectangle {
x: -222; y: 130;
w: 100; h: 20;
}
line {x1:-28; y1:96; x2:-90; y2:-26;}
line {x1:38; y1:52; x2:-171; y2:140;}
line {x1:-156; y1:40; x2:-171; y2:140;}
}
}

View File

@ -62,6 +62,18 @@
}
}
}
@pin memDirect inOut {
@tooltip "pin za izravno spajanje na RAM"
@connection optional("COMDEL se ne može stvoriti. FRISC nije spojen na sabirnicu")
@display {
pin {
x: 100; y: 42; w: 16; h:16;
type: "in";
orientation: "right";
}
}
}
}
@component Memorija memory {
@ -181,6 +193,29 @@
}
}
}
@pin memDirect inOut {
@tooltip "pin za spajanje na procesor"
@connection optional("COMDEL se ne može stvoriti. Memorija nije spojena na sabirnicu")
@display {
/*
pin {
x: 100; y: 25;
fillColor: blue;
lineColor: red;
fillColorConnected: white;
lineColorConnected: black;
side: right;
size: 20;
}
*/
pin {
x: -16; y: 20; w: 16; h:16;
type: "out";
orientation: "left";
}
}
}
}
@component DMA {
@ -305,6 +340,13 @@
}
}
@bus directRam automatic {
@wires {
INT
}
}
@connection (FRISC.glavniPin, glavnaSabirnica) {
@wires{ADR, DATA, READ, WRITE, SIZE, WAIT, INT0, INT1, INT2, INT3, IACK, 1, null}
}
@ -331,4 +373,9 @@
@connection (DMA.glavniPin, PIOSabirnica, FRISC.glavniPin) {
@wires{PIO_DATA, READY, STROBE}
@wires{PIO_DATA, READY, STROBE}
}
@connection (FRISC.memDirect, directRam, Memorija.memDirect) {
@wires{INT}
@wires{INT}
}

View File

@ -0,0 +1,34 @@
@source "/home/bbr/Documents/Personal/FER/schema_editor/examples/simplified FRISC model/frisc_library.csl"
@schema {
@instance memorija Memorija {
@position (-12, 68)
@attribute sinkroniziran false
@attribute brzina 1
@attribute kapacitet 65536
@attribute size 8
@attribute pocetnaAdresa 0
}
@instance procesor FRISC {
@position (-206, -76)
@attribute _memory memorija
}
@instance directRam directRam {
@position (0, 0)
@size 0
}
@instance glavnaSabirnica glavnaSabirnica {
@position (-222, 130)
@size 50
}
@connection (memorija.memDirect, directRam, procesor.memDirect) {
}
@connection (memorija.glavniPin, glavnaSabirnica) {
}
@connection (procesor.glavniPin, glavnaSabirnica) {
}
}