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. #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. void enterEvent(QEvent *event) override;
  31. void leaveEvent(QEvent *event) override;
  32. void setToolTip(const QString &toolTip);
  33. };
  34. class OBSHotkeyEdit : public QLineEdit {
  35. Q_OBJECT;
  36. public:
  37. OBSHotkeyEdit(obs_key_combination_t original, QWidget *parent = nullptr)
  38. : QLineEdit(parent), original(original)
  39. {
  40. #ifdef __APPLE__
  41. // disable the input cursor on OSX, focus should be clear
  42. // enough with the default focus frame
  43. setReadOnly(true);
  44. #endif
  45. setAttribute(Qt::WA_InputMethodEnabled, false);
  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, this)
  82. {
  83. auto layout = new QVBoxLayout;
  84. layout->setSpacing(0);
  85. layout->setMargin(0);
  86. setLayout(layout);
  87. SetKeyCombinations(combos);
  88. }
  89. void SetKeyCombinations(const std::vector<obs_key_combination_t> &);
  90. obs_hotkey_id id;
  91. std::string name;
  92. bool changed = false;
  93. bool Changed() const;
  94. QPointer<OBSHotkeyLabel> label;
  95. QString toolTip;
  96. void setToolTip(const QString &toolTip_)
  97. {
  98. toolTip = toolTip_;
  99. for (auto &edit : edits)
  100. edit->setToolTip(toolTip_);
  101. }
  102. void Apply();
  103. void GetCombinations(std::vector<obs_key_combination_t> &) const;
  104. void Save();
  105. void Save(std::vector<obs_key_combination_t> &combinations);
  106. void enterEvent(QEvent *event) override;
  107. void leaveEvent(QEvent *event) override;
  108. private:
  109. void AddEdit(obs_key_combination combo, int idx = -1);
  110. void RemoveEdit(size_t idx, bool signal = true);
  111. static void BindingsChanged(void *data, calldata_t *param);
  112. std::vector<QPointer<OBSHotkeyEdit>> edits;
  113. std::vector<QPointer<QPushButton>> removeButtons;
  114. std::vector<QPointer<QPushButton>> revertButtons;
  115. OBSSignal bindingsChanged;
  116. bool ignoreChangedBindings = false;
  117. QVBoxLayout *layout() const
  118. {
  119. return dynamic_cast<QVBoxLayout *>(QWidget::layout());
  120. }
  121. private slots:
  122. void HandleChangedBindings(obs_hotkey_id id_);
  123. signals:
  124. void KeyChanged();
  125. };