-
Notifications
You must be signed in to change notification settings - Fork 0
/
provariquadro.cpp
261 lines (242 loc) · 8.72 KB
/
provariquadro.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "provariquadro.h"
#include <iostream>
#include <QGraphicsWidget>
#include <QGraphicsSceneMouseEvent>
#include <QCursor>
#include <qitemselectionmodel.h>
#include "handleitem.h"
ProvaRiquadro::ProvaRiquadro()
{
setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
text = new QGraphicsTextItem(this);
//->text->setTextInteractionFlags(Qt::TextEditorInteraction);
text->setPos(5,5);
text->setPlainText("Test");
rekt = QRect (0,0,60,30);
brush = QBrush(Qt::gray);
arrow_target = false;
HandleItem *topHandle = new HandleItem( this, (this->scene()), Qt::red, HandleItem::TopHandle );
HandleItem *rightHandle = new HandleItem( this, this->scene(), Qt::red, HandleItem::RightHandle );
HandleItem *leftHandle = new HandleItem( this, this->scene(), Qt::red, HandleItem::LeftHandle );
HandleItem *bottomHandle = new HandleItem( this, this->scene(), Qt::red, HandleItem::BottomHandle );
HandleItem *centerHandle = new HandleItem( this, this->scene(), Qt::red, HandleItem::CenterHandle, QList<HandleItem*>() << topHandle << rightHandle << leftHandle << bottomHandle );
topHandle->setParentItem(this);
rightHandle->setParentItem(this);
leftHandle->setParentItem(this);
bottomHandle->setParentItem(this);
centerHandle->setParentItem(this);
// this->scene()->addItem(topHandle);
//QItemSelectionModel::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
}
void ProvaRiquadro::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
std::cout << "mouseDoubleClickEvent"<<std::endl;
this->setSelected(false);
emit riquadroDoubleClick();
// QGraphicsItem::mouseDoubleClickEvent(event);
}
void ProvaRiquadro::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
// advance();
being_moved=true;
std::cout << "mousemoveEvent"<<std::endl;
std::cerr<<"selected items are: "<<this->scene()->selectedItems().size()<<std::endl;
erase_lines();
event->accept();
QGraphicsItem::mouseMoveEvent(event);
//std::cout <<"bugseeker"<<std::endl;
redraw_lines();
// this->setSelected(false);
emit riquadroMosso();
}
void ProvaRiquadro::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
std::cout << "mouseClickEvent("<<event->pos().x()<<","<<event->pos().y()<<")"<<std::endl;
std::cout <<"rect top right is: "<<rect().topRight().x()<<","<<rect().topRight().y() << " and left bot is: "<<rect().bottomLeft().x()<<","<<rect().bottomLeft().y()<<std::endl;
this->setSelected(true);
std::cout<<this->text->toPlainText().toStdString()<<std::endl;
QPointF p = mapToScene(event->pos());
QPoint p1 = QCursor::pos();
std::cout<<p.x()<<","<<p.y()<<std::endl;
std::cout<<p1.x()<<","<<p1.y()<<std::endl;
std::cout<<"widget"<<this->parentWidget()<<std::endl;
std::cout<<"object"<<this->parentObject()<<std::endl;
this->setObjectName("cliked rectangle");
if(event->button() == Qt::LeftButton)
{
#if 0
std::cerr<<"selected items are: "<<this->scene()->selectedItems().size()<<std::endl;
foreach(QGraphicsItem* selectedItem, this->scene()->selectedItems())
{
std::cout<<selectedItem->type()<<std::endl;
if (selectedItem->type() == QGraphicsItem::UserType)
{
ProvaRiquadro* tmp = (ProvaRiquadro*)selectedItem;
std::cout << tmp->text->toPlainText().toStdString()<<std::endl;
//tmp->setSelected(false);
}
// perform action with selectedItem
}
this->setSelected(false);
std::cerr<<"selected items are: "<<this->scene()->selectedItems().size()<<std::endl;
std::cout << "no more seeleected"<<std::endl;
#endif
emit riquadroCliccatoSx();
}
else if (event->button() == Qt::RightButton)
emit riquadroCliccatoDx();
//QGraphicsItem::mousePressEvent(event);
}
void ProvaRiquadro::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
std::cout << "mousereleeaseEvent"<<std::endl;
QGraphicsItem::mouseReleaseEvent(event);
//event->accept();
std::cout<<"selected items are: "<<this->scene()->selectedItems().size()<<std::endl;
if(being_moved || arrow_target)
{
this->setSelected(false);
std::cout<<"selected items are: "<<this->scene()->selectedItems().size()<<std::endl;
being_moved = false;
arrow_target = false;
}
std::cout<<"release end"<<std::endl;
}
void ProvaRiquadro::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
QRectF rec = boundingRect();
painter->fillRect(rec,brush);
painter->drawRect(rec);
}
void ProvaRiquadro::advance()
{
QPointF location = this->pos();
std::cout<<"("<<location.x() << "," << location.y()<<")"<<std::endl;
//line update.
}
QRectF ProvaRiquadro::boundingRect() const
{
return rekt;
}
QRectF ProvaRiquadro::rect()
{
return rekt;
}
void ProvaRiquadro::setRect(QRectF rect)
{
rekt = rect;
//paint();//??????????
}
void ProvaRiquadro::provaselected()
{
if(this->isSelected())
std::cout <<"provaselected: "<< this->text->toPlainText().toStdString() <<" is selected"<<std::endl;
else
std::cout <<"provaselected: "<< this->text->toPlainText().toStdString() <<" is not selected"<<std::endl;
}
/*QVariant ProvaRiquadro::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
{
text->setPos(rekt.center());
return QGraphicsItem::itemChange(change, value);
}
*/
/*
QVariant ProvaRiquadro::itemChange(GraphicsItemChange change, const QVariant & value){
text->setPos(rekt.center());
}*/
/*
void ProvaRiquadro::update_lines(){
QPen blackpen = QPen(Qt::black);
for (std::map<ProvaRiquadro*,Arrow*>::iterator it = arriving_lines.begin(); it!= arriving_lines.end();++it)
{
//draw new line.
std::cout <<"bugseeker2"<<std::endl;
ProvaRiquadro* tmp = (*it).first;
tmp->scene()->removeItem(tmp->starting_lines.at(this));
QPointF* starting_point = new QPointF(tmp->x()+tmp->boundingRect().center().x(),
tmp->y()+tmp->boundingRect().bottom());
QPointF* arrival_point = new QPointF (this->x()+this->boundingRect().center().x(),
this->y()+this->boundingRect().top());
QLineF newline(*starting_point,*arrival_point);
QGraphicsLineItem* curr_line_item = this->scene()->addLine(newline,blackpen);
this->arriving_lines.at(tmp) = curr_line_item;
tmp->starting_lines.at(this) = curr_line_item;
}
for (std::map<ProvaRiquadro*,Arrow*>::iterator it = starting_lines.begin(); it!= starting_lines.end();++it)
{
//draw new line.
std::cout <<"bugseeker3"<<std::endl;
ProvaRiquadro* tmp = (*it).first;
tmp->scene()->removeItem(tmp->arriving_lines.at(this));
QPointF* starting_point = new QPointF(tmp->x()+tmp->boundingRect().center().x(),
tmp->y()+tmp->boundingRect().bottom());
QPointF* arrival_point = new QPointF (this->x()+this->boundingRect().center().x(),
this->y()+this->boundingRect().top());
QLineF newline(*starting_point,*arrival_point);
QGraphicsLineItem* curr_line_item = this->scene()->addLine(newline,blackpen);
this->starting_lines.at(tmp) = curr_line_item;
tmp->arriving_lines.at(this) = curr_line_item;
}
}
*/
void ProvaRiquadro::setLayer(int i)
{
switch (i)
{
case 0:
{
brush = QBrush(Qt::yellow);
break;
}
case 1:
{
brush = QBrush(Qt::magenta);
break;
}
case 2:
{
brush = QBrush(Qt::cyan);
break;
}
case 3:
{
brush = QBrush(Qt::blue);
break;
}
case 4:
{
brush = QBrush(Qt::green);
break;
}
default:
{
brush = QBrush(Qt::gray);
break;
}
}
}
void ProvaRiquadro::erase_lines()
{
}
void ProvaRiquadro::redraw_lines()
{
std::cout<<"enteer redraw lines function"<<std::endl;
for (std::map<ProvaRiquadro*,Arrow*>::iterator it = starting_lines.begin(); it!= starting_lines.end();++it)
{
Arrow* tmp = (*it).second;
std::cout<<"tmp assigned: "<<tmp<<std::endl;
tmp->update();
}
for (std::map<ProvaRiquadro*,Arrow*>::iterator it = arriving_lines.begin(); it!= arriving_lines.end();++it)
{
Arrow* tmp = (*it).second;
std::cout<<"tmp assigned: "<<tmp<<std::endl;
tmp->update();
}
}
void ProvaRiquadro::setArrowTarget()
{
std::cout<<"target set"<<std::endl;
arrow_target=true;
}