1
0

window-basic-source-select.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. struct AddSourceData {
  20. obs_source_t *source;
  21. bool visible;
  22. };
  23. bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t *source)
  24. {
  25. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  26. const char *name = obs_source_get_name(source);
  27. const char *id = obs_source_get_id(source);
  28. if (strcmp(id, window->id) == 0)
  29. window->ui->sourceList->addItem(QT_UTF8(name));
  30. return true;
  31. }
  32. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t *calldata)
  33. {
  34. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  35. obs_source_t *source = (obs_source_t*)calldata_ptr(calldata, "source");
  36. QMetaObject::invokeMethod(window, "SourceAdded",
  37. Q_ARG(OBSSource, source));
  38. }
  39. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t *calldata)
  40. {
  41. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  42. obs_source_t *source = (obs_source_t*)calldata_ptr(calldata, "source");
  43. QMetaObject::invokeMethod(window, "SourceRemoved",
  44. Q_ARG(OBSSource, source));
  45. }
  46. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  47. {
  48. const char *name = obs_source_get_name(source);
  49. const char *sourceId = obs_source_get_id(source);
  50. if (strcmp(sourceId, id) != 0)
  51. return;
  52. ui->sourceList->addItem(name);
  53. }
  54. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  55. {
  56. const char *name = obs_source_get_name(source);
  57. const char *sourceId = obs_source_get_id(source);
  58. if (strcmp(sourceId, id) != 0)
  59. return;
  60. QList<QListWidgetItem*> items =
  61. ui->sourceList->findItems(name, Qt::MatchFixedString);
  62. if (!items.count())
  63. return;
  64. delete items[0];
  65. }
  66. static void AddSource(void *_data, obs_scene_t *scene)
  67. {
  68. AddSourceData *data = (AddSourceData *)_data;
  69. obs_sceneitem_t *sceneitem;
  70. sceneitem = obs_scene_add(scene, data->source);
  71. obs_sceneitem_set_visible(sceneitem, data->visible);
  72. }
  73. static void AddExisting(const char *name, const bool visible)
  74. {
  75. obs_source_t *source = obs_get_output_source(0);
  76. obs_scene_t *scene = obs_scene_from_source(source);
  77. if (!scene)
  78. return;
  79. source = obs_get_source_by_name(name);
  80. if (source) {
  81. AddSourceData data;
  82. data.source = source;
  83. data.visible = visible;
  84. obs_scene_atomic_update(scene, AddSource, &data);
  85. obs_source_release(source);
  86. }
  87. obs_scene_release(scene);
  88. }
  89. bool AddNew(QWidget *parent, const char *id, const char *name,
  90. const bool visible, OBSSource &newSource)
  91. {
  92. obs_source_t *source = obs_get_output_source(0);
  93. obs_scene_t *scene = obs_scene_from_source(source);
  94. bool success = false;
  95. if (!source)
  96. return false;
  97. source = obs_get_source_by_name(name);
  98. if (source) {
  99. QMessageBox::information(parent,
  100. QTStr("NameExists.Title"),
  101. QTStr("NameExists.Text"));
  102. } else {
  103. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  104. id, name, NULL, nullptr);
  105. if (source) {
  106. obs_add_source(source);
  107. AddSourceData data;
  108. data.source = source;
  109. data.visible = visible;
  110. obs_scene_atomic_update(scene, AddSource, &data);
  111. newSource = source;
  112. success = true;
  113. }
  114. }
  115. obs_source_release(source);
  116. obs_scene_release(scene);
  117. return success;
  118. }
  119. void OBSBasicSourceSelect::on_buttonBox_accepted()
  120. {
  121. bool useExisting = ui->selectExisting->isChecked();
  122. bool visible = ui->sourceVisible->isChecked();
  123. if (useExisting) {
  124. QListWidgetItem *item = ui->sourceList->currentItem();
  125. if (!item)
  126. return;
  127. AddExisting(QT_TO_UTF8(item->text()), visible);
  128. } else {
  129. if (ui->sourceName->text().isEmpty()) {
  130. QMessageBox::information(this,
  131. QTStr("NoNameEntered.Title"),
  132. QTStr("NoNameEntered.Text"));
  133. return;
  134. }
  135. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  136. visible, newSource))
  137. return;
  138. }
  139. done(DialogCode::Accepted);
  140. }
  141. void OBSBasicSourceSelect::on_buttonBox_rejected()
  142. {
  143. done(DialogCode::Rejected);
  144. }
  145. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
  146. : QDialog (parent),
  147. ui (new Ui::OBSBasicSourceSelect),
  148. id (id_)
  149. {
  150. ui->setupUi(this);
  151. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  152. QString placeHolderText{QT_UTF8(obs_source_get_display_name(
  153. OBS_SOURCE_TYPE_INPUT, id))};
  154. QString text{placeHolderText};
  155. int i = 1;
  156. obs_source_t *source = nullptr;
  157. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  158. obs_source_release(source);
  159. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  160. }
  161. ui->sourceName->setText(text);
  162. ui->sourceName->setFocus(); //Fixes deselect of text.
  163. ui->sourceName->selectAll();
  164. installEventFilter(CreateShortcutFilter());
  165. obs_enum_sources(EnumSources, this);
  166. }