qt-wrappers.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <graphics/graphics.h>
  16. #include <QWidget>
  17. #include <QLayout>
  18. #include <QMessageBox>
  19. #include <QDataStream>
  20. #if !defined(_WIN32) && !defined(__APPLE__)
  21. #include <QX11Info>
  22. #endif
  23. static inline void OBSErrorBoxva(QWidget *parent, const char *msg, va_list args)
  24. {
  25. char full_message[4096];
  26. vsnprintf(full_message, 4095, msg, args);
  27. QMessageBox::critical(parent, "Error", full_message);
  28. }
  29. void OBSErrorBox(QWidget *parent, const char *msg, ...)
  30. {
  31. va_list args;
  32. va_start(args, msg);
  33. OBSErrorBoxva(parent, msg, args);
  34. va_end(args);
  35. }
  36. void QTToGSWindow(WId windowId, gs_window &gswindow)
  37. {
  38. #ifdef _WIN32
  39. gswindow.hwnd = (HWND)windowId;
  40. #elif __APPLE__
  41. gswindow.view = (id)windowId;
  42. #else
  43. gswindow.id = windowId;
  44. gswindow.display = QX11Info::display();
  45. #endif
  46. }
  47. uint32_t TranslateQtKeyboardEventModifiers(Qt::KeyboardModifiers mods)
  48. {
  49. int obsModifiers = INTERACT_NONE;
  50. if (mods.testFlag(Qt::ShiftModifier))
  51. obsModifiers |= INTERACT_SHIFT_KEY;
  52. if (mods.testFlag(Qt::AltModifier))
  53. obsModifiers |= INTERACT_ALT_KEY;
  54. #ifdef __APPLE__
  55. // Mac: Meta = Control, Control = Command
  56. if (mods.testFlag(Qt::ControlModifier))
  57. obsModifiers |= INTERACT_COMMAND_KEY;
  58. if (mods.testFlag(Qt::MetaModifier))
  59. obsModifiers |= INTERACT_CONTROL_KEY;
  60. #else
  61. // Handle windows key? Can a browser even trap that key?
  62. if (mods.testFlag(Qt::ControlModifier))
  63. obsModifiers |= INTERACT_CONTROL_KEY;
  64. if (mods.testFlag(Qt::MetaModifier))
  65. obsModifiers |= INTERACT_COMMAND_KEY;
  66. #endif
  67. return obsModifiers;
  68. }
  69. QDataStream &operator<<(QDataStream &out,
  70. const std::vector<std::shared_ptr<OBSSignal>> &)
  71. {
  72. return out;
  73. }
  74. QDataStream &operator>>(QDataStream &in,
  75. std::vector<std::shared_ptr<OBSSignal>> &)
  76. {
  77. return in;
  78. }
  79. QDataStream &operator<<(QDataStream &out, const OBSScene &scene)
  80. {
  81. return out << QString(obs_source_get_name(obs_scene_get_source(scene)));
  82. }
  83. QDataStream &operator>>(QDataStream &in, OBSScene &scene)
  84. {
  85. QString sceneName;
  86. in >> sceneName;
  87. obs_source_t *source = obs_get_source_by_name(QT_TO_UTF8(sceneName));
  88. scene = obs_scene_from_source(source);
  89. return in;
  90. }
  91. QDataStream &operator<<(QDataStream &out, const OBSSceneItem &si)
  92. {
  93. obs_scene_t *scene = obs_sceneitem_get_scene(si);
  94. obs_source_t *source = obs_sceneitem_get_source(si);
  95. return out << QString(obs_source_get_name(obs_scene_get_source(scene)))
  96. << QString(obs_source_get_name(source));
  97. }
  98. QDataStream &operator>>(QDataStream &in, OBSSceneItem &si)
  99. {
  100. QString sceneName;
  101. QString sourceName;
  102. in >> sceneName >> sourceName;
  103. obs_source_t *sceneSource =
  104. obs_get_source_by_name(QT_TO_UTF8(sceneName));
  105. obs_scene_t *scene = obs_scene_from_source(sceneSource);
  106. si = obs_scene_find_source(scene, QT_TO_UTF8(sourceName));
  107. obs_source_release(sceneSource);
  108. return in;
  109. }
  110. void DeleteLayout(QLayout *layout)
  111. {
  112. if (!layout)
  113. return;
  114. for (;;) {
  115. QLayoutItem *item = layout->takeAt(0);
  116. if (!item)
  117. break;
  118. QLayout *subLayout = item->layout();
  119. if (subLayout) {
  120. DeleteLayout(subLayout);
  121. } else {
  122. delete item->widget();
  123. delete item;
  124. }
  125. }
  126. delete layout;
  127. }