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-2016 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 
33 #include <utils/geom/Position.h>
39 #include "GUISUMOAbstractView.h"
42 #include "GUIDialog_EditViewport.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
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 // ===========================================================================
69  const char* name, int x, int y)
70  : FXDialogBox(parent, name, DECOR_TITLE | DECOR_BORDER, x, y, 0, 0),
71  myParent(parent) {
72  FXVerticalFrame* f1 = new FXVerticalFrame(this, LAYOUT_TOP | FRAME_NONE | LAYOUT_FILL_X, 0, 0, 0, 0, 0, 0, 1, 1);
73  {
74  FXHorizontalFrame* frame0 =
75  new FXHorizontalFrame(f1, FRAME_THICK, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2);
76  new FXButton(frame0, "\t\tLoad viewport from file",
78  ICON_ABOVE_TEXT | BUTTON_TOOLBAR | FRAME_RAISED | LAYOUT_TOP | LAYOUT_LEFT);
79  new FXButton(frame0, "\t\tSave viewport to file",
81  ICON_ABOVE_TEXT | BUTTON_TOOLBAR | FRAME_RAISED | LAYOUT_TOP | LAYOUT_LEFT);
82  }
83  FXMatrix* m1 = new FXMatrix(f1, 2, MATRIX_BY_COLUMNS);
84  new FXLabel(m1, "Zoom:", 0, LAYOUT_CENTER_Y);
85  myZoom = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK);
86  myZoom->setRange(0.0001, 100000);
87  myZoom->setNumberFormat(4);
88  new FXLabel(m1, "X:", 0, LAYOUT_CENTER_Y);
89  myXOff = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
90  new FXLabel(m1, "Y:", 0, LAYOUT_CENTER_Y);
91  myYOff = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
92  new FXLabel(m1, "Z:", 0, LAYOUT_CENTER_Y);
93  myZOff = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
94 #ifdef HAVE_OSG
95  new FXLabel(m1, "LookAtX:", 0, LAYOUT_CENTER_Y);
96  myLookAtX = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
97  new FXLabel(m1, "LookAtY:", 0, LAYOUT_CENTER_Y);
98  myLookAtY = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
99  new FXLabel(m1, "LookAtZ:", 0, LAYOUT_CENTER_Y);
100  myLookAtZ = new FXRealSpinDial(m1, 16, this, MID_CHANGED, LAYOUT_CENTER_Y | LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
101 #endif
102  // ok/cancel
103  new FXHorizontalSeparator(f1, SEPARATOR_GROOVE | LAYOUT_FILL_X);
104  FXHorizontalFrame* f6 = new FXHorizontalFrame(f1, LAYOUT_TOP | LAYOUT_LEFT | LAYOUT_FILL_X | PACK_UNIFORM_WIDTH, 0, 0, 0, 0, 10, 10, 5, 0);
105  FXButton* initial =
106  new FXButton(f6, "&OK", NULL, this, GUIDialog_EditViewport::MID_OK,
107  BUTTON_INITIAL | BUTTON_DEFAULT | FRAME_RAISED | FRAME_THICK | LAYOUT_TOP | LAYOUT_LEFT | LAYOUT_CENTER_X,
108  0, 0, 0, 0, 4, 4, 3, 3);
109  new FXButton(f6, "&Cancel", NULL, this, GUIDialog_EditViewport::MID_CANCEL,
110  FRAME_RAISED | FRAME_THICK | LAYOUT_TOP | LAYOUT_LEFT | LAYOUT_CENTER_X,
111  0, 0, 0, 0, 4, 4, 3, 3);
112  initial->setFocus();
114 }
115 
116 
118 
119 
120 long
121 GUIDialog_EditViewport::onCmdOk(FXObject*, FXSelector, void*) {
122  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
123 #ifdef HAVE_OSG
124  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
125 #else
127 #endif
128  );
129  hide();
130  return 1;
131 }
132 
133 
134 long
135 GUIDialog_EditViewport::onCmdCancel(FXObject*, FXSelector, void*) {
137  hide();
138  return 1;
139 }
140 
141 
142 long
143 GUIDialog_EditViewport::onCmdChanged(FXObject* o, FXSelector, void*) {
144  if (o == myZOff) {
145  myZoom->setValue(myParent->getChanger().zPos2Zoom(myZOff->getValue()));
146  } else if (o == myZoom) {
147  myZOff->setValue(myParent->getChanger().zoom2ZPos(myZoom->getValue()));
148  }
149  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
150 #ifdef HAVE_OSG
151  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
152 #else
154 #endif
155  );
156  return 1;
157 }
158 
159 
160 long
161 GUIDialog_EditViewport::onCmdLoad(FXObject*, FXSelector, void* /*data*/) {
162  FXFileDialog opendialog(this, "Load Viewport");
163  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
164  opendialog.setSelectMode(SELECTFILE_ANY);
165  opendialog.setPatternList("*.xml");
166  if (gCurrentFolder.length() != 0) {
167  opendialog.setDirectory(gCurrentFolder);
168  }
169  if (opendialog.execute()) {
170  gCurrentFolder = opendialog.getDirectory();
171  GUISettingsHandler handler(opendialog.getFilename().text());
172  handler.applyViewport(myParent);
174  }
175  return 1;
176 }
177 
178 
179 long
180 GUIDialog_EditViewport::onCmdSave(FXObject*, FXSelector, void* /*data*/) {
181  FXString file = MFXUtils::getFilename2Write(this, "Save Viewport", ".xml", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
182  if (file == "") {
183  return 1;
184  }
185  try {
186  OutputDevice& dev = OutputDevice::getDevice(file.text());
188  writeXML(dev);
189  dev.closeTag();
190  dev.close();
191  } catch (IOError& e) {
192  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
193  }
194  return 1;
195 }
196 
197 
198 void
201  dev.writeAttr(SUMO_ATTR_ZOOM, myZoom->getValue());
202  dev.writeAttr(SUMO_ATTR_X, myXOff->getValue());
203  dev.writeAttr(SUMO_ATTR_Y, myYOff->getValue());
204 #ifdef HAVE_OSG
205  dev.writeAttr(SUMO_ATTR_CENTER_X, myLookAtX->getValue());
206  dev.writeAttr(SUMO_ATTR_CENTER_Y, myLookAtY->getValue());
207  dev.writeAttr(SUMO_ATTR_CENTER_Z, myLookAtZ->getValue());
208 #endif
209  dev.closeTag();
210 }
211 
212 void
214  myZoom->setValue(zoom);
215  myXOff->setValue(xoff);
216  myYOff->setValue(yoff);
217  myZOff->setValue(myParent->getChanger().zoom2ZPos(zoom));
218 }
219 
220 
221 void
222 GUIDialog_EditViewport::setValues(const Position& lookFrom, const Position& lookAt) {
223  myXOff->setValue(lookFrom.x());
224  myYOff->setValue(lookFrom.y());
225  myZOff->setValue(lookFrom.z());
226  myZoom->setValue(myParent->getChanger().zPos2Zoom(lookFrom.z()));
227 #ifdef HAVE_OSG
228  myLookAtX->setValue(lookAt.x());
229  myLookAtY->setValue(lookAt.y());
230  myLookAtZ->setValue(lookAt.z());
231 #else
232  UNUSED_PARAMETER(lookAt);
233 #endif
234 }
235 
236 
237 void
238 GUIDialog_EditViewport::setOldValues(const Position& lookFrom, const Position& lookAt) {
239  setValues(lookFrom, lookAt);
240  myOldLookFrom = lookFrom;
241  myOldLookAt = lookAt;
242 }
243 
244 
245 bool
247  return myZoom->getDial().grabbed() || myXOff->getDial().grabbed() || myYOff->getDial().grabbed();
248 }
249 
250 
251 /****************************************************************************/
252 
void setValues(SUMOReal zoom, SUMOReal xoff, SUMOReal yoff)
Sets the given values into the dialog.
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:257
void close()
Closes the device and removes it from the dictionary.
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 SUMOReal getZoom() const =0
Returns the zoom factor computed stored in this changer.
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.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:95
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
virtual SUMOReal getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
void applyViewport(GUISUMOAbstractView *view) const
Sets the viewport which has been parsed.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
virtual SUMOReal zPos2Zoom(SUMOReal zPos) const =0
Returns the zoom level that is achieved at a given camera height.
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
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.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
GUIPerspectiveChanger & getChanger() const
get changer
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
An XML-handler for visualisation schemes.
A dialog to change the viewport.
virtual SUMOReal getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
bool haveGrabbed() const
Returns the information whether one of the spin dialers is grabbed.
virtual SUMOReal zoom2ZPos(SUMOReal zoom) const =0
Returns the camera height at which the given zoom level is reached.
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
static const Position INVALID
Definition: Position.h:261
void setOldValues(const Position &lookFrom, const Position &lookAt)
Resets old values.