From 13ee2d7f338d1c1d11675f892cc54ac76a17b67b Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 7 Jan 2022 19:20:42 +0800 Subject: [PATCH] - enhancement: auto sort files in the project view --- NEWS.md | 1 + RedPandaIDE/mainwindow.cpp | 43 +++++++++++++++++++++++++++++--------- RedPandaIDE/mainwindow.h | 3 +++ RedPandaIDE/mainwindow.ui | 4 ++-- RedPandaIDE/project.cpp | 29 ++++++------------------- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0ebff21e..27859649 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ Red Panda C++ Version 0.12.7 - fix: shouldn't auto add /bin/gcc to compiler sets - fix: if a dir duplicates in PATH, don't add it to compiler sets repeatedly - enhancement: add "--sanitize=address" to compile option in the Debug compiler set in Linux + - enhancement: auto sort files in the project view Red Panda C++ Version 0.12.6 - fix: heartbeat for gdb server async command shouldn't disable actions diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index c8d4f962..2f2eef05 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -116,6 +116,8 @@ MainWindow::MainWindow(QWidget *parent) connect(mEditorList, &EditorList::editorClosed, this, &MainWindow::onEditorClosed); mProject = nullptr; + ui->projectView->setModel(&mProjectProxyModel); + mProjectProxyModel.setDynamicSortFilter(false); setupActions(); ui->EditorTabsRight->setVisible(false); @@ -2404,7 +2406,7 @@ void MainWindow::buildContextMenus() [this](){ if (!mProject) return; - QModelIndex current = ui->projectView->currentIndex(); + QModelIndex current = mProjectProxyModel.mapToSource(ui->projectView->currentIndex()); if (!current.isValid()) { return; } @@ -2446,7 +2448,7 @@ void MainWindow::buildContextMenus() [this](){ if (!mProject) return; - QModelIndex current = ui->projectView->currentIndex(); + QModelIndex current = mProjectProxyModel.mapToSource(ui->projectView->currentIndex()); if (!current.isValid()) { return; } @@ -2838,7 +2840,7 @@ void MainWindow::onProjectViewContextMenu(const QPoint &pos) bool onRoot = false; bool folderEmpty = false; int unitIndex = -1; - QModelIndex current = ui->projectView->selectionModel()->currentIndex(); + QModelIndex current = mProjectProxyModel.mapToSource(ui->projectView->selectionModel()->currentIndex()); if (current.isValid() && mProject) { FolderNode * node = static_cast(current.internalPointer()); PFolderNode pNode = mProject->pointerToNode(node); @@ -3465,16 +3467,36 @@ void MainWindow::closeProject(bool refreshEditor) void MainWindow::updateProjectView() { if (mProject) { - ui->projectView->setModel(mProject->model()); - connect(mProject->model(), &QAbstractItemModel::modelReset, - ui->projectView,&QTreeView::expandAll); + if (mProjectProxyModel.sourceModel()!=mProject->model()) { + mProjectProxyModel.setSourceModel(mProject->model()); + connect(mProject->model(),&ProjectModel::dataChanged, &mProjectProxyModel, + [this]() { + mProjectProxyModel.invalidate(); + }); + connect(mProject->model(),&ProjectModel::modelReset, &mProjectProxyModel, + [this]() { + mProjectProxyModel.invalidate(); + }); + connect(mProject->model(),&ProjectModel::rowsInserted, &mProjectProxyModel, + [this]() { + mProjectProxyModel.invalidate(); + }); + connect(mProject->model(),&ProjectModel::rowsRemoved, &mProjectProxyModel, + [this]() { + mProjectProxyModel.invalidate(); + }); + mProjectProxyModel.sort(0); + connect(mProject->model(), &QAbstractItemModel::modelReset, + ui->projectView,&QTreeView::expandAll); + } + mProjectProxyModel.invalidate(); ui->projectView->expandAll(); openCloseLeftPanel(true); ui->tabProject->setVisible(true); ui->tabInfos->setCurrentWidget(ui->tabProject); } else { // Clear project browser - ui->projectView->setModel(nullptr); + mProjectProxyModel.setSourceModel(nullptr); ui->tabProject->setVisible(false); } updateProjectActions(); @@ -5033,7 +5055,7 @@ void MainWindow::on_actionAdd_to_project_triggered() dialog.setFileMode(QFileDialog::ExistingFiles); dialog.setAcceptMode(QFileDialog::AcceptOpen); if (dialog.exec()) { - QModelIndex current = ui->projectView->currentIndex(); + QModelIndex current = mProjectProxyModel.mapToSource(ui->projectView->currentIndex()); FolderNode * node = nullptr; if (current.isValid()) { node = static_cast(current.internalPointer()); @@ -5062,7 +5084,8 @@ void MainWindow::on_actionRemove_from_project_triggered() foreach (const QModelIndex& index, ui->projectView->selectionModel()->selectedIndexes()){ if (!index.isValid()) continue; - FolderNode * node = static_cast(index.internalPointer()); + QModelIndex realIndex = mProjectProxyModel.mapToSource(index); + FolderNode * node = static_cast(realIndex.internalPointer()); PFolderNode folderNode = mProject->pointerToNode(node); if (!folderNode) continue; @@ -5285,7 +5308,7 @@ void MainWindow::newProjectUnitFile() if (!mProject) return; int idx = -1; - QModelIndex current = ui->projectView->currentIndex(); + QModelIndex current = mProjectProxyModel.mapToSource(ui->projectView->currentIndex()); FolderNode * node = nullptr; if (current.isValid()) { node = static_cast(current.internalPointer()); diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index 6bcb7e18..0287d8a9 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "common.h" #include "widgets/searchresultview.h" #include "widgets/classbrowser.h" @@ -673,6 +674,8 @@ private: QAction * mProblem_OpenSource; QAction * mProblem_Properties; + QSortFilterProxyModel mProjectProxyModel; + // QWidget interface protected: void closeEvent(QCloseEvent *event) override; diff --git a/RedPandaIDE/mainwindow.ui b/RedPandaIDE/mainwindow.ui index 9e9b4b25..cd649490 100644 --- a/RedPandaIDE/mainwindow.ui +++ b/RedPandaIDE/mainwindow.ui @@ -85,7 +85,7 @@ QTabWidget::West - 4 + 0 true @@ -98,7 +98,7 @@ Project - + 0 diff --git a/RedPandaIDE/project.cpp b/RedPandaIDE/project.cpp index a5b8db54..752b4eba 100644 --- a/RedPandaIDE/project.cpp +++ b/RedPandaIDE/project.cpp @@ -309,15 +309,6 @@ Editor *Project::openUnit(int index) void Project::rebuildNodes() { - // Remember if folder nodes were expanded or collapsed - // Create a list of expanded folder nodes -// QStringList oldPaths := TStringList.Create; -// with MainForm.ProjectView do -// for idx := 0 to Items.Count - 1 do begin -// tempnode := Items[idx]; -// if tempnode.Expanded and (tempnode.Data = Pointer(-1)) then // data=pointer(-1) - it's folder -// oldPaths.Add(GetFolderPath(tempnode)); -// end; mModel.beginUpdate(); // Delete everything @@ -337,18 +328,6 @@ void Project::rebuildNodes() mUnits[idx]->node()->unitIndex = idx; } -// // expand nodes expanded before recreating the project tree -// fNode.Collapse(True); -// with MainForm.ProjectView do -// for idx := 0 to Items.Count - 1 do begin -// tempnode := Items[idx]; -// if (tempnode.Data = Pointer(-1)) then //it's a folder -// if oldPaths.IndexOf(GetFolderPath(tempnode)) >= 0 then -// tempnode.Expand(False); -// end; -// FreeAndNil(oldPaths); - -// fNode.Expand(False); mModel.endUpdate(); emit nodesChanged(); @@ -365,6 +344,8 @@ bool Project::removeUnit(int index, bool doClose , bool removeFile) PProjectUnit unit = mUnits[index]; + qDebug()<fileName(); + qDebug()<<(qint64)unit->editor(); // Attempt to close it if (doClose && (unit->editor())) { if (!pMainWindow->editorList()->closeEditor(unit->editor())) @@ -1816,16 +1797,18 @@ ProjectModel::ProjectModel(Project *project, QObject *parent): void ProjectModel::beginUpdate() { - if (mUpdateCount==0) + if (mUpdateCount==0) { beginResetModel(); + } mUpdateCount++; } void ProjectModel::endUpdate() { mUpdateCount--; - if (mUpdateCount==0) + if (mUpdateCount==0) { endResetModel(); + } } QModelIndex ProjectModel::index(int row, int column, const QModelIndex &parent) const