qt-wrappers.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. }
  129. #endif
  130. default:
  131. success = false;
  132. break;
  133. }
  134. #endif
  135. return success;
  136. }
  137. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods)
  138. {
  139. int obsModifiers = INTERACT_NONE;
  140. if (mods.testFlag(Qt::ShiftModifier))
  141. obsModifiers |= INTERACT_SHIFT_KEY;
  142. if (mods.testFlag(Qt::AltModifier))
  143. obsModifiers |= INTERACT_ALT_KEY;
  144. #ifdef __APPLE__
  145. // Mac: Meta = Control, Control = Command
  146. if (mods.testFlag(Qt::ControlModifier))
  147. obsModifiers |= INTERACT_COMMAND_KEY;
  148. if (mods.testFlag(Qt::MetaModifier))
  149. obsModifiers |= INTERACT_CONTROL_KEY;
  150. #else
  151. // Handle windows key? Can a browser even trap that key?
  152. if (mods.testFlag(Qt::ControlModifier))
  153. obsModifiers |= INTERACT_CONTROL_KEY;
  154. if (mods.testFlag(Qt::MetaModifier))
  155. obsModifiers |= INTERACT_COMMAND_KEY;
  156. #endif
  157. return obsModifiers;
  158. }
  159. QDataStream &operator<<(QDataStream &out,
  160. const std::vector<std::shared_ptr<OBSSignal>> &)
  161. {
  162. return out;
  163. }
  164. QDataStream &operator>>(QDataStream &in,
  165. std::vector<std::shared_ptr<OBSSignal>> &)
  166. {
  167. return in;
  168. }
  169. QDataStream &operator<<(QDataStream &out, const OBSScene &scene)
  170. {
  171. return out << QString(obs_source_get_name(obs_scene_get_source(scene)));
  172. }
  173. QDataStream &operator>>(QDataStream &in, OBSScene &scene)
  174. {
  175. QString sceneName;
  176. in >> sceneName;
  177. OBSSourceAutoRelease source =
  178. obs_get_source_by_name(QT_TO_UTF8(sceneName));
  179. scene = obs_scene_from_source(source);
  180. return in;
  181. }
  182. void DeleteLayout(QLayout *layout)
  183. {
  184. if (!layout)
  185. return;
  186. for (;;) {
  187. QLayoutItem *item = layout->takeAt(0);
  188. if (!item)
  189. break;
  190. QLayout *subLayout = item->layout();
  191. if (subLayout) {
  192. DeleteLayout(subLayout);
  193. } else {
  194. delete item->widget();
  195. delete item;
  196. }
  197. }
  198. delete layout;
  199. }
  200. class QuickThread : public QThread {
  201. public:
  202. explicit inline QuickThread(std::function<void()> func_) : func(func_)
  203. {
  204. }
  205. private:
  206. virtual void run() override { func(); }
  207. std::function<void()> func;
  208. };
  209. QThread *CreateQThread(std::function<void()> func)
  210. {
  211. return new QuickThread(func);
  212. }
  213. volatile long insideEventLoop = 0;
  214. void ExecuteFuncSafeBlock(std::function<void()> func)
  215. {
  216. QEventLoop eventLoop;
  217. auto wait = [&]() {
  218. func();
  219. QMetaObject::invokeMethod(&eventLoop, "quit",
  220. Qt::QueuedConnection);
  221. };
  222. os_atomic_inc_long(&insideEventLoop);
  223. QScopedPointer<QThread> thread(CreateQThread(wait));
  224. thread->start();
  225. eventLoop.exec();
  226. thread->wait();
  227. os_atomic_dec_long(&insideEventLoop);
  228. }
  229. void ExecuteFuncSafeBlockMsgBox(std::function<void()> func,
  230. const QString &title, const QString &text)
  231. {
  232. QMessageBox dlg;
  233. dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint);
  234. dlg.setWindowTitle(title);
  235. dlg.setText(text);
  236. dlg.setStandardButtons(QMessageBox::StandardButtons());
  237. auto wait = [&]() {
  238. func();
  239. QMetaObject::invokeMethod(&dlg, "accept", Qt::QueuedConnection);
  240. };
  241. os_atomic_inc_long(&insideEventLoop);
  242. QScopedPointer<QThread> thread(CreateQThread(wait));
  243. thread->start();
  244. dlg.exec();
  245. thread->wait();
  246. os_atomic_dec_long(&insideEventLoop);
  247. }
  248. static bool enable_message_boxes = false;
  249. void EnableThreadedMessageBoxes(bool enable)
  250. {
  251. enable_message_boxes = enable;
  252. }
  253. void ExecThreadedWithoutBlocking(std::function<void()> func,
  254. const QString &title, const QString &text)
  255. {
  256. if (!enable_message_boxes)
  257. ExecuteFuncSafeBlock(func);
  258. else
  259. ExecuteFuncSafeBlockMsgBox(func, title, text);
  260. }
  261. bool LineEditCanceled(QEvent *event)
  262. {
  263. if (event->type() == QEvent::KeyPress) {
  264. QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
  265. return keyEvent->key() == Qt::Key_Escape;
  266. }
  267. return false;
  268. }
  269. bool LineEditChanged(QEvent *event)
  270. {
  271. if (event->type() == QEvent::KeyPress) {
  272. QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
  273. switch (keyEvent->key()) {
  274. case Qt::Key_Tab:
  275. case Qt::Key_Backtab:
  276. case Qt::Key_Enter:
  277. case Qt::Key_Return:
  278. return true;
  279. }
  280. } else if (event->type() == QEvent::FocusOut) {
  281. return true;
  282. }
  283. return false;
  284. }
  285. void SetComboItemEnabled(QComboBox *c, int idx, bool enabled)
  286. {
  287. QStandardItemModel *model =
  288. dynamic_cast<QStandardItemModel *>(c->model());
  289. QStandardItem *item = model->item(idx);
  290. item->setFlags(enabled ? Qt::ItemIsSelectable | Qt::ItemIsEnabled
  291. : Qt::NoItemFlags);
  292. }
  293. void setThemeID(QWidget *widget, const QString &themeID)
  294. {
  295. if (widget->property("themeID").toString() != themeID) {
  296. widget->setProperty("themeID", themeID);
  297. /* force style sheet recalculation */
  298. QString qss = widget->styleSheet();
  299. widget->setStyleSheet("/* */");
  300. widget->setStyleSheet(qss);
  301. }
  302. }
  303. QString SelectDirectory(QWidget *parent, QString title, QString path)
  304. {
  305. QString dir = QFileDialog::getExistingDirectory(
  306. parent, title, path,
  307. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  308. return dir;
  309. }
  310. QString SaveFile(QWidget *parent, QString title, QString path,
  311. QString extensions)
  312. {
  313. QString file =
  314. QFileDialog::getSaveFileName(parent, title, path, extensions);
  315. return file;
  316. }
  317. QString OpenFile(QWidget *parent, QString title, QString path,
  318. QString extensions)
  319. {
  320. QString file =
  321. QFileDialog::getOpenFileName(parent, title, path, extensions);
  322. return file;
  323. }
  324. QStringList OpenFiles(QWidget *parent, QString title, QString path,
  325. QString extensions)
  326. {
  327. QStringList files =
  328. QFileDialog::getOpenFileNames(parent, title, path, extensions);
  329. return files;
  330. }
  331. static void SetLabelText(QLabel *label, const QString &newText)
  332. {
  333. if (label->text() != newText)
  334. label->setText(newText);
  335. }
  336. void TruncateLabel(QLabel *label, QString newText, int length)
  337. {
  338. if (newText.length() < length) {
  339. label->setToolTip(QString());
  340. SetLabelText(label, newText);
  341. return;
  342. }
  343. label->setToolTip(newText);
  344. newText.truncate(length);
  345. newText += "...";
  346. SetLabelText(label, newText);
  347. }
  348. void RefreshToolBarStyling(QToolBar *toolBar)
  349. {
  350. for (QAction *action : toolBar->actions()) {
  351. QWidget *widget = toolBar->widgetForAction(action);
  352. if (!widget)
  353. continue;
  354. widget->style()->unpolish(widget);
  355. widget->style()->polish(widget);
  356. }
  357. }