qt-wrappers.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[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 <QApplication>
  16. #include <QMessageBox>
  17. #include <QWidget>
  18. #include <QWindow>
  19. #include <QThread>
  20. #include <obs.hpp>
  21. #include <functional>
  22. #include <memory>
  23. #include <vector>
  24. #define QT_UTF8(str) QString::fromUtf8(str, -1)
  25. #define QT_TO_UTF8(str) str.toUtf8().constData()
  26. #define MAX_LABEL_LENGTH 80
  27. class QDataStream;
  28. class QComboBox;
  29. class QWidget;
  30. class QLayout;
  31. class QString;
  32. struct gs_window;
  33. class QLabel;
  34. class QToolBar;
  35. class OBSMessageBox {
  36. public:
  37. static QMessageBox::StandardButton
  38. question(QWidget *parent, const QString &title, const QString &text,
  39. QMessageBox::StandardButtons buttons =
  40. QMessageBox::StandardButtons(QMessageBox::Yes |
  41. QMessageBox::No),
  42. QMessageBox::StandardButton defaultButton =
  43. QMessageBox::NoButton);
  44. static void information(QWidget *parent, const QString &title,
  45. const QString &text);
  46. static void warning(QWidget *parent, const QString &title,
  47. const QString &text, bool enableRichText = false);
  48. static void critical(QWidget *parent, const QString &title,
  49. const QString &text);
  50. };
  51. void OBSErrorBox(QWidget *parent, const char *msg, ...);
  52. bool QTToGSWindow(QWindow *window, gs_window &gswindow);
  53. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods);
  54. QDataStream &
  55. operator<<(QDataStream &out,
  56. const std::vector<std::shared_ptr<OBSSignal>> &signal_vec);
  57. QDataStream &operator>>(QDataStream &in,
  58. std::vector<std::shared_ptr<OBSSignal>> &signal_vec);
  59. QDataStream &operator<<(QDataStream &out, const OBSScene &scene);
  60. QDataStream &operator>>(QDataStream &in, OBSScene &scene);
  61. QDataStream &operator<<(QDataStream &out, const OBSSource &source);
  62. QDataStream &operator>>(QDataStream &in, OBSSource &source);
  63. QThread *CreateQThread(std::function<void()> func);
  64. void ExecuteFuncSafeBlock(std::function<void()> func);
  65. void ExecuteFuncSafeBlockMsgBox(std::function<void()> func,
  66. const QString &title, const QString &text);
  67. /* allows executing without message boxes if starting up, otherwise with a
  68. * message box */
  69. void EnableThreadedMessageBoxes(bool enable);
  70. void ExecThreadedWithoutBlocking(std::function<void()> func,
  71. const QString &title, const QString &text);
  72. class SignalBlocker {
  73. QWidget *widget;
  74. bool blocked;
  75. public:
  76. inline explicit SignalBlocker(QWidget *widget_) : widget(widget_)
  77. {
  78. blocked = widget->blockSignals(true);
  79. }
  80. inline ~SignalBlocker() { widget->blockSignals(blocked); }
  81. };
  82. void DeleteLayout(QLayout *layout);
  83. static inline Qt::ConnectionType WaitConnection()
  84. {
  85. return QThread::currentThread() == qApp->thread()
  86. ? Qt::DirectConnection
  87. : Qt::BlockingQueuedConnection;
  88. }
  89. bool LineEditCanceled(QEvent *event);
  90. bool LineEditChanged(QEvent *event);
  91. void SetComboItemEnabled(QComboBox *c, int idx, bool enabled);
  92. void setThemeID(QWidget *widget, const QString &themeID);
  93. QString SelectDirectory(QWidget *parent, QString title, QString path);
  94. QString SaveFile(QWidget *parent, QString title, QString path,
  95. QString extensions);
  96. QString OpenFile(QWidget *parent, QString title, QString path,
  97. QString extensions);
  98. QStringList OpenFiles(QWidget *parent, QString title, QString path,
  99. QString extensions);
  100. void TruncateLabel(QLabel *label, QString newText,
  101. int length = MAX_LABEL_LENGTH);
  102. void RefreshToolBarStyling(QToolBar *toolBar);