-
Notifications
You must be signed in to change notification settings - Fork 0
/
prova_widget.cpp
371 lines (316 loc) · 14.4 KB
/
prova_widget.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "prova_widget.h"
#include "ui_prova_widget.h"
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/math/constants/constants.hpp>
#include <QTimer>
#include "portselect.h"
prova_widget::prova_widget(QWidget* parent): QWidget(parent), ui(new Ui::prova_widget)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(Layer_1_press_event()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(Layer_2_press_event()));
scene = new QGraphicsScene(this);
scene->setObjectName("scene");
ui->graphicsView->setScene(scene);
ui->graphicsView->setObjectName("graphicview");
scene->setParent(ui->graphicsView);
is_drawing = false;
starting_object = nullptr;
selected_arrow = nullptr;
arrows.reset(new std::map<QGraphicsLineItem*,Arrow*>);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),this, SLOT(redraw_line()));
timer->start(100);
}
prova_widget::~prova_widget(){
delete ui;
}
void prova_widget::Layer_1_press_event()
{
popup.reset(new ProvaPopup());
connect(popup.get(),SIGNAL(accepted()),this,SLOT(create_L1_obj()));
connect(popup.get(),SIGNAL(rejected()),this,SLOT(no_data()));
popup->exec();
popup->activateWindow();
std::cout<<"layer 1 press event triggered"<<std::endl;
}
void prova_widget::Layer_2_press_event()
{
porte.reset(new ProvaPorte());
connect(porte.get(),SIGNAL(accepted()),this,SLOT(create_L2_obj()));
connect(porte.get(),SIGNAL(rejected()),this,SLOT(no_data()));
porte->exec();
porte->activateWindow();
std::cout<<"layer 1 press event triggered"<<std::endl;
}
void prova_widget::no_data()
{
std::cout <<"no data provided in the popup"<<std::endl;
}
void prova_widget::create_L1_obj()
{
//dato che non fa la new, quando faccio reset di popup elimina il precedente e quindi segfaulta.
//risolvere con smartptr, nel momento in cui si perfeziona il tutto. ora puoi sbattertene le palle :D
//e procedere domattina con l'elimina
L4_Data* data = popup->get_data();
std::cout<<data->name<<std::endl;
ProvaRiquadro *temp= new ProvaRiquadro();
temp->setLayer(0);
scene->addItem(temp);
connect(temp,SIGNAL(riquadroCliccatoSx()),this,SLOT(component_clicked()));
connect(temp,SIGNAL(riquadroMosso()),this,SLOT(break_line_drawing()));
connect(temp,SIGNAL(riquadroCliccatoDx()),this,SLOT(break_line_drawing()));
connect(temp,SIGNAL(riquadroDoubleClick()),this,SLOT(update_L1_object()));
connect(scene,SIGNAL(selectionChanged()),temp,SLOT(provaselected()));
temp->text->setPlainText(QString::fromStdString(data->name));
graphical_to_data.insert(std::make_pair(temp,data));
std::cout<<scene->parent()->objectName().toStdString()<<std::endl;
std::cout<<ui->graphicsView->parentWidget()->objectName().toStdString()<<std::endl;
std::cout<<ui->scrollAreaWidgetContents->parentWidget()->objectName().toStdString()<<std::endl;
//temp->show();
//temp->setText(QString::fromStdString(boost::lexical_cast<std::string>(temp)));
//connect(temp, SIGNAL(clicked(bool)), this, SLOT(component_clicked()));
//std::cout<<"before: "<< ui->scrollAreaWidgetContents->size().width()<<std::endl;
//std::cout<<ui->scrollAreaWidgetContents->size().height()<<std::endl;
// scene->addWidget(temp);
// ui->scrollAreaWidgetContents->adjustSize();
// std::cout<<"after:" <<ui->scrollAreaWidgetContents->size().width()<<std::endl;
//std::cout<<ui->scrollAreaWidgetContents->size().height()<<std::endl;
//update();
}
void prova_widget::create_L2_obj() //NTS sono praticamente uguali se non fosse per l'updateL2. e il fatto che usa un qdialog diverso. se anche update L2 resta simile prova a pensare funzione univoca.
{
L4_Data* data = porte->get_data();
std::cout<<data->name<<std::endl;
ProvaRiquadro *temp= new ProvaRiquadro();
temp->setLayer(1);
scene->addItem(temp);
connect(temp,SIGNAL(riquadroCliccatoSx()),this,SLOT(component_clicked()));
connect(temp,SIGNAL(riquadroMosso()),this,SLOT(break_line_drawing()));
connect(temp,SIGNAL(riquadroCliccatoDx()),this,SLOT(break_line_drawing()));
connect(temp,SIGNAL(riquadroDoubleClick()),this,SLOT(update_L2_object()));
connect(scene,SIGNAL(selectionChanged()),temp,SLOT(provaselected()));
connect(ui->Delete_Button,SIGNAL(clicked()),this, SLOT(delete_items()));
temp->text->setPlainText(QString::fromStdString(data->name));
graphical_to_data.insert(std::make_pair(temp,data));
std::cout<<scene->parent()->objectName().toStdString()<<std::endl;
std::cout<<ui->graphicsView->parentWidget()->objectName().toStdString()<<std::endl;
std::cout<<ui->scrollAreaWidgetContents->parentWidget()->objectName().toStdString()<<std::endl;
}
void prova_widget::component_clicked()
{
std::cout<<"component_clicked"<<std::endl;
QPoint p1 = QCursor::pos();
QPoint p2 = ui->graphicsView->mapFromGlobal(p1);
QPointF p = ui->graphicsView->mapToScene(p2);
QGraphicsItem* item_tmp = scene->itemAt(p.x()-3, p.y()-3); //NON HO ANCORA CAPITO XK SI PERDA 3px nella conversione.
//che poi la domanda fondamentale è sono 3 qua e x altrove? xk in tal caso il fix non funziona...
// QPoint p = ui->frame_6->mapFromGlobal(QCursor::pos());
std::cout<<"item catched"<<std::endl;
std::cout<<"item catched"<<item_tmp->type() <<std::endl;
std::cout<<"item catched"<<std::endl;
QPen blackpen = QPen(Qt::black);
if (item_tmp->type() != ProvaRiquadro::ProvaRiquadro_type) //qtextgraphicitem
{
std::cout<<"item is not riquadro"<<std::endl;
QList<QGraphicsItem*> items;
QPointF point = QPointF(p.x()-3, p.y()-3);
items = scene->items(point, Qt::IntersectsItemShape,Qt::AscendingOrder,QTransform());
while (item_tmp->type() != ProvaRiquadro::ProvaRiquadro_type || items.size()!= 0)
{
std::cout<<"item is not riquadro size:"<<items.size()<<std::endl;
item_tmp = items.at(items.size()-1);
items.removeLast();
}
if (item_tmp->type() != ProvaRiquadro::ProvaRiquadro_type)
throw std::runtime_error("click su riquadro non trova oggetto");
}
ProvaRiquadro* item = qgraphicsitem_cast<ProvaRiquadro*>(item_tmp);
std::cout<<"item is: "<<item<<std::endl;
std::cout<<"item is: "<<item->text->toPlainText().toStdString()<<std::endl;
if (item != nullptr && item != 0)
{
if(is_drawing)
{
is_drawing = false;
scene->removeItem(curr_line_item);
if(item->starting_lines.count(starting_object)==0)
{
std::cout<<"test: precall di portselect"<<std::endl;
arrival_object = item;
std::cout<<"arrival set"<<std::endl;
// delete ps;
ps.reset(new PortSelect());
std::cout<<"reset done"<<std::endl;
ps->set_data(graphical_to_data.at(starting_object),graphical_to_data.at(arrival_object));
std::cout<<"data set set"<<std::endl;
connect(ps.get(),SIGNAL(accepted()),this,SLOT(finalize_line()));
connect(ps.get(),SIGNAL(rejected()),this,SLOT(break_line_drawing()));
std::cout<<"connection set"<<std::endl;
ps->exec();
ps->activateWindow();
std::cout<<"test: postcall di portselect"<<std::endl;
}
std::cerr<<"selecteditemscleardone"<<scene->selectedItems().size()<<std::endl;
}
else
{
is_drawing = true;
starting_object = item;
QPoint p1 = QCursor::pos();
QPoint p2 = ui->graphicsView->mapFromGlobal(p1);
QPointF p = ui->graphicsView->mapToScene(p2);
QLineF newline(item->x()+item->boundingRect().center().x(),item->y()+item->boundingRect().bottom(),p.x()-3, p.y()-3);
curr_line_item = scene->addLine(newline,blackpen);
}
}
else
std::cout << " other type, value is: " << item_tmp->type()<<std::endl;
}
//funzione update: if(is_drawing) drawline arrive_point to qcursor::pos.
//piu che drawline, updateline
void prova_widget::redraw_line()
{
if (!is_drawing) return;
// std::cout<<"inside redraw line" <<std::endl;
QPen blackpen = QPen(Qt::black);
//std::cout << "curr line != 0" <<std::endl;
QPoint p1 = QCursor::pos();
QPoint p2 = ui->graphicsView->mapFromGlobal(p1);
QPointF p = ui->graphicsView->mapToScene(p2);
scene->removeItem(curr_line_item);
QLineF newline(starting_object->x()+starting_object->boundingRect().center().x(),starting_object->y()+starting_object->boundingRect().bottom(),p.x()-3, p.y()-3);
curr_line_item = scene->addLine(newline,blackpen);
//curr_line = &newline;
}
void prova_widget::break_line_drawing(){
if (!is_drawing) return;
scene->removeItem(curr_line_item);
starting_object->setSelected(false);
std::cerr<<"selecteditems size: "<<scene->selectedItems().size()<<std::endl;
starting_object = 0;
is_drawing = false;
}
void prova_widget::mousePressEvent(QMouseEvent* e)
{
std::cout << "widget mouse press catched" << std::endl;
//QWidget::mousePressEvent(e);
if (e->button() == Qt::RightButton)
{
break_line_drawing();
selected_arrow = 0;
}
else
{
QPoint globpos = e->globalPos();
QPoint p2 = ui->graphicsView->mapFromGlobal(globpos);
QPointF p3 = ui->graphicsView->mapToScene(p2);
QRectF rect = QRectF(p3.x()-5,p3.y()-5,10,10);
QList<QGraphicsItem*> list = scene->items(rect);
std::cout<<"list size is: "<<list.size()<<std::endl;
for (QList<QGraphicsItem*>::iterator it = list.begin(); it != list.end();++it)
{
std::cout<< "type is:" << (*it)->type()<<std::endl;
QGraphicsLineItem* item = qgraphicsitem_cast<QGraphicsLineItem*>(*it);
if (arrows->count(item) != 0)
selected_arrow = arrows->at(item);
}
}
}
void prova_widget::update_L1_object()
{
if (is_drawing)
{
scene->removeItem(curr_line_item);
//starting_object = 0;
is_drawing = false;
popup.reset(new ProvaPopup());
connect(popup.get(),SIGNAL(accepted()),this,SLOT(update_object()));
connect(popup.get(),SIGNAL(rejected()),this,SLOT(no_data()));
popup->set_data(graphical_to_data.at(starting_object));
popup->exec();
popup->activateWindow();
//starting_object->text->setPlainText("modded");
}
else
{
std::cout<< "starting object not retrieved"<<std::endl;
}
}
void prova_widget::update_L2_object()
{
if (is_drawing)
{
scene->removeItem(curr_line_item);
//starting_object = 0;
is_drawing = false;
porte.reset(new ProvaPorte());
connect(porte.get(),SIGNAL(accepted()),this,SLOT(update_object2()));
connect(porte.get(),SIGNAL(rejected()),this,SLOT(no_data()));
porte->set_data(graphical_to_data.at(starting_object));
porte->exec();
porte->activateWindow();
//starting_object->text->setPlainText("modded");
}
else
{
std::cout<< "starting object not retrieved"<<std::endl;
}
}
void prova_widget::update_object(){
std::cout <<"entra in update"<<std::endl;
L4_Data* data = popup->get_data();
std::cout <<"data retrieved"<<std::endl;
starting_object->text->setPlainText(QString::fromStdString(data->name));
std::cout<<"update finished"<<std::endl;
}
void prova_widget::update_object2(){
std::cout <<"entra in update"<<std::endl;
L4_Data* data = porte->get_data();
std::cout <<"data retrieved"<<std::endl;
starting_object->text->setPlainText(QString::fromStdString(data->name));
std::cout<<"update finished"<<std::endl;
}
void prova_widget::finalize_line(){
std::pair<int,int> ports = ps->get_ports();
std::cout<< "finalize_line"<<std::endl;
std::cout << "arrows size is: "<<arrows->size()<<std::endl;
Arrow* a =new Arrow(starting_object,arrival_object,ports.first,ports.second,arrows);
starting_object->setSelected(false);
arrival_object->setArrowTarget();
starting_object = 0;
arrival_object->setSelected(false);
arrival_object = 0;
}
void prova_widget::delete_items()
{
std::cout <<"enter delete item"<<std::endl;
std::cout <<"selected arrow is: "<<selected_arrow<<std::endl;
std::cout <<"and starting_object is: "<<starting_object<<std::endl;
if (selected_arrow != 0 && selected_arrow != nullptr)
{
scene->removeItem(selected_arrow->dx_line);
scene->removeItem(selected_arrow->sx_line);
scene->removeItem(selected_arrow->line);
scene->removeItem(selected_arrow->label_start);
scene->removeItem(selected_arrow->label_stop);
Arrow* test = (*(selected_arrow->start_point->arriving_lines.find(selected_arrow->arrival_point))).second;
std::cout <<"test arrow is: "<<test<<std::endl;
ProvaRiquadro * test2 = (*(selected_arrow->start_point->arriving_lines.find(selected_arrow->arrival_point))).first;
std::cout <<"test riquadro is: "<<test2->text->toPlainText().toStdString()<<std::endl;
selected_arrow->start_point->arriving_lines.erase(selected_arrow->start_point->arriving_lines.find(selected_arrow->arrival_point));
selected_arrow->arrival_point->starting_lines.erase(selected_arrow->arrival_point->starting_lines.find(selected_arrow->start_point));
std::cout <<"erase end" <<std::endl;
selected_arrow = nullptr;
}
else if (starting_object != 0 && starting_object != nullptr)
{
std::cout <<"removing starting object"<<std::endl;
scene->removeItem(curr_line_item);
curr_line_item = nullptr;
is_drawing = false;
scene->removeItem(starting_object);
starting_object = nullptr;
}
}