1
0

hotkey-edit.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. static inline bool operator!=(const obs_key_combination_t &c1,
  24. const obs_key_combination_t &c2)
  25. {
  26. return c1.modifiers != c2.modifiers || c1.key != c2.key;
  27. }
  28. static inline bool operator==(const obs_key_combination_t &c1,
  29. const obs_key_combination_t &c2)
  30. {
  31. return !(c1 != c2);
  32. }
  33. class OBSBasicSettings;
  34. class OBSHotkeyWidget;
  35. class OBSHotkeyLabel : public QLabel {
  36. Q_OBJECT
  37. public:
  38. QPointer<OBSHotkeyLabel> pairPartner;
  39. QPointer<OBSHotkeyWidget> widget;
  40. void highlightPair(bool highlight);
  41. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  42. void enterEvent(QEnterEvent *event) override;
  43. #else
  44. void enterEvent(QEvent *event) override;
  45. #endif
  46. void leaveEvent(QEvent *event) override;
  47. void setToolTip(const QString &toolTip);
  48. };
  49. class OBSHotkeyEdit : public QLineEdit {
  50. Q_OBJECT;
  51. public:
  52. OBSHotkeyEdit(QWidget *parent, obs_key_combination_t original,
  53. OBSBasicSettings *settings)
  54. : QLineEdit(parent), original(original), settings(settings)
  55. {
  56. #ifdef __APPLE__
  57. // disable the input cursor on OSX, focus should be clear
  58. // enough with the default focus frame
  59. setReadOnly(true);
  60. #endif
  61. setAttribute(Qt::WA_InputMethodEnabled, false);
  62. setAttribute(Qt::WA_MacShowFocusRect, true);
  63. InitSignalHandler();
  64. CreateDupeIcon();
  65. ResetKey();
  66. }
  67. OBSHotkeyEdit(QWidget *parent = nullptr)
  68. : QLineEdit(parent), original({}), settings(nullptr)
  69. {
  70. #ifdef __APPLE__
  71. // disable the input cursor on OSX, focus should be clear
  72. // enough with the default focus frame
  73. setReadOnly(true);
  74. #endif
  75. setAttribute(Qt::WA_InputMethodEnabled, false);
  76. setAttribute(Qt::WA_MacShowFocusRect, true);
  77. InitSignalHandler();
  78. ResetKey();
  79. }
  80. obs_key_combination_t original;
  81. obs_key_combination_t key;
  82. OBSBasicSettings *settings;
  83. bool changed = false;
  84. void UpdateDuplicationState();
  85. bool hasDuplicate = false;
  86. protected:
  87. OBSSignal layoutChanged;
  88. QAction *dupeIcon;
  89. void InitSignalHandler();
  90. void CreateDupeIcon();
  91. void keyPressEvent(QKeyEvent *event) override;
  92. #ifdef __APPLE__
  93. void keyReleaseEvent(QKeyEvent *event) override;
  94. #endif
  95. void mousePressEvent(QMouseEvent *event) override;
  96. void RenderKey();
  97. public slots:
  98. void HandleNewKey(obs_key_combination_t new_key);
  99. void ReloadKeyLayout();
  100. void ResetKey();
  101. void ClearKey();
  102. signals:
  103. void KeyChanged(obs_key_combination_t);
  104. void SearchKey(obs_key_combination_t);
  105. };
  106. class OBSHotkeyWidget : public QWidget {
  107. Q_OBJECT;
  108. public:
  109. OBSHotkeyWidget(QWidget *parent, obs_hotkey_id id, std::string name,
  110. OBSBasicSettings *settings,
  111. const std::vector<obs_key_combination_t> &combos = {})
  112. : QWidget(parent),
  113. id(id),
  114. name(name),
  115. bindingsChanged(obs_get_signal_handler(),
  116. "hotkey_bindings_changed",
  117. &OBSHotkeyWidget::BindingsChanged, this),
  118. settings(settings)
  119. {
  120. auto layout = new QVBoxLayout;
  121. layout->setSpacing(0);
  122. layout->setContentsMargins(0, 0, 0, 0);
  123. setLayout(layout);
  124. SetKeyCombinations(combos);
  125. }
  126. void SetKeyCombinations(const std::vector<obs_key_combination_t> &);
  127. obs_hotkey_id id;
  128. std::string name;
  129. bool changed = false;
  130. bool Changed() const;
  131. QPointer<OBSHotkeyLabel> label;
  132. std::vector<QPointer<OBSHotkeyEdit>> edits;
  133. QString toolTip;
  134. void setToolTip(const QString &toolTip_)
  135. {
  136. toolTip = toolTip_;
  137. for (auto &edit : edits)
  138. edit->setToolTip(toolTip_);
  139. }
  140. void Apply();
  141. void GetCombinations(std::vector<obs_key_combination_t> &) const;
  142. void Save();
  143. void Save(std::vector<obs_key_combination_t> &combinations);
  144. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  145. void enterEvent(QEnterEvent *event) override;
  146. #else
  147. void enterEvent(QEvent *event) override;
  148. #endif
  149. void leaveEvent(QEvent *event) override;
  150. private:
  151. void AddEdit(obs_key_combination combo, int idx = -1);
  152. void RemoveEdit(size_t idx, bool signal = true);
  153. static void BindingsChanged(void *data, calldata_t *param);
  154. std::vector<QPointer<QPushButton>> removeButtons;
  155. std::vector<QPointer<QPushButton>> revertButtons;
  156. OBSSignal bindingsChanged;
  157. bool ignoreChangedBindings = false;
  158. OBSBasicSettings *settings;
  159. QVBoxLayout *layout() const
  160. {
  161. return dynamic_cast<QVBoxLayout *>(QWidget::layout());
  162. }
  163. private slots:
  164. void HandleChangedBindings(obs_hotkey_id id_);
  165. signals:
  166. void KeyChanged();
  167. void SearchKey(obs_key_combination_t);
  168. };