Possibly fix crash when room is cleared on event delegate

pull/1614/head
Nicolas Werner 1 year ago
parent 7982c2bd75
commit 81cf29f924
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
  1. 109
      src/timeline/EventDelegateChooser.cpp

@ -162,48 +162,50 @@ EventDelegateChooser::DelegateIncubator::setInitialState(QObject *obj)
// setInitialProperties(rolesToSet); // setInitialProperties(rolesToSet);
auto update = auto update = [this, obj, roleToPropIdx = std::move(roleToPropIdx)](
[this, obj, roleToPropIdx = std::move(roleToPropIdx)](const QList<int> &changedRoles) { const QList<int> &changedRoles, TimelineModel *room) {
if (changedRoles.empty() || changedRoles.contains(TimelineModel::Roles::Type)) { if (!room)
int type = chooser.room_ return;
->dataById(currentId,
TimelineModel::Roles::Type, if (changedRoles.empty() || changedRoles.contains(TimelineModel::Roles::Type)) {
forReply ? chooser.eventId_ : QString()) int type = room
.toInt(); ->dataById(currentId,
if (type != oldType) { TimelineModel::Roles::Type,
// nhlog::ui()->debug("Type changed!"); forReply ? chooser.eventId_ : QString())
reset(currentId); .toInt();
return; if (type != oldType) {
} // nhlog::ui()->debug("Type changed!");
} reset(currentId);
return;
std::vector<QModelRoleData> rolesToRequest; }
}
if (changedRoles.empty()) {
for (const auto role : std::vector<QModelRoleData> rolesToRequest;
std::ranges::subrange(roleToPropIdx.keyBegin(), roleToPropIdx.keyEnd()))
rolesToRequest.emplace_back(role); if (changedRoles.empty()) {
} else { for (const auto role :
for (auto role : changedRoles) { std::ranges::subrange(roleToPropIdx.keyBegin(), roleToPropIdx.keyEnd()))
if (roleToPropIdx.contains(role)) { rolesToRequest.emplace_back(role);
rolesToRequest.emplace_back(role); } else {
} for (auto role : changedRoles) {
} if (roleToPropIdx.contains(role)) {
} rolesToRequest.emplace_back(role);
}
if (rolesToRequest.empty()) }
return; }
auto mo = obj->metaObject(); if (rolesToRequest.empty())
chooser.room_->multiData( return;
currentId, forReply ? chooser.eventId_ : QString(), rolesToRequest);
auto mo = obj->metaObject();
Qt::beginPropertyUpdateGroup(); room->multiData(currentId, forReply ? chooser.eventId_ : QString(), rolesToRequest);
for (const auto &role : rolesToRequest) {
mo->property(roleToPropIdx[role.role()]).write(obj, role.data()); Qt::beginPropertyUpdateGroup();
} for (const auto &role : rolesToRequest) {
Qt::endPropertyUpdateGroup(); mo->property(roleToPropIdx[role.role()]).write(obj, role.data());
}; }
Qt::endPropertyUpdateGroup();
};
if (!forReply) { if (!forReply) {
auto row = chooser.room_->idToIndex(currentId); auto row = chooser.room_->idToIndex(currentId);
@ -211,18 +213,27 @@ EventDelegateChooser::DelegateIncubator::setInitialState(QObject *obj)
chooser.room_, chooser.room_,
&QAbstractItemModel::dataChanged, &QAbstractItemModel::dataChanged,
obj, obj,
[row, update](const QModelIndex &topLeft, [row, update, room = chooser.room_](const QModelIndex &topLeft,
const QModelIndex &bottomRight, const QModelIndex &bottomRight,
const QList<int> &changedRoles) { const QList<int> &changedRoles) {
if (row < topLeft.row() || row > bottomRight.row()) if (row < topLeft.row() || row > bottomRight.row())
return; return;
update(changedRoles); update(changedRoles, room);
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
connect(&this->chooser, &EventDelegateChooser::destroyed, obj, [connection]() { connect(
QObject::disconnect(connection); &this->chooser,
}); &EventDelegateChooser::destroyed,
obj,
[connection]() { QObject::disconnect(connection); },
Qt::SingleShotConnection);
connect(
&this->chooser,
&EventDelegateChooser::roomChanged,
obj,
[connection]() { QObject::disconnect(connection); },
Qt::SingleShotConnection);
} }
} }

Loading…
Cancel
Save