Eclipse SUMO - Simulation of Urban MObility
GUIDialog_GLChosenEditor.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 the list of chosen objects
21/****************************************************************************/
22#include <config.h>
23
24#include <string>
25#include <vector>
26#include <iostream>
27#include <fstream>
39
40
41// ===========================================================================
42// FOX callback mapping
43// ===========================================================================
44FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
49 FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
50};
51
52FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
53
54
55// ===========================================================================
56// method definitions
57// ===========================================================================
58
60 FXMainWindow(parent->getApp(), "List of Selected Items", GUIIconSubSys::getIcon(GUIIcon::APP_SELECTOR), nullptr, GUIDesignChooserDialog),
61 myParent(parent), myStorage(str) {
62 myStorage->add2Update(this);
63 FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
64 // create layout left
65 FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
66 // create frame for list
67 FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
68 // build the list and rebuild it
69 myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListMultiple);
70 rebuildList();
71 // build the layout
72 FXVerticalFrame* layout = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
73 // "Load"
74 new FXButton(layout, TL("&Load selection\t\t"), GUIIconSubSys::getIcon(GUIIcon::OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
75 // "Save"
76 new FXButton(layout, TL("&Save selection\t\t"), GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
77 // extra separator
78 new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
79 // "Deselect Chosen"
80 new FXButton(layout, TL("&Deselect chosen\t\t"), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_DESELECT, GUIDesignChooserButtons);
81 // "Clear List"
82 new FXButton(layout, TL("&Clear selection\t\t"), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
83 // extra separator
84 new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
85 // "Close"
86 new FXButton(layout, TL("Cl&ose\t\t"), GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);
87 myParent->addChild(this);
88}
89
90
93 myParent->removeChild(this);
94}
95
96
97void
99 myList->clearItems();
100 const std::set<GUIGlID>& chosen = gSelected.getSelected();
101 for (auto i : chosen) {
103 if (object != nullptr) {
104 std::string name = object->getFullName();
105 FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
106 item->setData(object);
108 }
109 }
110}
111
112
113void
115 rebuildList();
116 FXMainWindow::update();
117}
118
119
120long
121GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
122 // get the new file name
123 FXFileDialog opendialog(this, TL("Open List of Selected Items"));
124 opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::OPEN_CONFIG));
125 opendialog.setSelectMode(SELECTFILE_EXISTING);
126 opendialog.setPatternList("*.txt\nAll files (*)");
127 if (gCurrentFolder.length() != 0) {
128 opendialog.setDirectory(gCurrentFolder);
129 }
130 if (opendialog.execute()) {
131 gCurrentFolder = opendialog.getDirectory();
132 std::string file = opendialog.getFilename().text();
133 std::string msg = gSelected.load(file);
134 if (msg != "") {
135 FXMessageBox::error(this, MBOX_OK, TL("Errors while loading Selection"), "%s", msg.c_str());
136 }
137 rebuildList();
138 }
139 return 1;
140}
141
142
143long
144GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
145 FXString file = MFXUtils::getFilename2Write(this, TL("Save List of selected Items"), ".txt", GUIIconSubSys::getIcon(GUIIcon::SAVE), gCurrentFolder);
146 if (file == "") {
147 return 1;
148 }
149 try {
150 gSelected.save(file.text());
151 } catch (IOError& e) {
152 FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());
153 }
154 return 1;
155}
156
157
158long
159GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
160 FXint no = myList->getNumItems();
161 FXint i;
162 std::vector<GUIGlID> selected;
163 for (i = 0; i < no; ++i) {
164 if (myList->getItem(i)->isSelected()) {
165 selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
166 }
167 }
168 // remove items from list
169 for (i = 0; i < (FXint) selected.size(); ++i) {
170 gSelected.deselect(selected[i]);
171 }
172 // rebuild list
173 rebuildList();
175 return 1;
176}
177
178
179long
180GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
181 myList->clearItems();
184 return 1;
185}
186
187
188long
189GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
190 close(true);
191 return 1;
192}
193
194
195/****************************************************************************/
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:261
@ MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:594
@ MID_CHOOSEN_DESELECT
Deselect selected items.
Definition: GUIAppEnum.h:604
@ MID_CHOOSER_LIST
Object list.
Definition: GUIAppEnum.h:576
@ MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:592
@ MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:596
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:630
#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 GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:651
#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
#define GUIDesignChooserListMultiple
design for Chooser List
Definition: GUIDesigns.h:639
FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[]
GUISelectedStorage gSelected
A global holder of selected objects.
FXString gCurrentFolder
The folder used as last.
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:33
#define TL(string)
Definition: MsgHandler.h:282
Editor for the list of chosen objects.
FXList * myList
The list that holds the ids.
GUISelectedStorage * myStorage
The storage.
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
long onCmdDeselect(FXObject *, FXSelector, void *)
Called when the user presses the Deselect-button.
GUIMainWindow * myParent
The parent window.
~GUIDialog_GLChosenEditor()
Destructor (Notifies both the parent and the storage about being destroyed)
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user presses the Save-button.
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
void selectionUpdated()
called when selection is updated
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
void rebuildList()
Rebuilds the entire list.
const std::string & getFullName() const
Definition: GUIGlObject.h:92
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.h:102
void unblockObject(GUIGlID id)
Marks an object as unblocked.
GUIGlObject * getObjectBlocking(GUIGlID id) const
Returns the object from the container locking it.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void updateChildren(int msg=MID_SIMSTEP)
update childrens
void removeChild(FXMainWindow *child)
removes the given child window from the list (FXMainWindow)
Storage for "selected" objects.
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
void clear()
Clears the list of selected objects.
void remove2Update()
Removes the dialog to be updated.
void deselect(GUIGlID id)
Deselects the object with the given id.
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
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