File: string-list.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (185 lines) | stat: -rw-r--r-- 4,463 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
#include "string-list.hpp"
#include "name-dialog.hpp"
#include "ui-helpers.hpp"

#include <QLayout>
#include <QTimer>

namespace advss {

bool StringList::Save(obs_data_t *obj, const char *name,
		      const char *elementName) const
{
	obs_data_array_t *strings = obs_data_array_create();
	for (auto &string : *this) {
		obs_data_t *array_obj = obs_data_create();
		string.Save(array_obj, elementName);
		obs_data_array_push_back(strings, array_obj);
		obs_data_release(array_obj);
	}
	obs_data_set_array(obj, name, strings);
	obs_data_array_release(strings);
	return true;
}

bool StringList::Load(obs_data_t *obj, const char *name,
		      const char *elementName)
{
	clear();
	obs_data_array_t *strings = obs_data_get_array(obj, name);
	size_t count = obs_data_array_count(strings);
	for (size_t i = 0; i < count; i++) {
		obs_data_t *array_obj = obs_data_array_item(strings, i);
		StringVariable string;
		string.Load(array_obj, elementName);
		*this << string;
		obs_data_release(array_obj);
	}
	obs_data_array_release(strings);
	return true;
}

void StringList::ResolveVariables()
{
	for (auto &value : *this) {
		value.ResolveVariables();
	}
}

StringListEdit::StringListEdit(
	QWidget *parent, const QString &addString,
	const QString &addStringDescription, int maxStringSize,
	const std::function<bool(const std::string &)> &filter,
	const std::function<void(std::string &input)> &preprocess)
	: ListEditor(parent),
	  _addString(addString),
	  _addStringDescription(addStringDescription),
	  _maxStringSize(maxStringSize),
	  _filterCallback(filter),
	  _preprocessCallback(preprocess)
{
	if (_addString.isEmpty()) {
		_addString = obs_module_text("AdvSceneSwitcher.windowTitle");
	}
}

void StringListEdit::SetStringList(const StringList &list)
{
	_stringList = list;
	_list->clear();
	for (const auto &string : list) {
		QListWidgetItem *item = new QListWidgetItem(
			QString::fromStdString(string.UnresolvedValue()),
			_list);
		item->setData(Qt::UserRole, string);
	}
	UpdateListSize();
}

void StringListEdit::SetMaxStringSize(int size)
{
	_maxStringSize = size;
}

void StringListEdit::Add()
{
	std::string entry;
	bool accepted = NameDialog::AskForName(this, _addString,
					       _addStringDescription, entry, "",
					       _maxStringSize, false);
	if (!accepted) {
		return;
	}

	_preprocessCallback(entry);
	if (_filterCallback(entry)) {
		return;
	}
	StringVariable string = entry;
	QVariant v = QVariant::fromValue(string);
	QListWidgetItem *item = new QListWidgetItem(
		QString::fromStdString(string.UnresolvedValue()), _list);
	item->setData(Qt::UserRole, string);

	_stringList << string;

	// Delay resizing to make sure the list viewport was already updated
	QTimer::singleShot(0, this, [this]() { UpdateListSize(); });

	StringListChanged(_stringList);
}

void StringListEdit::Remove()
{
	int idx = _list->currentRow();
	if (idx == -1) {
		return;
	}
	_stringList.removeAt(idx);

	QListWidgetItem *item = _list->currentItem();
	if (!item) {
		return;
	}
	delete item;

	// Delay resizing to make sure the list viewport was already updated
	QTimer::singleShot(0, this, [this]() { UpdateListSize(); });

	StringListChanged(_stringList);
}

void StringListEdit::Up()
{
	int idx = _list->currentRow();
	if (idx != -1 && idx != 0) {
		_list->insertItem(idx - 1, _list->takeItem(idx));
		_list->setCurrentRow(idx - 1);

		_stringList.move(idx, idx - 1);
	}
	StringListChanged(_stringList);
}

void StringListEdit::Down()
{
	int idx = _list->currentRow();
	if (idx != -1 && idx != _list->count() - 1) {
		_list->insertItem(idx + 1, _list->takeItem(idx));
		_list->setCurrentRow(idx + 1);

		_stringList.move(idx, idx + 1);
	}
	StringListChanged(_stringList);
}

void StringListEdit::Clicked(QListWidgetItem *item)
{
	std::string entry;
	bool accepted = NameDialog::AskForName(this, _addString,
					       _addStringDescription, entry,
					       item->text(), _maxStringSize,
					       false);
	if (!accepted) {
		return;
	}

	_preprocessCallback(entry);
	if (_filterCallback(entry)) {
		return;
	}

	StringVariable string = entry;
	QVariant v = QVariant::fromValue(string);
	item->setText(QString::fromStdString(string.UnresolvedValue()));
	item->setData(Qt::UserRole, string);
	int idx = _list->currentRow();
	_stringList[idx] = string;

	// Delay resizing to make sure the list viewport was already updated
	QTimer::singleShot(0, this, [this]() { UpdateListSize(); });

	StringListChanged(_stringList);
}

} // namespace advss