window-basic-properties.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. #include <obs-data.h>
  25. #include <obs.h>
  26. #include <qpointer.h>
  27. #include <util/c99defs.h>
  28. using namespace std;
  29. static void CreateTransitionScene(OBSSource scene, const char *text,
  30. uint32_t color);
  31. OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
  32. : QDialog(parent),
  33. preview(new OBSQTDisplay(this)),
  34. main(qobject_cast<OBSBasic *>(parent)),
  35. acceptClicked(false),
  36. source(source_),
  37. removedSignal(obs_source_get_signal_handler(source), "remove",
  38. OBSBasicProperties::SourceRemoved, this),
  39. renamedSignal(obs_source_get_signal_handler(source), "rename",
  40. OBSBasicProperties::SourceRenamed, this),
  41. oldSettings(obs_data_create()),
  42. buttonBox(new QDialogButtonBox(this))
  43. {
  44. int cx = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
  45. "cx");
  46. int cy = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
  47. "cy");
  48. enum obs_source_type type = obs_source_get_type(source);
  49. buttonBox->setObjectName(QStringLiteral("buttonBox"));
  50. buttonBox->setStandardButtons(QDialogButtonBox::Ok |
  51. QDialogButtonBox::Cancel |
  52. QDialogButtonBox::RestoreDefaults);
  53. buttonBox->button(QDialogButtonBox::Ok)->setText(QTStr("OK"));
  54. buttonBox->button(QDialogButtonBox::Cancel)->setText(QTStr("Cancel"));
  55. buttonBox->button(QDialogButtonBox::RestoreDefaults)
  56. ->setText(QTStr("Defaults"));
  57. if (cx > 400 && cy > 400)
  58. resize(cx, cy);
  59. else
  60. resize(720, 580);
  61. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  62. QMetaObject::connectSlotsByName(this);
  63. /* The OBSData constructor increments the reference once */
  64. obs_data_release(oldSettings);
  65. OBSData nd_settings = obs_source_get_settings(source);
  66. OBSData settings = obs_data_get_defaults(nd_settings);
  67. obs_data_apply(settings, nd_settings);
  68. obs_data_apply(oldSettings, settings);
  69. obs_data_release(settings);
  70. obs_data_release(nd_settings);
  71. auto handle_memory = [](void *vp, obs_data_t *old_settings,
  72. obs_data_t *new_settings) {
  73. obs_source_t *source = reinterpret_cast<obs_source_t *>(vp);
  74. obs_source_update(source, new_settings);
  75. UNUSED_PARAMETER(old_settings);
  76. UNUSED_PARAMETER(vp);
  77. };
  78. view = new OBSPropertiesView(
  79. nd_settings, source,
  80. (PropertiesReloadCallback)obs_source_properties,
  81. (PropertiesUpdateCallback)handle_memory,
  82. (PropertiesVisualUpdateCb)obs_source_update);
  83. view->setMinimumHeight(150);
  84. preview->setMinimumSize(20, 150);
  85. preview->setSizePolicy(
  86. QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
  87. // Create a QSplitter to keep a unified workflow here.
  88. windowSplitter = new QSplitter(Qt::Orientation::Vertical, this);
  89. windowSplitter->addWidget(preview);
  90. windowSplitter->addWidget(view);
  91. windowSplitter->setChildrenCollapsible(false);
  92. //windowSplitter->setSizes(QList<int>({ 16777216, 150 }));
  93. windowSplitter->setStretchFactor(0, 3);
  94. windowSplitter->setStretchFactor(1, 1);
  95. setLayout(new QVBoxLayout(this));
  96. layout()->addWidget(windowSplitter);
  97. if (type == OBS_SOURCE_TYPE_TRANSITION) {
  98. AddPreviewButton();
  99. connect(view, SIGNAL(PropertiesRefreshed()), this,
  100. SLOT(AddPreviewButton()));
  101. }
  102. layout()->addWidget(buttonBox);
  103. layout()->setAlignment(buttonBox, Qt::AlignBottom);
  104. view->show();
  105. installEventFilter(CreateShortcutFilter());
  106. const char *name = obs_source_get_name(source);
  107. setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
  108. obs_source_inc_showing(source);
  109. updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
  110. "update_properties",
  111. OBSBasicProperties::UpdateProperties,
  112. this);
  113. auto addDrawCallback = [this]() {
  114. obs_display_add_draw_callback(preview->GetDisplay(),
  115. OBSBasicProperties::DrawPreview,
  116. this);
  117. };
  118. auto addTransitionDrawCallback = [this]() {
  119. obs_display_add_draw_callback(
  120. preview->GetDisplay(),
  121. OBSBasicProperties::DrawTransitionPreview, this);
  122. };
  123. uint32_t caps = obs_source_get_output_flags(source);
  124. bool drawable_type = type == OBS_SOURCE_TYPE_INPUT ||
  125. type == OBS_SOURCE_TYPE_SCENE;
  126. bool drawable_preview = (caps & OBS_SOURCE_VIDEO) != 0;
  127. if (drawable_preview && drawable_type) {
  128. preview->show();
  129. connect(preview.data(), &OBSQTDisplay::DisplayCreated,
  130. addDrawCallback);
  131. } else if (type == OBS_SOURCE_TYPE_TRANSITION) {
  132. sourceA =
  133. obs_source_create_private("scene", "sourceA", nullptr);
  134. sourceB =
  135. obs_source_create_private("scene", "sourceB", nullptr);
  136. obs_source_release(sourceA);
  137. obs_source_release(sourceB);
  138. uint32_t colorA = 0xFFB26F52;
  139. uint32_t colorB = 0xFF6FB252;
  140. CreateTransitionScene(sourceA, "A", colorA);
  141. CreateTransitionScene(sourceB, "B", colorB);
  142. /**
  143. * The cloned source is made from scratch, rather than using
  144. * obs_source_duplicate, as the stinger transition would not
  145. * play correctly otherwise.
  146. */
  147. obs_data_t *settings = obs_source_get_settings(source);
  148. sourceClone = obs_source_create_private(
  149. obs_source_get_id(source), "clone", settings);
  150. obs_source_release(sourceClone);
  151. obs_source_inc_active(sourceClone);
  152. obs_transition_set(sourceClone, sourceA);
  153. obs_data_release(settings);
  154. auto updateCallback = [=]() {
  155. obs_data_t *settings = obs_source_get_settings(source);
  156. obs_source_update(sourceClone, settings);
  157. obs_transition_clear(sourceClone);
  158. obs_transition_set(sourceClone, sourceA);
  159. obs_transition_force_stop(sourceClone);
  160. obs_data_release(settings);
  161. direction = true;
  162. };
  163. connect(view, &OBSPropertiesView::Changed, updateCallback);
  164. preview->show();
  165. connect(preview.data(), &OBSQTDisplay::DisplayCreated,
  166. addTransitionDrawCallback);
  167. } else {
  168. preview->hide();
  169. }
  170. }
  171. OBSBasicProperties::~OBSBasicProperties()
  172. {
  173. if (sourceClone) {
  174. obs_source_dec_active(sourceClone);
  175. }
  176. obs_source_dec_showing(source);
  177. main->SaveProject();
  178. main->UpdateContextBar();
  179. }
  180. void OBSBasicProperties::AddPreviewButton()
  181. {
  182. QPushButton *playButton =
  183. new QPushButton(QTStr("PreviewTransition"), this);
  184. VScrollArea *area = view;
  185. area->widget()->layout()->addWidget(playButton);
  186. playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  187. auto play = [=]() {
  188. OBSSource start;
  189. OBSSource end;
  190. if (direction) {
  191. start = sourceA;
  192. end = sourceB;
  193. } else {
  194. start = sourceB;
  195. end = sourceA;
  196. }
  197. obs_transition_set(sourceClone, start);
  198. obs_transition_start(sourceClone, OBS_TRANSITION_MODE_AUTO,
  199. main->GetTransitionDuration(), end);
  200. direction = !direction;
  201. start = nullptr;
  202. end = nullptr;
  203. };
  204. connect(playButton, &QPushButton::clicked, play);
  205. }
  206. static obs_source_t *CreateLabel(const char *name, size_t h)
  207. {
  208. obs_data_t *settings = obs_data_create();
  209. obs_data_t *font = obs_data_create();
  210. std::string text;
  211. text += " ";
  212. text += name;
  213. text += " ";
  214. #if defined(_WIN32)
  215. obs_data_set_string(font, "face", "Arial");
  216. #elif defined(__APPLE__)
  217. obs_data_set_string(font, "face", "Helvetica");
  218. #else
  219. obs_data_set_string(font, "face", "Monospace");
  220. #endif
  221. obs_data_set_int(font, "flags", 1); // Bold text
  222. obs_data_set_int(font, "size", min(int(h), 300));
  223. obs_data_set_obj(settings, "font", font);
  224. obs_data_set_string(settings, "text", text.c_str());
  225. obs_data_set_bool(settings, "outline", false);
  226. #ifdef _WIN32
  227. const char *text_source_id = "text_gdiplus";
  228. #else
  229. const char *text_source_id = "text_ft2_source";
  230. #endif
  231. obs_source_t *txtSource =
  232. obs_source_create_private(text_source_id, name, settings);
  233. obs_data_release(font);
  234. obs_data_release(settings);
  235. return txtSource;
  236. }
  237. static void CreateTransitionScene(OBSSource scene, const char *text,
  238. uint32_t color)
  239. {
  240. obs_data_t *settings = obs_data_create();
  241. obs_data_set_int(settings, "width", obs_source_get_width(scene));
  242. obs_data_set_int(settings, "height", obs_source_get_height(scene));
  243. obs_data_set_int(settings, "color", color);
  244. obs_source_t *colorBG = obs_source_create_private(
  245. "color_source", "background", settings);
  246. obs_scene_add(obs_scene_from_source(scene), colorBG);
  247. obs_source_t *label = CreateLabel(text, obs_source_get_height(scene));
  248. obs_sceneitem_t *item =
  249. obs_scene_add(obs_scene_from_source(scene), label);
  250. vec2 size;
  251. vec2_set(&size, obs_source_get_width(scene),
  252. #ifdef _WIN32
  253. obs_source_get_height(scene));
  254. #else
  255. obs_source_get_height(scene) * 0.8);
  256. #endif
  257. obs_sceneitem_set_bounds(item, &size);
  258. obs_sceneitem_set_bounds_type(item, OBS_BOUNDS_SCALE_INNER);
  259. obs_data_release(settings);
  260. obs_source_release(colorBG);
  261. obs_source_release(label);
  262. }
  263. void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
  264. {
  265. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
  266. "close");
  267. UNUSED_PARAMETER(params);
  268. }
  269. void OBSBasicProperties::SourceRenamed(void *data, calldata_t *params)
  270. {
  271. const char *name = calldata_string(params, "new_name");
  272. QString title = QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name));
  273. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
  274. "setWindowTitle", Q_ARG(QString, title));
  275. }
  276. void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
  277. {
  278. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data)->view,
  279. "ReloadProperties");
  280. }
  281. void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
  282. {
  283. QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);
  284. if (val == QDialogButtonBox::AcceptRole) {
  285. std::string scene_name =
  286. obs_source_get_name(main->GetCurrentSceneSource());
  287. auto undo_redo = [scene_name](const std::string &data) {
  288. obs_data_t *settings =
  289. obs_data_create_from_json(data.c_str());
  290. obs_source_t *source = obs_get_source_by_name(
  291. obs_data_get_string(settings, "undo_sname"));
  292. obs_source_reset_settings(source, settings);
  293. obs_source_update_properties(source);
  294. obs_source_t *scene_source =
  295. obs_get_source_by_name(scene_name.c_str());
  296. OBSBasic::Get()->SetCurrentScene(source, true);
  297. obs_source_release(scene_source);
  298. obs_data_release(settings);
  299. obs_source_release(source);
  300. };
  301. obs_data_t *new_settings = obs_data_create();
  302. obs_data_t *curr_settings = obs_source_get_settings(source);
  303. obs_data_apply(new_settings, curr_settings);
  304. obs_data_set_string(new_settings, "undo_sname",
  305. obs_source_get_name(source));
  306. obs_data_set_string(oldSettings, "undo_sname",
  307. obs_source_get_name(source));
  308. std::string undo_data(obs_data_get_json(oldSettings));
  309. std::string redo_data(obs_data_get_json(new_settings));
  310. if (undo_data.compare(redo_data) != 0)
  311. main->undo_s.add_action(
  312. QTStr("Undo.Properties")
  313. .arg(obs_source_get_name(source)),
  314. undo_redo, undo_redo, undo_data, redo_data);
  315. obs_data_release(new_settings);
  316. obs_data_release(curr_settings);
  317. acceptClicked = true;
  318. close();
  319. if (view->DeferUpdate())
  320. view->UpdateSettings();
  321. } else if (val == QDialogButtonBox::RejectRole) {
  322. obs_data_t *settings = obs_source_get_settings(source);
  323. obs_data_clear(settings);
  324. obs_data_release(settings);
  325. if (view->DeferUpdate())
  326. obs_data_apply(settings, oldSettings);
  327. else
  328. obs_source_update(source, oldSettings);
  329. close();
  330. } else if (val == QDialogButtonBox::ResetRole) {
  331. obs_data_t *settings = obs_source_get_settings(source);
  332. obs_data_clear(settings);
  333. obs_data_release(settings);
  334. if (!view->DeferUpdate())
  335. obs_source_update(source, nullptr);
  336. view->ReloadProperties();
  337. }
  338. }
  339. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  340. {
  341. OBSBasicProperties *window = static_cast<OBSBasicProperties *>(data);
  342. if (!window->source)
  343. return;
  344. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  345. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  346. int x, y;
  347. int newCX, newCY;
  348. float scale;
  349. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  350. newCX = int(scale * float(sourceCX));
  351. newCY = int(scale * float(sourceCY));
  352. gs_viewport_push();
  353. gs_projection_push();
  354. const bool previous = gs_set_linear_srgb(true);
  355. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
  356. gs_set_viewport(x, y, newCX, newCY);
  357. obs_source_video_render(window->source);
  358. gs_set_linear_srgb(previous);
  359. gs_projection_pop();
  360. gs_viewport_pop();
  361. }
  362. void OBSBasicProperties::DrawTransitionPreview(void *data, uint32_t cx,
  363. uint32_t cy)
  364. {
  365. OBSBasicProperties *window = static_cast<OBSBasicProperties *>(data);
  366. if (!window->sourceClone)
  367. return;
  368. uint32_t sourceCX = max(obs_source_get_width(window->sourceClone), 1u);
  369. uint32_t sourceCY = max(obs_source_get_height(window->sourceClone), 1u);
  370. int x, y;
  371. int newCX, newCY;
  372. float scale;
  373. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  374. newCX = int(scale * float(sourceCX));
  375. newCY = int(scale * float(sourceCY));
  376. gs_viewport_push();
  377. gs_projection_push();
  378. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
  379. gs_set_viewport(x, y, newCX, newCY);
  380. obs_source_video_render(window->sourceClone);
  381. gs_projection_pop();
  382. gs_viewport_pop();
  383. }
  384. void OBSBasicProperties::Cleanup()
  385. {
  386. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
  387. width());
  388. config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
  389. height());
  390. obs_display_remove_draw_callback(preview->GetDisplay(),
  391. OBSBasicProperties::DrawPreview, this);
  392. obs_display_remove_draw_callback(
  393. preview->GetDisplay(),
  394. OBSBasicProperties::DrawTransitionPreview, this);
  395. }
  396. void OBSBasicProperties::reject()
  397. {
  398. if (!acceptClicked && (CheckSettings() != 0)) {
  399. if (!ConfirmQuit()) {
  400. return;
  401. }
  402. }
  403. Cleanup();
  404. done(0);
  405. }
  406. void OBSBasicProperties::closeEvent(QCloseEvent *event)
  407. {
  408. if (!acceptClicked && (CheckSettings() != 0)) {
  409. if (!ConfirmQuit()) {
  410. event->ignore();
  411. return;
  412. }
  413. }
  414. QDialog::closeEvent(event);
  415. if (!event->isAccepted())
  416. return;
  417. Cleanup();
  418. }
  419. void OBSBasicProperties::Init()
  420. {
  421. show();
  422. }
  423. int OBSBasicProperties::CheckSettings()
  424. {
  425. OBSData currentSettings = obs_source_get_settings(source);
  426. const char *oldSettingsJson = obs_data_get_json(oldSettings);
  427. const char *currentSettingsJson = obs_data_get_json(currentSettings);
  428. int ret = strcmp(currentSettingsJson, oldSettingsJson);
  429. obs_data_release(currentSettings);
  430. return ret;
  431. }
  432. bool OBSBasicProperties::ConfirmQuit()
  433. {
  434. QMessageBox::StandardButton button;
  435. button = OBSMessageBox::question(
  436. this, QTStr("Basic.PropertiesWindow.ConfirmTitle"),
  437. QTStr("Basic.PropertiesWindow.Confirm"),
  438. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  439. switch (button) {
  440. case QMessageBox::Save:
  441. acceptClicked = true;
  442. if (view->DeferUpdate())
  443. view->UpdateSettings();
  444. // Do nothing because the settings are already updated
  445. break;
  446. case QMessageBox::Discard:
  447. obs_source_update(source, oldSettings);
  448. break;
  449. case QMessageBox::Cancel:
  450. return false;
  451. break;
  452. default:
  453. /* If somehow the dialog fails to show, just default to
  454. * saving the settings. */
  455. break;
  456. }
  457. return true;
  458. }