window-basic-source-select.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "window-basic-main.hpp"
  15. #include "window-basic-source-select.hpp"
  16. #include "qt-wrappers.hpp"
  17. #include "obs-app.hpp"
  18. bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t source)
  19. {
  20. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  21. const char *name = obs_source_getname(source);
  22. const char *type;
  23. obs_source_gettype(source, nullptr, &type);
  24. if (strcmp(type, window->type) == 0)
  25. window->ui->sourceList->addItem(QT_UTF8(name));
  26. return true;
  27. }
  28. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t calldata)
  29. {
  30. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  31. obs_source_t source = (obs_source_t)calldata_ptr(calldata, "source");
  32. QMetaObject::invokeMethod(window, "SourceAdded",
  33. Q_ARG(OBSSource, source));
  34. }
  35. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t calldata)
  36. {
  37. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  38. obs_source_t source = (obs_source_t)calldata_ptr(calldata, "source");
  39. QMetaObject::invokeMethod(window, "SourceRemoved",
  40. Q_ARG(OBSSource, source));
  41. }
  42. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  43. {
  44. const char *name = obs_source_getname(source);
  45. const char *sourceType;
  46. obs_source_gettype(source, nullptr, &sourceType);
  47. if (strcmp(sourceType, type) != 0)
  48. return;
  49. ui->sourceList->addItem(name);
  50. }
  51. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  52. {
  53. const char *name = obs_source_getname(source);
  54. const char *sourceType;
  55. obs_source_gettype(source, nullptr, &sourceType);
  56. if (strcmp(sourceType, type) != 0)
  57. return;
  58. QList<QListWidgetItem*> items =
  59. ui->sourceList->findItems(name, Qt::MatchFixedString);
  60. if (!items.count())
  61. return;
  62. delete items[0];
  63. }
  64. static void AddExisting(const char *name)
  65. {
  66. obs_source_t source = obs_get_output_source(0);
  67. obs_scene_t scene = obs_scene_fromsource(source);
  68. if (!scene)
  69. return;
  70. source = obs_get_source_by_name(name);
  71. if (source) {
  72. obs_scene_add(scene, source);
  73. obs_source_release(source);
  74. }
  75. obs_scene_release(scene);
  76. }
  77. void AddNew(const char *id, const char *name)
  78. {
  79. obs_source_t source = obs_get_output_source(0);
  80. obs_scene_t scene = obs_scene_fromsource(source);
  81. if (!scene)
  82. return;
  83. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  84. id, name, NULL);
  85. obs_add_source(source);
  86. obs_scene_add(scene, source);
  87. obs_source_release(source);
  88. obs_scene_release(scene);
  89. }
  90. void OBSBasicSourceSelect::on_buttonBox_accepted()
  91. {
  92. bool useExisting = ui->selectExisting->isChecked();
  93. if (useExisting) {
  94. QListWidgetItem *item = ui->sourceList->currentItem();
  95. if (!item)
  96. return;
  97. AddExisting(QT_TO_UTF8(item->text()));
  98. } else {
  99. AddNew(type, QT_TO_UTF8(ui->sourceName->text()));
  100. }
  101. done(DialogCode::Accepted);
  102. }
  103. void OBSBasicSourceSelect::on_buttonBox_rejected()
  104. {
  105. done(DialogCode::Rejected);
  106. }
  107. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *type_)
  108. : QDialog (parent),
  109. ui (new Ui::OBSBasicSourceSelect),
  110. type (type_)
  111. {
  112. ui->setupUi(this);
  113. const char *placeHolderText = obs_source_getdisplayname(
  114. OBS_SOURCE_TYPE_INPUT,
  115. type_, App()->GetLocale());
  116. ui->sourceName->setText(QT_UTF8(placeHolderText));
  117. ui->sourceName->setFocus(); //Fixes deselect of text.
  118. ui->sourceName->selectAll();
  119. obs_enum_sources(EnumSources, this);
  120. }