window-basic-properties.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. preview (new OBSQTDisplay(this)),
  28. main (qobject_cast<OBSBasic*>(parent)),
  29. acceptClicked (false),
  30. source (source_),
  31. removedSignal (obs_source_get_signal_handler(source),
  32. "remove", OBSBasicProperties::SourceRemoved,
  33. this),
  34. renamedSignal (obs_source_get_signal_handler(source),
  35. "rename", OBSBasicProperties::SourceRenamed,
  36. this),
  37. oldSettings (obs_data_create()),
  38. buttonBox (new QDialogButtonBox(this))
  39. {
  40. int cx = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
  41. "cx");
  42. int cy = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
  43. "cy");
  44. buttonBox->setStandardButtons(QDialogButtonBox::Ok |
  45. QDialogButtonBox::Cancel);
  46. buttonBox->setObjectName(QStringLiteral("buttonBox"));
  47. if (cx > 400 && cy > 400)
  48. resize(cx, cy);
  49. QMetaObject::connectSlotsByName(this);
  50. /* The OBSData constructor increments the reference once */
  51. obs_data_release(oldSettings);
  52. OBSData settings = obs_source_get_settings(source);
  53. obs_data_apply(oldSettings, settings);
  54. obs_data_release(settings);
  55. view = new OBSPropertiesView(settings, source,
  56. (PropertiesReloadCallback)obs_source_properties,
  57. (PropertiesUpdateCallback)obs_source_update);
  58. preview->setMinimumSize(20, 20);
  59. preview->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
  60. QSizePolicy::Expanding));
  61. setLayout(new QVBoxLayout(this));
  62. layout()->addWidget(preview);
  63. layout()->addWidget(view);
  64. layout()->addWidget(buttonBox);
  65. layout()->setAlignment(buttonBox, Qt::AlignRight | Qt::AlignBottom);
  66. layout()->setAlignment(view, Qt::AlignBottom);
  67. view->setMaximumHeight(250);
  68. view->setMinimumHeight(150);
  69. view->show();
  70. installEventFilter(CreateShortcutFilter());
  71. const char *name = obs_source_get_name(source);
  72. setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
  73. obs_source_inc_showing(source);
  74. updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
  75. "update_properties",
  76. OBSBasicProperties::UpdateProperties,
  77. this);
  78. auto addDrawCallback = [this] ()
  79. {
  80. obs_display_add_draw_callback(preview->GetDisplay(),
  81. OBSBasicProperties::DrawPreview, this);
  82. };
  83. connect(preview.data(), &OBSQTDisplay::DisplayCreated, addDrawCallback);
  84. }
  85. OBSBasicProperties::~OBSBasicProperties()
  86. {
  87. obs_source_dec_showing(source);
  88. main->SaveProject();
  89. }
  90. void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
  91. {
  92. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  93. "close");
  94. UNUSED_PARAMETER(params);
  95. }
  96. void OBSBasicProperties::SourceRenamed(void *data, calldata_t *params)
  97. {
  98. const char *name = calldata_string(params, "new_name");
  99. QString title = QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name));
  100. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  101. "setWindowTitle", Q_ARG(QString, title));
  102. }
  103. void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
  104. {
  105. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data)->view,
  106. "ReloadProperties");
  107. }
  108. void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
  109. {
  110. QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);
  111. if (val == QDialogButtonBox::AcceptRole) {
  112. acceptClicked = true;
  113. close();
  114. if (view->DeferUpdate())
  115. view->UpdateSettings();
  116. }
  117. if (val == QDialogButtonBox::RejectRole) {
  118. obs_data_t *settings = obs_source_get_settings(source);
  119. obs_data_clear(settings);
  120. obs_data_release(settings);
  121. if (view->DeferUpdate())
  122. obs_data_apply(settings, oldSettings);
  123. else
  124. obs_source_update(source, oldSettings);
  125. close();
  126. }
  127. }
  128. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  129. {
  130. OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
  131. if (!window->source)
  132. return;
  133. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  134. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  135. int x, y;
  136. int newCX, newCY;
  137. float scale;
  138. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  139. newCX = int(scale * float(sourceCX));
  140. newCY = int(scale * float(sourceCY));
  141. gs_viewport_push();
  142. gs_projection_push();
  143. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
  144. -100.0f, 100.0f);
  145. gs_set_viewport(x, y, newCX, newCY);
  146. obs_source_video_render(window->source);
  147. gs_projection_pop();
  148. gs_viewport_pop();
  149. }
  150. void OBSBasicProperties::Cleanup()
  151. {
  152. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
  153. width());
  154. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
  155. height());
  156. }
  157. void OBSBasicProperties::reject()
  158. {
  159. if (!acceptClicked && (CheckSettings() != 0)) {
  160. if (!ConfirmQuit()) {
  161. return;
  162. }
  163. }
  164. Cleanup();
  165. done(0);
  166. }
  167. void OBSBasicProperties::closeEvent(QCloseEvent *event)
  168. {
  169. if (!acceptClicked && (CheckSettings() != 0)) {
  170. if (!ConfirmQuit()) {
  171. event->ignore();
  172. return;
  173. }
  174. }
  175. QDialog::closeEvent(event);
  176. if (!event->isAccepted())
  177. return;
  178. Cleanup();
  179. }
  180. void OBSBasicProperties::Init()
  181. {
  182. show();
  183. }
  184. int OBSBasicProperties::CheckSettings()
  185. {
  186. OBSData currentSettings = obs_source_get_settings(source);
  187. const char *oldSettingsJson = obs_data_get_json(oldSettings);
  188. const char *currentSettingsJson = obs_data_get_json(currentSettings);
  189. int ret = strcmp(currentSettingsJson, oldSettingsJson);
  190. obs_data_release(currentSettings);
  191. return ret;
  192. }
  193. bool OBSBasicProperties::ConfirmQuit()
  194. {
  195. QMessageBox::StandardButton button;
  196. button = QMessageBox::question(this,
  197. QTStr("Basic.PropertiesWindow.ConfirmTitle"),
  198. QTStr("Basic.PropertiesWindow.Confirm"),
  199. QMessageBox::Save | QMessageBox::Discard |
  200. QMessageBox::Cancel);
  201. switch (button) {
  202. case QMessageBox::Save:
  203. if (view->DeferUpdate())
  204. view->UpdateSettings();
  205. // Do nothing because the settings are already updated
  206. break;
  207. case QMessageBox::Discard:
  208. obs_source_update(source, oldSettings);
  209. break;
  210. case QMessageBox::Cancel:
  211. return false;
  212. break;
  213. default:
  214. /* If somehow the dialog fails to show, just default to
  215. * saving the settings. */
  216. break;
  217. }
  218. return true;
  219. }