hotkey-edit.hpp 4.0 KB

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