File: macro-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 (171 lines) | stat: -rw-r--r-- 3,952 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
#include "macro-list.hpp"
#include "macro-helpers.hpp"
#include "macro-selection.hpp"
#include "macro-signals.hpp"
#include "obs-module-helper.hpp"
#include "ui-helpers.hpp"

namespace advss {

MacroList::MacroList(QWidget *parent, bool allowDuplicates, bool reorder)
	: ListEditor(parent, reorder),
	  _allowDuplicates(allowDuplicates)
{
	QWidget::connect(MacroSignalManager::Instance(),
			 SIGNAL(Rename(const QString &, const QString &)), this,
			 SLOT(MacroRename(const QString &, const QString &)));
	QWidget::connect(MacroSignalManager::Instance(),
			 SIGNAL(Remove(const QString &)), this,
			 SLOT(MacroRemove(const QString &)));
	UpdateListSize();
}

void MacroList::SetContent(const std::vector<MacroRef> &macros)
{
	for (auto &m : macros) {
		QString listEntryName;
		auto macroName = m.Name();
		if (macroName.empty()) {
			listEntryName = QString::fromStdString(
				std::string("<") +
				obs_module_text(
					"AdvSceneSwitcher.macroList.deleted") +
				">");
		} else {
			listEntryName = QString::fromStdString(macroName);
		}
		QListWidgetItem *item =
			new QListWidgetItem(listEntryName, _list);
		item->setData(Qt::UserRole, listEntryName);
	}
	UpdateListSize();
}

QAction *MacroList::AddControl(QWidget *widget, bool addSeperator)
{
	if (addSeperator) {
		_controls->addSeparator();
	}
	return _controls->addWidget(widget);
}

int MacroList::CurrentRow()
{
	return _list->currentRow();
}

void MacroList::MacroRename(const QString &oldName, const QString &newName)
{
	auto count = _list->count();
	for (int idx = 0; idx < count; ++idx) {
		QListWidgetItem *item = _list->item(idx);
		QString itemString = item->data(Qt::UserRole).toString();
		if (oldName == itemString) {
			item->setData(Qt::UserRole, newName);
			item->setText(newName);
		}
	}
}

void MacroList::MacroRemove(const QString &name)
{
	int idx = FindEntry(name.toStdString());
	while (idx != -1) {
		delete _list->item(idx);
		idx = FindEntry(name.toStdString());
	}
	UpdateListSize();
}

void MacroList::Add()
{
	std::string macroName;
	bool accepted = MacroSelectionDialog::AskForMacro(macroName);

	if (!accepted || macroName.empty()) {
		return;
	}

	if (!_allowDuplicates && FindEntry(macroName) != -1) {
		return;
	}

	QVariant v = QVariant::fromValue(QString::fromStdString(macroName));
	auto item =
		new QListWidgetItem(QString::fromStdString(macroName), _list);
	item->setData(Qt::UserRole, QString::fromStdString(macroName));
	UpdateListSize();
	emit Added(macroName);
}

void MacroList::Remove()
{
	auto item = _list->currentItem();
	int idx = _list->currentRow();
	if (!item || idx == -1) {
		return;
	}
	delete item;
	UpdateListSize();
	emit Removed(idx);
}

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

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

void MacroList::Clicked(QListWidgetItem *item)
{
	std::string macroName;
	bool accepted = MacroSelectionDialog::AskForMacro(macroName);

	if (!accepted || macroName.empty()) {
		return;
	}

	if (!_allowDuplicates && FindEntry(macroName) != -1) {
		QString err =
			obs_module_text("AdvSceneSwitcher.macroList.duplicate");
		DisplayMessage(err.arg(QString::fromStdString(macroName)));
		return;
	}

	item->setText(QString::fromStdString(macroName));
	int idx = _list->currentRow();
	emit Replaced(idx, macroName);
}

int MacroList::FindEntry(const std::string &macro)
{
	int count = _list->count();
	int idx = -1;

	for (int i = 0; i < count; i++) {
		QListWidgetItem *item = _list->item(i);
		QString itemString = item->data(Qt::UserRole).toString();
		if (QString::fromStdString(macro) == itemString) {
			idx = i;
			break;
		}
	}

	return idx;
}

} // namespace advss