BaseStringAspect: Provide a way to place a hook between UI and value

Change-Id: Ie6fd1c3215cba06dd67673b978af9f457f7fa9c8
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Tobias Hunger
2020-03-24 10:14:47 +01:00
parent fcd6384f4d
commit dbb640783c
2 changed files with 24 additions and 3 deletions

View File

@@ -107,6 +107,7 @@ public:
Utils::MacroExpanderProvider m_expanderProvider;
QPixmap m_labelPixmap;
Utils::FilePath m_baseFileName;
BaseStringAspect::ValueAcceptor m_valueAcceptor;
bool m_readOnly = false;
bool m_showToolTipOnLabel = false;
bool m_fileDialogOnly = false;
@@ -147,6 +148,11 @@ BaseStringAspect::BaseStringAspect()
BaseStringAspect::~BaseStringAspect() = default;
void BaseStringAspect::setValueAcceptor(BaseStringAspect::ValueAcceptor &&acceptor)
{
d->m_valueAcceptor = std::move(acceptor);
}
QString BaseStringAspect::value() const
{
return d->m_value;
@@ -155,10 +161,22 @@ QString BaseStringAspect::value() const
void BaseStringAspect::setValue(const QString &value)
{
const bool isSame = value == d->m_value;
d->m_value = value;
if (isSame)
return;
QString processedValue = value;
if (d->m_valueAcceptor) {
const Utils::optional<QString> tmp = d->m_valueAcceptor(d->m_value, value);
if (!tmp) {
update(); // Make sure the original value is retained in the UI
return;
}
processedValue = tmp.value();
}
d->m_value = processedValue;
update();
if (!isSame)
emit changed();
emit changed();
}
void BaseStringAspect::fromMap(const QVariantMap &map)

View File

@@ -115,6 +115,9 @@ public:
void addToLayout(LayoutBuilder &builder) override;
// Hook between UI and BaseStringAspect:
using ValueAcceptor = std::function<Utils::optional<QString>(const QString &, const QString &)>;
void setValueAcceptor(ValueAcceptor &&acceptor);
QString value() const;
void setValue(const QString &val);