Eclipse SUMO - Simulation of Urban MObility
GUIDialog_ChooserAbstract.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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Class for the window that allows to choose a street, junction or vehicle
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
25 #include <vector>
26 #include <fxkeys.h>
36 #include <gui/GUISUMOViewParent.h>
37 #include <netedit/GNEViewParent.h>
38 
40 
41 
42 // ===========================================================================
43 // FOX callback mapping
44 // ===========================================================================
45 FXDEFMAP(GUIDialog_ChooserAbstract) GUIDialog_ChooserAbstractMap[] = {
48  FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_ChooserAbstract::onCmdClose),
56 };
57 
58 FXIMPLEMENT(GUIDialog_ChooserAbstract, FXMainWindow, GUIDialog_ChooserAbstractMap, ARRAYNUMBER(GUIDialog_ChooserAbstractMap))
59 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
65  FXIcon* icon, const FXString& title, const std::vector<GUIGlID>& ids, GUIGlObjectStorage& /*glStorage*/) :
66  FXMainWindow(windowsParent->getApp(), title, icon, nullptr, GUIDesignChooserDialog),
67  myWindowsParent(windowsParent),
68  myLocateByName(false),
69  myHaveFilteredSubstring(false) {
70  FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
71  // build the list
72  FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
73  myTextEntry = new FXTextField(layoutLeft, 0, this, MID_CHOOSER_TEXT, GUIDesignChooserTextField);
74  FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
75  myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListSingle);
76  refreshList(ids);
77  // build the buttons
78  FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
79  myCenterButton = new FXButton(layoutRight, "Center\t\t", GUIIconSubSys::getIcon(GUIIcon::RECENTERVIEW), this, MID_CHOOSER_CENTER, GUIDesignChooserButtons);
80  myTrackButton = new FXButton(layoutRight, "Track\t\t", GUIIconSubSys::getIcon(GUIIcon::RECENTERVIEW), this, MID_CHOOSER_TRACK, GUIDesignChooserButtons);
81  // only enable Track Button if we're locating vehicles
82  if (title.text() != std::string("Vehicle Chooser")) {
83  myTrackButton->disable();
84  myTrackButton->hide();
85  }
86  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
87  new FXButton(layoutRight, "&Hide Unselected\t\t", GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSER_FILTER, GUIDesignChooserButtons);
88  new FXButton(layoutRight, "&Filter substring\t\t", nullptr, this, MID_CHOOSER_FILTER_SUBSTR, GUIDesignChooserButtons);
89  new FXButton(layoutRight, "&Select/deselect\tSelect/deselect current object\t", GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_INVERT, GUIDesignChooserButtons);
90  new FXButton(layoutRight, "By &Name\tLocate item by name\t", nullptr, this, MID_CHOOSEN_NAME, GUIDesignChooserButtons);
91  new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
92  new FXButton(layoutRight, "&Close\t\t", GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);
93  // add child in windowsParent
94  myWindowsParent->getParent()->addChild(this);
95  // create and show dialog
96  create();
97  show();
98 }
99 
100 
102  // remove child from windowsParent
104 }
105 
106 
109  return static_cast<GUIGlObject*>(mySelected);
110 }
111 
112 
113 void
115  FXMainWindow::show();
116  myTextEntry->setFocus();
117 }
118 
119 
120 long
121 GUIDialog_ChooserAbstract::onCmdCenter(FXObject*, FXSelector, void*) {
122  int selected = myList->getCurrentItem();
123  if (selected >= 0) {
125  myWindowsParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
126  }
127  return 1;
128 }
129 
130 
131 long
132 GUIDialog_ChooserAbstract::onCmdTrack(FXObject*, FXSelector, void*) {
133  int selected = myList->getCurrentItem();
134  if (selected >= 0) {
135  myWindowsParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
136  GUIGlID id = *static_cast<GUIGlID*>(myList->getItemData(selected));
138  if (o->getType() == GLO_VEHICLE) {
140  }
142  }
143  return 1;
144 }
145 
146 
147 long
148 GUIDialog_ChooserAbstract::onCmdClose(FXObject*, FXSelector, void*) {
149  close(true);
150  return 1;
151 }
152 
153 
154 long
155 GUIDialog_ChooserAbstract::onChgText(FXObject*, FXSelector, void*) {
156  int id = -1;
158  // findItem does not support substring search
159  const int numItems = myList->getNumItems();
160  FXString t = myTextEntry->getText().lower();
161  for (int i = 0; i < numItems; i++) {
162  if (myList->getItemText(i).lower().find(t) >= 0) {
163  id = i;
164  break;
165  }
166  }
167  } else {
168  id = myList->findItem(myTextEntry->getText(), -1, SEARCH_PREFIX);
169  }
170  if (id < 0) {
171  if (myList->getNumItems() > 0) {
172  myList->deselectItem(myList->getCurrentItem());
173  }
174  myCenterButton->disable();
175  myTrackButton->disable();
176  return 1;
177  }
178  myList->deselectItem(myList->getCurrentItem());
179  myList->makeItemVisible(id);
180  myList->selectItem(id);
181  myList->setCurrentItem(id, true);
182  myCenterButton->enable();
183  myTrackButton->enable();
184  return 1;
185 }
186 
187 
188 long
189 GUIDialog_ChooserAbstract::onCmdText(FXObject*, FXSelector, void*) {
190  int current = myList->getCurrentItem();
191  if (current >= 0 && myList->isItemSelected(current)) {
192  myWindowsParent->setView(*static_cast<GUIGlID*>(myList->getItemData(current)));
193  }
194  return 1;
195 }
196 
197 
198 
199 long
200 GUIDialog_ChooserAbstract::onListKeyPress(FXObject*, FXSelector, void* ptr) {
201  FXEvent* event = (FXEvent*)ptr;
202  switch (event->code) {
203  case KEY_Return:
204  onCmdText(nullptr, 0, nullptr);
205  break;
206  default:
207  break;
208  }
209  return 1;
210 }
211 
212 
213 long
214 GUIDialog_ChooserAbstract::onCmdFilter(FXObject*, FXSelector, void*) {
216  std::vector<GUIGlID> selectedGlIDs;
217  const int numItems = myList->getNumItems();
218  for (int i = 0; i < numItems; i++) {
219  const GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
220  if (myList->getItemIcon(i) == flag) {
221  selectedGlIDs.push_back(glID);
222  }
223  }
224  refreshList(selectedGlIDs);
225  return 1;
226 }
227 
228 
229 long
230 GUIDialog_ChooserAbstract::onCmdFilterSubstr(FXObject*, FXSelector, void*) {
231  std::vector<GUIGlID> selectedGlIDs;
232  const int numItems = myList->getNumItems();
233  FXString t = myTextEntry->getText().lower();
234  for (int i = 0; i < numItems; i++) {
235  if (myList->getItemText(i).lower().find(t) >= 0) {
236  const GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
237  selectedGlIDs.push_back(glID);
238  }
239  }
240  refreshList(selectedGlIDs);
241  // filter ACs in NETEDIT
242  filterACs(selectedGlIDs);
244  onChgText(nullptr, 0, nullptr);
245  return 1;
246 }
247 
248 
249 std::string
251  if (myLocateByName) {
252  return o->getOptionalName();
253  } else {
254  return o->getMicrosimID();
255  }
256 }
257 
258 void
259 GUIDialog_ChooserAbstract::refreshList(const std::vector<GUIGlID>& ids) {
260  myList->clearItems();
261  for (auto i : ids) {
263  if (o == nullptr) {
264  continue;
265  }
266  const std::string& name = getObjectName(o);
267  bool selected = myWindowsParent->isSelected(o);
268  FXIcon* icon = selected ? GUIIconSubSys::getIcon(GUIIcon::FLAG) : nullptr;
269  myIDs.insert(o->getGlID());
270  myList->appendItem(name.c_str(), icon, (void*) & (*myIDs.find(o->getGlID())));
272  }
273  myList->update();
274 }
275 
276 
277 long
278 GUIDialog_ChooserAbstract::onCmdToggleSelection(FXObject*, FXSelector, void*) {
280  int i = myList->getCurrentItem();
281  if (i >= 0) {
282  toggleSelection(i);
283  if (myList->getItemIcon(i) == flag) {
284  myList->setItemIcon(i, nullptr);
285  } else {
286  myList->setItemIcon(i, flag);
287  }
288  }
289  myList->update();
290  myWindowsParent->getView()->update();
291  return 1;
292 }
293 
294 
295 long
296 GUIDialog_ChooserAbstract::onCmdLocateByName(FXObject*, FXSelector, void*) {
297  std::vector<std::pair<std::string, GUIGlID> > namesAndIDs;
298  myLocateByName = true;
299  const int numItems = myList->getNumItems();
300  for (int i = 0; i < numItems; i++) {
301  GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
303  if (o != 0) {
304  const std::string& name = getObjectName(o);
305  if (name != "") {
306  namesAndIDs.push_back(std::make_pair(name, glID));
307  }
308  }
310  }
311  std::sort(namesAndIDs.begin(), namesAndIDs.end());
312  std::vector<GUIGlID> selectedGlIDs;
313  for (const auto& item : namesAndIDs) {
314  selectedGlIDs.push_back(item.second);
315  }
316  refreshList(selectedGlIDs);
317  myTextEntry->setFocus();
318  return 1;
319 }
320 
321 
322 void
324  GUIGlID* glID = static_cast<GUIGlID*>(myList->getItemData(listIndex));
325  gSelected.toggleSelection(*glID);
326 }
327 
328 
329 void
330 GUIDialog_ChooserAbstract::filterACs(const std::vector<GUIGlID>& /*GLIDs*/) {
331  // overrided in GNEDialogACChooser
332 }
333 
334 /****************************************************************************/
@ MID_CHOOSER_TRACK
Track object.
Definition: GUIAppEnum.h:519
@ MID_CANCEL
Cancel-button pressed.
Definition: GUIAppEnum.h:247
@ MID_CHOOSER_TEXT
Text entry.
Definition: GUIAppEnum.h:521
@ MID_CHOOSEN_INVERT
Deselect selected items.
Definition: GUIAppEnum.h:549
@ MID_CHOOSER_LIST
Object list.
Definition: GUIAppEnum.h:523
@ MID_CHOOSEN_NAME
Deselect selected items.
Definition: GUIAppEnum.h:551
@ MID_CHOOSER_FILTER_SUBSTR
Filter list by substring.
Definition: GUIAppEnum.h:527
@ MID_CHOOSER_FILTER
Filter selected.
Definition: GUIAppEnum.h:525
@ MID_CHOOSER_CENTER
Center object.
Definition: GUIAppEnum.h:517
#define GUIDesignChooserTextField
design for Chooser TextField
Definition: GUIDesigns.h:540
#define GUIDesignChooserListSingle
design for Chooser List
Definition: GUIDesigns.h:543
#define GUIDesignChooserButtons
design for Chooser buttons
Definition: GUIDesigns.h:537
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition: GUIDesigns.h:552
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition: GUIDesigns.h:555
#define GUIDesignHorizontalSeparator
Definition: GUIDesigns.h:362
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition: GUIDesigns.h:558
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frames used to pack another frames extended in all directions
Definition: GUIDesigns.h:310
#define GUIDesignChooserDialog
Definition: GUIDesigns.h:534
FXDEFMAP(GUIDialog_ChooserAbstract) GUIDialog_ChooserAbstractMap[]
unsigned int GUIGlID
Definition: GUIGlObject.h:40
@ GLO_VEHICLE
a vehicle
GUISelectedStorage gSelected
A global holder of selected objects.
@ RECENTERVIEW
bool myLocateByName
whether to locate by object name instead of id
FXButton * myCenterButton
The button that triggers centering on the select object.
long onCmdText(FXObject *, FXSelector, void *)
Callback: Selects to current item if enter is pressed.
long onCmdClose(FXObject *, FXSelector, void *)
Callback: The dialog shall be closed.
void refreshList(const std::vector< GUIGlID > &ids)
update the list with the given ids
long onCmdCenter(FXObject *, FXSelector, void *)
Callback: The selected item shall be centered within the calling view.
virtual ~GUIDialog_ChooserAbstract()
Destructor.
long onCmdFilter(FXObject *, FXSelector, void *)
Callback: Hides unselected items if pressed.
long onCmdFilterSubstr(FXObject *, FXSelector, void *)
Callback: Hides unmatched items if pressed.
void show()
sets the focus after the window is created to work-around bug in libfox
long onCmdTrack(FXObject *, FXSelector, void *)
Callback: The selected vehicle shall be tracked within the calling view.
long onCmdLocateByName(FXObject *, FXSelector, void *)
Callback: Toggle locator by name.
bool myHaveFilteredSubstring
whether the list was filter by substring
long onListKeyPress(FXObject *, FXSelector, void *)
Callback: Selects to current item if enter is pressed.
long onChgText(FXObject *, FXSelector, void *)
Callback: Something has been typed into the the field.
GUIGlChildWindow * myWindowsParent
window parent
std::set< GUIGlID > myIDs
myList contains (void) pointers to elements of myIDs instead of the more volatile pointers to GUIGlOb...
GUIGlObject * mySelected
The chosen id.
virtual void toggleSelection(int listIndex)
fox need this
FXButton * myTrackButton
The button that triggers tracking on the select vehicle.
virtual std::string getObjectName(GUIGlObject *o) const
@bbrief retrieve name for the given object
virtual void filterACs(const std::vector< GUIGlID > &GLIDs)
filter ACs (needed in NETEDIT)
FXTextField * myTextEntry
The text field.
long onCmdToggleSelection(FXObject *, FXSelector, void *)
Callback: Toggle selection status of current object.
GUIGlObject * getObject() const
Returns the chosen (selected) object.
FXList * myList
The list that holds the ids.
void setView(GUIGlID id)
Centers the view onto the given artifact.
virtual bool isSelected(GUIGlObject *o) const
true if the object is selected (may include extra logic besides calling gSelected)
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
GUIMainWindow * getParent()
Returns the main window.
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
virtual const std::string getOptionalName() const
Returns the name of the object (default "")
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
GUIGlID getGlID() const
Returns the numerical id of the object.
A storage for of displayed objects via their numerical id.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void removeChild(FXMainWindow *child)
removes the given child window from the list (FXMainWindow)
virtual void stopTrack()
stop track
virtual void startTrack(int)
star track
void toggleSelection(GUIGlID id)
Toggles selection of an object.
const unsigned char flag[]
Definition: flag.cpp:24