File: tab-helpers.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 (253 lines) | stat: -rw-r--r-- 6,191 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "tab-helpers.hpp"
#include "log-helper.hpp"
#include "obs-module-helper.hpp"
#include "plugin-state-helpers.hpp"

#include <obs.hpp>
#include <QTabBar>
#include <string>
#include <unordered_map>
#include <vector>

namespace advss {

static std::vector<std::string> tabNames = {
	"generalTab",      "macroTab",      "windowTitleTab",   "executableTab",
	"screenRegionTab", "mediaTab",      "fileTab",          "randomTab",
	"timeTab",         "idleTab",       "sceneSequenceTab", "audioTab",
	"videoTab",        "sceneGroupTab", "transitionsTab",   "pauseTab",
};

static std::vector<int> &getTabOrderVector()
{
	static std::vector<int> tabOrder = std::vector<int>(tabNames.size());
	return tabOrder;
}

namespace {
struct TabCallbacks {
	std::function<QWidget *()> createWidget;
	std::function<void(QTabWidget *)> setupTab;
};
} // namespace

static std::unordered_map<const char *, TabCallbacks> createTabCallbacks;

static int lastOpenedTab = -1;
static bool alwaysShowTabs = false;

[[maybe_unused]] static bool _ = []() {
	AddPluginInitStep([]() {
		AddSaveStep([](obs_data_t *data) {
			obs_data_set_bool(data, "alwaysShowTabs",
					  alwaysShowTabs);
		});
		AddLoadStep([](obs_data_t *data) {
			alwaysShowTabs =
				obs_data_get_bool(data, "alwaysShowTabs");
		});
	});
	return true;
}();

void SetTabVisibleByName(QTabWidget *tabWidget, bool visible,
			 const QString &name)
{
	for (int idx = 0; idx < tabWidget->count(); idx++) {
		if (tabWidget->tabText(idx) != name) {
			continue;
		}

		tabWidget->setTabVisible(idx, visible);
	}
}

void AddSetupTabCallback(const char *tabName,
			 std::function<QWidget *()> createWidget,
			 std::function<void(QTabWidget *)> setupTab)
{
	if (std::find(tabNames.begin(), tabNames.end(), tabName) !=
	    tabNames.end()) {
		return;
	}
	tabNames.emplace_back(tabName);
	TabCallbacks callbacks = {createWidget, setupTab};
	createTabCallbacks.emplace(tabName, callbacks);
}

void SaveLastOpenedTab(QTabWidget *tabWidget)
{
	lastOpenedTab = tabWidget->currentIndex();
}

void ResetLastOpenedTab()
{
	lastOpenedTab = -1;
}

static bool tabWidgetOrderValid()
{
	auto &tabOrder = getTabOrderVector();

	if (tabNames.size() != tabOrder.size()) {
		return false;
	}

	auto tmp = std::vector<int>(tabNames.size());
	std::iota(tmp.begin(), tmp.end(), 0);

	for (auto &p : tmp) {
		auto it = std::find(tabOrder.begin(), tabOrder.end(), p);
		if (it == tabOrder.end()) {
			return false;
		}
	}
	return true;
}

static void resetTabWidgetOrder()
{
	auto &tabOrder = getTabOrderVector();
	tabOrder = std::vector<int>(tabNames.size());
	std::iota(tabOrder.begin(), tabOrder.end(), 0);
}

void SaveTabOrder(obs_data_t *obj)
{
	auto &tabOrder = getTabOrderVector();

	// Can happen when corrupting settings files
	if (!tabWidgetOrderValid()) {
		resetTabWidgetOrder();
	}

	OBSDataArrayAutoRelease tabWidgetOrder = obs_data_array_create();
	for (size_t i = 0; i < tabNames.size(); i++) {
		OBSDataAutoRelease entry = obs_data_create();
		obs_data_set_int(entry, tabNames[i].c_str(), tabOrder[i]);
		obs_data_array_push_back(tabWidgetOrder, entry);
	}
	obs_data_set_array(obj, "tabWidgetOrder", tabWidgetOrder);
}

void LoadTabOrder(obs_data_t *obj)
{
	OBSDataArrayAutoRelease defaultTabWidgetOrder = obs_data_array_create();
	for (size_t i = 0; i < tabNames.size(); i++) {
		OBSDataAutoRelease entry = obs_data_create();
		obs_data_set_default_int(entry, tabNames[i].c_str(), i);
		obs_data_array_push_back(defaultTabWidgetOrder, entry);
	}
	obs_data_set_default_array(obj, "tabWidgetOrder",
				   defaultTabWidgetOrder);

	auto &tabOrder = getTabOrderVector();
	tabOrder.clear();
	OBSDataArrayAutoRelease tabWidgetOrder =
		obs_data_get_array(obj, "tabWidgetOrder");
	for (size_t i = 0; i < tabNames.size(); i++) {
		OBSDataAutoRelease entry =
			obs_data_array_item(tabWidgetOrder, i);
		tabOrder.emplace_back(
			(int)(obs_data_get_int(entry, tabNames[i].c_str())));
	}

	if (!tabWidgetOrderValid()) {
		resetTabWidgetOrder();
	}
}

static int findTabIndex(QTabWidget *tabWidget, int pos)
{
	int at = -1;
	QString tabName = QString::fromStdString(tabNames.at(pos));
	QWidget *page = tabWidget->findChild<QWidget *>(tabName);
	if (page) {
		at = tabWidget->indexOf(page);
	}
	if (at == -1) {
		blog(LOG_INFO, "failed to find tab %s",
		     tabName.toUtf8().constData());
	}

	return at;
}

void SetTabOrder(QTabWidget *tabWidget)
{
	if (!tabWidgetOrderValid()) {
		resetTabWidgetOrder();
	}

	auto &tabOrder = getTabOrderVector();

	auto bar = tabWidget->tabBar();
	for (int i = 0; i < bar->count(); ++i) {
		int curPos = findTabIndex(tabWidget, tabOrder[i]);

		if (i != curPos && curPos != -1) {
			bar->moveTab(curPos, i);
		}
	}

	QWidget::connect(bar, &QTabBar::tabMoved,
			 [&tabOrder](int from, int to) {
				 std::swap(tabOrder[from], tabOrder[to]);
			 });
}

void SetCurrentTab(QTabWidget *tabWidget)
{
	if (lastOpenedTab >= 0 && tabWidget->isTabVisible(lastOpenedTab)) {
		tabWidget->setCurrentIndex(lastOpenedTab);
	}
}

void SetupOtherTabs(QTabWidget *tabWidget)
{
	for (const auto &[name, callbacks] : createTabCallbacks) {
		auto widget = callbacks.createWidget();
		widget->setObjectName(name);
		auto tabText = obs_module_text(
			(std::string("AdvSceneSwitcher.") + name + ".title")
				.c_str());
		tabWidget->insertTab(0, widget, tabText);
		callbacks.setupTab(tabWidget);

		if (alwaysShowTabs) {
			tabWidget->setTabVisible(0, true);
		}
	}
}

void SetupShowAllTabsCheckBox(QCheckBox *checkBox, QTabWidget *tabWidget)
{
	const auto stateChanged = [tabWidget](int state) {
		const bool newState = (state != 0);
		if (newState == alwaysShowTabs) {
			return;
		}

		alwaysShowTabs = newState;

		// Only show currently hidden tabs, but don't hide tabs which
		// are already visible
		if (!alwaysShowTabs) {
			return;
		}

		for (const auto &[name, _] : createTabCallbacks) {
			const auto localeKey =
				std::string("AdvSceneSwitcher.") + name +
				".title";
			auto tabText = obs_module_text(localeKey.c_str());
			SetTabVisibleByName(tabWidget, true, tabText);
		}
	};

	checkBox->setChecked(alwaysShowTabs);
	QWidget::connect(checkBox, &QCheckBox::stateChanged, checkBox,
			 stateChanged);
}

} // namespace advss