Skip to content

Commit

Permalink
client: PropertyObjectEdit update id on change
Browse files Browse the repository at this point in the history
TODO: m_property.objectId() still returns old value
  • Loading branch information
gfgit committed Aug 17, 2024
1 parent dc4a6ea commit 34a64a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
36 changes: 36 additions & 0 deletions client/src/widget/propertyobjectedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ PropertyObjectEdit::PropertyObjectEdit(ObjectProperty& property, QWidget *parent
m_changeButton{m_property.isWritable() && m_property.hasAttribute(AttributeName::ObjectList) ? new QToolButton(this) : nullptr},
m_editButton{new QToolButton(this)}
, m_editObjectRequestId{Connection::invalidRequestId}
, m_getObjectRequestId{Connection::invalidRequestId}
{
bool enabled = m_property.getAttributeBool(AttributeName::Enabled, true);
bool visible = m_property.getAttributeBool(AttributeName::Visible, true);
Expand Down Expand Up @@ -67,8 +68,12 @@ PropertyObjectEdit::PropertyObjectEdit(ObjectProperty& property, QWidget *parent
connect(&m_property, &ObjectProperty::valueChanged, this,
[this]()
{
disconnect(m_idChangedConnection);

m_editButton->setEnabled(m_property.hasObject());
m_lineEdit->setText(m_property.objectId());

connectToIdChanged();
});

QHBoxLayout* l = new QHBoxLayout();
Expand Down Expand Up @@ -115,10 +120,41 @@ PropertyObjectEdit::PropertyObjectEdit(ObjectProperty& property, QWidget *parent
l->addWidget(m_editButton);

setLayout(l);

connectToIdChanged();
}

PropertyObjectEdit::~PropertyObjectEdit()
{
if(m_editObjectRequestId != Connection::invalidRequestId)
m_property.object().connection()->cancelRequest(m_editObjectRequestId);
}

void PropertyObjectEdit::connectToIdChanged()
{
if(!m_property.hasObject())
return;

if(m_getObjectRequestId != Connection::invalidRequestId)
m_property.object().connection()->cancelRequest(m_getObjectRequestId);

m_getObjectRequestId = m_property.getObject(
[this](const ObjectPtr& object, std::optional<const Error> /*error*/)
{
m_getObjectRequestId = Connection::invalidRequestId;

if(!object)
return;

AbstractProperty *idProp = object->getProperty("id");
if(!idProp)
return;

m_idChangedConnection = connect(idProp, &AbstractProperty::valueChangedString,
this,
[this](const QString& id)
{
m_lineEdit->setText(id);
});
});
}
5 changes: 5 additions & 0 deletions client/src/widget/propertyobjectedit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class PropertyObjectEdit : public QWidget
QToolButton* m_changeButton;
QToolButton* m_editButton;
int m_editObjectRequestId;
int m_getObjectRequestId;
QMetaObject::Connection m_idChangedConnection;

protected:
void connectToIdChanged();

public:
explicit PropertyObjectEdit(ObjectProperty& property, QWidget* parent = nullptr);
Expand Down

0 comments on commit 34a64a8

Please sign in to comment.