schema_editor/comdel/domain/display.h

174 lines
4.9 KiB
C++

#ifndef DOMAIN_DISPLAY_H
#define DOMAIN_DISPLAY_H
#include <QGraphicsItem>
#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, DisplayConfig config) : x(x), y(y), w(w), h(h), config(config) {}
void render(QGraphicsItemGroup *group);
void comdel(std::ostream &buffer, int x, int y);
};
class Ellipse {
public:
int x, y, w, h;
DisplayConfig config;
Ellipse(int x, int y, int w, int h, DisplayConfig config) : x(x), y(y), w(w), h(h), config(config) {}
void render(QGraphicsItemGroup *group);
void comdel(std::ostream &buffer, int x, int y);
};
class Line {
public:
int x1, y1, x2, y2;
DisplayConfig config;
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);
void comdel(std::ostream &buffer, int x, int y);
};
enum PinType {
IN, OUT, IN_OUT
};
enum PinOrientation {
LEFT, RIGHT, TOP, BOTTOM
};
enum BusOrientation {
VERTICAL, HORIZONTAL
};
class Bus {
public:
int x, y, w, h;
BusOrientation orientation;
DisplayConfig config;
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);
int getDefaultSize();
void comdel(std::ostream &buffer, int x, int y, int size);
};
class Pin {
public:
PinOrientation orientation;
PinType pinType;
int x, y, w, h;
DisplayConfig config;
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) {}
Pin(): config(DisplayConfig(Color(), Color())) {};
public:
void render(QGraphicsItemGroup *group);
int getConnectionX();
int getConnectionY();
private:
void renderIn(QGraphicsItemGroup *group);
void renderOut(QGraphicsItemGroup *group);
void renderInOut(QGraphicsItemGroup *group);
};
class Item {
public:
Item() : rect(std::nullopt), line(std::nullopt), pin(std::nullopt), bus(std::nullopt) {}
std::optional<Rect> rect = std::nullopt;
std::optional<Line> line = std::nullopt;
std::optional<Pin> pin = std::nullopt;
std::optional<Bus> bus = std::nullopt;
std::optional<Text> text = std::nullopt;
std::optional<Ellipse> ellipse = std::nullopt;
void render(QGraphicsItemGroup *group, ui::DisplayContext context, int size = 0);
void comdel(std::ostream &buffer, int x, int y, int size = 0);
};
}
class Display {
public:
Display(std::vector<ui::Item> items);
void render(QGraphicsItemGroup *group, ui::DisplayContext context);
void comdel(std::ostream &buffer, int x, int y, int size = 0);
std::vector<ui::Item> &getItems() { return items; }
private:
std::vector<ui::Item> items;
};
} // namespace domain
#endif // DOMAIN_DISPLAY_H