1
0

qt-wrappers.cpp 9.3 KB

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