Added dynamic color support + text display item
This commit is contained in:
1 parent
e6bbaabde4
commit
09991904e9
13 files changed
+265
-36
No files matched your search
+72
-13
@@ -2,17 +2,17 @@
|
||||
|
||||
#include <QBrush>
|
||||
#include <QPen>
|
||||
#include <QFont>
|
||||
|
||||
#include "comdel/domain/instance.h"
|
||||
|
||||
namespace domain {
|
||||
|
||||
QBrush busBrush(QColor::fromRgb(200, 200, 200));
|
||||
QPen busPen(QColor::fromRgb(200, 200, 200));
|
||||
|
||||
Display::Display(std::vector<ui::Item> items) : items(items) {}
|
||||
|
||||
void Display::render(QGraphicsItemGroup *group) {
|
||||
void Display::render(QGraphicsItemGroup *group, ui::DisplayContext context) {
|
||||
for (auto &item: items) {
|
||||
item.render(group);
|
||||
item.render(group, context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,26 @@ namespace domain {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
void ui::Rect::render(QGraphicsItemGroup *group) {
|
||||
group->addToGroup(new QGraphicsRectItem(x, y, w, h));
|
||||
auto rect = new QGraphicsRectItem(x, y, w, h);
|
||||
rect->setPen(QPen(config.lineColor));
|
||||
rect->setBrush(QBrush(config.fillColor));
|
||||
group->addToGroup(rect);
|
||||
}
|
||||
|
||||
void ui::Rect::comdel(std::ostream &buffer, int x, int y) {
|
||||
@@ -34,7 +52,9 @@ namespace domain {
|
||||
}
|
||||
|
||||
void ui::Line::render(QGraphicsItemGroup *group) {
|
||||
group->addToGroup(new QGraphicsLineItem(x1, y1, x2, y2));
|
||||
auto line = new QGraphicsLineItem(x1, y1, x2, y2);
|
||||
line->setPen(QPen(config.lineColor));
|
||||
group->addToGroup(line);
|
||||
}
|
||||
|
||||
void ui::Line::comdel(std::ostream &buffer, int x, int y) {
|
||||
@@ -52,8 +72,8 @@ namespace domain {
|
||||
_h = size;
|
||||
}
|
||||
auto rect = new QGraphicsRectItem(x, y, _w, _h);
|
||||
rect->setBrush(busBrush);
|
||||
rect->setPen(busPen);
|
||||
rect->setBrush(QBrush(config.fillColor));
|
||||
rect->setPen(QPen(config.lineColor));
|
||||
group->addToGroup(rect);
|
||||
}
|
||||
|
||||
@@ -97,7 +117,11 @@ namespace domain {
|
||||
<< QPoint(x + w / 2, y + h / 2) << QPoint(x + w, y);
|
||||
break;
|
||||
}
|
||||
group->addToGroup(new QGraphicsPolygonItem(polygon));
|
||||
auto item = new QGraphicsPolygonItem(polygon);
|
||||
item->setFillRule(Qt::OddEvenFill);
|
||||
item->setBrush(QBrush(config.fillColor));
|
||||
item->setPen(QPen(config.lineColor));
|
||||
group->addToGroup(item);
|
||||
}
|
||||
|
||||
void ui::Pin::renderOut(QGraphicsItemGroup *group) {
|
||||
@@ -121,11 +145,18 @@ namespace domain {
|
||||
<< QPoint(x + w, y + h / 2) << QPoint(x + w / 2, y);
|
||||
break;
|
||||
}
|
||||
group->addToGroup(new QGraphicsPolygonItem(polygon));
|
||||
auto item = new QGraphicsPolygonItem(polygon);
|
||||
item->setFillRule(Qt::OddEvenFill);
|
||||
item->setBrush(QBrush(config.fillColor));
|
||||
item->setPen(QPen(config.lineColor));
|
||||
group->addToGroup(item);
|
||||
}
|
||||
|
||||
void ui::Pin::renderInOut(QGraphicsItemGroup *group) {
|
||||
group->addToGroup(new QGraphicsRectItem(x, y, w, h));
|
||||
auto rect = new QGraphicsRectItem(x, y, w, h);
|
||||
rect->setBrush(QBrush(config.fillColor));
|
||||
rect->setPen(QPen(config.lineColor));
|
||||
group->addToGroup(rect);
|
||||
}
|
||||
|
||||
void ui::Pin::render(QGraphicsItemGroup *group) {
|
||||
@@ -168,11 +199,12 @@ namespace domain {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ui::Item::render(QGraphicsItemGroup *group, int size) {
|
||||
void ui::Item::render(QGraphicsItemGroup *group, ui::DisplayContext context, int size) {
|
||||
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);
|
||||
}
|
||||
|
||||
void ui::Item::comdel(std::ostream &buffer, int x, int y, int size) {
|
||||
@@ -180,5 +212,32 @@ namespace domain {
|
||||
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;
|
||||
}
|
||||
} // namespace domain
|
||||
+49
-9
@@ -5,16 +5,52 @@
|
||||
#include <optional>
|
||||
|
||||
#include <ostream>
|
||||
#include "comdel/parser/color.h"
|
||||
#include "value.h"
|
||||
|
||||
namespace domain {
|
||||
|
||||
namespace ui {
|
||||
|
||||
class DisplayContext {
|
||||
std::map<std::string, domain::Value> values;
|
||||
|
||||
public:
|
||||
explicit DisplayContext(ComponentInstance *instance);
|
||||
std::string populateMessage(std::string message);
|
||||
};
|
||||
|
||||
struct DisplayConfig {
|
||||
QColor lineColor;
|
||||
QColor fillColor;
|
||||
|
||||
DisplayConfig(Color lineColor, Color fillColor) {
|
||||
this->lineColor = QColor::fromRgb(lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
||||
this->fillColor = QColor::fromRgb(fillColor.r, fillColor.g, fillColor.b, fillColor.a);
|
||||
}
|
||||
};
|
||||
|
||||
class Text {
|
||||
public:
|
||||
int x, y, w, h;
|
||||
QColor color;
|
||||
std::string text;
|
||||
|
||||
Text(int x, int y, int w, int h, std::string text, Color color) : x(x), y(y), w(w), h(h), text(text),
|
||||
color(QColor::fromRgb(color.r, color.g, color.b, color.a)) {}
|
||||
|
||||
void render(QGraphicsItemGroup *group, ui::DisplayContext context);
|
||||
|
||||
void comdel(std::ostream &buffer, int x, int y);
|
||||
|
||||
};
|
||||
|
||||
class Rect {
|
||||
public:
|
||||
int x, y, w, h;
|
||||
DisplayConfig config;
|
||||
|
||||
Rect(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
|
||||
Rect(int x, int y, int w, int h, DisplayConfig config) : x(x), y(y), w(w), h(h), config(config) {}
|
||||
|
||||
void render(QGraphicsItemGroup *group);
|
||||
|
||||
@@ -25,8 +61,9 @@ namespace domain {
|
||||
class Line {
|
||||
public:
|
||||
int x1, y1, x2, y2;
|
||||
DisplayConfig config;
|
||||
|
||||
Line(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
|
||||
Line(int x1, int y1, int x2, int y2, DisplayConfig config) : x1(x1), y1(y1), x2(x2), y2(y2), config(config) {}
|
||||
|
||||
void render(QGraphicsItemGroup *group);
|
||||
|
||||
@@ -47,9 +84,11 @@ namespace domain {
|
||||
public:
|
||||
int x, y, w, h;
|
||||
BusOrientation orientation;
|
||||
DisplayConfig config;
|
||||
|
||||
Bus(int x, int y, int w, int h, BusOrientation orientation) : x(x), y(y), w(w), h(h),
|
||||
orientation(orientation) {}
|
||||
Bus(int x, int y, int w, int h, BusOrientation orientation, DisplayConfig config) : x(x), y(y), w(w), h(h),
|
||||
orientation(orientation),
|
||||
config(config) {}
|
||||
|
||||
void render(QGraphicsItemGroup *group, int size);
|
||||
|
||||
@@ -64,10 +103,10 @@ namespace domain {
|
||||
PinOrientation orientation;
|
||||
PinType pinType;
|
||||
int x, y, w, h;
|
||||
DisplayConfig config;
|
||||
|
||||
Pin(int x, int y, int w, int h, PinOrientation orientation, PinType pinType) : x(x), y(y), w(w), h(h),
|
||||
orientation(orientation),
|
||||
pinType(pinType) {}
|
||||
Pin(int x, int y, int w, int h, PinOrientation orientation, PinType pinType, DisplayConfig config)
|
||||
: x(x), y(y), w(w), h(h), orientation(orientation), pinType(pinType), config(config) {}
|
||||
|
||||
public:
|
||||
void render(QGraphicsItemGroup *group);
|
||||
@@ -90,8 +129,9 @@ namespace domain {
|
||||
std::optional<Line> line = std::nullopt;
|
||||
std::optional<Pin> pin = std::nullopt;
|
||||
std::optional<Bus> bus = std::nullopt;
|
||||
std::optional<Text> text = std::nullopt;
|
||||
|
||||
void render(QGraphicsItemGroup *group, int size = 0);
|
||||
void render(QGraphicsItemGroup *group, ui::DisplayContext context, int size = 0);
|
||||
void comdel(std::ostream &buffer, int x, int y, int size = 0);
|
||||
};
|
||||
|
||||
@@ -102,7 +142,7 @@ namespace domain {
|
||||
|
||||
Display(std::vector<ui::Item> items);
|
||||
|
||||
void render(QGraphicsItemGroup *group);
|
||||
void render(QGraphicsItemGroup *group, ui::DisplayContext context);
|
||||
|
||||
void comdel(std::ostream &buffer, int x, int y, int size = 0);
|
||||
|
||||
|
||||
@@ -615,20 +615,34 @@ namespace domain {
|
||||
for (auto &item: node.items) {
|
||||
ui::Item displayItem;
|
||||
std::string type = item.type.value;
|
||||
if (type == "rect") {
|
||||
|
||||
auto fillColor = item.asColor(&errors, "fillColor", Color(255, 255, 255, 255));
|
||||
auto lineColor = item.asColor(&errors, "lineColor", Color(0, 0, 0, 255));
|
||||
|
||||
if (type == "text") {
|
||||
long long int x, y, w, h;
|
||||
std::string text;
|
||||
auto color = item.asColor(&errors, "color", Color(0, 0, 0, 255));
|
||||
x = item.asInt(&errors, "x");
|
||||
y = item.asInt(&errors, "y");
|
||||
w = item.asInt(&errors, "w");
|
||||
h = item.asInt(&errors, "h");
|
||||
text = item.asString(&errors, "text");
|
||||
displayItem.text = ui::Text(x, y, w, h, text, color);
|
||||
} else if (type == "rect") {
|
||||
long long int x, y, w, h;
|
||||
x = item.asInt(&errors, "x");
|
||||
y = item.asInt(&errors, "y");
|
||||
w = item.asInt(&errors, "w");
|
||||
h = item.asInt(&errors, "h");
|
||||
displayItem.rect = ui::Rect(x, y, w, h);
|
||||
displayItem.rect = ui::Rect(x, y, w, h, {lineColor, fillColor});
|
||||
} else if (type == "line") {
|
||||
long long int x1, y1, x2, y2;
|
||||
x1 = item.asInt(&errors, "x1");
|
||||
y1 = item.asInt(&errors, "y1");
|
||||
x2 = item.asInt(&errors, "x2");
|
||||
y2 = item.asInt(&errors, "y2");
|
||||
displayItem.line = ui::Line(x1, y1, x2, y2);
|
||||
displayItem.line = ui::Line(x1, y1, x2, y2, {lineColor, fillColor});
|
||||
} else if (type == "pin") {
|
||||
long long int x, y, w, h;
|
||||
x = item.asInt(&errors, "x");
|
||||
@@ -662,7 +676,7 @@ namespace domain {
|
||||
errors.emplace_back(item.span, "unknown pin type '" + _pinType + "'");
|
||||
}
|
||||
|
||||
displayItem.pin = ui::Pin(x, y, w, h, orientation, pinType);
|
||||
displayItem.pin = ui::Pin(x, y, w, h, orientation, pinType, {lineColor, fillColor});
|
||||
} else if (type == "bus") {
|
||||
long long int x, y, w, h;
|
||||
x = item.asInt(&errors, "x");
|
||||
@@ -680,7 +694,7 @@ namespace domain {
|
||||
errors.emplace_back(item.span, "unknown bus orientation type '" + _orientation + "'");
|
||||
}
|
||||
|
||||
displayItem.bus = ui::Bus(x, y, w, h, orientation);
|
||||
displayItem.bus = ui::Bus(x, y, w, h, orientation, {lineColor, fillColor});
|
||||
} else {
|
||||
errors.emplace_back(item.type.span, "unsupported display type");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user