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