qt-wrappers.cpp 9.4 KB

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