mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
238 lines
5.4 KiB
C++
238 lines
5.4 KiB
C++
#pragma once
|
|
|
|
#include <QString>
|
|
#include <QTextStream>
|
|
#include <QLayout>
|
|
#include <QLabel>
|
|
#include <QMessageBox>
|
|
#include <unordered_map>
|
|
|
|
#include <obs.hpp>
|
|
#include <obs-module.h>
|
|
#include <obs-frontend-api.h>
|
|
|
|
static inline bool WeakSourceValid(obs_weak_source_t *ws)
|
|
{
|
|
obs_source_t *source = obs_weak_source_get_source(ws);
|
|
if (source) {
|
|
obs_source_release(source);
|
|
}
|
|
return !!source;
|
|
}
|
|
|
|
static inline std::string GetWeakSourceName(obs_weak_source_t *weak_source)
|
|
{
|
|
std::string name;
|
|
|
|
obs_source_t *source = obs_weak_source_get_source(weak_source);
|
|
if (source) {
|
|
name = obs_source_get_name(source);
|
|
obs_source_release(source);
|
|
}
|
|
|
|
return name;
|
|
}
|
|
|
|
static inline OBSWeakSource GetWeakSourceByName(const char *name)
|
|
{
|
|
OBSWeakSource weak;
|
|
obs_source_t *source = obs_get_source_by_name(name);
|
|
if (source) {
|
|
weak = obs_source_get_weak_source(source);
|
|
obs_weak_source_release(weak);
|
|
obs_source_release(source);
|
|
}
|
|
|
|
return weak;
|
|
}
|
|
|
|
static inline OBSWeakSource GetWeakSourceByQString(const QString &name)
|
|
{
|
|
return GetWeakSourceByName(name.toUtf8().constData());
|
|
}
|
|
|
|
static inline OBSWeakSource GetWeakTransitionByName(const char *transitionName)
|
|
{
|
|
OBSWeakSource weak;
|
|
obs_source_t *source = nullptr;
|
|
|
|
if (strcmp(transitionName, "Default") == 0) {
|
|
source = obs_frontend_get_current_transition();
|
|
weak = obs_source_get_weak_source(source);
|
|
obs_source_release(source);
|
|
obs_weak_source_release(weak);
|
|
return weak;
|
|
}
|
|
|
|
obs_frontend_source_list *transitions = new obs_frontend_source_list();
|
|
obs_frontend_get_transitions(transitions);
|
|
bool match = false;
|
|
|
|
for (size_t i = 0; i < transitions->sources.num; i++) {
|
|
const char *name =
|
|
obs_source_get_name(transitions->sources.array[i]);
|
|
if (strcmp(transitionName, name) == 0) {
|
|
match = true;
|
|
source = transitions->sources.array[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (match) {
|
|
weak = obs_source_get_weak_source(source);
|
|
obs_weak_source_release(weak);
|
|
}
|
|
obs_frontend_source_list_free(transitions);
|
|
|
|
return weak;
|
|
}
|
|
|
|
static inline OBSWeakSource GetWeakTransitionByQString(const QString &name)
|
|
{
|
|
return GetWeakTransitionByName(name.toUtf8().constData());
|
|
}
|
|
|
|
static inline OBSWeakSource GetWeakFilterByName(OBSWeakSource source,
|
|
const char *name)
|
|
{
|
|
OBSWeakSource weak;
|
|
auto s = obs_weak_source_get_source(source);
|
|
if (s) {
|
|
auto filterSource = obs_source_get_filter_by_name(s, name);
|
|
weak = obs_source_get_weak_source(filterSource);
|
|
obs_weak_source_release(weak);
|
|
obs_source_release(filterSource);
|
|
obs_source_release(s);
|
|
}
|
|
return weak;
|
|
}
|
|
|
|
static inline OBSWeakSource GetWeakFilterByQString(OBSWeakSource source,
|
|
const QString &name)
|
|
{
|
|
return GetWeakFilterByName(source, name.toUtf8().constData());
|
|
}
|
|
|
|
static inline std::string
|
|
getNextDelim(const std::string &text,
|
|
std::unordered_map<std::string, QWidget *> placeholders)
|
|
{
|
|
size_t pos = std::string::npos;
|
|
std::string res = "";
|
|
|
|
for (const auto &ph : placeholders) {
|
|
size_t newPos = text.find(ph.first);
|
|
if (newPos <= pos) {
|
|
pos = newPos;
|
|
res = ph.first;
|
|
}
|
|
}
|
|
|
|
if (pos == std::string::npos) {
|
|
return "";
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
/**
|
|
* Populate layout with labels and widgets based on provided text
|
|
*
|
|
* @param text Text based on which labels are generated and widgets are placed.
|
|
* @param layout Layout in which the widgets and labels will be placed.
|
|
* @param placeholders Map containing a mapping of placeholder strings to widgets.
|
|
* @param addStretch Add addStretch() to layout.
|
|
*/
|
|
static inline void
|
|
placeWidgets(std::string text, QBoxLayout *layout,
|
|
std::unordered_map<std::string, QWidget *> placeholders,
|
|
bool addStretch = true)
|
|
{
|
|
std::vector<std::pair<std::string, QWidget *>> labelsWidgetsPairs;
|
|
|
|
std::string delim = getNextDelim(text, placeholders);
|
|
while (delim != "") {
|
|
size_t pos = text.find(delim);
|
|
if (pos != std::string::npos) {
|
|
labelsWidgetsPairs.emplace_back(text.substr(0, pos),
|
|
placeholders[delim]);
|
|
text.erase(0, pos + delim.length());
|
|
}
|
|
delim = getNextDelim(text, placeholders);
|
|
}
|
|
|
|
if (text != "") {
|
|
labelsWidgetsPairs.emplace_back(text, nullptr);
|
|
}
|
|
|
|
for (auto &lw : labelsWidgetsPairs) {
|
|
if (lw.first != "") {
|
|
layout->addWidget(new QLabel(lw.first.c_str()));
|
|
}
|
|
if (lw.second) {
|
|
layout->addWidget(lw.second);
|
|
}
|
|
}
|
|
if (addStretch) {
|
|
layout->addStretch();
|
|
}
|
|
}
|
|
|
|
static inline void clearLayout(QLayout *layout)
|
|
{
|
|
QLayoutItem *item;
|
|
while ((item = layout->takeAt(0))) {
|
|
if (item->layout()) {
|
|
clearLayout(item->layout());
|
|
delete item->layout();
|
|
}
|
|
if (item->widget()) {
|
|
delete item->widget();
|
|
}
|
|
delete item;
|
|
}
|
|
}
|
|
|
|
static inline bool compareIgnoringLineEnding(QString &s1, QString &s2)
|
|
{
|
|
// Let QT deal with different types of lineendings
|
|
QTextStream s1stream(&s1);
|
|
QTextStream s2stream(&s2);
|
|
|
|
while (!s1stream.atEnd() || !s2stream.atEnd()) {
|
|
QString s1s = s1stream.readLine();
|
|
QString s2s = s2stream.readLine();
|
|
if (s1s != s2s) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!s1stream.atEnd() && !s2stream.atEnd()) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static inline bool DisplayMessage(const QString &msg, bool question = false)
|
|
{
|
|
if (question) {
|
|
QMessageBox::StandardButton reply;
|
|
reply = QMessageBox::question(
|
|
nullptr, "Advanced Scene Switcher", msg,
|
|
QMessageBox::Yes | QMessageBox::No);
|
|
if (reply == QMessageBox::Yes) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
QMessageBox Msgbox;
|
|
Msgbox.setWindowTitle("Advanced Scene Switcher");
|
|
Msgbox.setText(msg);
|
|
Msgbox.exec();
|
|
}
|
|
|
|
return false;
|
|
}
|