qt-wrappers.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <QWidget>
  18. #include <QLayout>
  19. #include <QMessageBox>
  20. #include <QDataStream>
  21. #if !defined(_WIN32) && !defined(__APPLE__)
  22. #include <QX11Info>
  23. #endif
  24. static inline void OBSErrorBoxva(QWidget *parent, const char *msg, va_list args)
  25. {
  26. char full_message[4096];
  27. vsnprintf(full_message, 4095, msg, args);
  28. QMessageBox::critical(parent, "Error", full_message);
  29. }
  30. void OBSErrorBox(QWidget *parent, const char *msg, ...)
  31. {
  32. va_list args;
  33. va_start(args, msg);
  34. OBSErrorBoxva(parent, msg, args);
  35. va_end(args);
  36. }
  37. QMessageBox::StandardButton OBSMessageBox::question(
  38. QWidget *parent,
  39. const QString &title,
  40. const QString &text,
  41. QMessageBox::StandardButtons buttons,
  42. QMessageBox::StandardButton defaultButton)
  43. {
  44. QMessageBox mb(QMessageBox::Question,
  45. title, text, buttons,
  46. parent);
  47. mb.setDefaultButton(defaultButton);
  48. if (buttons & QMessageBox::Ok) \
  49. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  50. #define translate_button(x) \
  51. if (buttons & QMessageBox::x) \
  52. mb.setButtonText(QMessageBox::x, QTStr(#x));
  53. translate_button(Open);
  54. translate_button(Save);
  55. translate_button(Cancel);
  56. translate_button(Close);
  57. translate_button(Discard);
  58. translate_button(Apply);
  59. translate_button(Reset);
  60. translate_button(Yes);
  61. translate_button(No);
  62. translate_button(No);
  63. translate_button(Abort);
  64. translate_button(Retry);
  65. translate_button(Ignore);
  66. #undef translate_button
  67. return (QMessageBox::StandardButton)mb.exec();
  68. }
  69. void OBSMessageBox::information(
  70. QWidget *parent,
  71. const QString &title,
  72. const QString &text)
  73. {
  74. QMessageBox mb(QMessageBox::Information,
  75. title, text, QMessageBox::Ok,
  76. parent);
  77. mb.setButtonText(QMessageBox::Ok, QTStr("OK"));
  78. mb.exec();
  79. }
  80. void QTToGSWindow(WId windowId, gs_window &gswindow)
  81. {
  82. #ifdef _WIN32
  83. gswindow.hwnd = (HWND)windowId;
  84. #elif __APPLE__
  85. gswindow.view = (id)windowId;
  86. #else
  87. gswindow.id = windowId;
  88. gswindow.display = QX11Info::display();
  89. #endif
  90. }
  91. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods)
  92. {
  93. int obsModifiers = INTERACT_NONE;
  94. if (mods.testFlag(Qt::ShiftModifier))
  95. obsModifiers |= INTERACT_SHIFT_KEY;
  96. if (mods.testFlag(Qt::AltModifier))
  97. obsModifiers |= INTERACT_ALT_KEY;
  98. #ifdef __APPLE__
  99. // Mac: Meta = Control, Control = Command
  100. if (mods.testFlag(Qt::ControlModifier))
  101. obsModifiers |= INTERACT_COMMAND_KEY;
  102. if (mods.testFlag(Qt::MetaModifier))
  103. obsModifiers |= INTERACT_CONTROL_KEY;
  104. #else
  105. // Handle windows key? Can a browser even trap that key?
  106. if (mods.testFlag(Qt::ControlModifier))
  107. obsModifiers |= INTERACT_CONTROL_KEY;
  108. if (mods.testFlag(Qt::MetaModifier))
  109. obsModifiers |= INTERACT_COMMAND_KEY;
  110. #endif
  111. return obsModifiers;
  112. }
  113. QDataStream &operator<<(QDataStream &out,
  114. const std::vector<std::shared_ptr<OBSSignal>> &)
  115. {
  116. return out;
  117. }
  118. QDataStream &operator>>(QDataStream &in,
  119. std::vector<std::shared_ptr<OBSSignal>> &)
  120. {
  121. return in;
  122. }
  123. QDataStream &operator<<(QDataStream &out, const OBSScene &scene)
  124. {
  125. return out << QString(obs_source_get_name(obs_scene_get_source(scene)));
  126. }
  127. QDataStream &operator>>(QDataStream &in, OBSScene &scene)
  128. {
  129. QString sceneName;
  130. in >> sceneName;
  131. obs_source_t *source = obs_get_source_by_name(QT_TO_UTF8(sceneName));
  132. scene = obs_scene_from_source(source);
  133. return in;
  134. }
  135. QDataStream &operator<<(QDataStream &out, const OBSSceneItem &si)
  136. {
  137. obs_scene_t *scene = obs_sceneitem_get_scene(si);
  138. obs_source_t *source = obs_sceneitem_get_source(si);
  139. return out << QString(obs_source_get_name(obs_scene_get_source(scene)))
  140. << QString(obs_source_get_name(source));
  141. }
  142. QDataStream &operator>>(QDataStream &in, OBSSceneItem &si)
  143. {
  144. QString sceneName;
  145. QString sourceName;
  146. in >> sceneName >> sourceName;
  147. obs_source_t *sceneSource =
  148. obs_get_source_by_name(QT_TO_UTF8(sceneName));
  149. obs_scene_t *scene = obs_scene_from_source(sceneSource);
  150. si = obs_scene_find_source(scene, QT_TO_UTF8(sourceName));
  151. obs_source_release(sceneSource);
  152. return in;
  153. }
  154. void DeleteLayout(QLayout *layout)
  155. {
  156. if (!layout)
  157. return;
  158. for (;;) {
  159. QLayoutItem *item = layout->takeAt(0);
  160. if (!item)
  161. break;
  162. QLayout *subLayout = item->layout();
  163. if (subLayout) {
  164. DeleteLayout(subLayout);
  165. } else {
  166. delete item->widget();
  167. delete item;
  168. }
  169. }
  170. delete layout;
  171. }