-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresizer.cpp
110 lines (95 loc) · 2.58 KB
/
resizer.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
#include "resizer.hpp"
Resizer::Resizer(QGraphicsItem *crop, Position pos) : position(pos),
anchored(false), pressed(false)
{
ColorBase.setRgb(0,0,255);
ColorAnchored.setRgb(0,180,0);
ColorMousePressed.setRgb(255,0,0);
setParentItem(crop);
setFlag(ItemIsSelectable);
setAcceptHoverEvents(true);
setCursor(Qt::SizeAllCursor);
updatePosition();
}
void Resizer::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
QRectF rect = boundingRect();
QBrush brush(ColorBase);
QPen pen(QColor(0,0,0,0));
if(pressed)
brush.setColor(ColorMousePressed);
else
{
if(anchored)
brush.setColor(ColorAnchored);
else
brush.setColor(ColorBase);
}
painter-> setRenderHint (QPainter:: Antialiasing);
painter->setPen(pen);
painter->fillRect(rect, brush);
painter->drawRect(rect);
}
QRectF Resizer::boundingRect() const
{
return QRectF(0, 0, width, height);
}
void Resizer::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
pressed = true;
update();
QGraphicsItem::mousePressEvent(event);
}
void Resizer::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
pressed = false;
update();
QGraphicsItem::mouseReleaseEvent(event);
}
void Resizer::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qreal dx = event->pos().x() - event->lastPos().x();
qreal dy = event->pos().y() - event->lastPos().y();
if(position == LEFT || position == RIGHT)
moveBy(dx, 0);
else
moveBy(0, dy);
update();
emit sizeChanged(dx, dy, position);
QGraphicsItem::mouseMoveEvent(event);
}
void Resizer::updatePosition()
{
QGraphicsItem *crop = parentItem();
qreal x, y;
switch(position)
{
parentItem();
case RIGHT:
width = 4;
height = crop->boundingRect().height();
x = crop->boundingRect().width()/2;
y = -crop->boundingRect().height()/2;
break;
case LEFT:
width = 4;
height = crop->boundingRect().height();
x = -width-crop->boundingRect().width()/2;
y = -crop->boundingRect().height()/2;
break;
case UPPER:
width = crop->boundingRect().width();
height = 4;
x = -crop->boundingRect().width()/2;
y = -height-crop->boundingRect().height()/2;
break;
case BOTTOM:
width = crop->boundingRect().width();
height = 4;
x = -crop->boundingRect().width()/2;
y = crop->boundingRect().height()/2;
}
setPos(x, y);
}