qt-wrappers.cpp 7.2 KB

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