SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
12 // Copyright (C) 2001-2012 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>
45 #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 // definitions
62 // ===========================================================================
63 #define INVALID_VALUE -1
64 #define INVALID_VALUE_STR "-1"
65 
66 
67 // ===========================================================================
68 // FOX callback mapping
69 // ===========================================================================
70 FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
71  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_LOAD, GUIDialog_Breakpoints::onCmdLoad),
72  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_SAVE, GUIDialog_Breakpoints::onCmdSave),
74  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_Breakpoints::onCmdClose),
76 };
77 
78 
79 FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
80 
81 
82 // ===========================================================================
83 // method definitions
84 // ===========================================================================
86  : FXMainWindow(parent->getApp(), "Breakpoints Editor", NULL, NULL, DECOR_ALL, 20, 20, 300, 300),
87  myParent(parent) {
88  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0);
89 
90  // build the table
91  myTable = new MFXAddEditTypedTable(hbox, this, MID_TABLE, LAYOUT_FILL_X | LAYOUT_FILL_Y);
92  myTable->setVisibleRows(20);
93  myTable->setVisibleColumns(1);
94  myTable->setTableSize(20, 1);
95  myTable->setBackColor(FXRGB(255, 255, 255));
96  myTable->setCellType(0, CT_REAL);
97  SUMOTime begin = string2time(OptionsCont::getOptions().getString("begin"));
98  SUMOTime end = string2time(OptionsCont::getOptions().getString("end"));
99  if (end < 0) {
100  end = SUMOTime_MAX;
101  }
102  myTable->setNumberCellParams(0, begin / 1000, end / 1000, 1, 10, 100, "%.2f");
103  myTable->getRowHeader()->setWidth(0);
104  rebuildList();
105  // build the layout
106  FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0, 4, 4, 4, 4);
107  // "Load"
108  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);
109  // "Save"
110  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);
111  new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
112  // "Clear List"
113  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);
114  new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
115  // "Close"
116  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);
117  //
119  myParent->addChild(this);
120 }
121 
122 
124  myParent->removeChild(this);
125 }
126 
127 
128 void
130  myTable->clearItems();
132  // set table attributes
133  myTable->setTableSize((FXint) GUIGlobals::gBreakpoints.size() + 1, 1);
134  myTable->setColumnText(0, "Time");
135  FXHeader* header = myTable->getColumnHeader();
136  header->setHeight(getApp()->getNormalFont()->getFontHeight() + getApp()->getNormalFont()->getFontAscent());
137  int k;
138  for (k = 0; k < 1; k++) {
139  header->setItemJustify(k, JUSTIFY_CENTER_X);
140  }
141  // insert into table
142  FXint row = 0;
143  std::vector<int>::iterator j;
144  for (j = GUIGlobals::gBreakpoints.begin(); j != GUIGlobals::gBreakpoints.end(); ++j) {
145  myTable->setItemText(row, 0, time2string(*j).c_str());
146  row++;
147  }
148  // insert dummy last field
149  for (k = 0; k < 1; k++) {
150  myTable->setItemText(row, k, " ");
151  }
152 }
153 
154 
155 long
157  FXFileDialog opendialog(this, "Load Breakpoints");
158  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
159  opendialog.setSelectMode(SELECTFILE_ANY);
160  opendialog.setPatternList("*.txt");
161  if (gCurrentFolder.length() != 0) {
162  opendialog.setDirectory(gCurrentFolder);
163  }
164  if (opendialog.execute()) {
165  gCurrentFolder = opendialog.getDirectory();
166  std::string file = opendialog.getFilename().text();
167  std::ifstream strm(file.c_str());
168  while (strm.good()) {
169  std::string val;
170  strm >> val;
171  if (val.length() == 0) {
172  continue;
173  }
174  try {
175  SUMOTime value = string2time(val);
176  GUIGlobals::gBreakpoints.push_back(value);
177  } catch (NumberFormatException&) {
178  WRITE_ERROR(" A breakpoint-value must be an int, is:" + val);
179  } catch (ProcessError&) {
180  WRITE_ERROR(" Could not decode breakpoint '" + val + "'");
181  } catch (EmptyData&) {}
182  }
183  rebuildList();
184  }
185  return 1;
186 }
187 
188 
189 long
191  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
192  if (file == "") {
193  return 1;
194  }
195  std::string content = encode2TXT();
196  try {
197  OutputDevice& dev = OutputDevice::getDevice(file.text());
198  dev << content;
199  dev.close();
200  } catch (IOError& e) {
201  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
202  }
203  return 1;
204 }
205 
206 
207 std::string
209  std::ostringstream strm;
210  std::sort(GUIGlobals::gBreakpoints.begin(), GUIGlobals::gBreakpoints.end());
211  for (std::vector<int>::iterator j = GUIGlobals::gBreakpoints.begin(); j != GUIGlobals::gBreakpoints.end(); ++j) {
212  if ((*j) != INVALID_VALUE) {
213  strm << time2string(*j) << std::endl;
214  }
215  }
216  return strm.str();
217 }
218 
219 
220 long
222  GUIGlobals::gBreakpoints.clear();
223  rebuildList();
224  return 1;
225 }
226 
227 
228 
229 long
231  close(true);
232  return 1;
233 }
234 
235 
236 long
239  std::string value = i->item->getText().text();
240  // check whether the inserted value is empty
241  if (value.find_first_not_of(" ") == std::string::npos) {
242  // replace by invalid if so
243  value = INVALID_VALUE_STR;
244  }
245  int row = i->row;
246  if (row == (int) GUIGlobals::gBreakpoints.size()) {
248  }
249 
250  switch (i->col) {
251  case 0:
252  try {
254  } catch (NumberFormatException&) {
255  std::string msg = "The value must be an int, is:" + value;
256  FXMessageBox::error(this, MBOX_OK, "Number format error", "%s", msg.c_str());
257  }
258  break;
259  default:
260  break;
261  }
262  if (!i->updateOnly) {
263  rebuildList();
264  }
265  return 1;
266 }
267 
268 
269 /****************************************************************************/
270