qt-wrappers.cpp 9.5 KB

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