Eclipse SUMO - Simulation of Urban MObility
GUIParameterTableWindow.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-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 /****************************************************************************/
21 // The window that holds the table of an object's parameter
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <string>
26 #include <fx.h>
29 #include <utils/common/ToString.h>
38 
39 
40 // ===========================================================================
41 // FOX callback mapping
42 // ===========================================================================
43 FXDEFMAP(GUIParameterTableWindow) GUIParameterTableWindowMap[] = {
44  FXMAPFUNC(SEL_COMMAND, MID_SIMSTEP, GUIParameterTableWindow::onSimStep),
45  FXMAPFUNC(SEL_SELECTED, MID_TABLE, GUIParameterTableWindow::onTableSelected),
46  FXMAPFUNC(SEL_DESELECTED, MID_TABLE, GUIParameterTableWindow::onTableDeselected),
47  FXMAPFUNC(SEL_RIGHTBUTTONPRESS, MID_TABLE, GUIParameterTableWindow::onRightButtonPress),
48  FXMAPFUNC(SEL_LEFTBUTTONPRESS, MID_TABLE, GUIParameterTableWindow::onLeftBtnPress),
49 };
50 
51 FXIMPLEMENT(GUIParameterTableWindow, FXMainWindow, GUIParameterTableWindowMap, ARRAYNUMBER(GUIParameterTableWindowMap))
52 
53 
54 // ===========================================================================
55 // static value definitions
56 // ===========================================================================
58 std::vector<GUIParameterTableWindow*> GUIParameterTableWindow::myContainer;
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
64  FXMainWindow(app.getApp(), (o.getFullName() + " Parameter").c_str(), nullptr, nullptr, DECOR_ALL, 20, 20, 200, 500),
65  myObject(&o),
66  myApplication(&app),
67  myTrackerY(50),
68  myCurrentPos(0) {
69  myTable = new FXTable(this, this, MID_TABLE, TABLE_COL_SIZABLE | TABLE_ROW_SIZABLE | LAYOUT_FILL_X | LAYOUT_FILL_Y);
70  myTable->setTableSize(1, 3);
71  myTable->setVisibleColumns(3);
72  myTable->setBackColor(FXRGB(255, 255, 255));
73  myTable->setColumnText(0, "Name");
74  myTable->setColumnText(1, "Value");
75  myTable->setColumnText(2, "Dynamic");
76  myTable->getRowHeader()->setWidth(0);
77  FXHeader* header = myTable->getColumnHeader();
78  header->setItemJustify(0, JUSTIFY_CENTER_X);
79  header->setItemSize(0, 240);
80  header->setItemJustify(1, JUSTIFY_CENTER_X);
81  header->setItemSize(1, 120);
82  header->setItemJustify(2, JUSTIFY_CENTER_X);
83  header->setItemSize(2, 60);
85  myLock.lock();
87  myLock.unlock();
88  FXMutexLock locker(myGlobalContainerLock);
89  myContainer.push_back(this);
90  // Table cannot be editable
91  myTable->setEditable(FALSE);
92 }
93 
96  myLock.lock();
97  for (std::vector<GUIParameterTableItemInterface*>::iterator i = myItems.begin(); i != myItems.end(); ++i) {
98  delete (*i);
99  }
100  if (myObject != nullptr) {
102  }
103  myLock.unlock();
104  FXMutexLock locker(myGlobalContainerLock);
105  std::vector<GUIParameterTableWindow*>::iterator i = std::find(myContainer.begin(), myContainer.end(), this);
106  if (i != myContainer.end()) {
107  myContainer.erase(i);
108  }
109 }
110 
111 
112 void
114  FXMutexLock locker(myLock);
115  myObject = nullptr;
116 }
117 
118 
119 long
120 GUIParameterTableWindow::onSimStep(FXObject*, FXSelector, void*) {
121  // table values are updated in GUINet::guiSimulationStep()
122  updateTable();
123  update();
124  return 1;
125 }
126 
127 
128 long
129 GUIParameterTableWindow::onTableSelected(FXObject*, FXSelector, void*) {
130  return 1;
131 }
132 
133 
134 long
135 GUIParameterTableWindow::onTableDeselected(FXObject*, FXSelector, void*) {
136  return 1;
137 }
138 
139 long
140 GUIParameterTableWindow::onLeftBtnPress(FXObject* sender, FXSelector sel, void* eventData) {
141  FXEvent* e = (FXEvent*) eventData;
142  int row = myTable->rowAtY(e->win_y);
143  int col = myTable->colAtX(e->win_x);
144  if (col == 2 && row >= 0 && row < (int)myItems.size()) {
146  if (i->dynamic() && i->getdoubleSourceCopy() != nullptr) {
147  // open tracker directly
148  const std::string trackerName = i->getName() + " from " + myObject->getFullName();
149  GUIParameterTracker* tr = new GUIParameterTracker(*myApplication, trackerName);
151  tr->addTracked(*myObject, i->getdoubleSourceCopy(), newTracked);
152  tr->setX(getX() + getWidth() + 10);
153  tr->setY(myTrackerY);
154  tr->create();
155  tr->show();
156  myTrackerY = (myTrackerY + tr->getHeight() + 20) % getApp()->getRootWindow()->getHeight();
157  }
158  }
159  return FXMainWindow::onLeftBtnPress(sender, sel, eventData);
160 }
161 
162 long
163 GUIParameterTableWindow::onRightButtonPress(FXObject* /*sender*/, FXSelector /*sel*/, void* eventData) {
164  // check which value entry was pressed
165  FXEvent* e = (FXEvent*) eventData;
166  int row = myTable->rowAtY(e->win_y);
167  if (row == -1 || row >= (int)(myItems.size())) {
168  return 1;
169  }
171  if (!i->dynamic()) {
172  return 1;
173  }
174  if (myObject == nullptr) {
175  return 1;
176  }
177 
178  ValueSource<double>* doubleSource = i->getdoubleSourceCopy();
179  if (doubleSource != nullptr) {
181  GUIDesigns::buildFXMenuCommand(p, "Open in new Tracker", nullptr, p, MID_OPENTRACKER);
182  // set geometry
183  p->setX(static_cast<FXEvent*>(eventData)->root_x);
184  p->setY(static_cast<FXEvent*>(eventData)->root_y);
185  p->create();
186  // show
187  p->show();
188  }
189  return 1;
190 }
191 
192 
193 void
194 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, std::string value) {
195  myTable->insertRows((int)myItems.size() + 1);
197  myItems.push_back(i);
198 }
199 
200 
201 void
202 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, double value) {
203  myTable->insertRows((int)myItems.size() + 1);
205  myItems.push_back(i);
206 }
207 
208 
209 void
210 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, unsigned value) {
211  myTable->insertRows((int)myItems.size() + 1);
213  myItems.push_back(i);
214 }
215 
216 
217 void
218 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, int value) {
219  myTable->insertRows((int)myItems.size() + 1);
221  myItems.push_back(i);
222 }
223 
224 
225 void
226 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, long long int value) {
227  myTable->insertRows((int)myItems.size() + 1);
229  myItems.push_back(i);
230 }
231 
232 
233 void
235  FXMutexLock locker(myLock);
236  if (myObject == nullptr) {
237  return;
238  }
239  for (GUIParameterTableItemInterface* const item : myItems) {
240  item->update();
241  }
242 }
243 
244 
245 void
247  // add generic paramters if available
248  if (p == nullptr) {
249  p = dynamic_cast<const Parameterised*>(myObject);
250  }
251  if (p != nullptr) {
252  const std::map<std::string, std::string>& map = p->getParametersMap();
253  for (std::map<std::string, std::string>::const_iterator it = map.begin(); it != map.end(); ++it) {
254  mkItem(("param:" + it->first).c_str(), false, it->second);
255  }
256  }
257  const int rows = (int)myItems.size() + 1;
258  setHeight(rows * 20 + 40);
259  myTable->fitColumnsToContents(1);
260  setWidth(myTable->getContentWidth() + 40);
261  myTable->setVisibleRows(rows);
262  myApplication->addChild(this);
263  create();
264  show();
265 }
266 
267 
268 int
270  const Parameterised* p = dynamic_cast<const Parameterised*>(obj);
271  return p != nullptr ? (int)p->getParametersMap().size() : 0;
272 }
273 
274 
275 /****************************************************************************/
@ MID_TABLE
The Table.
Definition: GUIAppEnum.h:479
@ MID_SIMSTEP
A Simulation step was performed.
Definition: GUIAppEnum.h:481
@ MID_OPENTRACKER
A Tracker shall be opened.
Definition: GUIAppEnum.h:483
FXDEFMAP(GUIParameterTableWindow) GUIParameterTableWindowMap[]
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
Definition: GUIDesigns.cpp:40
const std::string & getFullName() const
void addParameterTable(GUIParameterTableWindow *w)
void removeParameterTable(GUIParameterTableWindow *w)
Lets this object know a parameter window showing the object's values was closed.
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
virtual double getTrackerInterval() const =0
get tracker interval (must be implemented in all children)
virtual SUMOTime getCurrentSimTime() const =0
get current sim time (must be implemented in all children)
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)
A popup-menu for dynamic patameter table entries.
Instance of a single line in a parameter window.
Interface to a single line in a parameter window.
virtual ValueSource< double > * getdoubleSourceCopy() const =0
Returns a double-typed copy of the value-source.
virtual bool dynamic() const =0
Returns the information whether the value changes over simulation time.
virtual const std::string & getName() const =0
Returns the name of the value.
A window containing a gl-object's parameter.
long onRightButtonPress(FXObject *, FXSelector, void *)
Shows a popup.
GUIMainWindow * myApplication
The main application window.
static FXMutex myGlobalContainerLock
The mutex used to avoid concurrent updates of the instance container.
void removeObject(GUIGlObject *const o)
Lets this window know the object shown is being deleted.
GUIParameterTableWindow(GUIMainWindow &app, GUIGlObject &o)
Constructor.
static int numParams(const GUIGlObject *obj)
returns the number of parameters if obj is Parameterised and 0 otherwise
void mkItem(const char *name, bool dynamic, ValueSource< T > *src)
Adds a row which obtains its value from a ValueSource.
int myTrackerY
y-position for opening new tracker window
long onLeftBtnPress(FXObject *, FXSelector, void *)
directly opens tracker when clicking on last column
long onTableSelected(FXObject *, FXSelector, void *)
Does nothing.
void closeBuilding(const Parameterised *p=0)
Closes the building of the table.
FXMutex myLock
A lock assuring save updates in case of object deletion.
std::vector< GUIParameterTableItemInterface * > myItems
The list of table rows.
unsigned myCurrentPos
The index of the next row to add - used while building.
long onTableDeselected(FXObject *, FXSelector, void *)
Does nothing.
FXTable * myTable
The table to display the information in.
static std::vector< GUIParameterTableWindow * > myContainer
The container of items that shall be updated.
long onSimStep(FXObject *, FXSelector, void *)
Updates the table due to a simulation step.
void updateTable()
Updates the table.
GUIGlObject * myObject
The object to get the information from.
A window which displays the time line of one (or more) value(s)
void addTracked(GUIGlObject &o, ValueSource< double > *src, TrackerValueDesc *newTracked)
Adds a further time line to display.
void create()
Creates the window.
An upper class for objects with additional parameters.
Definition: Parameterised.h:39
const std::map< std::string, std::string > & getParametersMap() const
Returns the inner key/value map.
static const RGBColor BLACK
Definition: RGBColor.h:188
Representation of a timeline of floats with their names and moments.