window-basic-properties.cpp 8.3 KB

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