window-basic-properties.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_signalhandler(source), "remove",
  30. OBSBasicProperties::SourceRemoved, this)
  31. {
  32. setAttribute(Qt::WA_DeleteOnClose);
  33. ui->setupUi(this);
  34. OBSData settings = obs_source_getsettings(source);
  35. obs_data_release(settings);
  36. view = new OBSPropertiesView(settings,
  37. obs_source_properties(source, App()->GetLocale()),
  38. source, (PropertiesUpdateCallback)obs_source_update);
  39. layout()->addWidget(view);
  40. layout()->setAlignment(view, Qt::AlignRight);
  41. view->show();
  42. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  43. if (resizeTimer)
  44. killTimer(resizeTimer);
  45. resizeTimer = startTimer(100);
  46. });
  47. }
  48. void OBSBasicProperties::SourceRemoved(void *data, calldata_t params)
  49. {
  50. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  51. "close");
  52. UNUSED_PARAMETER(params);
  53. }
  54. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  55. {
  56. OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
  57. if (!window->source)
  58. return;
  59. uint32_t sourceCX = obs_source_getwidth(window->source);
  60. uint32_t sourceCY = obs_source_getheight(window->source);
  61. int x, y;
  62. float scale;
  63. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  64. gs_matrix_push();
  65. gs_matrix_scale3f(scale, scale, 1.0f);
  66. gs_matrix_translate3f(-x, -y, 0.0f);
  67. obs_source_video_render(window->source);
  68. gs_matrix_pop();
  69. }
  70. void OBSBasicProperties::resizeEvent(QResizeEvent *event)
  71. {
  72. if (isVisible()) {
  73. if (resizeTimer)
  74. killTimer(resizeTimer);
  75. resizeTimer = startTimer(100);
  76. }
  77. UNUSED_PARAMETER(event);
  78. }
  79. void OBSBasicProperties::timerEvent(QTimerEvent *event)
  80. {
  81. if (event->timerId() == resizeTimer) {
  82. killTimer(resizeTimer);
  83. resizeTimer = 0;
  84. QSize size = GetPixelSize(ui->preview);
  85. obs_display_resize(display, size.width(), size.height());
  86. }
  87. }
  88. void OBSBasicProperties::closeEvent(QCloseEvent *event)
  89. {
  90. QDialog::closeEvent(event);
  91. if (!event->isAccepted())
  92. return;
  93. // remove draw callback in case our drawable surfaces go away before
  94. // the destructor gets called
  95. obs_display_remove_draw_callback(display,
  96. OBSBasicProperties::DrawPreview, this);
  97. }
  98. void OBSBasicProperties::Init()
  99. {
  100. gs_init_data init_data = {};
  101. show();
  102. App()->processEvents();
  103. QSize previewSize = GetPixelSize(ui->preview);
  104. init_data.cx = uint32_t(previewSize.width());
  105. init_data.cy = uint32_t(previewSize.height());
  106. init_data.format = GS_RGBA;
  107. QTToGSWindow(ui->preview->winId(), init_data.window);
  108. display = obs_display_create(&init_data);
  109. if (display)
  110. obs_display_add_draw_callback(display,
  111. OBSBasicProperties::DrawPreview, this);
  112. }