hotkey-edit.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_MacShowFocusRect, true);
  47. InitSignalHandler();
  48. ResetKey();
  49. }
  50. obs_key_combination_t original;
  51. obs_key_combination_t key;
  52. bool changed = false;
  53. protected:
  54. OBSSignal layoutChanged;
  55. void InitSignalHandler();
  56. void keyPressEvent(QKeyEvent *event) override;
  57. #ifdef __APPLE__
  58. void keyReleaseEvent(QKeyEvent *event) override;
  59. #endif
  60. void mousePressEvent(QMouseEvent *event) override;
  61. void HandleNewKey(obs_key_combination_t new_key);
  62. void RenderKey();
  63. public slots:
  64. void ReloadKeyLayout();
  65. void ResetKey();
  66. void ClearKey();
  67. signals:
  68. void KeyChanged(obs_key_combination_t);
  69. };
  70. class OBSHotkeyWidget : public QWidget {
  71. Q_OBJECT;
  72. public:
  73. OBSHotkeyWidget(obs_hotkey_id id, std::string name,
  74. const std::vector<obs_key_combination_t> &combos={},
  75. QWidget *parent=nullptr)
  76. : QWidget(parent),
  77. id(id),
  78. name(name),
  79. bindingsChanged(obs_get_signal_handler(),
  80. "hotkey_bindings_changed",
  81. &OBSHotkeyWidget::BindingsChanged,
  82. this)
  83. {
  84. auto layout = new QVBoxLayout;
  85. layout->setSpacing(0);
  86. layout->setMargin(0);
  87. setLayout(layout);
  88. SetKeyCombinations(combos);
  89. }
  90. void SetKeyCombinations(const std::vector<obs_key_combination_t>&);
  91. obs_hotkey_id id;
  92. std::string name;
  93. bool changed = false;
  94. bool Changed() const;
  95. QPointer<OBSHotkeyLabel> label;
  96. QString toolTip;
  97. void setToolTip(const QString &toolTip_)
  98. {
  99. toolTip = toolTip_;
  100. for (auto &edit : edits)
  101. edit->setToolTip(toolTip_);
  102. }
  103. void Apply();
  104. void GetCombinations(std::vector<obs_key_combination_t>&) const;
  105. void Save();
  106. void Save(std::vector<obs_key_combination_t> &combinations);
  107. void enterEvent(QEvent *event) override;
  108. void leaveEvent(QEvent *event) override;
  109. private:
  110. void AddEdit(obs_key_combination combo, int idx=-1);
  111. void RemoveEdit(size_t idx, bool signal=true);
  112. static void BindingsChanged(void *data, calldata_t *param);
  113. std::vector<QPointer<OBSHotkeyEdit>> edits;
  114. std::vector<QPointer<QPushButton>> removeButtons;
  115. std::vector<QPointer<QPushButton>> revertButtons;
  116. OBSSignal bindingsChanged;
  117. bool ignoreChangedBindings = false;
  118. QVBoxLayout *layout() const
  119. {
  120. return dynamic_cast<QVBoxLayout*>(QWidget::layout());
  121. }
  122. private slots:
  123. void HandleChangedBindings(obs_hotkey_id id_);
  124. signals:
  125. void KeyChanged();
  126. };