From 97d5a1c662d45442be78a60e50ca8b27df3801e3 Mon Sep 17 00:00:00 2001 From: cg2121 Date: Tue, 20 Aug 2024 01:48:55 -0500 Subject: [PATCH 1/2] UI: Use OBSBasic::Get() in all places This reduces duplicated code when getting the OBSBasic context. --- UI/adv-audio-control.cpp | 2 +- UI/context-bar-controls.cpp | 2 +- UI/obs-app.cpp | 6 ++-- UI/source-tree.cpp | 10 +++---- UI/window-basic-adv-audio.cpp | 2 +- UI/window-basic-auto-config-test.cpp | 2 +- UI/window-basic-auto-config.cpp | 8 ++--- UI/window-basic-filters.cpp | 25 +++++++--------- UI/window-basic-interaction.cpp | 2 +- UI/window-basic-main-dropfiles.cpp | 2 +- UI/window-basic-main-scene-collections.cpp | 2 +- UI/window-basic-main-transitions.cpp | 10 +++---- UI/window-basic-main.cpp | 10 +++---- UI/window-basic-preview.cpp | 35 +++++++++++----------- UI/window-basic-properties.cpp | 2 +- UI/window-basic-settings.cpp | 2 +- UI/window-basic-source-select.cpp | 10 +++---- UI/window-basic-stats.cpp | 6 ++-- UI/window-basic-status-bar.cpp | 12 ++++---- UI/window-basic-transform.cpp | 2 +- UI/window-missing-files.cpp | 2 +- UI/window-projector.cpp | 4 +-- 22 files changed, 78 insertions(+), 80 deletions(-) diff --git a/UI/adv-audio-control.cpp b/UI/adv-audio-control.cpp index 80c0a5c1c4d057..8a5987c437f2fa 100644 --- a/UI/adv-audio-control.cpp +++ b/UI/adv-audio-control.cpp @@ -73,7 +73,7 @@ OBSAdvAudioCtrl::OBSAdvAudioCtrl(QGridLayout *, obs_source_t *source_) : source( labelL->setText("L"); labelR->setText("R"); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); QIcon sourceIcon = main->GetSourceIcon(obs_source_get_id(source)); QPixmap pixmap = sourceIcon.pixmap(QSize(16, 16)); diff --git a/UI/context-bar-controls.cpp b/UI/context-bar-controls.cpp index c45d06976d47ca..b5dc75245dc9a7 100644 --- a/UI/context-bar-controls.cpp +++ b/UI/context-bar-controls.cpp @@ -50,7 +50,7 @@ void SourceToolbar::SetUndoProperties(obs_source_t *source, bool repeatable) return; } - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSource currentSceneSource = main->GetCurrentSceneSource(); if (!currentSceneSource) diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index 883bee21a0774d..acebe0364f468c 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -1428,7 +1428,7 @@ bool OBSApp::notify(QObject *receiver, QEvent *e) if (windowType == Qt::WindowType::Dialog || windowType == Qt::WindowType::Window || windowType == Qt::WindowType::Tool) { - OBSBasic *main = reinterpret_cast(GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); if (main) main->SetDisplayAffinity(window); } @@ -1733,7 +1733,7 @@ string GetFormatExt(const char *container) string GetOutputFilename(const char *path, const char *container, bool noSpace, bool overwrite, const char *format) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); os_dir_t *dir = path && path[0] ? os_opendir(path) : nullptr; @@ -2408,7 +2408,7 @@ void OBSApp::ProcessSigInt(void) char tmp; recv(sigintFd[1], &tmp, sizeof(tmp), 0); - OBSBasic *main = reinterpret_cast(GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); if (main) main->close(); #endif diff --git a/UI/source-tree.cpp b/UI/source-tree.cpp index a0242e3cd64092..f0586164f4f23e 100644 --- a/UI/source-tree.cpp +++ b/UI/source-tree.cpp @@ -24,7 +24,7 @@ static inline OBSScene GetCurrentScene() { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); return main->GetCurrentScene(); } @@ -53,7 +53,7 @@ SourceTreeItem::SourceTreeItem(SourceTree *tree_, OBSSceneItem sceneitem_) : tre setStyleSheet("background: none"); } - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); const char *id = obs_source_get_id(source); bool sourceVisible = obs_sceneitem_visible(sceneitem); @@ -281,7 +281,7 @@ void SourceTreeItem::mouseDoubleClickEvent(QMouseEvent *event) expand->setChecked(!expand->isChecked()); } else { obs_source_t *source = obs_sceneitem_get_source(sceneitem); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); if (obs_source_configurable(source)) { main->CreatePropertiesWindow(source); } @@ -350,7 +350,7 @@ void SourceTreeItem::ExitEditModeInternal(bool save) return; } - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); newName = QT_TO_UTF8(editor->text()); @@ -1522,7 +1522,7 @@ bool SourceTree::GroupedItemsSelected() const void SourceTree::Remove(OBSSceneItem item, OBSScene scene) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); GetStm()->Remove(item); main->SaveProject(); diff --git a/UI/window-basic-adv-audio.cpp b/UI/window-basic-adv-audio.cpp index 001b804d7e6938..ca8d2fc91e4e0b 100644 --- a/UI/window-basic-adv-audio.cpp +++ b/UI/window-basic-adv-audio.cpp @@ -35,7 +35,7 @@ OBSBasicAdvAudio::OBSBasicAdvAudio(QWidget *parent) : QDialog(parent), ui(new Ui OBSBasicAdvAudio::~OBSBasicAdvAudio() { - OBSBasic *main = reinterpret_cast(parent()); + OBSBasic *main = OBSBasic::Get(); for (size_t i = 0; i < controls.size(); ++i) delete controls[i]; diff --git a/UI/window-basic-auto-config-test.cpp b/UI/window-basic-auto-config-test.cpp index b53f37aa25417d..b5e20d84818666 100644 --- a/UI/window-basic-auto-config-test.cpp +++ b/UI/window-basic-auto-config-test.cpp @@ -230,7 +230,7 @@ void AutoConfigTestPage::TestBandwidthThread() obs_data_set_int(aencoder_settings, "bitrate", 32); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); const char *bind_ip = config_get_string(main->Config(), "Output", "BindIP"); obs_data_set_string(output_settings, "bind_ip", bind_ip); diff --git a/UI/window-basic-auto-config.cpp b/UI/window-basic-auto-config.cpp index eaed589ae4de2b..8403d5e1f3459e 100644 --- a/UI/window-basic-auto-config.cpp +++ b/UI/window-basic-auto-config.cpp @@ -936,7 +936,7 @@ AutoConfig::AutoConfig(QWidget *parent) : QWizard(parent) proc_handler_call(ph, "amazon_ivs_ingests_refresh", &cd); calldata_free(&cd); - OBSBasic *main = reinterpret_cast(parent); + OBSBasic *main = OBSBasic::Get(); main->EnableOutputs(false); installEventFilter(CreateShortcutFilter()); @@ -1075,7 +1075,7 @@ AutoConfig::AutoConfig(QWidget *parent) : QWizard(parent) AutoConfig::~AutoConfig() { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); main->EnableOutputs(true); EnableThreadedMessageBoxes(false); } @@ -1162,7 +1162,7 @@ inline const char *AutoConfig::GetEncoderId(Encoder enc) void AutoConfig::SaveStreamSettings() { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); /* ---------------------------------- */ /* save service */ @@ -1226,7 +1226,7 @@ void AutoConfig::SaveStreamSettings() void AutoConfig::SaveSettings() { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); if (recordingEncoder != Encoder::Stream) config_set_string(main->Config(), "SimpleOutput", "RecEncoder", GetEncoderId(recordingEncoder)); diff --git a/UI/window-basic-filters.cpp b/UI/window-basic-filters.cpp index c92036c897e240..9630ce96f2e5ec 100644 --- a/UI/window-basic-filters.cpp +++ b/UI/window-basic-filters.cpp @@ -58,7 +58,7 @@ OBSBasicFilters::OBSBasicFilters(QWidget *parent, OBSSource source_) renameSourceSignal(obs_source_get_signal_handler(source), "rename", OBSBasicFilters::SourceRenamed, this), noPreviewMargin(13) { - main = reinterpret_cast(parent); + main = OBSBasic::Get(); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); @@ -255,7 +255,7 @@ void OBSBasicFilters::UpdatePropertiesView(int row, bool async) OBSDataAutoRelease settings = obs_source_get_settings(filter); auto disabled_undo = [](void *vp, obs_data_t *settings) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); main->undo_s.disable(); obs_source_t *source = reinterpret_cast(vp); obs_source_update(source, settings); @@ -549,8 +549,7 @@ void OBSBasicFilters::AddNewFilter(const char *id) } std::string parent_uuid(obs_source_get_uuid(source)); - std::string scene_uuid = obs_source_get_uuid( - reinterpret_cast(App()->GetMainWindow())->GetCurrentSceneSource()); + std::string scene_uuid = obs_source_get_uuid(OBSBasic::Get()->GetCurrentSceneSource()); /* In order to ensure that the UUID persists through undo/redo, * we save the source data rather than just recreating the * source from scratch. */ @@ -563,7 +562,7 @@ void OBSBasicFilters::AddNewFilter(const char *id) auto undo = [scene_uuid](const std::string &data) { OBSSourceAutoRelease ssource = obs_get_source_by_uuid(scene_uuid.c_str()); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(ssource.Get(), true); + OBSBasic::Get()->SetCurrentScene(ssource.Get(), true); OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "suuid")); @@ -574,7 +573,7 @@ void OBSBasicFilters::AddNewFilter(const char *id) auto redo = [scene_uuid](const std::string &data) { OBSSourceAutoRelease ssource = obs_get_source_by_uuid(scene_uuid.c_str()); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(ssource.Get(), true); + OBSBasic::Get()->SetCurrentScene(ssource.Get(), true); OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "undo_uuid")); @@ -978,11 +977,10 @@ void OBSBasicFilters::FilterNameEdited(QWidget *editor, QListWidget *list) listItem->setText(QT_UTF8(name.c_str())); obs_source_set_name(filter, name.c_str()); - std::string scene_uuid = obs_source_get_uuid( - reinterpret_cast(App()->GetMainWindow())->GetCurrentSceneSource()); + std::string scene_uuid = obs_source_get_uuid(OBSBasic::Get()->GetCurrentSceneSource()); auto undo = [scene_uuid, prev = std::string(prevName), name](const std::string &uuid) { OBSSourceAutoRelease ssource = obs_get_source_by_uuid(scene_uuid.c_str()); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(ssource.Get(), true); + OBSBasic::Get()->SetCurrentScene(ssource.Get(), true); OBSSourceAutoRelease filter = obs_get_source_by_uuid(uuid.c_str()); obs_source_set_name(filter, prev.c_str()); @@ -990,7 +988,7 @@ void OBSBasicFilters::FilterNameEdited(QWidget *editor, QListWidget *list) auto redo = [scene_uuid, prev = std::string(prevName), name](const std::string &uuid) { OBSSourceAutoRelease ssource = obs_get_source_by_uuid(scene_uuid.c_str()); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(ssource.Get(), true); + OBSBasic::Get()->SetCurrentScene(ssource.Get(), true); OBSSourceAutoRelease filter = obs_get_source_by_uuid(uuid.c_str()); obs_source_set_name(filter, name.c_str()); @@ -1076,11 +1074,10 @@ void OBSBasicFilters::delete_filter(OBSSource filter) std::string parent_uuid(obs_source_get_uuid(source)); obs_data_set_string(wrapper, "undo_uuid", parent_uuid.c_str()); - std::string scene_uuid = - obs_source_get_uuid(reinterpret_cast(App()->GetMainWindow())->GetCurrentSceneSource()); + std::string scene_uuid = obs_source_get_uuid(OBSBasic::Get()->GetCurrentSceneSource()); auto undo = [scene_uuid](const std::string &data) { OBSSourceAutoRelease ssource = obs_get_source_by_uuid(scene_uuid.c_str()); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(ssource.Get(), true); + OBSBasic::Get()->SetCurrentScene(ssource.Get(), true); OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "undo_uuid")); @@ -1093,7 +1090,7 @@ void OBSBasicFilters::delete_filter(OBSSource filter) obs_data_set_string(rwrapper, "suuid", parent_uuid.c_str()); auto redo = [scene_uuid](const std::string &data) { OBSSourceAutoRelease ssource = obs_get_source_by_uuid(scene_uuid.c_str()); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(ssource.Get(), true); + OBSBasic::Get()->SetCurrentScene(ssource.Get(), true); OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "suuid")); diff --git a/UI/window-basic-interaction.cpp b/UI/window-basic-interaction.cpp index 93d572c5b07c55..dd8ec8feed7ab5 100644 --- a/UI/window-basic-interaction.cpp +++ b/UI/window-basic-interaction.cpp @@ -35,7 +35,7 @@ using namespace std; OBSBasicInteraction::OBSBasicInteraction(QWidget *parent, OBSSource source_) : QDialog(parent), - main(qobject_cast(parent)), + main(OBSBasic::Get()), ui(new Ui::OBSBasicInteraction), source(source_), removedSignal(obs_source_get_signal_handler(source), "remove", OBSBasicInteraction::SourceRemoved, this), diff --git a/UI/window-basic-main-dropfiles.cpp b/UI/window-basic-main-dropfiles.cpp index 0e47138162b35f..d24723cb4d43d3 100644 --- a/UI/window-basic-main-dropfiles.cpp +++ b/UI/window-basic-main-dropfiles.cpp @@ -107,7 +107,7 @@ void OBSBasic::AddDropURL(const char *url, QString &name, obs_data_t *settings, void OBSBasic::AddDropSource(const char *data, DropType image) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSDataAutoRelease settings = obs_data_create(); const char *type = nullptr; QString name; diff --git a/UI/window-basic-main-scene-collections.cpp b/UI/window-basic-main-scene-collections.cpp index 7fec2c88dee097..7e2ff0cd155352 100644 --- a/UI/window-basic-main-scene-collections.cpp +++ b/UI/window-basic-main-scene-collections.cpp @@ -332,7 +332,7 @@ void OBSBasic::RefreshSceneCollections(bool refreshCache) ui->actionRemoveSceneCollection->setEnabled(numAddedCollections > 1); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); main->ui->actionPasteFilters->setEnabled(false); main->ui->actionPasteRef->setEnabled(false); diff --git a/UI/window-basic-main-transitions.cpp b/UI/window-basic-main-transitions.cpp index a17137b2ba8c7d..c32bad9dfd6568 100644 --- a/UI/window-basic-main-transitions.cpp +++ b/UI/window-basic-main-transitions.cpp @@ -89,7 +89,7 @@ void OBSBasic::AddQuickTransitionHotkey(QuickTransition *qt) auto quickTransition = [](void *data, obs_hotkey_id, obs_hotkey_t *, bool pressed) { int id = (int)(uintptr_t)data; - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); if (pressed) QMetaObject::invokeMethod(main, "TriggerQuickTransition", Qt::QueuedConnection, Q_ARG(int, id)); @@ -1056,7 +1056,7 @@ QMenu *OBSBasic::CreateVisibilityTransitionMenu(bool visible) duration->setValue(curDuration); auto setTransition = [this](QAction *action, bool visible) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); QString id = action->property("transition_id").toString(); OBSSceneItem sceneItem = main->GetCurrentSceneItem(); @@ -1105,7 +1105,7 @@ QMenu *OBSBasic::CreateVisibilityTransitionMenu(bool visible) undo_redo, undo_redo, undo_data, redo_data); }; auto setDuration = [visible](int duration) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSceneItem item = main->GetCurrentSceneItem(); obs_sceneitem_set_transition_duration(item, visible, duration); @@ -1141,7 +1141,7 @@ QMenu *OBSBasic::CreateVisibilityTransitionMenu(bool visible) } auto copyTransition = [this](QAction *, bool visible) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSceneItem item = main->GetCurrentSceneItem(); obs_source_t *tr = obs_sceneitem_get_transition(item, visible); int trDur = obs_sceneitem_get_transition_duration(item, visible); @@ -1154,7 +1154,7 @@ QMenu *OBSBasic::CreateVisibilityTransitionMenu(bool visible) connect(action, &QAction::triggered, std::bind(copyTransition, action, visible)); auto pasteTransition = [this](QAction *, bool show) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSource tr = OBSGetStrongRef(main->copySourceTransition); int trDuration = main->copySourceTransitionDuration; if (!tr) diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index bbc3e6cb3dda67..1afb9dc363e1a7 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -3079,7 +3079,7 @@ void OBSBasic::AddScene(OBSSource source) obs_hotkey_register_source( source, "OBSBasic.SelectScene", Str("Basic.Hotkeys.SelectScene"), [](void *data, obs_hotkey_id, obs_hotkey_t *, bool pressed) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); auto potential_source = static_cast(data); OBSSourceAutoRelease source = obs_source_get_ref(potential_source); @@ -7928,7 +7928,7 @@ void undo_redo(const std::string &data) { OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "scene_uuid")); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(source.Get(), true); + OBSBasic::Get()->SetCurrentScene(source.Get(), true); obs_scene_load_transform_states(data.c_str()); } @@ -7936,13 +7936,13 @@ void undo_redo(const std::string &data) void OBSBasic::on_actionPasteTransform_triggered() { OBSDataAutoRelease wrapper = obs_scene_save_transform_states(GetCurrentScene(), false); - auto func = [](obs_scene_t *, obs_sceneitem_t *item, void *data) { + auto func = [](obs_scene_t *, obs_sceneitem_t *item, void *) { if (!obs_sceneitem_selected(item)) return true; if (obs_sceneitem_locked(item)) return true; - OBSBasic *main = reinterpret_cast(data); + OBSBasic *main = OBSBasic::Get(); obs_sceneitem_defer_update_begin(item); obs_sceneitem_set_info2(item, &main->copiedTransformInfo); @@ -7952,7 +7952,7 @@ void OBSBasic::on_actionPasteTransform_triggered() return true; }; - obs_scene_enum_items(GetCurrentScene(), func, this); + obs_scene_enum_items(GetCurrentScene(), func, nullptr); OBSDataAutoRelease rwrapper = obs_scene_save_transform_states(GetCurrentScene(), false); diff --git a/UI/window-basic-preview.cpp b/UI/window-basic-preview.cpp index 361e318c2f360c..80ac51d5511b97 100644 --- a/UI/window-basic-preview.cpp +++ b/UI/window-basic-preview.cpp @@ -47,7 +47,7 @@ void OBSBasicPreview::Init() vec2 OBSBasicPreview::GetMouseEventPos(QMouseEvent *event) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); float pixelRatio = main->GetDevicePixelRatio(); float scale = pixelRatio / main->previewScale; QPoint qtPos = event->pos(); @@ -173,7 +173,7 @@ static inline vec2 GetOBSScreenSize() vec3 OBSBasicPreview::GetSnapOffset(const vec3 &tl, const vec3 &br) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); vec2 screenSize = GetOBSScreenSize(); vec3 clampOffset; @@ -218,7 +218,7 @@ vec3 OBSBasicPreview::GetSnapOffset(const vec3 &tl, const vec3 &br) OBSSceneItem OBSBasicPreview::GetItemAtPos(const vec2 &pos, bool selectBelow) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); if (!scene) @@ -274,7 +274,7 @@ static bool CheckItemSelected(obs_scene_t * /* scene */, obs_sceneitem_t *item, bool OBSBasicPreview::SelectedAtPos(const vec2 &pos) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); if (!scene) @@ -435,7 +435,7 @@ static vec2 GetItemSize(obs_sceneitem_t *item) void OBSBasicPreview::GetStretchHandleData(const vec2 &pos, bool ignoreGroup) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); if (!scene) @@ -563,7 +563,7 @@ void OBSBasicPreview::mousePressEvent(QMouseEvent *event) return; } - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); float pixelRatio = main->GetDevicePixelRatio(); float x = pos.x() - main->previewX / pixelRatio; float y = pos.y() - main->previewY / pixelRatio; @@ -685,7 +685,7 @@ static bool select_one(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *p void OBSBasicPreview::DoSelect(const vec2 &pos) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); OBSSceneItem item = GetItemAtPos(pos, true); @@ -776,13 +776,14 @@ void OBSBasicPreview::mouseReleaseEvent(QMouseEvent *event) hoveredPreviewItems.push_back(item); selectedItems.clear(); } - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + + OBSBasic *main = OBSBasic::Get(); OBSDataAutoRelease rwrapper = obs_scene_save_transform_states(main->GetCurrentScene(), true); auto undo_redo = [](const std::string &data) { OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "scene_uuid")); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(source.Get(), true); + OBSBasic::Get()->SetCurrentScene(source.Get(), true); obs_scene_load_transform_states(data.c_str()); }; @@ -909,7 +910,7 @@ static bool GetSourceSnapOffset(obs_scene_t * /* scene */, obs_sceneitem_t *item void OBSBasicPreview::SnapItemMovement(vec2 &offset) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); SelectedItemBounds data; @@ -985,7 +986,7 @@ static bool move_items(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *p void OBSBasicPreview::MoveItems(const vec2 &pos) { Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers(); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); vec2 offset, moveOffset; @@ -1153,7 +1154,7 @@ static bool FindItemsInBox(obs_scene_t * /* scene */, obs_sceneitem_t *item, voi void OBSBasicPreview::BoxItems(const vec2 &startPos, const vec2 &pos) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); if (!scene) @@ -1480,7 +1481,7 @@ void OBSBasicPreview::StretchItem(const vec2 &pos) void OBSBasicPreview::RotateItem(const vec2 &pos) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers(); bool shiftDown = (modifiers & Qt::ShiftModifier); @@ -1528,7 +1529,7 @@ void OBSBasicPreview::RotateItem(const vec2 &pos) void OBSBasicPreview::mouseMoveEvent(QMouseEvent *event) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); changed = true; QPointF qtPos = event->position(); @@ -1608,7 +1609,7 @@ void OBSBasicPreview::mouseMoveEvent(QMouseEvent *event) if (!mouseMoved && hoveredPreviewItems.size() > 0) { mousePos = pos; - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); float scale = main->GetDevicePixelRatio(); float x = qtPos.x() - main->previewX / scale; float y = qtPos.y() - main->previewY / scale; @@ -2091,7 +2092,7 @@ void OBSBasicPreview::DrawOverflow() overflow = gs_texture_create_from_file(path.c_str()); } - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); @@ -2114,7 +2115,7 @@ void OBSBasicPreview::DrawSceneEditing() GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawSceneEditing"); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID); gs_technique_t *tech = gs_effect_get_technique(solid, "Solid"); diff --git a/UI/window-basic-properties.cpp b/UI/window-basic-properties.cpp index 342b02dd4d5d9b..a6958d06a9a98d 100644 --- a/UI/window-basic-properties.cpp +++ b/UI/window-basic-properties.cpp @@ -43,7 +43,7 @@ static void CreateTransitionScene(OBSSource scene, const char *text, uint32_t co OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_) : QDialog(parent), ui(new Ui::OBSBasicProperties), - main(qobject_cast(parent)), + main(OBSBasic::Get()), acceptClicked(false), source(source_), removedSignal(obs_source_get_signal_handler(source), "remove", OBSBasicProperties::SourceRemoved, this), diff --git a/UI/window-basic-settings.cpp b/UI/window-basic-settings.cpp index d8a35501b79ceb..8ddfe1e0e1a4dc 100644 --- a/UI/window-basic-settings.cpp +++ b/UI/window-basic-settings.cpp @@ -341,7 +341,7 @@ void RestrictResetBitrates(initializer_list boxes, int maxbitrate); OBSBasicSettings::OBSBasicSettings(QWidget *parent) : QDialog(parent), - main(qobject_cast(parent)), + main(OBSBasic::Get()), ui(new Ui::OBSBasicSettings) { string path; diff --git a/UI/window-basic-source-select.cpp b/UI/window-basic-source-select.cpp index 430a3f140cc106..dcffc966cccf34 100644 --- a/UI/window-basic-source-select.cpp +++ b/UI/window-basic-source-select.cpp @@ -56,7 +56,7 @@ bool OBSBasicSourceSelect::EnumGroups(void *data, obs_source_t *source) const char *id = obs_source_get_unversioned_id(source); if (strcmp(id, window->id) == 0) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); obs_sceneitem_t *existing = obs_scene_get_group(scene, name); @@ -152,7 +152,7 @@ char *get_new_source_name(const char *name, const char *format) static void AddExisting(OBSSource source, bool visible, bool duplicate, obs_transform_info *transform, obs_sceneitem_crop *crop, obs_blending_method *blend_method, obs_blending_type *blend_mode) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); if (!scene) return; @@ -193,7 +193,7 @@ static void AddExisting(const char *name, bool visible, bool duplicate, obs_tran bool AddNew(QWidget *parent, const char *id, const char *name, const bool visible, OBSSource &newSource, OBSSceneItem &newSceneItem) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSScene scene = main->GetCurrentScene(); bool success = false; if (!scene) @@ -245,7 +245,7 @@ void OBSBasicSourceSelect::on_buttonBox_accepted() QString source_name = item->text(); AddExisting(QT_TO_UTF8(source_name), visible, false, nullptr, nullptr, nullptr, nullptr); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); const char *scene_name = obs_source_get_name(main->GetCurrentSceneSource()); auto undo = [scene_name, main](const std::string &) { @@ -384,7 +384,7 @@ OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_, un }); if (strcmp(id_, "scene") == 0) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSource curSceneSource = main->GetCurrentSceneSource(); ui->selectExisting->setChecked(true); diff --git a/UI/window-basic-stats.cpp b/UI/window-basic-stats.cpp index bb8ab0bd801d5c..dd69c0e698a257 100644 --- a/UI/window-basic-stats.cpp +++ b/UI/window-basic-stats.cpp @@ -192,7 +192,7 @@ OBSBasicStats::OBSBasicStats(QWidget *parent, bool closable) QObject::connect(&recTimeLeft, &QTimer::timeout, this, &OBSBasicStats::RecordingTimeLeft); recTimeLeft.setInterval(REC_TIME_LEFT_INTERVAL); - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); const char *geometry = config_get_string(main->Config(), "Stats", "geometry"); if (geometry != NULL) { @@ -214,7 +214,7 @@ OBSBasicStats::OBSBasicStats(QWidget *parent, bool closable) void OBSBasicStats::closeEvent(QCloseEvent *event) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); if (isVisible()) { config_set_string(main->Config(), "Stats", "geometry", saveGeometry().toBase64().constData()); config_save_safe(main->Config(), "tmp", nullptr); @@ -268,7 +268,7 @@ void OBSBasicStats::InitializeValues() void OBSBasicStats::Update() { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); /* TODO: Un-hardcode */ diff --git a/UI/window-basic-status-bar.cpp b/UI/window-basic-status-bar.cpp index 5bcae6f334fb22..f9de9790569fbf 100644 --- a/UI/window-basic-status-bar.cpp +++ b/UI/window-basic-status-bar.cpp @@ -99,7 +99,7 @@ void OBSBasicStatusBar::Activate() void OBSBasicStatusBar::Deactivate() { - OBSBasic *main = qobject_cast(parent()); + OBSBasic *main = OBSBasic::Get(); if (!main) return; @@ -212,7 +212,7 @@ void OBSBasicStatusBar::UpdateBandwidth() void OBSBasicStatusBar::UpdateCPUUsage() { - OBSBasic *main = qobject_cast(parent()); + OBSBasic *main = OBSBasic::Get(); if (!main) return; @@ -391,7 +391,7 @@ void OBSBasicStatusBar::OBSOutputReconnectSuccess(void *data, calldata_t *) void OBSBasicStatusBar::Reconnect(int seconds) { - OBSBasic *main = qobject_cast(parent()); + OBSBasic *main = OBSBasic::Get(); if (!retries) main->SysTrayNotify(QTStr("Basic.SystemTray.Message.Reconnecting"), QSystemTrayIcon::Warning); @@ -423,7 +423,7 @@ void OBSBasicStatusBar::ReconnectClear() void OBSBasicStatusBar::ReconnectSuccess() { - OBSBasic *main = qobject_cast(parent()); + OBSBasic *main = OBSBasic::Get(); QString msg = QTStr("Basic.StatusBar.ReconnectSuccessful"); showMessage(msg, 4000); @@ -444,7 +444,7 @@ void OBSBasicStatusBar::ReconnectSuccess() void OBSBasicStatusBar::UpdateStatusBar() { - OBSBasic *main = qobject_cast(parent()); + OBSBasic *main = OBSBasic::Get(); UpdateBandwidth(); @@ -478,7 +478,7 @@ void OBSBasicStatusBar::UpdateStatusBar() void OBSBasicStatusBar::StreamDelayStarting(int sec) { - OBSBasic *main = qobject_cast(parent()); + OBSBasic *main = OBSBasic::Get(); if (!main || !main->outputHandler) return; diff --git a/UI/window-basic-transform.cpp b/UI/window-basic-transform.cpp index 2e84f35195b854..5ad241845f56f7 100644 --- a/UI/window-basic-transform.cpp +++ b/UI/window-basic-transform.cpp @@ -89,7 +89,7 @@ OBSBasicTransform::~OBSBasicTransform() auto undo_redo = [](const std::string &data) { OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str()); OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "scene_uuid")); - reinterpret_cast(App()->GetMainWindow())->SetCurrentScene(source.Get(), true); + OBSBasic::Get()->SetCurrentScene(source.Get(), true); obs_scene_load_transform_states(data.c_str()); }; diff --git a/UI/window-missing-files.cpp b/UI/window-missing-files.cpp index 57e641a743300a..5d7b400987ed54 100644 --- a/UI/window-missing-files.cpp +++ b/UI/window-missing-files.cpp @@ -249,7 +249,7 @@ QVariant MissingFilesModel::data(const QModelIndex &index, int role) const break; } } else if (role == Qt::DecorationRole && index.column() == MissingFilesColumn::Source) { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSourceAutoRelease source = obs_get_source_by_name(files[index.row()].source.toStdString().c_str()); if (source) { diff --git a/UI/window-projector.cpp b/UI/window-projector.cpp index 60f873bc93b7d1..7e4afd1666fe4f 100644 --- a/UI/window-projector.cpp +++ b/UI/window-projector.cpp @@ -158,7 +158,7 @@ void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy) if (!window->ready) return; - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); OBSSource source = window->GetSource(); uint32_t targetCX; @@ -298,7 +298,7 @@ void OBSProjector::mousePressEvent(QMouseEvent *event) void OBSProjector::EscapeTriggered() { - OBSBasic *main = reinterpret_cast(App()->GetMainWindow()); + OBSBasic *main = OBSBasic::Get(); main->DeleteProjector(this); } From 35dbc1f424fb3ecd61e53a75b0e06e1e872e322a Mon Sep 17 00:00:00 2001 From: cg2121 Date: Tue, 20 Aug 2024 02:09:27 -0500 Subject: [PATCH 2/2] UI: Use qobject_cast when getting OBSBasic Since we are converting between qobjects, it is safer to use qobject_cast than reinterpret_cast. --- UI/window-basic-main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index 1afb9dc363e1a7..76b39f0653bb35 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -9688,7 +9688,7 @@ void OBSBasic::RepairCustomExtraDockName() OBSBasic *OBSBasic::Get() { - return reinterpret_cast(App()->GetMainWindow()); + return qobject_cast(App()->GetMainWindow()); } bool OBSBasic::StreamingActive()