Skip to content

Commit

Permalink
UI: Add Hotkey filter search and duplicate detection
Browse files Browse the repository at this point in the history
  • Loading branch information
furious authored and jp9000 committed Oct 24, 2021
1 parent 16906e7 commit 793eb40
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 64 deletions.
2 changes: 2 additions & 0 deletions UI/data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,8 @@ Basic.AdvAudio.AudioTracks="Tracks"
Basic.Settings.Hotkeys="Hotkeys"
Basic.Settings.Hotkeys.Pair="Key combinations shared with '%1' act as toggles"
Basic.Settings.Hotkeys.Filter="Filter"
Basic.Settings.Hotkeys.FilterByHotkey="Filter by Hotkey"
Basic.Settings.Hotkeys.DuplicateWarning="This hotkey is shared by one or more other actions, click to show conflicts"

# basic mode hotkeys
Basic.Hotkeys.SelectScene="Switch to scene"
Expand Down
1 change: 1 addition & 0 deletions UI/forms/images/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions UI/forms/obs.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<file>images/trash.svg</file>
<file>images/revert.svg</file>
<file>images/alert.svg</file>
<file>images/warning.svg</file>
<file>images/sources/brush.svg</file>
<file>images/sources/camera.svg</file>
<file>images/sources/gamepad.svg</file>
Expand Down
42 changes: 28 additions & 14 deletions UI/hotkey-edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,17 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "window-basic-settings.hpp"
#include "hotkey-edit.hpp"

#include <util/dstr.hpp>
#include <QPointer>
#include <QStyle>
#include <QAction>

#include "obs-app.hpp"
#include "qt-wrappers.hpp"

static inline bool operator!=(const obs_key_combination_t &c1,
const obs_key_combination_t &c2)
{
return c1.modifiers != c2.modifiers || c1.key != c2.key;
}

static inline bool operator==(const obs_key_combination_t &c1,
const obs_key_combination_t &c2)
{
return !(c1 != c2);
}

void OBSHotkeyEdit::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat())
Expand Down Expand Up @@ -183,6 +173,14 @@ void OBSHotkeyEdit::ClearKey()
RenderKey();
}

void OBSHotkeyEdit::UpdateDuplicationState()
{
if (dupeIcon->isVisible() != hasDuplicate) {
dupeIcon->setVisible(hasDuplicate);
update();
}
}

void OBSHotkeyEdit::InitSignalHandler()
{
layoutChanged = {
Expand All @@ -194,6 +192,16 @@ void OBSHotkeyEdit::InitSignalHandler()
this};
}

void OBSHotkeyEdit::CreateDupeIcon()
{
dupeIcon = this->addAction(settings->GetHotkeyConflictIcon(),
ActionPosition::TrailingPosition);
dupeIcon->setToolTip(QTStr("Basic.Settings.Hotkeys.DuplicateWarning"));
QObject::connect(dupeIcon, &QAction::triggered,
[=] { emit SearchKey(key); });
dupeIcon->setVisible(false);
}

void OBSHotkeyEdit::ReloadKeyLayout()
{
RenderKey();
Expand Down Expand Up @@ -266,7 +274,7 @@ void OBSHotkeyWidget::Save(std::vector<obs_key_combination_t> &combinations)

void OBSHotkeyWidget::AddEdit(obs_key_combination combo, int idx)
{
auto edit = new OBSHotkeyEdit(combo);
auto edit = new OBSHotkeyEdit(combo, settings);
edit->setToolTip(toolTip);

auto revert = new QPushButton;
Expand Down Expand Up @@ -347,14 +355,17 @@ void OBSHotkeyWidget::AddEdit(obs_key_combination combo, int idx)

QObject::connect(edit, &OBSHotkeyEdit::KeyChanged,
[&](obs_key_combination) { emit KeyChanged(); });
QObject::connect(edit, &OBSHotkeyEdit::SearchKey,
[=](obs_key_combination combo) {
emit SearchKey(combo);
});
}

void OBSHotkeyWidget::RemoveEdit(size_t idx, bool signal)
{
auto &edit = *(begin(edits) + idx);
if (!obs_key_combination_is_empty(edit->original) && signal) {
changed = true;
emit KeyChanged();
}

revertButtons.erase(begin(revertButtons) + idx);
Expand All @@ -371,6 +382,8 @@ void OBSHotkeyWidget::RemoveEdit(size_t idx, bool signal)

if (removeButtons.size() == 1)
removeButtons.front()->setEnabled(false);

emit KeyChanged();
}

void OBSHotkeyWidget::BindingsChanged(void *data, calldata_t *param)
Expand Down Expand Up @@ -458,6 +471,7 @@ void OBSHotkeyLabel::enterEvent(QEnterEvent *event)
void OBSHotkeyLabel::enterEvent(QEvent *event)
#endif
{

if (!pairPartner)
return;

Expand Down
41 changes: 33 additions & 8 deletions UI/hotkey-edit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@

#include <obs.hpp>

static inline bool operator!=(const obs_key_combination_t &c1,
const obs_key_combination_t &c2)
{
return c1.modifiers != c2.modifiers || c1.key != c2.key;
}

static inline bool operator==(const obs_key_combination_t &c1,
const obs_key_combination_t &c2)
{
return !(c1 != c2);
}

class OBSBasicSettings;
class OBSHotkeyWidget;

class OBSHotkeyLabel : public QLabel {
Expand All @@ -49,8 +62,9 @@ class OBSHotkeyEdit : public QLineEdit {
Q_OBJECT;

public:
OBSHotkeyEdit(obs_key_combination_t original, QWidget *parent = nullptr)
: QLineEdit(parent), original(original)
OBSHotkeyEdit(obs_key_combination_t original,
OBSBasicSettings *settings)
: QLineEdit(nullptr), original(original), settings(settings)
{
#ifdef __APPLE__
// disable the input cursor on OSX, focus should be clear
Expand All @@ -60,49 +74,58 @@ class OBSHotkeyEdit : public QLineEdit {
setAttribute(Qt::WA_InputMethodEnabled, false);
setAttribute(Qt::WA_MacShowFocusRect, true);
InitSignalHandler();
CreateDupeIcon();
ResetKey();
}

obs_key_combination_t original;
obs_key_combination_t key;
OBSBasicSettings *settings;
bool changed = false;

void UpdateDuplicationState();
bool hasDuplicate = false;

protected:
OBSSignal layoutChanged;
QAction *dupeIcon;

void InitSignalHandler();
void CreateDupeIcon();

void keyPressEvent(QKeyEvent *event) override;
#ifdef __APPLE__
void keyReleaseEvent(QKeyEvent *event) override;
#endif
void mousePressEvent(QMouseEvent *event) override;

void HandleNewKey(obs_key_combination_t new_key);
void RenderKey();

public slots:
void HandleNewKey(obs_key_combination_t new_key);
void ReloadKeyLayout();
void ResetKey();
void ClearKey();

signals:
void KeyChanged(obs_key_combination_t);
void SearchKey(obs_key_combination_t);
};

class OBSHotkeyWidget : public QWidget {
Q_OBJECT;

public:
OBSHotkeyWidget(obs_hotkey_id id, std::string name,
const std::vector<obs_key_combination_t> &combos = {},
QWidget *parent = nullptr)
: QWidget(parent),
OBSBasicSettings *settings,
const std::vector<obs_key_combination_t> &combos = {})
: QWidget(nullptr),
id(id),
name(name),
bindingsChanged(obs_get_signal_handler(),
"hotkey_bindings_changed",
&OBSHotkeyWidget::BindingsChanged, this)
&OBSHotkeyWidget::BindingsChanged, this),
settings(settings)
{
auto layout = new QVBoxLayout;
layout->setSpacing(0);
Expand All @@ -121,6 +144,7 @@ class OBSHotkeyWidget : public QWidget {
bool Changed() const;

QPointer<OBSHotkeyLabel> label;
std::vector<QPointer<OBSHotkeyEdit>> edits;

QString toolTip;
void setToolTip(const QString &toolTip_)
Expand Down Expand Up @@ -148,11 +172,11 @@ class OBSHotkeyWidget : public QWidget {

static void BindingsChanged(void *data, calldata_t *param);

std::vector<QPointer<OBSHotkeyEdit>> edits;
std::vector<QPointer<QPushButton>> removeButtons;
std::vector<QPointer<QPushButton>> revertButtons;
OBSSignal bindingsChanged;
bool ignoreChangedBindings = false;
OBSBasicSettings *settings;

QVBoxLayout *layout() const
{
Expand All @@ -164,4 +188,5 @@ private slots:

signals:
void KeyChanged();
void SearchKey(obs_key_combination_t);
};
Loading

0 comments on commit 793eb40

Please sign in to comment.