-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgui.h
More file actions
344 lines (300 loc) · 13.9 KB
/
gui.h
File metadata and controls
344 lines (300 loc) · 13.9 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
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
#ifndef GUI_H
#define GUI_H
#include <osg/Image>
#include <osg/Notify>
#include <osgViewer/CompositeViewer>
// Qt stuff
#include <QApplication>
#include <QMessageBox>
#include <QMainWindow>
#include <QStatusBar>
#include <QMdiArea>
#include <QMdiSubWindow>
#include "polygon_draw.h"
#include "graph_draw.h"
#include "utilities.h"
using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::QtGui;
using namespace osgEarth::Util::Controls;
using namespace osgEarth::Annotation;
//--------------------------------------------------------------
osg::ArgumentParser* arguments = NULL;
QWidget* viewerWidget = NULL;
QMainWindow* win = NULL;
EarthManipulator* map_controller = NULL;
osg::Group* root = NULL;
//--------------------------------------------------------------
/** Actions for the events */
// Update the down status bar whenever the mouse pointer crosses the pixel coordinate pair (x,y)
void update_status_bar(float x, float y, bool select_mode_on, bool turn_processing_on)
{
// TODO: handle out-of-the-earth mouse events and use the smaller code below
// GeoPoint mapPointGeodetic = util::GetMapCoordFromPixelCoord(x, y, view);
// long double lat = mapPointGeodetic.y();
// long double lon = mapPointGeodetic.x();
// std::string slat = std::to_string(lat);
// std::string slon = std::to_string(lon);
// std::string geocoordinates(slat);
// geocoordinates.append(std::string(", "));
// geocoordinates.append(slon);
// win->statusBar()->showMessage(QString(std::string(annotation_mode_on ? "Graph Selection: ON | " : "Graph Selection: OFF | ").append(geocoordinates).c_str()));
osgViewer::View* view = static_cast<osgViewer::View*>(viewer->asView());
osg::Vec3d world;
osgUtil::LineSegmentIntersector::Intersections hits;
if (view->computeIntersections(x, y, hits))
{
world = hits.begin()->getWorldIntersectPoint();
if(map_node != NULL)
{
osg::ref_ptr<TerrainEngineNode> te = map_node->getTerrainEngine();
Terrain* m_terrain = te->getTerrain();
// convert to map coords:
if(m_terrain != NULL)
{
const SpatialReference* sr = m_terrain->getSRS();
if(sr != NULL)
{
GeoPoint mapPoint;
mapPoint.fromWorld(sr, world);
// change status here
GeoPoint mapPointGeodetic( map_node->getMapSRS()->getGeodeticSRS(), mapPoint);
osg::Vec3d v;
mapPoint.toWorld(v);
long double lat = mapPointGeodetic.y();
long double lon = mapPointGeodetic.x();
std::string slat = std::to_string(lat);
std::string slon = std::to_string(lon);
std::string geocoordinates(slon);
geocoordinates.append(std::string(", "));
geocoordinates.append(slat);
win->statusBar()->showMessage(QString(std::string(select_mode_on ? "Graph Selection: ON | " : "Graph Selection: OFF | ")
.append(turn_processing_on ? "Turn Processing: ON | " : "Turn Processing: OFF | ")
.append(geocoordinates).c_str()));
}
}
}
}
}
//--------------------------------------------------------------
// Remove a polygon described by d
void erase_annotation(GeometricObject d)
{
size_t idx = d.idx;
size_t type = d.type;
if(type == 0)
{
std::vector<GeoNode> res = get_bounding_box_points(idx);
box_group->removeChild(idx);
}
else if(type == 1)
{
std::vector<GeoNode> res = get_polygon_points(idx);
polygon_group->removeChild(idx);
}else if(type == 2) pin_group->removeChild(idx);
}
// Remove the nearest polygon lying in the surroundings of the pixel coordinate pair (x,y)
void erase_annotation(float x, float y)
{
erase_annotation(get_nearest_geometric_object(x, y));
}
//--------------------------------------------------------------
/** Handle events regarding GUI stuff */
class UserEventHandler : public osgGA::GUIEventHandler
{
public:
// C'tor
UserEventHandler() : m_annotation_mode_on(false), m_boundingbox_selection_on(false), m_turn_processing_on(false) {}
/** Handle user events */
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
if(!m_boundingbox_selection_on &&
!m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) &&
(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_CTRL))
{
GeoPoint clicked_location = util::get_map_coord_from_pixel_coord(ea.getX(), ea.getY());
Viewpoint curr_viewpoint = map_controller->getViewpoint();
map_controller->setViewpoint(Viewpoint(
"Clicked Point",
osg::Vec3d(clicked_location.x(), clicked_location.y(), clicked_location.z()),
curr_viewpoint.getHeading(), curr_viewpoint.getPitch(), curr_viewpoint.getRange(),
map_node->getMapSRS()->getGeodeticSRS()));
}
// event for actualizing the status bar with the map coordinates corresponding to the actual pixel coordinates
else if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE &&
aa.asView()->getFrameStamp()->getFrameNumber() % 10 == 0)
{
update_status_bar(ea.getX(), ea.getY(), m_annotation_mode_on, m_turn_processing_on);
// event for activating/deactivating the annotation mode
} else if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&
ea.getKey() == osgGA::GUIEventAdapter::KEY_F2)
{
m_annotation_mode_on = !m_annotation_mode_on;
update_status_bar(ea.getX(), ea.getY(), m_annotation_mode_on, m_turn_processing_on);
// events for drawing a bounding box via two mouse clicks (1/2)
} else if(!m_boundingbox_selection_on &&
m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) &&
(ea.getModKeyMask() != osgGA::GUIEventAdapter::MODKEY_CTRL) && // not erasing annotations
ea.getModKeyMask() != osgGA::GUIEventAdapter::MODKEY_SHIFT) // not selecting points for polygon
{
m_boundingbox_selection_on = true;
m_boundingbox_fst_x = ea.getX();
m_boundingbox_fst_y = ea.getY();
// events for drawing a bounding box via two mouse clicks (2/2)
} else if(m_boundingbox_selection_on &&
m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON))
{
m_boundingbox_selection_on = false;
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
draw_bounding_box(m_boundingbox_fst_x, m_boundingbox_fst_y, ea.getX(), ea.getY(), view);
}
// event for erasing bounding boxes
else if(m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON
) &&
ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_CTRL && // not erasing annotations
ea.getModKeyMask() != osgGA::GUIEventAdapter::MODKEY_SHIFT) // not selecting points for polygon
{
erase_annotation(ea.getX(), ea.getY());
// events for constructing a 2D polygon: first even captures a sequence of point selections, as te second captures a final event to finish point capture and finally draw it
} else if(m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON
) &&
(ea.getModKeyMask() != osgGA::GUIEventAdapter::MODKEY_CTRL) && // not erasing annotations
(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_SHIFT)) // not selecting points for polygon
{
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
draw_polygon(ea.getX(), ea.getY(), view);
} else if(m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) &&
(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_SHIFT))
{
if(drawing_selected_points.size() == 2)
{
draw_segments(drawing_selected_points);
} else {
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
draw_polygon(ea.getX(), ea.getY(), view, true);
}
}
// event for activating the turn processing
else if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && ea.getKey() == osgGA::GUIEventAdapter::KEY_F3)
{
drawing_selected_points.clear();
m_annotation_mode_on = false;
m_turn_processing_on = !m_turn_processing_on;
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
update_status_bar(ea.getX(), ea.getY(), m_annotation_mode_on, m_turn_processing_on);
}
else if(m_turn_processing_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON))
{
draw_segments(ea.getX(), ea.getY());
}
// event for sticking unlabeled red pins
else if(m_annotation_mode_on && (ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON))
{
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
draw_pin(ea.getX(), ea.getY(), view);
}
// event for changing pin color
else if(!m_turn_processing_on &&
!m_annotation_mode_on &&
(ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) &&
(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_SHIFT))
{
change_pin_color(ea.getX(), ea.getY());
}
// event for drawing nodes
else if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && ea.getKey() == osgGA::GUIEventAdapter::KEY_F5)
{
// DrawNodes();
draw_node_simple(5);
// DrawSelectedNodes();
// DrawNodesStepwise();
// DrawNodesFromGeoCoordFile("allnodes.txt");
}
// event for drawing edges
else if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && ea.getKey() == osgGA::GUIEventAdapter::KEY_F6)
{
draw_edges();
// DrawSelectedEdges();
// DrawEdgesStepwise();
// DrawEdgesFromGeoCoordFile("alledges.txt");
}
// event for outputting nodes to a CO file
else if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && ea.getKey() == osgGA::GUIEventAdapter::KEY_F7)
{
output_selected_dimacs_nodes("output.co");
}
// event for outputting edges to a GR file
else if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && ea.getKey() == osgGA::GUIEventAdapter::KEY_F8)
{
output_selected_dimacs_edges("output.gr");
}
// event for point membership tests in polygon areas
else if((m_turn_processing_on &&
!m_annotation_mode_on &&
ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) &&
(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_SHIFT))
{
GeoPoint p = util::get_map_coord_from_pixel_coord(ea.getX(), ea.getY());
if(is_point_selected(GeoNode(p.x(), p.y())))
{
QMessageBox m;
m.setText("Yes!");
m.exec();
} else {
QMessageBox m;
m.setText("No.");
m.exec();
}
}
// event for point membership tests in polygon areas
else if((m_turn_processing_on &&
!m_annotation_mode_on &&
ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) &&
(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_CTRL))
{
draw_polygon_center(ea.getX(), ea.getY());
}
return false;
}
// data holders for testing new features
int m_boundingbox_fst_x;
int m_boundingbox_fst_y;
bool m_boundingbox_selection_on;
bool m_annotation_mode_on;
bool m_turn_processing_on;
};
//--------------------------------------------------------------
/** Event handler to interact with the annotations */
class SelectEventHandler : public AnnotationEventHandler
{
public:
void onHoverEnter(AnnotationNode* anno, const EventArgs& args)
{
//anno->setDecoration("hover");
OE_NOTICE << "Hover enter" << std::endl;
}
void onHoverLeave(AnnotationNode* anno, const EventArgs& args)
{
//anno->clearDecoration();
OE_NOTICE << "Hover leave" << std::endl;
}
virtual void onClick(AnnotationNode* node, const EventArgs& details)
{
float x = details.x;
float y = details.y;
}
};
#endif // GUI_H