window-basic-properties.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. QPushButton *b;
  45. b = buttonBox->addButton(QTStr("OK"), QDialogButtonBox::AcceptRole);
  46. buttonBox->addButton(QTStr("Cancel"), QDialogButtonBox::RejectRole);
  47. buttonBox->addButton(QTStr("Defaults"), QDialogButtonBox::ResetRole);
  48. buttonBox->setObjectName(QStringLiteral("buttonBox"));
  49. b->setDefault(true);
  50. if (cx > 400 && cy > 400)
  51. resize(cx, cy);
  52. else
  53. resize(720, 580);
  54. QMetaObject::connectSlotsByName(this);
  55. /* The OBSData constructor increments the reference once */
  56. obs_data_release(oldSettings);
  57. OBSData settings = obs_source_get_settings(source);
  58. obs_data_apply(oldSettings, settings);
  59. obs_data_release(settings);
  60. view = new OBSPropertiesView(settings, source,
  61. (PropertiesReloadCallback)obs_source_properties,
  62. (PropertiesUpdateCallback)obs_source_update);
  63. view->setMinimumHeight(150);
  64. preview->setMinimumSize(20, 150);
  65. preview->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
  66. QSizePolicy::Expanding));
  67. // Create a QSplitter to keep a unified workflow here.
  68. windowSplitter = new QSplitter(Qt::Orientation::Vertical, this);
  69. windowSplitter->addWidget(preview);
  70. windowSplitter->addWidget(view);
  71. windowSplitter->setChildrenCollapsible(false);
  72. //windowSplitter->setSizes(QList<int>({ 16777216, 150 }));
  73. windowSplitter->setStretchFactor(0, 3);
  74. windowSplitter->setStretchFactor(1, 1);
  75. setLayout(new QVBoxLayout(this));
  76. layout()->addWidget(windowSplitter);
  77. layout()->addWidget(buttonBox);
  78. layout()->setAlignment(buttonBox, Qt::AlignRight | Qt::AlignBottom);
  79. view->show();
  80. installEventFilter(CreateShortcutFilter());
  81. const char *name = obs_source_get_name(source);
  82. setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
  83. obs_source_inc_showing(source);
  84. updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
  85. "update_properties",
  86. OBSBasicProperties::UpdateProperties,
  87. this);
  88. auto addDrawCallback = [this] ()
  89. {
  90. obs_display_add_draw_callback(preview->GetDisplay(),
  91. OBSBasicProperties::DrawPreview, this);
  92. };
  93. enum obs_source_type type = obs_source_get_type(source);
  94. uint32_t caps = obs_source_get_output_flags(source);
  95. bool drawable_type = type == OBS_SOURCE_TYPE_INPUT ||
  96. type == OBS_SOURCE_TYPE_SCENE;
  97. if (drawable_type && (caps & OBS_SOURCE_VIDEO) != 0)
  98. connect(preview.data(), &OBSQTDisplay::DisplayCreated,
  99. addDrawCallback);
  100. }
  101. OBSBasicProperties::~OBSBasicProperties()
  102. {
  103. obs_source_dec_showing(source);
  104. main->SaveProject();
  105. }
  106. void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
  107. {
  108. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  109. "close");
  110. UNUSED_PARAMETER(params);
  111. }
  112. void OBSBasicProperties::SourceRenamed(void *data, calldata_t *params)
  113. {
  114. const char *name = calldata_string(params, "new_name");
  115. QString title = QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name));
  116. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
  117. "setWindowTitle", Q_ARG(QString, title));
  118. }
  119. void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
  120. {
  121. QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data)->view,
  122. "ReloadProperties");
  123. }
  124. void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
  125. {
  126. QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);
  127. if (val == QDialogButtonBox::AcceptRole) {
  128. acceptClicked = true;
  129. close();
  130. if (view->DeferUpdate())
  131. view->UpdateSettings();
  132. } else if (val == QDialogButtonBox::RejectRole) {
  133. obs_data_t *settings = obs_source_get_settings(source);
  134. obs_data_clear(settings);
  135. obs_data_release(settings);
  136. if (view->DeferUpdate())
  137. obs_data_apply(settings, oldSettings);
  138. else
  139. obs_source_update(source, oldSettings);
  140. close();
  141. } else if (val == QDialogButtonBox::ResetRole) {
  142. obs_data_t *settings = obs_source_get_settings(source);
  143. obs_data_clear(settings);
  144. obs_data_release(settings);
  145. if (!view->DeferUpdate())
  146. obs_source_update(source, nullptr);
  147. view->RefreshProperties();
  148. }
  149. }
  150. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  151. {
  152. OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
  153. if (!window->source)
  154. return;
  155. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  156. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  157. int x, y;
  158. int newCX, newCY;
  159. float scale;
  160. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  161. newCX = int(scale * float(sourceCX));
  162. newCY = int(scale * float(sourceCY));
  163. gs_viewport_push();
  164. gs_projection_push();
  165. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
  166. -100.0f, 100.0f);
  167. gs_set_viewport(x, y, newCX, newCY);
  168. obs_source_video_render(window->source);
  169. gs_projection_pop();
  170. gs_viewport_pop();
  171. }
  172. void OBSBasicProperties::Cleanup()
  173. {
  174. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
  175. width());
  176. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
  177. height());
  178. obs_display_remove_draw_callback(preview->GetDisplay(),
  179. OBSBasicProperties::DrawPreview, this);
  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. show();
  207. }
  208. int OBSBasicProperties::CheckSettings()
  209. {
  210. OBSData currentSettings = obs_source_get_settings(source);
  211. const char *oldSettingsJson = obs_data_get_json(oldSettings);
  212. const char *currentSettingsJson = obs_data_get_json(currentSettings);
  213. int ret = strcmp(currentSettingsJson, oldSettingsJson);
  214. obs_data_release(currentSettings);
  215. return ret;
  216. }
  217. bool OBSBasicProperties::ConfirmQuit()
  218. {
  219. QMessageBox::StandardButton button;
  220. button = OBSMessageBox::question(this,
  221. QTStr("Basic.PropertiesWindow.ConfirmTitle"),
  222. QTStr("Basic.PropertiesWindow.Confirm"),
  223. QMessageBox::Save | QMessageBox::Discard |
  224. QMessageBox::Cancel);
  225. switch (button) {
  226. case QMessageBox::Save:
  227. if (view->DeferUpdate())
  228. view->UpdateSettings();
  229. // Do nothing because the settings are already updated
  230. break;
  231. case QMessageBox::Discard:
  232. obs_source_update(source, oldSettings);
  233. break;
  234. case QMessageBox::Cancel:
  235. return false;
  236. break;
  237. default:
  238. /* If somehow the dialog fails to show, just default to
  239. * saving the settings. */
  240. break;
  241. }
  242. return true;
  243. }