hotkey-edit.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /******************************************************************************
  2. Copyright (C) 2014-2015 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <QLineEdit>
  16. #include <QKeyEvent>
  17. #include <QPushButton>
  18. #include <QVBoxLayout>
  19. #include <QWidget>
  20. #include <QPointer>
  21. #include <QLabel>
  22. #include <obs.hpp>
  23. class OBSHotkeyWidget;
  24. class OBSHotkeyLabel : public QLabel {
  25. Q_OBJECT
  26. public:
  27. QPointer<OBSHotkeyLabel> pairPartner;
  28. QPointer<OBSHotkeyWidget> widget;
  29. void highlightPair(bool highlight);
  30. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  31. void enterEvent(QEnterEvent *event) override;
  32. #else
  33. void enterEvent(QEvent *event) override;
  34. #endif
  35. void leaveEvent(QEvent *event) override;
  36. void setToolTip(const QString &toolTip);
  37. };
  38. class OBSHotkeyEdit : public QLineEdit {
  39. Q_OBJECT;
  40. public:
  41. OBSHotkeyEdit(obs_key_combination_t original, QWidget *parent = nullptr)
  42. : QLineEdit(parent), original(original)
  43. {
  44. #ifdef __APPLE__
  45. // disable the input cursor on OSX, focus should be clear
  46. // enough with the default focus frame
  47. setReadOnly(true);
  48. #endif
  49. setAttribute(Qt::WA_InputMethodEnabled, false);
  50. setAttribute(Qt::WA_MacShowFocusRect, true);
  51. InitSignalHandler();
  52. ResetKey();
  53. }
  54. obs_key_combination_t original;
  55. obs_key_combination_t key;
  56. bool changed = false;
  57. protected:
  58. OBSSignal layoutChanged;
  59. void InitSignalHandler();
  60. void keyPressEvent(QKeyEvent *event) override;
  61. #ifdef __APPLE__
  62. void keyReleaseEvent(QKeyEvent *event) override;
  63. #endif
  64. void mousePressEvent(QMouseEvent *event) override;
  65. void HandleNewKey(obs_key_combination_t new_key);
  66. void RenderKey();
  67. public slots:
  68. void ReloadKeyLayout();
  69. void ResetKey();
  70. void ClearKey();
  71. signals:
  72. void KeyChanged(obs_key_combination_t);
  73. };
  74. class OBSHotkeyWidget : public QWidget {
  75. Q_OBJECT;
  76. public:
  77. OBSHotkeyWidget(obs_hotkey_id id, std::string name,
  78. const std::vector<obs_key_combination_t> &combos = {},
  79. QWidget *parent = nullptr)
  80. : QWidget(parent),
  81. id(id),
  82. name(name),
  83. bindingsChanged(obs_get_signal_handler(),
  84. "hotkey_bindings_changed",
  85. &OBSHotkeyWidget::BindingsChanged, this)
  86. {
  87. auto layout = new QVBoxLayout;
  88. layout->setSpacing(0);
  89. layout->setContentsMargins(0, 0, 0, 0);
  90. setLayout(layout);
  91. SetKeyCombinations(combos);
  92. }
  93. void SetKeyCombinations(const std::vector<obs_key_combination_t> &);
  94. obs_hotkey_id id;
  95. std::string name;
  96. bool changed = false;
  97. bool Changed() const;
  98. QPointer<OBSHotkeyLabel> label;
  99. QString toolTip;
  100. void setToolTip(const QString &toolTip_)
  101. {
  102. toolTip = toolTip_;
  103. for (auto &edit : edits)
  104. edit->setToolTip(toolTip_);
  105. }
  106. void Apply();
  107. void GetCombinations(std::vector<obs_key_combination_t> &) const;
  108. void Save();
  109. void Save(std::vector<obs_key_combination_t> &combinations);
  110. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  111. void enterEvent(QEnterEvent *event) override;
  112. #else
  113. void enterEvent(QEvent *event) override;
  114. #endif
  115. void leaveEvent(QEvent *event) override;
  116. private:
  117. void AddEdit(obs_key_combination combo, int idx = -1);
  118. void RemoveEdit(size_t idx, bool signal = true);
  119. static void BindingsChanged(void *data, calldata_t *param);
  120. std::vector<QPointer<OBSHotkeyEdit>> edits;
  121. std::vector<QPointer<QPushButton>> removeButtons;
  122. std::vector<QPointer<QPushButton>> revertButtons;
  123. OBSSignal bindingsChanged;
  124. bool ignoreChangedBindings = false;
  125. QVBoxLayout *layout() const
  126. {
  127. return dynamic_cast<QVBoxLayout *>(QWidget::layout());
  128. }
  129. private slots:
  130. void HandleChangedBindings(obs_hotkey_id id_);
  131. signals:
  132. void KeyChanged();
  133. };