window-basic-properties.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <QScreen>
  20. #include <QWindow>
  21. using namespace std;
  22. OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
  23. : QDialog (parent),
  24. main (qobject_cast<OBSBasic*>(parent)),
  25. resizeTimer (0),
  26. ui (new Ui::OBSBasicProperties),
  27. source (source_),
  28. removedSignal (obs_source_signalhandler(source), "remove",
  29. OBSBasicProperties::SourceRemoved, this)
  30. {
  31. setAttribute(Qt::WA_DeleteOnClose);
  32. ui->setupUi(this);
  33. OBSData settings = obs_source_getsettings(source);
  34. obs_data_release(settings);
  35. view = new OBSPropertiesView(settings,
  36. obs_source_properties(source, App()->GetLocale()),
  37. source, (PropertiesUpdateCallback)obs_source_update);
  38. layout()->addWidget(view);
  39. layout()->setAlignment(view, Qt::AlignRight);
  40. view->show();
  41. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  42. if (resizeTimer)
  43. killTimer(resizeTimer);
  44. resizeTimer = startTimer(100);
  45. });
  46. }
  47. void OBSBasicProperties::SourceRemoved(void *data, calldata_t params)
  48. {
  49. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  50. "close");
  51. UNUSED_PARAMETER(params);
  52. }
  53. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  54. {
  55. OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
  56. if (!window->source)
  57. return;
  58. uint32_t sourceCX = obs_source_getwidth(window->source);
  59. uint32_t sourceCY = obs_source_getheight(window->source);
  60. int x, y;
  61. float scale;
  62. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  63. gs_matrix_push();
  64. gs_matrix_scale3f(scale, scale, 1.0f);
  65. gs_matrix_translate3f(-x, -y, 0.0f);
  66. obs_source_video_render(window->source);
  67. gs_matrix_pop();
  68. }
  69. void OBSBasicProperties::resizeEvent(QResizeEvent *event)
  70. {
  71. if (isVisible()) {
  72. if (resizeTimer)
  73. killTimer(resizeTimer);
  74. resizeTimer = startTimer(100);
  75. }
  76. UNUSED_PARAMETER(event);
  77. }
  78. void OBSBasicProperties::timerEvent(QTimerEvent *event)
  79. {
  80. if (event->timerId() == resizeTimer) {
  81. killTimer(resizeTimer);
  82. resizeTimer = 0;
  83. QSize size = GetPixelSize(ui->preview);
  84. obs_display_resize(display, size.width(), size.height());
  85. }
  86. }
  87. void OBSBasicProperties::Init()
  88. {
  89. gs_init_data init_data = {};
  90. show();
  91. App()->processEvents();
  92. QSize previewSize = GetPixelSize(ui->preview);
  93. init_data.cx = uint32_t(previewSize.width());
  94. init_data.cy = uint32_t(previewSize.height());
  95. init_data.format = GS_RGBA;
  96. QTToGSWindow(ui->preview->winId(), init_data.window);
  97. display = obs_display_create(&init_data);
  98. if (display)
  99. obs_display_add_draw_callback(display,
  100. OBSBasicProperties::DrawPreview, this);
  101. }