hotkey-edit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 "window-basic-settings.hpp"
  15. #include "hotkey-edit.hpp"
  16. #include <util/dstr.hpp>
  17. #include <QPointer>
  18. #include <QStyle>
  19. #include <QAction>
  20. #include "obs-app.hpp"
  21. #include "qt-wrappers.hpp"
  22. void OBSHotkeyEdit::keyPressEvent(QKeyEvent *event)
  23. {
  24. if (event->isAutoRepeat())
  25. return;
  26. obs_key_combination_t new_key;
  27. switch (event->key()) {
  28. case Qt::Key_Shift:
  29. case Qt::Key_Control:
  30. case Qt::Key_Alt:
  31. case Qt::Key_Meta:
  32. new_key.key = OBS_KEY_NONE;
  33. break;
  34. #ifdef __APPLE__
  35. case Qt::Key_CapsLock:
  36. // kVK_CapsLock == 57
  37. new_key.key = obs_key_from_virtual_key(57);
  38. break;
  39. #endif
  40. default:
  41. new_key.key =
  42. obs_key_from_virtual_key(event->nativeVirtualKey());
  43. }
  44. new_key.modifiers =
  45. TranslateQtKeyboardEventModifiers(event->modifiers());
  46. HandleNewKey(new_key);
  47. }
  48. QVariant OBSHotkeyEdit::inputMethodQuery(Qt::InputMethodQuery query) const
  49. {
  50. if (query == Qt::ImEnabled) {
  51. return false;
  52. } else {
  53. return QLineEdit::inputMethodQuery(query);
  54. }
  55. }
  56. #ifdef __APPLE__
  57. void OBSHotkeyEdit::keyReleaseEvent(QKeyEvent *event)
  58. {
  59. if (event->isAutoRepeat())
  60. return;
  61. if (event->key() != Qt::Key_CapsLock)
  62. return;
  63. obs_key_combination_t new_key;
  64. // kVK_CapsLock == 57
  65. new_key.key = obs_key_from_virtual_key(57);
  66. new_key.modifiers =
  67. TranslateQtKeyboardEventModifiers(event->modifiers());
  68. HandleNewKey(new_key);
  69. }
  70. #endif
  71. void OBSHotkeyEdit::mousePressEvent(QMouseEvent *event)
  72. {
  73. obs_key_combination_t new_key;
  74. switch (event->button()) {
  75. case Qt::NoButton:
  76. case Qt::LeftButton:
  77. case Qt::RightButton:
  78. case Qt::AllButtons:
  79. case Qt::MouseButtonMask:
  80. return;
  81. case Qt::MiddleButton:
  82. new_key.key = OBS_KEY_MOUSE3;
  83. break;
  84. #define MAP_BUTTON(i, j) \
  85. case Qt::ExtraButton##i: \
  86. new_key.key = OBS_KEY_MOUSE##j; \
  87. break;
  88. MAP_BUTTON(1, 4)
  89. MAP_BUTTON(2, 5)
  90. MAP_BUTTON(3, 6)
  91. MAP_BUTTON(4, 7)
  92. MAP_BUTTON(5, 8)
  93. MAP_BUTTON(6, 9)
  94. MAP_BUTTON(7, 10)
  95. MAP_BUTTON(8, 11)
  96. MAP_BUTTON(9, 12)
  97. MAP_BUTTON(10, 13)
  98. MAP_BUTTON(11, 14)
  99. MAP_BUTTON(12, 15)
  100. MAP_BUTTON(13, 16)
  101. MAP_BUTTON(14, 17)
  102. MAP_BUTTON(15, 18)
  103. MAP_BUTTON(16, 19)
  104. MAP_BUTTON(17, 20)
  105. MAP_BUTTON(18, 21)
  106. MAP_BUTTON(19, 22)
  107. MAP_BUTTON(20, 23)
  108. MAP_BUTTON(21, 24)
  109. MAP_BUTTON(22, 25)
  110. MAP_BUTTON(23, 26)
  111. MAP_BUTTON(24, 27)
  112. #undef MAP_BUTTON
  113. }
  114. new_key.modifiers =
  115. TranslateQtKeyboardEventModifiers(event->modifiers());
  116. HandleNewKey(new_key);
  117. }
  118. void OBSHotkeyEdit::HandleNewKey(obs_key_combination_t new_key)
  119. {
  120. if (new_key == key || obs_key_combination_is_empty(new_key))
  121. return;
  122. key = new_key;
  123. changed = true;
  124. emit KeyChanged(key);
  125. RenderKey();
  126. }
  127. void OBSHotkeyEdit::RenderKey()
  128. {
  129. DStr str;
  130. obs_key_combination_to_str(key, str);
  131. setText(QT_UTF8(str));
  132. }
  133. void OBSHotkeyEdit::ResetKey()
  134. {
  135. key = original;
  136. changed = false;
  137. emit KeyChanged(key);
  138. RenderKey();
  139. }
  140. void OBSHotkeyEdit::ClearKey()
  141. {
  142. key = {0, OBS_KEY_NONE};
  143. changed = true;
  144. emit KeyChanged(key);
  145. RenderKey();
  146. }
  147. void OBSHotkeyEdit::UpdateDuplicationState()
  148. {
  149. if (dupeIcon && dupeIcon->isVisible() != hasDuplicate) {
  150. dupeIcon->setVisible(hasDuplicate);
  151. update();
  152. }
  153. }
  154. void OBSHotkeyEdit::InitSignalHandler()
  155. {
  156. layoutChanged = {
  157. obs_get_signal_handler(), "hotkey_layout_change",
  158. [](void *this_, calldata_t *) {
  159. auto edit = static_cast<OBSHotkeyEdit *>(this_);
  160. QMetaObject::invokeMethod(edit, "ReloadKeyLayout");
  161. },
  162. this};
  163. }
  164. void OBSHotkeyEdit::CreateDupeIcon()
  165. {
  166. dupeIcon = addAction(settings->GetHotkeyConflictIcon(),
  167. ActionPosition::TrailingPosition);
  168. dupeIcon->setToolTip(QTStr("Basic.Settings.Hotkeys.DuplicateWarning"));
  169. QObject::connect(dupeIcon, &QAction::triggered,
  170. [=] { emit SearchKey(key); });
  171. dupeIcon->setVisible(false);
  172. }
  173. void OBSHotkeyEdit::ReloadKeyLayout()
  174. {
  175. RenderKey();
  176. }
  177. void OBSHotkeyWidget::SetKeyCombinations(
  178. const std::vector<obs_key_combination_t> &combos)
  179. {
  180. if (combos.empty())
  181. AddEdit({0, OBS_KEY_NONE});
  182. for (auto combo : combos)
  183. AddEdit(combo);
  184. }
  185. bool OBSHotkeyWidget::Changed() const
  186. {
  187. return changed ||
  188. std::any_of(begin(edits), end(edits),
  189. [](OBSHotkeyEdit *edit) { return edit->changed; });
  190. }
  191. void OBSHotkeyWidget::Apply()
  192. {
  193. for (auto &edit : edits) {
  194. edit->original = edit->key;
  195. edit->changed = false;
  196. }
  197. changed = false;
  198. for (auto &revertButton : revertButtons)
  199. revertButton->setEnabled(false);
  200. }
  201. void OBSHotkeyWidget::GetCombinations(
  202. std::vector<obs_key_combination_t> &combinations) const
  203. {
  204. combinations.clear();
  205. for (auto &edit : edits)
  206. if (!obs_key_combination_is_empty(edit->key))
  207. combinations.emplace_back(edit->key);
  208. }
  209. void OBSHotkeyWidget::Save()
  210. {
  211. std::vector<obs_key_combination_t> combinations;
  212. Save(combinations);
  213. }
  214. void OBSHotkeyWidget::Save(std::vector<obs_key_combination_t> &combinations)
  215. {
  216. GetCombinations(combinations);
  217. Apply();
  218. auto AtomicUpdate = [&]() {
  219. ignoreChangedBindings = true;
  220. obs_hotkey_load_bindings(id, combinations.data(),
  221. combinations.size());
  222. ignoreChangedBindings = false;
  223. };
  224. using AtomicUpdate_t = decltype(&AtomicUpdate);
  225. obs_hotkey_update_atomic(
  226. [](void *d) { (*static_cast<AtomicUpdate_t>(d))(); },
  227. static_cast<void *>(&AtomicUpdate));
  228. }
  229. void OBSHotkeyWidget::AddEdit(obs_key_combination combo, int idx)
  230. {
  231. auto edit = new OBSHotkeyEdit(parentWidget(), combo, settings);
  232. edit->setToolTip(toolTip);
  233. auto revert = new QPushButton;
  234. revert->setProperty("themeID", "revertIcon");
  235. revert->setToolTip(QTStr("Revert"));
  236. revert->setEnabled(false);
  237. auto clear = new QPushButton;
  238. clear->setProperty("themeID", "clearIconSmall");
  239. clear->setToolTip(QTStr("Clear"));
  240. clear->setEnabled(!obs_key_combination_is_empty(combo));
  241. QObject::connect(
  242. edit, &OBSHotkeyEdit::KeyChanged,
  243. [=](obs_key_combination_t new_combo) {
  244. clear->setEnabled(
  245. !obs_key_combination_is_empty(new_combo));
  246. revert->setEnabled(edit->original != new_combo);
  247. });
  248. auto add = new QPushButton;
  249. add->setProperty("themeID", "addIconSmall");
  250. add->setToolTip(QTStr("Add"));
  251. auto remove = new QPushButton;
  252. remove->setProperty("themeID", "removeIconSmall");
  253. remove->setToolTip(QTStr("Remove"));
  254. remove->setEnabled(removeButtons.size() > 0);
  255. auto CurrentIndex = [&, remove] {
  256. auto res = std::find(begin(removeButtons), end(removeButtons),
  257. remove);
  258. return std::distance(begin(removeButtons), res);
  259. };
  260. QObject::connect(add, &QPushButton::clicked, [&, CurrentIndex] {
  261. AddEdit({0, OBS_KEY_NONE}, CurrentIndex() + 1);
  262. });
  263. QObject::connect(remove, &QPushButton::clicked,
  264. [&, CurrentIndex] { RemoveEdit(CurrentIndex()); });
  265. QHBoxLayout *subLayout = new QHBoxLayout;
  266. subLayout->setContentsMargins(0, 4, 0, 0);
  267. subLayout->addWidget(edit);
  268. subLayout->addWidget(revert);
  269. subLayout->addWidget(clear);
  270. subLayout->addWidget(add);
  271. subLayout->addWidget(remove);
  272. if (removeButtons.size() == 1)
  273. removeButtons.front()->setEnabled(true);
  274. if (idx != -1) {
  275. revertButtons.insert(begin(revertButtons) + idx, revert);
  276. removeButtons.insert(begin(removeButtons) + idx, remove);
  277. edits.insert(begin(edits) + idx, edit);
  278. } else {
  279. revertButtons.emplace_back(revert);
  280. removeButtons.emplace_back(remove);
  281. edits.emplace_back(edit);
  282. }
  283. layout()->insertLayout(idx, subLayout);
  284. QObject::connect(revert, &QPushButton::clicked, edit,
  285. &OBSHotkeyEdit::ResetKey);
  286. QObject::connect(clear, &QPushButton::clicked, edit,
  287. &OBSHotkeyEdit::ClearKey);
  288. QObject::connect(edit, &OBSHotkeyEdit::KeyChanged,
  289. [&](obs_key_combination) { emit KeyChanged(); });
  290. QObject::connect(edit, &OBSHotkeyEdit::SearchKey,
  291. [=](obs_key_combination combo) {
  292. emit SearchKey(combo);
  293. });
  294. }
  295. void OBSHotkeyWidget::RemoveEdit(size_t idx, bool signal)
  296. {
  297. auto &edit = *(begin(edits) + idx);
  298. if (!obs_key_combination_is_empty(edit->original) && signal) {
  299. changed = true;
  300. }
  301. revertButtons.erase(begin(revertButtons) + idx);
  302. removeButtons.erase(begin(removeButtons) + idx);
  303. edits.erase(begin(edits) + idx);
  304. auto item = layout()->takeAt(static_cast<int>(idx));
  305. QLayoutItem *child = nullptr;
  306. while ((child = item->layout()->takeAt(0))) {
  307. delete child->widget();
  308. delete child;
  309. }
  310. delete item;
  311. if (removeButtons.size() == 1)
  312. removeButtons.front()->setEnabled(false);
  313. emit KeyChanged();
  314. }
  315. void OBSHotkeyWidget::BindingsChanged(void *data, calldata_t *param)
  316. {
  317. auto widget = static_cast<OBSHotkeyWidget *>(data);
  318. auto key = static_cast<obs_hotkey_t *>(calldata_ptr(param, "key"));
  319. QMetaObject::invokeMethod(widget, "HandleChangedBindings",
  320. Q_ARG(obs_hotkey_id, obs_hotkey_get_id(key)));
  321. }
  322. void OBSHotkeyWidget::HandleChangedBindings(obs_hotkey_id id_)
  323. {
  324. if (ignoreChangedBindings || id != id_)
  325. return;
  326. std::vector<obs_key_combination_t> bindings;
  327. auto LoadBindings = [&](obs_hotkey_binding_t *binding) {
  328. if (obs_hotkey_binding_get_hotkey_id(binding) != id)
  329. return;
  330. auto get_combo = obs_hotkey_binding_get_key_combination;
  331. bindings.push_back(get_combo(binding));
  332. };
  333. using LoadBindings_t = decltype(&LoadBindings);
  334. obs_enum_hotkey_bindings(
  335. [](void *data, size_t, obs_hotkey_binding_t *binding) {
  336. auto LoadBindings = *static_cast<LoadBindings_t>(data);
  337. LoadBindings(binding);
  338. return true;
  339. },
  340. static_cast<void *>(&LoadBindings));
  341. while (edits.size() > 0)
  342. RemoveEdit(edits.size() - 1, false);
  343. SetKeyCombinations(bindings);
  344. }
  345. static inline void updateStyle(QWidget *widget)
  346. {
  347. auto style = widget->style();
  348. style->unpolish(widget);
  349. style->polish(widget);
  350. widget->update();
  351. }
  352. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  353. void OBSHotkeyWidget::enterEvent(QEnterEvent *event)
  354. #else
  355. void OBSHotkeyWidget::enterEvent(QEvent *event)
  356. #endif
  357. {
  358. if (!label)
  359. return;
  360. event->accept();
  361. label->highlightPair(true);
  362. }
  363. void OBSHotkeyWidget::leaveEvent(QEvent *event)
  364. {
  365. if (!label)
  366. return;
  367. event->accept();
  368. label->highlightPair(false);
  369. }
  370. void OBSHotkeyLabel::highlightPair(bool highlight)
  371. {
  372. if (!pairPartner)
  373. return;
  374. pairPartner->setProperty("hotkeyPairHover", highlight);
  375. updateStyle(pairPartner);
  376. setProperty("hotkeyPairHover", highlight);
  377. updateStyle(this);
  378. }
  379. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  380. void OBSHotkeyLabel::enterEvent(QEnterEvent *event)
  381. #else
  382. void OBSHotkeyLabel::enterEvent(QEvent *event)
  383. #endif
  384. {
  385. if (!pairPartner)
  386. return;
  387. event->accept();
  388. highlightPair(true);
  389. }
  390. void OBSHotkeyLabel::leaveEvent(QEvent *event)
  391. {
  392. if (!pairPartner)
  393. return;
  394. event->accept();
  395. highlightPair(false);
  396. }
  397. void OBSHotkeyLabel::setToolTip(const QString &toolTip)
  398. {
  399. QLabel::setToolTip(toolTip);
  400. if (widget)
  401. widget->setToolTip(toolTip);
  402. }