window-basic-properties.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /******************************************************************************
  2. Copyright (C) 2014 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 "obs-app.hpp"
  15. #include "window-basic-properties.hpp"
  16. #include "window-basic-main.hpp"
  17. #include "qt-wrappers.hpp"
  18. #include "display-helpers.hpp"
  19. #include <QCloseEvent>
  20. #include <QScreen>
  21. #include <QWindow>
  22. using namespace std;
  23. OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
  24. : QDialog (parent),
  25. main (qobject_cast<OBSBasic*>(parent)),
  26. resizeTimer (0),
  27. ui (new Ui::OBSBasicProperties),
  28. source (source_),
  29. removedSignal (obs_source_get_signal_handler(source),
  30. "remove", OBSBasicProperties::SourceRemoved,
  31. this),
  32. updatePropertiesSignal (obs_source_get_signal_handler(source),
  33. "update_properties",
  34. OBSBasicProperties::UpdateProperties,
  35. this)
  36. {
  37. int cx = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
  38. "cx");
  39. int cy = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
  40. "cy");
  41. ui->setupUi(this);
  42. if (cx > 400 && cy > 400)
  43. resize(cx, cy);
  44. OBSData settings = obs_source_get_settings(source);
  45. obs_data_release(settings);
  46. view = new OBSPropertiesView(settings, source,
  47. (PropertiesReloadCallback)obs_source_properties,
  48. (PropertiesUpdateCallback)obs_source_update);
  49. layout()->addWidget(view);
  50. layout()->setAlignment(view, Qt::AlignBottom);
  51. view->setMaximumHeight(250);
  52. view->setMinimumHeight(150);
  53. view->show();
  54. connect(view, SIGNAL(PropertiesResized()),
  55. this, SLOT(OnPropertiesResized()));
  56. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  57. if (resizeTimer)
  58. killTimer(resizeTimer);
  59. resizeTimer = startTimer(100);
  60. });
  61. const char *name = obs_source_get_name(source);
  62. setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
  63. }
  64. void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
  65. {
  66. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  67. "close");
  68. UNUSED_PARAMETER(params);
  69. }
  70. void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
  71. {
  72. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data)->view,
  73. "ReloadProperties");
  74. }
  75. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  76. {
  77. OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
  78. if (!window->source)
  79. return;
  80. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  81. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  82. int x, y;
  83. int newCX, newCY;
  84. float scale;
  85. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  86. newCX = int(scale * float(sourceCX));
  87. newCY = int(scale * float(sourceCY));
  88. gs_viewport_push();
  89. gs_projection_push();
  90. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
  91. -100.0f, 100.0f);
  92. gs_set_viewport(x, y, newCX, newCY);
  93. obs_source_video_render(window->source);
  94. gs_projection_pop();
  95. gs_viewport_pop();
  96. }
  97. void OBSBasicProperties::OnPropertiesResized()
  98. {
  99. if (resizeTimer)
  100. killTimer(resizeTimer);
  101. resizeTimer = startTimer(100);
  102. }
  103. void OBSBasicProperties::resizeEvent(QResizeEvent *event)
  104. {
  105. if (isVisible()) {
  106. if (resizeTimer)
  107. killTimer(resizeTimer);
  108. resizeTimer = startTimer(100);
  109. }
  110. UNUSED_PARAMETER(event);
  111. }
  112. void OBSBasicProperties::timerEvent(QTimerEvent *event)
  113. {
  114. if (event->timerId() == resizeTimer) {
  115. killTimer(resizeTimer);
  116. resizeTimer = 0;
  117. QSize size = GetPixelSize(ui->preview);
  118. obs_display_resize(display, size.width(), size.height());
  119. }
  120. }
  121. void OBSBasicProperties::closeEvent(QCloseEvent *event)
  122. {
  123. QDialog::closeEvent(event);
  124. if (!event->isAccepted())
  125. return;
  126. // remove draw callback and release display in case our drawable
  127. // surfaces go away before the destructor gets called
  128. obs_display_remove_draw_callback(display,
  129. OBSBasicProperties::DrawPreview, this);
  130. display = nullptr;
  131. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
  132. width());
  133. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
  134. height());
  135. }
  136. void OBSBasicProperties::Init()
  137. {
  138. gs_init_data init_data = {};
  139. show();
  140. QSize previewSize = GetPixelSize(ui->preview);
  141. init_data.cx = uint32_t(previewSize.width());
  142. init_data.cy = uint32_t(previewSize.height());
  143. init_data.format = GS_RGBA;
  144. QTToGSWindow(ui->preview->winId(), init_data.window);
  145. display = obs_display_create(&init_data);
  146. if (display)
  147. obs_display_add_draw_callback(display,
  148. OBSBasicProperties::DrawPreview, this);
  149. }