qt-wrappers.cpp 10.0 KB

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