yolobs-studio/UI/hotkey-edit.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

473 lines
10 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2014-2015 by Ruwen Hahn <palana@stunned.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "hotkey-edit.hpp"
#include <util/dstr.hpp>
#include <QPointer>
#include <QStyle>
#include "obs-app.hpp"
#include "qt-wrappers.hpp"
static inline bool operator!=(const obs_key_combination_t &c1,
2019-09-22 21:19:10 +00:00
const obs_key_combination_t &c2)
2016-02-23 23:16:51 +00:00
{
return c1.modifiers != c2.modifiers || c1.key != c2.key;
}
static inline bool operator==(const obs_key_combination_t &c1,
2019-09-22 21:19:10 +00:00
const obs_key_combination_t &c2)
2016-02-23 23:16:51 +00:00
{
return !(c1 != c2);
}
void OBSHotkeyEdit::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat())
return;
obs_key_combination_t new_key;
switch (event->key()) {
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Alt:
case Qt::Key_Meta:
new_key.key = OBS_KEY_NONE;
break;
#ifdef __APPLE__
case Qt::Key_CapsLock:
// kVK_CapsLock == 57
new_key.key = obs_key_from_virtual_key(57);
break;
#endif
default:
new_key.key =
obs_key_from_virtual_key(event->nativeVirtualKey());
}
new_key.modifiers =
TranslateQtKeyboardEventModifiers(event->modifiers());
HandleNewKey(new_key);
}
#ifdef __APPLE__
void OBSHotkeyEdit::keyReleaseEvent(QKeyEvent *event)
{
if (event->isAutoRepeat())
return;
if (event->key() != Qt::Key_CapsLock)
return;
obs_key_combination_t new_key;
// kVK_CapsLock == 57
new_key.key = obs_key_from_virtual_key(57);
new_key.modifiers =
TranslateQtKeyboardEventModifiers(event->modifiers());
HandleNewKey(new_key);
}
#endif
void OBSHotkeyEdit::mousePressEvent(QMouseEvent *event)
{
obs_key_combination_t new_key;
switch (event->button()) {
case Qt::NoButton:
case Qt::LeftButton:
case Qt::RightButton:
case Qt::AllButtons:
case Qt::MouseButtonMask:
return;
case Qt::MidButton:
new_key.key = OBS_KEY_MOUSE3;
break;
2019-09-22 21:19:10 +00:00
#define MAP_BUTTON(i, j) \
case Qt::ExtraButton##i: \
new_key.key = OBS_KEY_MOUSE##j; \
break;
MAP_BUTTON(1, 4)
MAP_BUTTON(2, 5)
MAP_BUTTON(3, 6)
MAP_BUTTON(4, 7)
MAP_BUTTON(5, 8)
MAP_BUTTON(6, 9)
MAP_BUTTON(7, 10)
MAP_BUTTON(8, 11)
MAP_BUTTON(9, 12)
MAP_BUTTON(10, 13)
MAP_BUTTON(11, 14)
MAP_BUTTON(12, 15)
MAP_BUTTON(13, 16)
MAP_BUTTON(14, 17)
MAP_BUTTON(15, 18)
MAP_BUTTON(16, 19)
MAP_BUTTON(17, 20)
MAP_BUTTON(18, 21)
MAP_BUTTON(19, 22)
MAP_BUTTON(20, 23)
MAP_BUTTON(21, 24)
MAP_BUTTON(22, 25)
MAP_BUTTON(23, 26)
MAP_BUTTON(24, 27)
2016-02-23 23:16:51 +00:00
#undef MAP_BUTTON
}
new_key.modifiers =
TranslateQtKeyboardEventModifiers(event->modifiers());
HandleNewKey(new_key);
}
void OBSHotkeyEdit::HandleNewKey(obs_key_combination_t new_key)
{
if (new_key == key || obs_key_combination_is_empty(new_key))
return;
key = new_key;
changed = true;
emit KeyChanged(key);
RenderKey();
}
void OBSHotkeyEdit::RenderKey()
{
DStr str;
obs_key_combination_to_str(key, str);
setText(QT_UTF8(str));
}
void OBSHotkeyEdit::ResetKey()
{
key = original;
changed = false;
emit KeyChanged(key);
RenderKey();
}
void OBSHotkeyEdit::ClearKey()
{
key = {0, OBS_KEY_NONE};
changed = true;
emit KeyChanged(key);
RenderKey();
}
void OBSHotkeyEdit::InitSignalHandler()
{
2019-09-22 21:19:10 +00:00
layoutChanged = {
obs_get_signal_handler(), "hotkey_layout_change",
[](void *this_, calldata_t *) {
auto edit = static_cast<OBSHotkeyEdit *>(this_);
QMetaObject::invokeMethod(edit, "ReloadKeyLayout");
},
this};
2016-02-23 23:16:51 +00:00
}
void OBSHotkeyEdit::ReloadKeyLayout()
{
RenderKey();
}
void OBSHotkeyWidget::SetKeyCombinations(
2019-09-22 21:19:10 +00:00
const std::vector<obs_key_combination_t> &combos)
2016-02-23 23:16:51 +00:00
{
if (combos.empty())
AddEdit({0, OBS_KEY_NONE});
for (auto combo : combos)
AddEdit(combo);
}
bool OBSHotkeyWidget::Changed() const
{
return changed ||
2019-09-22 21:19:10 +00:00
std::any_of(begin(edits), end(edits),
[](OBSHotkeyEdit *edit) { return edit->changed; });
2016-02-23 23:16:51 +00:00
}
void OBSHotkeyWidget::Apply()
{
for (auto &edit : edits) {
edit->original = edit->key;
edit->changed = false;
}
changed = false;
for (auto &revertButton : revertButtons)
revertButton->setEnabled(false);
}
void OBSHotkeyWidget::GetCombinations(
2019-09-22 21:19:10 +00:00
std::vector<obs_key_combination_t> &combinations) const
2016-02-23 23:16:51 +00:00
{
combinations.clear();
for (auto &edit : edits)
if (!obs_key_combination_is_empty(edit->key))
combinations.emplace_back(edit->key);
}
void OBSHotkeyWidget::Save()
{
std::vector<obs_key_combination_t> combinations;
Save(combinations);
}
void OBSHotkeyWidget::Save(std::vector<obs_key_combination_t> &combinations)
{
GetCombinations(combinations);
Apply();
2019-09-22 21:19:10 +00:00
auto AtomicUpdate = [&]() {
2016-02-23 23:16:51 +00:00
ignoreChangedBindings = true;
2019-09-22 21:19:10 +00:00
obs_hotkey_load_bindings(id, combinations.data(),
combinations.size());
2016-02-23 23:16:51 +00:00
ignoreChangedBindings = false;
};
using AtomicUpdate_t = decltype(&AtomicUpdate);
2019-09-22 21:19:10 +00:00
obs_hotkey_update_atomic(
[](void *d) { (*static_cast<AtomicUpdate_t>(d))(); },
static_cast<void *>(&AtomicUpdate));
2016-02-23 23:16:51 +00:00
}
void OBSHotkeyWidget::AddEdit(obs_key_combination combo, int idx)
{
auto edit = new OBSHotkeyEdit(combo);
edit->setToolTip(toolTip);
auto revert = new QPushButton;
2019-07-27 12:47:10 +00:00
revert->setProperty("themeID", "revertIcon");
revert->setToolTip(QTStr("Revert"));
revert->setFixedSize(24, 24);
revert->setFlat(true);
2016-02-23 23:16:51 +00:00
revert->setEnabled(false);
auto clear = new QPushButton;
2019-07-27 12:47:10 +00:00
clear->setProperty("themeID", "trashIcon");
clear->setToolTip(QTStr("Clear"));
clear->setFixedSize(24, 24);
clear->setFlat(true);
2016-02-23 23:16:51 +00:00
clear->setEnabled(!obs_key_combination_is_empty(combo));
2019-09-22 21:19:10 +00:00
QObject::connect(
edit, &OBSHotkeyEdit::KeyChanged,
[=](obs_key_combination_t new_combo) {
clear->setEnabled(
!obs_key_combination_is_empty(new_combo));
revert->setEnabled(edit->original != new_combo);
});
2016-02-23 23:16:51 +00:00
auto add = new QPushButton;
2019-07-27 12:47:10 +00:00
add->setProperty("themeID", "addIconSmall");
add->setFixedSize(24, 24);
add->setFlat(true);
2016-02-23 23:16:51 +00:00
auto remove = new QPushButton;
2019-07-27 12:47:10 +00:00
remove->setProperty("themeID", "removeIconSmall");
2016-02-23 23:16:51 +00:00
remove->setEnabled(removeButtons.size() > 0);
2019-07-27 12:47:10 +00:00
remove->setFixedSize(24, 24);
remove->setFlat(true);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
auto CurrentIndex = [&, remove] {
auto res = std::find(begin(removeButtons), end(removeButtons),
remove);
2016-02-23 23:16:51 +00:00
return std::distance(begin(removeButtons), res);
};
2019-09-22 21:19:10 +00:00
QObject::connect(add, &QPushButton::clicked, [&, CurrentIndex] {
2016-02-23 23:16:51 +00:00
AddEdit({0, OBS_KEY_NONE}, CurrentIndex() + 1);
});
QObject::connect(remove, &QPushButton::clicked,
2019-09-22 21:19:10 +00:00
[&, CurrentIndex] { RemoveEdit(CurrentIndex()); });
2016-02-23 23:16:51 +00:00
QHBoxLayout *subLayout = new QHBoxLayout;
2019-07-27 12:47:10 +00:00
subLayout->setContentsMargins(0, 4, 0, 0);
2016-02-23 23:16:51 +00:00
subLayout->addWidget(edit);
subLayout->addWidget(revert);
subLayout->addWidget(clear);
subLayout->addWidget(add);
subLayout->addWidget(remove);
if (removeButtons.size() == 1)
removeButtons.front()->setEnabled(true);
if (idx != -1) {
revertButtons.insert(begin(revertButtons) + idx, revert);
removeButtons.insert(begin(removeButtons) + idx, remove);
edits.insert(begin(edits) + idx, edit);
} else {
revertButtons.emplace_back(revert);
removeButtons.emplace_back(remove);
edits.emplace_back(edit);
}
layout()->insertLayout(idx, subLayout);
2019-09-22 21:19:10 +00:00
QObject::connect(revert, &QPushButton::clicked, edit,
&OBSHotkeyEdit::ResetKey);
QObject::connect(clear, &QPushButton::clicked, edit,
&OBSHotkeyEdit::ClearKey);
2016-02-23 23:16:51 +00:00
QObject::connect(edit, &OBSHotkeyEdit::KeyChanged,
2019-09-22 21:19:10 +00:00
[&](obs_key_combination) { emit KeyChanged(); });
2016-02-23 23:16:51 +00:00
}
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);
removeButtons.erase(begin(removeButtons) + idx);
edits.erase(begin(edits) + idx);
auto item = layout()->takeAt(static_cast<int>(idx));
QLayoutItem *child = nullptr;
while ((child = item->layout()->takeAt(0))) {
delete child->widget();
delete child;
}
delete item;
if (removeButtons.size() == 1)
removeButtons.front()->setEnabled(false);
}
void OBSHotkeyWidget::BindingsChanged(void *data, calldata_t *param)
{
2019-09-22 21:19:10 +00:00
auto widget = static_cast<OBSHotkeyWidget *>(data);
auto key = static_cast<obs_hotkey_t *>(calldata_ptr(param, "key"));
2016-02-23 23:16:51 +00:00
QMetaObject::invokeMethod(widget, "HandleChangedBindings",
2019-09-22 21:19:10 +00:00
Q_ARG(obs_hotkey_id, obs_hotkey_get_id(key)));
2016-02-23 23:16:51 +00:00
}
void OBSHotkeyWidget::HandleChangedBindings(obs_hotkey_id id_)
{
2019-09-22 21:19:10 +00:00
if (ignoreChangedBindings || id != id_)
return;
2016-02-23 23:16:51 +00:00
std::vector<obs_key_combination_t> bindings;
2019-09-22 21:19:10 +00:00
auto LoadBindings = [&](obs_hotkey_binding_t *binding) {
if (obs_hotkey_binding_get_hotkey_id(binding) != id)
return;
2016-02-23 23:16:51 +00:00
auto get_combo = obs_hotkey_binding_get_key_combination;
bindings.push_back(get_combo(binding));
};
using LoadBindings_t = decltype(&LoadBindings);
2019-09-22 21:19:10 +00:00
obs_enum_hotkey_bindings(
[](void *data, size_t, obs_hotkey_binding_t *binding) {
auto LoadBindings = *static_cast<LoadBindings_t>(data);
LoadBindings(binding);
return true;
},
static_cast<void *>(&LoadBindings));
2016-02-23 23:16:51 +00:00
while (edits.size() > 0)
RemoveEdit(edits.size() - 1, false);
SetKeyCombinations(bindings);
}
static inline void updateStyle(QWidget *widget)
{
auto style = widget->style();
style->unpolish(widget);
style->polish(widget);
widget->update();
}
void OBSHotkeyWidget::enterEvent(QEvent *event)
{
if (!label)
return;
event->accept();
label->highlightPair(true);
}
void OBSHotkeyWidget::leaveEvent(QEvent *event)
{
if (!label)
return;
event->accept();
label->highlightPair(false);
}
void OBSHotkeyLabel::highlightPair(bool highlight)
{
if (!pairPartner)
return;
pairPartner->setProperty("hotkeyPairHover", highlight);
updateStyle(pairPartner);
setProperty("hotkeyPairHover", highlight);
updateStyle(this);
}
void OBSHotkeyLabel::enterEvent(QEvent *event)
{
if (!pairPartner)
return;
event->accept();
highlightPair(true);
}
void OBSHotkeyLabel::leaveEvent(QEvent *event)
{
if (!pairPartner)
return;
event->accept();
highlightPair(false);
}
void OBSHotkeyLabel::setToolTip(const QString &toolTip)
{
QLabel::setToolTip(toolTip);
if (widget)
widget->setToolTip(toolTip);
}