qt-wrappers.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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. #include "qt-wrappers.hpp"
  15. #include "obs-app.hpp"
  16. #include <graphics/graphics.h>
  17. #include <util/threading.h>
  18. #include <QWidget>
  19. #include <QLayout>
  20. #include <QMessageBox>
  21. #include <QDataStream>
  22. #if !defined(_WIN32) && !defined(__APPLE__)
  23. #include <QX11Info>
  24. #endif
  25. static inline void OBSErrorBoxva(QWidget *parent, const char *msg, va_list args)
  26. {
  27. char full_message[4096];
  28. vsnprintf(full_message, 4095, msg, args);
  29. QMessageBox::critical(parent, "Error", full_message);
  30. }
  31. void OBSErrorBox(QWidget *parent, const char *msg, ...)
  32. {
  33. va_list args;
  34. va_start(args, msg);
  35. OBSErrorBoxva(parent, msg, args);
  36. va_end(args);
  37. }
  38. QMessageBox::StandardButton
  39. OBSMessageBox::question(QWidget *parent, const QString &title,
  40. const QString &text,
  41. QMessageBox::StandardButtons buttons,
  42. QMessageBox::StandardButton defaultButton)
  43. {
  44. QMessageBox mb(QMessageBox::Question, title, text, buttons, parent);
  45. mb.setDefaultButton(defaultButton);
  46. if (buttons & QMessageBox::Ok)
  47. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  48. #define translate_button(x) \
  49. if (buttons & QMessageBox::x) \
  50. mb.setButtonText(QMessageBox::x, QTStr(#x));
  51. translate_button(Open);
  52. translate_button(Save);
  53. translate_button(Cancel);
  54. translate_button(Close);
  55. translate_button(Discard);
  56. translate_button(Apply);
  57. translate_button(Reset);
  58. translate_button(Yes);
  59. translate_button(No);
  60. translate_button(No);
  61. translate_button(Abort);
  62. translate_button(Retry);
  63. translate_button(Ignore);
  64. #undef translate_button
  65. return (QMessageBox::StandardButton)mb.exec();
  66. }
  67. void OBSMessageBox::information(QWidget *parent, const QString &title,
  68. const QString &text)
  69. {
  70. QMessageBox mb(QMessageBox::Information, title, text, QMessageBox::Ok,
  71. parent);
  72. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  73. mb.exec();
  74. }
  75. void OBSMessageBox::warning(QWidget *parent, const QString &title,
  76. const QString &text, bool enableRichText)
  77. {
  78. QMessageBox mb(QMessageBox::Warning, title, text, QMessageBox::Ok,
  79. parent);
  80. if (enableRichText)
  81. mb.setTextFormat(Qt::RichText);
  82. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  83. mb.exec();
  84. }
  85. void OBSMessageBox::critical(QWidget *parent, const QString &title,
  86. const QString &text)
  87. {
  88. QMessageBox mb(QMessageBox::Critical, title, text, QMessageBox::Ok,
  89. parent);
  90. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  91. mb.exec();
  92. }
  93. void QTToGSWindow(WId windowId, gs_window &gswindow)
  94. {
  95. #ifdef _WIN32
  96. gswindow.hwnd = (HWND)windowId;
  97. #elif __APPLE__
  98. gswindow.view = (id)windowId;
  99. #else
  100. gswindow.id = windowId;
  101. gswindow.display = QX11Info::display();
  102. #endif
  103. }
  104. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods)
  105. {
  106. int obsModifiers = INTERACT_NONE;
  107. if (mods.testFlag(Qt::ShiftModifier))
  108. obsModifiers |= INTERACT_SHIFT_KEY;
  109. if (mods.testFlag(Qt::AltModifier))
  110. obsModifiers |= INTERACT_ALT_KEY;
  111. #ifdef __APPLE__
  112. // Mac: Meta = Control, Control = Command
  113. if (mods.testFlag(Qt::ControlModifier))
  114. obsModifiers |= INTERACT_COMMAND_KEY;
  115. if (mods.testFlag(Qt::MetaModifier))
  116. obsModifiers |= INTERACT_CONTROL_KEY;
  117. #else
  118. // Handle windows key? Can a browser even trap that key?
  119. if (mods.testFlag(Qt::ControlModifier))
  120. obsModifiers |= INTERACT_CONTROL_KEY;
  121. if (mods.testFlag(Qt::MetaModifier))
  122. obsModifiers |= INTERACT_COMMAND_KEY;
  123. #endif
  124. return obsModifiers;
  125. }
  126. QDataStream &operator<<(QDataStream &out,
  127. const std::vector<std::shared_ptr<OBSSignal>> &)
  128. {
  129. return out;
  130. }
  131. QDataStream &operator>>(QDataStream &in,
  132. std::vector<std::shared_ptr<OBSSignal>> &)
  133. {
  134. return in;
  135. }
  136. QDataStream &operator<<(QDataStream &out, const OBSScene &scene)
  137. {
  138. return out << QString(obs_source_get_name(obs_scene_get_source(scene)));
  139. }
  140. QDataStream &operator>>(QDataStream &in, OBSScene &scene)
  141. {
  142. QString sceneName;
  143. in >> sceneName;
  144. obs_source_t *source = obs_get_source_by_name(QT_TO_UTF8(sceneName));
  145. scene = obs_scene_from_source(source);
  146. return in;
  147. }
  148. QDataStream &operator<<(QDataStream &out, const OBSSceneItem &si)
  149. {
  150. obs_scene_t *scene = obs_sceneitem_get_scene(si);
  151. obs_source_t *source = obs_sceneitem_get_source(si);
  152. return out << QString(obs_source_get_name(obs_scene_get_source(scene)))
  153. << QString(obs_source_get_name(source));
  154. }
  155. QDataStream &operator>>(QDataStream &in, OBSSceneItem &si)
  156. {
  157. QString sceneName;
  158. QString sourceName;
  159. in >> sceneName >> sourceName;
  160. obs_source_t *sceneSource =
  161. obs_get_source_by_name(QT_TO_UTF8(sceneName));
  162. obs_scene_t *scene = obs_scene_from_source(sceneSource);
  163. si = obs_scene_find_source(scene, QT_TO_UTF8(sourceName));
  164. obs_source_release(sceneSource);
  165. return in;
  166. }
  167. void DeleteLayout(QLayout *layout)
  168. {
  169. if (!layout)
  170. return;
  171. for (;;) {
  172. QLayoutItem *item = layout->takeAt(0);
  173. if (!item)
  174. break;
  175. QLayout *subLayout = item->layout();
  176. if (subLayout) {
  177. DeleteLayout(subLayout);
  178. } else {
  179. delete item->widget();
  180. delete item;
  181. }
  182. }
  183. delete layout;
  184. }
  185. class QuickThread : public QThread {
  186. public:
  187. explicit inline QuickThread(std::function<void()> func_) : func(func_)
  188. {
  189. }
  190. private:
  191. virtual void run() override { func(); }
  192. std::function<void()> func;
  193. };
  194. QThread *CreateQThread(std::function<void()> func)
  195. {
  196. return new QuickThread(func);
  197. }
  198. volatile long insideEventLoop = 0;
  199. void ExecuteFuncSafeBlock(std::function<void()> func)
  200. {
  201. QEventLoop eventLoop;
  202. auto wait = [&]() {
  203. func();
  204. QMetaObject::invokeMethod(&eventLoop, "quit",
  205. Qt::QueuedConnection);
  206. };
  207. os_atomic_inc_long(&insideEventLoop);
  208. QScopedPointer<QThread> thread(CreateQThread(wait));
  209. thread->start();
  210. eventLoop.exec();
  211. thread->wait();
  212. os_atomic_dec_long(&insideEventLoop);
  213. }
  214. void ExecuteFuncSafeBlockMsgBox(std::function<void()> func,
  215. const QString &title, const QString &text)
  216. {
  217. QMessageBox dlg;
  218. dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint);
  219. dlg.setWindowTitle(title);
  220. dlg.setText(text);
  221. dlg.setStandardButtons(0);
  222. auto wait = [&]() {
  223. func();
  224. QMetaObject::invokeMethod(&dlg, "accept", Qt::QueuedConnection);
  225. };
  226. os_atomic_inc_long(&insideEventLoop);
  227. QScopedPointer<QThread> thread(CreateQThread(wait));
  228. thread->start();
  229. dlg.exec();
  230. thread->wait();
  231. os_atomic_dec_long(&insideEventLoop);
  232. }
  233. static bool enable_message_boxes = false;
  234. void EnableThreadedMessageBoxes(bool enable)
  235. {
  236. enable_message_boxes = enable;
  237. }
  238. void ExecThreadedWithoutBlocking(std::function<void()> func,
  239. const QString &title, const QString &text)
  240. {
  241. if (!enable_message_boxes)
  242. ExecuteFuncSafeBlock(func);
  243. else
  244. ExecuteFuncSafeBlockMsgBox(func, title, text);
  245. }