|
1 | | -/* Pan Tilt Zoom device factory |
| 1 | +/* Pan Tilt Zoom Controls - PTZDevice base object |
2 | 2 | * |
3 | | - * Copyright 2020 Grant Likely <grant.likely@secretlab.ca> |
| 3 | + * Copyright 2020-2026 Grant Likely <grant.likely@secretlab.ca> |
4 | 4 | * |
5 | 5 | * SPDX-License-Identifier: GPLv2 |
6 | 6 | */ |
7 | 7 |
|
8 | 8 | #include <obs.hpp> |
9 | 9 | #include "ptz-device.hpp" |
10 | 10 | #include "ptz-list-model.hpp" |
| 11 | +#include "ptz-preset-model.hpp" |
11 | 12 | #include "ptz.h" |
12 | 13 | #include "protocol-helpers.hpp" |
13 | 14 |
|
14 | | -#if defined(ENABLE_SERIALPORT) |
15 | | -#include "ptz-visca-uart.hpp" |
16 | | -#include "ptz-pelco.hpp" |
17 | | -#endif |
18 | | - |
19 | | -bool PTZPresetListModel::insertRows(int row, int count, const QModelIndex &parent) |
20 | | -{ |
21 | | - if (row < 0 || row > rowCount()) |
22 | | - return false; |
23 | | - size_t curr_id = 0; |
24 | | - QList<size_t> ids; |
25 | | - for (int i = 0; i < count; i++) { |
26 | | - while (m_presets.contains(curr_id)) |
27 | | - curr_id++; |
28 | | - if (curr_id >= m_maxPresets) |
29 | | - return false; |
30 | | - ids.append(curr_id); |
31 | | - } |
32 | | - |
33 | | - beginInsertRows(parent, row, count); |
34 | | - for (auto id : ids) { |
35 | | - QVariantMap map; |
36 | | - map["id"] = (uint)id; |
37 | | - m_presets[id] = map; |
38 | | - m_displayOrder.insert(row++, id); |
39 | | - } |
40 | | - endInsertRows(); |
41 | | - return true; |
42 | | -} |
43 | | - |
44 | | -bool PTZPresetListModel::removeRows(int row, int count, const QModelIndex &parent) |
45 | | -{ |
46 | | - if (row < 0 || row >= rowCount()) |
47 | | - return false; |
48 | | - beginRemoveRows(parent, row, count); |
49 | | - QList<size_t> ids = m_displayOrder.mid(row, count); |
50 | | - ; |
51 | | - for (auto id : ids) { |
52 | | - m_displayOrder.removeAt(row); |
53 | | - m_presets.remove(id); |
54 | | - } |
55 | | - endRemoveRows(); |
56 | | - return true; |
57 | | -} |
58 | | - |
59 | | -bool PTZPresetListModel::moveRows(const QModelIndex &srcParent, int srcRow, int count, const QModelIndex &destParent, |
60 | | - int destChild) |
61 | | -{ |
62 | | - if (srcRow < 0 || srcRow >= rowCount() || destChild < 0 || destChild > rowCount() || count != 1) |
63 | | - return false; |
64 | | - |
65 | | - if (!beginMoveRows(srcParent, srcRow, srcRow + count - 1, destParent, destChild)) |
66 | | - return false; |
67 | | - if (srcRow < destChild) |
68 | | - destChild--; |
69 | | - m_displayOrder.move(srcRow, destChild); |
70 | | - endMoveRows(); |
71 | | - return true; |
72 | | -} |
73 | | -int PTZPresetListModel::rowCount(const QModelIndex &parent) const |
74 | | -{ |
75 | | - Q_UNUSED(parent); |
76 | | - return (int)m_displayOrder.size(); |
77 | | -} |
78 | | - |
79 | | -int PTZPresetListModel::getPresetId(const QModelIndex &index) const |
80 | | -{ |
81 | | - if (!index.isValid()) |
82 | | - return -1; |
83 | | - |
84 | | - if (index.row() >= m_displayOrder.size()) { |
85 | | - blog(LOG_ERROR, "ERROR: Preset Row %i is not valid", index.row()); |
86 | | - return -1; |
87 | | - } |
88 | | - return (int)m_displayOrder[index.row()]; |
89 | | -} |
90 | | - |
91 | | -QVariant PTZPresetListModel::data(const QModelIndex &index, int role) const |
92 | | -{ |
93 | | - auto id = getPresetId(index); |
94 | | - if (id < 0) |
95 | | - return QVariant(); |
96 | | - auto preset = m_presets[id]; |
97 | | - if (role == Qt::DisplayRole) { |
98 | | - QString name = preset["name"].toString(); |
99 | | - return (name != "") ? name : QString(obs_module_text("PTZ.PresetNum")).arg(id); |
100 | | - } |
101 | | - if (role == Qt::ToolTipRole) { |
102 | | - auto token = preset["token"].toString(); |
103 | | - if (token != "") |
104 | | - return QString(obs_module_text("PTZ.Preset.Tooltip")).arg("'" + token + "'"); |
105 | | - return QString(obs_module_text("PTZ.Preset.Tooltip")).arg(id); |
106 | | - } |
107 | | - if (role == Qt::EditRole) |
108 | | - return preset["name"].toString(); |
109 | | - if (role == Qt::UserRole) |
110 | | - return id; |
111 | | - if (role == Qt::SizeHintRole) |
112 | | - return QSize(0, 20); |
113 | | - |
114 | | - return QVariant(); |
115 | | -} |
116 | | - |
117 | | -Qt::ItemFlags PTZPresetListModel::flags(const QModelIndex &index) const |
118 | | -{ |
119 | | - auto f = QAbstractListModel::flags(index); |
120 | | - if (index.column() == 0) |
121 | | - f |= Qt::ItemIsEditable; |
122 | | - return f; |
123 | | -} |
124 | | - |
125 | | -void PTZPresetListModel::sanitize(size_t id) |
126 | | -{ |
127 | | - if (!m_presets.contains(id)) |
128 | | - return; |
129 | | - if (!m_displayOrder.contains(id)) |
130 | | - m_displayOrder.append(id); |
131 | | - QVariantMap &preset = m_presets[id]; |
132 | | - QString name = preset["name"].toString(); |
133 | | - if (name == "" || name == QString(obs_module_text("PTZ.PresetNum")).arg(id)) |
134 | | - preset.remove("name"); |
135 | | -} |
136 | | - |
137 | | -bool PTZPresetListModel::setData(const QModelIndex &index, const QVariant &value, int role) |
138 | | -{ |
139 | | - auto id = getPresetId(index); |
140 | | - if (id < 0) |
141 | | - return false; |
142 | | - |
143 | | - if (role == Qt::EditRole) { |
144 | | - QVariantMap &preset = m_presets[id]; |
145 | | - preset["name"] = value.toString(); |
146 | | - sanitize(id); |
147 | | - emit dataChanged(index, index); |
148 | | - return true; |
149 | | - } |
150 | | - return false; |
151 | | -} |
152 | | - |
153 | | -/* Insert a new preset and return the ID */ |
154 | | -int PTZPresetListModel::newPreset() |
155 | | -{ |
156 | | - size_t id = 0; |
157 | | - while (m_presets.contains(id)) |
158 | | - id++; |
159 | | - if (id >= m_maxPresets) |
160 | | - return -1; |
161 | | - |
162 | | - QVariantMap map; |
163 | | - map["id"] = (uint)id; |
164 | | - beginInsertRows(QModelIndex(), rowCount(), 1); |
165 | | - m_presets[id] = map; |
166 | | - m_displayOrder.append(id); |
167 | | - endInsertRows(); |
168 | | - return (int)id; |
169 | | -} |
170 | | - |
171 | | -QVariant PTZPresetListModel::presetProperty(size_t id, QString key) |
172 | | -{ |
173 | | - /* Safe to derefernce unconditionally here. Both levels will return |
174 | | - * default empty values without segfaulting */ |
175 | | - return m_presets[id][key]; |
176 | | -} |
177 | | - |
178 | | -bool PTZPresetListModel::updatePreset(size_t id, const QVariantMap &map) |
179 | | -{ |
180 | | - if (!m_presets.contains(id)) |
181 | | - return false; |
182 | | - m_presets[id].insert(map); |
183 | | - return true; |
184 | | -} |
185 | | - |
186 | | -int PTZPresetListModel::find(QString key, QVariant value) |
187 | | -{ |
188 | | - /* Search for the matching key/value pair in all presets. |
189 | | - * This is an O(N) operation */ |
190 | | - auto end = m_presets.cend(); |
191 | | - for (auto i = m_presets.cbegin(); i != end; i++) { |
192 | | - if (i.value()[key] == value) |
193 | | - return (int)i.key(); |
194 | | - } |
195 | | - return -1; |
196 | | -} |
197 | | - |
198 | | -void PTZPresetListModel::loadPresets(OBSDataArray preset_array) |
199 | | -{ |
200 | | - if (!preset_array) |
201 | | - return; |
202 | | - beginResetModel(); |
203 | | - m_presets.clear(); |
204 | | - m_displayOrder.clear(); |
205 | | - for (size_t i = 0; i < obs_data_array_count(preset_array); i++) { |
206 | | - OBSDataAutoRelease item = obs_data_array_item(preset_array, i); |
207 | | - auto id = obs_data_get_int(item, "id"); |
208 | | - if (m_displayOrder.contains(id)) |
209 | | - continue; |
210 | | - QVariantMap preset = OBSDataToVariantMap(item.Get()); |
211 | | - m_presets[id] = preset; |
212 | | - sanitize(id); |
213 | | - } |
214 | | - endResetModel(); |
215 | | -} |
216 | | - |
217 | | -OBSDataArray PTZPresetListModel::savePresets() const |
218 | | -{ |
219 | | - OBSDataArrayAutoRelease preset_array = obs_data_array_create(); |
220 | | - for (auto id : m_displayOrder) { |
221 | | - OBSDataAutoRelease data = variantMapToOBSData(m_presets[id]); |
222 | | - obs_data_set_int(data, "id", id); |
223 | | - obs_data_array_push_back(preset_array, data); |
224 | | - } |
225 | | - return preset_array.Get(); |
226 | | -} |
227 | | - |
228 | 15 | /** |
229 | 16 | * Lambda factory macro for the PTZ proc_handler methods. This macro |
230 | 17 | * simplifies the registration of PTZDevice methods as targets for |
|
0 commit comments