qt-wrappers.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 OBSMessageBox::warning(
  82. QWidget *parent,
  83. const QString &title,
  84. const QString &text)
  85. {
  86. QMessageBox mb(QMessageBox::Warning,
  87. title, text, QMessageBox::Ok,
  88. parent);
  89. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  90. mb.exec();
  91. }
  92. void OBSMessageBox::critical(
  93. QWidget *parent,
  94. const QString &title,
  95. const QString &text)
  96. {
  97. QMessageBox mb(QMessageBox::Critical,
  98. title, text, QMessageBox::Ok,
  99. parent);
  100. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  101. mb.exec();
  102. }
  103. void QTToGSWindow(WId windowId, gs_window &gswindow)
  104. {
  105. #ifdef _WIN32
  106. gswindow.hwnd = (HWND)windowId;
  107. #elif __APPLE__
  108. gswindow.view = (id)windowId;
  109. #else
  110. gswindow.id = windowId;
  111. gswindow.display = QX11Info::display();
  112. #endif
  113. }
  114. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods)
  115. {
  116. int obsModifiers = INTERACT_NONE;
  117. if (mods.testFlag(Qt::ShiftModifier))
  118. obsModifiers |= INTERACT_SHIFT_KEY;
  119. if (mods.testFlag(Qt::AltModifier))
  120. obsModifiers |= INTERACT_ALT_KEY;
  121. #ifdef __APPLE__
  122. // Mac: Meta = Control, Control = Command
  123. if (mods.testFlag(Qt::ControlModifier))
  124. obsModifiers |= INTERACT_COMMAND_KEY;
  125. if (mods.testFlag(Qt::MetaModifier))
  126. obsModifiers |= INTERACT_CONTROL_KEY;
  127. #else
  128. // Handle windows key? Can a browser even trap that key?
  129. if (mods.testFlag(Qt::ControlModifier))
  130. obsModifiers |= INTERACT_CONTROL_KEY;
  131. if (mods.testFlag(Qt::MetaModifier))
  132. obsModifiers |= INTERACT_COMMAND_KEY;
  133. #endif
  134. return obsModifiers;
  135. }
  136. QDataStream &operator<<(QDataStream &out,
  137. const std::vector<std::shared_ptr<OBSSignal>> &)
  138. {
  139. return out;
  140. }
  141. QDataStream &operator>>(QDataStream &in,
  142. std::vector<std::shared_ptr<OBSSignal>> &)
  143. {
  144. return in;
  145. }
  146. QDataStream &operator<<(QDataStream &out, const OBSScene &scene)
  147. {
  148. return out << QString(obs_source_get_name(obs_scene_get_source(scene)));
  149. }
  150. QDataStream &operator>>(QDataStream &in, OBSScene &scene)
  151. {
  152. QString sceneName;
  153. in >> sceneName;
  154. obs_source_t *source = obs_get_source_by_name(QT_TO_UTF8(sceneName));
  155. scene = obs_scene_from_source(source);
  156. return in;
  157. }
  158. QDataStream &operator<<(QDataStream &out, const OBSSceneItem &si)
  159. {
  160. obs_scene_t *scene = obs_sceneitem_get_scene(si);
  161. obs_source_t *source = obs_sceneitem_get_source(si);
  162. return out << QString(obs_source_get_name(obs_scene_get_source(scene)))
  163. << QString(obs_source_get_name(source));
  164. }
  165. QDataStream &operator>>(QDataStream &in, OBSSceneItem &si)
  166. {
  167. QString sceneName;
  168. QString sourceName;
  169. in >> sceneName >> sourceName;
  170. obs_source_t *sceneSource =
  171. obs_get_source_by_name(QT_TO_UTF8(sceneName));
  172. obs_scene_t *scene = obs_scene_from_source(sceneSource);
  173. si = obs_scene_find_source(scene, QT_TO_UTF8(sourceName));
  174. obs_source_release(sceneSource);
  175. return in;
  176. }
  177. void DeleteLayout(QLayout *layout)
  178. {
  179. if (!layout)
  180. return;
  181. for (;;) {
  182. QLayoutItem *item = layout->takeAt(0);
  183. if (!item)
  184. break;
  185. QLayout *subLayout = item->layout();
  186. if (subLayout) {
  187. DeleteLayout(subLayout);
  188. } else {
  189. delete item->widget();
  190. delete item;
  191. }
  192. }
  193. delete layout;
  194. }
  195. class QuickThread : public QThread {
  196. public:
  197. explicit inline QuickThread(std::function<void()> func_)
  198. : func(func_)
  199. {}
  200. private:
  201. virtual void run() override
  202. {
  203. func();
  204. }
  205. std::function<void()> func;
  206. };
  207. QThread *CreateQThread(std::function<void()> func)
  208. {
  209. return new QuickThread(func);
  210. }
  211. volatile long insideEventLoop = 0;
  212. void ExecuteFuncSafeBlock(std::function<void()> func)
  213. {
  214. QEventLoop eventLoop;
  215. auto wait = [&] ()
  216. {
  217. func();
  218. QMetaObject::invokeMethod(&eventLoop, "quit",
  219. Qt::QueuedConnection);
  220. };
  221. os_atomic_inc_long(&insideEventLoop);
  222. QScopedPointer<QThread> thread(CreateQThread(wait));
  223. thread->start();
  224. eventLoop.exec();
  225. thread->wait();
  226. os_atomic_dec_long(&insideEventLoop);
  227. }
  228. void ExecuteFuncSafeBlockMsgBox(
  229. std::function<void()> func,
  230. const QString &title,
  231. const QString &text)
  232. {
  233. QMessageBox dlg;
  234. dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint);
  235. dlg.setWindowTitle(title);
  236. dlg.setText(text);
  237. dlg.setStandardButtons(0);
  238. auto wait = [&] ()
  239. {
  240. func();
  241. QMetaObject::invokeMethod(&dlg, "accept", Qt::QueuedConnection);
  242. };
  243. os_atomic_inc_long(&insideEventLoop);
  244. QScopedPointer<QThread> thread(CreateQThread(wait));
  245. thread->start();
  246. dlg.exec();
  247. thread->wait();
  248. os_atomic_dec_long(&insideEventLoop);
  249. }
  250. static bool enable_message_boxes = false;
  251. void EnableThreadedMessageBoxes(bool enable)
  252. {
  253. enable_message_boxes = enable;
  254. }
  255. void ExecThreadedWithoutBlocking(
  256. std::function<void()> func,
  257. const QString &title,
  258. const QString &text)
  259. {
  260. if (!enable_message_boxes)
  261. ExecuteFuncSafeBlock(func);
  262. else
  263. ExecuteFuncSafeBlockMsgBox(func, title, text);
  264. }