SUMO - Simulation of Urban MObility
GUIDialog_GLChosenEditor.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Editor for the list of chosen objects
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <vector>
35 #include <iostream>
36 #include <fstream>
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // FOX callback mapping
55 // ===========================================================================
56 FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
61  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
62 };
63 
64 FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
65 
66 
67 // ===========================================================================
68 // method definitions
69 // ===========================================================================
71  GUISelectedStorage* str)
72  : FXMainWindow(parent->getApp(), "List of Selected Items", NULL, NULL, DECOR_ALL, 20, 20, 300, 300),
73  myParent(parent), myStorage(str) {
74  myStorage->add2Update(this);
75  FXHorizontalFrame* hbox =
76  new FXHorizontalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0,
77  0, 0, 0, 0);
78  // build the list
79  myList = new FXList(hbox, 0, 0,
80  LAYOUT_FILL_X | LAYOUT_FILL_Y | LIST_MULTIPLESELECT);
81  rebuildList();
82  // build the layout
83  FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0,
84  4, 4, 4, 4);
85  // "Load"
86  new FXButton(layout, "Load\t\t", 0, this, MID_CHOOSEN_LOAD,
87  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
88  0, 0, 0, 0, 4, 4, 3, 3);
89  // "Save"
90  new FXButton(layout, "Save\t\t", 0, this, MID_CHOOSEN_SAVE,
91  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
92  0, 0, 0, 0, 4, 4, 3, 3);
93 
94  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
95 
96  // "Deselect Chosen"
97  new FXButton(layout, "Deselect Chosen\t\t", 0, this, MID_CHOOSEN_DESELECT,
98  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
99  0, 0, 0, 0, 4, 4, 3, 3);
100  // "Clear List"
101  new FXButton(layout, "Clear\t\t", 0, this, MID_CHOOSEN_CLEAR,
102  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
103  0, 0, 0, 0, 4, 4, 3, 3);
104 
105  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
106 
107  // "Close"
108  new FXButton(layout, "Close\t\t", 0, this, MID_CANCEL,
109  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
110  0, 0, 0, 0, 4, 4, 3, 3);
112  myParent->addChild(this);
113 }
114 
115 
118  myParent->removeChild(this);
119 }
120 
121 
122 void
124  myList->clearItems();
125  const std::set<GUIGlID>& chosen = gSelected.getSelected();
126  for (std::set<GUIGlID>::const_iterator i = chosen.begin(); i != chosen.end(); ++i) {
128  if (object != 0) {
129  std::string name = object->getFullName();
130  FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
131  item->setData(object);
133  }
134  }
135 }
136 
137 
138 void
140  rebuildList();
141  FXMainWindow::update();
142 }
143 
144 
145 long
146 GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
147  // get the new file name
148  FXFileDialog opendialog(this, "Open List of Selected Items");
149  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
150  opendialog.setSelectMode(SELECTFILE_EXISTING);
151  opendialog.setPatternList("*.txt\nAll files (*)");
152  if (gCurrentFolder.length() != 0) {
153  opendialog.setDirectory(gCurrentFolder);
154  }
155  if (opendialog.execute()) {
156  gCurrentFolder = opendialog.getDirectory();
157  std::string file = opendialog.getFilename().text();
158  std::string msg = gSelected.load(file);
159  if (msg != "") {
160  FXMessageBox::error(this, MBOX_OK, "Errors while loading Selection", "%s", msg.c_str());
161  }
162  rebuildList();
163  }
164  return 1;
165 }
166 
167 
168 long
169 GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
170  FXString file = MFXUtils::getFilename2Write(this, "Save List of selected Items", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
171  if (file == "") {
172  return 1;
173  }
174  try {
175  gSelected.save(file.text());
176  } catch (IOError& e) {
177  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
178  }
179  return 1;
180 }
181 
182 
183 long
184 GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
185  FXint no = myList->getNumItems();
186  FXint i;
187  std::vector<GUIGlID> selected;
188  for (i = 0; i < no; ++i) {
189  if (myList->getItem(i)->isSelected()) {
190  selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
191  }
192  }
193  // remove items from list
194  for (i = 0; i < (FXint) selected.size(); ++i) {
195  gSelected.deselect(selected[i]);
196  }
197  // rebuild list
198  rebuildList();
200  return 1;
201 }
202 
203 
204 
205 long
206 GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
207  myList->clearItems();
208  gSelected.clear();
210  return 1;
211 }
212 
213 
214 
215 long
216 GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
217  close(true);
218  return 1;
219 }
220 
221 
222 
223 /****************************************************************************/
224 
FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[]
const std::string & getFullName() const
void remove2Update()
Removes the dialog to be updated.
Editor for the list of chosen objects.
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:95
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:266
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.
Cancel-button pressed.
Definition: GUIAppEnum.h:65
Save set.
Definition: GUIAppEnum.h:347
Clear set.
Definition: GUIAppEnum.h:349
void deselect(GUIGlID id)
Deselects the object with the given id.
Deselect selected items.
Definition: GUIAppEnum.h:353
Load set.
Definition: GUIAppEnum.h:345
void clear()
Clears the list of selected objects.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
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.