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 
49 // ===========================================================================
50 // FOX callback mapping
51 // ===========================================================================
52 FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
57  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
58 };
59 
60 FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
61 
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
67  GUISelectedStorage* str)
68  : FXMainWindow(parent->getApp(), "List of Selected Items", NULL, NULL, DECOR_ALL, 20, 20, 300, 300),
69  myParent(parent), myStorage(str) {
70  myStorage->add2Update(this);
71  FXHorizontalFrame* hbox =
72  new FXHorizontalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0,
73  0, 0, 0, 0);
74  // build the list
75  myList = new FXList(hbox, 0, 0,
76  LAYOUT_FILL_X | LAYOUT_FILL_Y | LIST_MULTIPLESELECT);
77  rebuildList();
78  // build the layout
79  FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0,
80  4, 4, 4, 4);
81  // "Load"
82  new FXButton(layout, "Load\t\t", 0, this, MID_CHOOSEN_LOAD,
83  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
84  0, 0, 0, 0, 4, 4, 3, 3);
85  // "Save"
86  new FXButton(layout, "Save\t\t", 0, this, MID_CHOOSEN_SAVE,
87  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
88  0, 0, 0, 0, 4, 4, 3, 3);
89 
90  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
91 
92  // "Deselect Chosen"
93  new FXButton(layout, "Deselect Chosen\t\t", 0, this, MID_CHOOSEN_DESELECT,
94  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
95  0, 0, 0, 0, 4, 4, 3, 3);
96  // "Clear List"
97  new FXButton(layout, "Clear\t\t", 0, this, MID_CHOOSEN_CLEAR,
98  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
99  0, 0, 0, 0, 4, 4, 3, 3);
100 
101  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
102 
103  // "Close"
104  new FXButton(layout, "Close\t\t", 0, this, MID_CANCEL,
105  ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED,
106  0, 0, 0, 0, 4, 4, 3, 3);
108  myParent->addChild(this);
109 }
110 
111 
114  myParent->removeChild(this);
115 }
116 
117 
118 void
120  myList->clearItems();
121  const std::set<GUIGlID>& chosen = gSelected.getSelected();
122  for (std::set<GUIGlID>::const_iterator i = chosen.begin(); i != chosen.end(); ++i) {
124  if (object != 0) {
125  std::string name = object->getFullName();
126  FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
127  item->setData(object);
129  }
130  }
131 }
132 
133 
134 void
136  rebuildList();
137  FXMainWindow::update();
138 }
139 
140 
141 long
142 GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
143  // get the new file name
144  FXFileDialog opendialog(this, "Open List of Selected Items");
145  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
146  opendialog.setSelectMode(SELECTFILE_EXISTING);
147  opendialog.setPatternList("*.txt\nAll files (*)");
148  if (gCurrentFolder.length() != 0) {
149  opendialog.setDirectory(gCurrentFolder);
150  }
151  if (opendialog.execute()) {
152  gCurrentFolder = opendialog.getDirectory();
153  std::string file = opendialog.getFilename().text();
154  std::string msg = gSelected.load(file);
155  if (msg != "") {
156  FXMessageBox::error(this, MBOX_OK, "Errors while loading Selection", "%s", msg.c_str());
157  }
158  rebuildList();
159  }
160  return 1;
161 }
162 
163 
164 long
165 GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
166  FXString file = MFXUtils::getFilename2Write(this, "Save List of selected Items", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
167  if (file == "") {
168  return 1;
169  }
170  try {
171  gSelected.save(file.text());
172  } catch (IOError& e) {
173  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
174  }
175  return 1;
176 }
177 
178 
179 long
180 GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
181  FXint no = myList->getNumItems();
182  FXint i;
183  std::vector<GUIGlID> selected;
184  for (i = 0; i < no; ++i) {
185  if (myList->getItem(i)->isSelected()) {
186  selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
187  }
188  }
189  // remove items from list
190  for (i = 0; i < (FXint) selected.size(); ++i) {
191  gSelected.deselect(selected[i]);
192  }
193  // rebuild list
194  rebuildList();
196  return 1;
197 }
198 
199 
200 
201 long
202 GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
203  myList->clearItems();
204  gSelected.clear();
206  return 1;
207 }
208 
209 
210 
211 long
212 GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
213  close(true);
214  return 1;
215 }
216 
217 
218 
219 /****************************************************************************/
220 
FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[]
const std::string & getFullName() const
Clear set.
Definition: GUIAppEnum.h:351
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:91
Cancel-button pressed.
Definition: GUIAppEnum.h:65
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:281
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.
Save set.
Definition: GUIAppEnum.h:349
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.
void deselect(GUIGlID id)
Deselects the object with the given id.
Deselect selected items.
Definition: GUIAppEnum.h:355
void clear()
Clears the list of selected objects.
Load set.
Definition: GUIAppEnum.h:347
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.