qt-wrappers.cpp 7.8 KB

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