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