ImporterEntryPathItemDelegate.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "ImporterEntryPathItemDelegate.hpp"
  15. #include "ImporterModel.hpp"
  16. #include <OBSApp.hpp>
  17. #include <qt-wrappers.hpp>
  18. #include <QHBoxLayout>
  19. #include <QLineEdit>
  20. #include <QToolButton>
  21. #include "moc_ImporterEntryPathItemDelegate.cpp"
  22. ImporterEntryPathItemDelegate::ImporterEntryPathItemDelegate() : QStyledItemDelegate() {}
  23. QWidget *ImporterEntryPathItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  24. const QModelIndex &index) const
  25. {
  26. bool empty = index.model()
  27. ->index(index.row(), ImporterColumn::Path)
  28. .data(ImporterEntryRole::CheckEmpty)
  29. .value<bool>();
  30. QSizePolicy buttonSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Expanding,
  31. QSizePolicy::ControlType::PushButton);
  32. QWidget *container = new QWidget(parent);
  33. auto browseCallback = [this, container]() {
  34. const_cast<ImporterEntryPathItemDelegate *>(this)->handleBrowse(container);
  35. };
  36. auto clearCallback = [this, container]() {
  37. const_cast<ImporterEntryPathItemDelegate *>(this)->handleClear(container);
  38. };
  39. QHBoxLayout *layout = new QHBoxLayout();
  40. layout->setContentsMargins(0, 0, 0, 0);
  41. layout->setSpacing(0);
  42. QLineEdit *text = new QLineEdit();
  43. text->setObjectName(QStringLiteral("text"));
  44. text->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding,
  45. QSizePolicy::ControlType::LineEdit));
  46. layout->addWidget(text);
  47. QObject::connect(text, &QLineEdit::editingFinished, this, &ImporterEntryPathItemDelegate::updateText);
  48. QToolButton *browseButton = new QToolButton();
  49. browseButton->setText("...");
  50. browseButton->setSizePolicy(buttonSizePolicy);
  51. layout->addWidget(browseButton);
  52. container->connect(browseButton, &QToolButton::clicked, browseCallback);
  53. // The "clear" button is not shown in output cells
  54. // or the insertion point's input cell.
  55. if (!empty) {
  56. QToolButton *clearButton = new QToolButton();
  57. clearButton->setText("X");
  58. clearButton->setSizePolicy(buttonSizePolicy);
  59. layout->addWidget(clearButton);
  60. container->connect(clearButton, &QToolButton::clicked, clearCallback);
  61. }
  62. container->setLayout(layout);
  63. container->setFocusProxy(text);
  64. return container;
  65. }
  66. void ImporterEntryPathItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  67. {
  68. QLineEdit *text = editor->findChild<QLineEdit *>();
  69. text->setText(index.data().toString());
  70. editor->setProperty(PATH_LIST_PROP, QVariant());
  71. }
  72. void ImporterEntryPathItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  73. const QModelIndex &index) const
  74. {
  75. // We use the PATH_LIST_PROP property to pass a list of
  76. // path strings from the editor widget into the model's
  77. // NewPathsToProcessRole. This is only used when paths
  78. // are selected through the "browse" or "delete" buttons
  79. // in the editor. If the user enters new text in the
  80. // text box, we simply pass that text on to the model
  81. // as normal text data in the default role.
  82. QVariant pathListProp = editor->property(PATH_LIST_PROP);
  83. if (pathListProp.isValid()) {
  84. QStringList list = editor->property(PATH_LIST_PROP).toStringList();
  85. model->setData(index, list, ImporterEntryRole::NewPath);
  86. } else {
  87. QLineEdit *lineEdit = editor->findChild<QLineEdit *>();
  88. model->setData(index, lineEdit->text());
  89. }
  90. }
  91. void ImporterEntryPathItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  92. const QModelIndex &index) const
  93. {
  94. QStyleOptionViewItem localOption = option;
  95. initStyleOption(&localOption, index);
  96. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &localOption, painter);
  97. }
  98. void ImporterEntryPathItemDelegate::handleBrowse(QWidget *container)
  99. {
  100. QString Pattern = "(*.json *.bpres *.xml *.xconfig)";
  101. QLineEdit *text = container->findChild<QLineEdit *>();
  102. QString currentPath = text->text();
  103. bool isSet = false;
  104. QStringList paths = OpenFiles(container, QTStr("Importer.SelectCollection"), currentPath,
  105. QTStr("Importer.Collection") + QString(" ") + Pattern);
  106. if (!paths.empty()) {
  107. container->setProperty(PATH_LIST_PROP, paths);
  108. isSet = true;
  109. }
  110. if (isSet)
  111. emit commitData(container);
  112. }
  113. void ImporterEntryPathItemDelegate::handleClear(QWidget *container)
  114. {
  115. // An empty string list will indicate that the entry is being
  116. // blanked and should be deleted.
  117. container->setProperty(PATH_LIST_PROP, QStringList());
  118. emit commitData(container);
  119. }
  120. void ImporterEntryPathItemDelegate::updateText()
  121. {
  122. QLineEdit *lineEdit = dynamic_cast<QLineEdit *>(sender());
  123. QWidget *editor = lineEdit->parentWidget();
  124. emit commitData(editor);
  125. }