-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleitem.cpp
More file actions
175 lines (154 loc) · 5.58 KB
/
handleitem.cpp
File metadata and controls
175 lines (154 loc) · 5.58 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "handleitem.h"
#include "provariquadro.h"
#include <QPainter>
#include <QGraphicsScene>
#include <qgraphicssceneevent.h>
#include <QPointF>
#include <cmath>
#include <iostream>
HandleItem::HandleItem( ProvaRiquadro *item, QGraphicsScene *scene, QColor color, HandleItem::HandleRole role, QList<HandleItem*> handles ) : QGraphicsItem( 0, scene )
{
m_role = role;
m_color = color;
m_item = item;
m_handles = handles;
m_pressed = false;
setZValue( 100 );
setFlag( ItemIsMovable );
setFlag(ItemSendsGeometryChanges);
// std::cout<<"handler created"<<std::endl;
// std::cout<<"scene is: "<<this->scene()->objectName().toStdString()<<std::endl;
// std::cout<<"item is: "<<this->m_item->text->toPlainText().toStdString() <<std::endl;
}
void HandleItem::paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
paint->setPen( m_color );
paint->setBrush( m_color );
//std::cout<<"handler painted"<<std::endl;
QRectF rect = boundingRect();
QVector<QPointF> points;
switch( m_role )
{
case RightHandle:
points << rect.center()+QPointF(3,0) << rect.center()+QPointF(-3,-5) << rect.center()+QPointF(-3,5);
paint->drawConvexPolygon( QPolygonF(points) );
break;
case LeftHandle:
points << rect.center()+QPointF(-3,0) << rect.center()+QPointF(3,-5) << rect.center()+QPointF(3,5);
paint->drawConvexPolygon( QPolygonF(points) );
break;
case TopHandle:
points << rect.center()+QPointF(0,-3) << rect.center()+QPointF(-5,3) << rect.center()+QPointF(5,3);
paint->drawConvexPolygon( QPolygonF(points) );
break;
case BottomHandle:
points << rect.center()+QPointF(0,3) << rect.center()+QPointF(-5,-3) << rect.center()+QPointF(5,-3);
paint->drawConvexPolygon( QPolygonF(points) );
break;
}
}
QRectF HandleItem::boundingRect() const
{
switch( m_role )
{
case LeftHandle:
{
QPointF point(m_item->boundingRect().left() - pos().x(), m_item->boundingRect().top() + m_item->boundingRect().height()/2);
return QRectF( point-QPointF(3, 5), QSize( 6, 10 ) );
}
case RightHandle:
{
QPointF point(m_item->boundingRect().right() - pos().x(), m_item->boundingRect().top() + m_item->boundingRect().height()/2);
return QRectF( point-QPointF(3, 5), QSize( 6, 10 ) );
}
case TopHandle:
{
QPointF point(m_item->boundingRect().left() + m_item->boundingRect().width()/2, m_item->boundingRect().top() - pos().y());
return QRectF( point-QPointF(5, 3), QSize( 10, 6 ) );
}
case BottomHandle:
{
QPointF point(m_item->boundingRect().left() + m_item->boundingRect().width()/2, m_item->boundingRect().bottom() - pos().y());
return QRectF( point-QPointF(5, 3), QSize( 10, 6 ) );
}
}
return QRectF();
}
QVariant HandleItem::itemChange( GraphicsItemChange change, const QVariant &data )
{
if( change == ItemPositionChange && m_pressed )
{
QPointF movement;
QPointF newData = data.toPointF();
QRectF newRect = m_item->rect();
switch( m_role )
{
case LeftHandle:
{
// Snap the movement to the X direction
newData.setY(0);
movement = newData - pos();
// Resize the rectangle
// std::cout<< "rect width is: "<<m_item->rect().width() <<" and text bounding rect width is: "<<m_item->text->boundingRect().width()<<std::endl;
m_item->rect().width() < m_item->text->boundingRect().width()+20?newRect.setLeft(m_item->rect().left()-1) :newRect.setLeft(m_item->rect().left() + movement.x());
m_item->setRect(newRect);
break;
}
case RightHandle:
{
// Snap the movement to the X direction
newData.setY(0);
movement = newData - pos();
// Resize the rectangle
m_item->rect().width() + movement.x() < m_item->text->boundingRect().width()+20?newRect.setRight(m_item->rect().right()+1) :newRect.setRight(m_item->rect().right() + movement.x());
m_item->setRect(newRect);
break;
}
case TopHandle:
{
// Snap the movement to the Y direction
newData.setX(0);
movement = newData - pos();
// Resize the rectangle
m_item->rect().height() < 25?newRect.setTop(m_item->rect().top() - 1):newRect.setTop(m_item->rect().top() + movement.y());
m_item->setRect(newRect);
break;
}
case BottomHandle:
{
// Snap the movement to the Y direction
newData.setX(0);
movement = newData - pos();
// Resize the rectangle, preventing the collapse and the lock
m_item->rect().height() < 25?newRect.setBottom(m_item->rect().bottom() + 1):newRect.setBottom(m_item->rect().bottom() + movement.y());
m_item->setRect(newRect);
break;
}
} // end switch
m_item->text->setPos(m_item->boundingRect().topLeft().x()+2,m_item->boundingRect().topLeft().y()+2);
// std::cout<< "number of selected items in the end of item change: "<<this->scene()->selectedItems().size()<<std::endl;
m_item->erase_lines();
m_item->redraw_lines();
m_item->setSelected(false);
this->scene()->update(this->scene()->sceneRect());
return QGraphicsItem::itemChange( change, newData);
} // end if pressed
return QGraphicsItem::itemChange( change, data );
}
void HandleItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
m_pressed = true;
event->accept();
//QGraphicsItem::mousePressEvent( event );
}
void HandleItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
m_pressed = false;
event->accept();
QGraphicsItem::mouseReleaseEvent( event );
}
void HandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
event->accept();
QGraphicsItem::mouseMoveEvent(event);
}