-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.cpp
79 lines (66 loc) · 1.7 KB
/
item.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
#include "item.h"
#include "scene.h"
void Item::drawPixel(int x, int y)
{
drawPixel(x, y, path);
}
void Item::drawPixel(const QPoint &p)
{
drawPixel(p.x(), p.y(), path);
}
void Item::drawPixel(int x, int y, QPainterPath &painterPath)
{
const QPoint scenePos = toScenePos({x, y});
const int thickness = this->scene->getThickness();
if (scenePos.x() < 0 || scenePos.x() > this->scene->width() ||
scenePos.y() < 0 || scenePos.y() > this->scene->height()) return;
painterPath.addRect(QRect(scenePos, QSize(thickness, thickness)));
}
void Item::drawPixel(const QPoint &p, QPainterPath &painterPath)
{
drawPixel(p.x(), p.y(), painterPath);
}
QPoint Item::toScenePos(const QPoint &userPos) const
{
return scene->toScenePos(userPos);
}
void Item::setBrush(const QBrush &value)
{
brush = value;
scene->update();
}
QColor Item::getColor() const
{
return brush.color();
}
Item::Item(Scene *scene, QGraphicsItem *parent)
: QGraphicsItem(parent), scene(scene)
{
setFlags(ItemIsSelectable | ItemSendsScenePositionChanges);
}
QColor Item::getFillColor() const
{
return Qt::color0;
}
void Item::reDraw(){
QGraphicsItem::prepareGeometryChange();
}
QPainterPath Item::shape() const{
return path;
}
void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// setSelected(true);
QGraphicsItem::mousePressEvent(event);
}
void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->fillPath(path, brush);
if (isSelected()) {
painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
painter->setOpacity(0.5);
painter->drawRect(boundingRect());
}
}