2012-04-14 05:01:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
|
2012-04-14 05:01:12 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-04-14 16:07:37 +02:00
|
|
|
|
2012-01-10 22:39:22 +01:00
|
|
|
#include "selectfilesdialog.h"
|
2012-01-11 21:32:52 +01:00
|
|
|
#include "ui_selectfilesdialog.h"
|
2012-01-12 07:51:59 +01:00
|
|
|
#include "filelist.h"
|
2012-01-10 22:39:22 +01:00
|
|
|
|
|
|
|
#include <QFileSystemModel>
|
2012-01-11 06:20:30 +01:00
|
|
|
#include <QStringList>
|
2012-01-10 22:39:22 +01:00
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
class SelectFilesModel : public QFileSystemModel {
|
|
|
|
private:
|
2012-01-15 07:59:54 +01:00
|
|
|
/**
|
|
|
|
* paths that are user-checked. on the screen all children
|
|
|
|
* for these paths will appear to be checked too unless
|
|
|
|
* they are "unchecked".
|
|
|
|
*/
|
|
|
|
QStringList checked;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* paths that are user-unchecked.
|
|
|
|
*/
|
|
|
|
QStringList unchecked;
|
2012-01-11 06:20:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get index in stringlist where start of string matches. If
|
|
|
|
* many strings in the stringlist match then return the index
|
|
|
|
* for the longest string.
|
2012-01-15 07:59:54 +01:00
|
|
|
* \param paths stringlist with filepaths
|
2012-01-11 06:20:30 +01:00
|
|
|
* \param filepath the filepath that is matched against the stringlist
|
|
|
|
*/
|
2012-01-15 07:59:54 +01:00
|
|
|
int getindex(const QStringList &paths, const QString &filepath) const {
|
2012-01-11 06:20:30 +01:00
|
|
|
int matchlen = 0;
|
|
|
|
int matchindex = -1;
|
2012-01-15 07:59:54 +01:00
|
|
|
for (int i = 0; i < paths.size(); ++i) {
|
|
|
|
if (filepath.startsWith(paths[i])) {
|
2012-01-13 06:18:56 +01:00
|
|
|
// not a real match of paths..
|
2012-01-15 07:59:54 +01:00
|
|
|
if (paths[i].size() < filepath.size() && filepath[paths[i].size()] != '/')
|
2012-01-13 06:18:56 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// paths match. the return value is the index for the
|
|
|
|
// longest match
|
2012-01-15 07:59:54 +01:00
|
|
|
if (paths[i].size() > matchlen)
|
2012-01-11 06:20:30 +01:00
|
|
|
matchindex = i;
|
|
|
|
}
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
2012-01-11 06:20:30 +01:00
|
|
|
return matchindex;
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
/**
|
|
|
|
* Is filepath partially checked?
|
|
|
|
* \param filepath the filepath to investigate
|
|
|
|
* \param checkindex result from getindex(checked,filepath). If not given the getindex will be called.
|
|
|
|
* \return true if filepath is partially checked
|
|
|
|
*/
|
|
|
|
bool partiallyChecked(const QString &filepath, int checkindex = -2) const {
|
2012-01-15 07:43:50 +01:00
|
|
|
const QString filepath2 = filepath.endsWith("/") ? filepath : (filepath + "/");
|
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
for (int i = 0; i < unchecked.size(); ++i) {
|
|
|
|
if (unchecked[i].startsWith(filepath2)) {
|
|
|
|
return true;
|
2012-01-15 07:43:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
if (checkindex == -2)
|
|
|
|
checkindex = getindex(checked, filepath);
|
|
|
|
|
|
|
|
|
|
|
|
if (checkindex == -1) {
|
|
|
|
for (int i = 0; i < checked.size(); ++i) {
|
|
|
|
if (checked[i].startsWith(filepath2)) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-15 07:43:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 22:39:22 +01:00
|
|
|
public:
|
|
|
|
SelectFilesModel() : QFileSystemModel() {
|
2012-01-12 07:51:59 +01:00
|
|
|
class FileLister : private FileList {
|
|
|
|
public:
|
|
|
|
static QStringList filters() {
|
|
|
|
return GetDefaultFilters();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setNameFilters(FileLister::filters());
|
2012-01-10 22:39:22 +01:00
|
|
|
setNameFilterDisables(false);
|
2012-01-11 06:01:14 +01:00
|
|
|
setRootPath("/");
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags flags(const QModelIndex& index) const {
|
|
|
|
if (index.column() == 0)
|
|
|
|
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
|
|
|
|
return QFileSystemModel::flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const {
|
|
|
|
if (role == Qt::CheckStateRole) {
|
|
|
|
const QString filepath = filePath(index);
|
2012-01-15 07:59:54 +01:00
|
|
|
const int checkindex = getindex(checked, filepath);
|
|
|
|
const int uncheckindex = getindex(unchecked, filepath);
|
2012-01-15 07:43:50 +01:00
|
|
|
|
|
|
|
// If some children are not checked then this item should be partially checked..
|
2012-01-15 07:59:54 +01:00
|
|
|
if (partiallyChecked(filepath, checkindex))
|
2012-01-15 07:43:50 +01:00
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
|
|
|
|
// Is item selected but not unselected?
|
2012-01-15 07:59:54 +01:00
|
|
|
if (checkindex >= 0 && uncheckindex == -1)
|
2012-01-11 06:20:30 +01:00
|
|
|
return Qt::Checked;
|
2012-01-15 07:59:54 +01:00
|
|
|
if (checkindex >= 0 && uncheckindex >= 0 &&
|
|
|
|
checked[checkindex].size() > unchecked[uncheckindex].size())
|
2012-01-10 22:39:22 +01:00
|
|
|
return Qt::Checked;
|
|
|
|
|
2012-01-15 07:43:50 +01:00
|
|
|
// Item is either not selected at all or else it is unselected
|
2012-01-10 22:39:22 +01:00
|
|
|
return Qt::Unchecked;
|
|
|
|
}
|
|
|
|
return QFileSystemModel::data(index, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setData(const QModelIndex& index, const QVariant& value, int role) {
|
|
|
|
if (role == Qt::CheckStateRole) {
|
|
|
|
const QString filepath = filePath(index);
|
2012-01-15 07:43:50 +01:00
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
bool partiallychecked = partiallyChecked(filepath);
|
2012-01-15 07:43:50 +01:00
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
if (unchecked.indexOf(filepath) != -1) {
|
2012-01-10 22:39:22 +01:00
|
|
|
// remove unchecked path
|
2012-01-15 07:59:54 +01:00
|
|
|
unchecked.removeAll(filepath);
|
|
|
|
} else if (partiallychecked || checked.indexOf(filepath) != -1) {
|
2012-01-11 06:20:30 +01:00
|
|
|
// remove child selected paths
|
2012-01-15 07:59:54 +01:00
|
|
|
for (int i = checked.size() - 1; i >= 0; --i) {
|
|
|
|
if (checked[i].startsWith(filepath))
|
|
|
|
checked.removeAt(i);
|
2012-01-11 06:20:30 +01:00
|
|
|
}
|
2012-01-10 22:39:22 +01:00
|
|
|
|
2012-01-11 06:20:30 +01:00
|
|
|
// remove child unselected paths
|
2012-01-15 07:59:54 +01:00
|
|
|
for (int i = unchecked.size() - 1; i >= 0; --i) {
|
|
|
|
if (unchecked[i].startsWith(filepath))
|
|
|
|
unchecked.removeAt(i);
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
2012-01-15 07:43:50 +01:00
|
|
|
|
|
|
|
// If partialChecked then select this item
|
2012-01-15 07:59:54 +01:00
|
|
|
if (partiallychecked)
|
|
|
|
checked.append(filepath);
|
2012-01-10 22:39:22 +01:00
|
|
|
} else {
|
2012-01-15 07:59:54 +01:00
|
|
|
const int checkindex = getindex(checked, filepath);
|
|
|
|
const int uncheckindex = getindex(unchecked, filepath);
|
|
|
|
if (checkindex == -1)
|
|
|
|
checked.append(filepath);
|
|
|
|
else if (uncheckindex >= 0 && checked[checkindex].size() < unchecked[uncheckindex].size())
|
|
|
|
checked.append(filepath);
|
2012-01-10 22:39:22 +01:00
|
|
|
else
|
2012-01-15 07:59:54 +01:00
|
|
|
unchecked.append(filepath);
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rowCount(index) > 0)
|
|
|
|
emit(dataChanged(index, index.child(rowCount(index)-1,0)));
|
|
|
|
|
2012-01-15 07:43:50 +01:00
|
|
|
// update parents
|
|
|
|
QModelIndex parent = index.parent();
|
|
|
|
while (parent != QModelIndex()) {
|
|
|
|
emit(dataChanged(parent,parent));
|
|
|
|
parent = parent.parent();
|
|
|
|
}
|
|
|
|
|
2012-01-10 22:39:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return QFileSystemModel::setData(index, value, role);
|
|
|
|
}
|
2012-01-12 07:51:59 +01:00
|
|
|
|
|
|
|
QStringList getFiles() const {
|
|
|
|
QStringList ret;
|
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
// List all files in "checked" folders..
|
2012-01-12 07:51:59 +01:00
|
|
|
FileList fileLister;
|
2012-01-15 07:59:54 +01:00
|
|
|
fileLister.AddPathList(checked);
|
2012-01-12 07:51:59 +01:00
|
|
|
ret = fileLister.GetFileList();
|
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
// Remove all items from ret that are unchecked but not checked..
|
2012-01-12 07:51:59 +01:00
|
|
|
for (int i = ret.size() - 1; i >= 0; i--) {
|
2012-01-15 07:59:54 +01:00
|
|
|
int uncheckindex = getindex(unchecked, ret[i]);
|
|
|
|
if (uncheckindex == -1)
|
2012-01-12 07:51:59 +01:00
|
|
|
continue;
|
|
|
|
|
2012-01-15 07:59:54 +01:00
|
|
|
// both checked and unchecked, check which to rely on
|
|
|
|
int checkindex = getindex(checked, ret[i]);
|
|
|
|
if (checked[checkindex].size() < unchecked[uncheckindex].size())
|
2012-01-12 07:51:59 +01:00
|
|
|
ret.removeAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-01-10 22:39:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-11 21:32:52 +01:00
|
|
|
SelectFilesDialog::SelectFilesDialog(QWidget *w) :
|
|
|
|
QDialog(w),
|
|
|
|
ui(new Ui::SelectFilesDialog)
|
2012-01-10 22:39:22 +01:00
|
|
|
{
|
2012-01-11 21:32:52 +01:00
|
|
|
ui->setupUi(this);
|
2012-01-10 22:39:22 +01:00
|
|
|
|
2012-01-12 07:51:59 +01:00
|
|
|
selectfilesmodel = new SelectFilesModel;
|
|
|
|
|
|
|
|
ui->treeView->setModel(selectfilesmodel);
|
2012-01-10 22:39:22 +01:00
|
|
|
for (int i = 1; i < 4; ++i)
|
2012-01-11 21:32:52 +01:00
|
|
|
ui->treeView->setColumnHidden(i, true);
|
2012-01-13 06:33:10 +01:00
|
|
|
|
|
|
|
// Change text of "OK" button to "Check"
|
|
|
|
QPushButton *okbutton = ui->buttonBox->button(QDialogButtonBox::Ok);
|
|
|
|
if (okbutton)
|
|
|
|
okbutton->setText(tr("Check"));
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 07:51:59 +01:00
|
|
|
SelectFilesDialog::~SelectFilesDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2012-01-10 22:39:22 +01:00
|
|
|
|
2012-01-12 07:51:59 +01:00
|
|
|
QStringList SelectFilesDialog::getFiles() const
|
|
|
|
{
|
|
|
|
return selectfilesmodel->getFiles();
|
|
|
|
}
|