Skip to content

Commit

Permalink
UI: Use OBSSourceLabel for item widget labels
Browse files Browse the repository at this point in the history
This changes the labels for the source tree/visibility item widgets
to use OBSSourceLabel, as it handles the renaming of sources.
  • Loading branch information
cg2121 authored and tt2468 committed May 12, 2024
1 parent 09be4f9 commit 023d9bd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 45 deletions.
19 changes: 2 additions & 17 deletions UI/source-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "source-tree.hpp"
#include "qt-wrappers.hpp"
#include "platform.hpp"
#include "source-label.hpp"

#include <obs-frontend-api.h>
#include <obs.h>
Expand Down Expand Up @@ -96,7 +97,7 @@ SourceTreeItem::SourceTreeItem(SourceTree *tree_, OBSSceneItem sceneitem_)
lock->setAccessibleDescription(
QTStr("Basic.Main.Sources.LockDescription").arg(name));

label = new QLabel(QT_UTF8(name));
label = new OBSSourceLabel(source);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
label->setAttribute(Qt::WA_TranslucentBackground);
Expand Down Expand Up @@ -288,15 +289,6 @@ void SourceTreeItem::ReconnectSignals()

/* --------------------------------------------------------- */

auto renamed = [](void *data, calldata_t *cd) {
SourceTreeItem *this_ =
reinterpret_cast<SourceTreeItem *>(data);
const char *name = calldata_string(cd, "new_name");

QMetaObject::invokeMethod(this_, "Renamed",
Q_ARG(QString, QT_UTF8(name)));
};

auto removeSource = [](void *data, calldata_t *) {
SourceTreeItem *this_ =
reinterpret_cast<SourceTreeItem *>(data);
Expand All @@ -307,7 +299,6 @@ void SourceTreeItem::ReconnectSignals()

obs_source_t *source = obs_sceneitem_get_source(sceneitem);
signal = obs_source_get_signal_handler(source);
sigs.emplace_back(signal, "rename", renamed, this);
sigs.emplace_back(signal, "remove", removeSource, this);
}

Expand Down Expand Up @@ -470,7 +461,6 @@ void SourceTreeItem::ExitEditModeInternal(bool save)
redo, uuid, uuid);

obs_source_set_name(source, newName.c_str());
label->setText(QT_UTF8(newName.c_str()));
}

bool SourceTreeItem::eventFilter(QObject *object, QEvent *event)
Expand Down Expand Up @@ -509,11 +499,6 @@ void SourceTreeItem::LockedChanged(bool locked)
OBSBasic::Get()->UpdateEditMenu();
}

void SourceTreeItem::Renamed(const QString &name)
{
label->setText(name);
}

void SourceTreeItem::Update(bool force)
{
OBSScene scene = GetCurrentScene();
Expand Down
4 changes: 2 additions & 2 deletions UI/source-tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <obs-frontend-api.h>

class QLabel;
class OBSSourceLabel;
class QCheckBox;
class QLineEdit;
class SourceTree;
Expand Down Expand Up @@ -57,7 +58,7 @@ class SourceTreeItem : public QFrame {
QCheckBox *vis = nullptr;
QCheckBox *lock = nullptr;
QHBoxLayout *boxLayout = nullptr;
QLabel *label = nullptr;
OBSSourceLabel *label = nullptr;

QLineEdit *editor = nullptr;

Expand All @@ -79,7 +80,6 @@ private slots:

void VisibilityChanged(bool visible);
void LockedChanged(bool locked);
void Renamed(const QString &name);

void ExpandClicked(bool checked);

Expand Down
24 changes: 3 additions & 21 deletions UI/visibility-item-widget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "visibility-item-widget.hpp"
#include "qt-wrappers.hpp"
#include "obs-app.hpp"
#include "source-label.hpp"
#include <QListWidget>
#include <QLineEdit>
#include <QHBoxLayout>
Expand All @@ -12,19 +13,16 @@
VisibilityItemWidget::VisibilityItemWidget(obs_source_t *source_)
: source(source_),
enabledSignal(obs_source_get_signal_handler(source), "enable",
OBSSourceEnabled, this),
renamedSignal(obs_source_get_signal_handler(source), "rename",
OBSSourceRenamed, this)
OBSSourceEnabled, this)
{
const char *name = obs_source_get_name(source);
bool enabled = obs_source_enabled(source);

vis = new QCheckBox();
vis->setProperty("visibilityCheckBox", true);
vis->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
vis->setChecked(enabled);

label = new QLabel(QT_UTF8(name));
label = new OBSSourceLabel(source);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

QHBoxLayout *itemLayout = new QHBoxLayout();
Expand All @@ -50,28 +48,12 @@ void VisibilityItemWidget::OBSSourceEnabled(void *param, calldata_t *data)
Q_ARG(bool, enabled));
}

void VisibilityItemWidget::OBSSourceRenamed(void *param, calldata_t *data)
{
VisibilityItemWidget *window =
reinterpret_cast<VisibilityItemWidget *>(param);
const char *name = calldata_string(data, "new_name");

QMetaObject::invokeMethod(window, "SourceRenamed",
Q_ARG(QString, QT_UTF8(name)));
}

void VisibilityItemWidget::SourceEnabled(bool enabled)
{
if (vis->isChecked() != enabled)
vis->setChecked(enabled);
}

void VisibilityItemWidget::SourceRenamed(QString name)
{
if (label && name != label->text())
label->setText(name);
}

void VisibilityItemWidget::SetColor(const QColor &color, bool active_,
bool selected_)
{
Expand Down
7 changes: 2 additions & 5 deletions UI/visibility-item-widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ class QLineEdit;
class QListWidget;
class QListWidgetItem;
class QCheckBox;
class OBSSourceLabel;

class VisibilityItemWidget : public QWidget {
Q_OBJECT

private:
OBSSource source;
QLabel *label = nullptr;
OBSSourceLabel *label = nullptr;
QCheckBox *vis = nullptr;
QString oldName;

OBSSignal enabledSignal;
OBSSignal renamedSignal;

bool active = false;
bool selected = false;

static void OBSSourceEnabled(void *param, calldata_t *data);
static void OBSSourceRenamed(void *param, calldata_t *data);

private slots:
void SourceEnabled(bool enabled);
void SourceRenamed(QString name);

public:
VisibilityItemWidget(obs_source_t *source);
Expand Down

0 comments on commit 023d9bd

Please sign in to comment.