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