FolderStatusModel: add a function to get a QModelIndex from the path

Will be usefull to solve #3704
This commit is contained in:
Olivier Goffart 2015-08-31 10:12:45 +02:00
parent 17e9b65cad
commit 9172a5fc4c
2 changed files with 45 additions and 1 deletions

View file

@ -330,6 +330,45 @@ FolderStatusModel::SubFolderInfo* FolderStatusModel::infoForIndex(const QModelIn
}
}
QModelIndex FolderStatusModel::indexForPath(Folder *f, const QString& path) const
{
int slashPos = path.lastIndexOf('/');
if (slashPos == -1) {
// first level folder
for (int i = 0; i < _folders.size(); ++i) {
if (_folders.at(i)._folder == f) {
for (int j = 0; j < _folders.at(i)._subs.size(); ++j) {
if (_folders.at(i)._subs.at(j)._name == path) {
return index(j, 0, index(i));
}
}
return QModelIndex();
}
}
return QModelIndex();
}
auto parent = indexForPath(f, path.left(slashPos));
if (!parent.isValid())
return parent;
if (slashPos == path.size() - 1) {
// The slash is the last part, we found our index
return parent;
}
auto parentInfo = infoForIndex(parent);
if (!parentInfo) {
return QModelIndex();
}
for (int i = 0; i < parentInfo->_subs.size(); ++i) {
if (parentInfo->_subs.at(i)._name == path.mid(slashPos + 1)) {
return index(i, 0, parent);
}
}
return QModelIndex();
}
QModelIndex FolderStatusModel::index(int row, int column, const QModelIndex& parent) const
{

View file

@ -49,7 +49,6 @@ public:
void fetchMore(const QModelIndex& parent) Q_DECL_OVERRIDE;
bool hasChildren(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
struct SubFolderInfo {
SubFolderInfo()
: _folder(0), _size(0), _fetched(false), _fetching(false), _isUndecided(false),
@ -87,6 +86,12 @@ public:
// If the selective sync check boxes were changed
bool isDirty() { return _dirty; }
/**
* return a QModelIndex for the given path within the given folder.
* Note: this method returns an invalid index if the path was not fetch from the server before
*/
QModelIndex indexForPath(Folder *f, const QString &path) const;
public slots:
void slotUpdateFolderState(Folder *);
void slotApplySelectiveSync();