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 <QTreeView>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#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-11 06:20:30 +01:00
|
|
|
QStringList selected;
|
|
|
|
QStringList unselected;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get index in stringlist where start of string matches. If
|
|
|
|
* many strings in the stringlist match then return the index
|
|
|
|
* for the longest string.
|
|
|
|
* \param s stringlist with filepaths
|
|
|
|
* \param filepath the filepath that is matched against the stringlist
|
|
|
|
*/
|
|
|
|
int getindex(const QStringList &s, const QString &filepath) const {
|
|
|
|
int matchlen = 0;
|
|
|
|
int matchindex = -1;
|
2012-01-10 22:39:22 +01:00
|
|
|
for (int i = 0; i < s.size(); ++i) {
|
2012-01-11 06:20:30 +01:00
|
|
|
if (filepath.startsWith(s[i])) {
|
2012-01-13 06:18:56 +01:00
|
|
|
// not a real match of paths..
|
|
|
|
if (s[i].size() < filepath.size() && filepath[s[i].size()] != '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// paths match. the return value is the index for the
|
|
|
|
// longest match
|
2012-01-11 06:20:30 +01:00
|
|
|
if (s[i].size() > matchlen)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-11 06:20:30 +01:00
|
|
|
int selindex = getindex(selected, filepath);
|
|
|
|
int unselindex = getindex(unselected, filepath);
|
|
|
|
if (selindex >= 0 && unselindex == -1)
|
|
|
|
return Qt::Checked;
|
|
|
|
if (selindex >= 0 && unselindex >= 0 &&
|
2012-01-12 21:21:51 +01:00
|
|
|
selected[selindex].size() > unselected[unselindex].size())
|
2012-01-10 22:39:22 +01:00
|
|
|
return Qt::Checked;
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (unselected.indexOf(filepath) != -1) {
|
|
|
|
// remove unchecked path
|
|
|
|
unselected.removeAll(filepath);
|
|
|
|
} else if (selected.indexOf(filepath) != -1) {
|
2012-01-11 06:20:30 +01:00
|
|
|
// remove child selected paths
|
|
|
|
for (int i = selected.size() - 1; i >= 0; --i) {
|
|
|
|
if (selected[i].startsWith(filepath))
|
|
|
|
selected.removeAt(i);
|
|
|
|
}
|
2012-01-10 22:39:22 +01:00
|
|
|
|
2012-01-11 06:20:30 +01:00
|
|
|
// remove child unselected paths
|
|
|
|
for (int i = unselected.size() - 1; i >= 0; --i) {
|
|
|
|
if (unselected[i].startsWith(filepath))
|
|
|
|
unselected.removeAt(i);
|
2012-01-10 22:39:22 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-11 06:20:30 +01:00
|
|
|
int selindex = getindex(selected, filepath);
|
|
|
|
int unselindex = getindex(unselected, filepath);
|
|
|
|
if (selindex == -1)
|
|
|
|
selected.append(filepath);
|
|
|
|
else if (unselindex >= 0 && selected[selindex].size() < unselected[unselindex].size())
|
2012-01-10 22:39:22 +01:00
|
|
|
selected.append(filepath);
|
|
|
|
else
|
|
|
|
unselected.append(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowCount(index) > 0)
|
|
|
|
emit(dataChanged(index, index.child(rowCount(index)-1,0)));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return QFileSystemModel::setData(index, value, role);
|
|
|
|
}
|
2012-01-12 07:51:59 +01:00
|
|
|
|
|
|
|
QStringList getFiles() const {
|
|
|
|
QStringList ret;
|
|
|
|
|
|
|
|
// List all files in "selected" folders..
|
|
|
|
FileList fileLister;
|
|
|
|
fileLister.AddPathList(selected);
|
|
|
|
ret = fileLister.GetFileList();
|
|
|
|
|
|
|
|
// Remove all items from ret that are unselected but not selected..
|
|
|
|
for (int i = ret.size() - 1; i >= 0; i--) {
|
|
|
|
int unselindex = getindex(unselected, ret[i]);
|
|
|
|
if (unselindex == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// both selected and unselected, check which to rely on
|
|
|
|
int selindex = getindex(selected, ret[i]);
|
|
|
|
if (selected[selindex].size() < unselected[unselindex].size())
|
|
|
|
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();
|
|
|
|
}
|