qt-wrappers.cpp 9.8 KB

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