Fix connection selection being lost after renaming connection

This commit is contained in:
WarmUpTill 2023-03-28 22:26:58 +02:00 committed by WarmUpTill
parent cff2d805db
commit fe1ddc3a94
7 changed files with 60 additions and 14 deletions

View File

@ -538,8 +538,8 @@ void SwitcherData::loadSettings(obs_data_t *obj)
// selections to be available.
loadSceneGroups(obj);
loadVariables(obj);
loadMacros(obj);
loadConnections(obj);
loadMacros(obj);
loadWindowTitleSwitches(obj);
loadScreenRegionSwitches(obj);
loadPauseSwitches(obj);

View File

@ -18,7 +18,7 @@ static std::map<MacroActionWebsocket::Type, std::string> actionTypes = {
void MacroActionWebsocket::SendRequest()
{
auto connection = GetConnectionByName(_connection);
auto connection = _connection.lock();
if (!connection) {
return;
}
@ -46,7 +46,7 @@ void MacroActionWebsocket::LogAction() const
switch (_type) {
case MacroActionWebsocket::Type::REQUEST:
vblog(LOG_INFO, "sent msg \"%s\" via \"%s\"", _message.c_str(),
_connection.c_str());
GetWeakConnectionName(_connection).c_str());
break;
case MacroActionWebsocket::Type::EVENT:
vblog(LOG_INFO, "sent event \"%s\" to connected clients",
@ -62,7 +62,8 @@ bool MacroActionWebsocket::Save(obs_data_t *obj) const
MacroAction::Save(obj);
obs_data_set_int(obj, "type", static_cast<int>(_type));
_message.Save(obj, "message");
obs_data_set_string(obj, "connection", _connection.c_str());
obs_data_set_string(obj, "connection",
GetWeakConnectionName(_connection).c_str());
return true;
}
@ -71,14 +72,15 @@ bool MacroActionWebsocket::Load(obs_data_t *obj)
MacroAction::Load(obj);
_type = static_cast<Type>(obs_data_get_int(obj, "type"));
_message.Load(obj, "message");
_connection = obs_data_get_string(obj, "connection");
_connection =
GetWeakConnectionByName(obs_data_get_string(obj, "connection"));
return true;
}
std::string MacroActionWebsocket::GetShortDesc() const
{
if (_type == Type::REQUEST) {
return _connection;
return GetWeakConnectionName(_connection);
}
return "";
}
@ -206,6 +208,6 @@ void MacroActionWebsocketEdit::ConnectionSelectionChanged(
}
std::lock_guard<std::mutex> lock(switcher->m);
_entryData->_connection = connection.toStdString();
_entryData->_connection = GetWeakConnectionByQString(connection);
emit(HeaderInfoChanged(connection));
}

View File

@ -29,7 +29,7 @@ public:
Type _type = Type::REQUEST;
StringVariable _message = obs_module_text("AdvSceneSwitcher.enterText");
std::string _connection;
std::weak_ptr<Connection> _connection;
private:
void SendRequest();

View File

@ -38,7 +38,7 @@ bool MacroConditionWebsocket::CheckCondition()
messages = &switcher->websocketMessages;
break;
case MacroConditionWebsocket::Type::EVENT: {
auto connection = GetConnectionByName(_connection);
auto connection = _connection.lock();
if (!connection) {
return false;
}
@ -76,7 +76,8 @@ bool MacroConditionWebsocket::Save(obs_data_t *obj) const
obs_data_set_int(obj, "type", static_cast<int>(_type));
_message.Save(obj, "message");
_regex.Save(obj);
obs_data_set_string(obj, "connection", _connection.c_str());
obs_data_set_string(obj, "connection",
GetWeakConnectionName(_connection).c_str());
return true;
}
@ -91,7 +92,8 @@ bool MacroConditionWebsocket::Load(obs_data_t *obj)
_regex.CreateBackwardsCompatibleRegex(
obs_data_get_bool(obj, "useRegex"), false);
}
_connection = obs_data_get_string(obj, "connection");
_connection =
GetWeakConnectionByName(obs_data_get_string(obj, "connection"));
return true;
}
@ -100,7 +102,7 @@ std::string MacroConditionWebsocket::GetShortDesc() const
if (_type == Type::REQUEST) {
return "";
}
return _connection;
return GetWeakConnectionName(_connection);
}
static inline void populateConditionSelection(QComboBox *list)
@ -237,7 +239,7 @@ void MacroConditionWebsocketEdit::ConnectionSelectionChanged(
}
std::lock_guard<std::mutex> lock(switcher->m);
_entryData->_connection = connection.toStdString();
_entryData->_connection = GetWeakConnectionByQString(connection);
emit(HeaderInfoChanged(connection));
}

View File

@ -27,7 +27,7 @@ public:
Type _type = Type::REQUEST;
StringVariable _message = obs_module_text("AdvSceneSwitcher.enterText");
RegexConfig _regex;
std::string _connection;
std::weak_ptr<Connection> _connection;
private:
static bool _registered;

View File

@ -133,6 +133,32 @@ Connection *GetConnectionByName(const std::string &name)
return nullptr;
}
std::weak_ptr<Connection> GetWeakConnectionByName(const std::string &name)
{
for (const auto &c : switcher->connections) {
if (c->Name() == name) {
std::weak_ptr<Connection> wp =
std::dynamic_pointer_cast<Connection>(c);
return wp;
}
}
return std::weak_ptr<Connection>();
}
std::weak_ptr<Connection> GetWeakConnectionByQString(const QString &name)
{
return GetWeakConnectionByName(name.toStdString());
}
std::string GetWeakConnectionName(std::weak_ptr<Connection> connection)
{
auto con = connection.lock();
if (!con) {
return "invalid connection selection";
}
return con->Name();
}
bool ConnectionNameAvailable(const QString &name)
{
return !GetConnectionByName(name);
@ -187,6 +213,18 @@ void ConnectionSelection::SetConnection(const std::string &con)
}
}
void ConnectionSelection::SetConnection(
const std::weak_ptr<Connection> &connection_)
{
const QSignalBlocker blocker(_selection);
auto connection = connection_.lock();
if (connection) {
SetConnection(connection->Name());
} else {
_selection->setCurrentIndex(0);
}
}
ConnectionSettingsDialog::ConnectionSettingsDialog(QWidget *parent,
const Connection &settings)
: ItemSettingsDialog(settings, switcher->connections,

View File

@ -56,6 +56,9 @@ private:
Connection *GetConnectionByName(const QString &);
Connection *GetConnectionByName(const std::string &);
std::weak_ptr<Connection> GetWeakConnectionByName(const std::string &name);
std::weak_ptr<Connection> GetWeakConnectionByQString(const QString &name);
std::string GetWeakConnectionName(std::weak_ptr<Connection>);
class ConnectionSettingsDialog : public ItemSettingsDialog {
Q_OBJECT
@ -92,4 +95,5 @@ class ConnectionSelection : public ItemSelection {
public:
ConnectionSelection(QWidget *parent = 0);
void SetConnection(const std::string &);
void SetConnection(const std::weak_ptr<Connection> &);
};