Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2005-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file GUIDialog_EditViewport.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @date Mon, 25.04.2005
19 : ///
20 : // A dialog to change the viewport
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <utils/common/MsgHandler.h>
25 : #include <utils/geom/Position.h>
26 : #include <utils/foxtools/MFXUtils.h>
27 : #include <utils/gui/windows/GUIAppEnum.h>
28 : #include <utils/gui/windows/GUIPerspectiveChanger.h>
29 : #include <utils/gui/images/GUIIconSubSys.h>
30 : #include <utils/gui/div/GUIIOGlobals.h>
31 : #include <utils/gui/div/GUIDesigns.h>
32 : #include <utils/gui/settings/GUISettingsHandler.h>
33 : #include <utils/options/OptionsCont.h>
34 : #include <utils/geom/GeoConvHelper.h>
35 :
36 : #include "GUISUMOAbstractView.h"
37 : #include "GUIDialog_EditViewport.h"
38 :
39 :
40 : // ===========================================================================
41 : // FOX callback mapping
42 : // ===========================================================================
43 : FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[] = {
44 : FXMAPFUNC(SEL_COMMAND, GUIDialog_EditViewport::MID_CHANGED, GUIDialog_EditViewport::onCmdChanged),
45 : FXMAPFUNC(SEL_COMMAND, GUIDialog_EditViewport::MID_OK, GUIDialog_EditViewport::onCmdOk),
46 : FXMAPFUNC(SEL_COMMAND, GUIDialog_EditViewport::MID_CANCEL, GUIDialog_EditViewport::onCmdCancel),
47 : FXMAPFUNC(SEL_COMMAND, GUIDialog_EditViewport::MID_LOAD, GUIDialog_EditViewport::onCmdLoad),
48 : FXMAPFUNC(SEL_COMMAND, GUIDialog_EditViewport::MID_SAVE, GUIDialog_EditViewport::onCmdSave),
49 : };
50 :
51 : // Object implementation
52 0 : FXIMPLEMENT(GUIDialog_EditViewport, FXDialogBox, GUIDialog_EditViewportMap, ARRAYNUMBER(GUIDialog_EditViewportMap))
53 :
54 : // ===========================================================================
55 : // method definitions
56 : // ===========================================================================
57 :
58 : #ifdef _MSC_VER
59 : #pragma warning(push)
60 : #pragma warning(disable: 4355) // mask warning about "this" in initializers
61 : #endif
62 0 : GUIDialog_EditViewport::GUIDialog_EditViewport(GUISUMOAbstractView* parent, const char* name) :
63 : FXDialogBox(parent, name, GUIDesignDialogBox, 0, 0, 0, 0, 0, 0, 0, 0),
64 : GUIPersistentWindowPos(this, "VIEWPORT_DIALOG_SETTINGS", false, 20, 40, 150, 150, 100, 20),
65 0 : myParent(parent) {
66 : // create contents frame
67 0 : FXVerticalFrame* contentsFrame = new FXVerticalFrame(this, GUIDesignContentsFrame);
68 : // create frame for file icons
69 0 : FXHorizontalFrame* frameFiles = new FXHorizontalFrame(contentsFrame, GUIDesignHorizontalFrameIcons);
70 0 : myLoadButton = GUIDesigns::buildFXButton(frameFiles, TL("Load"), "", TL("Load viewport from file"),
71 : GUIIconSubSys::getIcon(GUIIcon::OPEN), this, GUIDialog_EditViewport::MID_LOAD, GUIDesignButtonToolbarWithText);
72 0 : mySaveButton = GUIDesigns::buildFXButton(frameFiles, TL("Save"), "", TL("Save viewport to file"),
73 : GUIIconSubSys::getIcon(GUIIcon::SAVE), this, GUIDialog_EditViewport::MID_SAVE, GUIDesignButtonToolbarWithText);
74 : // create horizontalframe for zoom elements and OSG
75 0 : FXHorizontalFrame* editElementsFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
76 :
77 : // create vertical frame for XYZ values
78 0 : FXVerticalFrame* lookFromFrame = new FXVerticalFrame(editElementsFrame, GUIDesignAuxiliarVerticalFrame);
79 :
80 : // create zoom elements
81 0 : FXHorizontalFrame* zoomFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
82 0 : new FXLabel(zoomFrame, "Zoom:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
83 0 : myZoom = new FXRealSpinner(zoomFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPortZoom);
84 0 : myZoom->setRange(0.0001, 100000);
85 : //myZoom->setNumberFormat(4);
86 :
87 : // create lookFromX elements
88 0 : FXHorizontalFrame* lookFromXFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
89 0 : new FXLabel(lookFromXFrame, "X:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
90 0 : myXOff = new FXRealSpinner(lookFromXFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
91 :
92 : // create lookFromY elements
93 0 : FXHorizontalFrame* lookFromYFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
94 0 : new FXLabel(lookFromYFrame, "Y:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
95 0 : myYOff = new FXRealSpinner(lookFromYFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
96 :
97 : // create lookFromZ elements
98 0 : FXHorizontalFrame* lookFromZFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
99 0 : new FXLabel(lookFromZFrame, "Z:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
100 0 : myZOff = new FXRealSpinner(lookFromZFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
101 0 : myZOff->setRange(0.12, 100000000);
102 :
103 : // create rotation elements
104 0 : FXHorizontalFrame* rotationFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
105 0 : new FXLabel(rotationFrame, "A:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
106 0 : myRotation = new FXRealSpinner(rotationFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
107 :
108 : // create Latitude elements
109 0 : FXHorizontalFrame* latFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
110 0 : new FXLabel(latFrame, "Lat:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
111 0 : myLatitude = new FXRealSpinner(latFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
112 0 : myLatitude->setRange(-90.0, 90.0);
113 0 : myLatitude->setIncrement(0.001);
114 0 : myLatitude->setGranularity(1e-6);
115 :
116 : // create Longitude elements
117 0 : FXHorizontalFrame* lonFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
118 0 : new FXLabel(lonFrame, "Lon:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
119 0 : myLongitude = new FXRealSpinner(lonFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
120 0 : myLongitude->setRange(-180.0, 180.0);
121 0 : myLongitude->setIncrement(0.001);
122 0 : myLongitude->setGranularity(1e-6);
123 :
124 : // create vertical frame for OSG
125 0 : FXVerticalFrame* lookAtFrame = new FXVerticalFrame(editElementsFrame, GUIDesignAuxiliarVerticalFrame);
126 0 : new FXLabel(lookAtFrame, "OSG", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
127 :
128 : // create lookAtX elements
129 0 : FXHorizontalFrame* lookAtXFrame = new FXHorizontalFrame(lookAtFrame, GUIDesignAuxiliarHorizontalFrame);
130 0 : new FXLabel(lookAtXFrame, "LookAtX:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
131 0 : myLookAtX = new FXRealSpinner(lookAtXFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
132 :
133 : // create lookAtY elements
134 0 : FXHorizontalFrame* lookAtYFrame = new FXHorizontalFrame(lookAtFrame, GUIDesignAuxiliarHorizontalFrame);
135 0 : new FXLabel(lookAtYFrame, "LookAtY:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
136 0 : myLookAtY = new FXRealSpinner(lookAtYFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
137 :
138 : // create lookAtZ elements
139 0 : FXHorizontalFrame* lookAtZFrame = new FXHorizontalFrame(lookAtFrame, GUIDesignAuxiliarHorizontalFrame);
140 0 : new FXLabel(lookAtZFrame, "LookAtZ:", nullptr, GUIDesignLabelThick(JUSTIFY_NORMAL));
141 0 : myLookAtZ = new FXRealSpinner(lookAtZFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
142 :
143 : // only show LookAt elements for OSG views
144 0 : if (parent->is3DView()) {
145 0 : lookAtFrame->show();
146 : } else {
147 0 : lookAtFrame->hide();
148 : }
149 :
150 : // create buttons ok/cancel
151 0 : new FXHorizontalSeparator(contentsFrame, GUIDesignHorizontalSeparator);
152 0 : FXHorizontalFrame* frameButtons = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
153 0 : new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
154 0 : myOKButton = GUIDesigns::buildFXButton(frameButtons, TL("&OK"), "", TL("accept"), GUIIconSubSys::getIcon(GUIIcon::ACCEPT), this, GUIDialog_EditViewport::MID_OK, GUIDesignButtonDialog);
155 0 : myCancelButton = GUIDesigns::buildFXButton(frameButtons, TL("&Cancel"), "", TL("close"), GUIIconSubSys::getIcon(GUIIcon::CANCEL), this, GUIDialog_EditViewport::MID_CANCEL, GUIDesignButtonDialog);
156 0 : new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
157 : // set dialog icon
158 0 : setIcon(GUIIconSubSys::getIcon(GUIIcon::EDITVIEWPORT));
159 0 : loadWindowPos();
160 0 : }
161 : #ifdef _MSC_VER
162 : #pragma warning(pop)
163 : #endif
164 :
165 :
166 0 : GUIDialog_EditViewport::~GUIDialog_EditViewport() {}
167 :
168 :
169 : void
170 0 : GUIDialog_EditViewport::show() {
171 : // If testing mode is enabled, we need to place focus in the Z dial
172 0 : if (OptionsCont::getOptions().getBool("gui-testing")) {
173 0 : myLoadButton->setFocus();
174 : } else {
175 0 : myOKButton->setFocus();
176 : }
177 0 : FXDialogBox::show();
178 0 : }
179 :
180 :
181 : long
182 0 : GUIDialog_EditViewport::onCmdOk(FXObject*, FXSelector, void*) {
183 0 : myParent->setViewportFromToRot(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
184 : #ifdef HAVE_OSG
185 0 : Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
186 : #else
187 : Position::INVALID
188 : #endif
189 0 : , myRotation->getValue()
190 : );
191 0 : hide();
192 0 : return 1;
193 : }
194 :
195 :
196 : long
197 0 : GUIDialog_EditViewport::onCmdCancel(FXObject*, FXSelector, void*) {
198 0 : myParent->setViewportFromToRot(myOldLookFrom, myOldLookAt, myOldRotation);
199 0 : hide();
200 0 : return 1;
201 : }
202 :
203 :
204 : long
205 0 : GUIDialog_EditViewport::onCmdChanged(FXObject* o, FXSelector, void*) {
206 0 : if (o == myZOff) {
207 0 : myZoom->setValue(myParent->getChanger().zPos2Zoom(myZOff->getValue()));
208 0 : } else if (o == myZoom) {
209 0 : if (myParent->is3DView()) {
210 0 : Position camera(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()), lookAt(myLookAtX->getValue(), myLookAtY->getValue(),
211 0 : myLookAtZ->getValue());
212 0 : myParent->zoom2Pos(camera, lookAt, myZoom->getValue());
213 : } else {
214 0 : myZOff->setValue(myParent->getChanger().zoom2ZPos(myZoom->getValue()));
215 : }
216 0 : } else if (o == myLatitude || o == myLongitude) {
217 0 : if (GeoConvHelper::getFinal().usingGeoProjection()) {
218 0 : Position geo(myLongitude->getValue(), myLatitude->getValue());
219 0 : if (GeoConvHelper::getFinal().x2cartesian_const(geo)) {
220 0 : myXOff->setValue(geo.x());
221 0 : myYOff->setValue(geo.y());
222 : }
223 : }
224 0 : } else if (o == myXOff || o == myYOff) {
225 0 : if (GeoConvHelper::getFinal().usingGeoProjection()) {
226 0 : Position pos(myXOff->getValue(), myYOff->getValue());
227 0 : GeoConvHelper::getFinal().cartesian2geo(pos);
228 0 : myLatitude->setValue(pos.y());
229 0 : myLongitude->setValue(pos.x());
230 : }
231 : }
232 0 : myParent->setViewportFromToRot(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
233 : #ifdef HAVE_OSG
234 0 : Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
235 : #else
236 : Position::INVALID
237 : #endif
238 0 : , myRotation->getValue()
239 : );
240 0 : return 1;
241 : }
242 :
243 :
244 : long
245 0 : GUIDialog_EditViewport::onCmdLoad(FXObject*, FXSelector, void*) {
246 0 : FXFileDialog opendialog(this, TL("Load Viewport"));
247 0 : opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::OPEN));
248 0 : opendialog.setSelectMode(SELECTFILE_ANY);
249 0 : opendialog.setPatternList(SUMOXMLDefinitions::ViewSettingsFileExtensions.getMultilineString().c_str());
250 0 : if (gCurrentFolder.length() != 0) {
251 0 : opendialog.setDirectory(gCurrentFolder);
252 : }
253 0 : if (opendialog.execute()) {
254 0 : gCurrentFolder = opendialog.getDirectory();
255 0 : GUISettingsHandler handler(getApp(), opendialog.getFilename().text());
256 0 : handler.applyViewport(myParent);
257 0 : setValues(myParent->getChanger().getZoom(), myParent->getChanger().getXPos(), myParent->getChanger().getYPos(), myParent->getChanger().getRotation());
258 0 : }
259 0 : return 1;
260 0 : }
261 :
262 :
263 : long
264 0 : GUIDialog_EditViewport::onCmdSave(FXObject*, FXSelector, void*) {
265 0 : FXString file = MFXUtils::getFilename2Write(this, TL("Save Viewport"),
266 0 : SUMOXMLDefinitions::XMLFileExtensions.getMultilineString().c_str(),
267 0 : GUIIconSubSys::getIcon(GUIIcon::SAVE), gCurrentFolder);
268 0 : if (file == "") {
269 : return 1;
270 : }
271 : try {
272 0 : OutputDevice& dev = OutputDevice::getDevice(file.text(), false);
273 0 : dev.openTag(SUMO_TAG_VIEWSETTINGS);
274 0 : writeXML(dev);
275 0 : dev.closeTag();
276 0 : dev.close();
277 0 : } catch (IOError& e) {
278 0 : FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());
279 0 : }
280 : return 1;
281 0 : }
282 :
283 :
284 : void
285 0 : GUIDialog_EditViewport::writeXML(OutputDevice& dev) {
286 0 : dev.openTag(SUMO_TAG_VIEWPORT);
287 0 : dev.writeAttr(SUMO_ATTR_ZOOM, myZoom->getValue());
288 0 : dev.writeAttr(SUMO_ATTR_X, myXOff->getValue());
289 0 : dev.writeAttr(SUMO_ATTR_Y, myYOff->getValue());
290 0 : if (myParent->is3DView()) {
291 0 : dev.writeAttr(SUMO_ATTR_Z, myZOff->getValue());
292 : }
293 0 : dev.writeAttr(SUMO_ATTR_ANGLE, myRotation->getValue());
294 0 : if (myParent->is3DView()) {
295 0 : if (myLookAtX->getValue() != Position::INVALID.x()) {
296 0 : dev.writeAttr(SUMO_ATTR_CENTER_X, myLookAtX->getValue());
297 : }
298 0 : if (myLookAtY->getValue() != Position::INVALID.y()) {
299 0 : dev.writeAttr(SUMO_ATTR_CENTER_Y, myLookAtY->getValue());
300 : }
301 0 : if (myLookAtZ->getValue() != Position::INVALID.z()) {
302 0 : dev.writeAttr(SUMO_ATTR_CENTER_Z, myLookAtZ->getValue());
303 : }
304 : }
305 0 : dev.closeTag();
306 0 : }
307 :
308 :
309 : void
310 0 : GUIDialog_EditViewport::setValues(double zoom, double xoff, double yoff, double rotation) {
311 0 : myZoom->setValue(zoom);
312 0 : myXOff->setValue(xoff);
313 0 : myYOff->setValue(yoff);
314 0 : myZOff->setValue(myParent->getChanger().zoom2ZPos(zoom));
315 0 : myRotation->setValue(rotation);
316 0 : if (GeoConvHelper::getFinal().usingGeoProjection()) {
317 : Position pos(xoff, yoff);
318 0 : GeoConvHelper::getFinal().cartesian2geo(pos);
319 0 : myLatitude->setValue(pos.y());
320 0 : myLongitude->setValue(pos.x());
321 0 : myLatitude->enable();
322 0 : myLongitude->enable();
323 : } else {
324 0 : myLatitude->setValue(0);
325 0 : myLongitude->setValue(0);
326 0 : myLatitude->disable();
327 0 : myLongitude->disable();
328 : }
329 0 : }
330 :
331 :
332 : void
333 0 : GUIDialog_EditViewport::setValues(const Position& lookFrom, const Position& lookAt, double rotation) {
334 0 : myXOff->setValue(lookFrom.x());
335 0 : myYOff->setValue(lookFrom.y());
336 0 : myZOff->setValue(lookFrom.z());
337 0 : if (!myParent->is3DView()) {
338 0 : myZoom->setValue(myParent->getChanger().zPos2Zoom(lookFrom.z()));
339 : }
340 : #ifdef HAVE_OSG
341 0 : myLookAtX->setValue(lookAt.x());
342 0 : myLookAtY->setValue(lookAt.y());
343 0 : myLookAtZ->setValue(lookAt.z());
344 : #else
345 : UNUSED_PARAMETER(lookAt);
346 : #endif
347 0 : myRotation->setValue(rotation);
348 0 : if (GeoConvHelper::getFinal().usingGeoProjection()) {
349 : Position pos(lookFrom.x(), lookFrom.y());
350 0 : GeoConvHelper::getFinal().cartesian2geo(pos);
351 0 : myLatitude->setValue(pos.y());
352 0 : myLongitude->setValue(pos.x());
353 0 : myLatitude->enable();
354 0 : myLongitude->enable();
355 : } else {
356 0 : myLatitude->setValue(0);
357 0 : myLongitude->setValue(0);
358 0 : myLatitude->disable();
359 0 : myLongitude->disable();
360 : }
361 0 : }
362 :
363 :
364 : void
365 0 : GUIDialog_EditViewport::setOldValues(const Position& lookFrom, const Position& lookAt, double rotation) {
366 0 : setValues(lookFrom, lookAt, rotation);
367 0 : myOldLookFrom = lookFrom;
368 0 : myOldLookAt = lookAt;
369 0 : myOldRotation = rotation;
370 0 : }
371 :
372 :
373 : bool
374 0 : GUIDialog_EditViewport::haveGrabbed() const {
375 0 : return false;
376 : //return myZoom->getDial().grabbed() || myXOff->getDial().grabbed() || myYOff->getDial().grabbed();
377 : }
378 :
379 :
380 : double
381 0 : GUIDialog_EditViewport::getZoomValue() const {
382 0 : return myZoom->getValue();
383 : }
384 :
385 : void
386 0 : GUIDialog_EditViewport::setZoomValue(double zoom) {
387 0 : myZoom->setValue(zoom);
388 0 : }
389 :
390 :
391 : void
392 0 : GUIDialog_EditViewport::saveWindowPos() {
393 0 : getApp()->reg().writeIntEntry("VIEWPORT_DIALOG_SETTINGS", "x", getX());
394 0 : getApp()->reg().writeIntEntry("VIEWPORT_DIALOG_SETTINGS", "y", getY());
395 0 : }
396 :
397 :
398 : /****************************************************************************/
|