Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GUIDialog_GLChosenEditor.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-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 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 GUIPersistentWindowPos(this, "DIALOG_EDIT_SELECTED", true, 20, 40, 300, 350),
62 myParent(parent), myStorage(str) {
63 myStorage->add2Update(this);
64 FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
65 // create layout left
66 FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
67 // create frame for list
68 FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
69 // build the list and rebuild it
70 myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListMultiple);
71 rebuildList();
72 // build the layout
73 FXVerticalFrame* layout = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
74 // "Load"
75 new FXButton(layout, (TL("&Load selection") + std::string("\t\t")).c_str(), GUIIconSubSys::getIcon(GUIIcon::OPEN), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
76 // "Save"
77 new FXButton(layout, (TL("&Save selection") + std::string("\t\t")).c_str(), GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
78 // extra separator
79 new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
80 // "Deselect Chosen"
81 new FXButton(layout, (TL("&Deselect chosen") + std::string("\t\t")).c_str(), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_DESELECT, GUIDesignChooserButtons);
82 // "Clear List"
83 new FXButton(layout, (TL("&Clear selection") + std::string("\t\t")).c_str(), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
84 // extra separator
85 new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
86 // "Close"
87 new FXButton(layout, (TL("Cl&ose") + std::string("\t\t")).c_str(), GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);
88 myParent->addChild(this);
89 loadWindowPos();
90}
91
92
97
98
99void
101 myList->clearItems();
102 const std::set<GUIGlID>& chosen = gSelected.getSelected();
103 for (auto i : chosen) {
105 if (object != nullptr) {
106 std::string name = object->getFullName();
107 FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
108 item->setData(object);
110 }
111 }
112}
113
114
115void
117 rebuildList();
118 FXMainWindow::update();
119}
120
121
122long
123GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
124 // get the new file name
125 FXFileDialog opendialog(this, TL("Open List of Selected Items"));
126 opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::OPEN));
127 opendialog.setSelectMode(SELECTFILE_EXISTING);
128 opendialog.setPatternList("*.txt\nAll files (*)");
129 if (gCurrentFolder.length() != 0) {
130 opendialog.setDirectory(gCurrentFolder);
131 }
132 if (opendialog.execute()) {
133 gCurrentFolder = opendialog.getDirectory();
134 std::string file = opendialog.getFilename().text();
135 std::string msg = gSelected.load(file);
136 if (msg != "") {
137 FXMessageBox::error(this, MBOX_OK, TL("Errors while loading Selection"), "%s", msg.c_str());
138 }
139 rebuildList();
141 }
142 return 1;
143}
144
145
146long
147GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
148 FXString file = MFXUtils::getFilename2Write(this, TL("Save List of selected Items"), ".txt", GUIIconSubSys::getIcon(GUIIcon::SAVE), gCurrentFolder);
149 if (file == "") {
150 return 1;
151 }
152 try {
153 gSelected.save(file.text());
154 } catch (IOError& e) {
155 FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());
156 }
157 return 1;
158}
159
160
161long
162GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
163 FXint no = myList->getNumItems();
164 FXint i;
165 std::vector<GUIGlID> selected;
166 for (i = 0; i < no; ++i) {
167 if (myList->getItem(i)->isSelected()) {
168 selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
169 }
170 }
171 // remove items from list
172 for (i = 0; i < (FXint) selected.size(); ++i) {
173 gSelected.deselect(selected[i]);
174 }
175 // rebuild list
176 rebuildList();
178 return 1;
179}
180
181
182long
183GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
184 myList->clearItems();
187 return 1;
188}
189
190
191long
192GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
193 close(true);
194 return 1;
195}
196
197
198/****************************************************************************/
@ MID_CANCEL
Cancel-button pressed.
Definition GUIAppEnum.h:303
@ MID_CHOOSEN_SAVE
Save set.
Definition GUIAppEnum.h:596
@ MID_CHOOSEN_DESELECT
Deselect selected items.
Definition GUIAppEnum.h:606
@ MID_CHOOSER_LIST
Object list.
Definition GUIAppEnum.h:578
@ MID_CHOOSEN_LOAD
Load set.
Definition GUIAppEnum.h:594
@ MID_CHOOSEN_CLEAR
Clear set.
Definition GUIAppEnum.h:598
#define GUIDesignChooserButtons
design for Chooser buttons
Definition GUIDesigns.h:637
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition GUIDesigns.h:652
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition GUIDesigns.h:655
#define GUIDesignHorizontalSeparator
Definition GUIDesigns.h:452
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition GUIDesigns.h:658
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frame extended in all directions
Definition GUIDesigns.h:385
#define GUIDesignChooserDialog
Definition GUIDesigns.h:634
#define GUIDesignChooserListMultiple
design for Chooser List
Definition GUIDesigns.h:646
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
@ APP_SELECTOR
@ OPEN
open icons
@ SAVE
save icons
#define TL(string)
Definition MsgHandler.h:287
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.
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)
Persists window position in the registry.
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