Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 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 : /****************************************************************************/
14 : /// @file GUIParameterTableItem.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date
18 : ///
19 : // A single line in a parameter window
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 :
24 : #include <string>
25 : #include <utils/foxtools/fxheader.h>
26 : #include <utils/common/ValueSource.h>
27 : #include <utils/common/ToString.h>
28 : #include <utils/gui/div/GUIParam_PopupMenu.h>
29 : #include <utils/gui/images/GUIIconSubSys.h>
30 : #include <utils/gui/windows/GUIAppEnum.h>
31 :
32 :
33 : // ===========================================================================
34 : // class definitions
35 : // ===========================================================================
36 : // ---------------------------------------------------------------------------
37 : // GUIParameterTableItemInterface
38 : // ---------------------------------------------------------------------------
39 : /**
40 : * @class GUIParameterTableItemInterface
41 : * @brief Interface to a single line in a parameter window
42 : *
43 : * Because a single line in a parameter window may display different kinds of
44 : * information (different value types, dynamic or static information), an
45 : * interface is needed in order to allow a common access to the functions of
46 : * a line - basically the possibility to open a tracker (GUIParameterTracker)
47 : * for the stored, dynamic value.
48 : *
49 : * The implementation is done by GUIParameterTableItem.
50 : *
51 : * @see GUIParameterTracker
52 : * @see GUIParameterTableItem
53 : */
54 : class GUIParameterTableItemInterface {
55 : public:
56 : /// @brief Destructor
57 : virtual ~GUIParameterTableItemInterface() {}
58 :
59 :
60 : /// @name Methods to be implemented by derived classes
61 : /// @{
62 :
63 : /// @brief Returns the information whether the value changes over simulation time
64 : virtual bool dynamic() const = 0;
65 :
66 : /// @brief Forces an update of the value
67 : virtual void update() = 0;
68 :
69 : /// @brief Returns a double-typed copy of the value-source
70 : virtual ValueSource<double>* getdoubleSourceCopy() const = 0;
71 :
72 : /// @brief Returns the name of the value
73 : virtual const std::string& getName() const = 0;
74 : /// @}
75 : };
76 :
77 :
78 : // ---------------------------------------------------------------------------
79 : // GUIParameterTableItem
80 : // ---------------------------------------------------------------------------
81 : /**
82 : * @class GUIParameterTableItem
83 : * @brief Instance of a single line in a parameter window
84 : *
85 : * This class represents a single item of a parameter table and is an
86 : * implementation of the GUIParameterTableItemInterface that allows different
87 : * value-types.
88 : *
89 : * As some values may change over the simulation, this class holds the
90 : * information whether they change and how to ask for new values if they do.
91 : *
92 : * @see GUIParameterTracker
93 : * @see GUIParameterTableItemInterface
94 : */
95 : template<class T>
96 : class GUIParameterTableItem : public GUIParameterTableItemInterface {
97 : public:
98 : /** @brief Constructor for changing (dynamic) values
99 : *
100 : * @param[in] table The table this item belongs to
101 : * @param[in] pos The row of the table this item fills
102 : * @param[in] name The name of the represented value
103 : * @param[in] dynamic Information whether this value changes over time
104 : * @param[in] src The value source
105 : * @todo Consider using a reference to the table
106 : * @todo Check whether the name should be stored in GUIParameterTableItemInterface
107 : */
108 0 : GUIParameterTableItem(FXTable* table, unsigned pos, const std::string& name,
109 : bool dynamic, ValueSource<T>* src) :
110 0 : myAmDynamic(dynamic), myName(name), myTablePosition((FXint) pos), mySource(src),
111 0 : myValue(src->getValue()), myTable(table) {
112 0 : init(dynamic, toString<T>(src->getValue()));
113 0 : }
114 :
115 : /** @brief Constructor for non-changing (static) values
116 : *
117 : * @param[in] table The table this item belongs to
118 : * @param[in] pos The row of the table this item fills
119 : * @param[in] name The name of the represented value
120 : * @param[in] dynamic Information whether this value changes over time
121 : * @param[in] value The value
122 : * @todo Consider using a reference to the table
123 : * @todo Check whether the name should be stored in GUIParameterTableItemInterface
124 : * @todo Should never be dynamic!?
125 : */
126 0 : GUIParameterTableItem(FXTable* table, unsigned pos, const std::string& name,
127 : bool dynamic, T value) :
128 0 : myAmDynamic(dynamic), myName(name), myTablePosition((FXint) pos), mySource(0),
129 0 : myValue(value), myTable(table) {
130 0 : init(dynamic, toString<T>(value));
131 0 : }
132 :
133 : /// @brief Destructor
134 0 : ~GUIParameterTableItem() {
135 0 : delete mySource;
136 0 : }
137 :
138 : /** @brief Initialises the line
139 : *
140 : * Fills the line using the name, the current value, and the information
141 : * whether the value changes over time.
142 : *
143 : * @param[in] dynamic Information whether this value changes over time
144 : * @param[in] value The current (initial) value
145 : */
146 0 : void init(bool dynamic, std::string value) {
147 0 : myTable->setItemText(myTablePosition, 0, myName.c_str());
148 0 : myTable->setItemText(myTablePosition, 1, value.c_str());
149 0 : if (dynamic) {
150 0 : if (getdoubleSourceCopy() == nullptr) {
151 0 : myTable->setItemIcon(myTablePosition, 2, GUIIconSubSys::getIcon(GUIIcon::YES));
152 : } else {
153 0 : myTable->setItemIcon(myTablePosition, 2, GUIIconSubSys::getIcon(GUIIcon::TRACKER));
154 : }
155 : } else {
156 0 : myTable->setItemIcon(myTablePosition, 2, GUIIconSubSys::getIcon(GUIIcon::NO));
157 : }
158 0 : const int lineBreaks = (int)std::count(value.begin(), value.end(), '\n');
159 0 : if (lineBreaks > 0) {
160 0 : myTable->setRowHeight(myTablePosition, myTable->getRowHeight(myTablePosition) * (lineBreaks + 1));
161 : }
162 0 : myTable->setItemJustify(myTablePosition, 2, FXTableItem::CENTER_X | FXTableItem::CENTER_Y);
163 0 : }
164 :
165 : /// @brief Returns the information whether this item may change over time
166 0 : bool dynamic() const {
167 0 : return myAmDynamic;
168 : }
169 :
170 : /// @brief Returns the name of this value
171 0 : const std::string& getName() const {
172 0 : return myName;
173 : }
174 :
175 : /** @brief Resets the value if it's dynamic
176 : *
177 : * If the value is dynamic, the current value is retrieved from the value
178 : * source. If it is different from the previous one (stored in myValue),
179 : * it is stored in myValue and set as the current value text within the
180 : * according table cell.
181 : */
182 0 : void update() {
183 0 : if (!dynamic() || mySource == 0) {
184 0 : return;
185 : }
186 0 : T value = mySource->getValue();
187 0 : if (value != myValue) {
188 0 : myValue = value;
189 0 : myTable->setItemText(myTablePosition, 1, toString<T>(myValue).c_str());
190 : }
191 : }
192 :
193 : /// @brief Returns a copy of the source if the value is dynamic
194 : ValueSource<T>* getSourceCopy() const {
195 : if (mySource == 0) {
196 : return 0;
197 : }
198 : return mySource->copy();
199 : }
200 :
201 : /// @brief Returns a double-typed copy of the source if the value is dynamic
202 0 : ValueSource<double>* getdoubleSourceCopy() const {
203 0 : if (mySource == 0) {
204 : return 0;
205 : }
206 0 : return mySource->makedoubleReturningCopy();
207 : }
208 :
209 : private:
210 : /// @brief Information whether the value may change
211 : bool myAmDynamic;
212 :
213 : /// @brief The name of this value
214 : std::string myName;
215 :
216 : /// @brief The position within the table
217 : FXint myTablePosition;
218 :
219 : /// @brief The source to gain new values from; this source is==0 if the values are not dynamic
220 : ValueSource<T>* mySource;
221 :
222 : /// @brief A backup of the value to avoid the redrawing when nothing has changed
223 : T myValue;
224 :
225 : /// @brief The table this entry belongs to
226 : FXTable* myTable;
227 : };
|