Eclipse SUMO - Simulation of Urban MObility
GUIDialog_EditViewport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2022 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 /****************************************************************************/
20 // A dialog to change the viewport
21 /****************************************************************************/
22 #include <config.h>
23 
25 #include <utils/geom/Position.h>
34 
35 #include "GUISUMOAbstractView.h"
36 #include "GUIDialog_EditViewport.h"
37 
38 
39 // ===========================================================================
40 // FOX callback mapping
41 // ===========================================================================
42 FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[] = {
48 };
49 
50 // Object implementation
51 FXIMPLEMENT(GUIDialog_EditViewport, FXDialogBox, GUIDialog_EditViewportMap, ARRAYNUMBER(GUIDialog_EditViewportMap))
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 
57 GUIDialog_EditViewport::GUIDialog_EditViewport(GUISUMOAbstractView* parent, const char* name, int x, int y) :
58  FXDialogBox(parent, name, GUIDesignDialogBox, x, y, 0, 0, 0, 0, 0, 0),
59  myParent(parent) {
60  // create contents frame
61  FXVerticalFrame* contentsFrame = new FXVerticalFrame(this, GUIDesignContentsFrame);
62  // create frame for file icons
63  FXHorizontalFrame* frameFiles = new FXHorizontalFrame(contentsFrame, GUIDesignHorizontalFrameIcons);
64  myLoadButton = new FXButton(frameFiles, "Load\t\tLoad viewport from file",
66  mySaveButton = new FXButton(frameFiles, "Save\t\tSave viewport to file",
68  // create horizontalframe for zoom elements and OSG
69  FXHorizontalFrame* editElementsFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
70 
71  // create vertical frame for XYZ values
72  FXVerticalFrame* lookFromFrame = new FXVerticalFrame(editElementsFrame, GUIDesignAuxiliarVerticalFrame);
73 
74  // create zoom elements
75  FXHorizontalFrame* zoomFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
76  new FXLabel(zoomFrame, "Zoom:", nullptr, GUIDesignLabelLeftThick);
77  myZoom = new FXRealSpinner(zoomFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPortZoom);
78  myZoom->setRange(0.0001, 100000);
79  //myZoom->setNumberFormat(4);
80 
81  // create lookFromX elements
82  FXHorizontalFrame* lookFromXFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
83  new FXLabel(lookFromXFrame, "X:", nullptr, GUIDesignLabelLeftThick);
84  myXOff = new FXRealSpinner(lookFromXFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
85 
86  // create lookFromY elements
87  FXHorizontalFrame* lookFromYFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
88  new FXLabel(lookFromYFrame, "Y:", nullptr, GUIDesignLabelLeftThick);
89  myYOff = new FXRealSpinner(lookFromYFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
90 
91  // create lookFromZ elements
92  FXHorizontalFrame* lookFromZFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
93  new FXLabel(lookFromZFrame, "Z:", nullptr, GUIDesignLabelLeftThick);
94  myZOff = new FXRealSpinner(lookFromZFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
95  myZOff->setRange(0.12, 100000000);
96 
97  // create rotation elements
98  FXHorizontalFrame* rotationFrame = new FXHorizontalFrame(lookFromFrame, GUIDesignAuxiliarHorizontalFrame);
99  new FXLabel(rotationFrame, "A:", nullptr, GUIDesignLabelLeftThick);
100  myRotation = new FXRealSpinner(rotationFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
101 
102  // create vertical frame for OSG
103  FXVerticalFrame* lookAtFrame = new FXVerticalFrame(editElementsFrame, GUIDesignAuxiliarVerticalFrame);
104  new FXLabel(lookAtFrame, "OSG", nullptr, GUIDesignLabelCenterThick);
105 
106  // create lookAtX elements
107  FXHorizontalFrame* lookAtXFrame = new FXHorizontalFrame(lookAtFrame, GUIDesignAuxiliarHorizontalFrame);
108  new FXLabel(lookAtXFrame, "LookAtX:", nullptr, GUIDesignLabelLeftThick);
109  myLookAtX = new FXRealSpinner(lookAtXFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
110 
111  // create lookAtY elements
112  FXHorizontalFrame* lookAtYFrame = new FXHorizontalFrame(lookAtFrame, GUIDesignAuxiliarHorizontalFrame);
113  new FXLabel(lookAtYFrame, "LookAtY:", nullptr, GUIDesignLabelLeftThick);
114  myLookAtY = new FXRealSpinner(lookAtYFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
115 
116  // create lookAtZ elements
117  FXHorizontalFrame* lookAtZFrame = new FXHorizontalFrame(lookAtFrame, GUIDesignAuxiliarHorizontalFrame);
118  new FXLabel(lookAtZFrame, "LookAtZ:", nullptr, GUIDesignLabelLeftThick);
119  myLookAtZ = new FXRealSpinner(lookAtZFrame, 16, this, MID_CHANGED, GUIDesignSpinDialViewPort);
120 
121  // only show LookAt elements if OSG is enabled
122 #ifdef HAVE_OSG
123  lookAtFrame->show();
124 #else
125  lookAtFrame->hide();
126 #endif
127 
128  // create buttons ok/cancel
129  new FXHorizontalSeparator(contentsFrame, GUIDesignHorizontalSeparator);
130  FXHorizontalFrame* frameButtons = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
131  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
132  myOKButton = new FXButton(frameButtons, "&OK\t\taccept", GUIIconSubSys::getIcon(GUIIcon::ACCEPT), this, GUIDialog_EditViewport::MID_OK, GUIDesignButtonOK);
133  myCancelButton = new FXButton(frameButtons, "&Cancel\t\tclose", GUIIconSubSys::getIcon(GUIIcon::CANCEL), this, GUIDialog_EditViewport::MID_CANCEL, GUIDesignButtonCancel);
134  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
135  // set dialog icon
137 }
138 
139 
141 
142 
143 void
145  // If testing mode is enabled, we need to place focus in the Z dial
146  if (OptionsCont::getOptions().getBool("gui-testing")) {
147  myLoadButton->setFocus();
148  } else {
149  myOKButton->setFocus();
150  }
151  FXDialogBox::show();
152 }
153 
154 
155 long
156 GUIDialog_EditViewport::onCmdOk(FXObject*, FXSelector, void*) {
157  myParent->setViewportFromToRot(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
158 #ifdef HAVE_OSG
159  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
160 #else
162 #endif
163  , myRotation->getValue()
164  );
165  // write information of current zoom status
166  WRITE_DEBUG("Current Viewport values: " + toString(myXOff->getValue()) + ", " + toString(myYOff->getValue()) + ", " + toString(myZOff->getValue()) +
167  ". Zoom = '" + toString(myZoom->getValue()) + "'");
168  saveWindowPos();
169  hide();
170  return 1;
171 }
172 
173 
174 long
175 GUIDialog_EditViewport::onCmdCancel(FXObject*, FXSelector, void*) {
177  saveWindowPos();
178  hide();
179  return 1;
180 }
181 
182 
183 long
184 GUIDialog_EditViewport::onCmdChanged(FXObject* o, FXSelector, void*) {
185  if (o == myZOff) {
186  myZoom->setValue(myParent->getChanger().zPos2Zoom(myZOff->getValue()));
187  } else if (o == myZoom) {
188  myZOff->setValue(myParent->getChanger().zoom2ZPos(myZoom->getValue()));
189  }
190  myParent->setViewportFromToRot(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
191 #ifdef HAVE_OSG
192  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
193 #else
195 #endif
196  , myRotation->getValue()
197  );
198  return 1;
199 }
200 
201 
202 long
203 GUIDialog_EditViewport::onCmdLoad(FXObject*, FXSelector, void*) {
204  FXFileDialog opendialog(this, "Load Viewport");
205  opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::OPEN_CONFIG));
206  opendialog.setSelectMode(SELECTFILE_ANY);
207  opendialog.setPatternList("*.xml");
208  if (gCurrentFolder.length() != 0) {
209  opendialog.setDirectory(gCurrentFolder);
210  }
211  if (opendialog.execute()) {
212  gCurrentFolder = opendialog.getDirectory();
213  GUISettingsHandler handler(opendialog.getFilename().text());
214  handler.applyViewport(myParent);
216  }
217  return 1;
218 }
219 
220 
221 long
222 GUIDialog_EditViewport::onCmdSave(FXObject*, FXSelector, void*) {
223  FXString file = MFXUtils::getFilename2Write(this, "Save Viewport", ".xml", GUIIconSubSys::getIcon(GUIIcon::SAVE), gCurrentFolder);
224  if (file == "") {
225  return 1;
226  }
227  try {
228  OutputDevice& dev = OutputDevice::getDevice(file.text());
230  writeXML(dev);
231  dev.closeTag();
232  dev.close();
233  } catch (IOError& e) {
234  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
235  }
236  return 1;
237 }
238 
239 
240 void
243  dev.writeAttr(SUMO_ATTR_ZOOM, myZoom->getValue());
244  dev.writeAttr(SUMO_ATTR_X, myXOff->getValue());
245  dev.writeAttr(SUMO_ATTR_Y, myYOff->getValue());
246  dev.writeAttr(SUMO_ATTR_ANGLE, myRotation->getValue());
247 #ifdef HAVE_OSG
248  if (myLookAtX->getValue() != Position::INVALID.x()) {
249  dev.writeAttr(SUMO_ATTR_CENTER_X, myLookAtX->getValue());
250  }
251  if (myLookAtY->getValue() != Position::INVALID.y()) {
252  dev.writeAttr(SUMO_ATTR_CENTER_Y, myLookAtY->getValue());
253  }
254  if (myLookAtZ->getValue() != Position::INVALID.z()) {
255  dev.writeAttr(SUMO_ATTR_CENTER_Z, myLookAtZ->getValue());
256  }
257 #endif
258  dev.closeTag();
259 }
260 
261 
262 void
263 GUIDialog_EditViewport::setValues(double zoom, double xoff, double yoff, double rotation) {
264  myZoom->setValue(zoom);
265  myXOff->setValue(xoff);
266  myYOff->setValue(yoff);
267  myZOff->setValue(myParent->getChanger().zoom2ZPos(zoom));
268  myRotation->setValue(rotation);
269 }
270 
271 
272 void
273 GUIDialog_EditViewport::setValues(const Position& lookFrom, const Position& lookAt, double rotation) {
274  myXOff->setValue(lookFrom.x());
275  myYOff->setValue(lookFrom.y());
276  myZOff->setValue(lookFrom.z());
277  myZoom->setValue(myParent->getChanger().zPos2Zoom(lookFrom.z()));
278 #ifdef HAVE_OSG
279  myLookAtX->setValue(lookAt.x());
280  myLookAtY->setValue(lookAt.y());
281  myLookAtZ->setValue(lookAt.z());
282 #else
283  UNUSED_PARAMETER(lookAt);
284 #endif
285  myRotation->setValue(rotation);
286 }
287 
288 
289 void
290 GUIDialog_EditViewport::setOldValues(const Position& lookFrom, const Position& lookAt, double rotation) {
291  setValues(lookFrom, lookAt, rotation);
292  myOldLookFrom = lookFrom;
293  myOldLookAt = lookAt;
294  myOldRotation = rotation;
295 }
296 
297 
298 bool
300  return false;
301  //return myZoom->getDial().grabbed() || myXOff->getDial().grabbed() || myYOff->getDial().grabbed();
302 }
303 
304 void
306  getApp()->reg().writeIntEntry("VIEWPORT_DIALOG_SETTINGS", "x", getX());
307  getApp()->reg().writeIntEntry("VIEWPORT_DIALOG_SETTINGS", "y", getY());
308 }
309 
310 
311 /****************************************************************************/
#define GUIDesignButtonToolbarWithText
little button with text and icon
Definition: GUIDesigns.h:103
#define GUIDesignButtonCancel
Cancel Button.
Definition: GUIDesigns.h:130
#define GUIDesignContentsFrame
design for the main content frame of every frame/dialog
Definition: GUIDesigns.h:352
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames
Definition: GUIDesigns.h:343
#define GUIDesignDialogBox
Definition: GUIDesigns.h:527
#define GUIDesignSpinDialViewPort
desing for standard spin dial
Definition: GUIDesigns.h:423
#define GUIDesignLabelLeftThick
label extended over frame with thick and with text justify to left
Definition: GUIDesigns.h:205
#define GUIDesignButtonOK
Definition: GUIDesigns.h:124
#define GUIDesignAuxiliarVerticalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames
Definition: GUIDesigns.h:346
#define GUIDesignHorizontalFrameIcons
Horizontal frame used for pack icons.
Definition: GUIDesigns.h:299
#define GUIDesignSpinDialViewPortZoom
desing for standard spin dial
Definition: GUIDesigns.h:420
#define GUIDesignLabelCenterThick
label extended over frame with thick and with text justify to center
Definition: GUIDesigns.h:211
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:395
FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[]
FXString gCurrentFolder
The folder used as last.
@ EDITVIEWPORT
#define WRITE_DEBUG(msg)
Definition: MsgHandler.h:290
@ SUMO_TAG_VIEWSETTINGS
@ SUMO_TAG_VIEWPORT
@ SUMO_ATTR_Y
@ SUMO_ATTR_X
@ SUMO_ATTR_CENTER_Y
@ SUMO_ATTR_ZOOM
@ SUMO_ATTR_ANGLE
@ SUMO_ATTR_CENTER_Z
@ SUMO_ATTR_CENTER_X
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A dialog to change the viewport.
void setOldValues(const Position &lookFrom, const Position &lookAt, double rotation)
Resets old values.
void writeXML(OutputDevice &dev)
write the settings to the given device
bool haveGrabbed() const
Returns the information whether one of the spin dialers is grabbed.
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user wants to save a viewport.
Position myOldLookFrom
The old viewport.
void setValues(double zoom, double xoff, double yoff, double rotation)
Sets the given values into the dialog.
void saveWindowPos()
save window position to the registry
FXButton * myLoadButton
load button
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user wants to load a viewport.
GUISUMOAbstractView * myParent
The calling view.
FXButton * myOKButton
OK button.
long onCmdCancel(FXObject *, FXSelector, void *)
Called when the user wants to restore the viewport.
long onCmdChanged(FXObject *, FXSelector, void *)
Called when the user changes the viewport.
FXRealSpinner * myLookAtX
The spin dialers used to change the view at (osg only)
long onCmdOk(FXObject *, FXSelector, void *)
Called when the user wants to keep the viewport.
FXRealSpinner * myZoom
The spin dialers used to change the view.
void show()
overload show function to focus always in OK Button
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
virtual double zoom2ZPos(double zoom) const =0
Returns the camera height at which the given zoom level is reached.
virtual double zPos2Zoom(double zPos) const =0
Returns the zoom level that is achieved at a given camera height.
virtual double getRotation() const =0
Returns the rotation of the canvas stored in this changer.
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
virtual double getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
virtual double getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
GUIPerspectiveChanger & getChanger() const
get changer
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
An XML-handler for visualisation schemes.
void applyViewport(GUISUMOAbstractView *view) const
Sets the viewport which has been parsed.
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:82
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
void close()
Closes the device and removes it from the dictionary.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:293
double x() const
Returns the x-position.
Definition: Position.h:55
double z() const
Returns the z-position.
Definition: Position.h:65
double y() const
Returns the y-position.
Definition: Position.h:60