schema_editor/comdel/domain/display.cpp

266 lines
9.3 KiB
C++
Raw Permalink Normal View History

2022-03-31 21:20:41 +00:00
#include "display.h"
2022-06-09 18:24:27 +00:00
#include <QBrush>
#include <QPen>
#include <QFont>
2022-06-09 18:24:27 +00:00
#include "comdel/domain/instance.h"
2022-03-31 21:20:41 +00:00
namespace domain {
2022-06-09 18:24:27 +00:00
2022-05-27 06:18:17 +00:00
Display::Display(std::vector<ui::Item> items) : items(items) {}
2022-03-31 21:20:41 +00:00
void Display::render(QGraphicsItemGroup *group, ui::DisplayContext context) {
2022-05-27 06:18:17 +00:00
for (auto &item: items) {
item.render(group, context);
2022-05-27 06:18:17 +00:00
}
}
void Display::comdel(std::ostream &buffer, int x, int y, int size) {
for (auto &item: items) {
item.comdel(buffer, x, y, size);
}
}
void ui::Text::render(QGraphicsItemGroup *group, ui::DisplayContext context) {
auto formattedText = context.populateMessage(text);
auto content = new QGraphicsTextItem(QString::fromStdString(formattedText));
content->setDefaultTextColor(color);
content->setPos(x, y);
content->setFont(QFont("arial", 8));
group->addToGroup(content);
}
void ui::Text::comdel(std::ostream &buffer, int x, int y) {
// TODO
}
2022-05-27 06:18:17 +00:00
void ui::Rect::render(QGraphicsItemGroup *group) {
auto rect = new QGraphicsRectItem(x, y, w, h);
rect->setPen(QPen(config.lineColor));
rect->setBrush(QBrush(config.fillColor));
group->addToGroup(rect);
2022-05-27 06:18:17 +00:00
}
void ui::Rect::comdel(std::ostream &buffer, int x, int y) {
buffer << "\t\trectangle {\n";
buffer << "\t\t\tx: " << (this->x + x) << "; y: " << (this->y + y) << ";\n";
2022-06-27 09:39:13 +00:00
buffer << "\t\t\twidth: " << w << "; height: " << h << ";\n";
buffer << "\t\t\tfill_color: " << this->config.fillColor.name().toStdString() << ";\n";
buffer << "\t\t\tline_color: " << this->config.lineColor.name().toStdString() << ";\n";
2022-05-27 06:18:17 +00:00
buffer << "\t\t}\n\n";
}
2022-06-19 18:10:44 +00:00
void ui::Ellipse::render(QGraphicsItemGroup *group) {
auto ellipse = new QGraphicsEllipseItem(x, y, w, h);
ellipse->setPen(QPen(config.lineColor));
ellipse->setBrush(QBrush(config.fillColor));
group->addToGroup(ellipse);
}
void ui::Ellipse::comdel(std::ostream &buffer, int x, int y) {
buffer << "\t\tellipse {\n";
buffer << "\t\t\tx: " << (this->x + x) << "; y: " << (this->y + y) << ";\n";
2022-06-27 09:39:13 +00:00
buffer << "\t\t\twidth: " << w << "; height: " << h << ";\n";
2022-06-19 18:10:44 +00:00
buffer << "\t\t}\n\n";
}
2022-05-27 06:18:17 +00:00
void ui::Line::render(QGraphicsItemGroup *group) {
auto line = new QGraphicsLineItem(x1, y1, x2, y2);
line->setPen(QPen(config.lineColor));
group->addToGroup(line);
2022-05-27 06:18:17 +00:00
}
void ui::Line::comdel(std::ostream &buffer, int x, int y) {
2022-06-27 09:39:13 +00:00
buffer << "\t\tpath {\n";
buffer << "\t\t\tx:0; y:0;\n";
buffer << "\t\t\tpoints: (";
buffer << "(" << (x1 + x) << ", " << (y1 + y) << "),";
buffer << "(" << (x2 - x1) << "," << (y2 - y1) << "));\n";
buffer << "\t\t}" << std::endl;
2022-05-27 06:18:17 +00:00
}
void ui::Bus::render(QGraphicsItemGroup *group, int size) {
2022-06-09 18:24:27 +00:00
int _w = w, _h = h;
2022-05-27 06:18:17 +00:00
if (orientation == HORIZONTAL) {
2022-06-09 18:24:27 +00:00
_w = size;
2022-05-27 06:18:17 +00:00
} else {
2022-06-09 18:24:27 +00:00
_h = size;
2022-05-27 06:18:17 +00:00
}
2022-06-09 18:24:27 +00:00
auto rect = new QGraphicsRectItem(x, y, _w, _h);
rect->setBrush(QBrush(config.fillColor));
rect->setPen(QPen(config.lineColor));
2022-06-09 18:24:27 +00:00
group->addToGroup(rect);
2022-05-27 06:18:17 +00:00
}
int ui::Bus::getDefaultSize() {
if (orientation == HORIZONTAL) {
return w;
} else {
return h;
}
}
void ui::Bus::comdel(std::ostream &buffer, int x, int y, int size) {
buffer << "\t\trectangle {\n";
buffer << "\t\t\tx: " << (this->x + x) << "; y: " << (this->y + y) << ";\n";
if (orientation == HORIZONTAL) {
2022-06-27 09:39:13 +00:00
buffer << "\t\t\twidth: " << size << "; height: " << h << ";\n";
2022-05-27 06:18:17 +00:00
} else {
2022-06-27 09:39:13 +00:00
buffer << "\t\t\twidth: " << w << "; height: " << size << ";\n";
2022-05-27 06:18:17 +00:00
}
2022-06-27 09:39:13 +00:00
buffer << "\t\t\tfill_color: " << this->config.fillColor.name().toStdString() << ";\n";
buffer << "\t\t\tline_color: " << this->config.lineColor.name().toStdString() << ";\n";
2022-05-27 06:18:17 +00:00
buffer << "\t\t}\n\n";
}
void ui::Pin::renderIn(QGraphicsItemGroup *group) {
QPolygon polygon;
switch (orientation) {
case PinOrientation::TOP:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w, y + h) << QPoint(x + w, y)
<< QPoint(x + w / 2, y + h / 2);
break;
case PinOrientation::BOTTOM:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w / 2, y + h / 2)
<< QPoint(x + w, y + h) << QPoint(x + w, y);
break;
case PinOrientation::LEFT:
polygon << QPoint(x, y) << QPoint(x + w / 2, y + h / 2) << QPoint(x, y + h)
<< QPoint(x + w, y + h) << QPoint(x + w, y);
break;
case PinOrientation::RIGHT:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w, y + h)
<< QPoint(x + w / 2, y + h / 2) << QPoint(x + w, y);
break;
}
auto item = new QGraphicsPolygonItem(polygon);
item->setFillRule(Qt::OddEvenFill);
item->setBrush(QBrush(config.fillColor));
item->setPen(QPen(config.lineColor));
group->addToGroup(item);
2022-05-27 06:18:17 +00:00
}
void ui::Pin::renderOut(QGraphicsItemGroup *group) {
QPolygon polygon;
switch (orientation) {
case PinOrientation::TOP:
polygon << QPoint(x, y + h / 2) << QPoint(x, y + h) << QPoint(x + w, y + h)
<< QPoint(x + w, y + h / 2) << QPoint(x + w / 2, y);
break;
case PinOrientation::BOTTOM:
polygon << QPoint(x, y) << QPoint(x, y + h / 2) << QPoint(x + w / 2, y + h)
<< QPoint(x + w, y + h / 2) << QPoint(x + w, y);
break;
case PinOrientation::LEFT:
polygon << QPoint(x + w, y) << QPoint(x + w / 2, y) << QPoint(x, y + h / 2)
<< QPoint(x + w / 2, y + h) << QPoint(x + w, y + w);
break;
case PinOrientation::RIGHT:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w / 2, y + h)
<< QPoint(x + w, y + h / 2) << QPoint(x + w / 2, y);
break;
}
auto item = new QGraphicsPolygonItem(polygon);
item->setFillRule(Qt::OddEvenFill);
item->setBrush(QBrush(config.fillColor));
item->setPen(QPen(config.lineColor));
group->addToGroup(item);
2022-05-27 06:18:17 +00:00
}
void ui::Pin::renderInOut(QGraphicsItemGroup *group) {
auto rect = new QGraphicsRectItem(x, y, w, h);
rect->setBrush(QBrush(config.fillColor));
rect->setPen(QPen(config.lineColor));
group->addToGroup(rect);
2022-05-27 06:18:17 +00:00
}
void ui::Pin::render(QGraphicsItemGroup *group) {
switch (pinType) {
case PinType::IN:
renderIn(group);
break;
case PinType::OUT:
renderOut(group);
break;
case PinType::IN_OUT:
renderInOut(group);
break;
}
}
int ui::Pin::getConnectionX() {
switch (orientation) {
case ui::PinOrientation::TOP:
case ui::PinOrientation::BOTTOM:
return x + w / 2;
case ui::PinOrientation::LEFT:
return x;
case ui::PinOrientation::RIGHT:
return x + w;
}
return 0;
}
int ui::Pin::getConnectionY() {
switch (orientation) {
case ui::PinOrientation::LEFT:
case ui::PinOrientation::RIGHT:
return y + h / 2;
case ui::PinOrientation::TOP:
return y;
case ui::PinOrientation::BOTTOM:
return y + h;
}
return 0;
}
void ui::Item::render(QGraphicsItemGroup *group, ui::DisplayContext context, int size) {
2022-05-27 06:18:17 +00:00
if (rect) rect->render(group);
if (line) line->render(group);
if (pin) pin->render(group);
if (bus) bus->render(group, size);
if (text) text->render(group, context);
2022-06-19 18:10:44 +00:00
if (ellipse) ellipse->render(group);
2022-05-27 06:18:17 +00:00
}
void ui::Item::comdel(std::ostream &buffer, int x, int y, int size) {
if (rect) rect->comdel(buffer, x, y);
if (line) line->comdel(buffer, x, y);
// pins aren't exported
if (bus) bus->comdel(buffer, x, y, size);
// text currently isn't exported TODO
}
ui::DisplayContext::DisplayContext(ComponentInstance *instance) {
for(auto attr: instance->attributes) {
this->values[attr.name] = attr.value;
}
this->values["instanceName"] = Value::fromString(instance->name);
}
string replacePlaceholder(string source, string key, Value value) {
key = "{" + key + "}";
auto placeholderValue = value.string();
auto found = source.find(key);
while (found != string::npos) {
source.replace(found, key.length(), placeholderValue);
found = source.find(key);
}
return source;
}
std::string ui::DisplayContext::populateMessage(std::string source) {
for (auto &[key, value]: values) {
source = replacePlaceholder(source, key, value);
}
return source;
2022-05-27 06:18:17 +00:00
}
2022-03-31 21:20:41 +00:00
} // namespace domain