qt-wrappers.cpp 6.5 KB

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