-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings-model.cpp
More file actions
176 lines (149 loc) · 5.61 KB
/
Copy pathsettings-model.cpp
File metadata and controls
176 lines (149 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "action-config.hpp"
#include "settings-model.hpp"
#include <obs-module.h>
#include <QSize>
#define tr(token) obs_module_text("observer_settings." token)
#define item_field(field) _settings.actions[row].field
static QString implode(const std::vector<std::string> &vec, const char *delim = "\n")
{
QString str;
for (auto it = vec.begin(); it < vec.end(); it++) {
str.append(it->c_str()).append(delim);
}
if (!str.isEmpty()) {
auto len = strlen(delim);
str.remove(str.length() - len, len);
}
return str;
}
#define add_enum(item) { item, #item }
static QMap<int, std::string> action_labels {
add_enum(ActionType::Toggle),
add_enum(ActionType::Hide),
add_enum(ActionType::Show),
add_enum(ActionType::SwitchScene),
};
ObserverSettingsModel::ObserverSettingsModel(observer_settings settings, QObject *parent)
: QAbstractTableModel(parent), _settings(settings)
{
}
int ObserverSettingsModel::columnCount(const QModelIndex & /* parent */) const
{
return Columns::COUNT;
}
int ObserverSettingsModel::rowCount(const QModelIndex & /* parent */) const
{
return _settings.actions.size();
}
QVariant ObserverSettingsModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
switch (role) {
case Qt::DisplayRole:
switch (col) {
case Columns::Users: {
auto users = implode(item_field(users));
return users.isEmpty() ? tr("any_user") : users;
}
case Columns::Expr: {
auto str = QString::fromStdString(item_field(expression)).prepend('/').append('/');
return item_field(ignore_case) ? str.append('i') : str;
}
case Columns::Action: {
QString sources;
switch (item_field(type)) {
case ActionType::Toggle:
case ActionType::Hide:
case ActionType::Show: {
auto data = get<sceneitems_context_data>(item_field(context_data));
sources = implode(data.sceneitems);
if (data.rollback_timeout > 0 && !sources.isEmpty()) {
sources.prepend(QString::fromStdString(obs_module_text("observer_settings.action_rollback"))
.arg(double(data.rollback_timeout) / 1000));
}
}
break;
case ActionType::SwitchScene: {
auto data = get<scene_context_data>(item_field(context_data));
sources = QString::fromStdString(data.scene);
if (data.rollback_timeout > 0 && !sources.isEmpty()) {
sources.prepend(QString::fromStdString(obs_module_text("observer_settings.action_rollback"))
.arg(double(data.rollback_timeout) / 1000));
}
}
break;
default:
return QString();
}
if (sources.isEmpty()) {
return QString();
}
auto timeout = item_field(timeout);
return QString::fromStdString(timeout > 0 ? tr("action_to") : tr("action"))
.arg(obs_module_text(action_labels.value(item_field(type)).c_str()))
.arg(sources)
.arg(timeout);
}
case Columns::Buttons:
case Columns::Active:
return QString();
default:
return QString("Row%1, Column%2").arg(row + 1).arg(col + 1);
}
case Qt::CheckStateRole:
if (col == Columns::Active) {
return item_field(active) ? Qt::Checked : Qt::Unchecked;
}
break;
}
return QVariant();
}
bool ObserverSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
switch (index.column()) {
case Columns::Active:
if (role == Qt::CheckStateRole) {
int row = index.row();
item_field(active) = value.toBool();
return true;
}
break;
}
return false;
}
QVariant ObserverSettingsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case Columns::Users:
return QString(tr("head_users"));
case Columns::Expr:
return QString(tr("head_expression"));
case Columns::Action:
return QString(tr("head_action"));
}
}
return QVariant();
}
Qt::ItemFlags ObserverSettingsModel::flags(const QModelIndex &index) const
{
switch (index.column()) {
case Columns::Active:
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
return Qt::ItemIsEnabled;
}
void ObserverSettingsModel::addItem(const action_descriptor action)
{
auto row = rowCount();
beginInsertRows(QModelIndex(), row, row);
_settings.actions.push_back(action);
endInsertRows();
}
void ObserverSettingsModel::removeItem(int index)
{
beginRemoveRows(QModelIndex(), index, index);
_settings.actions.erase(_settings.actions.begin() + index);
endRemoveRows();
}