GUI: SelectFilesDialog - tweaked behaviour when unchecking a folder and checking a subfolder

This commit is contained in:
Daniel Marjamaki 2012-01-11 06:20:30 +01:00
parent 9beb38a74d
commit 8df66c9df7
1 changed files with 38 additions and 17 deletions

View File

@ -4,21 +4,31 @@
#include <QTreeView> #include <QTreeView>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QFileSystemModel> #include <QFileSystemModel>
#include <QList> #include <QStringList>
#include <QString>
#include <QPushButton> #include <QPushButton>
class SelectFilesModel : public QFileSystemModel { class SelectFilesModel : public QFileSystemModel {
private: private:
QList<QString> selected; QStringList selected;
QList<QString> unselected; QStringList unselected;
int getindex(const QList<QString> &s, const QString &filepath) const { /**
* 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;
for (int i = 0; i < s.size(); ++i) { for (int i = 0; i < s.size(); ++i) {
if (filepath.startsWith(s[i])) if (filepath.startsWith(s[i])) {
return i; if (s[i].size() > matchlen)
matchindex = i;
} }
return -1; }
return matchindex;
} }
public: public:
@ -39,8 +49,12 @@ public:
QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const { QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const {
if (role == Qt::CheckStateRole) { if (role == Qt::CheckStateRole) {
const QString filepath = filePath(index); const QString filepath = filePath(index);
if (getindex(selected,filepath) >= 0 && int selindex = getindex(selected, filepath);
getindex(unselected,filepath) == -1) int unselindex = getindex(unselected, filepath);
if (selindex >= 0 && unselindex == -1)
return Qt::Checked;
if (selindex >= 0 && unselindex >= 0 &&
selected[selindex].size() > unselected[unselindex].size())
return Qt::Checked; return Qt::Checked;
return Qt::Unchecked; return Qt::Unchecked;
@ -55,16 +69,23 @@ public:
// remove unchecked path // remove unchecked path
unselected.removeAll(filepath); unselected.removeAll(filepath);
} else if (selected.indexOf(filepath) != -1) { } else if (selected.indexOf(filepath) != -1) {
// remove checked path // remove child selected paths
selected.removeAll(filepath); for (int i = selected.size() - 1; i >= 0; --i) {
if (selected[i].startsWith(filepath))
selected.removeAt(i);
}
// remove child unchecked paths // remove child unselected paths
int i; for (int i = unselected.size() - 1; i >= 0; --i) {
while ((i = getindex(unselected,filepath)) != -1) { if (unselected[i].startsWith(filepath))
unselected.removeAt(i); unselected.removeAt(i);
} }
} else { } else {
if (getindex(selected,filepath) == -1) 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())
selected.append(filepath); selected.append(filepath);
else else
unselected.append(filepath); unselected.append(filepath);