Skip to content

Commit

Permalink
Fix tank and environment input object cleanup in mock mode
Browse files Browse the repository at this point in the history
Set the deviceInstance property value directly, instead of calling
_deviceInstance.setValue(), so that BaseDevice::setDeviceInstance() is
called and BaseDevice::validChanged is emitted when the device
instance is set to -1. This ensures the clean up handlers in Tank.qml
and EnvironmentInput.qml are triggered.

Also clean up Tank.qml to use a single signal handler to check whether
the tank needs to be added/removed to/from the tank models.
  • Loading branch information
blammit committed Jul 26, 2024
1 parent c53a528 commit 2d2bd56
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
10 changes: 4 additions & 6 deletions data/common/Tank.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ Device {
Component.onCompleted: Qt.callLater(tank._updateMeasurements)
}

onValidChanged: Qt.callLater(tank._updateModel)
onTypeChanged: Qt.callLater(tank._updateModel)

function _updateModel() {
if (valid && type >= 0) {
if (_tankModel && _tankModel.type !== type) {
readonly property bool _deviceAndTypeValid: valid && type >= 0
on_DeviceAndTypeValidChanged: {
if (_deviceAndTypeValid) {
if (_tankModel) {
_tankModel.removeDevice(tank.serviceUid)
}
_tankModel = Global.tanks.tankModel(type)
Expand Down
4 changes: 2 additions & 2 deletions data/mock/EnvironmentInputsImpl.qml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Item {
const hasCreatedObjects = _createdObjects.length > 0
while (_createdObjects.length > 1) {
let obj = _createdObjects.pop()
obj._deviceInstance.setValue(-1)
obj.deviceInstance = -1
obj.destroy()
}

Expand All @@ -97,7 +97,7 @@ Item {

if (hasCreatedObjects) {
let lastObject = _createdObjects.shift()
lastObject._deviceInstance.setValue(-1)
lastObject.deviceInstance = -1
lastObject.destroy()
}
}
Expand Down
8 changes: 4 additions & 4 deletions data/mock/TanksImpl.qml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ QtObject {
const hasCreatedObjects = _createdObjects.length > 0
while (_createdObjects.length > 1) {
let obj = _createdObjects.pop()
obj._deviceInstance.setValue(-1)
obj.deviceInstance = -1
obj.destroy()
}

Expand All @@ -103,7 +103,7 @@ QtObject {

if (hasCreatedObjects) {
let lastObject = _createdObjects.shift()
lastObject._deviceInstance.setValue(-1)
lastObject.deviceInstance = -1
lastObject.destroy()
}
}
Expand Down Expand Up @@ -132,7 +132,7 @@ QtObject {
} else {
// remove a tank
const index = Math.floor(Math.random() * _createdObjects.length)
_createdObjects[index]._deviceInstance.setValue(-1) // causes tank to remove itself from model
_createdObjects[index].deviceInstance = -1 // causes tank to remove itself from model
_createdObjects.splice(index, 1)
}
}
Expand All @@ -155,7 +155,7 @@ QtObject {
root.addTank(tankProperties)
} else {
for (var i = 0; i < _createdObjects.length; ++i) {
_createdObjects[i]._deviceInstance.setValue(-1) // causes tank to remove itself from model
_createdObjects[i].deviceInstance = -1 // causes tank to remove itself from model
}

_createdObjects = []
Expand Down

0 comments on commit 2d2bd56

Please sign in to comment.