#ifndef DOMAIN_DISPLAY_H #define DOMAIN_DISPLAY_H #include #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)); } void 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"; buffer << "\t\t\tw: " << w << "; h: " << h << ";\n"; buffer << "\t\t}\n\n"; } }; 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)); } void comdel(std::ostream &buffer, int x, int y, int size) { buffer << "\t\tline {\n"; buffer << "\t\t\tx1: " << (x1 + x) << "; y1: " << (y1 + y) << ";\n"; buffer << "\t\t\tx2: " << (x2 + x) << "; y2: " << (y2 + y) << ";\n"; buffer << "\t\t}\n\n"; } }; 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, Orientation orientation, PinType pinType): x(x), y(y), w(w), h(h), orientation(orientation), pinType(pinType) {} void renderIn(QGraphicsItemGroup *group) { QPolygon polygon; switch (orientation) { case Orientation::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 Orientation::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 Orientation::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 Orientation::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; } group->addToGroup(new QGraphicsPolygonItem(polygon)); } void renderOut(QGraphicsItemGroup *group) { QPolygon polygon; switch (orientation) { case Orientation::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 Orientation::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 Orientation::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 Orientation::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; } group->addToGroup(new QGraphicsPolygonItem(polygon)); } void renderInOut(QGraphicsItemGroup *group) { group->addToGroup(new QGraphicsRectItem(x, y ,w, h)); } void 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 getConnectionX() { switch (orientation) { case ui::Orientation::TOP: case ui::Orientation::BOTTOM: return x+w/2; case ui::Orientation::LEFT: return x; case ui::Orientation::RIGHT: return x+w; } return 0; } int getConnectionY() { switch (orientation) { case ui::Orientation::LEFT: case ui::Orientation::RIGHT: return y+h/2; case ui::Orientation::TOP: return y; case ui::Orientation::BOTTOM: return y+h; } return 0; } }; 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; void comdel(std::ostream &buffer, int x, int y, int size) { if(rect) rect->comdel(buffer, x, y, size); if(line) line->comdel(buffer, x, y, size); } }; } class Display { public: Display(std::vector items); void render(QGraphicsItemGroup *group) { for(auto &item: items) { item.render(group); } } void comdel(std::ostream &buffer, int x, int y, int size) { for(auto &item: items) { item.comdel(buffer, x, y, size); } } std::vector& getItems() {return items;} private: std::vector items; }; } // namespace domain #endif // DOMAIN_DISPLAY_H