From 3cdd126927c1b9e032ab7ca900e6ce77ab48c543 Mon Sep 17 00:00:00 2001 From: Borna Rajkovic Date: Thu, 7 Apr 2022 22:40:31 +0200 Subject: [PATCH] Added display parsing --- comdel/domain/comdelgenerator.cpp | 41 +++++++++++++++- comdel/domain/display.cpp | 5 +- comdel/domain/display.h | 77 ++++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 7 deletions(-) diff --git a/comdel/domain/comdelgenerator.cpp b/comdel/domain/comdelgenerator.cpp index 3259d4f..73c1f2c 100644 --- a/comdel/domain/comdelgenerator.cpp +++ b/comdel/domain/comdelgenerator.cpp @@ -190,7 +190,7 @@ std::optional ComdelGenerator::loadBus(BusNode node) } optional display; - if(Bus::REGULAR) { + if(type == Bus::REGULAR) { display = loadDisplay(*node.display); if(!display) { return nullopt; @@ -395,10 +395,47 @@ optional ComdelGenerator::loadPin(PinNode node) return Pin(name, type, tooltip, connection, *display); } +int getIntProperty(DisplayItemNode &node, std::string property) { + for(auto prop: node.values) { + if(prop.key.value == property) { + return prop.value.asInt(); + } + } +} std::optional ComdelGenerator::loadDisplay(DisplayNode node) { - return Display(); + std::vector items; + for(auto &item: node.items) { + ui::Item displayItem; + std::string type = item.type.value; + if(type == "rect") { + int x, y, w, h; + x = getIntProperty(item, "x"); + y = getIntProperty(item, "y"); + w = getIntProperty(item, "w"); + h = getIntProperty(item, "h"); + displayItem.rect = ui::Rect(x, y, w, h); + } else if(type == "line") { + int x1, y1, x2, y2; + x1 = getIntProperty(item, "x1"); + y1 = getIntProperty(item, "y1"); + x2 = getIntProperty(item, "x2"); + y2 = getIntProperty(item, "y2"); + displayItem.line = ui::Line(x1, y1, x2, y2); + } else if(type == "pin") { + int x, y, w, h; + x = getIntProperty(item, "x"); + y = getIntProperty(item, "y"); + w = getIntProperty(item, "w"); + h = getIntProperty(item, "h"); + displayItem.pin = ui::Pin(x, y, w, h); + } else { + errors.push_back(SourceError{item.type.span, "unsuported display type"}); + } + items.push_back(displayItem); + } + return Display(items); } diff --git a/comdel/domain/display.cpp b/comdel/domain/display.cpp index 6155c9c..177e8f7 100644 --- a/comdel/domain/display.cpp +++ b/comdel/domain/display.cpp @@ -2,9 +2,6 @@ namespace domain { -Display::Display() -{ - -} +Display::Display(std::vector items): items(items) {} } // namespace domain diff --git a/comdel/domain/display.h b/comdel/domain/display.h index d8c2435..54a035a 100644 --- a/comdel/domain/display.h +++ b/comdel/domain/display.h @@ -1,13 +1,88 @@ #ifndef DOMAIN_DISPLAY_H #define DOMAIN_DISPLAY_H +#include +#include namespace domain { +namespace ui { + +class Rect { +public: + int x, y, w, h; + + Rect(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} + + void render(QGraphicsItemGroup *group) { + group->addToGroup(new QGraphicsRectItem(x,y,w,h)); + } +}; + +class Line { +public: + int x1, y1, x2, y2; + + Line(int x1, int y1, int x2, int y2): x1(x1), y1(y1), x2(x2), y2(y2) {} + + void render(QGraphicsItemGroup *group) { + group->addToGroup(new QGraphicsLineItem(x1, y1, x2, y2)); + } +}; + +enum Orientation { + LEFT, RIGHT, TOP, BOTTOM +}; + +enum PinType { + IN, OUT, IN_OUT +}; + +class Pin { +public: + Orientation orientation; + PinType pinType; + int x, y, w, h; + + Pin(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} + + void render(QGraphicsItemGroup *group) { + group->addToGroup(new QGraphicsRectItem(x,y,w,h)); + } +}; + + +class Item { +public: + Item(): rect(std::nullopt), line(std::nullopt), pin(std::nullopt) {} + + void render(QGraphicsItemGroup *group) { + if(rect) rect->render(group); + if(line) line->render(group); + if(pin) pin->render(group); + } + + std::optional rect = std::nullopt; + std::optional line = std::nullopt; + std::optional pin = std::nullopt; +}; + +} + class Display { public: - Display(); + + Display(std::vector items); + + void render(QGraphicsItemGroup *group) { + for(auto &item: items) { + item.render(group); + } + } + +private: + std::vector items; }; } // namespace domain