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, "Save 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  try {
172  SUMOTime value = string2time(val);
173  GUIGlobals::gBreakpoints.push_back(value);
174  } catch (NumberFormatException&) {
175  WRITE_ERROR(" A breakpoint-value must be an int, is:" + val);
176  } catch (EmptyData&) {}
177  }
178  rebuildList();
179  }
180  return 1;
181 }
182 
183 
184 long
186  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
187  if (file == "") {
188  return 1;
189  }
190  std::string content = encode2TXT();
191  try {
192  OutputDevice& dev = OutputDevice::getDevice(file.text());
193  dev << content;
194  dev.close();
195  } catch (IOError& e) {
196  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
197  }
198  return 1;
199 }
200 
201 
202 std::string
204  std::ostringstream strm;
205  std::sort(GUIGlobals::gBreakpoints.begin(), GUIGlobals::gBreakpoints.end());
206  for (std::vector<int>::iterator j = GUIGlobals::gBreakpoints.begin(); j != GUIGlobals::gBreakpoints.end(); ++j) {
207  if ((*j) != INVALID_VALUE) {
208  strm << time2string(*j) << std::endl;
209  }
210  }
211  return strm.str();
212 }
213 
214 
215 long
217  GUIGlobals::gBreakpoints.clear();
218  rebuildList();
219  return 1;
220 }
221 
222 
223 
224 long
226  close(true);
227  return 1;
228 }
229 
230 
231 long
234  std::string value = i->item->getText().text();
235  // check whether the inserted value is empty
236  if (value.find_first_not_of(" ") == std::string::npos) {
237  // replace by invalid if so
238  value = INVALID_VALUE_STR;
239  }
240  int row = i->row;
241  if (row == (int) GUIGlobals::gBreakpoints.size()) {
243  }
244 
245  switch (i->col) {
246  case 0:
247  try {
249  } catch (NumberFormatException&) {
250  std::string msg = "The value must be an int, is:" + value;
251  FXMessageBox::error(this, MBOX_OK, "Number format error", "%s", msg.c_str());
252  }
253  break;
254  default:
255  break;
256  }
257  if (!i->updateOnly) {
258  rebuildList();
259  }
260  return 1;
261 }
262 
263 
264 /****************************************************************************/
265