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-2020 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>
38 
39 
40 // ===========================================================================
41 // FOX callback mapping
42 // ===========================================================================
43 FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
48  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
49 };
50 
51 FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 
59  FXMainWindow(parent->getApp(), "List of Selected Items", GUIIconSubSys::getIcon(GUIIcon::APP_SELECTOR), nullptr, GUIDesignChooserDialog),
60  myParent(parent), myStorage(str) {
61  myStorage->add2Update(this);
62  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
63  // create layout left
64  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
65  // create frame for list
66  FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
67  // build the list and rebuild it
68  myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListMultiple);
69  rebuildList();
70  // build the layout
71  FXVerticalFrame* layout = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
72  // "Load"
73  new FXButton(layout, "&Load selection\t\t", GUIIconSubSys::getIcon(GUIIcon::OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
74  // "Save"
75  new FXButton(layout, "&Save selection\t\t", GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
76  // extra separator
77  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
78  // "Deselect Chosen"
79  new FXButton(layout, "&Deselect chosen\t\t", GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_DESELECT, GUIDesignChooserButtons);
80  // "Clear List"
81  new FXButton(layout, "&Clear selection\t\t", GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
82  // extra separator
83  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
84  // "Close"
85  new FXButton(layout, "Cl&ose\t\t", GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);
86  myParent->addChild(this);
87 }
88 
89 
92  myParent->removeChild(this);
93 }
94 
95 
96 void
98  myList->clearItems();
99  const std::set<GUIGlID>& chosen = gSelected.getSelected();
100  for (auto i : chosen) {
102  if (object != nullptr) {
103  std::string name = object->getFullName();
104  FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
105  item->setData(object);
107  }
108  }
109 }
110 
111 
112 void
114  rebuildList();
115  FXMainWindow::update();
116 }
117 
118 
119 long
120 GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
121  // get the new file name
122  FXFileDialog opendialog(this, "Open List of Selected Items");
123  opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::EMPTY));
124  opendialog.setSelectMode(SELECTFILE_EXISTING);
125  opendialog.setPatternList("*.txt\nAll files (*)");
126  if (gCurrentFolder.length() != 0) {
127  opendialog.setDirectory(gCurrentFolder);
128  }
129  if (opendialog.execute()) {
130  gCurrentFolder = opendialog.getDirectory();
131  std::string file = opendialog.getFilename().text();
132  std::string msg = gSelected.load(file);
133  if (msg != "") {
134  FXMessageBox::error(this, MBOX_OK, "Errors while loading Selection", "%s", msg.c_str());
135  }
136  rebuildList();
137  }
138  return 1;
139 }
140 
141 
142 long
143 GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
144  FXString file = MFXUtils::getFilename2Write(this, "Save List of selected Items", ".txt", GUIIconSubSys::getIcon(GUIIcon::EMPTY), gCurrentFolder);
145  if (file == "") {
146  return 1;
147  }
148  try {
149  gSelected.save(file.text());
150  } catch (IOError& e) {
151  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
152  }
153  return 1;
154 }
155 
156 
157 long
158 GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
159  FXint no = myList->getNumItems();
160  FXint i;
161  std::vector<GUIGlID> selected;
162  for (i = 0; i < no; ++i) {
163  if (myList->getItem(i)->isSelected()) {
164  selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
165  }
166  }
167  // remove items from list
168  for (i = 0; i < (FXint) selected.size(); ++i) {
169  gSelected.deselect(selected[i]);
170  }
171  // rebuild list
172  rebuildList();
174  return 1;
175 }
176 
177 
178 long
179 GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
180  myList->clearItems();
181  gSelected.clear();
183  return 1;
184 }
185 
186 
187 long
188 GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
189  close(true);
190  return 1;
191 }
192 
193 
194 /****************************************************************************/
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:247
@ MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:541
@ MID_CHOOSEN_DESELECT
Deselect selected items.
Definition: GUIAppEnum.h:547
@ MID_CHOOSER_LIST
Object list.
Definition: GUIAppEnum.h:523
@ MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:539
@ MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:543
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:537
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:552
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:555
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:362
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:558
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions
Definition: GUIDesigns.h:310
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:534
#define GUIDesignChooserListMultiple
design for Chooser List
Definition: GUIDesigns.h:546
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
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
GUIGlID getGlID() const
Returns the numerical id of the object.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void updateChildren()
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