window-basic-source-select.cpp 4.8 KB

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