window-basic-source-select.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_get_name(source);
  23. const char *id = obs_source_get_id(source);
  24. if (strcmp(id, window->id) == 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_get_name(source);
  45. const char *sourceId = obs_source_get_id(source);
  46. if (strcmp(sourceId, id) != 0)
  47. return;
  48. ui->sourceList->addItem(name);
  49. }
  50. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  51. {
  52. const char *name = obs_source_get_name(source);
  53. const char *sourceId = obs_source_get_id(source);
  54. if (strcmp(sourceId, id) != 0)
  55. return;
  56. QList<QListWidgetItem*> items =
  57. ui->sourceList->findItems(name, Qt::MatchFixedString);
  58. if (!items.count())
  59. return;
  60. delete items[0];
  61. }
  62. static void AddExisting(const char *name)
  63. {
  64. obs_source_t *source = obs_get_output_source(0);
  65. obs_scene_t *scene = obs_scene_from_source(source);
  66. if (!scene)
  67. return;
  68. source = obs_get_source_by_name(name);
  69. if (source) {
  70. obs_scene_add(scene, source);
  71. obs_source_release(source);
  72. }
  73. obs_scene_release(scene);
  74. }
  75. bool AddNew(QWidget *parent, const char *id, const char *name)
  76. {
  77. obs_source_t *source = obs_get_output_source(0);
  78. obs_scene_t *scene = obs_scene_from_source(source);
  79. bool success = false;
  80. if (!source)
  81. return false;
  82. source = obs_get_source_by_name(name);
  83. if (source) {
  84. QMessageBox::information(parent,
  85. QTStr("NameExists.Title"),
  86. QTStr("NameExists.Text"));
  87. } else {
  88. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  89. id, name, NULL, nullptr);
  90. if (source) {
  91. obs_add_source(source);
  92. obs_scene_add(scene, source);
  93. success = true;
  94. }
  95. }
  96. obs_source_release(source);
  97. obs_scene_release(scene);
  98. return success;
  99. }
  100. void OBSBasicSourceSelect::on_buttonBox_accepted()
  101. {
  102. bool useExisting = ui->selectExisting->isChecked();
  103. if (useExisting) {
  104. QListWidgetItem *item = ui->sourceList->currentItem();
  105. if (!item)
  106. return;
  107. AddExisting(QT_TO_UTF8(item->text()));
  108. } else {
  109. if (ui->sourceName->text().isEmpty()) {
  110. QMessageBox::information(this,
  111. QTStr("NoNameEntered.Title"),
  112. QTStr("NoNameEntered.Text"));
  113. return;
  114. }
  115. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text())))
  116. return;
  117. }
  118. done(DialogCode::Accepted);
  119. }
  120. void OBSBasicSourceSelect::on_buttonBox_rejected()
  121. {
  122. done(DialogCode::Rejected);
  123. }
  124. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
  125. : QDialog (parent),
  126. ui (new Ui::OBSBasicSourceSelect),
  127. id (id_)
  128. {
  129. ui->setupUi(this);
  130. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  131. QString placeHolderText{QT_UTF8(obs_source_get_display_name(
  132. OBS_SOURCE_TYPE_INPUT, id))};
  133. QString text{placeHolderText};
  134. int i = 1;
  135. obs_source_t *source = nullptr;
  136. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  137. obs_source_release(source);
  138. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  139. }
  140. ui->sourceName->setText(text);
  141. ui->sourceName->setFocus(); //Fixes deselect of text.
  142. ui->sourceName->selectAll();
  143. obs_enum_sources(EnumSources, this);
  144. }