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-2022 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>
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();
151  GUIParameterTracker* tr = new GUIParameterTracker(*myApplication, trackerName);
152  tr->addTracked(*myObject, i->getdoubleSourceCopy(), newTracked);
153  tr->setX(getX() + getWidth() + 10);
154  tr->setY(myTrackerY);
155  tr->create();
156  tr->show();
157  myTrackerY = (myTrackerY + tr->getHeight() + 20) % getApp()->getRootWindow()->getHeight();
158  }
159  }
160  }
161  return FXMainWindow::onLeftBtnPress(sender, sel, eventData);
162 }
163 
164 long
165 GUIParameterTableWindow::onRightButtonPress(FXObject* /*sender*/, FXSelector /*sel*/, void* eventData) {
166  // check which value entry was pressed
167  FXEvent* e = (FXEvent*) eventData;
168  int row = myTable->rowAtY(e->win_y);
169  if (row == -1 || row >= (int)(myItems.size())) {
170  return 1;
171  }
173  if (!i->dynamic()) {
174  return 1;
175  }
176  if (myObject == nullptr) {
177  return 1;
178  }
179 
180  ValueSource<double>* doubleSource = i->getdoubleSourceCopy();
181  if (doubleSource != nullptr) {
183  GUIDesigns::buildFXMenuCommand(p, "Open in new Tracker", nullptr, p, MID_OPENTRACKER);
184  // set geometry
185  p->setX(static_cast<FXEvent*>(eventData)->root_x);
186  p->setY(static_cast<FXEvent*>(eventData)->root_y);
187  p->create();
188  // show
189  p->show();
190  }
191  return 1;
192 }
193 
194 
195 void
196 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, std::string value) {
197  myTable->insertRows((int)myItems.size() + 1);
199  myItems.push_back(i);
200 }
201 
202 
203 void
204 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, double value) {
205  myTable->insertRows((int)myItems.size() + 1);
207  myItems.push_back(i);
208 }
209 
210 
211 void
212 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, unsigned value) {
213  myTable->insertRows((int)myItems.size() + 1);
215  myItems.push_back(i);
216 }
217 
218 
219 void
220 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, int value) {
221  myTable->insertRows((int)myItems.size() + 1);
223  myItems.push_back(i);
224 }
225 
226 
227 void
228 GUIParameterTableWindow::mkItem(const char* name, bool dynamic, long long int value) {
229  myTable->insertRows((int)myItems.size() + 1);
231  myItems.push_back(i);
232 }
233 
234 
235 void
237  FXMutexLock locker(myLock);
238  if (myObject == nullptr) {
239  return;
240  }
241  for (GUIParameterTableItemInterface* const item : myItems) {
242  item->update();
243  }
244 }
245 
246 
247 void
249  // add generic paramters if available
250  if (p == nullptr) {
251  p = dynamic_cast<const Parameterised*>(myObject);
252  }
253  if (p != nullptr) {
254  const std::map<std::string, std::string>& map = p->getParametersMap();
255  for (std::map<std::string, std::string>::const_iterator it = map.begin(); it != map.end(); ++it) {
256  mkItem(("param:" + it->first).c_str(), false, it->second);
257  }
258  }
259  const int rows = (int)myItems.size() + 1;
260  setHeight(rows * 20 + 40);
261  myTable->fitColumnsToContents(1);
262  setWidth(myTable->getContentWidth() + 40);
263  myTable->setVisibleRows(rows);
264  myApplication->addChild(this);
265  create();
266  show();
267 }
268 
269 void
270 GUIParameterTableWindow::checkFont(const std::string& text) {
271  bool missingChar = false;
272  FXString fxs(text.c_str());
273  for (FXint i = 0; i < fxs.length(); i = fxs.inc(i)) {
274  FXwchar wc = fxs.wc(i);
275  if (myTable->getFont()->hasChar(wc) != TRUE) {
276  missingChar = true;
277  break;
278  }
279  //std::cout << i << ": " << wc << " char:" << (char)(wc) << " has: " << (myTable->getFont()->hasChar(wc) == TRUE) << "\n";
280  }
281  if (missingChar) {
283  }
284 }
285 
286 
287 int
289  const Parameterised* p = dynamic_cast<const Parameterised*>(obj);
290  return p != nullptr ? (int)p->getParametersMap().size() : 0;
291 }
292 
293 
294 /****************************************************************************/
@ MID_TABLE
The Table.
Definition: GUIAppEnum.h:493
@ MID_SIMSTEP
A Simulation step was performed.
Definition: GUIAppEnum.h:495
@ MID_OPENTRACKER
A Tracker shall be opened.
Definition: GUIAppEnum.h:497
FXDEFMAP(GUIParameterTableWindow) GUIParameterTableWindowMap[]
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
Definition: GUIDesigns.cpp:42
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)
FXFont * getFallbackFont()
get fallback front
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.
void checkFont(const std::string &text)
ensure that the font covers the given text
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.
static bool addTrackedMultiplot(GUIGlObject &o, ValueSource< double > *src, TrackerValueDesc *newTracked)
all value source to multiplot trackers
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
const std::map< std::string, std::string > & getParametersMap() const
Returns the inner key/value map.
static const RGBColor BLACK
Definition: RGBColor.h:193
Representation of a timeline of floats with their names and moments.