diff --git a/src/plugins/project/mainframe/projecttree.h b/src/plugins/project/mainframe/projecttree.h index 3b3bd1975..5b5979ed0 100644 --- a/src/plugins/project/mainframe/projecttree.h +++ b/src/plugins/project/mainframe/projecttree.h @@ -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 getAllProjectInfo(); diff --git a/src/services/project/directoryasynparse.cpp b/src/services/project/directoryasynparse.cpp index 1a7b78846..906513fda 100644 --- a/src/services/project/directoryasynparse.cpp +++ b/src/services/project/directoryasynparse.cpp @@ -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); @@ -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); @@ -115,6 +115,9 @@ void DirectoryAsynParse::createRows(const QString &path) d->fileList.insert(fileItera.filePath()); } } + + // sort all files + sortAllRows(); } QList DirectoryAsynParse::rows(const QStandardItem *item) const @@ -123,6 +126,7 @@ QList DirectoryAsynParse::rows(const QStandardItem *item) const for (int i = 0; i < item->rowCount(); i++) { result << item->child(i); } + return result; } @@ -166,6 +170,34 @@ QStandardItem *DirectoryAsynParse::findItem(const QString &path, return parent; } +void DirectoryAsynParse::sortAllRows() +{ + auto sortRows = [=](QList &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 sortItems = [&](QStandardItem* parent) { + if (!parent) return; + QList 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(); diff --git a/src/services/project/directoryasynparse.h b/src/services/project/directoryasynparse.h index 91d0bbcdd..db124d55c 100644 --- a/src/services/project/directoryasynparse.h +++ b/src/services/project/directoryasynparse.h @@ -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 rows(const QStandardItem *item) const;