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 
33 #include <utils/geom/Position.h>
39 #include "GUISUMOAbstractView.h"
43 #include "GUIDialog_EditViewport.h"
44 
45 
46 // ===========================================================================
47 // FOX callback mapping
48 // ===========================================================================
49 FXDEFMAP(GUIDialog_EditViewport) GUIDialog_EditViewportMap[] = {
55 };
56 
57 
58 // Object implementation
59 FXIMPLEMENT(GUIDialog_EditViewport, FXDialogBox, GUIDialog_EditViewportMap, ARRAYNUMBER(GUIDialog_EditViewportMap))
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
65 GUIDialog_EditViewport::GUIDialog_EditViewport(GUISUMOAbstractView* parent, const char* name, int x, int y) :
66  FXDialogBox(parent, name, GUIDesignDialogBox, x, y, 0, 0, 0, 0, 0, 0),
67  myParent(parent) {
68  // create contents frame
69  FXVerticalFrame* contentsFrame = new FXVerticalFrame(this, GUIDesignContentsFrame);
70  // create frame for file icons
71  FXHorizontalFrame* frameFiles = new FXHorizontalFrame(contentsFrame, GUIDesignHorizontalFrameIcons);
72  new FXButton(frameFiles, "Load\t\tLoad viewport from file",
74  new FXButton(frameFiles, "Save\t\tSave viewport to file",
76  // create zoom elements
77  FXHorizontalFrame* zoomFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
78  new FXLabel(zoomFrame, "Zoom:", 0, GUIDesignLabelLeftThick);
79  myZoom = new FXRealSpinDial(zoomFrame, 16, this, MID_CHANGED, GUIDesignSpinDial);
80  myZoom->setRange(0.0001, 100000);
81  myZoom->setNumberFormat(4);
82  // create X elements
83  FXHorizontalFrame* XFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
84  new FXLabel(XFrame, "X:", 0, GUIDesignLabelLeftThick);
85  myXOff = new FXRealSpinDial(XFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
86  // create Y elements
87  FXHorizontalFrame* YFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
88  new FXLabel(YFrame, "Y:", 0, GUIDesignLabelLeftThick);
89  myYOff = new FXRealSpinDial(YFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
90  // create Z elements
91  FXHorizontalFrame* ZFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
92  new FXLabel(ZFrame, "Z:", 0, GUIDesignLabelLeftThick);
93  myZOff = new FXRealSpinDial(ZFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
94 #ifdef HAVE_OSG
95  FXHorizontalFrame* lookAtXFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
96  new FXLabel(lookAtXFrame, "LookAtX:", 0, GUIDesignLabelLeftThick);
97  myLookAtX = new FXRealSpinDial(lookAtXFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
98  FXHorizontalFrame* lookAtYFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
99  new FXLabel(lookAtYFrame, "LookAtY:", 0, GUIDesignLabelLeftThick);
100  myLookAtY = new FXRealSpinDial(lookAtYFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
101  FXHorizontalFrame* lookAtZFrame = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
102  new FXLabel(lookAtZFrame, "LookAtZ:", 0, GUIDesignLabelLeftThick);
103  myLookAtZ = new FXRealSpinDial(lookAtZFrame, 16, this, MID_CHANGED, GUIDesignSpinDial | SPINDIAL_NOMIN | SPINDIAL_NOMAX);
104  // update width of dialog for the new elements
105  setHeight(257);
106 #endif
107  // create buttons ok/cancel
108  new FXHorizontalSeparator(contentsFrame, GUIDesignHorizontalSeparator);
109  FXHorizontalFrame* frameButtons = new FXHorizontalFrame(contentsFrame, GUIDesignAuxiliarHorizontalFrame);
110  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
111  buttonOk = new FXButton(frameButtons, "&OK\t\taccept", GUIIconSubSys::getIcon(ICON_ACCEPT), this, GUIDialog_EditViewport::MID_OK, GUIDesignButtonOK);
112  new FXButton(frameButtons, "&Cancel\t\tclose", GUIIconSubSys::getIcon(ICON_CANCEL), this, GUIDialog_EditViewport::MID_CANCEL, GUIDesignButtonCancel);
113  new FXHorizontalFrame(frameButtons, GUIDesignAuxiliarHorizontalFrame);
114  // set dialog icon
116 }
117 
118 
120 
121 
122 void
124  // set focus in OK button before opening
125  buttonOk->setFocus();
126  FXDialogBox::show();
127 }
128 
129 
130 long
131 GUIDialog_EditViewport::onCmdOk(FXObject*, FXSelector, void*) {
132  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
133 #ifdef HAVE_OSG
134  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
135 #else
137 #endif
138  );
139  hide();
140  return 1;
141 }
142 
143 
144 long
145 GUIDialog_EditViewport::onCmdCancel(FXObject*, FXSelector, void*) {
147  hide();
148  return 1;
149 }
150 
151 
152 long
153 GUIDialog_EditViewport::onCmdChanged(FXObject* o, FXSelector, void*) {
154  if (o == myZOff) {
155  myZoom->setValue(myParent->getChanger().zPos2Zoom(myZOff->getValue()));
156  } else if (o == myZoom) {
157  myZOff->setValue(myParent->getChanger().zoom2ZPos(myZoom->getValue()));
158  }
159  myParent->setViewportFromTo(Position(myXOff->getValue(), myYOff->getValue(), myZOff->getValue()),
160 #ifdef HAVE_OSG
161  Position(myLookAtX->getValue(), myLookAtY->getValue(), myLookAtZ->getValue())
162 #else
164 #endif
165  );
166  return 1;
167 }
168 
169 
170 long
171 GUIDialog_EditViewport::onCmdLoad(FXObject*, FXSelector, void*) {
172  FXFileDialog opendialog(this, "Load Viewport");
173  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
174  opendialog.setSelectMode(SELECTFILE_ANY);
175  opendialog.setPatternList("*.xml");
176  if (gCurrentFolder.length() != 0) {
177  opendialog.setDirectory(gCurrentFolder);
178  }
179  if (opendialog.execute()) {
180  gCurrentFolder = opendialog.getDirectory();
181  GUISettingsHandler handler(opendialog.getFilename().text());
182  handler.applyViewport(myParent);
184  }
185  return 1;
186 }
187 
188 
189 long
190 GUIDialog_EditViewport::onCmdSave(FXObject*, FXSelector, void*) {
191  FXString file = MFXUtils::getFilename2Write(this, "Save Viewport", ".xml", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
192  if (file == "") {
193  return 1;
194  }
195  try {
196  OutputDevice& dev = OutputDevice::getDevice(file.text());
198  writeXML(dev);
199  dev.closeTag();
200  dev.close();
201  } catch (IOError& e) {
202  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
203  }
204  return 1;
205 }
206 
207 
208 void
211  dev.writeAttr(SUMO_ATTR_ZOOM, myZoom->getValue());
212  dev.writeAttr(SUMO_ATTR_X, myXOff->getValue());
213  dev.writeAttr(SUMO_ATTR_Y, myYOff->getValue());
214 #ifdef HAVE_OSG
215  dev.writeAttr(SUMO_ATTR_CENTER_X, myLookAtX->getValue());
216  dev.writeAttr(SUMO_ATTR_CENTER_Y, myLookAtY->getValue());
217  dev.writeAttr(SUMO_ATTR_CENTER_Z, myLookAtZ->getValue());
218 #endif
219  dev.closeTag();
220 }
221 
222 void
223 GUIDialog_EditViewport::setValues(double zoom, double xoff, double yoff) {
224  myZoom->setValue(zoom);
225  myXOff->setValue(xoff);
226  myYOff->setValue(yoff);
227  myZOff->setValue(myParent->getChanger().zoom2ZPos(zoom));
228 }
229 
230 
231 void
232 GUIDialog_EditViewport::setValues(const Position& lookFrom, const Position& lookAt) {
233  myXOff->setValue(lookFrom.x());
234  myYOff->setValue(lookFrom.y());
235  myZOff->setValue(lookFrom.z());
236  myZoom->setValue(myParent->getChanger().zPos2Zoom(lookFrom.z()));
237 #ifdef HAVE_OSG
238  myLookAtX->setValue(lookAt.x());
239  myLookAtY->setValue(lookAt.y());
240  myLookAtZ->setValue(lookAt.z());
241 #else
242  UNUSED_PARAMETER(lookAt);
243 #endif
244 }
245 
246 
247 void
248 GUIDialog_EditViewport::setOldValues(const Position& lookFrom, const Position& lookAt) {
249  setValues(lookFrom, lookAt);
250  myOldLookFrom = lookFrom;
251  myOldLookAt = lookAt;
252 }
253 
254 
255 bool
257  return myZoom->getDial().grabbed() || myXOff->getDial().grabbed() || myYOff->getDial().grabbed();
258 }
259 
260 
261 /****************************************************************************/
262 
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:73
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:68
#define GUIDesignContentsFrame
design for the main content frame of every frame/dialog
Definition: GUIDesigns.h:253
double x() const
Returns the x-position.
Definition: Position.h:63
#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:38
FXButton * buttonOk
OK button.
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:281
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
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:393
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:210
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: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.
#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
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
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.