GUI: Refactorings. Changed names. Added comments.
This commit is contained in:
parent
fe12b280b9
commit
9f493c7c51
|
@ -11,51 +11,67 @@
|
||||||
|
|
||||||
class SelectFilesModel : public QFileSystemModel {
|
class SelectFilesModel : public QFileSystemModel {
|
||||||
private:
|
private:
|
||||||
QStringList selected;
|
/**
|
||||||
QStringList unselected;
|
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get index in stringlist where start of string matches. If
|
* Get index in stringlist where start of string matches. If
|
||||||
* many strings in the stringlist match then return the index
|
* many strings in the stringlist match then return the index
|
||||||
* for the longest string.
|
* for the longest string.
|
||||||
* \param s stringlist with filepaths
|
* \param paths stringlist with filepaths
|
||||||
* \param filepath the filepath that is matched against the stringlist
|
* \param filepath the filepath that is matched against the stringlist
|
||||||
*/
|
*/
|
||||||
int getindex(const QStringList &s, const QString &filepath) const {
|
int getindex(const QStringList &paths, const QString &filepath) const {
|
||||||
int matchlen = 0;
|
int matchlen = 0;
|
||||||
int matchindex = -1;
|
int matchindex = -1;
|
||||||
for (int i = 0; i < s.size(); ++i) {
|
for (int i = 0; i < paths.size(); ++i) {
|
||||||
if (filepath.startsWith(s[i])) {
|
if (filepath.startsWith(paths[i])) {
|
||||||
// not a real match of paths..
|
// not a real match of paths..
|
||||||
if (s[i].size() < filepath.size() && filepath[s[i].size()] != '/')
|
if (paths[i].size() < filepath.size() && filepath[paths[i].size()] != '/')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// paths match. the return value is the index for the
|
// paths match. the return value is the index for the
|
||||||
// longest match
|
// longest match
|
||||||
if (s[i].size() > matchlen)
|
if (paths[i].size() > matchlen)
|
||||||
matchindex = i;
|
matchindex = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matchindex;
|
return matchindex;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool partiallySelected(const QString &filepath, int selindex = -2) const {
|
/**
|
||||||
if (selindex == -2)
|
* Is filepath partially checked?
|
||||||
selindex = getindex(selected, filepath);
|
* \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 {
|
||||||
const QString filepath2 = filepath.endsWith("/") ? filepath : (filepath + "/");
|
const QString filepath2 = filepath.endsWith("/") ? filepath : (filepath + "/");
|
||||||
|
|
||||||
if (selindex == -1) {
|
for (int i = 0; i < unchecked.size(); ++i) {
|
||||||
for (int i = 0; i < selected.size(); ++i) {
|
if (unchecked[i].startsWith(filepath2)) {
|
||||||
if (selected[i].startsWith(filepath2)) {
|
return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < unselected.size(); ++i) {
|
if (checkindex == -2)
|
||||||
if (unselected[i].startsWith(filepath2)) {
|
checkindex = getindex(checked, filepath);
|
||||||
return true;
|
|
||||||
|
|
||||||
|
if (checkindex == -1) {
|
||||||
|
for (int i = 0; i < checked.size(); ++i) {
|
||||||
|
if (checked[i].startsWith(filepath2)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,18 +100,18 @@ 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);
|
||||||
const int selindex = getindex(selected, filepath);
|
const int checkindex = getindex(checked, filepath);
|
||||||
const int unselindex = getindex(unselected, filepath);
|
const int uncheckindex = getindex(unchecked, filepath);
|
||||||
|
|
||||||
// If some children are not checked then this item should be partially checked..
|
// If some children are not checked then this item should be partially checked..
|
||||||
if (partiallySelected(filepath, selindex))
|
if (partiallyChecked(filepath, checkindex))
|
||||||
return Qt::PartiallyChecked;
|
return Qt::PartiallyChecked;
|
||||||
|
|
||||||
// Is item selected but not unselected?
|
// Is item selected but not unselected?
|
||||||
if (selindex >= 0 && unselindex == -1)
|
if (checkindex >= 0 && uncheckindex == -1)
|
||||||
return Qt::Checked;
|
return Qt::Checked;
|
||||||
if (selindex >= 0 && unselindex >= 0 &&
|
if (checkindex >= 0 && uncheckindex >= 0 &&
|
||||||
selected[selindex].size() > unselected[unselindex].size())
|
checked[checkindex].size() > unchecked[uncheckindex].size())
|
||||||
return Qt::Checked;
|
return Qt::Checked;
|
||||||
|
|
||||||
// Item is either not selected at all or else it is unselected
|
// Item is either not selected at all or else it is unselected
|
||||||
|
@ -108,36 +124,36 @@ public:
|
||||||
if (role == Qt::CheckStateRole) {
|
if (role == Qt::CheckStateRole) {
|
||||||
const QString filepath = filePath(index);
|
const QString filepath = filePath(index);
|
||||||
|
|
||||||
bool partiallyChecked = partiallySelected(filepath);
|
bool partiallychecked = partiallyChecked(filepath);
|
||||||
|
|
||||||
if (unselected.indexOf(filepath) != -1) {
|
if (unchecked.indexOf(filepath) != -1) {
|
||||||
// remove unchecked path
|
// remove unchecked path
|
||||||
unselected.removeAll(filepath);
|
unchecked.removeAll(filepath);
|
||||||
} else if (partiallyChecked || selected.indexOf(filepath) != -1) {
|
} else if (partiallychecked || checked.indexOf(filepath) != -1) {
|
||||||
// remove child selected paths
|
// remove child selected paths
|
||||||
for (int i = selected.size() - 1; i >= 0; --i) {
|
for (int i = checked.size() - 1; i >= 0; --i) {
|
||||||
if (selected[i].startsWith(filepath))
|
if (checked[i].startsWith(filepath))
|
||||||
selected.removeAt(i);
|
checked.removeAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove child unselected paths
|
// remove child unselected paths
|
||||||
for (int i = unselected.size() - 1; i >= 0; --i) {
|
for (int i = unchecked.size() - 1; i >= 0; --i) {
|
||||||
if (unselected[i].startsWith(filepath))
|
if (unchecked[i].startsWith(filepath))
|
||||||
unselected.removeAt(i);
|
unchecked.removeAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If partialChecked then select this item
|
// If partialChecked then select this item
|
||||||
if (partiallyChecked)
|
if (partiallychecked)
|
||||||
selected.append(filepath);
|
checked.append(filepath);
|
||||||
} else {
|
} else {
|
||||||
const int selindex = getindex(selected, filepath);
|
const int checkindex = getindex(checked, filepath);
|
||||||
const int unselindex = getindex(unselected, filepath);
|
const int uncheckindex = getindex(unchecked, filepath);
|
||||||
if (selindex == -1)
|
if (checkindex == -1)
|
||||||
selected.append(filepath);
|
checked.append(filepath);
|
||||||
else if (unselindex >= 0 && selected[selindex].size() < unselected[unselindex].size())
|
else if (uncheckindex >= 0 && checked[checkindex].size() < unchecked[uncheckindex].size())
|
||||||
selected.append(filepath);
|
checked.append(filepath);
|
||||||
else
|
else
|
||||||
unselected.append(filepath);
|
unchecked.append(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rowCount(index) > 0)
|
if (rowCount(index) > 0)
|
||||||
|
@ -158,20 +174,20 @@ public:
|
||||||
QStringList getFiles() const {
|
QStringList getFiles() const {
|
||||||
QStringList ret;
|
QStringList ret;
|
||||||
|
|
||||||
// List all files in "selected" folders..
|
// List all files in "checked" folders..
|
||||||
FileList fileLister;
|
FileList fileLister;
|
||||||
fileLister.AddPathList(selected);
|
fileLister.AddPathList(checked);
|
||||||
ret = fileLister.GetFileList();
|
ret = fileLister.GetFileList();
|
||||||
|
|
||||||
// Remove all items from ret that are unselected but not selected..
|
// Remove all items from ret that are unchecked but not checked..
|
||||||
for (int i = ret.size() - 1; i >= 0; i--) {
|
for (int i = ret.size() - 1; i >= 0; i--) {
|
||||||
int unselindex = getindex(unselected, ret[i]);
|
int uncheckindex = getindex(unchecked, ret[i]);
|
||||||
if (unselindex == -1)
|
if (uncheckindex == -1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// both selected and unselected, check which to rely on
|
// both checked and unchecked, check which to rely on
|
||||||
int selindex = getindex(selected, ret[i]);
|
int checkindex = getindex(checked, ret[i]);
|
||||||
if (selected[selindex].size() < unselected[unselindex].size())
|
if (checked[checkindex].size() < unchecked[uncheckindex].size())
|
||||||
ret.removeAt(i);
|
ret.removeAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue