File: source-properties-button.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 (122 lines) | stat: -rw-r--r-- 3,402 bytes parent folder | download | duplicates (2)
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
#include "source-properties-button.hpp"
#include "log-helper.hpp"
#include "obs-module-helper.hpp"

namespace advss {

bool SourceSettingButton::Save(obs_data_t *obj) const
{
	OBSDataAutoRelease data = obs_data_create();
	obs_data_set_string(data, "id", id.c_str());
	obs_data_set_string(data, "description", description.c_str());
	obs_data_set_obj(obj, "sourceSettingButton", data);
	return true;
}

bool SourceSettingButton::Load(obs_data_t *obj)
{
	OBSDataAutoRelease data = obs_data_get_obj(obj, "sourceSettingButton");
	id = obs_data_get_string(data, "id");
	description = obs_data_get_string(data, "description");
	return true;
}

std::string SourceSettingButton::ToString() const
{
	if (id.empty()) {
		return "";
	}
	return "[" + id + "] " + description;
}

static void getSourceButtonsHelper(obs_properties_t *sourceProperties,
				   std::vector<SourceSettingButton> &buttons)
{
	auto it = obs_properties_first(sourceProperties);
	do {
		if (!it) {
			continue;
		}

		auto type = obs_property_get_type(it);
		if (type == OBS_PROPERTY_GROUP) {
			auto group = obs_property_group_content(it);
			getSourceButtonsHelper(group, buttons);
			continue;
		}

		if (obs_property_get_type(it) != OBS_PROPERTY_BUTTON) {
			continue;
		}
		SourceSettingButton button = {obs_property_name(it),
					      obs_property_description(it)};
		buttons.emplace_back(button);
	} while (obs_property_next(&it));
}

std::vector<SourceSettingButton> GetSourceButtons(OBSWeakSource source)
{
	OBSSourceAutoRelease s = obs_weak_source_get_source(source);
	std::vector<SourceSettingButton> buttons;
	auto sourceProperties = obs_source_properties(s);
	getSourceButtonsHelper(sourceProperties, buttons);
	obs_properties_destroy(sourceProperties);
	return buttons;
}

void PressSourceButton(const SourceSettingButton &button, obs_source_t *source)
{
	obs_properties_t *sourceProperties = obs_source_properties(source);
	obs_property_t *property =
		obs_properties_get(sourceProperties, button.id.c_str());
	if (!obs_property_button_clicked(property, source)) {
		blog(LOG_WARNING, "Failed to press settings button '%s' for %s",
		     button.id.c_str(), obs_source_get_name(source));
	}
	obs_properties_destroy(sourceProperties);
}

SourceSettingsButtonSelection::SourceSettingsButtonSelection(QWidget *parent)
	: FilterComboBox(parent)
{
	connect(this, &FilterComboBox::currentIndexChanged, [this](int idx) {
		emit SelectionChanged(
			qvariant_cast<SourceSettingButton>(itemData(idx)));
	});
}

void SourceSettingsButtonSelection::SetSource(const OBSWeakSource &source,
					      bool restorePreviousSelection)
{
	const auto previousSelection = currentText();
	PopulateSelection(source);
	if (restorePreviousSelection) {
		setCurrentIndex(findText(previousSelection));
	}
}

void SourceSettingsButtonSelection::SetSelection(
	const OBSWeakSource &source, const SourceSettingButton &button)
{
	SetSource(source, false);
	setCurrentText(QString::fromStdString(button.ToString()));
}

void SourceSettingsButtonSelection::PopulateSelection(
	const OBSWeakSource &source)
{
	const QSignalBlocker b(this);
	clear();
	auto buttons = GetSourceButtons(source);
	if (buttons.empty()) {
		addItem(obs_module_text("AdvSceneSwitcher.noSettingsButtons"));
	}

	for (const auto &button : buttons) {
		QVariant value;
		value.setValue(button);
		addItem(QString::fromStdString(button.ToString()), value);
	}
}

} // namespace advss