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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A dialog to change the viewport
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
33 #include <utils/geom/Position.h>
43 
44 #include "GUISUMOAbstractView.h"
45 #include "GUIDialog_EditViewport.h"
46 
47 
48 // ===========================================================================
49 // FOX callback mapping
50 // ===========================================================================
51 FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[] = {
57 };
58 
59 
60 // Object implementation
61 FXIMPLEMENT(GUIDialog_EditViewport, FXDialogBox, GUIDialog_EditViewportMap, ARRAYNUMBER(GUIDialog_EditViewportMap))
62 
63 
64 // ===========================================================================
65 // method definitions
66 // ===========================================================================
67 GUIDialog_EditViewport::GUIDialog_EditViewport(GUISUMOAbstractView* parent, const char* name, int x, int y) :
68  FXDialogBox(parent, name, GUIDesignDialogBox, x, y, 0, 0, 0, 0, 0, 0),
69  myParent(parent) {
70  // create contents frame
71  FXVerticalFrame* contentsFrame = new FXVerticalFrame(this, GUIDesignContentsFrame);
72  // create frame for file icons
73  FXHorizontalFrame* frameFiles = new FXHorizontalFrame(contentsFrame, GUIDesignHorizontalFrameIcons);
74  new FXButton(frameFiles, "Load\t\tLoad viewport from file",
76  new FXButton(frameFiles, "Save\t\tSave viewport to file",
78  // create zoom elements
79  FXHorizontalFrame* zoomFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
80  new FXLabel(zoomFrame, "Zoom:", 0, GUIDesignLabelLeftThick);
81  myZoom = new FXRealSpinDial(zoomFrame, 16, this, MID_CHANGED, GUIDesignSpinDial);
82  myZoom->setRange(0.0001, 100000);
83  myZoom->setNumberFormat(4);
84  // create X elements
85  FXHorizontalFrame* XFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
86  new FXLabel(XFrame, "X:", 0, GUIDesignLabelLeftThick);
87  myXOff = new FXRealSpinDial(XFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
88  // create Y elements
89  FXHorizontalFrame* YFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
90  new FXLabel(YFrame, "Y:", 0, GUIDesignLabelLeftThick);
91  myYOff = new FXRealSpinDial(YFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
92  // create Z elements
93  FXHorizontalFrame* ZFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
94  new FXLabel(ZFrame, "Z:", 0, GUIDesignLabelLeftThick);
95  myZOff = new FXRealSpinDial(ZFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
96 #ifdef HAVE_OSG
97  FXHorizontalFrame* lookAtXFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
98  new FXLabel(lookAtXFrame, "LookAtX:", 0, GUIDesignLabelLeftThick);
99  myLookAtX = new FXRealSpinDial(lookAtXFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
100  FXHorizontalFrame* lookAtYFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
101  new FXLabel(lookAtYFrame, "LookAtY:", 0, GUIDesignLabelLeftThick);
102  myLookAtY = new FXRealSpinDial(lookAtYFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
103  FXHorizontalFrame* lookAtZFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
104  new FXLabel(lookAtZFrame, "LookAtZ:", 0, GUIDesignLabelLeftThick);
105  myLookAtZ = new FXRealSpinDial(lookAtZFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
106  // update width of dialog for the new elements
107  setHeight(257);
108 #endif
109  // create buttons ok/cancel
110  new FXHorizontalSeparator(contentsFrame, GUIDesignHorizontalSeparator);
111  FXHorizontalFrame* frameButtons = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
112  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
113  buttonOk = new FXButton(frameButtons, "&OK\t\taccept", GUIIconSubSys::getIcon(ICON_ACCEPT), this, GUIDialog_EditViewport::MID_OK, GUIDesignButtonOK);
114  new FXButton(frameButtons, "&Cancel\t\tclose", GUIIconSubSys::getIcon(ICON_CANCEL), this, GUIDialog_EditViewport::MID_CANCEL, GUIDesignButtonCancel);
115  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
116  // set dialog icon
118 }
119 
120 
122 
123 
124 void
126  // If testing mode is enabled, we need to place focus in the Z dial
127  if (OptionsCont::getOptions().getBool("gui-testing")) {
128  myZOff->setFocus();
129  } else {
130  buttonOk->setFocus();
131  }
132  FXDialogBox::show();
133 }
134 
135 
136 long
137 GUIDialog_EditViewport::onCmdOk(FXObject*, FXSelector, void*) {
138  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
139 #ifdef HAVE_OSG
140  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
141 #else
143 #endif
144  );
145  // write information of current zoom status
146  if (OptionsCont::getOptions().getBool("gui-testing-debug")) {
147  WRITE_WARNING("Current Viewport values: " + toString(myXOff->getValue()) + ", " + toString(myYOff->getValue()) + ", " + toString(myZOff->getValue()) +
148  ". Zoom = '" + toString(myZoom->getValue()) + "'");
149  }
150  hide();
151  return 1;
152 }
153 
154 
155 long
156 GUIDialog_EditViewport::onCmdCancel(FXObject*, FXSelector, void*) {
158  hide();
159  return 1;
160 }
161 
162 
163 long
164 GUIDialog_EditViewport::onCmdChanged(FXObject* o, FXSelector, void*) {
165  if (o == myZOff) {
166  myZoom->setValue(myParent->getChanger().zPos2Zoom(myZOff->getValue()));
167  } else if (o == myZoom) {
168  myZOff->setValue(myParent->getChanger().zoom2ZPos(myZoom->getValue()));
169  }
170  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
171 #ifdef HAVE_OSG
172  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
173 #else
175 #endif
176  );
177  return 1;
178 }
179 
180 
181 long
182 GUIDialog_EditViewport::onCmdLoad(FXObject*, FXSelector, void*) {
183  FXFileDialog opendialog(this, "Load Viewport");
184  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
185  opendialog.setSelectMode(SELECTFILE_ANY);
186  opendialog.setPatternList("*.xml");
187  if (gCurrentFolder.length() != 0) {
188  opendialog.setDirectory(gCurrentFolder);
189  }
190  if (opendialog.execute()) {
191  gCurrentFolder = opendialog.getDirectory();
192  GUISettingsHandler handler(opendialog.getFilename().text());
193  handler.applyViewport(myParent);
195  }
196  return 1;
197 }
198 
199 
200 long
201 GUIDialog_EditViewport::onCmdSave(FXObject*, FXSelector, void*) {
202  FXString file = MFXUtils::getFilename2Write(this, "Save Viewport", ".xml", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
203  if (file == "") {
204  return 1;
205  }
206  try {
207  OutputDevice& dev = OutputDevice::getDevice(file.text());
209  writeXML(dev);
210  dev.closeTag();
211  dev.close();
212  } catch (IOError& e) {
213  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
214  }
215  return 1;
216 }
217 
218 
219 void
222  dev.writeAttr(SUMO_ATTR_ZOOM, myZoom->getValue());
223  dev.writeAttr(SUMO_ATTR_X, myXOff->getValue());
224  dev.writeAttr(SUMO_ATTR_Y, myYOff->getValue());
225 #ifdef HAVE_OSG
226  dev.writeAttr(SUMO_ATTR_CENTER_X, myLookAtX->getValue());
227  dev.writeAttr(SUMO_ATTR_CENTER_Y, myLookAtY->getValue());
228  dev.writeAttr(SUMO_ATTR_CENTER_Z, myLookAtZ->getValue());
229 #endif
230  dev.closeTag();
231 }
232 
233 void
234 GUIDialog_EditViewport::setValues(double zoom, double xoff, double yoff) {
235  myZoom->setValue(zoom);
236  myXOff->setValue(xoff);
237  myYOff->setValue(yoff);
238  myZOff->setValue(myParent->getChanger().zoom2ZPos(zoom));
239 }
240 
241 
242 void
243 GUIDialog_EditViewport::setValues(const Position& lookFrom, const Position& lookAt) {
244  myXOff->setValue(lookFrom.x());
245  myYOff->setValue(lookFrom.y());
246  myZOff->setValue(lookFrom.z());
247  myZoom->setValue(myParent->getChanger().zPos2Zoom(lookFrom.z()));
248 #ifdef HAVE_OSG
249  myLookAtX->setValue(lookAt.x());
250  myLookAtY->setValue(lookAt.y());
251  myLookAtZ->setValue(lookAt.z());
252 #else
253  UNUSED_PARAMETER(lookAt);
254 #endif
255 }
256 
257 
258 void
259 GUIDialog_EditViewport::setOldValues(const Position& lookFrom, const Position& lookAt) {
260  setValues(lookFrom, lookAt);
261  myOldLookFrom = lookFrom;
262  myOldLookAt = lookAt;
263 }
264 
265 
266 bool
268  return myZoom->getDial().grabbed() || myXOff->getDial().grabbed() || myYOff->getDial().grabbed();
269 }
270 
271 
272 /****************************************************************************/
273 
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:260
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:72
virtual void setViewportFromTo(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
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:67
#define GUIDesignContentsFrame
design for the main content frame of every frame/dialog
Definition: GUIDesigns.h:255
double x() const
Returns the x-position.
Definition: Position.h:62
#define GUIDesignButtonCancel
Cancel Button.
Definition: GUIDesigns.h:103
void writeXML(OutputDevice &dev)
write the settings to the given device
FXString gCurrentFolder
The folder used as last.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user wants to load a viewport.
FXRealSpinDial * myZoom
The spin dialers used to change the view.
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:90
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
FXButton * buttonOk
OK button.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:283
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames ...
Definition: GUIDesigns.h:246
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.
FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[]
Position myOldLookFrom
The old viewport.
#define GUIDesignDialogBox
Definition: GUIDesigns.h:395
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:212
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:97
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
An XML-handler for visualisation schemes.
#define GUIDesignSpinDial
Definition: GUIDesigns.h:303
A dialog to change the viewport.
void setValues(double zoom, double xoff, double yoff)
Sets the given values into the dialog.
#define GUIDesignButtonToolbarWithText
little button with text and icon
Definition: GUIDesigns.h:84
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:155
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:277
void setOldValues(const Position &lookFrom, const Position &lookAt)
Resets old values.