Improved values/alias attribute support for string properties.

Dieser Commit ist enthalten in:
Reinder Feenstra 2025-12-30 00:22:34 +01:00
Ursprung cd53ddc713
Commit 72029dc806
3 geänderte Dateien mit 65 neuen und 13 gelöschten Zeilen

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020-2021 Reinder Feenstra
* Copyright (C) 2020-2025 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -36,7 +36,7 @@ PropertyComboBox::PropertyComboBox(Property& property, QWidget* parent) :
setEnabled(m_property.getAttributeBool(AttributeName::Enabled, true));
setVisible(m_property.getAttributeBool(AttributeName::Visible, true));
setEditable(m_property.type() == ValueType::String); //! \todo add attribute for this
setEditable(m_property.getAttributeBool(AttributeName::Custom, m_property.type() == ValueType::String));
setInsertPolicy(QComboBox::NoInsert);
switch(m_property.type())
@ -114,13 +114,28 @@ PropertyComboBox::PropertyComboBox(Property& property, QWidget* parent) :
if(m_internalUpdate == 0)
{
const QVariant v = currentData();
if(v.canConvert<qint64>())
m_property.setValueInt64(v.value<qint64>());
else
m_property.setValueString(v.toString());
switch(m_property.type())
{
case ValueType::Integer:
case ValueType::Enum:
m_property.setValueInt64(v.value<qint64>());
break;
case ValueType::String:
m_property.setValueString(v.toString());
break;
case ValueType::Invalid:
case ValueType::Boolean:
case ValueType::Float:
case ValueType::Object:
case ValueType::Set:
assert(false);
break;
}
}
});
}
}
updateValues();
}
@ -174,10 +189,21 @@ void PropertyComboBox::updateValues()
break;
case ValueType::String:
{
const QVariantList aliasKeys = m_property.getAttribute(AttributeName::AliasKeys, QVariant()).toList();
const QVariantList aliasValues = m_property.getAttribute(AttributeName::AliasValues, QVariant()).toList();
for(QVariant& v : values.toList())
{
const QString value = v.toString();
addItem(value, value);
if(int index = aliasKeys.indexOf(value); index != -1)
{
addItem(Locale::instance->parse(aliasValues[index].toString()), value);
}
else
{
addItem(value, value);
}
if(m_property.toString() == value)
{
setCurrentIndex(count() - 1);
@ -185,7 +211,7 @@ void PropertyComboBox::updateValues()
}
}
break;
}
case ValueType::Invalid:
case ValueType::Boolean:
case ValueType::Float:

Datei anzeigen

@ -31,6 +31,12 @@
#include "unitproperty.hpp"
#include "vectorproperty.hpp"
#include <span>
#include <type_traits>
template<typename T>
concept StringOrStringView =
std::same_as<std::remove_cvref_t<T>, std::string> ||
std::same_as<std::remove_cvref_t<T>, std::string_view>;
struct Attributes
{
@ -58,16 +64,24 @@ struct Attributes
property.setAttribute(AttributeName::AliasValues, values);
}
template<class T>
static inline void addAliases(Property<T>& property, std::span<const T> keys, std::span<const std::string> values)
template<class T, StringOrStringView S>
static inline void addAliases(Property<T>& property, std::span<const T> keys, std::span<const S> values)
{
assert(keys.size() == values.size());
property.addAttribute(AttributeName::AliasKeys, keys);
property.addAttribute(AttributeName::AliasValues, values);
}
template<class T>
static inline void setAliases(Property<T>& property, std::span<const T> keys, std::span<const std::string> values)
template<StringOrStringView S>
static inline void addAliases(Property<std::string>& property, std::span<const S> keys, std::span<const S> values)
{
assert(keys.size() == values.size());
property.addAttribute(AttributeName::AliasKeys, keys);
property.addAttribute(AttributeName::AliasValues, values);
}
template<class T, StringOrStringView S>
static inline void setAliases(Property<T>& property, std::span<const T> keys, std::span<const S> values)
{
assert(keys.size() == values.size());
property.setAttribute(AttributeName::AliasKeys, keys);
@ -93,6 +107,12 @@ struct Attributes
item.addAttribute(AttributeName::ClassList, classList);
}
template<typename T>
static inline void addCustom(Property<T>& property, bool value)
{
property.addAttribute(AttributeName::Custom, value);
}
static inline void addDisplayName(InterfaceItem& item, std::string_view value)
{
item.addAttribute(AttributeName::DisplayName, value);
@ -340,6 +360,11 @@ struct Attributes
property.addAttribute(AttributeName::Values, values);
}
static inline void addValues(Property<std::string>& property, std::span<const std::string_view> values)
{
property.addAttribute(AttributeName::Values, values);
}
template<typename T, typename Unit, size_t N>
static inline void addValues(UnitProperty<T, Unit>& property, std::span<const T, N> values)
{

Datei anzeigen

@ -43,6 +43,7 @@ enum class AttributeName : uint16_t
Unit = 14,
Step = 15,
Help = 16,
Custom = 17, //!< Boolean: allow/disallow custom values, unset means auto detect.
};
#endif