SUMO - Simulation of Urban MObility
GUIDialog_Breakpoints.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 // Editor for simulation breakpoints
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 
32 #include <string>
33 #include <vector>
34 #include <iostream>
35 #include <fstream>
36 #include <set>
39 #include <gui/GUIGlobals.h>
42 #include <utils/common/ToString.h>
53 #include "GUIDialog_Breakpoints.h"
54 
55 
56 // ===========================================================================
57 // FOX callback mapping
58 // ===========================================================================
59 FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
60  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_LOAD, GUIDialog_Breakpoints::onCmdLoad),
61  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_SAVE, GUIDialog_Breakpoints::onCmdSave),
63  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_Breakpoints::onCmdClose),
64  FXMAPFUNC(SEL_REPLACED, MID_TABLE, GUIDialog_Breakpoints::onCmdEditTable),
65 };
66 
67 
68 FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
69 
70 
71 // ===========================================================================
72 // method definitions
73 // ===========================================================================
74 GUIDialog_Breakpoints::GUIDialog_Breakpoints(GUIMainWindow* parent, std::vector<SUMOTime>& breakpoints, FXMutex& breakpointLock) :
75  FXMainWindow(parent->getApp(), "Breakpoints Editor", NULL, NULL, DECOR_ALL, 20, 20, 170, 300),
76  myParent(parent), myBreakpoints(&breakpoints), myBreakpointLock(&breakpointLock) {
77  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
78 
79  // build the table
80  myTable = new FXTable(hbox, this, MID_TABLE, GUIDesignTable);
81  myTable->setVisibleRows(20);
82  myTable->setVisibleColumns(1);
83  myTable->setTableSize(20, 1);
84  myTable->setBackColor(FXRGB(255, 255, 255));
85  myTable->getRowHeader()->setWidth(0);
86  myBreakpointLock->lock();
87  rebuildList();
88  myBreakpointLock->unlock();
89  // build the layout
90  FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0, 4, 4, 4, 4);
91 
92  // create buttons ('&' in the label creates a hot key)
93  // "Load"
94  new FXButton(layout, "&Load\t\t", 0, this, MID_CHOOSEN_LOAD, GUIDesignButtonBreakpoint);
95  // "Save"
96  new FXButton(layout, "&Save\t\t", 0, this, MID_CHOOSEN_SAVE, GUIDesignButtonBreakpoint);
97  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
98  // "Clear List"
99  new FXButton(layout, "Clea&r\t\t", 0, this, MID_CHOOSEN_CLEAR, GUIDesignButtonBreakpoint);
100  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
101  // "Close"
102  new FXButton(layout, "&Close\t\t", 0, this, MID_CANCEL, GUIDesignButtonBreakpoint);
103 
104  //
106  myParent->addChild(this);
107 }
108 
109 
111  myParent->removeChild(this);
112 }
113 
114 
115 void
117  FXMainWindow::show();
118  myTable->startInput((int)myBreakpoints->size(), 0);
119 }
120 
121 
122 void
124  myTable->clearItems();
125  sort(myBreakpoints->begin(), myBreakpoints->end());
126  // set table attributes
127  myTable->setTableSize((FXint)myBreakpoints->size() + 1, 1);
128  myTable->setColumnText(0, "Time");
129  FXHeader* header = myTable->getColumnHeader();
130  header->setHeight(getApp()->getNormalFont()->getFontHeight() + getApp()->getNormalFont()->getFontAscent());
131  header->setItemJustify(0, JUSTIFY_CENTER_X);
132  // insert into table
133  for (int row = 0; row < (int)myBreakpoints->size(); row++) {
134  myTable->setItemText(row, 0, time2string((*myBreakpoints)[row]).c_str());
135  }
136  // insert dummy last field
137  myTable->setItemText((int)myBreakpoints->size(), 0, " ");
138 }
139 
140 
141 long
142 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
143  FXFileDialog opendialog(this, "Load Breakpoints");
144  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
145  opendialog.setSelectMode(SELECTFILE_ANY);
146  opendialog.setPatternList("*.txt");
147  if (gCurrentFolder.length() != 0) {
148  opendialog.setDirectory(gCurrentFolder);
149  }
150  if (opendialog.execute()) {
151  gCurrentFolder = opendialog.getDirectory();
152  std::string file = opendialog.getFilename().text();
153  std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
154  FXMutexLock lock(*myBreakpointLock);
155  myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
156  rebuildList();
157  }
158  return 1;
159 }
160 
161 
162 long
163 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
164  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
165  if (file == "") {
166  return 1;
167  }
168  std::string content = encode2TXT();
169  try {
170  OutputDevice& dev = OutputDevice::getDevice(file.text());
171  dev << content;
172  dev.close();
173  } catch (IOError& e) {
174  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
175  }
176  return 1;
177 }
178 
179 
180 std::string
182  FXMutexLock lock(*myBreakpointLock);
183  std::ostringstream strm;
184  std::sort(myBreakpoints->begin(), myBreakpoints->end());
185  for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
186  strm << time2string(*j) << std::endl;
187  }
188  return strm.str();
189 }
190 
191 
192 long
193 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
194  FXMutexLock lock(*myBreakpointLock);
195  myBreakpoints->clear();
196  rebuildList();
197  return 1;
198 }
199 
200 
201 
202 long
203 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
204  close(true);
205  return 1;
206 }
207 
208 
209 long
210 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* data) {
211  FXMutexLock lock(*myBreakpointLock);
212  const FXTablePos* const i = (FXTablePos*) data;
213  const std::string value = myTable->getItemText(i->row, i->col).text();
214  // check whether the inserted value is empty
215  const bool empty = value.find_first_not_of(" ") == std::string::npos;
216  try {
217  if (i->row == (int)myBreakpoints->size()) {
218  if (!empty) {
219  myBreakpoints->push_back(string2time(value));
220  }
221  } else {
222  if (empty) {
223  myBreakpoints->erase(myBreakpoints->begin() + i->row);
224  } else {
225  (*myBreakpoints)[i->row] = string2time(value);
226  }
227  }
228  } catch (NumberFormatException&) {
229  std::string msg = "The value must be a number, is:" + value;
230  FXMessageBox::error(this, MBOX_OK, "Number format error", "%s", msg.c_str());
231  }
232  rebuildList();
233  return 1;
234 }
235 
236 
237 /****************************************************************************/
238 
void close()
Closes the device and removes it from the dictionary.
std::vector< SUMOTime > * myBreakpoints
List of breakpoints.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
GUIMainWindow * myParent
The parent window.
Cancel-button pressed.
Definition: GUIAppEnum.h:64
Clear set.
Definition: GUIAppEnum.h:348
FXString gCurrentFolder
The folder used as last.
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user presses the Save-button.
static std::vector< SUMOTime > loadBreakpoints(const std::string &file)
loads breakpoints from the specified file
void show()
sets the focus after the window is created
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
FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[]
Editor for simulation breakpoints.
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
FXMutex * myBreakpointLock
Lock for modifying the list of breakpoints.
Load set.
Definition: GUIAppEnum.h:344
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions ...
Definition: GUIDesigns.h:243
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:283
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
void removeChild(FXMDIChild *child)
removes the given child window from the list
long onCmdEditTable(FXObject *, FXSelector, void *)
Called when the table was changed.
Save set.
Definition: GUIAppEnum.h:346
void rebuildList()
Rebuilds the entire list.
#define GUIDesignButtonBreakpoint
button used in breakpoint editor
Definition: GUIDesigns.h:71
std::string encode2TXT()
Builds a text representation of the items in the list.
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
#define GUIDesignTable
Definition: GUIDesigns.h:405
FXTable * myTable
The list that holds the ids.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
long long int SUMOTime
Definition: TraCIDefs.h:51
The Table.
Definition: GUIAppEnum.h:288
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon