qt-wrappers.cpp 9.7 KB

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