Skip to content

Commit 56a0267

Browse files
committed
Split PTZListModel into its own source files
Code reorganization only. No functional changes. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
1 parent 8f95ec9 commit 56a0267

7 files changed

Lines changed: 349 additions & 316 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ target_sources(
9696
src/ptz.c
9797
src/ptz-controls.cpp
9898
src/ptz-device.cpp
99+
src/ptz-list-model.cpp
99100
src/settings.cpp
100101
src/ptz-visca.cpp
101102
src/ptz-visca-udp.cpp
@@ -107,6 +108,7 @@ target_sources(
107108
src/ptz.h
108109
src/ptz-controls.hpp
109110
src/ptz-device.hpp
111+
src/ptz-list-model.hpp
110112
src/settings.hpp
111113
src/ptz-visca.hpp
112114
src/ptz-visca-udp.hpp

src/ptz-controls.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "touch-control.hpp"
2323
#include "ui_ptz-controls.h"
2424
#include "ptz-controls.hpp"
25+
#include "ptz-list-model.hpp"
2526
#include "settings.hpp"
2627
#include "ptz.h"
2728

src/ptz-device.cpp

Lines changed: 1 addition & 261 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
#include <obs.hpp>
99
#include "ptz-device.hpp"
10-
#include "ptz-visca-udp.hpp"
11-
#include "ptz-visca-tcp.hpp"
12-
#include "ptz-onvif.hpp"
13-
#include "ptz-usb-cam.hpp"
10+
#include "ptz-list-model.hpp"
1411
#include "ptz.h"
1512
#include "protocol-helpers.hpp"
1613

@@ -19,215 +16,6 @@
1916
#include "ptz-pelco.hpp"
2017
#endif
2118

22-
PTZListModel ptzDeviceList;
23-
24-
static void source_rename_cb(void *data, calldata_t *cd)
25-
{
26-
auto ptzlm = static_cast<PTZListModel *>(data);
27-
ptzlm->renameDevice(calldata_string(cd, "new_name"), calldata_string(cd, "prev_name"));
28-
}
29-
30-
void PTZListModel::renameDevice(QString new_name, QString prev_name)
31-
{
32-
auto ptz = getDeviceByName(prev_name);
33-
if (ptz)
34-
ptz->setObjectName(new_name);
35-
}
36-
37-
PTZListModel::PTZListModel()
38-
{
39-
signal_handler_t *sh = obs_get_signal_handler();
40-
signal_handler_connect(sh, "source_rename", source_rename_cb, this);
41-
}
42-
43-
PTZListModel::~PTZListModel()
44-
{
45-
//signal_handler_t *sh = obs_get_signal_handler();
46-
//signal_handler_disconnect(sh, "source_rename", source_rename_cb, this);
47-
}
48-
49-
int PTZListModel::rowCount(const QModelIndex &parent) const
50-
{
51-
Q_UNUSED(parent);
52-
return (int)devices.size();
53-
}
54-
55-
Qt::ItemFlags PTZListModel::flags(const QModelIndex &index) const
56-
{
57-
if (!index.isValid())
58-
return Qt::ItemIsEnabled;
59-
return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
60-
}
61-
62-
QVariant PTZListModel::data(const QModelIndex &index, int role) const
63-
{
64-
if (!index.isValid())
65-
return QVariant();
66-
67-
auto ptz = getDevice(index);
68-
if (!ptz)
69-
return QVariant();
70-
71-
if (role == Qt::DisplayRole || role == Qt::EditRole)
72-
return ptz->objectName();
73-
74-
if (role == PTZListModel::DeviceIdRole)
75-
return ptz->getId();
76-
77-
if (role == PTZListModel::DescriptionRole)
78-
return ptz->description();
79-
80-
if (role == PTZListModel::IsLiveRole)
81-
return ptz->isLive();
82-
83-
if (role == PTZListModel::IsConnectedRole)
84-
return ptz->isConnected();
85-
86-
if (role == PTZListModel::IsLockedRole)
87-
return ptz->isLocked();
88-
89-
if (role == PTZListModel::SupportsSetHomeRole)
90-
return ptz->supportsSetHome();
91-
92-
return QVariant();
93-
}
94-
95-
bool PTZListModel::setData(const QModelIndex &index, const QVariant &value, int role)
96-
{
97-
auto ptz = getDevice(index);
98-
if (!ptz)
99-
return false;
100-
101-
if (role == PTZListModel::IsLockedRole && ptz->isLocked() != value.toBool()) {
102-
ptz->setLock(value.toBool());
103-
emit dataChanged(index, index, {role});
104-
return true;
105-
}
106-
107-
return false;
108-
}
109-
110-
void PTZListModel::do_reset()
111-
{
112-
beginResetModel();
113-
endResetModel();
114-
}
115-
116-
void PTZListModel::name_changed(PTZDevice *ptz)
117-
{
118-
auto index = indexFromDeviceId(ptz->id);
119-
if (index.isValid())
120-
emit dataChanged(index, index);
121-
}
122-
123-
void PTZListModel::onSceneChanged()
124-
{
125-
for (PTZDevice *ptz : devices)
126-
ptz->onSceneChanged();
127-
}
128-
129-
PTZDevice *PTZListModel::getDevice(const QModelIndex &index) const
130-
{
131-
if (!index.isValid() || index.model() != this)
132-
return nullptr;
133-
return devices.value(index.row());
134-
}
135-
136-
PTZDevice *PTZListModel::getDevice(uint32_t device_id) const
137-
{
138-
return devicesById.value(device_id, nullptr);
139-
}
140-
141-
PTZDevice *PTZListModel::getDeviceByName(const QString &name) const
142-
{
143-
for (PTZDevice *ptz : devices)
144-
if (name == ptz->objectName())
145-
return ptz;
146-
return nullptr;
147-
}
148-
149-
QStringList PTZListModel::getDeviceNames() const
150-
{
151-
QStringList names;
152-
for (const PTZDevice *ptz : devices)
153-
names.append(ptz->objectName());
154-
return names;
155-
}
156-
157-
/**
158-
* This variant of callDevice accepts a QModelIndex into the data model
159-
* to choose which device will receive the call
160-
*/
161-
bool PTZListModel::callDevice(const QModelIndex &index, const char *method, calldata_t *cd)
162-
{
163-
auto ptz = getDevice(index);
164-
return ptz ? proc_handler_call(ptz->handler, method, cd) : false;
165-
}
166-
167-
/**
168-
* This variant of callDevice extracts the device_id from calldata
169-
* before calling the device's proc handler.
170-
*/
171-
bool PTZListModel::callDevice(const char *method, calldata_t *cd)
172-
{
173-
auto ptz = getDevice(calldata_int(cd, "device_id"));
174-
return ptz ? proc_handler_call(ptz->handler, method, cd) : false;
175-
}
176-
177-
QModelIndex PTZListModel::indexFromDeviceId(uint32_t device_id)
178-
{
179-
auto ptz = getDevice(device_id);
180-
if (ptz)
181-
return index(devices.indexOf(ptz), 0);
182-
return QModelIndex();
183-
}
184-
185-
/**
186-
* Look up model index from the device name
187-
*/
188-
QModelIndex PTZListModel::indexFromName(const QString &name)
189-
{
190-
for (auto row = 0; row < devices.size(); row++) {
191-
if (name == devices.at(row)->objectName())
192-
return index(row, 0);
193-
}
194-
return QModelIndex();
195-
}
196-
197-
obs_data_array_t *PTZListModel::getConfigs()
198-
{
199-
obs_data_array_t *configs = obs_data_array_create();
200-
for (PTZDevice *ptz : devices)
201-
obs_data_array_push_back(configs, ptz->get_config());
202-
return configs;
203-
}
204-
205-
void PTZListModel::add(PTZDevice *ptz)
206-
{
207-
/* Assign a unique ID */
208-
uint32_t id = ptz->getId();
209-
while (devicesById.contains(id) || id == 0)
210-
id++;
211-
ptz->id = id;
212-
devices.append(ptz);
213-
devicesById[ptz->id] = ptz;
214-
do_reset();
215-
}
216-
217-
void PTZListModel::removeDevice(const QModelIndex &index)
218-
{
219-
PTZDevice *ptz = getDevice(index);
220-
if (ptz)
221-
delete ptz;
222-
}
223-
224-
void PTZListModel::remove(PTZDevice *ptz)
225-
{
226-
devicesById.remove(ptz->getId());
227-
devices.removeAll(ptz);
228-
do_reset();
229-
}
230-
23119
bool PTZPresetListModel::insertRows(int row, int count, const QModelIndex &parent)
23220
{
23321
if (row < 0 || row > rowCount())
@@ -282,54 +70,6 @@ bool PTZPresetListModel::moveRows(const QModelIndex &srcParent, int srcRow, int
28270
endMoveRows();
28371
return true;
28472
}
285-
286-
PTZDevice *PTZListModel::make_device(OBSData config)
287-
{
288-
PTZDevice *ptz = nullptr;
289-
std::string type = obs_data_get_string(config, "type");
290-
291-
#if defined(ENABLE_SERIALPORT)
292-
if (type == "pelco" || type == "pelco-p")
293-
ptz = new PTZPelco(config);
294-
if (type == "visca")
295-
ptz = new PTZViscaSerial(config);
296-
#endif /* ENABLE_SERIALPORT */
297-
if (type == "visca-over-ip")
298-
ptz = new PTZViscaOverIP(config);
299-
if (type == "visca-over-tcp")
300-
ptz = new PTZViscaOverTCP(config);
301-
#if defined(ENABLE_ONVIF)
302-
if (type == "onvif")
303-
ptz = new PTZOnvif(config);
304-
#endif /* ENABLE_ONVIF */
305-
#if defined(ENABLE_USB_CAM)
306-
if (type == "usb-cam")
307-
ptz = new PTZUSBCam(config);
308-
#endif /* ENABLE_USB_CAM */
309-
return ptz;
310-
}
311-
312-
void PTZListModel::delete_all()
313-
{
314-
// Devices remove themselves when deleted, so just loop until empty
315-
while (!devices.isEmpty())
316-
delete devices.first();
317-
}
318-
319-
void PTZListModel::preset_recall(uint32_t device_id, int preset_id)
320-
{
321-
PTZDevice *ptz = ptzDeviceList.getDevice(device_id);
322-
if (ptz)
323-
ptz->memory_recall(preset_id);
324-
}
325-
326-
void PTZListModel::preset_save(uint32_t device_id, int preset_id)
327-
{
328-
PTZDevice *ptz = ptzDeviceList.getDevice(device_id);
329-
if (ptz)
330-
ptz->memory_set(preset_id);
331-
}
332-
33373
int PTZPresetListModel::rowCount(const QModelIndex &parent) const
33474
{
33575
Q_UNUSED(parent);

src/ptz-device.hpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include <qt-wrappers.hpp>
1111
#include <memory>
1212
#include <QObject>
13-
#include <QHash>
14-
#include <QSet>
1513
#include <QStringListModel>
1614
#include <QtGlobal>
1715
#include <obs.hpp>
@@ -26,59 +24,6 @@
2624
if (this->protocol_trace) \
2725
ptz_log(LOG_DEBUG, format, ##__VA_ARGS__)
2826

29-
class PTZDevice;
30-
31-
class PTZListModel : public QAbstractListModel {
32-
Q_OBJECT
33-
34-
private:
35-
QList<PTZDevice *> devices;
36-
QHash<uint32_t, PTZDevice *> devicesById;
37-
38-
public:
39-
enum PTZListModelRole {
40-
DeviceIdRole = Qt::UserRole,
41-
DescriptionRole,
42-
IsLiveRole,
43-
IsConnectedRole,
44-
IsLockedRole,
45-
SupportsSetHomeRole,
46-
};
47-
48-
PTZListModel();
49-
~PTZListModel();
50-
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
51-
QVariant data(const QModelIndex &index, int role) const override;
52-
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
53-
void do_reset();
54-
void name_changed(PTZDevice *ptz);
55-
Qt::ItemFlags flags(const QModelIndex &index) const override;
56-
void onSceneChanged();
57-
58-
/* Data Model */
59-
PTZDevice *make_device(OBSData config);
60-
PTZDevice *getDevice(const QModelIndex &index) const;
61-
PTZDevice *getDevice(uint32_t device_id) const;
62-
PTZDevice *getDeviceByName(const QString &name) const;
63-
QStringList getDeviceNames() const;
64-
bool callDevice(const QModelIndex &index, const char *method, calldata_t *cd = nullptr);
65-
bool callDevice(const char *method, calldata_t *cd = nullptr);
66-
QModelIndex indexFromDeviceId(uint32_t device_id);
67-
QModelIndex indexFromName(const QString &name);
68-
void renameDevice(QString new_name, QString prev_name);
69-
obs_data_array_t *getConfigs();
70-
void removeDevice(const QModelIndex &index);
71-
void add(PTZDevice *ptz);
72-
void remove(PTZDevice *ptz);
73-
void delete_all();
74-
75-
public slots:
76-
void preset_recall(uint32_t device_id, int preset_id);
77-
void preset_save(uint32_t device_id, int preset_id);
78-
};
79-
80-
extern PTZListModel ptzDeviceList;
81-
8227
class PTZPresetListModel : public QAbstractListModel {
8328
Q_OBJECT
8429

0 commit comments

Comments
 (0)