window-basic-properties.cpp 14 KB

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