Skip to content

Commit ab74fa2

Browse files
committed
Don't access PTZDevice* when scene changes
We don't want the UI directly accessing PTZDevice*, but when the active scene changed, the front end was using PTZListModel::getDeviceByName() which returns a PTZDevice* pointer. Solve this by implmenting PTZListModel::indexFromName() which allows the frontend to find a relevant entry without having to access the PTZDevice directly. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
1 parent 8a3a48f commit ab74fa2

3 files changed

Lines changed: 20 additions & 21 deletions

File tree

src/ptz-controls.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,18 @@ void PTZControls::OBSFrontendEvent(enum obs_frontend_event event)
103103
if (!scene)
104104
return;
105105

106-
struct active_src_cb_data {
107-
PTZDevice *ptz;
106+
auto active_src_cb = [](obs_source_t *, obs_source_t *child, void *context_data) {
107+
QModelIndex *index = static_cast<QModelIndex *>(context_data);
108+
if (!index->isValid())
109+
*index = ptzDeviceList.indexFromName(obs_source_get_name(child));
108110
};
109-
auto active_src_cb = [](obs_source_t *parent, obs_source_t *child, void *context_data) {
110-
Q_UNUSED(parent);
111-
struct active_src_cb_data *context = static_cast<struct active_src_cb_data *>(context_data);
112-
if (!context->ptz)
113-
context->ptz = ptzDeviceList.getDeviceByName(obs_source_get_name(child));
114-
};
115-
struct active_src_cb_data cb_data;
116-
cb_data.ptz = ptzDeviceList.getDeviceByName(obs_source_get_name(scene));
117-
if (!cb_data.ptz)
118-
obs_source_enum_active_sources(scene, active_src_cb, &cb_data);
111+
QModelIndex cb_index = ptzDeviceList.indexFromName(obs_source_get_name(scene));
112+
if (!cb_index.isValid())
113+
obs_source_enum_active_sources(scene, active_src_cb, &cb_index);
119114
obs_source_release(scene);
120115

121-
if (cb_data.ptz)
122-
setCurrent(cb_data.ptz->getId());
116+
if (cb_index.isValid())
117+
ui->cameraList->setCurrentIndex(cb_index);
123118
}
124119

125120
/* Helper funciton for changing currently selected OBS scene */
@@ -855,13 +850,6 @@ void PTZControls::on_focusButton_onetouch_clicked()
855850
callCamera("focus_onetouch");
856851
}
857852

858-
void PTZControls::setCurrent(uint32_t device_id)
859-
{
860-
if (device_id == ptzDeviceList.getDeviceId(ui->cameraList->currentIndex()))
861-
return;
862-
ui->cameraList->setCurrentIndex(ptzDeviceList.indexFromDeviceId(device_id));
863-
}
864-
865853
void PTZControls::setAutofocusEnabled(bool autofocus_on)
866854
{
867855
ui->focusButton_auto->setChecked(autofocus_on);

src/ptz-device.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ QModelIndex PTZListModel::indexFromDeviceId(uint32_t device_id)
189189
return index(row, 0);
190190
}
191191

192+
QModelIndex PTZListModel::indexFromName(const QString &name)
193+
{
194+
for (auto key : devices.keys()) {
195+
auto ptz = devices.value(key);
196+
if (name == ptz->objectName())
197+
return index(devices.keys().indexOf(key), 0);
198+
}
199+
return QModelIndex();
200+
}
201+
192202
obs_data_array_t *PTZListModel::getConfigs()
193203
{
194204
obs_data_array_t *configs = obs_data_array_create();

src/ptz-device.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class PTZListModel : public QAbstractListModel {
5757
bool callDevice(const QModelIndex &index, const char *method, calldata *cd);
5858
bool callDevice(const char *method, calldata *cd);
5959
QModelIndex indexFromDeviceId(uint32_t device_id);
60+
QModelIndex indexFromName(const QString &name);
6061
void renameDevice(QString new_name, QString prev_name);
6162
obs_data_array_t *getConfigs();
6263
void removeDevice(const QModelIndex &index);

0 commit comments

Comments
 (0)