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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Editor for simulation breakpoints
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
25 #include <vector>
26 #include <iostream>
27 #include <fstream>
28 #include <set>
31 #include <gui/GUIGlobals.h>
34 #include <utils/common/ToString.h>
45 #include "GUIDialog_Breakpoints.h"
46 
47 
48 // ===========================================================================
49 // FOX callback mapping
50 // ===========================================================================
51 
52 FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
53  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_LOAD, GUIDialog_Breakpoints::onCmdLoad),
54  FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_SAVE, GUIDialog_Breakpoints::onCmdSave),
56  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_Breakpoints::onCmdClose),
57  FXMAPFUNC(SEL_REPLACED, MID_TABLE, GUIDialog_Breakpoints::onCmdEditTable),
58 };
59 
60 
61 FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 
67 GUIDialog_Breakpoints::GUIDialog_Breakpoints(GUIMainWindow* parent, std::vector<SUMOTime>& breakpoints, FXMutex& breakpointLock) :
68  FXMainWindow(parent->getApp(), "Breakpoints Editor", GUIIconSubSys::getIcon(GUIIcon::APP_BREAKPOINTS), nullptr, GUIDesignChooserDialog),
69  myParent(parent), myBreakpoints(&breakpoints), myBreakpointLock(&breakpointLock) {
70  // build main Frame
71  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
72  // build the table
73  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
74  myTable = new FXTable(layoutLeft, this, MID_TABLE, GUIDesignBreakpointTable);
75  myTable->setVisibleRows(20);
76  myTable->setVisibleColumns(1);
77  myTable->setTableSize(20, 1);
78  myTable->setBackColor(FXRGB(255, 255, 255));
79  myTable->getRowHeader()->setWidth(0);
80  myBreakpointLock->lock();
81  rebuildList();
82  myBreakpointLock->unlock();
83  // build the layout
84  FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
85  // create buttons ('&' in the label creates a hot key)
86  // "Load"
87  new FXButton(layoutRight, "&Load\t\t", GUIIconSubSys::getIcon(GUIIcon::OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
88  // "Save"
89  new FXButton(layoutRight, "&Save\t\t", GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
90  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
91  // "Clear List"
92  new FXButton(layoutRight, "Clea&r\t\t", GUIIconSubSys::getIcon(GUIIcon::CLEANJUNCTIONS), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
93  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
94  // "Close"
95  new FXButton(layoutRight, "&Close\t\t", GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);
96  // add this dialog as child of GUIMainWindow parent
97  myParent->addChild(this);
98 }
99 
100 
102  // remove this dialog as child of GUIMainWindow parent
103  myParent->removeChild(this);
104 }
105 
106 
107 void
109  FXMainWindow::show();
110  myTable->startInput((int)myBreakpoints->size(), 0);
111 }
112 
113 
114 void
116  myTable->clearItems();
117  sort(myBreakpoints->begin(), myBreakpoints->end());
118  // set table attributes
119  myTable->setTableSize((FXint)myBreakpoints->size() + 1, 1);
120  myTable->setColumnText(0, "Time");
121  FXHeader* header = myTable->getColumnHeader();
122  header->setHeight(GUIDesignHeight);
123  header->setItemJustify(0, JUSTIFY_CENTER_X);
124  // insert into table
125  for (int row = 0; row < (int)myBreakpoints->size(); row++) {
126  myTable->setItemText(row, 0, time2string((*myBreakpoints)[row]).c_str());
127  }
128  // insert dummy last field
129  myTable->setItemText((int)myBreakpoints->size(), 0, " ");
130 }
131 
132 
133 long
134 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
135  FXFileDialog opendialog(this, "Load Breakpoints");
136  opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::EMPTY));
137  opendialog.setSelectMode(SELECTFILE_ANY);
138  opendialog.setPatternList("*.txt");
139  if (gCurrentFolder.length() != 0) {
140  opendialog.setDirectory(gCurrentFolder);
141  }
142  if (opendialog.execute()) {
143  gCurrentFolder = opendialog.getDirectory();
144  std::string file = opendialog.getFilename().text();
145  std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
146  FXMutexLock lock(*myBreakpointLock);
147  myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
148  rebuildList();
149  }
150  return 1;
151 }
152 
153 
154 long
155 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
156  FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(GUIIcon::EMPTY), gCurrentFolder);
157  if (file == "") {
158  return 1;
159  }
160  std::string content = encode2TXT();
161  try {
162  OutputDevice& dev = OutputDevice::getDevice(file.text());
163  dev << content;
164  dev.close();
165  } catch (IOError& e) {
166  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
167  }
168  return 1;
169 }
170 
171 
172 std::string
174  FXMutexLock lock(*myBreakpointLock);
175  std::ostringstream strm;
176  std::sort(myBreakpoints->begin(), myBreakpoints->end());
177  for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
178  strm << time2string(*j) << std::endl;
179  }
180  return strm.str();
181 }
182 
183 
184 long
185 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
186  FXMutexLock lock(*myBreakpointLock);
187  myBreakpoints->clear();
188  rebuildList();
189  return 1;
190 }
191 
192 
193 
194 long
195 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
196  close(true);
197  return 1;
198 }
199 
200 
201 long
202 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* ptr) {
203  FXMutexLock lock(*myBreakpointLock);
204  const FXTablePos* const i = (FXTablePos*) ptr;
205  const std::string value = StringUtils::prune(myTable->getItemText(i->row, i->col).text());
206  // check whether the inserted value is empty
207  const bool empty = value.find_first_not_of(" ") == std::string::npos;
208  try {
209  if (i->row == (int)myBreakpoints->size()) {
210  if (!empty) {
211  myBreakpoints->push_back(string2time(value));
212  }
213  } else {
214  if (empty) {
215  myBreakpoints->erase(myBreakpoints->begin() + i->row);
216  } else {
217  (*myBreakpoints)[i->row] = string2time(value);
218  }
219  }
220  } catch (NumberFormatException&) {
221  std::string msg = "The value must be a number, is:" + value;
222  FXMessageBox::error(this, MBOX_OK, "Time format error", "%s", msg.c_str());
223  } catch (ProcessError&) {
224  std::string msg = "The value must be a number or a string of the form hh:mm:ss, is:" + value;
225  FXMessageBox::error(this, MBOX_OK, "Time format error", "%s", msg.c_str());
226  }
227  rebuildList();
228  return 1;
229 }
230 
231 
232 void
234  FXMainWindow::layout();
235  myTable->setColumnWidth(0, myTable->getWidth() - 1);
236 }
237 
238 
239 /****************************************************************************/
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:247
@ MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:541
@ MID_TABLE
The Table.
Definition: GUIAppEnum.h:479
@ MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:539
@ MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:543
#define GUIDesignHeight
define a standard height for all elements (Change it carefully)
Definition: GUIDesigns.h:31
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:537
#define GUIDesignBreakpointTable
design for Breakpoint table
Definition: GUIDesigns.h:549
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:552
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:555
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:362
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions
Definition: GUIDesigns.h:310
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:534
FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[]
FXString gCurrentFolder
The folder used as last.
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:33
@ CLEANJUNCTIONS
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:31
Editor for simulation breakpoints.
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
FXMutex * myBreakpointLock
Lock for modifying the list of breakpoints.
FXTable * myTable
The list that holds the ids.
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
GUIMainWindow * myParent
The parent window.
std::vector< SUMOTime > * myBreakpoints
List of breakpoints.
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
void show()
sets the focus after the window is created
long onCmdEditTable(FXObject *, FXSelector, void *)
Called when the table was changed.
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user presses the Save-button.
void rebuildList()
Rebuilds the entire list.
std::string encode2TXT()
Builds a text representation of the items in the list.
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void removeChild(FXMainWindow *child)
removes the given child window from the list (FXMainWindow)
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:82
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
void close()
Closes the device and removes it from the dictionary.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:47