OBSHotkeyEdit.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <obs.hpp>
  16. #include <QLineEdit>
  17. class OBSBasicSettings;
  18. class QWidget;
  19. static inline bool operator!=(const obs_key_combination_t &c1, const obs_key_combination_t &c2)
  20. {
  21. return c1.modifiers != c2.modifiers || c1.key != c2.key;
  22. }
  23. static inline bool operator==(const obs_key_combination_t &c1, const obs_key_combination_t &c2)
  24. {
  25. return !(c1 != c2);
  26. }
  27. class OBSHotkeyEdit : public QLineEdit {
  28. Q_OBJECT;
  29. public:
  30. OBSHotkeyEdit(QWidget *parent, obs_key_combination_t original, OBSBasicSettings *settings)
  31. : QLineEdit(parent),
  32. original(original),
  33. settings(settings)
  34. {
  35. #ifdef __APPLE__
  36. // disable the input cursor on OSX, focus should be clear
  37. // enough with the default focus frame
  38. setReadOnly(true);
  39. #endif
  40. setAttribute(Qt::WA_InputMethodEnabled, false);
  41. setAttribute(Qt::WA_MacShowFocusRect, true);
  42. InitSignalHandler();
  43. ResetKey();
  44. }
  45. OBSHotkeyEdit(QWidget *parent = nullptr) : QLineEdit(parent), original({}), settings(nullptr)
  46. {
  47. #ifdef __APPLE__
  48. // disable the input cursor on OSX, focus should be clear
  49. // enough with the default focus frame
  50. setReadOnly(true);
  51. #endif
  52. setAttribute(Qt::WA_InputMethodEnabled, false);
  53. setAttribute(Qt::WA_MacShowFocusRect, true);
  54. InitSignalHandler();
  55. ResetKey();
  56. }
  57. obs_key_combination_t original;
  58. obs_key_combination_t key;
  59. OBSBasicSettings *settings;
  60. bool changed = false;
  61. void UpdateDuplicationState();
  62. bool hasDuplicate = false;
  63. QVariant inputMethodQuery(Qt::InputMethodQuery) const override;
  64. protected:
  65. OBSSignal layoutChanged;
  66. QAction *dupeIcon = nullptr;
  67. void InitSignalHandler();
  68. void CreateDupeIcon();
  69. void keyPressEvent(QKeyEvent *event) override;
  70. #ifdef __APPLE__
  71. void keyReleaseEvent(QKeyEvent *event) override;
  72. #endif
  73. void mousePressEvent(QMouseEvent *event) override;
  74. void RenderKey();
  75. public slots:
  76. void HandleNewKey(obs_key_combination_t new_key);
  77. void ReloadKeyLayout();
  78. void ResetKey();
  79. void ClearKey();
  80. signals:
  81. void KeyChanged(obs_key_combination_t);
  82. void SearchKey(obs_key_combination_t);
  83. };