Skip to content

Commit e3d524a

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

7 files changed

Lines changed: 284 additions & 260 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ target_sources(
9797
src/ptz-controls.cpp
9898
src/ptz-device.cpp
9999
src/ptz-list-model.cpp
100+
src/ptz-preset-model.cpp
100101
src/settings.cpp
101102
src/ptz-visca.cpp
102103
src/ptz-visca-udp.cpp
@@ -109,6 +110,7 @@ target_sources(
109110
src/ptz-controls.hpp
110111
src/ptz-device.hpp
111112
src/ptz-list-model.hpp
113+
src/ptz-preset-model.hpp
112114
src/settings.hpp
113115
src/ptz-visca.hpp
114116
src/ptz-visca-udp.hpp

src/ptz-device.cpp

Lines changed: 3 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -1,230 +1,17 @@
1-
/* Pan Tilt Zoom device factory
1+
/* Pan Tilt Zoom Controls - PTZDevice base object
22
*
3-
* Copyright 2020 Grant Likely <grant.likely@secretlab.ca>
3+
* Copyright 2020-2026 Grant Likely <grant.likely@secretlab.ca>
44
*
55
* SPDX-License-Identifier: GPLv2
66
*/
77

88
#include <obs.hpp>
99
#include "ptz-device.hpp"
1010
#include "ptz-list-model.hpp"
11+
#include "ptz-preset-model.hpp"
1112
#include "ptz.h"
1213
#include "protocol-helpers.hpp"
1314

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-
22815
/**
22916
* Lambda factory macro for the PTZ proc_handler methods. This macro
23017
* simplifies the registration of PTZDevice methods as targets for

src/ptz-device.hpp

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
/* Pan Tilt Zoom device base object
1+
/* Pan Tilt Zoom Controls - PTZDevice base object
22
*
3-
* Copyright 2020 Grant Likely <grant.likely@secretlab.ca>
3+
* Copyright 2020-2026 Grant Likely <grant.likely@secretlab.ca>
44
*
55
* SPDX-License-Identifier: GPLv2
66
*/
77
#pragma once
88

9-
#include "ptz.h"
10-
#include <qt-wrappers.hpp>
11-
#include <memory>
129
#include <QObject>
13-
#include <QStringListModel>
14-
#include <QtGlobal>
1510
#include <obs.hpp>
1611
#include <obs-frontend-api.h>
12+
#include <qt-wrappers.hpp>
1713
#include <util/platform.h>
14+
#include "ptz.h"
15+
#include "ptz-preset-model.hpp"
1816

1917
#define ptz_log(level, format, ...) \
2018
blog(level, "[%s/%.12s] " format, this->type.c_str(), QT_TO_UTF8(this->objectName()), ##__VA_ARGS__)
@@ -24,42 +22,6 @@
2422
if (this->protocol_trace) \
2523
ptz_log(LOG_DEBUG, format, ##__VA_ARGS__)
2624

27-
class PTZPresetListModel : public QAbstractListModel {
28-
Q_OBJECT
29-
30-
protected:
31-
/* Collection of all presets, keyed by unique integer id.
32-
* On cameras that use preset numbers, the id is mapped 1:1 with the
33-
* preset number. */
34-
size_t m_maxPresets = 16;
35-
QMap<size_t, QVariantMap> m_presets;
36-
QList<size_t> m_displayOrder;
37-
void sanitize(size_t id);
38-
39-
public:
40-
/* QAbstractListModel overrides */
41-
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
42-
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
43-
bool moveRows(const QModelIndex &srcParent, int srcRow, int count, const QModelIndex &destParent,
44-
int destChild);
45-
int rowCount(const QModelIndex &parent = QModelIndex()) const;
46-
QVariant data(const QModelIndex &index, int role) const;
47-
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
48-
Qt::ItemFlags flags(const QModelIndex &index) const;
49-
50-
/* PTZ Preset API */
51-
int getPresetId(const QModelIndex &index) const;
52-
size_t maxPresets() const { return m_maxPresets; };
53-
void setMaxPresets(size_t max) { m_maxPresets = max; };
54-
int newPreset();
55-
QVariant presetProperty(size_t id, QString key);
56-
bool updatePreset(size_t id, const QVariantMap &map);
57-
int find(QString key, QVariant value);
58-
59-
void loadPresets(OBSDataArray preset_array);
60-
OBSDataArray savePresets() const;
61-
};
62-
6325
class PTZDevice : public QObject {
6426
Q_OBJECT
6527
friend class PTZListModel;

src/ptz-list-model.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,3 @@ void PTZListModel::preset_save(uint32_t device_id, int preset_id)
275275
if (ptz)
276276
ptz->memory_set(preset_id);
277277
}
278-

0 commit comments

Comments
 (0)