forked from qt-creator/qt-creator
QmlDesigner: Modernize Enumeration
Save some allocations and return a view instead of a new value. If you want a value because you depend on a null terminated string you have to use toScope() and toName(). Task-number: QDS-10290 Change-Id: Ia87b8d559fc1fe9bb1992e61da0aa86c68d53535 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -15,76 +15,80 @@
|
||||
namespace QmlDesigner {
|
||||
|
||||
using EnumerationName = QByteArray;
|
||||
using EnumerationNameView = QByteArrayView;
|
||||
|
||||
class Enumeration
|
||||
{
|
||||
friend bool operator ==(const Enumeration &first, const Enumeration &second);
|
||||
friend bool operator <(const Enumeration &first, const Enumeration &second);
|
||||
friend QDataStream &operator>>(QDataStream &in, Enumeration &enumeration);
|
||||
|
||||
public:
|
||||
Enumeration() = default;
|
||||
Enumeration(const EnumerationName &enumerationName)
|
||||
: m_enumerationName(enumerationName)
|
||||
{
|
||||
}
|
||||
Enumeration(EnumerationName enumerationName)
|
||||
: m_enumerationName{std::move(enumerationName)}
|
||||
{}
|
||||
|
||||
Enumeration(const char *text)
|
||||
: m_enumerationName{text, static_cast<qsizetype>(std::strlen(text))}
|
||||
{}
|
||||
|
||||
Enumeration(const QString &enumerationName)
|
||||
: m_enumerationName(enumerationName.toUtf8())
|
||||
{}
|
||||
|
||||
Enumeration(const EnumerationName &scope, const EnumerationName &name)
|
||||
{
|
||||
}
|
||||
Enumeration(const QString &scope, const QString &name)
|
||||
{
|
||||
QString enumerationString = scope + QLatin1Char('.') + name;
|
||||
m_enumerationName = enumerationString.toUtf8();
|
||||
m_enumerationName.reserve(scope.size() + 1 + name.size());
|
||||
m_enumerationName.append(scope);
|
||||
m_enumerationName.append(1);
|
||||
m_enumerationName.append(name);
|
||||
}
|
||||
|
||||
EnumerationName scope() const
|
||||
EnumerationNameView scope() const
|
||||
{
|
||||
return m_enumerationName.split('.').constFirst();
|
||||
auto found = std::find(m_enumerationName.begin(), m_enumerationName.end(), '.');
|
||||
return {m_enumerationName.begin(), found};
|
||||
}
|
||||
EnumerationName name() const
|
||||
|
||||
EnumerationNameView toScope() const { return scope().toByteArray(); }
|
||||
|
||||
EnumerationNameView name() const
|
||||
{
|
||||
return m_enumerationName.split('.').last();
|
||||
auto found = std::find(m_enumerationName.begin(), m_enumerationName.end(), '.');
|
||||
if (found != m_enumerationName.end())
|
||||
return {std::next(found), m_enumerationName.end()};
|
||||
|
||||
return {m_enumerationName.end(), m_enumerationName.end()};
|
||||
}
|
||||
EnumerationName toEnumerationName() const
|
||||
|
||||
EnumerationName toName() const { return name().toByteArray(); }
|
||||
|
||||
EnumerationName toEnumerationName() const { return m_enumerationName; }
|
||||
|
||||
QString toString() const { return QString::fromUtf8(m_enumerationName); }
|
||||
QString nameToString() const { return QString::fromUtf8(name()); }
|
||||
|
||||
friend bool operator==(const Enumeration &first, const Enumeration &second)
|
||||
{
|
||||
return m_enumerationName;
|
||||
return first.m_enumerationName == second.m_enumerationName;
|
||||
}
|
||||
QString toString() const
|
||||
|
||||
friend bool operator<(const Enumeration &first, const Enumeration &second)
|
||||
{
|
||||
return QString::fromUtf8(m_enumerationName);
|
||||
return first.m_enumerationName < second.m_enumerationName;
|
||||
}
|
||||
QString nameToString() const
|
||||
|
||||
friend QDataStream &operator<<(QDataStream &out, const Enumeration &enumeration)
|
||||
{
|
||||
return QString::fromUtf8(name());
|
||||
return out << enumeration.m_enumerationName;
|
||||
}
|
||||
|
||||
friend QDataStream &operator>>(QDataStream &in, Enumeration &enumeration)
|
||||
{
|
||||
return in >> enumeration.m_enumerationName;
|
||||
}
|
||||
|
||||
private:
|
||||
EnumerationName m_enumerationName;
|
||||
};
|
||||
|
||||
inline QDataStream &operator<<(QDataStream &out, const Enumeration &enumeration){
|
||||
out << enumeration.toEnumerationName();
|
||||
return out;
|
||||
}
|
||||
|
||||
inline QDataStream &operator>>(QDataStream &in, Enumeration &enumeration)
|
||||
{
|
||||
in >> enumeration.m_enumerationName;
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
inline bool operator==(const Enumeration &first, const Enumeration &second)
|
||||
{
|
||||
return first.m_enumerationName == second.m_enumerationName;
|
||||
}
|
||||
|
||||
inline bool operator<(const Enumeration &first, const Enumeration &second)
|
||||
{
|
||||
return first.m_enumerationName < second.m_enumerationName;
|
||||
}
|
||||
|
||||
inline QDebug operator <<(QDebug debug, const Enumeration &enumeration)
|
||||
{
|
||||
debug.nospace() << "Enumeration("
|
||||
|
@@ -339,7 +339,7 @@ void PropertyEditorValue::resetValue()
|
||||
|
||||
void PropertyEditorValue::setEnumeration(const QString &scope, const QString &name)
|
||||
{
|
||||
Enumeration newEnumeration(scope, name);
|
||||
Enumeration newEnumeration(scope.toUtf8(), name.toUtf8());
|
||||
|
||||
setValueWithEmit(QVariant::fromValue(newEnumeration));
|
||||
}
|
||||
|
@@ -437,7 +437,7 @@ QVariant ObjectNodeInstance::convertEnumToValue(const QVariant &value, const Pro
|
||||
QVariant adjustedValue;
|
||||
Enumeration enumeration = value.value<Enumeration>();
|
||||
if (metaProperty.isValid() && metaProperty.isEnumType()) {
|
||||
adjustedValue = metaProperty.enumerator().keyToValue(enumeration.name());
|
||||
adjustedValue = metaProperty.enumerator().keyToValue(enumeration.toName());
|
||||
} else {
|
||||
QQmlExpression expression(context(), object(), enumeration.toString());
|
||||
adjustedValue = expression.evaluate();
|
||||
|
Reference in New Issue
Block a user