Eclipse 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-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 /****************************************************************************/
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[] = {
47  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLObjChooser::onCmdClose),
48  FXMAPFUNC(SEL_CHANGED, MID_CHOOSER_TEXT, GUIDialog_GLObjChooser::onChgText),
49  FXMAPFUNC(SEL_COMMAND, MID_CHOOSER_TEXT, GUIDialog_GLObjChooser::onCmdText),
54 };
55 
56 FXIMPLEMENT(GUIDialog_GLObjChooser, FXMainWindow, GUIDialog_GLObjChooserMap, ARRAYNUMBER(GUIDialog_GLObjChooserMap))
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 GUIDialog_GLObjChooser::GUIDialog_GLObjChooser(GUIGlChildWindow* parent, FXIcon* icon, const FXString& title, const std::vector<GUIGlID>& ids, GUIGlObjectStorage& /*glStorage*/) :
63  FXMainWindow(parent->getApp(), title, icon, nullptr, GUIDesignChooserDialog),
64  myParent(parent),
65  myLocateByName(false) {
66  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
67  // build the list
68  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
69  myTextEntry = new FXTextField(layoutLeft, 0, this, MID_CHOOSER_TEXT, GUIDesignChooserTextField);
70  FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
71  myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListSingle);
72  refreshList(ids);
73  // build the buttons
74  FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
75  myCenterButton = new FXButton(layoutRight, "Center\t\t", GUIIconSubSys::getIcon(ICON_RECENTERVIEW), this, MID_CHOOSER_CENTER, GUIDesignChooserButtons);
76  myTrackButton = new FXButton(layoutRight, "Track\t\t", GUIIconSubSys::getIcon(ICON_RECENTERVIEW), this, MID_CHOOSER_TRACK, GUIDesignChooserButtons);
77  // only enable Track Button if we're locating vehicles
78  if (title.text() != std::string("Vehicle Chooser")) {
79  myTrackButton->disable();
80  myTrackButton->hide();
81  }
82  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
83  new FXButton(layoutRight, "&Hide Unselected\t\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSER_FILTER, GUIDesignChooserButtons);
84  new FXButton(layoutRight, "&Select/deselect\tSelect/deselect current object\t", GUIIconSubSys::getIcon(ICON_FLAG), this, MID_CHOOSEN_INVERT, GUIDesignChooserButtons);
85  new FXButton(layoutRight, "By &Name\tLocate item by name\t", nullptr, this, MID_CHOOSEN_NAME, GUIDesignChooserButtons);
86  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
87  new FXButton(layoutRight, "&Close\t\t", GUIIconSubSys::getIcon(ICON_NO), this, MID_CANCEL, GUIDesignChooserButtons);
88 
89  myParent->getParent()->addChild(this);
90  // create and show dialog
91  create();
92  show();
93 }
94 
95 
97  myParent->getParent()->removeChild(this);
98 }
99 
100 
101 void
103  FXMainWindow::show();
104  myTextEntry->setFocus();
105 }
106 
107 
108 long
109 GUIDialog_GLObjChooser::onCmdCenter(FXObject*, FXSelector, void*) {
110  int selected = myList->getCurrentItem();
111  if (selected >= 0) {
112  myParent->getView()->stopTrack();
113  myParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
114  }
115  return 1;
116 }
117 
118 
119 long
120 GUIDialog_GLObjChooser::onCmdTrack(FXObject*, FXSelector, void*) {
121  int selected = myList->getCurrentItem();
122  if (selected >= 0) {
123  myParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
124  GUIGlID id = *static_cast<GUIGlID*>(myList->getItemData(selected));
126  if (o->getType() == GLO_VEHICLE) {
128  }
130  }
131  return 1;
132 }
133 
134 
135 long
136 GUIDialog_GLObjChooser::onCmdClose(FXObject*, FXSelector, void*) {
137  close(true);
138  return 1;
139 }
140 
141 
142 long
143 GUIDialog_GLObjChooser::onChgText(FXObject*, FXSelector, void*) {
144  int id = -1;
145  if (myLocateByName) {
146  // findItem does not support substring search
147  const int numItems = myList->getNumItems();
148  FXString t = myTextEntry->getText().lower();
149  for (int i = 0; i < numItems; i++) {
150  if (myList->getItemText(i).lower().find(t) >= 0) {
151  id = i;
152  break;
153  }
154  }
155  } else {
156  id = myList->findItem(myTextEntry->getText(), -1, SEARCH_PREFIX);
157  }
158  if (id < 0) {
159  if (myList->getNumItems() > 0) {
160  myList->deselectItem(myList->getCurrentItem());
161  }
162  myCenterButton->disable();
163  myTrackButton->disable();
164  return 1;
165  }
166  myList->deselectItem(myList->getCurrentItem());
167  myList->makeItemVisible(id);
168  myList->selectItem(id);
169  myList->setCurrentItem(id, true);
170  myCenterButton->enable();
171  myTrackButton->enable();
172  return 1;
173 }
174 
175 
176 long
177 GUIDialog_GLObjChooser::onCmdText(FXObject*, FXSelector, void*) {
178  int current = myList->getCurrentItem();
179  if (current >= 0 && myList->isItemSelected(current)) {
180  myParent->setView(*static_cast<GUIGlID*>(myList->getItemData(current)));
181  }
182  return 1;
183 }
184 
185 
186 
187 long
188 GUIDialog_GLObjChooser::onListKeyPress(FXObject*, FXSelector, void* ptr) {
189  FXEvent* event = (FXEvent*)ptr;
190  switch (event->code) {
191  case KEY_Return:
192  onCmdText(nullptr, 0, nullptr);
193  break;
194  default:
195  break;
196  }
197  return 1;
198 }
199 
200 
201 long
202 GUIDialog_GLObjChooser::onCmdFilter(FXObject*, FXSelector, void*) {
204  std::vector<GUIGlID> selectedGlIDs;
205  const int numItems = myList->getNumItems();
206  for (int i = 0; i < numItems; i++) {
207  const GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
208  if (myList->getItemIcon(i) == flag) {
209  selectedGlIDs.push_back(glID);
210  }
211  }
212  refreshList(selectedGlIDs);
213  return 1;
214 }
215 
216 std::string
218  if (myLocateByName) {
219  return o->getOptionalName();
220  } else {
221  return o->getMicrosimID();
222  }
223 }
224 
225 void
226 GUIDialog_GLObjChooser::refreshList(const std::vector<GUIGlID>& ids) {
227  myList->clearItems();
228  for (auto i : ids) {
230  if (o == nullptr) {
231  continue;
232  }
233  const std::string& name = getObjectName(o);
234  bool selected = myParent->isSelected(o);
235  FXIcon* icon = selected ? GUIIconSubSys::getIcon(ICON_FLAG) : nullptr;
236  myIDs.insert(o->getGlID());
237  myList->appendItem(name.c_str(), icon, (void*) & (*myIDs.find(o->getGlID())));
239  }
240  myList->update();
241 }
242 
243 
244 long
245 GUIDialog_GLObjChooser::onCmdToggleSelection(FXObject*, FXSelector, void*) {
247  int i = myList->getCurrentItem();
248  if (i >= 0) {
249  toggleSelection(i);
250  if (myList->getItemIcon(i) == flag) {
251  myList->setItemIcon(i, nullptr);
252  } else {
253  myList->setItemIcon(i, flag);
254  }
255  }
256  myList->update();
257  myParent->getView()->update();
258  return 1;
259 }
260 
261 
262 long
263 GUIDialog_GLObjChooser::onCmdLocateByName(FXObject*, FXSelector, void*) {
264  std::vector<std::pair<std::string, GUIGlID> > namesAndIDs;
265  myLocateByName = true;
266  const int numItems = myList->getNumItems();
267  for (int i = 0; i < numItems; i++) {
268  GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
270  const std::string& name = getObjectName(o);
271  if (name != "") {
272  namesAndIDs.push_back(std::make_pair(name, glID));
273  }
275  }
276  std::sort(namesAndIDs.begin(), namesAndIDs.end());
277  std::vector<GUIGlID> selectedGlIDs;
278  for (const auto& item : namesAndIDs) {
279  selectedGlIDs.push_back(item.second);
280  }
281  refreshList(selectedGlIDs);
282  myTextEntry->setFocus();
283  return 1;
284 }
285 
286 
287 void
289  GUIGlID* glID = static_cast<GUIGlID*>(myList->getItemData(listIndex));
290  gSelected.toggleSelection(*glID);
291 }
292 
293 
294 
295 
296 /****************************************************************************/
297 
void show()
sets the focus after the window is created to work-around bug in libfox
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:490
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.
Deselect selected items.
Definition: GUIAppEnum.h:509
FXDEFMAP(GUIDialog_GLObjChooser) GUIDialog_GLObjChooserMap[]
virtual void stopTrack()
stop track
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.
long onCmdLocateByName(FXObject *, FXSelector, void *)
Callback: Toggle locator by name.
const unsigned char flag[]
Definition: flag.cpp:21
Track object.
Definition: GUIAppEnum.h:479
virtual void startTrack(int)
star track
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions ...
Definition: GUIDesigns.h:286
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:337
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:517
Deselect selected items.
Definition: GUIAppEnum.h:507
virtual std::string getObjectName(GUIGlObject *o) const
retrieve name for the given object
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
long onCmdTrack(FXObject *, FXSelector, void *)
Callback: The selected vehicle shall be tracked within the calling view.
bool myLocateByName
whether to locate by object name instead of id
FXList * myList
The list that holds the ids.
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:511
GUIGlChildWindow * myParent
The parent window.
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:493
Center object.
Definition: GUIAppEnum.h:477
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
Object list.
Definition: GUIAppEnum.h:483
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:514
long onCmdClose(FXObject *, FXSelector, void *)
Callback: The dialog shall be closed.
virtual const std::string getOptionalName() const
Returns the name of the object (default "")
unsigned int GUIGlID
Definition: GUIGlObject.h:43
FXButton * myTrackButton
The button that triggers tracking on the select vehicle.
void setView(GUIGlID id)
Centers the view onto the given artifact.
Text entry.
Definition: GUIAppEnum.h:481
void removeChild(FXMainWindow *child)
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
#define GUIDesignChooserTextField
design for Chooser TextField
Definition: GUIDesigns.h:496
Cancel-button pressed.
Definition: GUIAppEnum.h:215
GUIGlID getGlID() const
Returns the numerical id of the object.
FXButton * myCenterButton
The button that triggers centering on the select object.
GUIMainWindow * getParent()
Returns the main window.
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:499
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:485
FXTextField * myTextEntry
The text field.
long onChgText(FXObject *, FXSelector, void *)
Callback: Something has been typed into the the field.