1
0

ImporterModel.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /******************************************************************************
  2. Copyright (C) 2019-2020 by Dillon Pentz <[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 "ImporterModel.hpp"
  15. #include <OBSApp.hpp>
  16. #include <importers/importers.hpp>
  17. #include "moc_ImporterModel.cpp"
  18. int ImporterModel::rowCount(const QModelIndex &) const
  19. {
  20. return options.length() + 1;
  21. }
  22. int ImporterModel::columnCount(const QModelIndex &) const
  23. {
  24. return ImporterColumn::Count;
  25. }
  26. QVariant ImporterModel::data(const QModelIndex &index, int role) const
  27. {
  28. QVariant result = QVariant();
  29. if (index.row() >= options.length()) {
  30. if (role == ImporterEntryRole::CheckEmpty)
  31. result = true;
  32. else
  33. return QVariant();
  34. } else if (role == Qt::DisplayRole) {
  35. switch (index.column()) {
  36. case ImporterColumn::Path:
  37. result = options[index.row()].path;
  38. break;
  39. case ImporterColumn::Program:
  40. result = options[index.row()].program;
  41. break;
  42. case ImporterColumn::Name:
  43. result = options[index.row()].name;
  44. }
  45. } else if (role == Qt::EditRole) {
  46. if (index.column() == ImporterColumn::Name) {
  47. result = options[index.row()].name;
  48. }
  49. } else if (role == Qt::CheckStateRole) {
  50. switch (index.column()) {
  51. case ImporterColumn::Selected:
  52. if (options[index.row()].program != "")
  53. result = options[index.row()].selected ? Qt::Checked : Qt::Unchecked;
  54. else
  55. result = Qt::Unchecked;
  56. }
  57. } else if (role == ImporterEntryRole::CheckEmpty) {
  58. result = options[index.row()].empty;
  59. }
  60. return result;
  61. }
  62. Qt::ItemFlags ImporterModel::flags(const QModelIndex &index) const
  63. {
  64. Qt::ItemFlags flags = QAbstractTableModel::flags(index);
  65. if (index.column() == ImporterColumn::Selected && index.row() != options.length()) {
  66. flags |= Qt::ItemIsUserCheckable;
  67. } else if (index.column() == ImporterColumn::Path ||
  68. (index.column() == ImporterColumn::Name && index.row() != options.length())) {
  69. flags |= Qt::ItemIsEditable;
  70. }
  71. return flags;
  72. }
  73. void ImporterModel::checkInputPath(int row)
  74. {
  75. ImporterEntry &entry = options[row];
  76. if (entry.path.isEmpty()) {
  77. entry.program = "";
  78. entry.empty = true;
  79. entry.selected = false;
  80. entry.name = "";
  81. } else {
  82. entry.empty = false;
  83. std::string program = DetectProgram(entry.path.toStdString());
  84. entry.program = QTStr(program.c_str());
  85. if (program.empty()) {
  86. entry.selected = false;
  87. } else {
  88. std::string name = GetSCName(entry.path.toStdString(), program);
  89. entry.name = name.c_str();
  90. }
  91. }
  92. emit dataChanged(index(row, 0), index(row, ImporterColumn::Count));
  93. }
  94. bool ImporterModel::setData(const QModelIndex &index, const QVariant &value, int role)
  95. {
  96. if (role == ImporterEntryRole::NewPath) {
  97. QStringList list = value.toStringList();
  98. if (list.size() == 0) {
  99. if (index.row() < options.size()) {
  100. beginRemoveRows(QModelIndex(), index.row(), index.row());
  101. options.removeAt(index.row());
  102. endRemoveRows();
  103. }
  104. } else {
  105. if (list.size() > 0 && index.row() < options.length()) {
  106. options[index.row()].path = list[0];
  107. checkInputPath(index.row());
  108. list.removeAt(0);
  109. }
  110. if (list.size() > 0) {
  111. int row = index.row();
  112. int lastRow = row + list.size() - 1;
  113. beginInsertRows(QModelIndex(), row, lastRow);
  114. for (QString path : list) {
  115. ImporterEntry entry;
  116. entry.path = path;
  117. entry.selected = true;
  118. entry.empty = true;
  119. options.insert(row, entry);
  120. row++;
  121. }
  122. endInsertRows();
  123. for (row = index.row(); row <= lastRow; row++) {
  124. checkInputPath(row);
  125. }
  126. }
  127. }
  128. } else if (index.row() == options.length()) {
  129. QString path = value.toString();
  130. if (!path.isEmpty()) {
  131. ImporterEntry entry;
  132. entry.path = path;
  133. entry.selected = role != ImporterEntryRole::AutoPath;
  134. entry.empty = false;
  135. beginInsertRows(QModelIndex(), options.length() + 1, options.length() + 1);
  136. options.append(entry);
  137. endInsertRows();
  138. checkInputPath(index.row());
  139. }
  140. } else if (index.column() == ImporterColumn::Selected) {
  141. bool select = value.toBool();
  142. options[index.row()].selected = select;
  143. } else if (index.column() == ImporterColumn::Path) {
  144. QString path = value.toString();
  145. options[index.row()].path = path;
  146. checkInputPath(index.row());
  147. } else if (index.column() == ImporterColumn::Name) {
  148. QString name = value.toString();
  149. options[index.row()].name = name;
  150. }
  151. emit dataChanged(index, index);
  152. return true;
  153. }
  154. QVariant ImporterModel::headerData(int section, Qt::Orientation orientation, int role) const
  155. {
  156. QVariant result = QVariant();
  157. if (role == Qt::DisplayRole && orientation == Qt::Orientation::Horizontal) {
  158. switch (section) {
  159. case ImporterColumn::Path:
  160. result = QTStr("Importer.Path");
  161. break;
  162. case ImporterColumn::Program:
  163. result = QTStr("Importer.Program");
  164. break;
  165. case ImporterColumn::Name:
  166. result = QTStr("Name");
  167. }
  168. }
  169. return result;
  170. }