hotkey-edit.hpp 4.0 KB

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