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