qt-wrappers.cpp 9.3 KB

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