From f2f458214146859d341fd9ae6975978f002147a3 Mon Sep 17 00:00:00 2001 From: Clayton Groeneveld Date: Wed, 9 Oct 2019 00:30:22 -0500 Subject: [PATCH] UI: Add ability to reorder filters by drag & drop This adds the ability for filters to be dragged and dropped to be reordered, similar to scenes and sources. --- UI/focus-list.cpp | 17 +++++++++++++++++ UI/focus-list.hpp | 3 +++ UI/forms/OBSBasicFilters.ui | 18 ++++++++++++++++++ UI/window-basic-filters.cpp | 30 ++++++++++++++++++++++++++++++ UI/window-basic-filters.hpp | 4 ++++ 5 files changed, 72 insertions(+) diff --git a/UI/focus-list.cpp b/UI/focus-list.cpp index bf8bd461e..2894cfa8b 100644 --- a/UI/focus-list.cpp +++ b/UI/focus-list.cpp @@ -1,4 +1,5 @@ #include "focus-list.hpp" +#include FocusList::FocusList(QWidget *parent) : QListWidget(parent) {} @@ -8,3 +9,19 @@ void FocusList::focusInEvent(QFocusEvent *event) emit GotFocus(); } + +void FocusList::dragMoveEvent(QDragMoveEvent *event) +{ +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QPoint pos = event->position().toPoint(); +#else + QPoint pos = event->pos(); +#endif + int itemRow = row(itemAt(pos)); + + if ((itemRow == currentRow() + 1) || + (currentRow() == count() - 1 && itemRow == -1)) + event->ignore(); + else + QListWidget::dragMoveEvent(event); +} diff --git a/UI/focus-list.hpp b/UI/focus-list.hpp index b75e329d9..370dcbcc1 100644 --- a/UI/focus-list.hpp +++ b/UI/focus-list.hpp @@ -2,6 +2,8 @@ #include +class QDragMoveEvent; + class FocusList : public QListWidget { Q_OBJECT @@ -10,6 +12,7 @@ public: protected: void focusInEvent(QFocusEvent *event) override; + virtual void dragMoveEvent(QDragMoveEvent *event) override; signals: void GotFocus(); diff --git a/UI/forms/OBSBasicFilters.ui b/UI/forms/OBSBasicFilters.ui index 3594ac174..7cab6c152 100644 --- a/UI/forms/OBSBasicFilters.ui +++ b/UI/forms/OBSBasicFilters.ui @@ -72,6 +72,15 @@ 1 + + true + + + QAbstractItemView::InternalMove + + + Qt::TargetMoveAction + @@ -285,6 +294,15 @@ 1 + + true + + + QAbstractItemView::InternalMove + + + Qt::TargetMoveAction + diff --git a/UI/window-basic-filters.cpp b/UI/window-basic-filters.cpp index 5b3caa7b6..9f5520349 100644 --- a/UI/window-basic-filters.cpp +++ b/UI/window-basic-filters.cpp @@ -103,6 +103,11 @@ OBSBasicFilters::OBSBasicFilters(QWidget *parent, OBSSource source_) connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), this, SLOT(ResetFilters())); + connect(ui->asyncFilters->model(), &QAbstractItemModel::rowsMoved, this, + &OBSBasicFilters::FiltersMoved); + connect(ui->effectFilters->model(), &QAbstractItemModel::rowsMoved, + this, &OBSBasicFilters::FiltersMoved); + uint32_t caps = obs_source_get_output_flags(source); bool audio = (caps & OBS_SOURCE_AUDIO) != 0; bool audioOnly = (caps & OBS_SOURCE_VIDEO) == 0; @@ -1276,3 +1281,28 @@ void OBSBasicFilters::delete_filter(OBSSource filter) redo, undo_data, redo_data, false); obs_source_filter_remove(source, filter); } + +void OBSBasicFilters::FiltersMoved(const QModelIndex &, int srcIdxStart, int, + const QModelIndex &, int) +{ + QListWidget *list = isAsync ? ui->asyncFilters : ui->effectFilters; + int neighborIdx = 0; + + if (srcIdxStart < list->currentRow()) + neighborIdx = list->currentRow() - 1; + else if (srcIdxStart > list->currentRow()) + neighborIdx = list->currentRow() + 1; + else + return; + + if (neighborIdx > list->count() - 1) + neighborIdx = list->count() - 1; + else if (neighborIdx < 0) + neighborIdx = 0; + + OBSSource neighbor = GetFilter(neighborIdx, isAsync); + size_t idx = obs_source_filter_get_index(source, neighbor); + + OBSSource filter = GetFilter(list->currentRow(), isAsync); + obs_source_filter_set_index(source, filter, idx); +} diff --git a/UI/window-basic-filters.hpp b/UI/window-basic-filters.hpp index 315995e66..99ecc249f 100644 --- a/UI/window-basic-filters.hpp +++ b/UI/window-basic-filters.hpp @@ -121,6 +121,10 @@ private slots: void CopyFilter(); void PasteFilter(); + void FiltersMoved(const QModelIndex &srcParent, int srcIdxStart, + int srcIdxEnd, const QModelIndex &dstParent, + int dstIdx); + public: OBSBasicFilters(QWidget *parent, OBSSource source_); ~OBSBasicFilters();