window-basic-properties.cpp 8.3 KB

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