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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Editor for the list of chosen objects
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <vector>
34 #include <iostream>
35 #include <fstream>
46 
47 
48 // ===========================================================================
49 // FOX callback mapping
50 // ===========================================================================
51 FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
56  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
57 };
58 
59 FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
66  GUISelectedStorage* str)
67  : FXMainWindow(parent->getApp(), "List of Selected Items", NULL, NULL, DECOR_ALL, 20, 20, 300, 300),
68  myParent(parent), myStorage(str) {
69  myStorage->add2Update(this);
70  FXHorizontalFrame* hbox =
71  new FXHorizontalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0,
72  0, 0, 0, 0);
73  // build the list
74  myList = new FXList(hbox, 0, 0,
75  LAYOUT_FILL_X | LAYOUT_FILL_Y | LIST_MULTIPLESELECT);
76  rebuildList();
77  // build the layout
78  FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0,
79  4, 4, 4, 4);
80  // "Load"
81  new FXButton(layout, "Load\t\t", 0, this, MID_CHOOSEN_LOAD,
82  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
83  0, 0, 0, 0, 4, 4, 3, 3);
84  // "Save"
85  new FXButton(layout, "Save\t\t", 0, this, MID_CHOOSEN_SAVE,
86  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
87  0, 0, 0, 0, 4, 4, 3, 3);
88 
89  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
90 
91  // "Deselect Chosen"
92  new FXButton(layout, "Deselect Chosen\t\t", 0, this, MID_CHOOSEN_DESELECT,
93  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
94  0, 0, 0, 0, 4, 4, 3, 3);
95  // "Clear List"
96  new FXButton(layout, "Clear\t\t", 0, this, MID_CHOOSEN_CLEAR,
97  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
98  0, 0, 0, 0, 4, 4, 3, 3);
99 
100  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
101 
102  // "Close"
103  new FXButton(layout, "Close\t\t", 0, this, MID_CANCEL,
104  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
105  0, 0, 0, 0, 4, 4, 3, 3);
107  myParent->addChild(this);
108 }
109 
110 
113  myParent->removeChild(this);
114 }
115 
116 
117 void
119  myList->clearItems();
120  const std::set<GUIGlID>& chosen = gSelected.getSelected();
121  for (std::set<GUIGlID>::const_iterator i = chosen.begin(); i != chosen.end(); ++i) {
123  if (object != 0) {
124  std::string name = object->getFullName();
125  FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
126  item->setData(object);
128  }
129  }
130 }
131 
132 
133 void
135  rebuildList();
136  FXMainWindow::update();
137 }
138 
139 
140 long
141 GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
142  // get the new file name
143  FXFileDialog opendialog(this, "Open List of Selected Items");
144  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
145  opendialog.setSelectMode(SELECTFILE_EXISTING);
146  opendialog.setPatternList("*.txt\nAll files (*)");
147  if (gCurrentFolder.length() != 0) {
148  opendialog.setDirectory(gCurrentFolder);
149  }
150  if (opendialog.execute()) {
151  gCurrentFolder = opendialog.getDirectory();
152  std::string file = opendialog.getFilename().text();
153  std::string msg = gSelected.load(file);
154  if (msg != "") {
155  FXMessageBox::error(this, MBOX_OK, "Errors while loading Selection", "%s", msg.c_str());
156  }
157  rebuildList();
158  }
159  return 1;
160 }
161 
162 
163 long
164 GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
165  FXString file = MFXUtils::getFilename2Write(this, "Save List of selected Items", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
166  if (file == "") {
167  return 1;
168  }
169  try {
170  gSelected.save(file.text());
171  } catch (IOError& e) {
172  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
173  }
174  return 1;
175 }
176 
177 
178 long
179 GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
180  FXint no = myList->getNumItems();
181  FXint i;
182  std::vector<GUIGlID> selected;
183  for (i = 0; i < no; ++i) {
184  if (myList->getItem(i)->isSelected()) {
185  selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
186  }
187  }
188  // remove items from list
189  for (i = 0; i < (FXint) selected.size(); ++i) {
190  gSelected.deselect(selected[i]);
191  }
192  // rebuild list
193  rebuildList();
195  return 1;
196 }
197 
198 
199 
200 long
201 GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
202  myList->clearItems();
203  gSelected.clear();
205  return 1;
206 }
207 
208 
209 
210 long
211 GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
212  close(true);
213  return 1;
214 }
215 
216 
217 
218 /****************************************************************************/
219 
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[]
void remove2Update()
Removes the dialog to be updated.
Editor for the list of chosen objects.
Cancel-button pressed.
Definition: GUIAppEnum.h:64
Clear set.
Definition: GUIAppEnum.h:348
FXString gCurrentFolder
The folder used as last.
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:90
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
Load set.
Definition: GUIAppEnum.h:344
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:283
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.
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
void removeChild(FXMDIChild *child)
removes the given child window from the list
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
Storage for "selected" objects.
Save set.
Definition: GUIAppEnum.h:346
void deselect(GUIGlID id)
Deselects the object with the given id.
Deselect selected items.
Definition: GUIAppEnum.h:352
void clear()
Clears the list of selected objects.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
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.
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.