Path: blob/main/src/utils/gui/div/GUIParameterTableItem.h
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GUIParameterTableItem.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @date17///18// A single line in a parameter window19/****************************************************************************/20#pragma once21#include <config.h>2223#include <string>24#include <utils/foxtools/fxheader.h>25#include <utils/common/ValueSource.h>26#include <utils/common/ToString.h>27#include <utils/gui/div/GUIParam_PopupMenu.h>28#include <utils/gui/images/GUIIconSubSys.h>29#include <utils/gui/windows/GUIAppEnum.h>303132// ===========================================================================33// class definitions34// ===========================================================================35// ---------------------------------------------------------------------------36// GUIParameterTableItemInterface37// ---------------------------------------------------------------------------38/**39* @class GUIParameterTableItemInterface40* @brief Interface to a single line in a parameter window41*42* Because a single line in a parameter window may display different kinds of43* information (different value types, dynamic or static information), an44* interface is needed in order to allow a common access to the functions of45* a line - basically the possibility to open a tracker (GUIParameterTracker)46* for the stored, dynamic value.47*48* The implementation is done by GUIParameterTableItem.49*50* @see GUIParameterTracker51* @see GUIParameterTableItem52*/53class GUIParameterTableItemInterface {54public:55/// @brief Destructor56virtual ~GUIParameterTableItemInterface() {}575859/// @name Methods to be implemented by derived classes60/// @{6162/// @brief Returns the information whether the value changes over simulation time63virtual bool dynamic() const = 0;6465/// @brief Forces an update of the value66virtual void update() = 0;6768/// @brief Returns a double-typed copy of the value-source69virtual ValueSource<double>* getdoubleSourceCopy() const = 0;7071/// @brief Returns the name of the value72virtual const std::string& getName() const = 0;73/// @}74};757677// ---------------------------------------------------------------------------78// GUIParameterTableItem79// ---------------------------------------------------------------------------80/**81* @class GUIParameterTableItem82* @brief Instance of a single line in a parameter window83*84* This class represents a single item of a parameter table and is an85* implementation of the GUIParameterTableItemInterface that allows different86* value-types.87*88* As some values may change over the simulation, this class holds the89* information whether they change and how to ask for new values if they do.90*91* @see GUIParameterTracker92* @see GUIParameterTableItemInterface93*/94template<class T>95class GUIParameterTableItem : public GUIParameterTableItemInterface {96public:97/** @brief Constructor for changing (dynamic) values98*99* @param[in] table The table this item belongs to100* @param[in] pos The row of the table this item fills101* @param[in] name The name of the represented value102* @param[in] dynamic Information whether this value changes over time103* @param[in] src The value source104* @todo Consider using a reference to the table105* @todo Check whether the name should be stored in GUIParameterTableItemInterface106*/107GUIParameterTableItem(FXTable* table, unsigned pos, const std::string& name,108bool dynamic, ValueSource<T>* src) :109myAmDynamic(dynamic), myName(name), myTablePosition((FXint) pos), mySource(src),110myValue(src->getValue()), myTable(table) {111init(dynamic, toString<T>(src->getValue()));112}113114/** @brief Constructor for non-changing (static) values115*116* @param[in] table The table this item belongs to117* @param[in] pos The row of the table this item fills118* @param[in] name The name of the represented value119* @param[in] dynamic Information whether this value changes over time120* @param[in] value The value121* @todo Consider using a reference to the table122* @todo Check whether the name should be stored in GUIParameterTableItemInterface123* @todo Should never be dynamic!?124*/125GUIParameterTableItem(FXTable* table, unsigned pos, const std::string& name,126bool dynamic, T value) :127myAmDynamic(dynamic), myName(name), myTablePosition((FXint) pos), mySource(0),128myValue(value), myTable(table) {129init(dynamic, toString<T>(value));130}131132/// @brief Destructor133~GUIParameterTableItem() {134delete mySource;135}136137/** @brief Initialises the line138*139* Fills the line using the name, the current value, and the information140* whether the value changes over time.141*142* @param[in] dynamic Information whether this value changes over time143* @param[in] value The current (initial) value144*/145void init(bool dynamic, std::string value) {146myTable->setItemText(myTablePosition, 0, myName.c_str());147myTable->setItemText(myTablePosition, 1, value.c_str());148if (dynamic) {149if (getdoubleSourceCopy() == nullptr) {150myTable->setItemIcon(myTablePosition, 2, GUIIconSubSys::getIcon(GUIIcon::YES));151} else {152myTable->setItemIcon(myTablePosition, 2, GUIIconSubSys::getIcon(GUIIcon::TRACKER));153}154} else {155myTable->setItemIcon(myTablePosition, 2, GUIIconSubSys::getIcon(GUIIcon::NO));156}157const int lineBreaks = (int)std::count(value.begin(), value.end(), '\n');158if (lineBreaks > 0) {159myTable->setRowHeight(myTablePosition, myTable->getRowHeight(myTablePosition) * (lineBreaks + 1));160}161myTable->setItemJustify(myTablePosition, 2, FXTableItem::CENTER_X | FXTableItem::CENTER_Y);162}163164/// @brief Returns the information whether this item may change over time165bool dynamic() const {166return myAmDynamic;167}168169/// @brief Returns the name of this value170const std::string& getName() const {171return myName;172}173174/** @brief Resets the value if it's dynamic175*176* If the value is dynamic, the current value is retrieved from the value177* source. If it is different from the previous one (stored in myValue),178* it is stored in myValue and set as the current value text within the179* according table cell.180*/181void update() {182if (!dynamic() || mySource == 0) {183return;184}185T value = mySource->getValue();186if (value != myValue) {187myValue = value;188myTable->setItemText(myTablePosition, 1, toString<T>(myValue).c_str());189}190}191192/// @brief Returns a copy of the source if the value is dynamic193ValueSource<T>* getSourceCopy() const {194if (mySource == 0) {195return 0;196}197return mySource->copy();198}199200/// @brief Returns a double-typed copy of the source if the value is dynamic201ValueSource<double>* getdoubleSourceCopy() const {202if (mySource == 0) {203return 0;204}205return mySource->makedoubleReturningCopy();206}207208private:209/// @brief Information whether the value may change210bool myAmDynamic;211212/// @brief The name of this value213std::string myName;214215/// @brief The position within the table216FXint myTablePosition;217218/// @brief The source to gain new values from; this source is==0 if the values are not dynamic219ValueSource<T>* mySource;220221/// @brief A backup of the value to avoid the redrawing when nothing has changed222T myValue;223224/// @brief The table this entry belongs to225FXTable* myTable;226};227228229