qt-wrappers.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 <QComboBox>
  21. #include <QMessageBox>
  22. #include <QDataStream>
  23. #include <QKeyEvent>
  24. #include <QFileDialog>
  25. #include <QStandardItemModel>
  26. #if !defined(_WIN32) && !defined(__APPLE__)
  27. #include <obs-nix-platform.h>
  28. #include <QX11Info>
  29. #endif
  30. #ifdef ENABLE_WAYLAND
  31. #include <qpa/qplatformnativeinterface.h>
  32. #endif
  33. static inline void OBSErrorBoxva(QWidget *parent, const char *msg, va_list args)
  34. {
  35. char full_message[4096];
  36. vsnprintf(full_message, 4095, msg, args);
  37. QMessageBox::critical(parent, "Error", full_message);
  38. }
  39. void OBSErrorBox(QWidget *parent, const char *msg, ...)
  40. {
  41. va_list args;
  42. va_start(args, msg);
  43. OBSErrorBoxva(parent, msg, args);
  44. va_end(args);
  45. }
  46. QMessageBox::StandardButton
  47. OBSMessageBox::question(QWidget *parent, const QString &title,
  48. const QString &text,
  49. QMessageBox::StandardButtons buttons,
  50. QMessageBox::StandardButton defaultButton)
  51. {
  52. QMessageBox mb(QMessageBox::Question, title, text, buttons, parent);
  53. mb.setDefaultButton(defaultButton);
  54. if (buttons & QMessageBox::Ok)
  55. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  56. #define translate_button(x) \
  57. if (buttons & QMessageBox::x) \
  58. mb.setButtonText(QMessageBox::x, QTStr(#x));
  59. translate_button(Open);
  60. translate_button(Save);
  61. translate_button(Cancel);
  62. translate_button(Close);
  63. translate_button(Discard);
  64. translate_button(Apply);
  65. translate_button(Reset);
  66. translate_button(Yes);
  67. translate_button(No);
  68. translate_button(No);
  69. translate_button(Abort);
  70. translate_button(Retry);
  71. translate_button(Ignore);
  72. #undef translate_button
  73. return (QMessageBox::StandardButton)mb.exec();
  74. }
  75. void OBSMessageBox::information(QWidget *parent, const QString &title,
  76. const QString &text)
  77. {
  78. QMessageBox mb(QMessageBox::Information, title, text, QMessageBox::Ok,
  79. parent);
  80. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  81. mb.exec();
  82. }
  83. void OBSMessageBox::warning(QWidget *parent, const QString &title,
  84. const QString &text, bool enableRichText)
  85. {
  86. QMessageBox mb(QMessageBox::Warning, title, text, QMessageBox::Ok,
  87. parent);
  88. if (enableRichText)
  89. mb.setTextFormat(Qt::RichText);
  90. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  91. mb.exec();
  92. }
  93. void OBSMessageBox::critical(QWidget *parent, const QString &title,
  94. const QString &text)
  95. {
  96. QMessageBox mb(QMessageBox::Critical, title, text, QMessageBox::Ok,
  97. parent);
  98. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  99. mb.exec();
  100. }
  101. bool QTToGSWindow(QWindow *window, gs_window &gswindow)
  102. {
  103. bool success = true;
  104. #ifdef _WIN32
  105. gswindow.hwnd = (HWND)window->winId();
  106. #elif __APPLE__
  107. gswindow.view = (id)window->winId();
  108. #else
  109. switch (obs_get_nix_platform()) {
  110. case OBS_NIX_PLATFORM_X11_GLX:
  111. case OBS_NIX_PLATFORM_X11_EGL:
  112. gswindow.id = window->winId();
  113. gswindow.display = obs_get_nix_platform_display();
  114. break;
  115. #ifdef ENABLE_WAYLAND
  116. case OBS_NIX_PLATFORM_WAYLAND:
  117. QPlatformNativeInterface *native =
  118. QGuiApplication::platformNativeInterface();
  119. gswindow.display =
  120. native->nativeResourceForWindow("surface", window);
  121. success = gswindow.display != nullptr;
  122. break;
  123. #endif
  124. }
  125. #endif
  126. return success;
  127. }
  128. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods)
  129. {
  130. int obsModifiers = INTERACT_NONE;
  131. if (mods.testFlag(Qt::ShiftModifier))
  132. obsModifiers |= INTERACT_SHIFT_KEY;
  133. if (mods.testFlag(Qt::AltModifier))
  134. obsModifiers |= INTERACT_ALT_KEY;
  135. #ifdef __APPLE__
  136. // Mac: Meta = Control, Control = Command
  137. if (mods.testFlag(Qt::ControlModifier))
  138. obsModifiers |= INTERACT_COMMAND_KEY;
  139. if (mods.testFlag(Qt::MetaModifier))
  140. obsModifiers |= INTERACT_CONTROL_KEY;
  141. #else
  142. // Handle windows key? Can a browser even trap that key?
  143. if (mods.testFlag(Qt::ControlModifier))
  144. obsModifiers |= INTERACT_CONTROL_KEY;
  145. if (mods.testFlag(Qt::MetaModifier))
  146. obsModifiers |= INTERACT_COMMAND_KEY;
  147. #endif
  148. return obsModifiers;
  149. }
  150. QDataStream &operator<<(QDataStream &out,
  151. const std::vector<std::shared_ptr<OBSSignal>> &)
  152. {
  153. return out;
  154. }
  155. QDataStream &operator>>(QDataStream &in,
  156. std::vector<std::shared_ptr<OBSSignal>> &)
  157. {
  158. return in;
  159. }
  160. QDataStream &operator<<(QDataStream &out, const OBSScene &scene)
  161. {
  162. return out << QString(obs_source_get_name(obs_scene_get_source(scene)));
  163. }
  164. QDataStream &operator>>(QDataStream &in, OBSScene &scene)
  165. {
  166. QString sceneName;
  167. in >> sceneName;
  168. obs_source_t *source = obs_get_source_by_name(QT_TO_UTF8(sceneName));
  169. scene = obs_scene_from_source(source);
  170. obs_source_release(source);
  171. return in;
  172. }
  173. QDataStream &operator<<(QDataStream &out, const OBSSceneItem &si)
  174. {
  175. obs_scene_t *scene = obs_sceneitem_get_scene(si);
  176. obs_source_t *source = obs_sceneitem_get_source(si);
  177. return out << QString(obs_source_get_name(obs_scene_get_source(scene)))
  178. << QString(obs_source_get_name(source));
  179. }
  180. QDataStream &operator>>(QDataStream &in, OBSSceneItem &si)
  181. {
  182. QString sceneName;
  183. QString sourceName;
  184. in >> sceneName >> sourceName;
  185. obs_source_t *sceneSource =
  186. obs_get_source_by_name(QT_TO_UTF8(sceneName));
  187. obs_scene_t *scene = obs_scene_from_source(sceneSource);
  188. si = obs_scene_find_source(scene, QT_TO_UTF8(sourceName));
  189. obs_source_release(sceneSource);
  190. return in;
  191. }
  192. void DeleteLayout(QLayout *layout)
  193. {
  194. if (!layout)
  195. return;
  196. for (;;) {
  197. QLayoutItem *item = layout->takeAt(0);
  198. if (!item)
  199. break;
  200. QLayout *subLayout = item->layout();
  201. if (subLayout) {
  202. DeleteLayout(subLayout);
  203. } else {
  204. delete item->widget();
  205. delete item;
  206. }
  207. }
  208. delete layout;
  209. }
  210. class QuickThread : public QThread {
  211. public:
  212. explicit inline QuickThread(std::function<void()> func_) : func(func_)
  213. {
  214. }
  215. private:
  216. virtual void run() override { func(); }
  217. std::function<void()> func;
  218. };
  219. QThread *CreateQThread(std::function<void()> func)
  220. {
  221. return new QuickThread(func);
  222. }
  223. volatile long insideEventLoop = 0;
  224. void ExecuteFuncSafeBlock(std::function<void()> func)
  225. {
  226. QEventLoop eventLoop;
  227. auto wait = [&]() {
  228. func();
  229. QMetaObject::invokeMethod(&eventLoop, "quit",
  230. Qt::QueuedConnection);
  231. };
  232. os_atomic_inc_long(&insideEventLoop);
  233. QScopedPointer<QThread> thread(CreateQThread(wait));
  234. thread->start();
  235. eventLoop.exec();
  236. thread->wait();
  237. os_atomic_dec_long(&insideEventLoop);
  238. }
  239. void ExecuteFuncSafeBlockMsgBox(std::function<void()> func,
  240. const QString &title, const QString &text)
  241. {
  242. QMessageBox dlg;
  243. dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint);
  244. dlg.setWindowTitle(title);
  245. dlg.setText(text);
  246. dlg.setStandardButtons(QMessageBox::StandardButtons());
  247. auto wait = [&]() {
  248. func();
  249. QMetaObject::invokeMethod(&dlg, "accept", Qt::QueuedConnection);
  250. };
  251. os_atomic_inc_long(&insideEventLoop);
  252. QScopedPointer<QThread> thread(CreateQThread(wait));
  253. thread->start();
  254. dlg.exec();
  255. thread->wait();
  256. os_atomic_dec_long(&insideEventLoop);
  257. }
  258. static bool enable_message_boxes = false;
  259. void EnableThreadedMessageBoxes(bool enable)
  260. {
  261. enable_message_boxes = enable;
  262. }
  263. void ExecThreadedWithoutBlocking(std::function<void()> func,
  264. const QString &title, const QString &text)
  265. {
  266. if (!enable_message_boxes)
  267. ExecuteFuncSafeBlock(func);
  268. else
  269. ExecuteFuncSafeBlockMsgBox(func, title, text);
  270. }
  271. bool LineEditCanceled(QEvent *event)
  272. {
  273. if (event->type() == QEvent::KeyPress) {
  274. QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
  275. return keyEvent->key() == Qt::Key_Escape;
  276. }
  277. return false;
  278. }
  279. bool LineEditChanged(QEvent *event)
  280. {
  281. if (event->type() == QEvent::KeyPress) {
  282. QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
  283. switch (keyEvent->key()) {
  284. case Qt::Key_Tab:
  285. case Qt::Key_Backtab:
  286. case Qt::Key_Enter:
  287. case Qt::Key_Return:
  288. return true;
  289. }
  290. } else if (event->type() == QEvent::FocusOut) {
  291. return true;
  292. }
  293. return false;
  294. }
  295. void SetComboItemEnabled(QComboBox *c, int idx, bool enabled)
  296. {
  297. QStandardItemModel *model =
  298. dynamic_cast<QStandardItemModel *>(c->model());
  299. QStandardItem *item = model->item(idx);
  300. item->setFlags(enabled ? Qt::ItemIsSelectable | Qt::ItemIsEnabled
  301. : Qt::NoItemFlags);
  302. }
  303. void setThemeID(QWidget *widget, const QString &themeID)
  304. {
  305. if (widget->property("themeID").toString() != themeID) {
  306. widget->setProperty("themeID", themeID);
  307. /* force style sheet recalculation */
  308. QString qss = widget->styleSheet();
  309. widget->setStyleSheet("/* */");
  310. widget->setStyleSheet(qss);
  311. }
  312. }
  313. QString SelectDirectory(QWidget *parent, QString title, QString path)
  314. {
  315. QString dir = QFileDialog::getExistingDirectory(
  316. parent, title, path,
  317. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  318. return dir;
  319. }
  320. QString SaveFile(QWidget *parent, QString title, QString path,
  321. QString extensions)
  322. {
  323. QString file =
  324. QFileDialog::getSaveFileName(parent, title, path, extensions);
  325. return file;
  326. }
  327. QString OpenFile(QWidget *parent, QString title, QString path,
  328. QString extensions)
  329. {
  330. QString file =
  331. QFileDialog::getOpenFileName(parent, title, path, extensions);
  332. return file;
  333. }
  334. QStringList OpenFiles(QWidget *parent, QString title, QString path,
  335. QString extensions)
  336. {
  337. QStringList files =
  338. QFileDialog::getOpenFileNames(parent, title, path, extensions);
  339. return files;
  340. }