forked from seub/CirclePackings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas_delegate_draw_graph.cpp
317 lines (287 loc) · 10.8 KB
/
canvas_delegate_draw_graph.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
/*This file is part of Circle Packings.
Circle Packings is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Circle Packings is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Circle Packings. If not, see <http://www.gnu.org/licenses/>.*/
#include <QString>
#include <QComboBox>
#include <QPainter>
#include "canvas_delegate_draw_graph.hpp"
#include "graph_completer.hpp"
#include "window.hpp"
Canvas_Delegate_Draw_Graph::Canvas_Delegate_Draw_Graph(
const int &size_in_pixels, draw_graph_mode_type *draw_graph_flag, Graph<Point> *input_planar_graph, Window *window,
bool reset_contents) :
draw_graph_flag_(draw_graph_flag), input_planar_graph_(input_planar_graph)
{
window_ = window;
message_ = &(window_->message_received_);
size_in_pixels_ = size_in_pixels;
points_radius_ = 0.06;
reset_canvas_delegate(reset_contents);
}
void Canvas_Delegate_Draw_Graph::set_points_radius(double radius)
{
points_radius_ = radius;
return;
}
void Canvas_Delegate_Draw_Graph::reset_canvas_delegate(bool reset_contents)
{
common_reset();
if (reset_contents)
{
points_radius_ = 0.06;
input_planar_graph_->reset_graph();
}
is_vertex_selected_ = false;
vertex_created_ = false;
edge_added_ = false;
vertex_selected_ = false;
return;
}
void Canvas_Delegate_Draw_Graph::reset_selected_stuff()
{
//std::cout << "Entering Canvas_Delegate_Draw_Graph::reset_selected_stuff" << std::endl;
is_vertex_selected_ = false;
vertex_created_ = false;
edge_added_ = false;
vertex_selected_ = false;
common_reset_selected_stuff();
return;
}
void Canvas_Delegate_Draw_Graph::set_vertex_selected(bool is_vertex_selected, const unsigned int index_vertex_selected)
{
is_vertex_selected_ = is_vertex_selected;
index_vertex_selected_ = index_vertex_selected;
return;
}
void Canvas_Delegate_Draw_Graph::paint_event()
{
draw_planar_graph(*input_planar_graph_, points_radius_);
if (is_vertex_highlighted_)
{
mark_circle(Circle(input_planar_graph_->get_content_by_index(index_vertex_highlighted_), points_radius_), QColor("red"));
}
if (is_vertex_selected_)
{
mark_circle(Circle(input_planar_graph_->get_content_by_index(index_vertex_selected_), points_radius_), QColor("blue"));
}
return;
}
void Canvas_Delegate_Draw_Graph::mouse_press_event()
{
unsigned int n=input_planar_graph_->nb_vertices();
is_vertex_under_mouse_ = input_planar_graph_->index_point_containing(index_vertex_under_mouse_, point_clicked_, points_radius_);
if (*draw_graph_flag_==DRAW_GRAPH_VERTICES)
{
if (!is_vertex_under_mouse_)
{
unsigned int index_vertex_too_close;
bool is_vertex_too_close = input_planar_graph_->index_point_containing(
index_vertex_too_close, point_clicked_, 2*points_radius_);
if (is_vertex_too_close)
{
QString qstring = QString("No vertex added (too close from vertex %1 %2)")
.arg(QChar(0x00B0))
.arg(index_vertex_too_close + 1);
*message_ = qstring;
window_->signal_received(MESSAGE);
}
else
{
input_planar_graph_->add_isolated_vertex(n+1, point_clicked_, QColor(rand()%256,rand()%256,rand()%256));
vertex_created_ = true;
QString qstring = QString("Vertex n%1 %2 added!")
.arg(QChar(0x00B0))
.arg(n+1);
*message_ = qstring;
window_->signal_received(MESSAGE);
n++;
if (n>=2)
{
window_->signal_received(GRAPH_COMPLETABLE_ON);
window_->signal_received(DRAW_EDGES_ON);
}
}
}
else
{
input_planar_graph_->remove_vertex_by_index(index_vertex_under_mouse_);
input_planar_graph_->rename_all_labels();
QString qstring = QString("Vertex n%1 %2 deleted!")
.arg(QChar(0x00B0))
.arg(index_vertex_under_mouse_+1);
*message_ = qstring;
window_->signal_received(MESSAGE);
n--;
if (n<2)
{
window_->signal_received(GRAPH_COMPLETABLE_OFF);
window_->signal_received(DRAW_EDGES_OFF);
}
is_vertex_selected_ = false;
is_vertex_highlighted_ = false;
}
}
else if (*draw_graph_flag_==DRAW_GRAPH_EDGES)
{
if (is_vertex_under_mouse_)
{
if (!is_vertex_selected_)
{
is_vertex_selected_ = true;
vertex_selected_ = true;
index_vertex_selected_ = index_vertex_under_mouse_;
QString qstring = QString("Vertex n%1 %2 selected")
.arg(QChar(0x00B0))
.arg(index_vertex_selected_ + 1);
*message_ = qstring;
window_->signal_received(MESSAGE);
}
else if (index_vertex_under_mouse_==index_vertex_selected_)
{
is_vertex_selected_ = false;
}
else if (input_planar_graph_->are_neighbor_indices(index_vertex_under_mouse_, index_vertex_selected_))
{
input_planar_graph_->remove_edge_by_indices(index_vertex_under_mouse_, index_vertex_selected_);
edge_added_ = true;
QString qstring = QString("Edge %1 <-> %2 deleted!")
.arg(index_vertex_under_mouse_ + 1)
.arg(index_vertex_selected_ + 1);
*message_ = qstring;
window_->signal_received(MESSAGE);
is_vertex_selected_ = false;
}
else
{
Graph_Completer GC;
GC.set_graph(input_planar_graph_);
segment S(input_planar_graph_->get_content_by_index(index_vertex_under_mouse_),
input_planar_graph_->get_content_by_index(index_vertex_selected_));
if (GC.crosses_an_edge(S))
{
QString qstring = QString("Illegal edge! (no intersections allowed)");
*message_ = qstring;
window_->signal_received(MESSAGE);
}
else
{
input_planar_graph_->add_edge_by_indices(index_vertex_under_mouse_, index_vertex_selected_);
edge_added_ = true;
QString qstring = QString("Edge %1 <-> %2 added!")
.arg(index_vertex_under_mouse_ + 1)
.arg(index_vertex_selected_ + 1);
*message_ = qstring;
window_->signal_received(MESSAGE);
}
is_vertex_selected_ = false;
}
}
}
else if (*draw_graph_flag_==DRAW_GRAPH_DONE)
{
is_vertex_highlighted_ = is_vertex_under_mouse_;
index_vertex_highlighted_ = index_vertex_under_mouse_;
window_->signal_received(VERTEX_HIGHLIGHTED_LEFT);
}
return;
}
void Canvas_Delegate_Draw_Graph::mouse_release_event()
{
is_vertex_highlighted_ = input_planar_graph_->index_point_containing(index_vertex_highlighted_, point_released_, points_radius_);
if (*draw_graph_flag_==DRAW_GRAPH_DONE)
{
window_->signal_received(VERTEX_HIGHLIGHTED_LEFT);
}
return;
}
void Canvas_Delegate_Draw_Graph::mouse_move_event()
{
is_vertex_under_mouse_ = input_planar_graph_->index_point_containing(index_vertex_under_mouse_, point_under_mouse_, points_radius_);
if (!click_on_)
{
is_vertex_highlighted_ = is_vertex_under_mouse_;
index_vertex_highlighted_ = index_vertex_under_mouse_;
}
if (*draw_graph_flag_==DRAW_GRAPH_DONE && is_vertex_highlighted_)
{
window_->signal_received(VERTEX_HIGHLIGHTED_LEFT);
}
QString message;
if (is_vertex_under_mouse_)
{
if (*draw_graph_flag_ == DRAW_GRAPH_VERTICES)
{
if (!(vertex_created_ && index_vertex_under_mouse_ + 1 == input_planar_graph_->nb_vertices()))
{
message = QString("Vertex n%1 %2 (click to delete this vertex)")
.arg(QChar(0x00B0))
.arg(index_vertex_under_mouse_+1);
*message_ = message;
window_->signal_received(MESSAGE);
vertex_created_ = false;
}
}
else if (*draw_graph_flag_ == DRAW_GRAPH_EDGES)
{
if (is_vertex_selected_)
{
if (index_vertex_selected_ != index_vertex_under_mouse_)
{
message = QString("Vertex n%1 %2 (click to add/delete an edge)")
.arg(QChar(0x00B0))
.arg(index_vertex_under_mouse_+1);
*message_ = message;
window_->signal_received(MESSAGE);
}
else if (!vertex_selected_)
{
message = QString("Vertex n%1 %2 (click to unselect this vertex)")
.arg(QChar(0x00B0))
.arg(index_vertex_under_mouse_+1);
*message_ = message;
window_->signal_received(MESSAGE);
vertex_selected_ = false;
}
}
else
{
if (!edge_added_)
{
message = QString("Vertex n%1 %2 (click to select this vertex)")
.arg(QChar(0x00B0))
.arg(index_vertex_under_mouse_+1);
*message_ = message;
window_->signal_received(MESSAGE);
edge_added_ = false;
}
}
}
else
{
message = QString("Vertex n%1 %2")
.arg(QChar(0x00B0))
.arg(index_vertex_under_mouse_+1);
*message_ = message;
window_->signal_received(MESSAGE);
}
}
else
{
vertex_created_ = false;
edge_added_ = false;
vertex_selected_ = false;
}
if (!is_vertex_under_mouse_)
{
window_->signal_received(MESSAGE, -1);
}
return;
}