hotkey-edit.hpp 5.0 KB

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