| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "compilersetdirectorieswidget.h" |
| 18 | #include "ui_compilersetdirectorieswidget.h" |
| 19 | #include "../iconsmanager.h" |
| 20 | |
| 21 | #include <QFileDialog> |
| 22 | #include <QStringListModel> |
| 23 | #include <QDebug> |
| 24 | |
| 25 | CompilerSetDirectoriesWidget::CompilerSetDirectoriesWidget(QWidget *parent) : |
| 26 | QWidget(parent), |
| 27 | ui(new Ui::CompilerSetDirectoriesWidget) |
| 28 | { |
| 29 | ui->setupUi(this); |
| 30 | |
| 31 | mModel = new CompilerSetDirectoriesWidget::ListModel(); |
| 32 | ui->listView->setModel(mModel); |
| 33 | connect(ui->listView->selectionModel(), &QItemSelectionModel::selectionChanged, |
| 34 | this, &CompilerSetDirectoriesWidget::selectionChanged); |
| 35 | ui->listView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 36 | onUpdateIcons(); |
| 37 | connect(pIconsManager, &IconsManager::actionIconsUpdated, |
| 38 | this, &CompilerSetDirectoriesWidget::onUpdateIcons); |
| 39 | } |
| 40 | |
| 41 | CompilerSetDirectoriesWidget::~CompilerSetDirectoriesWidget() |
| 42 | { |
| 43 | delete ui; |
| 44 | } |
| 45 | |
| 46 | void CompilerSetDirectoriesWidget::setDirList(const QStringList &list) |
| 47 | { |
| 48 | mModel->setStringList(list); |
| 49 | QModelIndexList lst =ui->listView->selectionModel()->selectedIndexes(); |
| 50 | ui->btnDelete->setEnabled(lst.count()>0); |
| 51 | } |
| 52 | |
| 53 | QStringList CompilerSetDirectoriesWidget::dirList() const |
| 54 | { |
| 55 | return mModel->stringList(); |
| 56 | } |
| 57 | |
| 58 | Qt::ItemFlags CompilerSetDirectoriesWidget::ListModel::flags(const QModelIndex &index) const |
| 59 | { |
| 60 | Qt::ItemFlags flags = Qt::NoItemFlags; |
| 61 | if (index.isValid()) { |
| 62 | flags = Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable ; |
| 63 | } else if (index.row() == -1) { |
| 64 | // -1 means it's a drop target? |
| 65 | flags = Qt::ItemIsDropEnabled; |
| 66 | } |
| 67 | return flags; |
| 68 | } |
| 69 | |
| 70 | void CompilerSetDirectoriesWidget::on_btnAdd_pressed() |
| 71 | { |
| 72 | QString folder = QFileDialog::getExistingDirectory(this,tr("Choose Folder" )); |
| 73 | if (!folder.isEmpty()) { |
| 74 | int row = mModel->rowCount(); |
| 75 | mModel->insertRow(row); |
| 76 | QModelIndex index= mModel->index(row,0); |
| 77 | mModel->setData(index,folder,Qt::DisplayRole); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void CompilerSetDirectoriesWidget::selectionChanged(const QItemSelection &selected, const QItemSelection &/*deselected*/) |
| 82 | { |
| 83 | ui->btnDelete->setEnabled(!selected.isEmpty()); |
| 84 | } |
| 85 | |
| 86 | void CompilerSetDirectoriesWidget::on_btnDelete_pressed() |
| 87 | { |
| 88 | QModelIndexList lst =ui->listView->selectionModel()->selectedIndexes(); |
| 89 | if (lst.count()>0) { |
| 90 | mModel->removeRow(lst[0].row()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void CompilerSetDirectoriesWidget::on_btnRemoveInvalid_pressed() |
| 96 | { |
| 97 | QStringList lst; |
| 98 | for (const QString& folder : dirList() ) { |
| 99 | QFileInfo info(folder); |
| 100 | if (info.exists() && info.isDir() ) { |
| 101 | lst.append(folder.trimmed()); |
| 102 | } |
| 103 | } |
| 104 | setDirList(lst); |
| 105 | } |
| 106 | |
| 107 | void CompilerSetDirectoriesWidget::onUpdateIcons() |
| 108 | { |
| 109 | pIconsManager->setIcon(ui->btnAdd,IconsManager::ACTION_MISC_ADD); |
| 110 | pIconsManager->setIcon(ui->btnDelete, IconsManager::ACTION_MISC_REMOVE); |
| 111 | pIconsManager->setIcon(ui->btnRemoveInvalid, IconsManager::ACTION_MISC_VALIDATE); |
| 112 | } |
| 113 | |