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.dev/sumo
3 // Copyright (C) 2001-2024 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),
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(GUIApplicationWindow* parent, std::vector<SUMOTime>& breakpoints, FXMutex& breakpointLock, const SUMOTime simBegin) :
69  FXMainWindow(parent->getApp(), TL("Breakpoints Editor"), GUIIconSubSys::getIcon(GUIIcon::APP_BREAKPOINTS), nullptr, GUIDesignChooserDialog),
70  GUIPersistentWindowPos(this, "DIALOG_BREAKPOINTS", true, 20, 40, 300, 350),
71  myParent(parent),
72  myBreakpoints(&breakpoints),
73  myBreakpointLock(&breakpointLock),
74  mySimBegin(simBegin) {
75  // build main Frame
76  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
77  // build the table
78  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
79  myTable = new FXTable(layoutLeft, this, MID_TABLE, GUIDesignBreakpointTable);
80  myTable->setVisibleRows(20);
81  myTable->setVisibleColumns(1);
82  myTable->setTableSize(20, 1);
83  myTable->setBackColor(FXRGB(255, 255, 255));
84  myTable->getRowHeader()->setWidth(0);
85  myBreakpointLock->lock();
86  rebuildList();
87  myBreakpointLock->unlock();
88  // build the layout
89  FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
90  // create buttons ('&' in the label creates a hot key)
91  // "Load"
93  // "Save"
95  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
96  // "Clear List"
98  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
99  // "Close"
101  // add this dialog as child of GUIMainWindow parent
102  myParent->addChild(this);
103  loadWindowPos();
104  create();
105  show();
106 }
107 
108 
110  // remove this dialog as child of GUIMainWindow parent
111  myParent->removeChild(this);
113 }
114 
115 
116 void
118  FXMainWindow::show();
119  myTable->startInput((int)myBreakpoints->size(), 0);
120 }
121 
122 
123 void
125  myTable->clearItems();
126  sort(myBreakpoints->begin(), myBreakpoints->end());
127  // set table attributes
128  myTable->setTableSize((FXint)myBreakpoints->size() + 1, 1);
129  myTable->setColumnText(0, TL("Time"));
130  FXHeader* header = myTable->getColumnHeader();
131  header->setHeight(GUIDesignHeight);
132  header->setItemJustify(0, JUSTIFY_CENTER_X);
133  // insert into table
134  for (int row = 0; row < (int)myBreakpoints->size(); row++) {
135  myTable->setItemText(row, 0, time2string((*myBreakpoints)[row]).c_str());
136  }
137  // insert dummy last field
138  myTable->setItemText((int)myBreakpoints->size(), 0, " ");
139 }
140 
141 
142 long
143 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
144  FXFileDialog opendialog(this, TL("Load Breakpoints"));
145  opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::EMPTY));
146  opendialog.setSelectMode(SELECTFILE_ANY);
147  opendialog.setPatternList("*.txt");
148  if (gCurrentFolder.length() != 0) {
149  opendialog.setDirectory(gCurrentFolder);
150  }
151  if (opendialog.execute()) {
152  gCurrentFolder = opendialog.getDirectory();
153  std::string file = opendialog.getFilename().text();
154  std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
155  FXMutexLock lock(*myBreakpointLock);
156  myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
157  rebuildList();
158  }
159  return 1;
160 }
161 
162 
163 long
164 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
165  FXString file = MFXUtils::getFilename2Write(this, TL("Save Breakpoints"), ".txt", GUIIconSubSys::getIcon(GUIIcon::EMPTY), gCurrentFolder);
166  if (file == "") {
167  return 1;
168  }
169  std::string content = encode2TXT();
170  try {
171  OutputDevice& dev = OutputDevice::getDevice(file.text());
172  dev << content;
173  dev.close();
174  } catch (IOError& e) {
175  FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());
176  }
177  return 1;
178 }
179 
180 
181 std::string
183  FXMutexLock lock(*myBreakpointLock);
184  std::ostringstream strm;
185  std::sort(myBreakpoints->begin(), myBreakpoints->end());
186  for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
187  strm << time2string(*j) << std::endl;
188  }
189  return strm.str();
190 }
191 
192 
193 long
194 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
195  FXMutexLock lock(*myBreakpointLock);
196  myBreakpoints->clear();
197  rebuildList();
198  return 1;
199 }
200 
201 
202 long
203 GUIDialog_Breakpoints::onCmdUpdateBreakpoints(FXObject*, FXSelector, void*) {
204  FXMutexLock lock(*myBreakpointLock);
205  rebuildList();
206  return 1;
207 }
208 
209 
210 long
211 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
212  close(true);
213  return 1;
214 }
215 
216 
217 long
218 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* ptr) {
219  FXMutexLock lock(*myBreakpointLock);
220  const FXTablePos* const i = (FXTablePos*) ptr;
221  const std::string value = StringUtils::prune(myTable->getItemText(i->row, i->col).text());
222  // check whether the inserted value is empty
223  const bool empty = value.find_first_not_of(" ") == std::string::npos;
224  try {
225  SUMOTime t = -1;
226  if (!empty) {
227  t = string2time(value);
228  // round down to nearest reachable time step
229  t -= (t - mySimBegin) % DELTA_T;
230  }
231  if (i->row == (int)myBreakpoints->size()) {
232  if (!empty) {
233  myBreakpoints->push_back(t);
234  }
235  } else {
236  if (empty) {
237  myBreakpoints->erase(myBreakpoints->begin() + i->row);
238  } else {
239  (*myBreakpoints)[i->row] = t;
240  }
241  }
242  } catch (NumberFormatException&) {
243  std::string msg = "The value must be a number, is:" + value;
244  FXMessageBox::error(this, MBOX_OK, TL("Time format error"), "%s", msg.c_str());
245  } catch (ProcessError&) {
246  std::string msg = "The value must be a number or a string of the form hh:mm:ss, is:" + value;
247  FXMessageBox::error(this, MBOX_OK, TL("Time format error"), "%s", msg.c_str());
248  }
249  rebuildList();
250  return 1;
251 }
252 
253 
254 void
256  FXMainWindow::layout();
257  myTable->setColumnWidth(0, myTable->getWidth() - 1);
258 }
259 
260 
261 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:308
@ MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:603
@ MID_TABLE
The Table.
Definition: GUIAppEnum.h:539
@ MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:601
@ MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:605
@ MID_TIMELINK_BREAKPOINT
Set breakpionts from messages - Option.
Definition: GUIAppEnum.h:571
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:651
#define GUIDesignBreakpointTable
design for Breakpoint table
Definition: GUIDesigns.h:663
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:666
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:669
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:466
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frame extended in all directions
Definition: GUIDesigns.h:396
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:648
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
@ OPEN
open icons
@ SAVE
save icons
#define TL(string)
Definition: MsgHandler.h:315
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
int GUIDesignHeight
the default size for GUI elements
Definition: StdDefs.cpp:35
The main window of the SUMO-gui.
void eraseBreakpointDialog()
erase current breakpoint dialog
static FXButton * buildFXButton(FXComposite *p, const std::string &text, const std::string &tip, const std::string &help, FXIcon *ic, FXObject *tgt, FXSelector sel, FXuint opts=BUTTON_NORMAL, FXint x=0, FXint y=0, FXint w=0, FXint h=0, FXint pl=DEFAULT_PAD, FXint pr=DEFAULT_PAD, FXint pt=DEFAULT_PAD, FXint pb=DEFAULT_PAD)
build button
Definition: GUIDesigns.cpp:128
Editor for simulation breakpoints.
long onCmdUpdateBreakpoints(FXObject *, FXSelector, void *)
Called when the user clicks a time link in the message window.
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
FXMutex * myBreakpointLock
Lock for modifying the list of breakpoints.
SUMOTime mySimBegin
simulation begin
FXTable * myTable
The list that holds the ids.
GUIApplicationWindow * myParent
The parent window.
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
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()
FOX need this.
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)
Persists window position in the registry.
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:61
void close()
Closes the device and removes it from the dictionary.
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:58
Definition: json.hpp:4471