File: status-control.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 (234 lines) | stat: -rw-r--r-- 5,734 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
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
#include "status-control.hpp"
#include "log-helper.hpp"
#include "obs-module-helper.hpp"
#include "path-helpers.hpp"
#include "plugin-state-helpers.hpp"
#include "ui-helpers.hpp"
#include "utility.hpp"

#include <obs-frontend-api.h>
#include <QAction>
#include <QMainWindow>
#include <QPalette>
#include <QToolBar>

#if LIBOBS_API_VER < MAKE_SEMANTIC_VERSION(30, 0, 0)
#include <QDockWidget>

static bool obs_frontend_add_dock_by_id(const char *id, const char *title,
					QWidget *widget)
{
	widget->setObjectName(id);

	auto dock = new QDockWidget();
	dock->setWindowTitle(title);
	dock->setWidget(widget);
	dock->setFloating(true);
	dock->setVisible(false);
	dock->setFeatures(QDockWidget::DockWidgetClosable |
			  QDockWidget::DockWidgetMovable |
			  QDockWidget::DockWidgetFloatable);

	return !!obs_frontend_add_dock(dock);
}
#endif

namespace advss {

void OpenSettingsWindow();

static QString colorToString(const QColor &color)
{
	return QString("rgba(") + QString::number(color.red()) + "," +
	       QString::number(color.green()) + "," +
	       QString::number(color.blue()) + "," +
	       QString::number(color.alpha()) + ")";
}

static QString getDefaultBackgroundColorString()
{
	static QString defaultColorString;
	static bool cacheReady = false;

	if (cacheReady) {
		return defaultColorString;
	}

	QWidget tempWidget;
	const auto defaultColor =
		tempWidget.palette().brush(QPalette::Window).color();
	defaultColorString = colorToString(defaultColor);

	cacheReady = true;
	return defaultColorString;
}

static QString getDefaultTextColorString()
{
	static QString defaultColorString;
	static bool cacheReady = false;

	if (cacheReady) {
		return defaultColorString;
	}

	QLabel tempWidget;
	const auto defaultColor =
		tempWidget.palette().brush(QPalette::Text).color();
	defaultColorString = colorToString(defaultColor);

	cacheReady = true;
	return defaultColorString;
}

StatusControl::StatusControl(QWidget *parent, bool noLayout)
	: QWidget(parent),
	  _button(new QPushButton("-", this)),
	  _buttonLayout(new QHBoxLayout()),
	  _status(new QLabel("-", this)),
	  _statusPrefix(new QLabel(
		  obs_module_text(
			  "AdvSceneSwitcher.generalTab.status.currentStatus"),
		  this))
{
	_statusPrefix->setWordWrap(true);
	_statusPrefix->setSizePolicy(QSizePolicy::Expanding,
				     QSizePolicy::Expanding);
	QWidget::connect(_button, SIGNAL(clicked()), this,
			 SLOT(ButtonClicked()));

	if (!noLayout) {
		_buttonLayout->setContentsMargins(0, 0, 0, 0);
		_buttonLayout->addWidget(_button);
		QVBoxLayout *layout = new QVBoxLayout();
		_statusPrefix->setAlignment(Qt::AlignCenter);
		_status->setAlignment(Qt::AlignCenter);
		layout->addWidget(_statusPrefix);
		layout->addWidget(_status);
		layout->addLayout(_buttonLayout);
		setLayout(layout);
	} else {
		_status->setSizePolicy(QSizePolicy::Maximum,
				       QSizePolicy::MinimumExpanding);
	}

	if (PluginIsRunning()) {
		SetStarted();
	} else {
		SetStopped();
	}
	connect(&_timer, SIGNAL(timeout()), this, SLOT(UpdateStatus()));
	_timer.start(1000);
}

void StatusControl::ButtonClicked()
{
	if (PluginIsRunning()) {
		StopPlugin();
		SetStopped();
	} else {
		StartPlugin();
		SetStarted();
	}
}

void StatusControl::UpdateStatus()
{
	if (PluginIsRunning()) {
		if (!_setToStopped) {
			return;
		}
		SetStarted();
	} else {
		if (_setToStopped) {
			return;
		}
		SetStopped();
	}
}

void StatusControl::SetStarted()
{
	_button->setText(
		obs_module_text("AdvSceneSwitcher.generalTab.status.stop"));
	_status->setText(obs_module_text("AdvSceneSwitcher.status.active"));
	if (_pulse) {
		_pulse->deleteLater();
		_pulse = nullptr;
	}
	SetStatusStyleSheet(false);
	_setToStopped = false;
}

void StatusControl::SetStopped()
{
	_button->setText(
		obs_module_text("AdvSceneSwitcher.generalTab.status.start"));
	_status->setText(obs_module_text("AdvSceneSwitcher.status.inactive"));
	SetStatusStyleSheet(true);
	if (HighlightUIElementsEnabled()) {
		_pulse = HighlightWidget(_status, QColor(Qt::red),
					 QColor(0, 0, 0, 0));
	}
	_setToStopped = true;
}

void StatusControl::SetStatusStyleSheet(bool stopped) const
{
	auto style = QString("QLabel{ "
			     "background-color: ");
	if (stopped) {
		style += getDefaultBackgroundColorString() + "; ";
	} else {
		style += "transparent; ";
	}
	style += "border-style: outset; "
		 "border-width: 2px; "
		 "border-radius: 7px; "
		 "border-color: " +
		 getDefaultTextColorString() + "; }";
	_status->setStyleSheet(style);
}

StatusDockWidget::StatusDockWidget(QWidget *parent) : QFrame(parent)
{
	setFrameShape(QFrame::StyledPanel);
	setFrameShadow(QFrame::Sunken);

	auto action = new QAction;
	action->setProperty("themeID", QVariant(QString::fromUtf8("cogsIcon")));
	action->setProperty("class", QVariant(QString::fromUtf8("icon-cogs")));
	action->connect(action, &QAction::triggered, OpenSettingsWindow);
	const auto path = QString::fromStdString(GetDataFilePath(
		"res/images/" + GetThemeTypeName() + "Advanced.svg"));
	QIcon icon(path);
	action->setIcon(icon);

	auto toolbar = new QToolBar;
	toolbar->setIconSize({16, 16});
	toolbar->setFloatable(false);
	toolbar->addAction(action);

	auto statusControl = new StatusControl(this);
	statusControl->ButtonLayout()->addWidget(toolbar);
	statusControl->ButtonLayout()->setStretchFactor(statusControl->Button(),
							100);

	auto layout = new QVBoxLayout;
	layout->addWidget(statusControl);
	layout->setContentsMargins(0, 0, 0, 0);
	setLayout(layout);
}

void SetupDock()
{
	auto dock = new StatusDockWidget();
	if (!obs_frontend_add_dock_by_id(
		    "advss-status-dock",
		    obs_module_text("AdvSceneSwitcher.windowTitle"), dock)) {
		blog(LOG_INFO, "failed to register status dock!");
		dock->deleteLater();
	}
}

} // namespace advss