Skip to content

Commit

Permalink
Gui: propertyeditor: use std::string setPropertyValue
Browse files Browse the repository at this point in the history
Avoid std::string->QString->std::string where possible.
  • Loading branch information
3x380V authored and wwmayer committed Nov 27, 2024
1 parent a80c87c commit 6860aab
Showing 1 changed file with 56 additions and 100 deletions.
156 changes: 56 additions & 100 deletions src/Gui/propertyeditor/PropertyItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#ifndef _PreComp_
#include <algorithm>
#include <iomanip>
#include <QApplication>
#include <QComboBox>
#include <QFontDatabase>
Expand Down Expand Up @@ -842,14 +843,9 @@ QVariant PropertyStringItem::value(const App::Property* prop) const

void PropertyStringItem::setValue(const QVariant& value)
{
if (!hasExpression()) {
if (!value.canConvert<QString>()) {
return;
}
QString val = value.toString();
val = QString::fromUtf8(Base::InterpreterSingleton::strToPython(val.toUtf8()).c_str());
QString data = QString::fromLatin1("\"%1\"").arg(val);
setPropertyValue(data);
if (!hasExpression() && value.canConvert<QString>()) {
std::string val = Base::InterpreterSingleton::strToPython(value.toString().toStdString());
setPropertyValue(Base::Tools::quoted(val));
}
}

Expand Down Expand Up @@ -896,13 +892,9 @@ QVariant PropertyFontItem::value(const App::Property* prop) const

void PropertyFontItem::setValue(const QVariant& value)
{
if (hasExpression() || !value.canConvert<QString>()) {
return;
if (!hasExpression() && value.canConvert<QString>()) {
setPropertyValue(Base::Tools::quoted(value.toString().toStdString()));
}

QString val = value.toString();
QString data = QString::fromLatin1("\"%1\"").arg(val);
setPropertyValue(data);
}

QWidget* PropertyFontItem::createEditor(QWidget* parent, const std::function<void()>& method) const
Expand Down Expand Up @@ -966,13 +958,9 @@ QVariant PropertyIntegerItem::value(const App::Property* prop) const
void PropertyIntegerItem::setValue(const QVariant& value)
{
// if the item has an expression it issues the python code
if (!hasExpression()) {
if (!value.canConvert<int>()) {
return;
}
if (!hasExpression() && value.canConvert<int>()) {
int val = value.toInt();
QString data = QString::fromLatin1("%1").arg(val);
setPropertyValue(data);
setPropertyValue(std::to_string(val));
}
}

Expand Down Expand Up @@ -1035,13 +1023,9 @@ QVariant PropertyIntegerConstraintItem::value(const App::Property* prop) const
void PropertyIntegerConstraintItem::setValue(const QVariant& value)
{
// if the item has an expression it issues the python code
if (!hasExpression()) {
if (!value.canConvert<int>()) {
return;
}
if (!hasExpression() && value.canConvert<int>()) {
int val = value.toInt();
QString data = QString::fromLatin1("%1").arg(val);
setPropertyValue(data);
setPropertyValue(std::to_string(val));
}
}

Expand Down Expand Up @@ -1133,13 +1117,10 @@ QVariant PropertyFloatItem::value(const App::Property* prop) const
void PropertyFloatItem::setValue(const QVariant& value)
{
// if the item has an expression it issues the python code
if (!hasExpression()) {
if (!value.canConvert<double>()) {
return;
}
double val = value.toDouble();
QString data = QString::fromLatin1("%1").arg(val, 0, 'g', highPrec);
setPropertyValue(data);
if (!hasExpression() && value.canConvert<double>()) {
std::ostringstream ss;
ss << std::setprecision(highPrec) << value.toDouble();
setPropertyValue(ss.str());
}
}

Expand Down Expand Up @@ -1202,12 +1183,8 @@ QVariant PropertyUnitItem::value(const App::Property* prop) const
void PropertyUnitItem::setValue(const QVariant& value)
{
// if the item has an expression it handles the python code
if (!hasExpression()) {
if (!value.canConvert<Base::Quantity>()) {
return;
}
if (!hasExpression() && value.canConvert<Base::Quantity>()) {
const Base::Quantity& val = value.value<Base::Quantity>();

Base::QuantityFormat format(Base::QuantityFormat::Default, highPrec);
QString unit = Base::UnitsApi::toString(val, format);
setPropertyValue(unit);
Expand Down Expand Up @@ -1305,13 +1282,10 @@ QVariant PropertyFloatConstraintItem::value(const App::Property* prop) const
void PropertyFloatConstraintItem::setValue(const QVariant& value)
{
// if the item has an expression it issues the python code
if (!hasExpression()) {
if (!value.canConvert<double>()) {
return;
}
double val = value.toDouble();
QString data = QString::fromLatin1("%1").arg(val, 0, 'g', highPrec);
setPropertyValue(data);
if (!hasExpression() && value.canConvert<double>()) {
std::ostringstream ss;
ss << std::setprecision(highPrec) << value.toDouble();
setPropertyValue(ss.str());
}
}

Expand Down Expand Up @@ -1403,12 +1377,10 @@ QVariant PropertyBoolItem::value(const App::Property* prop) const

void PropertyBoolItem::setValue(const QVariant& value)
{
if (hasExpression() || !value.canConvert<bool>()) {
return;
if (!hasExpression() && value.canConvert<bool>()) {
std::string val = value.toBool() ? "True" : "False";
setPropertyValue(val);
}
bool val = value.toBool();
QString data = (val ? QLatin1String("True") : QLatin1String("False"));
setPropertyValue(data);
}

QWidget* PropertyBoolItem::createEditor(QWidget* parent, const std::function<void()>& method) const
Expand Down Expand Up @@ -3012,28 +2984,26 @@ void PropertyEnumItem::setValue(const QVariant& value)
return;
}

QString data;
std::ostringstream ss;

if (value.userType() == QMetaType::QStringList) {
QStringList values = value.toStringList();
QTextStream str(&data);
str << "[";
ss << "[";
for (const auto& it : values) {
QString text(it);
text.replace(QString::fromUtf8("'"), QString::fromUtf8("\\'"));

std::string pystr = Base::Tools::escapedUnicodeFromUtf8(text.toUtf8());
pystr = Base::InterpreterSingleton::strToPython(pystr.c_str());
str << "u\"" << pystr.c_str() << "\", ";
std::string str = Base::Tools::escapedUnicodeFromUtf8(text.toUtf8());
str = Base::InterpreterSingleton::strToPython(str);
ss << "u\"" << str << "\", ";
}
str << "]";
setPropertyValue(data);
ss << "]";
setPropertyValue(ss.str());
}
else if (value.canConvert<QString>()) {
QByteArray val = value.toString().toUtf8();
std::string str = Base::Tools::escapedUnicodeFromUtf8(val);
data = QString::fromLatin1("u\"%1\"").arg(QString::fromStdString(str));
setPropertyValue(data);
std::string str = Base::Tools::escapedUnicodeFromUtf8(value.toString().toUtf8());
ss << "u\"" << str << "\"";
setPropertyValue(ss.str());
}
}

Expand Down Expand Up @@ -3282,22 +3252,17 @@ void PropertyStringListItem::setValue(const QVariant& value)
if (hasExpression() || !value.canConvert<QStringList>()) {
return;
}
QStringList values = value.toStringList();
QString data;
QTextStream str(&data);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
str.setCodec("UTF-8");
#endif

str << "[";
QStringList values = value.toStringList();
std::ostringstream ss;
ss << "[";
for (const auto& it : values) {
QString text(it);
std::string pystr = Base::InterpreterSingleton::strToPython(text.toUtf8().constData());
str << "\"" << QString::fromUtf8(pystr.c_str()) << "\", ";
ss << "\"" << pystr << "\", ";
}
str << "]";

setPropertyValue(data);
ss << "]";
setPropertyValue(ss.str());
}

// ---------------------------------------------------------------
Expand Down Expand Up @@ -3363,17 +3328,18 @@ void PropertyFloatListItem::setValue(const QVariant& value)
if (hasExpression() || !value.canConvert<QStringList>()) {
return;
}

QStringList values = value.toStringList();
QString data;
QTextStream str(&data);
str << "[";
for (const auto& it : values) {
str << it << ",";
for (auto it = values.begin(); it != values.end(); ++it) {
if (it != values.begin()) {
str << ",";
}
str << *it;
}
str << "]";
if (data == QString::fromUtf8("[,]")) {
data = QString::fromUtf8("[]");
}
setPropertyValue(data);
}

Expand Down Expand Up @@ -3441,17 +3407,18 @@ void PropertyIntegerListItem::setValue(const QVariant& value)
if (hasExpression() || !value.canConvert<QStringList>()) {
return;
}

QStringList values = value.toStringList();
QString data;
QTextStream str(&data);
str << "[";
for (const auto& value : values) {
str << value << ",";
for (auto it = values.begin(); it != values.end(); ++it) {
if (it != values.begin()) {
str << ",";
}
str << *it;
}
str << "]";
if (data == QString::fromUtf8("[,]")) {
data = QString::fromUtf8("[]");
}
setPropertyValue(data);
}

Expand Down Expand Up @@ -4426,12 +4393,9 @@ QVariant PropertyFileItem::value(const App::Property* prop) const

void PropertyFileItem::setValue(const QVariant& value)
{
if (hasExpression() || !value.canConvert<QString>()) {
return;
if (!hasExpression() && value.canConvert<QString>()) {
setPropertyValue(Base::Tools::quoted(value.toString().toStdString()));
}
QString val = value.toString();
QString data = QString::fromLatin1("\"%1\"").arg(val);
setPropertyValue(data);
}

QVariant PropertyFileItem::toolTip(const App::Property* prop) const
Expand Down Expand Up @@ -4483,13 +4447,9 @@ QVariant PropertyPathItem::value(const App::Property* prop) const

void PropertyPathItem::setValue(const QVariant& value)
{
if (hasExpression() || !value.canConvert<QString>()) {
return;
if (!hasExpression() && value.canConvert<QString>()) {
setPropertyValue(Base::Tools::quoted(value.toString().toStdString()));
}

QString val = value.toString();
QString data = QString::fromLatin1("\"%1\"").arg(val);
setPropertyValue(data);
}

QVariant PropertyPathItem::toolTip(const App::Property* prop) const
Expand Down Expand Up @@ -4535,13 +4495,9 @@ QVariant PropertyTransientFileItem::value(const App::Property* prop) const

void PropertyTransientFileItem::setValue(const QVariant& value)
{
if (hasExpression() || !value.canConvert<QString>()) {
return;
if (!hasExpression() && value.canConvert<QString>()) {
setPropertyValue(Base::Tools::quoted(value.toString().toStdString()));
}

QString val = value.toString();
QString data = QString::fromLatin1("\"%1\"").arg(val);
setPropertyValue(data);
}

QVariant PropertyTransientFileItem::toolTip(const App::Property* prop) const
Expand Down

0 comments on commit 6860aab

Please sign in to comment.