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