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>
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 // definitions
63 // ===========================================================================
64 #define INVALID_VALUE -1
65 #define INVALID_VALUE_STR "-1"
66 
67 
68 // ===========================================================================
69 // FOX callback mapping
70 // ===========================================================================
71 FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
72  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_LOAD, GUIDialog_Breakpoints::onCmdLoad),
73  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_SAVE, GUIDialog_Breakpoints::onCmdSave),
75  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_Breakpoints::onCmdClose),
77 };
78 
79 
80 FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
81 
82 
83 // ===========================================================================
84 // method definitions
85 // ===========================================================================
86 GUIDialog_Breakpoints::GUIDialog_Breakpoints(GUIMainWindow* parent, std::vector<SUMOTime>& breakpoints, MFXMutex& breakpointLock)
87  : FXMainWindow(parent->getApp(), "Breakpoints Editor", NULL, NULL, DECOR_ALL, 20, 20, 300, 300),
88  myParent(parent), myBreakpoints(&breakpoints), myBreakpointLock(&breakpointLock) {
89  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0);
90 
91  // build the table
92  myTable = new MFXAddEditTypedTable(hbox, this, MID_TABLE, LAYOUT_FILL_X | LAYOUT_FILL_Y);
93  myTable->setVisibleRows(20);
94  myTable->setVisibleColumns(1);
95  myTable->setTableSize(20, 1);
96  myTable->setBackColor(FXRGB(255, 255, 255));
97  myTable->setCellType(0, CT_REAL);
98  SUMOTime begin = string2time(OptionsCont::getOptions().getString("begin"));
99  SUMOTime end = string2time(OptionsCont::getOptions().getString("end"));
100  if (end < 0) {
101  end = SUMOTime_MAX;
102  }
103  myTable->setNumberCellParams(0, begin / 1000, end / 1000, 1, 10, 100, "%.2f");
104  myTable->getRowHeader()->setWidth(0);
105  myBreakpointLock->lock();
106  rebuildList();
107  myBreakpointLock->unlock();
108  // build the layout
109  FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0, 4, 4, 4, 4);
110  // "Load"
111  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);
112  // "Save"
113  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);
114  new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
115  // "Clear List"
116  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);
117  new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
118  // "Close"
119  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);
120  //
122  myParent->addChild(this);
123 }
124 
125 
127  myParent->removeChild(this);
128 }
129 
130 
131 void
133  myTable->clearItems();
134  sort(myBreakpoints->begin(), myBreakpoints->end());
135  // set table attributes
136  myTable->setTableSize((FXint) myBreakpoints->size() + 1, 1);
137  myTable->setColumnText(0, "Time");
138  FXHeader* header = myTable->getColumnHeader();
139  header->setHeight(getApp()->getNormalFont()->getFontHeight() + getApp()->getNormalFont()->getFontAscent());
140  int k;
141  for (k = 0; k < 1; k++) {
142  header->setItemJustify(k, JUSTIFY_CENTER_X);
143  }
144  // insert into table
145  FXint row = 0;
146  std::vector<SUMOTime>::iterator j;
147  for (j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
148  myTable->setItemText(row, 0, time2string(*j).c_str());
149  row++;
150  }
151  // insert dummy last field
152  for (k = 0; k < 1; k++) {
153  myTable->setItemText(row, k, " ");
154  }
155 }
156 
157 
158 long
159 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
160  FXFileDialog opendialog(this, "Load Breakpoints");
161  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
162  opendialog.setSelectMode(SELECTFILE_ANY);
163  opendialog.setPatternList("*.txt");
164  if (gCurrentFolder.length() != 0) {
165  opendialog.setDirectory(gCurrentFolder);
166  }
167  if (opendialog.execute()) {
168  gCurrentFolder = opendialog.getDirectory();
169  std::string file = opendialog.getFilename().text();
170  std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
172  myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
173  rebuildList();
175  }
176  return 1;
177 }
178 
179 
180 long
181 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
182  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
183  if (file == "") {
184  return 1;
185  }
186  std::string content = encode2TXT();
187  try {
188  OutputDevice& dev = OutputDevice::getDevice(file.text());
189  dev << content;
190  dev.close();
191  } catch (IOError& e) {
192  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
193  }
194  return 1;
195 }
196 
197 
198 std::string
201  std::ostringstream strm;
202  std::sort(myBreakpoints->begin(), myBreakpoints->end());
203  for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
204  if ((*j) != INVALID_VALUE) {
205  strm << time2string(*j) << std::endl;
206  }
207  }
209  return strm.str();
210 }
211 
212 
213 long
214 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
216  myBreakpoints->clear();
217  rebuildList();
219  return 1;
220 }
221 
222 
223 
224 long
225 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
226  close(true);
227  return 1;
228 }
229 
230 
231 long
232 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* data) {
235  std::string value = i->item->getText().text();
236  // check whether the inserted value is empty
237  if (value.find_first_not_of(" ") == std::string::npos) {
238  // replace by invalid if so
239  value = INVALID_VALUE_STR;
240  }
241  int row = i->row;
242  if (row == (int) myBreakpoints->size()) {
243  myBreakpoints->push_back(INVALID_VALUE);
244  }
245 
246  switch (i->col) {
247  case 0:
248  try {
249  (*myBreakpoints)[row] = string2time(value);
250  } catch (NumberFormatException&) {
251  std::string msg = "The value must be an int, is:" + value;
252  FXMessageBox::error(this, MBOX_OK, "Number format error", "%s", msg.c_str());
253  }
254  break;
255  default:
256  break;
257  }
258  if (!i->updateOnly) {
259  rebuildList();
260  }
262  return 1;
263 }
264 
265 
266 /****************************************************************************/
267 
#define INVALID_VALUE_STR
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.
MFXAddEditTypedTable * myTable
The list that holds the ids.
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:327
static std::vector< SUMOTime > loadBreakpoints(const std::string &file)
loads breakpoints from the specified file
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[]
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
Editor for simulation breakpoints.
FXTableItem * item
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
The Table.
Definition: GUIAppEnum.h:277
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:329
MFXMutex * myBreakpointLock
Lock for modifying the list of breakpoints.
void unlock()
release mutex lock
Definition: MFXMutex.cpp:96
void rebuildList()
Rebuilds the entire list.
#define SUMOTime_MAX
Definition: SUMOTime.h:44
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:325
void lock()
lock mutex
Definition: MFXMutex.cpp:86
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
static FXIcon * getIcon(GUIIcon which)
#define INVALID_VALUE