Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GUIDialog_ChooserAbstract.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 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>
37
39
40
41// ===========================================================================
42// FOX callback mapping
43// ===========================================================================
61
62FXIMPLEMENT(GUIDialog_ChooserAbstract, FXMainWindow, GUIDialog_ChooserAbstractMap, ARRAYNUMBER(GUIDialog_ChooserAbstractMap))
63
64
65// ===========================================================================
66// method definitions
67// ===========================================================================
68#ifdef _MSC_VER
69#pragma warning(push)
70#pragma warning(disable: 4355) // mask warning about "this" in initializers
71#endif
73 FXIcon* icon, const FXString& title, const std::vector<GUIGlID>& ids, GUIGlObjectStorage& /*glStorage*/) :
74 FXMainWindow(windowsParent->getApp(), title, icon, nullptr, GUIDesignChooserDialog),
75 GUIPersistentWindowPos(this, "LOCATOR", true, 20, 40, 300, 350),
76 myWindowsParent(windowsParent),
77 myMessageId(messageId),
78 myLocateByName(false),
79 myHaveFilteredSubstring(false) {
80 FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);
81 // build the list
82 FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);
83 myTextEntry = new FXTextField(layoutLeft, 0, this, MID_CHOOSER_TEXT, TEXTFIELD_ENTER_ONLY | GUIDesignChooserTextField);
84 FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);
85 myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListSingle);
86 // build the buttons
87 FXVerticalFrame* layoutRight = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);
90 // only enable Track Button if we're locating vehicles
91 if (title.text() != std::string(TL("Vehicle Chooser"))) {
92 myTrackButton->disable();
93 myTrackButton->hide();
94 }
95 new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
97 GUIDesigns::buildFXButton(layoutRight, TL("By &Name"), TL("Locate item by name"), "", nullptr, this, MID_CHOOSEN_NAME, GUIDesignChooserButtons);
98 GUIDesigns::buildFXButton(layoutRight, TL("&Select/deselect"), "", TL("Select/deselect current object"), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_INVERT, GUIDesignChooserButtons);
99 GUIDesigns::buildFXButton(layoutRight, TL("&Filter substring"), "", "", nullptr, this, MID_CHOOSER_FILTER_SUBSTR, GUIDesignChooserButtons);
100 GUIDesigns::buildFXButton(layoutRight, TL("Select &all"), "", TL("Select all items in list"), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_SELECT, GUIDesignChooserButtons);
101 GUIDesigns::buildFXButton(layoutRight, TL("&Deselect all"), "", TL("Deselect all items in list"), GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);
102 GUIDesigns::buildFXButton(layoutRight, TL("&Update"), "", TL("Reload all ids"), GUIIconSubSys::getIcon(GUIIcon::RELOAD), this, MID_UPDATE, GUIDesignChooserButtons);
103 new FXHorizontalSeparator(layoutRight, GUIDesignHorizontalSeparator);
105 myCountLabel = new FXLabel(layoutRight, "placeholder", nullptr, LAYOUT_BOTTOM | LAYOUT_FILL_X | JUSTIFY_LEFT);
106 myCaseSensitive = new FXCheckButton(layoutRight, TL("case-sensitive search"));
107 myCaseSensitive->setCheck(getApp()->reg().readIntEntry("LOCATOR", "caseSensitive", 0) == 1);
108 myInstantCenter = new FXCheckButton(layoutRight, TL("auto-center"));
109 myInstantCenter->setCheck(getApp()->reg().readIntEntry("LOCATOR", "autoCenter", 0) == 1);
110 refreshList(ids);
111 // add child in windowsParent
114 // create and show dialog
115 create();
116 show();
117}
118#ifdef _MSC_VER
119#pragma warning(pop)
120#endif
121
122
124 // remove child from windowsParent
126 getApp()->reg().writeIntEntry("LOCATOR", "autoCenter", myInstantCenter->getCheck());
127 getApp()->reg().writeIntEntry("LOCATOR", "caseSensitive", myCaseSensitive->getCheck());
128}
129
130
133 return static_cast<GUIGlObject*>(mySelected);
134}
135
136
137void
139 FXMainWindow::show();
140 myTextEntry->setFocus();
141}
142
143
144long
145GUIDialog_ChooserAbstract::onCmdCenter(FXObject*, FXSelector, void*) {
146 int selected = myList->getCurrentItem();
147 if (selected >= 0) {
149 myWindowsParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
150 }
151 return 1;
152}
153
154
155long
156GUIDialog_ChooserAbstract::onCmdTrack(FXObject*, FXSelector, void*) {
157 int selected = myList->getCurrentItem();
158 if (selected >= 0) {
159 myWindowsParent->setView(*static_cast<GUIGlID*>(myList->getItemData(selected)));
160 GUIGlID id = *static_cast<GUIGlID*>(myList->getItemData(selected));
162 if (o->getType() == GLO_VEHICLE) {
164 }
166 }
167 return 1;
168}
169
170
171long
172GUIDialog_ChooserAbstract::onCmdClose(FXObject*, FXSelector, void*) {
173 close(true);
174 return 1;
175}
176
177long
178GUIDialog_ChooserAbstract::onChgList(FXObject*, FXSelector, void*) {
179 // mouse-click toggles item selection but changked current item with
180 // keyboard does not affect select
181 // Enabling the line blow toggles the behavior (which must be fixed via onChgListSel)
182 myList->selectItem(myList->getCurrentItem());
183 if (myInstantCenter->getCheck()) {
184 onCmdCenter(nullptr, 0, nullptr);
185 }
186 return 1;
187}
188
189long
190GUIDialog_ChooserAbstract::onChgListSel(FXObject*, FXSelector, void*) {
191 myList->selectItem(myList->getCurrentItem());
192 return 1;
193}
194
195long
196GUIDialog_ChooserAbstract::onChgText(FXObject*, FXSelector, void*) {
197 const bool caseSensitive = myCaseSensitive->getCheck() == TRUE;
198 int id = -1;
200 // findItem does not support substring search
201 const int numItems = myList->getNumItems();
202 FXString t = myTextEntry->getText();
203 if (!caseSensitive) {
204 t = t.lower();
205 }
206 for (int i = 0; i < numItems; i++) {
207 FXString t2 = myList->getItemText(i);
208 if (!caseSensitive) {
209 t2 = t2.lower();
210 }
211 if (t2.find(t) >= 0) {
212 id = i;
213 break;
214 }
215 }
216 } else {
217 const int caseOpt = caseSensitive ? 0 : SEARCH_IGNORECASE;
218 id = myList->findItem(myTextEntry->getText(), -1, SEARCH_PREFIX | caseOpt);
219 }
220 if (id < 0) {
221 if (myList->getNumItems() > 0) {
222 myList->deselectItem(myList->getCurrentItem());
223 }
224 myCenterButton->disable();
225 myTrackButton->disable();
226 return 1;
227 }
228 myList->deselectItem(myList->getCurrentItem());
229 myList->makeItemVisible(id);
230 myList->selectItem(id);
231 myList->setCurrentItem(id, true);
232 myCenterButton->enable();
233 myTrackButton->enable();
234 return 1;
235}
236
237
238long
239GUIDialog_ChooserAbstract::onCmdText(FXObject*, FXSelector, void*) {
240 int current = myList->getCurrentItem();
241 if (current >= 0 && myList->isItemSelected(current)) {
242 myWindowsParent->setView(*static_cast<GUIGlID*>(myList->getItemData(current)));
243 }
244 return 1;
245}
246
247
248
249long
250GUIDialog_ChooserAbstract::onListKeyPress(FXObject*, FXSelector, void* ptr) {
251 FXEvent* event = (FXEvent*)ptr;
252 if (event->code == KEY_Return) {
253 onCmdText(nullptr, 0, nullptr);
254 if ((event->state & CONTROLMASK) != 0) {
255 close(true);
256 }
257 return 1;
258 } else if (event->code == KEY_Left || (event->code == KEY_Up && myList->getCurrentItem() == 0)) {
259 myTextEntry->setFocus();
260 return 1;
261 }
262 // let other elements handle the keypress
263 return 0;
264}
265
266
267long
268GUIDialog_ChooserAbstract::onCmdFilter(FXObject*, FXSelector, void*) {
270 std::vector<GUIGlID> selectedGlIDs;
271 const int numItems = myList->getNumItems();
272 for (int i = 0; i < numItems; i++) {
273 const GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
274 if (myList->getItemIcon(i) == flag) {
275 selectedGlIDs.push_back(glID);
276 }
277 }
278 refreshList(selectedGlIDs);
279 return 1;
280}
281
282
283long
284GUIDialog_ChooserAbstract::onCmdFilterSubstr(FXObject*, FXSelector, void*) {
285 const bool caseSensitive = myCaseSensitive->getCheck() == TRUE;
286 std::vector<GUIGlID> selectedGlIDs;
287 const int numItems = myList->getNumItems();
288 FXString t = myTextEntry->getText();
289 if (!caseSensitive) {
290 t = t.lower();
291 }
292 for (int i = 0; i < numItems; i++) {
293 FXString t2 = myList->getItemText(i);
294 if (!caseSensitive) {
295 t2 = t2.lower();
296 }
297 if (t2.find(t) >= 0) {
298 const GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
299 selectedGlIDs.push_back(glID);
300 }
301 }
302 refreshList(selectedGlIDs);
303 // filter ACs in netedit
304 filterACs(selectedGlIDs);
306 onChgText(nullptr, 0, nullptr);
307 return 1;
308}
309
310
311std::string
313 if (myLocateByName) {
314 return o->getOptionalName();
315 } else {
316 return o->getMicrosimID();
317 }
318}
319
320void
321GUIDialog_ChooserAbstract::refreshList(const std::vector<GUIGlID>& ids) {
322 myList->clearItems();
323 for (auto i : ids) {
325 if (o == nullptr) {
326 continue;
327 }
328 const std::string& name = getObjectName(o);
329 const bool selected = myWindowsParent->isSelected(o);
330 FXIcon* const ico = selected ? GUIIconSubSys::getIcon(GUIIcon::FLAG) : nullptr;
331 myIDs.insert(o->getGlID());
332 myList->appendItem(name.c_str(), ico, (void*) & (*myIDs.find(o->getGlID())));
334 }
335 myList->update();
336 myCountLabel->setText(TLF("% objects", toString(ids.size())).c_str());
337}
338
339
340long
343 int i = myList->getCurrentItem();
344 if (i >= 0) {
346 if (myList->getItemIcon(i) == flag) {
347 myList->setItemIcon(i, nullptr);
348 } else {
349 myList->setItemIcon(i, flag);
350 }
351 }
352 myList->update();
353 myWindowsParent->getView()->update();
354 return 1;
355}
356
357
358long
361 const int numItems = myList->getNumItems();
362 for (int i = 0; i < numItems; i++) {
363 select(i);
364 myList->setItemIcon(i, flag);
365 }
366 myList->update();
367 myWindowsParent->getView()->update();
368 return 1;
369}
370
371
372long
374 const int numItems = myList->getNumItems();
375 for (int i = 0; i < numItems; i++) {
376 deselect(i);
377 myList->setItemIcon(i, nullptr);
378 }
379 myList->update();
380 myWindowsParent->getView()->update();
381 return 1;
382}
383
384
385long
386GUIDialog_ChooserAbstract::onCmdLocateByName(FXObject*, FXSelector, void*) {
387 std::vector<std::pair<std::string, GUIGlID> > namesAndIDs;
388 myLocateByName = true;
389 const int numItems = myList->getNumItems();
390 for (int i = 0; i < numItems; i++) {
391 GUIGlID glID = *static_cast<GUIGlID*>(myList->getItemData(i));
393 if (o != 0) {
394 const std::string& name = getObjectName(o);
395 if (name != "") {
396 namesAndIDs.push_back(std::make_pair(name, glID));
397 }
398 }
400 }
401 std::sort(namesAndIDs.begin(), namesAndIDs.end());
402 std::vector<GUIGlID> selectedGlIDs;
403 for (const auto& item : namesAndIDs) {
404 selectedGlIDs.push_back(item.second);
405 }
406 refreshList(selectedGlIDs);
407 myTextEntry->setFocus();
408 return 1;
409}
410
411long
412GUIDialog_ChooserAbstract::onCmdUpdate(FXObject*, FXSelector, void*) {
414 return 1;
415}
416
417void
419 GUIGlID* glID = static_cast<GUIGlID*>(myList->getItemData(listIndex));
421}
422
423void
425 GUIGlID* glID = static_cast<GUIGlID*>(myList->getItemData(listIndex));
426 gSelected.select(*glID);
427}
428
429void
431 GUIGlID* glID = static_cast<GUIGlID*>(myList->getItemData(listIndex));
432 gSelected.deselect(*glID);
433}
434
435
436void
437GUIDialog_ChooserAbstract::filterACs(const std::vector<GUIGlID>& /*GLIDs*/) {
438 // overrided in GNEDialogACChooser
439}
440
441/****************************************************************************/
@ MID_CHOOSER_TRACK
Track object.
Definition GUIAppEnum.h:581
@ MID_CANCEL
Cancel-button pressed.
Definition GUIAppEnum.h:308
@ MID_CHOOSER_TEXT
Text entry.
Definition GUIAppEnum.h:583
@ MID_UPDATE
Update-button pressed.
Definition GUIAppEnum.h:310
@ MID_CHOOSEN_INVERT
Deselect selected items.
Definition GUIAppEnum.h:615
@ MID_CHOOSEN_SELECT
select all items
Definition GUIAppEnum.h:611
@ MID_CHOOSER_LIST
Object list.
Definition GUIAppEnum.h:585
@ MID_CHOOSEN_NAME
Deselect selected items.
Definition GUIAppEnum.h:617
@ MID_CHOOSER_FILTER_SUBSTR
Filter list by substring.
Definition GUIAppEnum.h:589
@ MID_CHOOSEN_CLEAR
Clear set.
Definition GUIAppEnum.h:605
@ MID_CHOOSER_FILTER
Filter selected.
Definition GUIAppEnum.h:587
@ MID_CHOOSER_CENTER
Center object.
Definition GUIAppEnum.h:579
#define GUIDesignChooserTextField
design for Chooser TextField
Definition GUIDesigns.h:651
#define GUIDesignChooserListSingle
design for Chooser List
Definition GUIDesigns.h:654
#define GUIDesignChooserButtons
design for Chooser buttons
Definition GUIDesigns.h:648
#define GUIDesignChooserLayoutLeft
design for Chooser Layout left
Definition GUIDesigns.h:663
#define GUIDesignChooserLayoutRight
design for Chooser Layout right
Definition GUIDesigns.h:666
#define GUIDesignHorizontalSeparator
Definition GUIDesigns.h:463
#define GUIDesignChooserLayoutList
design for Chooser Layout list
Definition GUIDesigns.h:669
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frame extended in all directions
Definition GUIDesigns.h:390
#define GUIDesignChooserDialog
Definition GUIDesigns.h:645
FXDEFMAP(GUIDialog_ChooserAbstract) GUIDialog_ChooserAbstractMap[]
unsigned int GUIGlID
Definition GUIGlObject.h:44
@ GLO_VEHICLE
a vehicle
GUISelectedStorage gSelected
A global holder of selected objects.
@ RECENTERVIEW
#define TL(string)
Definition MsgHandler.h:305
#define TLF(string,...)
Definition MsgHandler.h:307
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
static FXButton * buildFXButton(FXComposite *p, const std::string &text, const std::string &tip, const std::string &help, FXIcon *ic, FXObject *tgt, FXSelector sel, FXuint opts=BUTTON_NORMAL, FXint x=0, FXint y=0, FXint w=0, FXint h=0, FXint pl=DEFAULT_PAD, FXint pr=DEFAULT_PAD, FXint pt=DEFAULT_PAD, FXint pb=DEFAULT_PAD)
build button
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.
FXCheckButton * myCaseSensitive
Whether search is case sensitive.
int myMessageId
the object type being chosen
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.
virtual void deselect(int listIndex)
unset selection (handled differently in netedit)
virtual void select(int listIndex)
set selection (handled differently in netedit)
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
FXLabel * myCountLabel
label for declaring list size
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 field.
GUIGlChildWindow * myWindowsParent
window parent
long onCmdUpdate(FXObject *, FXSelector, void *)
Callback: Update list.
std::set< GUIGlID > myIDs
myList contains (void) pointers to elements of myIDs instead of the more volatile pointers to GUIGlOb...
long onChgListSel(FXObject *, FXSelector, void *)
Callback: Current list item selection has changed.
GUIGlObject * mySelected
The chosen id.
long onCmdClearListSelection(FXObject *, FXSelector, void *)
long onCmdAddListSelection(FXObject *, FXSelector, void *)
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
retrieve name for the given object
GUIDialog_ChooserAbstract(GUIGlChildWindow *windowsParent, int messageId, FXIcon *icon, const FXString &title, const std::vector< GUIGlID > &ids, GUIGlObjectStorage &glStorage)
Constructor.
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 / list.
FXCheckButton * myInstantCenter
Whether each change in the list should re-center the view.
long onChgList(FXObject *, FXSelector, void *)
Callback: Current list item has changed.
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 * getGUIMainWindowParent()
Returns the GUIMainWindow parent.
virtual std::vector< GUIGlID > getObjectIDs(int messageId) const
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.
GUIGlObject * getObjectBlocking(GUIGlID id) const
Returns the object from the container locking it.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
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)
void addChild(FXMainWindow *child)
Adds a further child window to the list (FXMainWindow)
Persists window position in the registry.
virtual void stopTrack()
stop track
virtual void startTrack(int)
star track
void toggleSelection(GUIGlID id)
Toggles selection of an object.
void select(GUIGlID id, bool update=true)
Adds the object with the given id.
void deselect(GUIGlID id)
Deselects the object with the given id.