Eclipse SUMO - Simulation of Urban MObility
GUIDialog_Breakpoints.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Editor for simulation breakpoints
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <vector>
27 #include <iostream>
28 #include <fstream>
29 #include <set>
32 #include <gui/GUIGlobals.h>
35 #include <utils/common/ToString.h>
46 #include "GUIDialog_Breakpoints.h"
47 
48 
49 // ===========================================================================
50 // FOX callback mapping
51 // ===========================================================================
52 
53 FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
54  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_LOAD, GUIDialog_Breakpoints::onCmdLoad),
55  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_SAVE, GUIDialog_Breakpoints::onCmdSave),
57  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_Breakpoints::onCmdClose),
58  FXMAPFUNC(SEL_REPLACED, MID_TABLE, GUIDialog_Breakpoints::onCmdEditTable),
59 };
60 
61 
62 FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
63 
64 // ===========================================================================
65 // method definitions
66 // ===========================================================================
67 
68 GUIDialog_Breakpoints::GUIDialog_Breakpoints(GUIMainWindow* parent, std::vector<SUMOTime>& breakpoints, FXMutex& breakpointLock) :
69  FXMainWindow(parent->getApp(), "Breakpoints Editor", GUIIconSubSys::getIcon(ICON_APP_BREAKPOINTS), nullptr, GUIDesignChooserDialog),
70  myParent(parent), myBreakpoints(&breakpoints), myBreakpointLock(&breakpointLock) {
71  // build main Frame
72  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
73  // build the table
74  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
75  myTable = new FXTable(layoutLeft, this, MID_TABLE, GUIDesignBreakpointTable);
76  myTable->setVisibleRows(20);
77  myTable->setVisibleColumns(1);
78  myTable->setTableSize(20, 1);
79  myTable->setBackColor(FXRGB(255, 255, 255));
80  myTable->getRowHeader()->setWidth(0);
81  myBreakpointLock->lock();
82  rebuildList();
83  myBreakpointLock->unlock();
84  // build the layout
85  FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
86  // create buttons ('&' in the label creates a hot key)
87  // "Load"
88  new FXButton(layoutRight, "&Load\t\t", GUIIconSubSys::getIcon(ICON_OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
89  // "Save"
90  new FXButton(layoutRight, "&Save\t\t", GUIIconSubSys::getIcon(ICON_SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
91  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
92  // "Clear List"
93  new FXButton(layoutRight, "Clea&r\t\t", GUIIconSubSys::getIcon(ICON_CLEANJUNCTIONS), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
94  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
95  // "Close"
96  new FXButton(layoutRight, "&Close\t\t", GUIIconSubSys::getIcon(ICON_NO), this, MID_CANCEL, GUIDesignChooserButtons);
97  // add this dialog as child of GUIMainWindow parent
98  myParent->addChild(this);
99 }
100 
101 
103  // remove this dialog as child of GUIMainWindow parent
104  myParent->removeChild(this);
105 }
106 
107 
108 void
110  FXMainWindow::show();
111  myTable->startInput((int)myBreakpoints->size(), 0);
112 }
113 
114 
115 void
117  myTable->clearItems();
118  sort(myBreakpoints->begin(), myBreakpoints->end());
119  // set table attributes
120  myTable->setTableSize((FXint)myBreakpoints->size() + 1, 1);
121  myTable->setColumnText(0, "Time");
122  FXHeader* header = myTable->getColumnHeader();
123  header->setHeight(GUIDesignBreakpointTableHeaderHeight);
124  header->setItemJustify(0, JUSTIFY_CENTER_X);
125  // insert into table
126  for (int row = 0; row < (int)myBreakpoints->size(); row++) {
127  myTable->setItemText(row, 0, time2string((*myBreakpoints)[row]).c_str());
128  }
129  // insert dummy last field
130  myTable->setItemText((int)myBreakpoints->size(), 0, " ");
131 }
132 
133 
134 long
135 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
136  FXFileDialog opendialog(this, "Load Breakpoints");
137  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
138  opendialog.setSelectMode(SELECTFILE_ANY);
139  opendialog.setPatternList("*.txt");
140  if (gCurrentFolder.length() != 0) {
141  opendialog.setDirectory(gCurrentFolder);
142  }
143  if (opendialog.execute()) {
144  gCurrentFolder = opendialog.getDirectory();
145  std::string file = opendialog.getFilename().text();
146  std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
147  FXMutexLock lock(*myBreakpointLock);
148  myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
149  rebuildList();
150  }
151  return 1;
152 }
153 
154 
155 long
156 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
157  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
158  if (file == "") {
159  return 1;
160  }
161  std::string content = encode2TXT();
162  try {
163  OutputDevice& dev = OutputDevice::getDevice(file.text());
164  dev << content;
165  dev.close();
166  } catch (IOError& e) {
167  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
168  }
169  return 1;
170 }
171 
172 
173 std::string
175  FXMutexLock lock(*myBreakpointLock);
176  std::ostringstream strm;
177  std::sort(myBreakpoints->begin(), myBreakpoints->end());
178  for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
179  strm << time2string(*j) << std::endl;
180  }
181  return strm.str();
182 }
183 
184 
185 long
186 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
187  FXMutexLock lock(*myBreakpointLock);
188  myBreakpoints->clear();
189  rebuildList();
190  return 1;
191 }
192 
193 
194 
195 long
196 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
197  close(true);
198  return 1;
199 }
200 
201 
202 long
203 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* ptr) {
204  FXMutexLock lock(*myBreakpointLock);
205  const FXTablePos* const i = (FXTablePos*) ptr;
206  const std::string value = StringUtils::prune(myTable->getItemText(i->row, i->col).text());
207  // check whether the inserted value is empty
208  const bool empty = value.find_first_not_of(" ") == std::string::npos;
209  try {
210  if (i->row == (int)myBreakpoints->size()) {
211  if (!empty) {
212  myBreakpoints->push_back(string2time(value));
213  }
214  } else {
215  if (empty) {
216  myBreakpoints->erase(myBreakpoints->begin() + i->row);
217  } else {
218  (*myBreakpoints)[i->row] = string2time(value);
219  }
220  }
221  } catch (NumberFormatException&) {
222  std::string msg = "The value must be a number, is:" + value;
223  FXMessageBox::error(this, MBOX_OK, "Time format error", "%s", msg.c_str());
224  } catch (ProcessError&) {
225  std::string msg = "The value must be a number or a string of the form hh:mm:ss, is:" + value;
226  FXMessageBox::error(this, MBOX_OK, "Time format error", "%s", msg.c_str());
227  }
228  rebuildList();
229  return 1;
230 }
231 
232 
233 void
235  FXMainWindow::layout();
236  myTable->setColumnWidth(0, myTable->getWidth() - 1);
237 }
238 /****************************************************************************/
239 
GUIGlObject.h
ToString.h
GUIDialog_Breakpoints::show
void show()
sets the focus after the window is created
Definition: GUIDialog_Breakpoints.cpp:109
GUIDesignChooserLayoutLeft
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:495
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
ICON_CLEANJUNCTIONS
Definition: GUIIcons.h:248
MID_TABLE
The Table.
Definition: GUIAppEnum.h:454
GUIDialog_Breakpoints::onCmdLoad
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
Definition: GUIDialog_Breakpoints.cpp:135
OptionsCont.h
ICON_EMPTY
Definition: GUIIcons.h:41
MsgHandler.h
GUIDialog_Breakpoints::encode2TXT
std::string encode2TXT()
Builds a text representation of the items in the list.
Definition: GUIDialog_Breakpoints.cpp:174
ICON_SAVE
Definition: GUIIcons.h:48
MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:518
GUIDialog_Breakpoints::myBreakpoints
std::vector< SUMOTime > * myBreakpoints
List of breakpoints.
Definition: GUIDialog_Breakpoints.h:98
FileHelpers.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
GUIDialog_Breakpoints::myBreakpointLock
FXMutex * myBreakpointLock
Lock for modifying the list of breakpoints.
Definition: GUIDialog_Breakpoints.h:101
ICON_OPEN_CONFIG
Definition: GUIIcons.h:42
ICON_NO
Definition: GUIIcons.h:120
GUIDesigns.h
MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:230
GUISettingsHandler.h
GUIIconSubSys::getIcon
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
Definition: GUIIconSubSys.cpp:609
OutputDevice::close
void close()
Closes the device and removes it from the dictionary.
Definition: OutputDevice.cpp:207
MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:514
MFXUtils::getFilename2Write
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:83
GUIAppEnum.h
GUISUMOAbstractView.h
NumberFormatException
Definition: UtilExceptions.h:95
StringUtils::prune
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
GUIDialog_Breakpoints::onCmdClear
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
Definition: GUIDialog_Breakpoints.cpp:186
GUIDialog_Breakpoints::~GUIDialog_Breakpoints
~GUIDialog_Breakpoints()
Destructor.
Definition: GUIDialog_Breakpoints.cpp:102
GUIDesignHorizontalSeparator
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:321
GUIGlobals.h
GUIMainWindow::removeChild
void removeChild(FXMainWindow *child)
Definition: GUIMainWindow.cpp:115
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:39
GUIApplicationWindow.h
GUIDesignBreakpointTableHeaderHeight
#define GUIDesignBreakpointTableHeaderHeight
Height of breakpoint Table header.
Definition: GUIDesigns.h:492
GUIDesignBreakpointTable
#define GUIDesignBreakpointTable
design for Breakpoint table
Definition: GUIDesigns.h:489
FXDEFMAP
FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[]
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
GUIDialog_Breakpoints::onCmdSave
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user presses the Save-button.
Definition: GUIDialog_Breakpoints.cpp:156
GUIIOGlobals.h
GUIDialog_Breakpoints::myTable
FXTable * myTable
The list that holds the ids.
Definition: GUIDialog_Breakpoints.h:92
GUIIconSubSys
Definition: GUIIconSubSys.h:32
GUIDesignChooserButtons
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:477
gCurrentFolder
FXString gCurrentFolder
The folder used as last.
Definition: GUIIOGlobals.cpp:32
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
GUIDialog_Breakpoints
Editor for simulation breakpoints.
Definition: GUIDialog_Breakpoints.h:42
MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:516
GUIDialog_Breakpoints.h
GUIIconSubSys.h
StringUtils.h
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:54
GUIMainWindow
Definition: GUIMainWindow.h:46
ICON_APP_BREAKPOINTS
Definition: GUIIcons.h:111
GUIDialog_Breakpoints::rebuildList
void rebuildList()
Rebuilds the entire list.
Definition: GUIDialog_Breakpoints.cpp:116
MFXUtils.h
GUIDialog_Breakpoints::onCmdEditTable
long onCmdEditTable(FXObject *, FXSelector, void *)
Called when the table was changed.
Definition: GUIDialog_Breakpoints.cpp:203
IOError
Definition: UtilExceptions.h:161
GUIDesignAuxiliarFrame
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions
Definition: GUIDesigns.h:270
config.h
GUIDesignChooserDialog
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:474
GUIDialog_Breakpoints::onCmdClose
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
Definition: GUIDialog_Breakpoints.cpp:196
GUIDialog_Breakpoints::layout
virtual void layout()
Definition: GUIDialog_Breakpoints.cpp:234
GUIDesignChooserLayoutRight
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:498
GUISettingsHandler::loadBreakpoints
static std::vector< SUMOTime > loadBreakpoints(const std::string &file)
loads breakpoints from the specified file
Definition: GUISettingsHandler.cpp:422
GUIDialog_Breakpoints::myParent
GUIMainWindow * myParent
The parent window.
Definition: GUIDialog_Breakpoints.h:95