|
7 | 7 |
|
8 | 8 | #include <obs.hpp> |
9 | 9 | #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" |
14 | 11 | #include "ptz.h" |
15 | 12 | #include "protocol-helpers.hpp" |
16 | 13 |
|
|
19 | 16 | #include "ptz-pelco.hpp" |
20 | 17 | #endif |
21 | 18 |
|
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 | | - |
231 | 19 | bool PTZPresetListModel::insertRows(int row, int count, const QModelIndex &parent) |
232 | 20 | { |
233 | 21 | if (row < 0 || row > rowCount()) |
@@ -282,54 +70,6 @@ bool PTZPresetListModel::moveRows(const QModelIndex &srcParent, int srcRow, int |
282 | 70 | endMoveRows(); |
283 | 71 | return true; |
284 | 72 | } |
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 | | - |
333 | 73 | int PTZPresetListModel::rowCount(const QModelIndex &parent) const |
334 | 74 | { |
335 | 75 | Q_UNUSED(parent); |
|
0 commit comments