SUMO - Simulation of Urban MObility
GUIDialog_EditViewport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A dialog to change the viewport
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2005-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
34 #include <utils/geom/Position.h>
44 
45 #include "GUISUMOAbstractView.h"
46 #include "GUIDialog_EditViewport.h"
47 
48 
49 // ===========================================================================
50 // FOX callback mapping
51 // ===========================================================================
52 FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[] = {
58 };
59 
60 
61 // Object implementation
62 FXIMPLEMENT(GUIDialog_EditViewport, FXDialogBox, GUIDialog_EditViewportMap, ARRAYNUMBER(GUIDialog_EditViewportMap))
63 
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
68 GUIDialog_EditViewport::GUIDialog_EditViewport(GUISUMOAbstractView* parent, const char* name, int x, int y) :
69  FXDialogBox(parent, name, GUIDesignDialogBox, x, y, 0, 0, 0, 0, 0, 0),
70  myParent(parent) {
71  // create contents frame
72  FXVerticalFrame* contentsFrame = new FXVerticalFrame(this, GUIDesignContentsFrame);
73  // create frame for file icons
74  FXHorizontalFrame* frameFiles = new FXHorizontalFrame(contentsFrame, GUIDesignHorizontalFrameIcons);
75  new FXButton(frameFiles, "Load\t\tLoad viewport from file",
77  new FXButton(frameFiles, "Save\t\tSave viewport to file",
79  // create zoom elements
80  FXHorizontalFrame* zoomFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
81  new FXLabel(zoomFrame, "Zoom:", 0, GUIDesignLabelLeftThick);
82  myZoom = new FXRealSpinDial(zoomFrame, 16, this, MID_CHANGED, GUIDesignSpinDial);
83  myZoom->setRange(0.0001, 100000);
84  myZoom->setNumberFormat(4);
85  // create X elements
86  FXHorizontalFrame* XFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
87  new FXLabel(XFrame, "X:", 0, GUIDesignLabelLeftThick);
88  myXOff = new FXRealSpinDial(XFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
89  // create Y elements
90  FXHorizontalFrame* YFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
91  new FXLabel(YFrame, "Y:", 0, GUIDesignLabelLeftThick);
92  myYOff = new FXRealSpinDial(YFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
93  // create Z elements
94  FXHorizontalFrame* ZFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
95  new FXLabel(ZFrame, "Z:", 0, GUIDesignLabelLeftThick);
96  myZOff = new FXRealSpinDial(ZFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
97 #ifdef HAVE_OSG
98  FXHorizontalFrame* lookAtXFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
99  new FXLabel(lookAtXFrame, "LookAtX:", 0, GUIDesignLabelLeftThick);
100  myLookAtX = new FXRealSpinDial(lookAtXFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
101  FXHorizontalFrame* lookAtYFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
102  new FXLabel(lookAtYFrame, "LookAtY:", 0, GUIDesignLabelLeftThick);
103  myLookAtY = new FXRealSpinDial(lookAtYFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
104  FXHorizontalFrame* lookAtZFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
105  new FXLabel(lookAtZFrame, "LookAtZ:", 0, GUIDesignLabelLeftThick);
106  myLookAtZ = new FXRealSpinDial(lookAtZFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
107  // update width of dialog for the new elements
108  setHeight(257);
109 #endif
110  // create buttons ok/cancel
111  new FXHorizontalSeparator(contentsFrame, GUIDesignHorizontalSeparator);
112  FXHorizontalFrame* frameButtons = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
113  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
114  buttonOk = new FXButton(frameButtons, "&OK\t\taccept", GUIIconSubSys::getIcon(ICON_ACCEPT), this, GUIDialog_EditViewport::MID_OK, GUIDesignButtonOK);
115  new FXButton(frameButtons, "&Cancel\t\tclose", GUIIconSubSys::getIcon(ICON_CANCEL), this, GUIDialog_EditViewport::MID_CANCEL, GUIDesignButtonCancel);
116  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
117  // set dialog icon
119 }
120 
121 
123 
124 
125 void
127  // If testing mode is enabled, we need to place focus in the Z dial
128  if (OptionsCont::getOptions().getBool("gui-testing")) {
129  myZOff->setFocus();
130  } else {
131  buttonOk->setFocus();
132  }
133  FXDialogBox::show();
134 }
135 
136 
137 long
138 GUIDialog_EditViewport::onCmdOk(FXObject*, FXSelector, void*) {
139  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
140 #ifdef HAVE_OSG
141  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
142 #else
144 #endif
145  );
146  // write information of current zoom status
147  if (OptionsCont::getOptions().getBool("gui-testing-debug")) {
148  WRITE_WARNING("Current Viewport values: " + toString(myXOff->getValue()) + ", " + toString(myYOff->getValue()) + ", " + toString(myZOff->getValue()) +
149  ". Zoom = '" + toString(myZoom->getValue()) + "'");
150  }
151  hide();
152  return 1;
153 }
154 
155 
156 long
157 GUIDialog_EditViewport::onCmdCancel(FXObject*, FXSelector, void*) {
159  hide();
160  return 1;
161 }
162 
163 
164 long
165 GUIDialog_EditViewport::onCmdChanged(FXObject* o, FXSelector, void*) {
166  if (o == myZOff) {
167  myZoom->setValue(myParent->getChanger().zPos2Zoom(myZOff->getValue()));
168  } else if (o == myZoom) {
169  myZOff->setValue(myParent->getChanger().zoom2ZPos(myZoom->getValue()));
170  }
171  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
172 #ifdef HAVE_OSG
173  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
174 #else
176 #endif
177  );
178  return 1;
179 }
180 
181 
182 long
183 GUIDialog_EditViewport::onCmdLoad(FXObject*, FXSelector, void*) {
184  FXFileDialog opendialog(this, "Load Viewport");
185  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
186  opendialog.setSelectMode(SELECTFILE_ANY);
187  opendialog.setPatternList("*.xml");
188  if (gCurrentFolder.length() != 0) {
189  opendialog.setDirectory(gCurrentFolder);
190  }
191  if (opendialog.execute()) {
192  gCurrentFolder = opendialog.getDirectory();
193  GUISettingsHandler handler(opendialog.getFilename().text());
194  handler.applyViewport(myParent);
196  }
197  return 1;
198 }
199 
200 
201 long
202 GUIDialog_EditViewport::onCmdSave(FXObject*, FXSelector, void*) {
203  FXString file = MFXUtils::getFilename2Write(this, "Save Viewport", ".xml", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
204  if (file == "") {
205  return 1;
206  }
207  try {
208  OutputDevice& dev = OutputDevice::getDevice(file.text());
210  writeXML(dev);
211  dev.closeTag();
212  dev.close();
213  } catch (IOError& e) {
214  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
215  }
216  return 1;
217 }
218 
219 
220 void
223  dev.writeAttr(SUMO_ATTR_ZOOM, myZoom->getValue());
224  dev.writeAttr(SUMO_ATTR_X, myXOff->getValue());
225  dev.writeAttr(SUMO_ATTR_Y, myYOff->getValue());
226 #ifdef HAVE_OSG
227  dev.writeAttr(SUMO_ATTR_CENTER_X, myLookAtX->getValue());
228  dev.writeAttr(SUMO_ATTR_CENTER_Y, myLookAtY->getValue());
229  dev.writeAttr(SUMO_ATTR_CENTER_Z, myLookAtZ->getValue());
230 #endif
231  dev.closeTag();
232 }
233 
234 void
235 GUIDialog_EditViewport::setValues(double zoom, double xoff, double yoff) {
236  myZoom->setValue(zoom);
237  myXOff->setValue(xoff);
238  myYOff->setValue(yoff);
239  myZOff->setValue(myParent->getChanger().zoom2ZPos(zoom));
240 }
241 
242 
243 void
244 GUIDialog_EditViewport::setValues(const Position& lookFrom, const Position& lookAt) {
245  myXOff->setValue(lookFrom.x());
246  myYOff->setValue(lookFrom.y());
247  myZOff->setValue(lookFrom.z());
248  myZoom->setValue(myParent->getChanger().zPos2Zoom(lookFrom.z()));
249 #ifdef HAVE_OSG
250  myLookAtX->setValue(lookAt.x());
251  myLookAtY->setValue(lookAt.y());
252  myLookAtZ->setValue(lookAt.z());
253 #else
254  UNUSED_PARAMETER(lookAt);
255 #endif
256 }
257 
258 
259 void
260 GUIDialog_EditViewport::setOldValues(const Position& lookFrom, const Position& lookAt) {
261  setValues(lookFrom, lookAt);
262  myOldLookFrom = lookFrom;
263  myOldLookAt = lookAt;
264 }
265 
266 
267 bool
269  return myZoom->getDial().grabbed() || myXOff->getDial().grabbed() || myYOff->getDial().grabbed();
270 }
271 
272 
273 /****************************************************************************/
274 
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:261
double z() const
Returns the z-position.
Definition: Position.h:73
void close()
Closes the device and removes it from the dictionary.
virtual double getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
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.
double x() const
Returns the x-position.
Definition: Position.h:63
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
#define GUIDesignContentsFrame
design for the main content frame of every frame/dialog
Definition: GUIDesigns.h:253
#define GUIDesignButtonCancel
Cancel Button.
Definition: GUIDesigns.h:104
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.
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:91
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
FXButton * buttonOk
OK button.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
virtual double zPos2Zoom(double zPos) const =0
Returns the zoom level that is achieved at a given camera height.
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:281
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
void applyViewport(GUISUMOAbstractView *view) const
Sets the viewport which has been parsed.
virtual double zoom2ZPos(double zoom) const =0
Returns the camera height at which the given zoom level is reached.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames ...
Definition: GUIDesigns.h:244
double y() const
Returns the y-position.
Definition: Position.h:68
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user wants to save a viewport.
GUISUMOAbstractView * myParent
The calling view.
FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[]
Position myOldLookFrom
The old viewport.
#define GUIDesignDialogBox
Definition: GUIDesigns.h:393
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
#define GUIDesignHorizontalFrameIcons
Horizontal frame used for pack icons.
Definition: GUIDesigns.h:210
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
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.
GUIPerspectiveChanger & getChanger() const
get changer
#define GUIDesignButtonOK
Definition: GUIDesigns.h:98
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
An XML-handler for visualisation schemes.
#define GUIDesignSpinDial
Definition: GUIDesigns.h:301
A dialog to change the viewport.
void setValues(double zoom, double xoff, double yoff)
Sets the given values into the dialog.
bool haveGrabbed() const
Returns the information whether one of the spin dialers is grabbed.
#define GUIDesignButtonToolbarWithText
little button with text and icon
Definition: GUIDesigns.h:85
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
#define GUIDesignLabelLeftThick
label extended over frame with thick and with text justify to left
Definition: GUIDesigns.h:156
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:278
void setOldValues(const Position &lookFrom, const Position &lookAt)
Resets old values.