Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2025 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>
46
47
48// ===========================================================================
49// FOX callback mapping
50// ===========================================================================
51
52FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
56 FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_Breakpoints::onCmdClose),
58 FXMAPFUNC(SEL_REPLACED, MID_TABLE, GUIDialog_Breakpoints::onCmdEditTable),
59};
60
61
62FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
63
64
65// ===========================================================================
66// method definitions
67// ===========================================================================
68GUIDialog_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 myParent(parent),
71 myBreakpoints(&breakpoints),
72 myBreakpointLock(&breakpointLock),
73 mySimBegin(simBegin) {
74 // build main Frame
75 FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
76 // build the table
77 FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
78 myTable = new FXTable(layoutLeft, this, MID_TABLE, GUIDesignBreakpointTable);
79 myTable->setVisibleRows(20);
80 myTable->setVisibleColumns(1);
81 myTable->setTableSize(20, 1);
82 myTable->setBackColor(GUIDesignBackgroundColorWhite);
83 myTable->getRowHeader()->setWidth(0);
84 myBreakpointLock->lock();
85 rebuildList();
86 myBreakpointLock->unlock();
87 // build the layout
88 FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
89 // create buttons ('&' in the label creates a hot key)
90 // "Load"
92 // "Save"
94 new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
95 // "Clear List"
97 new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
98 // "Close"
100 // add this dialog as child of GUIMainWindow parent
101 myParent->addChild(this);
102 myPersistentPos = std::unique_ptr<GUIPersistentWindowPos>(new GUIPersistentWindowPos(this, "DIALOG_BREAKPOINTS", true, 20, 40, 300, 350));
103 myPersistentPos->loadWindowPos();
104 create();
105 show();
106}
107
108
110 // remove this dialog as child of GUIMainWindow parent
111 myParent->removeChild(this);
113}
114
115
116void
118 FXMainWindow::show();
119 myTable->startInput((int)myBreakpoints->size(), 0);
120}
121
122
123void
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
142long
143GUIDialog_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(SUMOXMLDefinitions::TXTFileExtensions.getMultilineString().c_str());
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
163long
164GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
165 FXString file = MFXUtils::getFilename2Write(this, TL("Save Breakpoints"),
166 SUMOXMLDefinitions::TXTFileExtensions.getMultilineString().c_str(),
168 if (file == "") {
169 return 1;
170 }
171 std::string content = encode2TXT();
172 try {
173 OutputDevice& dev = OutputDevice::getDevice(file.text());
174 dev << content;
175 dev.close();
176 } catch (IOError& e) {
177 FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());
178 }
179 return 1;
180}
181
182
183std::string
185 FXMutexLock lock(*myBreakpointLock);
186 std::ostringstream strm;
187 std::sort(myBreakpoints->begin(), myBreakpoints->end());
188 for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
189 strm << time2string(*j) << std::endl;
190 }
191 return strm.str();
192}
193
194
195long
196GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
197 FXMutexLock lock(*myBreakpointLock);
198 myBreakpoints->clear();
199 rebuildList();
200 return 1;
201}
202
203
204long
206 FXMutexLock lock(*myBreakpointLock);
207 rebuildList();
208 return 1;
209}
210
211
212long
213GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
214 close(true);
215 return 1;
216}
217
218
219long
220GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* ptr) {
221 FXMutexLock lock(*myBreakpointLock);
222 const FXTablePos* const i = (FXTablePos*) ptr;
223 const std::string value = StringUtils::prune(myTable->getItemText(i->row, i->col).text());
224 // check whether the inserted value is empty
225 const bool empty = value.find_first_not_of(" ") == std::string::npos;
226 try {
227 SUMOTime t = -1;
228 if (!empty) {
229 t = string2time(value);
230 // round down to nearest reachable time step
231 t -= (t - mySimBegin) % DELTA_T;
232 }
233 if (i->row == (int)myBreakpoints->size()) {
234 if (!empty) {
235 myBreakpoints->push_back(t);
236 }
237 } else {
238 if (empty) {
239 myBreakpoints->erase(myBreakpoints->begin() + i->row);
240 } else {
241 (*myBreakpoints)[i->row] = t;
242 }
243 }
244 } catch (NumberFormatException&) {
245 std::string msg = "The value must be a number, is:" + value;
246 FXMessageBox::error(this, MBOX_OK, TL("Time format error"), "%s", msg.c_str());
247 } catch (ProcessError&) {
248 std::string msg = "The value must be a number or a string of the form hh:mm:ss, is:" + value;
249 FXMessageBox::error(this, MBOX_OK, TL("Time format error"), "%s", msg.c_str());
250 }
251 rebuildList();
252 return 1;
253}
254
255
256void
258 FXMainWindow::layout();
259 myTable->setColumnWidth(0, myTable->getWidth() - 1);
260}
261
262
263/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
@ 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:664
#define GUIDesignBreakpointTable
design for Breakpoint table
Definition GUIDesigns.h:676
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition GUIDesigns.h:679
#define GUIDesignBackgroundColorWhite
white background color (for valid text)
Definition GUIDesigns.h:47
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition GUIDesigns.h:682
#define GUIDesignHorizontalSeparator
Definition GUIDesigns.h:494
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frame extended in all directions
Definition GUIDesigns.h:409
#define GUIDesignChooserDialog
Definition GUIDesigns.h:661
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
@ APP_BREAKPOINTS
@ SAVE
save icons
#define TL(string)
Definition MsgHandler.h:305
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:91
int GUIDesignHeight
the default height for GUI elements
Definition StdDefs.cpp:37
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
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 &extensions, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition MFXUtils.cpp:116
Static storage of an output device and its base (abstract) implementation.
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 StringBijection< TXTFileExtension > TXTFileExtensions
TXT file Extensions.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition json.hpp:4471