File indexing completed on 2024-04-28 04:34:27

0001 /*
0002  * This file is part of KDevelop Krazy2 Plugin.
0003  *
0004  * Copyright 2012 Daniel Calviño Sánchez <danxuliu@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version 2
0009  * of the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "selectpathswidget.h"
0021 
0022 #include <QPointer>
0023 #include <QStringListModel>
0024 
0025 #include "ui_selectpathswidget.h"
0026 
0027 #include <QIcon>
0028 #include <QFileDialog>
0029 
0030 //public:
0031 
0032 SelectPathsWidget::SelectPathsWidget(const QStringList& paths, QWidget* parent /*= 0*/):
0033         QWidget(parent) {
0034     m_ui = new Ui::SelectPathsWidget();
0035     m_ui->setupUi(this);
0036 
0037     m_pathsModel = new QStringListModel(this);
0038     m_pathsModel->setStringList(m_paths);
0039     m_ui->pathsView->setModel(m_pathsModel);
0040 
0041     m_ui->pathsView->setSelectionMode(QAbstractItemView::ExtendedSelection);
0042 
0043     connect(m_ui->pathsView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
0044             this, SLOT(handleSelectionChanged()));
0045 
0046     m_ui->addButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0047     m_ui->removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0048 
0049     connect(m_ui->addButton, SIGNAL(clicked()),
0050             this, SLOT(add()));
0051     connect(m_ui->removeButton, SIGNAL(clicked()),
0052             this, SLOT(remove()));
0053 
0054     addPaths(paths);
0055 }
0056 
0057 SelectPathsWidget::~SelectPathsWidget() {
0058     delete m_ui;
0059 }
0060 
0061 QStringList SelectPathsWidget::selectedFilesAndDirectories() const {
0062     return m_paths;
0063 }
0064 
0065 //private:
0066 
0067 void SelectPathsWidget::addPaths(const QStringList& paths) {
0068     foreach (QString path, paths) { //krazy:exclude=foreach
0069         QFileInfo pathInfo(path);
0070         if (pathInfo.isDir() && !path.endsWith('/')) {
0071             path = path + '/';
0072         }
0073 
0074         if (pathInfo.isAbsolute() && pathInfo.exists() && !m_paths.contains(path)) {
0075             m_paths.append(path);
0076         }
0077     }
0078 
0079     updatePaths();
0080 }
0081 
0082 void SelectPathsWidget::clearPaths()
0083 {
0084     m_paths.clear();
0085 
0086     updatePaths();
0087 }
0088 
0089 void SelectPathsWidget::updatePaths() {
0090     m_paths.sort();
0091     m_pathsModel->setStringList(m_paths);
0092 
0093     //It seems that the selection model is reset when the string list is set in
0094     //the item model. When the selection model is reset, selectionChanged is not
0095     //emitted, so the remove button has to be explicitly disabled.
0096     m_ui->removeButton->setEnabled(false);
0097 }
0098 
0099 //private slots:
0100 
0101 void SelectPathsWidget::add() {
0102     QPointer<QFileDialog> fileDialog = new QFileDialog(this);
0103     fileDialog->setOption(QFileDialog::DontUseNativeDialog);
0104     fileDialog->setFileMode(QFileDialog::ExistingFiles);
0105 
0106     if (fileDialog->exec() == QDialog::Rejected) {
0107         return;
0108     }
0109 
0110     addPaths(fileDialog->selectedFiles());
0111 
0112     fileDialog->deleteLater();
0113 }
0114 
0115 void SelectPathsWidget::remove() {
0116     QModelIndexList indexes = m_ui->pathsView->selectionModel()->selectedIndexes();
0117 
0118     int i = 0;
0119     QMutableStringListIterator it(m_paths);
0120     while (it.hasNext() && !indexes.isEmpty()) {
0121         it.next();
0122         if (indexes.first().row() == i) {
0123             it.remove();
0124             indexes.removeFirst();
0125         }
0126         ++i;
0127     }
0128 
0129     updatePaths();
0130 }
0131 
0132 void SelectPathsWidget::handleSelectionChanged() {
0133     if (m_ui->pathsView->selectionModel()->selection().isEmpty()) {
0134         m_ui->removeButton->setEnabled(false);
0135     } else {
0136         m_ui->removeButton->setEnabled(true);
0137     }
0138 }