window-basic-properties.cpp 15 KB

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