qt-wrappers.cpp 9.9 KB

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