1
0

window-basic-properties.cpp 8.7 KB

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