window-basic-properties.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include <QMessageBox>
  23. using namespace std;
  24. OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
  25. : QDialog (parent),
  26. main (qobject_cast<OBSBasic*>(parent)),
  27. resizeTimer (0),
  28. acceptClicked (false),
  29. ui (new Ui::OBSBasicProperties),
  30. source (source_),
  31. removedSignal (obs_source_get_signal_handler(source),
  32. "remove", OBSBasicProperties::SourceRemoved,
  33. this),
  34. oldSettings (obs_data_create()),
  35. buttonBox (new QDialogButtonBox(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. buttonBox->setStandardButtons(QDialogButtonBox::Ok |
  42. QDialogButtonBox::Cancel);
  43. buttonBox->setObjectName(QStringLiteral("buttonBox"));
  44. ui->setupUi(this);
  45. if (cx > 400 && cy > 400)
  46. resize(cx, cy);
  47. OBSData settings = obs_source_get_settings(source);
  48. obs_data_apply(oldSettings, settings);
  49. obs_data_release(settings);
  50. view = new OBSPropertiesView(settings, source,
  51. (PropertiesReloadCallback)obs_source_properties,
  52. (PropertiesUpdateCallback)obs_source_update);
  53. layout()->addWidget(view);
  54. layout()->addWidget(buttonBox);
  55. layout()->setAlignment(buttonBox, Qt::AlignRight | Qt::AlignBottom);
  56. layout()->setAlignment(view, Qt::AlignBottom);
  57. view->setMaximumHeight(250);
  58. view->setMinimumHeight(150);
  59. view->show();
  60. connect(view, SIGNAL(PropertiesResized()),
  61. this, SLOT(OnPropertiesResized()));
  62. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  63. if (resizeTimer)
  64. killTimer(resizeTimer);
  65. resizeTimer = startTimer(100);
  66. });
  67. const char *name = obs_source_get_name(source);
  68. setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
  69. obs_source_inc_showing(source);
  70. updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
  71. "update_properties",
  72. OBSBasicProperties::UpdateProperties,
  73. this);
  74. }
  75. OBSBasicProperties::~OBSBasicProperties()
  76. {
  77. obs_source_dec_showing(source);
  78. }
  79. void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
  80. {
  81. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  82. "close");
  83. UNUSED_PARAMETER(params);
  84. }
  85. void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
  86. {
  87. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data)->view,
  88. "ReloadProperties");
  89. }
  90. void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
  91. {
  92. QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);
  93. if (val == QDialogButtonBox::AcceptRole) {
  94. acceptClicked = true;
  95. close();
  96. }
  97. if (val == QDialogButtonBox::RejectRole) {
  98. obs_data_t *settings = obs_source_get_settings(source);
  99. obs_data_clear(settings);
  100. obs_data_release(settings);
  101. obs_source_update(source, oldSettings);
  102. close();
  103. }
  104. }
  105. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  106. {
  107. OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
  108. if (!window->source)
  109. return;
  110. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  111. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  112. int x, y;
  113. int newCX, newCY;
  114. float scale;
  115. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  116. newCX = int(scale * float(sourceCX));
  117. newCY = int(scale * float(sourceCY));
  118. gs_viewport_push();
  119. gs_projection_push();
  120. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
  121. -100.0f, 100.0f);
  122. gs_set_viewport(x, y, newCX, newCY);
  123. obs_source_video_render(window->source);
  124. gs_projection_pop();
  125. gs_viewport_pop();
  126. }
  127. void OBSBasicProperties::OnPropertiesResized()
  128. {
  129. if (resizeTimer)
  130. killTimer(resizeTimer);
  131. resizeTimer = startTimer(100);
  132. }
  133. void OBSBasicProperties::resizeEvent(QResizeEvent *event)
  134. {
  135. if (isVisible()) {
  136. if (resizeTimer)
  137. killTimer(resizeTimer);
  138. resizeTimer = startTimer(100);
  139. }
  140. QDialog::resizeEvent(event);
  141. }
  142. void OBSBasicProperties::timerEvent(QTimerEvent *event)
  143. {
  144. if (event->timerId() == resizeTimer) {
  145. killTimer(resizeTimer);
  146. resizeTimer = 0;
  147. QSize size = GetPixelSize(ui->preview);
  148. obs_display_resize(display, size.width(), size.height());
  149. }
  150. }
  151. void OBSBasicProperties::closeEvent(QCloseEvent *event)
  152. {
  153. if (!acceptClicked && (CheckSettings() != 0)) {
  154. if (!ConfirmQuit()) {
  155. event->ignore();
  156. return;
  157. }
  158. }
  159. QDialog::closeEvent(event);
  160. if (!event->isAccepted())
  161. return;
  162. obs_data_release(oldSettings);
  163. // remove draw callback and release display in case our drawable
  164. // surfaces go away before the destructor gets called
  165. obs_display_remove_draw_callback(display,
  166. OBSBasicProperties::DrawPreview, this);
  167. display = nullptr;
  168. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
  169. width());
  170. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
  171. height());
  172. }
  173. void OBSBasicProperties::Init()
  174. {
  175. gs_init_data init_data = {};
  176. show();
  177. QSize previewSize = GetPixelSize(ui->preview);
  178. init_data.cx = uint32_t(previewSize.width());
  179. init_data.cy = uint32_t(previewSize.height());
  180. init_data.format = GS_RGBA;
  181. QTToGSWindow(ui->preview->winId(), init_data.window);
  182. display = obs_display_create(&init_data);
  183. if (display)
  184. obs_display_add_draw_callback(display,
  185. OBSBasicProperties::DrawPreview, this);
  186. }
  187. int OBSBasicProperties::CheckSettings()
  188. {
  189. OBSData currentSettings = obs_source_get_settings(source);
  190. const char *oldSettingsJson = obs_data_get_json(oldSettings);
  191. const char *currentSettingsJson = obs_data_get_json(currentSettings);
  192. int ret = strcmp(currentSettingsJson, oldSettingsJson);
  193. obs_data_release(currentSettings);
  194. return ret;
  195. }
  196. bool OBSBasicProperties::ConfirmQuit()
  197. {
  198. QMessageBox::StandardButton button;
  199. button = QMessageBox::question(this,
  200. QTStr("Basic.PropertiesWindow.ConfirmTitle"),
  201. QTStr("Basic.PropertiesWindow.Confirm"),
  202. QMessageBox::Save | QMessageBox::Discard |
  203. QMessageBox::Cancel);
  204. switch (button) {
  205. case QMessageBox::Save:
  206. // Do nothing because the settings are already updated
  207. break;
  208. case QMessageBox::Discard:
  209. obs_source_update(source, oldSettings);
  210. break;
  211. case QMessageBox::Cancel:
  212. return false;
  213. break;
  214. default:
  215. /* If somehow the dialog fails to show, just default to
  216. * saving the settings. */
  217. break;
  218. }
  219. return true;
  220. }