Skip to content

Commit

Permalink
fix: [directory] sort the entry at project tree
Browse files Browse the repository at this point in the history
Log: as title
  • Loading branch information
LiHua000 committed Dec 6, 2024
1 parent 7d53433 commit 8d83738
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/plugins/project/mainframe/projecttree.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProjectTree : public DTreeView
void expandedProjectAll(const QStandardItem *root);
void selectProjectFile(const QString &file);
void setAutoFocusState(bool state);
bool getAutoFocusState() const;
bool getAutoFocusState() const;
void expandItemByFile(const QStringList &filePaths);
void focusCurrentFile();
QList<dpfservice::ProjectInfo> getAllProjectInfo();
Expand Down
40 changes: 36 additions & 4 deletions src/services/project/directoryasynparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ void DirectoryAsynParse::createRows(const QString &path)
rootPath = rootPath.remove(rootPath.size() - separatorSize, separatorSize);
}

// 缓存当前工程目录
d->rootPath = rootPath;
QFileSystemWatcher::addPath(d->rootPath);

if (!directories().contains(d->rootPath))
addPath(d->rootPath);
{ // 避免变量冲突 迭代文件夹
QDir dir;
dir.setPath(rootPath);
Expand All @@ -83,7 +82,8 @@ void DirectoryAsynParse::createRows(const QString &path)
QDirIterator dirItera(dir, QDirIterator::Subdirectories);
while (dirItera.hasNext()) {
QString childPath = dirItera.next().remove(0, rootPath.size());
QFileSystemWatcher::addPath(dirItera.filePath());
if (!directories().contains(dirItera.filePath()))
addPath(dirItera.filePath());
QStandardItem *item = findItem(childPath);
auto newItem = new QStandardItem(dirItera.fileName());
newItem->setData(dirItera.filePath(), ProjectItemRole::FileIconRole);
Expand Down Expand Up @@ -115,6 +115,9 @@ void DirectoryAsynParse::createRows(const QString &path)
d->fileList.insert(fileItera.filePath());
}
}

// sort all files
sortAllRows();
}

QList<QStandardItem *> DirectoryAsynParse::rows(const QStandardItem *item) const
Expand All @@ -123,6 +126,7 @@ QList<QStandardItem *> DirectoryAsynParse::rows(const QStandardItem *item) const
for (int i = 0; i < item->rowCount(); i++) {
result << item->child(i);
}

return result;
}

Expand Down Expand Up @@ -166,6 +170,34 @@ QStandardItem *DirectoryAsynParse::findItem(const QString &path,
return parent;
}

void DirectoryAsynParse::sortAllRows()
{
auto sortRows = [=](QList<QStandardItem*> &rows) {
std::sort(rows.begin(),rows.end(), [](const QStandardItem* a, const QStandardItem* b) {
if (QFileInfo(a->toolTip()).isDir() && !QFileInfo(b->toolTip()).isDir()) return true;
if (!QFileInfo(a->toolTip()).isDir() && QFileInfo(b->toolTip()).isDir()) return false;
return a->text().compare(b->text(), Qt::CaseInsensitive) < 0;
});
};

std::function<void(QStandardItem*)> sortItems = [&](QStandardItem* parent) {
if (!parent) return;
QList<QStandardItem*> children;
while (parent->hasChildren())
children.append(parent->takeRow(0));
sortRows(children);
for (auto child : children) {
parent->appendRow(child);
sortItems(child);
}
};

sortRows(d->rows);
for (auto row : d->rows) {
sortItems(row);
}
}

int DirectoryAsynParse::separatorSize() const
{
return QString(QDir::separator()).size();
Expand Down
1 change: 1 addition & 0 deletions src/services/project/directoryasynparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private slots:

private:
void createRows(const QString &path);
void sortAllRows();
QString itemDisplayName(const QStandardItem *item) const;
QStandardItem *findItem(const QString &path, QStandardItem *parent = nullptr) const;
QList<QStandardItem *> rows(const QStandardItem *item) const;
Expand Down

0 comments on commit 8d83738

Please sign in to comment.