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 /****************************************************************************/
16 // Editor for the list of chosen objects
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <vector>
27 #include <iostream>
28 #include <fstream>
39 
40 
41 // ===========================================================================
42 // FOX callback mapping
43 // ===========================================================================
44 FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {
49  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),
50 };
51 
52 FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 
60  FXMainWindow(parent->getApp(), "List of Selected Items", GUIIconSubSys::getIcon(ICON_APP_SELECTOR), nullptr, GUIDesignChooserDialog),
61  myParent(parent), myStorage(str) {
62  myStorage->add2Update(this);
63  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
64  // create layout left
65  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
66  // create frame for list
67  FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
68  // build the list and rebuild it
69  myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListMultiple);
70  rebuildList();
71  // build the layout
72  FXVerticalFrame* layout = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
73  // "Load"
74  new FXButton(layout, "&Load selection\t\t", GUIIconSubSys::getIcon(ICON_OPEN_CONFIG), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);
75  // "Save"
76  new FXButton(layout, "&Save selection\t\t", GUIIconSubSys::getIcon(ICON_SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);
77  // extra separator
78  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
79  // "Deselect Chosen"
80  new FXButton(layout, "&Deselect chosen\t\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSEN_DESELECT, GUIDesignChooserButtons);
81  // "Clear List"
82  new FXButton(layout, "&Clear selection\t\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
83  // extra separator
84  new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);
85  // "Close"
86  new FXButton(layout, "Cl&ose\t\t", GUIIconSubSys::getIcon(ICON_NO), this, MID_CANCEL, GUIDesignChooserButtons);
87  myParent->addChild(this);
88 }
89 
90 
93  myParent->removeChild(this);
94 }
95 
96 
97 void
99  myList->clearItems();
100  const std::set<GUIGlID>& chosen = gSelected.getSelected();
101  for (auto i : chosen) {
103  if (object != nullptr) {
104  std::string name = object->getFullName();
105  FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));
106  item->setData(object);
108  }
109  }
110 }
111 
112 
113 void
115  rebuildList();
116  FXMainWindow::update();
117 }
118 
119 
120 long
121 GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {
122  // get the new file name
123  FXFileDialog opendialog(this, "Open List of Selected Items");
124  opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
125  opendialog.setSelectMode(SELECTFILE_EXISTING);
126  opendialog.setPatternList("*.txt\nAll files (*)");
127  if (gCurrentFolder.length() != 0) {
128  opendialog.setDirectory(gCurrentFolder);
129  }
130  if (opendialog.execute()) {
131  gCurrentFolder = opendialog.getDirectory();
132  std::string file = opendialog.getFilename().text();
133  std::string msg = gSelected.load(file);
134  if (msg != "") {
135  FXMessageBox::error(this, MBOX_OK, "Errors while loading Selection", "%s", msg.c_str());
136  }
137  rebuildList();
138  }
139  return 1;
140 }
141 
142 
143 long
144 GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {
145  FXString file = MFXUtils::getFilename2Write(this, "Save List of selected Items", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
146  if (file == "") {
147  return 1;
148  }
149  try {
150  gSelected.save(file.text());
151  } catch (IOError& e) {
152  FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
153  }
154  return 1;
155 }
156 
157 
158 long
159 GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {
160  FXint no = myList->getNumItems();
161  FXint i;
162  std::vector<GUIGlID> selected;
163  for (i = 0; i < no; ++i) {
164  if (myList->getItem(i)->isSelected()) {
165  selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());
166  }
167  }
168  // remove items from list
169  for (i = 0; i < (FXint) selected.size(); ++i) {
170  gSelected.deselect(selected[i]);
171  }
172  // rebuild list
173  rebuildList();
175  return 1;
176 }
177 
178 
179 long
180 GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {
181  myList->clearItems();
182  gSelected.clear();
184  return 1;
185 }
186 
187 
188 long
189 GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {
190  close(true);
191  return 1;
192 }
193 
194 
195 /****************************************************************************/
196 
GUIDialog_GLChosenEditor::onCmdClear
long onCmdClear(FXObject *, FXSelector, void *)
Called when the user presses the Clear-button.
Definition: GUIDialog_GLChosenEditor.cpp:180
GUIGlObject.h
GUIDesignChooserLayoutLeft
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:495
GUISelectedStorage::save
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
Definition: GUISelectedStorage.cpp:247
GUIDialog_GLChosenEditor::~GUIDialog_GLChosenEditor
~GUIDialog_GLChosenEditor()
Destructor (Notifies both the parent and the storage about being destroyed)
Definition: GUIDialog_GLChosenEditor.cpp:91
MID_CHOOSEN_DESELECT
@ MID_CHOOSEN_DESELECT
Deselect selected items.
Definition: GUIAppEnum.h:522
ICON_EMPTY
@ ICON_EMPTY
Definition: GUIIcons.h:41
ICON_SAVE
@ ICON_SAVE
Definition: GUIIcons.h:48
GUIDialog_GLChosenEditor::onCmdDeselect
long onCmdDeselect(FXObject *, FXSelector, void *)
Called when the user presses the Deselect-button.
Definition: GUIDialog_GLChosenEditor.cpp:159
GUIGlobalSelection.h
MID_CHOOSEN_CLEAR
@ MID_CHOOSEN_CLEAR
Clear set.
Definition: GUIAppEnum.h:518
GUIDialog_GLChosenEditor::myParent
GUIMainWindow * myParent
The parent window.
Definition: GUIDialog_GLChosenEditor.h:123
ICON_OPEN_CONFIG
@ ICON_OPEN_CONFIG
Definition: GUIIcons.h:42
ICON_NO
@ ICON_NO
Definition: GUIIcons.h:120
GUIGlObject::getFullName
const std::string & getFullName() const
Definition: GUIGlObject.cpp:137
GUIDesigns.h
GUIDialog_GLChosenEditor::myStorage
GUISelectedStorage * myStorage
The storage.
Definition: GUIDialog_GLChosenEditor.h:126
GUIDialog_GLChosenEditor::rebuildList
void rebuildList()
Rebuilds the entire list.
Definition: GUIDialog_GLChosenEditor.cpp:98
MID_CANCEL
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:230
GUIDialog_GLChosenEditor::onCmdLoad
long onCmdLoad(FXObject *, FXSelector, void *)
Called when the user presses the Load-button.
Definition: GUIDialog_GLChosenEditor.cpp:121
GUIIconSubSys::getIcon
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
Definition: GUIIconSubSys.cpp:609
MID_CHOOSEN_LOAD
@ MID_CHOOSEN_LOAD
Load set.
Definition: GUIAppEnum.h:514
GUISelectedStorage::load
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
Definition: GUISelectedStorage.cpp:233
MFXUtils::getFilename2Write
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extension, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition: MFXUtils.cpp:83
GUIGlObjectStorage.h
GUIMainWindow.h
GUIAppEnum.h
GUIDialog_GLChosenEditor::selectionUpdated
void selectionUpdated()
called when selection is updated
Definition: GUIDialog_GLChosenEditor.cpp:114
FXDEFMAP
FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[]
GUIDialog_GLChosenEditor::onCmdClose
long onCmdClose(FXObject *, FXSelector, void *)
Called when the user presses the Close-button.
Definition: GUIDialog_GLChosenEditor.cpp:189
GUIDesignHorizontalSeparator
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:321
GUIGlObject::getGlID
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.cpp:149
GUISelectedStorage::remove2Update
void remove2Update()
Removes the dialog to be updated.
Definition: GUISelectedStorage.cpp:265
GUIMainWindow::removeChild
void removeChild(FXMainWindow *child)
Definition: GUIMainWindow.cpp:115
GUIDialog_GLChosenEditor.h
GUIMainWindow::updateChildren
void updateChildren()
Definition: GUIMainWindow.cpp:151
GUIIOGlobals.h
GUIIconSubSys
Definition: GUIIconSubSys.h:32
GUIGlObjectStorage::getObjectBlocking
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
Definition: GUIGlObjectStorage.cpp:62
GUIDesignChooserButtons
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:477
GUIGlObject
Definition: GUIGlObject.h:65
gCurrentFolder
FXString gCurrentFolder
The folder used as last.
Definition: GUIIOGlobals.cpp:32
GUISelectedStorage::clear
void clear()
Clears the list of selected objects.
Definition: GUISelectedStorage.cpp:176
MID_CHOOSEN_SAVE
@ MID_CHOOSEN_SAVE
Save set.
Definition: GUIAppEnum.h:516
GUIIconSubSys.h
GUIGlObjectStorage::unblockObject
void unblockObject(GUIGlID id)
Marks an object as unblocked.
Definition: GUIGlObjectStorage.cpp:119
GUIDialog_GLChosenEditor::onCmdSave
long onCmdSave(FXObject *, FXSelector, void *)
Called when the user presses the Save-button.
Definition: GUIDialog_GLChosenEditor.cpp:144
GUIMainWindow
Definition: GUIMainWindow.h:46
MFXUtils.h
GUISelectedStorage
Storage for "selected" objects.
Definition: GUISelectedStorage.h:69
GUIGlObjectStorage::gIDStorage
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
Definition: GUIGlObjectStorage.h:140
ICON_FLAG
@ ICON_FLAG
Definition: GUIIcons.h:125
IOError
Definition: UtilExceptions.h:161
gSelected
GUISelectedStorage gSelected
A global holder of selected objects.
Definition: GUIGlobalSelection.cpp:33
ICON_APP_SELECTOR
@ ICON_APP_SELECTOR
Definition: GUIIcons.h:114
GUIDesignChooserLayoutList
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:501
GUIDesignAuxiliarFrame
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions
Definition: GUIDesigns.h:270
config.h
GUIDesignChooserDialog
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:474
MID_CHOOSER_LIST
@ MID_CHOOSER_LIST
Object list.
Definition: GUIAppEnum.h:498
GUIDesignChooserListMultiple
#define GUIDesignChooserListMultiple
design for Chooser List
Definition: GUIDesigns.h:486
GUISelectedStorage::getSelected
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
Definition: GUISelectedStorage.cpp:164
GUIDesignChooserLayoutRight
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:498
GUISelectedStorage::deselect
void deselect(GUIGlID id)
Deselects the object with the given id.
Definition: GUISelectedStorage.cpp:130
GUIDialog_GLChosenEditor::myList
FXList * myList
The list that holds the ids.
Definition: GUIDialog_GLChosenEditor.h:120
GUIDialog_GLChosenEditor
Editor for the list of chosen objects.
Definition: GUIDialog_GLChosenEditor.h:49