SUMO - Simulation of Urban MObility
GUIDialog_GLObjChooser.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-2018 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 // Class for the window that allows to choose a street, junction or vehicle
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <vector>
28 #include <fxkeys.h>
38 #include "GUIDialog_GLObjChooser.h"
39 
40 
41 // ===========================================================================
42 // FOX callback mapping
43 // ===========================================================================
44 FXDEFMAP(GUIDialog_GLObjChooser) GUIDialog_GLObjChooserMap[] = {
46  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLObjChooser::onCmdClose),
47  FXMAPFUNC(SEL_CHANGED, MID_CHOOSER_TEXT, GUIDialog_GLObjChooser::onChgText),
48  FXMAPFUNC(SEL_COMMAND, MID_CHOOSER_TEXT, GUIDialog_GLObjChooser::onCmdText),
52 };
53 
54 FXIMPLEMENT(GUIDialog_GLObjChooser, FXMainWindow, GUIDialog_GLObjChooserMap, ARRAYNUMBER(GUIDialog_GLObjChooserMap))
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 GUIDialog_GLObjChooser::GUIDialog_GLObjChooser(GUIGlChildWindow* parent, FXIcon* icon, const FXString& title, const std::vector<GUIGlID>& ids, GUIGlObjectStorage& /*glStorage*/) :
61  FXMainWindow(parent->getApp(), title, icon, nullptr, GUIDesignChooserDialog),
62  myParent(parent) {
63  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
64  // build the list
65  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
66  myTextEntry = new FXTextField(layoutLeft, 0, this, MID_CHOOSER_TEXT, GUIDesignChooserTextField);
67  FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
68  myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListSingle);
69  refreshList(ids);
70  // build the buttons
71  FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
72  myCenterButton = new FXButton(layoutRight, "Center\t\t", GUIIconSubSys::getIcon(ICON_RECENTERVIEW), this, MID_CHOOSER_CENTER, GUIDesignChooserButtons);
73  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
74  new FXButton(layoutRight, "&Hide Unselected\t\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSER_FILTER, GUIDesignChooserButtons);
75  new FXButton(layoutRight, "&Select/deselect\tSelect/deselect current object\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSEN_INVERT, GUIDesignChooserButtons);
76  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
77  new FXButton(layoutRight, "&Close\t\t", GUIIconSubSys::getIcon(ICON_NO), this, MID_CANCEL, GUIDesignChooserButtons);
78 
79  myParent->getParent()->addChild(this);
80  // create and show dialog
81  create();
82  show();
83 }
84 
85 
87  myParent->getParent()->removeChild(this);
88 }
89 
90 
91 void
93  FXMainWindow::show();
94  myTextEntry->setFocus();
95 }
96 
97 
98 long
99 GUIDialog_GLObjChooser::onCmdCenter(FXObject*, FXSelector, void*) {
100  int selected = myList->getCurrentItem();
101  if (selected >= 0) {
102  myParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
103  }
104  return 1;
105 }
106 
107 
108 long
109 GUIDialog_GLObjChooser::onCmdClose(FXObject*, FXSelector, void*) {
110  close(true);
111  return 1;
112 }
113 
114 
115 long
116 GUIDialog_GLObjChooser::onChgText(FXObject*, FXSelector, void*) {
117  int id = myList->findItem(myTextEntry->getText(), -1, SEARCH_PREFIX);
118  if (id < 0) {
119  if (myList->getNumItems() > 0) {
120  myList->deselectItem(myList->getCurrentItem());
121  }
122  myCenterButton->disable();
123  return 1;
124  }
125  myList->deselectItem(myList->getCurrentItem());
126  myList->makeItemVisible(id);
127  myList->selectItem(id);
128  myList->setCurrentItem(id, true);
129  myCenterButton->enable();
130  return 1;
131 }
132 
133 
134 long
135 GUIDialog_GLObjChooser::onCmdText(FXObject*, FXSelector, void*) {
136  int current = myList->getCurrentItem();
137  if (current >= 0 && myList->isItemSelected(current)) {
138  myParent->setView(*static_cast<GUIGlID*>(myList->getItemData(current)));
139  }
140  return 1;
141 }
142 
143 
144 
145 long
146 GUIDialog_GLObjChooser::onListKeyPress(FXObject*, FXSelector, void* ptr) {
147  FXEvent* event = (FXEvent*)ptr;
148  switch (event->code) {
149  case KEY_Return:
150  onCmdText(nullptr, 0, nullptr);
151  break;
152  default:
153  break;
154  }
155  return 1;
156 }
157 
158 
159 long
160 GUIDialog_GLObjChooser::onCmdFilter(FXObject*, FXSelector, void*) {
162  std::vector<GUIGlID> selectedGlIDs;
163  const int numItems = myList->getNumItems();
164  for (int i = 0; i < numItems; i++) {
165  const GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
166  if (myList->getItemIcon(i) == flag) {
167  selectedGlIDs.push_back(glID);
168  }
169  }
170  refreshList(selectedGlIDs);
171  return 1;
172 }
173 
174 std::string
176  return o->getMicrosimID();
177 }
178 
179 void
180 GUIDialog_GLObjChooser::refreshList(const std::vector<GUIGlID>& ids) {
181  myList->clearItems();
182  for (auto i : ids) {
184  if (o == nullptr) {
185  continue;
186  }
187  const std::string& name = getObjectName(o);
188  bool selected = myParent->isSelected(o);
189  FXIcon* icon = selected ? GUIIconSubSys::getIcon(ICON_FLAG) : nullptr;
190  myIDs.insert(o->getGlID());
191  myList->appendItem(name.c_str(), icon, (void*) & (*myIDs.find(o->getGlID())));
193  }
194  myList->update();
195 }
196 
197 
198 long
199 GUIDialog_GLObjChooser::onCmdToggleSelection(FXObject*, FXSelector, void*) {
201  int i = myList->getCurrentItem();
202  if (i >= 0) {
203  toggleSelection(i);
204  if (myList->getItemIcon(i) == flag) {
205  myList->setItemIcon(i, nullptr);
206  } else {
207  myList->setItemIcon(i, flag);
208  }
209  }
210  myList->update();
211  myParent->getView()->update();
212  return 1;
213 }
214 
215 
216 void
218  GUIGlID* glID = static_cast<GUIGlID*>(myList->getItemData(listIndex));
219  gSelected.toggleSelection(*glID);
220 }
221 
222 
223 
224 
225 /****************************************************************************/
226 
void show()
sets the focus after the window is created to work-around bug in libfox
GUISUMOAbstractView * getView() const
long onCmdFilter(FXObject *, FXSelector, void *)
Callback: Hides unselected items if pressed.
void toggleSelection(GUIGlID id)
Toggles selection of an object.
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:457
virtual void toggleSelection(int listIndex)
toggle selection (handled differently in NETEDIT)
virtual ~GUIDialog_GLObjChooser()
Destructor.
long onCmdCenter(FXObject *, FXSelector, void *)
Callback: The selected item shall be centered within the calling view.
FXDEFMAP(GUIDialog_GLObjChooser) GUIDialog_GLObjChooserMap[]
long onCmdText(FXObject *, FXSelector, void *)
Callback: Selects to current item if enter is pressed.
long onCmdToggleSelection(FXObject *, FXSelector, void *)
Callback: Toggle selection status of current object.
const unsigned char flag[]
Definition: flag.cpp:21
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions ...
Definition: GUIDesigns.h:258
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:298
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:484
GUIMainWindow * getParent()
Returns the main window.
Deselect selected items.
Definition: GUIAppEnum.h:358
virtual std::string getObjectName(GUIGlObject *o) const
retrieve name for the given object
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
FXList * myList
The list that holds the ids.
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:478
GUIGlChildWindow * myParent
The parent window.
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:460
Center object.
Definition: GUIAppEnum.h:330
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
Object list.
Definition: GUIAppEnum.h:334
A storage for of displayed objects via their numerical id.
void refreshList(const std::vector< GUIGlID > &ids)
update the list with the given ids
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:481
long onCmdClose(FXObject *, FXSelector, void *)
Callback: The dialog shall be closed.
unsigned int GUIGlID
Definition: GUIGlObject.h:43
void setView(GUIGlID id)
Centers the view onto the given artifact.
Text entry.
Definition: GUIAppEnum.h:332
void removeChild(FXMainWindow *child)
#define GUIDesignChooserTextField
design for Chooser TextField
Definition: GUIDesigns.h:463
Cancel-button pressed.
Definition: GUIAppEnum.h:58
GUIGlID getGlID() const
Returns the numerical id of the object.
FXButton * myCenterButton
The button that triggers centering on the select object.
virtual bool isSelected(GUIGlObject *o) const
true if the object is selected (may include extra logic besides calling gSelected) ...
std::set< GUIGlID > myIDs
myList contains (void) pointers to elements of myIDs instead of the more
void unblockObject(GUIGlID id)
Marks an object as unblocked.
#define GUIDesignChooserListSingle
design for Chooser List
Definition: GUIDesigns.h:466
long onListKeyPress(FXObject *, FXSelector, void *)
Callback: Selects to current item if enter is pressed.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
GUISelectedStorage gSelected
A global holder of selected objects.
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
Filter selected.
Definition: GUIAppEnum.h:336
FXTextField * myTextEntry
The text field.
long onChgText(FXObject *, FXSelector, void *)
Callback: Something has been typed into the the field.