-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphicscomponent.cpp
executable file
·127 lines (97 loc) · 2.99 KB
/
graphicscomponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "graphicscomponent.h"
#include <QGraphicsTextItem>
GraphicsComponent::GraphicsComponent(AbstractScheme *parent)
{
this->setFlag(QGraphicsItem::ItemIsMovable);
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
this->parent = parent;
this->setProperty("angle", QVariant(0.0f));
}
GraphicsComponent::GraphicsComponent(AbstractScheme *parent,
ComponentInfo *info) : Component(info)
{
this->parent = parent;
this->setFlag(QGraphicsItem::ItemIsMovable);
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
this->setProperty("angle", QVariant(0.0f));
}
GraphicsComponent::~GraphicsComponent()
{
}
int GraphicsComponent::round(int num, int border)
{
int res = num;
int odd = num % border;
if (odd < (border / 2))
{
res = res - odd;
} else {
res = res + border - odd;
}
return res;
}
QPoint GraphicsComponent::calculateSnap(const QPoint &point)
{
QPoint res(round(point.x(), this->parent->getGridStep()),
round(point.y(), this->parent->getGridStep()));
return res;
}
QVariant GraphicsComponent::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change)
{
case ItemPositionChange:
{
emit itemMoved(this);
QPoint pos = value.toPoint();
if (this->parent->getSnapToGrid()){
pos = this->calculateSnap(pos);
this->setProperty("x", pos.x());
this->setProperty("y", pos.y());
return QGraphicsItem::itemChange(change, QVariant(pos));
}
this->setProperty("x", this->x());
this->setProperty("y", this->y());
}
break;
case ItemSelectedChange:
break;
default:
break;
}
return QGraphicsItem::itemChange(change, value);
}
void GraphicsComponent::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyleOptionGraphicsItem correctedOption = (*option);
QGraphicsItemGroup ::paint(painter, &correctedOption, widget);
}
void GraphicsComponent::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF pos = event->pos();
QGraphicsItem::mouseMoveEvent(event);
}
QGraphicsSvgItem *GraphicsComponent::getBody()
{
return this->body;
}
void GraphicsComponent::setProperty(const QString &name, const QVariant &value)
{
if (name == "x")
{
this->setX(value.toReal());
} else if (name == "y")
{
this->setY(value.toReal());
} else if (name == "angle")
{
this->setRotation(value.toDouble());
}
Component::setProperty(name, value);
}
QVariant GraphicsComponent::getProperty(const QString &property)
{
return Component::getProperty(property);
}