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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Editor for the list of chosen objects
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 #include <fstream>
40 
41 
42 // ===========================================================================
43 // FOX callback mapping
44 // ===========================================================================
45 FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
50  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
51 };
52 
53 FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 
61  FXMainWindow(parent->getApp(), "List of Selected Items", GUIIconSubSys::getIcon(ICON_APP_SELECTOR), nullptr, GUIDesignChooserDialog),
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, "&Load selection\t\t", GUIIconSubSys::getIcon(ICON_OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
76  // "Save"
77  new FXButton(layout, "&Save selection\t\t", GUIIconSubSys::getIcon(ICON_SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
78  // extra separator
79  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
80  // "Deselect Chosen"
81  new FXButton(layout, "&Deselect chosen\t\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSEN_DESELECT, GUIDesignChooserButtons);
82  // "Clear List"
83  new FXButton(layout, "&Clear selection\t\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
84  // extra separator
85  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
86  // "Close"
87  new FXButton(layout, "Cl&ose\t\t", GUIIconSubSys::getIcon(ICON_NO), this, MID_CANCEL, GUIDesignChooserButtons);
88  myParent->addChild(this);
89 }
90 
91 
94  myParent->removeChild(this);
95 }
96 
97 
98 void
100  myList->clearItems();
101  const std::set<GUIGlID>& chosen = gSelected.getSelected();
102  for (auto i : chosen) {
104  if (object != nullptr) {
105  std::string name = object->getFullName();
106  FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
107  item->setData(object);
109  }
110  }
111 }
112 
113 
114 void
116  rebuildList();
117  FXMainWindow::update();
118 }
119 
120 
121 long
122 GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
123  // get the new file name
124  FXFileDialog opendialog(this, "Open List of Selected Items");
125  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
126  opendialog.setSelectMode(SELECTFILE_EXISTING);
127  opendialog.setPatternList("*.txt\nAll files (*)");
128  if (gCurrentFolder.length() != 0) {
129  opendialog.setDirectory(gCurrentFolder);
130  }
131  if (opendialog.execute()) {
132  gCurrentFolder = opendialog.getDirectory();
133  std::string file = opendialog.getFilename().text();
134  std::string msg = gSelected.load(file);
135  if (msg != "") {
136  FXMessageBox::error(this, MBOX_OK, "Errors while loading Selection", "%s", msg.c_str());
137  }
138  rebuildList();
139  }
140  return 1;
141 }
142 
143 
144 long
145 GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
146  FXString file = MFXUtils::getFilename2Write(this, "Save List of selected Items", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
147  if (file == "") {
148  return 1;
149  }
150  try {
151  gSelected.save(file.text());
152  } catch (IOError& e) {
153  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
154  }
155  return 1;
156 }
157 
158 
159 long
160 GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
161  FXint no = myList->getNumItems();
162  FXint i;
163  std::vector<GUIGlID> selected;
164  for (i = 0; i < no; ++i) {
165  if (myList->getItem(i)->isSelected()) {
166  selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
167  }
168  }
169  // remove items from list
170  for (i = 0; i < (FXint) selected.size(); ++i) {
171  gSelected.deselect(selected[i]);
172  }
173  // rebuild list
174  rebuildList();
176  return 1;
177 }
178 
179 
180 long
181 GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
182  myList->clearItems();
183  gSelected.clear();
185  return 1;
186 }
187 
188 
189 long
190 GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
191  close(true);
192  return 1;
193 }
194 
195 
196 /****************************************************************************/
197 
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[]
Save set.
Definition: GUIAppEnum.h:499
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:490
void remove2Update()
Removes the dialog to be updated.
Editor for the list of chosen objects.
FXString gCurrentFolder
The folder used as last.
~GUIDialog_GLChosenEditor()
Destructor (Notifies both the parent and the storage about being destroyed)
Deselect selected items.
Definition: GUIAppEnum.h:505
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:84
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions ...
Definition: GUIDesigns.h:286
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:337
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:517
long onCmdDeselect(FXObject *, FXSelector, void *)
Called when the user presses the Deselect-button.
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
void rebuildList()
Rebuilds the entire list.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:511
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:493
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
Object list.
Definition: GUIAppEnum.h:483
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:514
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
Storage for "selected" objects.
#define GUIDesignChooserListMultiple
design for Chooser List
Definition: GUIDesigns.h:502
void deselect(GUIGlID id)
Deselects the object with the given id.
void removeChild(FXMainWindow *child)
void clear()
Clears the list of selected objects.
Cancel-button pressed.
Definition: GUIAppEnum.h:215
void unblockObject(GUIGlID id)
Marks an object as unblocked.
Load set.
Definition: GUIAppEnum.h:497
const std::string & getFullName() const
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user presses the Save-button.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
GUIMainWindow * myParent
The parent window.
GUISelectedStorage gSelected
A global holder of selected objects.
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
GUISelectedStorage * myStorage
The storage.
Clear set.
Definition: GUIAppEnum.h:501
void selectionUpdated()
called when selection is updated
FXList * myList
The list that holds the ids.
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.