qt-wrappers.cpp 9.8 KB

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