window-basic-properties.cpp 14 KB

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