Utils.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /******************************************************************************
  2. Copyright (C) 2024 by Taylor Giampaolo <[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 <QFocusEvent>
  16. #include <QRegularExpression>
  17. #include <QStyle>
  18. #include <QWidget>
  19. namespace idian {
  20. // Helpers for OBS Idian widgets
  21. class Utils {
  22. static bool classNameIsValid(const QString &name)
  23. {
  24. static const QRegularExpression classRegex("^[a-zA-Z][a-zA-Z0-9_-]*$");
  25. const QRegularExpressionMatch match = classRegex.match(name);
  26. return match.hasMatch();
  27. }
  28. public:
  29. QWidget *parent = nullptr;
  30. Utils(QWidget *w) { parent = w; }
  31. // Set a custom property whenever the widget has keyboard focus specifically
  32. void showKeyFocused(QFocusEvent *e)
  33. {
  34. if (e->reason() != Qt::MouseFocusReason && e->reason() != Qt::PopupFocusReason) {
  35. addClass("keyFocus");
  36. } else {
  37. removeClass("keyFocus");
  38. }
  39. }
  40. void hideKeyFocused(QFocusEvent *e)
  41. {
  42. if (e->reason() != Qt::PopupFocusReason) {
  43. removeClass("keyFocus");
  44. }
  45. }
  46. // Force all children widgets to repaint
  47. void polishChildren() { polishChildren(parent); }
  48. static void polishChildren(QWidget *widget)
  49. {
  50. for (QWidget *child : widget->findChildren<QWidget *>()) {
  51. repolish(child);
  52. }
  53. }
  54. void repolish() { repolish(parent); }
  55. static void repolish(QWidget *widget)
  56. {
  57. widget->style()->unpolish(widget);
  58. widget->style()->polish(widget);
  59. }
  60. // Adds a style class to the widget
  61. void addClass(const QString &classname) { addClass(parent, classname); }
  62. static void addClass(QWidget *widget, const QString &classname)
  63. {
  64. if (!classNameIsValid(classname)) {
  65. return;
  66. }
  67. QVariant current = widget->property("class");
  68. QStringList classList = current.toString().split(" ");
  69. if (classList.contains(classname)) {
  70. return;
  71. }
  72. classList.removeDuplicates();
  73. classList.append(classname);
  74. widget->setProperty("class", classList.join(" "));
  75. repolish(widget);
  76. }
  77. // Removes a style class from a widget
  78. void removeClass(const QString &classname) { removeClass(parent, classname); }
  79. static void removeClass(QWidget *widget, const QString &classname)
  80. {
  81. if (!classNameIsValid(classname)) {
  82. return;
  83. }
  84. QVariant current = widget->property("class");
  85. if (current.isNull()) {
  86. return;
  87. }
  88. QStringList classList = current.toString().split(" ");
  89. if (!classList.contains(classname, Qt::CaseSensitive)) {
  90. return;
  91. }
  92. classList.removeDuplicates();
  93. classList.removeAll(classname);
  94. widget->setProperty("class", classList.join(" "));
  95. repolish(widget);
  96. }
  97. // Forces the addition or removal of a style class from a widget
  98. void toggleClass(const QString &classname, bool toggle) { toggleClass(parent, classname, toggle); }
  99. static void toggleClass(QWidget *widget, const QString &classname, bool toggle)
  100. {
  101. if (toggle) {
  102. addClass(widget, classname);
  103. } else {
  104. removeClass(widget, classname);
  105. }
  106. }
  107. };
  108. } // namespace idian