MissingFilesPathItemDelegate.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /******************************************************************************
  2. Copyright (C) 2019 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 "MissingFilesPathItemDelegate.hpp"
  15. #include <OBSApp.hpp>
  16. #include <QFileDialog>
  17. #include <QHBoxLayout>
  18. #include <QLineEdit>
  19. #include <QToolButton>
  20. #include "moc_MissingFilesPathItemDelegate.cpp"
  21. enum MissingFilesRole { EntryStateRole = Qt::UserRole, NewPathsToProcessRole };
  22. MissingFilesPathItemDelegate::MissingFilesPathItemDelegate(bool isOutput, const QString &defaultPath)
  23. : QStyledItemDelegate(),
  24. isOutput(isOutput),
  25. defaultPath(defaultPath)
  26. {
  27. }
  28. QWidget *MissingFilesPathItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  29. const QModelIndex &) const
  30. {
  31. QSizePolicy buttonSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Expanding,
  32. QSizePolicy::ControlType::PushButton);
  33. QWidget *container = new QWidget(parent);
  34. auto browseCallback = [this, container]() {
  35. const_cast<MissingFilesPathItemDelegate *>(this)->handleBrowse(container);
  36. };
  37. auto clearCallback = [this, container]() {
  38. const_cast<MissingFilesPathItemDelegate *>(this)->handleClear(container);
  39. };
  40. QHBoxLayout *layout = new QHBoxLayout();
  41. layout->setContentsMargins(0, 0, 0, 0);
  42. layout->setSpacing(0);
  43. QLineEdit *text = new QLineEdit();
  44. text->setObjectName(QStringLiteral("text"));
  45. text->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding,
  46. QSizePolicy::ControlType::LineEdit));
  47. layout->addWidget(text);
  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 input cells
  54. if (isOutput) {
  55. QToolButton *clearButton = new QToolButton();
  56. clearButton->setText("X");
  57. clearButton->setSizePolicy(buttonSizePolicy);
  58. layout->addWidget(clearButton);
  59. container->connect(clearButton, &QToolButton::clicked, clearCallback);
  60. }
  61. container->setLayout(layout);
  62. container->setFocusProxy(text);
  63. return container;
  64. }
  65. void MissingFilesPathItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  66. {
  67. QLineEdit *text = editor->findChild<QLineEdit *>();
  68. text->setText(index.data().toString());
  69. editor->setProperty(PATH_LIST_PROP, QVariant());
  70. }
  71. void MissingFilesPathItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  72. const QModelIndex &index) const
  73. {
  74. // We use the PATH_LIST_PROP property to pass a list of
  75. // path strings from the editor widget into the model's
  76. // NewPathsToProcessRole. This is only used when paths
  77. // are selected through the "browse" or "delete" buttons
  78. // in the editor. If the user enters new text in the
  79. // text box, we simply pass that text on to the model
  80. // as normal text data in the default role.
  81. QVariant pathListProp = editor->property(PATH_LIST_PROP);
  82. if (pathListProp.isValid()) {
  83. QStringList list = editor->property(PATH_LIST_PROP).toStringList();
  84. if (isOutput) {
  85. model->setData(index, list);
  86. } else
  87. model->setData(index, list, MissingFilesRole::NewPathsToProcessRole);
  88. } else {
  89. QLineEdit *lineEdit = editor->findChild<QLineEdit *>();
  90. model->setData(index, lineEdit->text(), 0);
  91. }
  92. }
  93. void MissingFilesPathItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  94. const QModelIndex &index) const
  95. {
  96. QStyleOptionViewItem localOption = option;
  97. initStyleOption(&localOption, index);
  98. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &localOption, painter);
  99. }
  100. void MissingFilesPathItemDelegate::handleBrowse(QWidget *container)
  101. {
  102. QLineEdit *text = container->findChild<QLineEdit *>();
  103. QString currentPath = text->text();
  104. if (currentPath.isEmpty() || currentPath.compare(QTStr("MissingFiles.Clear")) == 0)
  105. currentPath = defaultPath;
  106. bool isSet = false;
  107. if (isOutput) {
  108. QString newPath =
  109. QFileDialog::getOpenFileName(container, QTStr("MissingFiles.SelectFile"), currentPath, nullptr);
  110. #ifdef __APPLE__
  111. // TODO: Revisit when QTBUG-42661 is fixed
  112. container->window()->raise();
  113. #endif
  114. if (!newPath.isEmpty()) {
  115. container->setProperty(PATH_LIST_PROP, QStringList() << newPath);
  116. isSet = true;
  117. }
  118. }
  119. if (isSet)
  120. emit commitData(container);
  121. }
  122. void MissingFilesPathItemDelegate::handleClear(QWidget *container)
  123. {
  124. // An empty string list will indicate that the entry is being
  125. // blanked and should be deleted.
  126. container->setProperty(PATH_LIST_PROP, QStringList() << QTStr("MissingFiles.Clear"));
  127. container->findChild<QLineEdit *>()->clearFocus();
  128. ((QWidget *)container->parent())->setFocus();
  129. emit commitData(container);
  130. }