qt-wrappers.cpp 6.7 KB

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