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  // "Load"
96  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);
97  // "Save"
98  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);
99  new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
100  // "Clear List"
101  new FXButton(layout, "Clear\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);
102  new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
103  // "Close"
104  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);
105  //
107  myParent->addChild(this);
108 }
109 
110 
112  myParent->removeChild(this);
113 }
114 
115 
116 void
118  FXMainWindow::show();
119  myTable->startInput((int)myBreakpoints->size(), 0);
120 }
121 
122 
123 void
125  myTable->clearItems();
126  sort(myBreakpoints->begin(), myBreakpoints->end());
127  // set table attributes
128  myTable->setTableSize((FXint)myBreakpoints->size() + 1, 1);
129  myTable->setColumnText(0, "Time");
130  FXHeader* header = myTable->getColumnHeader();
131  header->setHeight(getApp()->getNormalFont()->getFontHeight() + getApp()->getNormalFont()->getFontAscent());
132  header->setItemJustify(0, JUSTIFY_CENTER_X);
133  // insert into table
134  for (int row = 0; row < (int)myBreakpoints->size(); row++) {
135  myTable->setItemText(row, 0, time2string((*myBreakpoints)[row]).c_str());
136  }
137  // insert dummy last field
138  myTable->setItemText((int)myBreakpoints->size(), 0, " ");
139 }
140 
141 
142 long
143 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
144  FXFileDialog opendialog(this, "Load Breakpoints");
145  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
146  opendialog.setSelectMode(SELECTFILE_ANY);
147  opendialog.setPatternList("*.txt");
148  if (gCurrentFolder.length() != 0) {
149  opendialog.setDirectory(gCurrentFolder);
150  }
151  if (opendialog.execute()) {
152  gCurrentFolder = opendialog.getDirectory();
153  std::string file = opendialog.getFilename().text();
154  std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
155  FXMutexLock lock(*myBreakpointLock);
156  myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
157  rebuildList();
158  }
159  return 1;
160 }
161 
162 
163 long
164 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
165  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
166  if (file == "") {
167  return 1;
168  }
169  std::string content = encode2TXT();
170  try {
171  OutputDevice& dev = OutputDevice::getDevice(file.text());
172  dev << content;
173  dev.close();
174  } catch (IOError& e) {
175  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
176  }
177  return 1;
178 }
179 
180 
181 std::string
183  FXMutexLock lock(*myBreakpointLock);
184  std::ostringstream strm;
185  std::sort(myBreakpoints->begin(), myBreakpoints->end());
186  for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
187  strm << time2string(*j) << std::endl;
188  }
189  return strm.str();
190 }
191 
192 
193 long
194 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
195  FXMutexLock lock(*myBreakpointLock);
196  myBreakpoints->clear();
197  rebuildList();
198  return 1;
199 }
200 
201 
202 
203 long
204 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
205  close(true);
206  return 1;
207 }
208 
209 
210 long
211 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* data) {
212  FXMutexLock lock(*myBreakpointLock);
213  const FXTablePos* const i = (FXTablePos*) data;
214  const std::string value = myTable->getItemText(i->row, i->col).text();
215  // check whether the inserted value is empty
216  const bool empty = value.find_first_not_of(" ") == std::string::npos;
217  try {
218  if (i->row == (int)myBreakpoints->size()) {
219  if (!empty) {
220  myBreakpoints->push_back(string2time(value));
221  }
222  } else {
223  if (empty) {
224  myBreakpoints->erase(myBreakpoints->begin() + i->row);
225  } else {
226  (*myBreakpoints)[i->row] = string2time(value);
227  }
228  }
229  } catch (NumberFormatException&) {
230  std::string msg = "The value must be a number, is:" + value;
231  FXMessageBox::error(this, MBOX_OK, "Number format error", "%s", msg.c_str());
232  }
233  rebuildList();
234  return 1;
235 }
236 
237 
238 /****************************************************************************/
239 
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:343
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:345
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:341
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