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-2022 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// method definitions
66// ===========================================================================
67
68GUIDialog_Breakpoints::GUIDialog_Breakpoints(GUIApplicationWindow* parent, std::vector<SUMOTime>& breakpoints, FXMutex& breakpointLock) :
69 FXMainWindow(parent->getApp(), "Breakpoints Editor", GUIIconSubSys::getIcon(GUIIcon::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, TL("&Load\t\t"), GUIIconSubSys::getIcon(GUIIcon::OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
89 // "Save"
90 new FXButton(layoutRight, TL("&Save\t\t"), GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
91 new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
92 // "Clear List"
93 new FXButton(layoutRight, TL("Clea&r\t\t"), GUIIconSubSys::getIcon(GUIIcon::CLEANJUNCTIONS), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
94 new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
95 // "Close"
96 new FXButton(layoutRight, TL("&Close\t\t"), GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);
97 // add this dialog as child of GUIMainWindow parent
98 myParent->addChild(this);
99 create();
100 show();
101}
102
103
105 // remove this dialog as child of GUIMainWindow parent
106 myParent->removeChild(this);
108}
109
110
111void
113 FXMainWindow::show();
114 myTable->startInput((int)myBreakpoints->size(), 0);
115}
116
117
118void
120 myTable->clearItems();
121 sort(myBreakpoints->begin(), myBreakpoints->end());
122 // set table attributes
123 myTable->setTableSize((FXint)myBreakpoints->size() + 1, 1);
124 myTable->setColumnText(0, "Time");
125 FXHeader* header = myTable->getColumnHeader();
126 header->setHeight(GUIDesignHeight);
127 header->setItemJustify(0, JUSTIFY_CENTER_X);
128 // insert into table
129 for (int row = 0; row < (int)myBreakpoints->size(); row++) {
130 myTable->setItemText(row, 0, time2string((*myBreakpoints)[row]).c_str());
131 }
132 // insert dummy last field
133 myTable->setItemText((int)myBreakpoints->size(), 0, " ");
134}
135
136
137long
138GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
139 FXFileDialog opendialog(this, TL("Load Breakpoints"));
140 opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::EMPTY));
141 opendialog.setSelectMode(SELECTFILE_ANY);
142 opendialog.setPatternList("*.txt");
143 if (gCurrentFolder.length() != 0) {
144 opendialog.setDirectory(gCurrentFolder);
145 }
146 if (opendialog.execute()) {
147 gCurrentFolder = opendialog.getDirectory();
148 std::string file = opendialog.getFilename().text();
149 std::vector<SUMOTime> newBreakpoints = GUISettingsHandler::loadBreakpoints(file);
150 FXMutexLock lock(*myBreakpointLock);
151 myBreakpoints->assign(newBreakpoints.begin(), newBreakpoints.end());
152 rebuildList();
153 }
154 return 1;
155}
156
157
158long
159GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
160 FXString file = MFXUtils::getFilename2Write(this, TL("Save Breakpoints"), ".txt", GUIIconSubSys::getIcon(GUIIcon::EMPTY), gCurrentFolder);
161 if (file == "") {
162 return 1;
163 }
164 std::string content = encode2TXT();
165 try {
166 OutputDevice& dev = OutputDevice::getDevice(file.text());
167 dev << content;
168 dev.close();
169 } catch (IOError& e) {
170 FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());
171 }
172 return 1;
173}
174
175
176std::string
178 FXMutexLock lock(*myBreakpointLock);
179 std::ostringstream strm;
180 std::sort(myBreakpoints->begin(), myBreakpoints->end());
181 for (std::vector<SUMOTime>::iterator j = myBreakpoints->begin(); j != myBreakpoints->end(); ++j) {
182 strm << time2string(*j) << std::endl;
183 }
184 return strm.str();
185}
186
187
188long
189GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
190 FXMutexLock lock(*myBreakpointLock);
191 myBreakpoints->clear();
192 rebuildList();
193 return 1;
194}
195
196
197long
199 FXMutexLock lock(*myBreakpointLock);
200 rebuildList();
201 return 1;
202}
203
204
205long
206GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
207 close(true);
208 return 1;
209}
210
211
212long
213GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* ptr) {
214 FXMutexLock lock(*myBreakpointLock);
215 const FXTablePos* const i = (FXTablePos*) ptr;
216 const std::string value = StringUtils::prune(myTable->getItemText(i->row, i->col).text());
217 // check whether the inserted value is empty
218 const bool empty = value.find_first_not_of(" ") == std::string::npos;
219 try {
220 SUMOTime t = -1;
221 if (!empty) {
222 t = string2time(value);
223 // round down to nearest reachable time step
224 t -= t % DELTA_T;
225 }
226 if (i->row == (int)myBreakpoints->size()) {
227 if (!empty) {
228 myBreakpoints->push_back(t);
229 }
230 } else {
231 if (empty) {
232 myBreakpoints->erase(myBreakpoints->begin() + i->row);
233 } else {
234 (*myBreakpoints)[i->row] = t;
235 }
236 }
237 } catch (NumberFormatException&) {
238 std::string msg = "The value must be a number, is:" + value;
239 FXMessageBox::error(this, MBOX_OK, TL("Time format error"), "%s", msg.c_str());
240 } catch (ProcessError&) {
241 std::string msg = "The value must be a number or a string of the form hh:mm:ss, is:" + value;
242 FXMessageBox::error(this, MBOX_OK, TL("Time format error"), "%s", msg.c_str());
243 }
244 rebuildList();
245 return 1;
246}
247
248
249void
251 FXMainWindow::layout();
252 myTable->setColumnWidth(0, myTable->getWidth() - 1);
253}
254
255
256/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:261
@ MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:594
@ MID_TABLE
The Table.
Definition: GUIAppEnum.h:530
@ MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:592
@ MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:596
@ MID_TIMELINK_BREAKPOINT
Set breakpionts from messages - Option.
Definition: GUIAppEnum.h:562
#define GUIDesignHeight
define a standard height for all elements (Change it carefully)
Definition: GUIDesigns.h:37
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:630
#define GUIDesignBreakpointTable
design for Breakpoint table
Definition: GUIDesigns.h:642
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:645
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:648
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:452
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions
Definition: GUIDesigns.h:394
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:627
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
#define TL(string)
Definition: MsgHandler.h:282
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
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
The main window of the SUMO-gui.
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.
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()
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: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:55
Definition: json.hpp:4471