Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNETypeDistributionFrame.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-2023 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/****************************************************************************/
18// The Widget for edit type distribution elements
19/****************************************************************************/
20#include <config.h>
21
22#include <netedit/GNENet.h>
23#include <netedit/GNEUndoList.h>
24#include <netedit/GNEViewNet.h>
33
35
36
37// ===========================================================================
38// FOX callback mapping
39// ===========================================================================
40
46
51
55
59
60// Object implementation
61FXIMPLEMENT(GNETypeDistributionFrame::TypeDistributionEditor, MFXGroupBoxModule, typeEditorMap, ARRAYNUMBER(typeEditorMap))
62FXIMPLEMENT(GNETypeDistributionFrame::TypeDistributionSelector, MFXGroupBoxModule, typeSelectorMap, ARRAYNUMBER(typeSelectorMap))
63FXIMPLEMENT(GNETypeDistributionFrame::TypeAttributesEditorRow, FXHorizontalFrame, TypeAttributesEditorRowMap, ARRAYNUMBER(TypeAttributesEditorRowMap))
64FXIMPLEMENT(GNETypeDistributionFrame::TypeAttributesEditor, MFXGroupBoxModule, TypeAttributesEditorMap, ARRAYNUMBER(TypeAttributesEditorMap))
65
66
67// ===========================================================================
68// method definitions
69// ===========================================================================
70
71// ---------------------------------------------------------------------------
72// GNETypeFrame::TypeDistributionEditor - methods
73// ---------------------------------------------------------------------------
74
76 MFXGroupBoxModule(typeDistributionFrameParent, TL("Type Editor")),
77 myTypeDistributionFrameParent(typeDistributionFrameParent) {
78 // Create new vehicle type
79 myCreateTypeButton = new FXButton(getCollapsableFrame(), TL("Create type distribution"), GUIIconSubSys::getIcon(GUIIcon::VTYPEDISTRIBUTION), this, MID_GNE_CREATE, GUIDesignButton);
80 // Create delete/reset vehicle type
81 myDeleteTypeButton = new FXButton(getCollapsableFrame(), TL("Delete type distribution"), GUIIconSubSys::getIcon(GUIIcon::MODEDELETE), this, MID_GNE_DELETE, GUIDesignButton);
82 // show type editor
83 show();
84}
85
86
88
89
90long
92 auto viewNet = myTypeDistributionFrameParent->myViewNet;
93 // obtain a new valid Type ID
94 const std::string typeDistributionID = viewNet->getNet()->getAttributeCarriers()->generateDemandElementID(SUMO_TAG_VTYPE_DISTRIBUTION);
95 // create new vehicle type
96 GNEDemandElement* type = new GNEVTypeDistribution(viewNet->getNet(), typeDistributionID);
97 // add it using undoList (to allow undo-redo)
98 viewNet->getUndoList()->begin(GUIIcon::VTYPEDISTRIBUTION, "create vehicle type distribution");
99 viewNet->getUndoList()->add(new GNEChange_DemandElement(type, true), true);
100 viewNet->getUndoList()->end();
101 return 1;
102}
103
104
105long
107 auto viewNet = myTypeDistributionFrameParent->myViewNet;
108 // begin undo list operation
109 viewNet->getUndoList()->begin(GUIIcon::VTYPE, "delete vehicle type distribution");
110 // remove vehicle type (and all of their children)
111 viewNet->getNet()->deleteDemandElement(myTypeDistributionFrameParent->myTypeDistributionSelector->getCurrentTypeDistribution(), viewNet->getUndoList());
112 // end undo list operation
113 viewNet->getUndoList()->end();
114 return 1;
115}
116
117
118long
120 // first check if selected VType is valid
121 if (myTypeDistributionFrameParent->myTypeDistributionSelector->getCurrentTypeDistribution()) {
122 return sender->handle(this, FXSEL(SEL_COMMAND, ID_ENABLE), nullptr);
123 } else {
124 return sender->handle(this, FXSEL(SEL_COMMAND, ID_DISABLE), nullptr);
125 }
126}
127
128// ---------------------------------------------------------------------------
129// GNETypeFrame::TypeDistributionSelector - methods
130// ---------------------------------------------------------------------------
131
133 MFXGroupBoxModule(typeFrameParent, TL("Current Type")),
134 myTypeDistributionFrameParent(typeFrameParent) {
135 // Create FXComboBox
137 // add default Types (always first)
139 myTypeComboBox->appendItem(vType->getID().c_str(), vType->getACIcon());
140 }
141 // Set visible items
142 if (myTypeComboBox->getNumItems() <= 20) {
143 myTypeComboBox->setNumVisible((int)myTypeComboBox->getNumItems());
144 } else {
145 myTypeComboBox->setNumVisible(20);
146 }
147 // TypeDistributionSelector is always shown
148 show();
149}
150
151
153
154
157 return myTypeDistributionFrameParent->getViewNet()->getNet()->getAttributeCarriers()->retrieveDemandElement(SUMO_TAG_VTYPE_DISTRIBUTION, myCurrentTypeDistribution, false);
158}
159
160
161void
163 myCurrentTypeDistribution = vTypeDistribution->getID();
164 refreshTypeDistributionSelector();
165}
166
167
168void
170 // get ACs
171 const auto& ACs = myTypeDistributionFrameParent->getViewNet()->getNet()->getAttributeCarriers();
172 // clear items
173 myTypeComboBox->clearItems();
174 // fill myTypeMatchBox with list of type distributions sorted by ID
175 std::map<std::string, GNEDemandElement*> typeDistributions;
176 for (const auto& vTypeDistribution : ACs->getDemandElements().at(SUMO_TAG_VTYPE_DISTRIBUTION)) {
177 typeDistributions[vTypeDistribution->getID()] = vTypeDistribution;
178 }
179 for (const auto& vTypeDistribution : typeDistributions) {
180 myTypeComboBox->appendItem(vTypeDistribution.first.c_str(), vTypeDistribution.second->getACIcon());
181 }
182 // Set visible items
183 if (myTypeComboBox->getNumItems() <= 20) {
184 myTypeComboBox->setNumVisible((int)myTypeComboBox->getNumItems());
185 } else {
186 myTypeComboBox->setNumVisible(20);
187 }
188 // check current type
189 bool validCurrentTypeDistribution = false;
190 for (int i = 0; i < (int)myTypeComboBox->getNumItems(); i++) {
191 if (myTypeComboBox->getItem(i).text() == myCurrentTypeDistribution) {
192 myTypeComboBox->setCurrentItem(i);
193 validCurrentTypeDistribution = true;
194 }
195 }
196 // Check that give vType type is valid
197 GNEDemandElement* vTypeDistribution = nullptr;
198 if (validCurrentTypeDistribution) {
199 vTypeDistribution = ACs->retrieveDemandElement(SUMO_TAG_VTYPE_DISTRIBUTION, myCurrentTypeDistribution);
200 } else {
201 vTypeDistribution = ACs->retrieveFirstDemandElement(SUMO_TAG_VTYPE_DISTRIBUTION);
202 }
203 // Check that give vType type is valid
204 if (vTypeDistribution) {
205 myCurrentTypeDistribution = vTypeDistribution->getID();
206 // set myCurrentType as inspected element
207 myTypeDistributionFrameParent->getViewNet()->setInspectedAttributeCarriers({vTypeDistribution});
208 // show modules
209 myTypeDistributionFrameParent->myTypeTypeAttributesEditor->showAttributeEditorModule();
210 } else {
211 myCurrentTypeDistribution.clear();
212 // set myCurrentType as inspected element
213 myTypeDistributionFrameParent->getViewNet()->setInspectedAttributeCarriers({});
214 // hide modules
215 myTypeDistributionFrameParent->myTypeTypeAttributesEditor->hideTypeAttributesEditorModule();
216 }
217}
218
219
220long
222 const auto viewNet = myTypeDistributionFrameParent->getViewNet();
223 const auto& vTypeDistributions = viewNet->getNet()->getAttributeCarriers()->getDemandElements().at(SUMO_TAG_VTYPE_DISTRIBUTION);
224 // Check if value of myTypeMatchBox correspond of an allowed additional tags
225 for (const auto& vTypeDistribution : vTypeDistributions) {
226 if (vTypeDistribution->getID() == myTypeComboBox->getText().text()) {
227 // set pointer
228 myCurrentTypeDistribution = vTypeDistribution->getID();
229 // set color of myTypeMatchBox to black (valid)
230 myTypeComboBox->setTextColor(FXRGB(0, 0, 0));
231 // set myCurrentType as inspected element
232 viewNet->setInspectedAttributeCarriers({vTypeDistribution});
233 // show modules if selected item is valid
234 myTypeDistributionFrameParent->myTypeTypeAttributesEditor->showAttributeEditorModule();
235 // Write Warning in console if we're in testing mode
236 WRITE_DEBUG(("Selected item '" + myTypeComboBox->getText() + "' in TypeDistributionSelector").text());
237 // update viewNet
238 viewNet->updateViewNet();
239 return 1;
240 }
241 }
242 myCurrentTypeDistribution.clear();
243 // hide all modules if selected item isn't valid
244 myTypeDistributionFrameParent->myTypeTypeAttributesEditor->hideTypeAttributesEditorModule();
245 // set color of myTypeMatchBox to red (invalid)
246 myTypeComboBox->setTextColor(FXRGB(255, 0, 0));
247 // Write Warning in console if we're in testing mode
248 WRITE_DEBUG("Selected invalid item in TypeDistributionSelector");
249 // update viewNet
250 viewNet->updateViewNet();
251 return 1;
252}
253
254
255long
257 const auto& demandElements = myTypeDistributionFrameParent->getViewNet()->getNet()->getAttributeCarriers()->getDemandElements();
258 if (demandElements.at(SUMO_TAG_VTYPE_DISTRIBUTION).size() > 0) {
259 return sender->handle(this, FXSEL(SEL_COMMAND, ID_ENABLE), nullptr);
260 } else {
261 return sender->handle(this, FXSEL(SEL_COMMAND, ID_DISABLE), nullptr);
262 }
263}
264
265// ---------------------------------------------------------------------------
266// GNETypeDistributionFrame::TypeAttributesEditorRow - methods
267// ---------------------------------------------------------------------------
268
270 const GNEAttributeProperties& ACAttr, const std::string& value) :
271 FXHorizontalFrame(attributeEditorParent->getCollapsableFrame(), GUIDesignAuxiliarHorizontalFrame),
272 myTypeAttributesEditorParent(attributeEditorParent),
273 myACAttr(ACAttr) {
274 // Create and hide label
277 "attributeLabel", nullptr, GUIDesignLabelThickedFixed(100));
278 // Create and hide MFXTextFieldTooltip for string attributes
282 // only create if parent was created
283 if (getParent()->id()) {
284 // create TypeAttributesEditorRow
285 FXHorizontalFrame::create();
286 // Show attribute Label
287 myAttributeLabel->setText(myACAttr.getAttrStr().c_str());
288 myAttributeLabel->setTipText(myACAttr.getDefinition().c_str());
289 // In any other case (String, list, etc.), show value as String
290 myValueTextField->setText(value.c_str());
291 myValueTextField->setTextColor(FXRGB(0, 0, 0));
292 myValueTextField->killFocus();
293 // Show TypeAttributesEditorRow
294 show();
295 }
296}
297
298
299void
301 // only destroy if parent was created
302 if (getParent()->id()) {
303 FXHorizontalFrame::destroy();
304 }
305}
306
307
308void
310 // set last valid value and restore color if onlyValid is disabled
311 myValueTextField->setText(value.c_str());
312 // set blue color if is an computed value
313 myValueTextField->setTextColor(FXRGB(0, 0, 0));
314 myValueTextField->killFocus();
315}
316
317
318bool
320 return (myValueTextField->getTextColor() == FXRGB(0, 0, 0));
321}
322
323
324long
326 // Declare changed value
327 std::string newVal;
328 // Check if default value must be set
329 if (myValueTextField->getText().empty() && myACAttr.hasDefaultValue()) {
330 newVal = myACAttr.getDefaultValue();
331 myValueTextField->setText(newVal.c_str());
332 } else {
333 // obtain value of myValueTextField
334 newVal = myValueTextField->getText().text();
335 }
336 // get current distribution
337 auto currentDistribution = myTypeAttributesEditorParent->getTypeDistributionFrameParent()->getTypeDistributionSelector()->getCurrentTypeDistribution();
338 // continue if we have a distribution to edit
339 if (currentDistribution) {
340 // Check if attribute must be changed
341 if (currentDistribution->isValid(myACAttr.getAttr(), newVal)) {
342 // set attribute
343 currentDistribution->setAttribute(myACAttr.getAttr(), myACAttr.getDefaultValue(), myTypeAttributesEditorParent->getTypeDistributionFrameParent()->getViewNet()->getUndoList());
344 // update text field
345 myValueTextField->setTextColor(FXRGB(0, 0, 0));
346 myValueTextField->setBackColor(FXRGB(255, 255, 255));
347 myValueTextField->killFocus();
348 // in this case, we need to refresh the other values (For example, allow/Disallow objects)
349 myTypeAttributesEditorParent->refreshAttributeEditor();
350 // update frame parent after attribute successfully set
351 myTypeAttributesEditorParent->getTypeDistributionFrameParent()->attributeUpdated(myACAttr.getAttr());
352 } else {
353 myValueTextField->setTextColor(FXRGB(255, 0, 0));
354 if (newVal.empty()) {
355 myValueTextField->setBackColor(FXRGBA(255, 213, 213, 255));
356 }
357 // Write Warning in console if we're in testing mode
358 WRITE_DEBUG(TL("Value '") + newVal + TL("' for attribute ") + myACAttr.getAttrStr() + TL(" of ") + myACAttr.getTagPropertyParent().getTagStr() + TL(" isn't valid"));
359 }
360 }
361 return 1;
362}
363
364
366 myTypeAttributesEditorParent(nullptr) {
367}
368
369// ---------------------------------------------------------------------------
370// GNETypeDistributionFrame::TypeAttributesEditor - methods
371// ---------------------------------------------------------------------------
372
374 MFXGroupBoxModule(typeDistributionFrameParent, TL("Internal attributes")),
375 myTypeDistributionFrameParent(typeDistributionFrameParent) {
376 // resize myTypeAttributesEditorRows
378 // Create help button
379 myHelpButton = new FXButton(getCollapsableFrame(), TL("Help"), nullptr, this, MID_HELP, GUIDesignButtonRectangular);
380}
381
382
383void
385 // first remove all rows
386 for (auto& row : myTypeAttributesEditorRows) {
387 // destroy and delete all rows
388 if (row != nullptr) {
389 row->destroy();
390 delete row;
391 row = nullptr;
392 }
393 }
394 // get current distribution
395 auto currentDistribution = myTypeDistributionFrameParent->getTypeDistributionSelector()->getCurrentTypeDistribution();
396 // continue if we have a distribution to edit
397 if (currentDistribution) {
398 // Iterate over attributes
399 for (const auto& attrProperty : currentDistribution->getTagProperty()) {
400 // create attribute editor row
401 myTypeAttributesEditorRows[attrProperty.getPositionListed()] = new TypeAttributesEditorRow(this,
402 attrProperty, currentDistribution->getAttribute(attrProperty.getAttr()));
403 }
404 // show TypeAttributesEditor
405 show();
406 }
407 // reparent help button (to place it at bottom)
408 myHelpButton->reparent(this);
409}
410
411
412void
417
418
419void
421 // get current distribution
422 auto currentDistribution = myTypeDistributionFrameParent->getTypeDistributionSelector()->getCurrentTypeDistribution();
423 // continue if we have a distribution to edit
424 if (currentDistribution) {
425 // Iterate over inspected attribute carriers
426 for (const auto& attrProperty : currentDistribution->getTagProperty()) {
427 // Refresh attributes
428 myTypeAttributesEditorRows[attrProperty.getPositionListed()]->refreshTypeAttributesEditorRow(currentDistribution->getAttribute(attrProperty.getAttr()));
429 }
430 }
431}
432
433
436 return myTypeDistributionFrameParent;
437}
438
439
440long
442 // open Help attributes dialog
443 myTypeDistributionFrameParent->openHelpAttributesDialog(myTypeDistributionFrameParent->getTypeDistributionSelector()->getCurrentTypeDistribution());
444 return 1;
445}
446
447// ---------------------------------------------------------------------------
448// GNETypeDistributionFrame - methods
449// ---------------------------------------------------------------------------
450
452 GNEFrame(viewParent, viewNet, "Type Distributions") {
453
454 // create type editor
456
457 // create type selector
459
460 // Create vehicle type attributes editor
462}
463
464
466
467
468void
470 // refresh type selector
472 // show frame
474}
475
476
481
482
483void
487
488/****************************************************************************/
FXDEFMAP(GNETypeDistributionFrame::TypeDistributionEditor) typeEditorMap[]
@ MID_GNE_DELETE
delete element
Definition GUIAppEnum.h:911
@ MID_GNE_SET_ATTRIBUTE
attribute edited
Definition GUIAppEnum.h:907
@ MID_GNE_CREATE
create element
Definition GUIAppEnum.h:909
@ MID_HELP
help button
Definition GUIAppEnum.h:645
@ MID_GNE_SET_TYPE
used to select a type of element in a combo box
Definition GUIAppEnum.h:927
#define GUIDesignButton
Definition GUIDesigns.h:74
#define GUIDesignComboBox
Definition GUIDesigns.h:288
#define GUIDesignComboBoxNCol
number of column of every combo box
Definition GUIDesigns.h:306
#define GUIDesignTextField
Definition GUIDesigns.h:51
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames
Definition GUIDesigns.h:394
#define GUIDesignButtonRectangular
little rectangular button used in frames (For example, in "help" buttons)
Definition GUIDesigns.h:80
#define GUIDesignTextFieldNCol
Num of column of text field.
Definition GUIDesigns.h:66
#define GUIDesignLabelThickedFixed(width)
label thicked, icon before text, text centered and custom width
Definition GUIDesigns.h:247
@ VTYPEDISTRIBUTION
#define WRITE_DEBUG(msg)
Definition MsgHandler.h:281
#define TL(string)
Definition MsgHandler.h:287
@ SUMO_TAG_VTYPE_DISTRIBUTION
distribution of a vehicle type
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
const std::string getID() const
get ID (all Attribute Carriers have one)
GNENet * getNet() const
get pointer to net
static const size_t MAXNUMBEROFATTRIBUTES
max number of attributes allowed for every tag
const std::string & getAttrStr() const
get XML Attribute
const std::string & getDefinition() const
get default value
An Element which don't belong to GNENet but has influence in the simulation.
GNEViewNet * getViewNet() const
get view net
Definition GNEFrame.cpp:150
virtual void show()
show Frame
Definition GNEFrame.cpp:115
virtual void hide()
hide Frame
Definition GNEFrame.cpp:124
void openHelpAttributesDialog(const GNEAttributeCarrier *AC) const
Open help attributes dialog.
Definition GNEFrame.cpp:184
const std::map< SumoXMLTag, std::set< GNEDemandElement * > > & getDemandElements() const
get demand elements
GNEDemandElement * retrieveDemandElement(SumoXMLTag type, const std::string &id, bool hardFail=true) const
Returns the named demand element.
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition GNENet.cpp:120
void showAttributeEditorModule()
show attributes of multiple ACs
long onCmdTypeAttributesEditorHelp(FXObject *, FXSelector, void *)
Called when user press the help button.
TypeAttributesEditor(GNETypeDistributionFrame *typeDistributionFrameParent)
FOX-declaration.
void refreshAttributeEditor()
refresh attribute editor (only the valid values will be refresh)
GNETypeDistributionFrame * getTypeDistributionFrameParent() const
pointer to GNEFrame parent
std::vector< TypeAttributesEditorRow * > myTypeAttributesEditorRows
list of Attribute editor rows
const GNEAttributeProperties myACAttr
current AC Attribute
void refreshTypeAttributesEditorRow(const std::string &value)
refresh current row
long onCmdSetAttribute(FXObject *, FXSelector, void *)
try to set new attribute value
void destroy()
destroy GNEAttributesCreatorRow (but don't delete)
MFXLabelTooltip * myAttributeLabel
pointer to attribute label
MFXTextFieldTooltip * myValueTextField
textField to modify the value of string attributes
bool isTypeAttributesEditorRowValid() const
check if current attribute of TextField/ComboBox is valid
long onCmdCreateType(FXObject *, FXSelector, void *)
Called when "create type distribution" button is clicked.
long onUpdDeleteType(FXObject *sender, FXSelector, void *)
Called when "Delete type distribution" button is updated.
long onCmdDeleteType(FXObject *, FXSelector, void *)
Called when "Delete type distribution" button is clicked.
FXComboBox * myTypeComboBox
comboBox with the list of type distributions
GNETypeDistributionFrame * myTypeDistributionFrameParent
pointer to Frame Parent
void setCurrentTypeDistribution(const GNEDemandElement *vTypeDistribution)
set current vehicle type distribution
long onCmdUpdateTypeDistribution(FXObject *sender, FXSelector, void *)
update type distribution comboBox
TypeDistributionSelector(GNETypeDistributionFrame *typeFrameParent)
FOX-declaration.
GNEDemandElement * getCurrentTypeDistribution() const
get current Vehicle Type distribution
long onCmdSelectTypeDistribution(FXObject *, FXSelector, void *)
Called when the user select type distribution in ComboBox.
TypeDistributionSelector * getTypeDistributionSelector() const
get type distribution selector
TypeDistributionSelector * myTypeDistributionSelector
type distribution selector
void attributeUpdated(SumoXMLAttr attribute)
function called after set a valid attribute in AttributeCreator/AttributeEditor/ParametersEditor/....
TypeDistributionEditor * myTypeDistributionEditor
type editor
GNETypeDistributionFrame(GNEViewParent *viewParent, GNEViewNet *viewNet)
Constructor.
TypeAttributesEditor * myTypeTypeAttributesEditor
editor for vehicle type attributes
GNENet * getNet() const
get the net object
GNEViewParent * getViewParent() const
get the net object
A single child window which contains a view of the simulation area.
GNEApplicationWindow * getGNEAppWindows() const
get GNE Application Windows
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
MFXStaticToolTip * getStaticTooltipMenu() const
get static toolTip for menus
MFXGroupBoxModule (based on FXGroupBox)
FXVerticalFrame * getCollapsableFrame()
get collapsable frame (used by all elements that will be collapsed if button is toggled)