From 898d5f4e08fe9dc2d950183f147d7e4f21bc0c06 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 6 Jul 2022 19:44:44 +0800 Subject: [PATCH] - enhancement: resort files in the files view after rename --- NEWS.md | 3 ++- RedPandaIDE/mainwindow.cpp | 7 +++++++ RedPandaIDE/mainwindow.h | 2 ++ RedPandaIDE/widgets/customfilesystemmodel.cpp | 19 +++++++++++++++++++ RedPandaIDE/widgets/customfilesystemmodel.h | 8 ++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 6ef33f8a..9c9b3a67 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,7 +12,8 @@ Red Panda C++ Version 1.1.4 - enhancement: prevent group undo when caret position changed - fix: undo link break may lose leading spaces - fix: correctly restore editor's modified status when undo/redo - - enhancement: set current index to the folder after new folder created in the file view + - enhancement: set current index to the folder after new folder created in the files view + - enhancement: resort files in the files view after rename Red Panda C++ Version 1.1.3 diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 2f789a66..805e0b10 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -298,6 +298,8 @@ MainWindow::MainWindow(QWidget *parent) //files view ui->treeFiles->setModel(&mFileSystemModel); + connect(&mFileSystemModel, &QFileSystemModel::layoutChanged, + this, &MainWindow::onFileSystemModelLayoutChanged, Qt::QueuedConnection); mFileSystemModel.setReadOnly(false); mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider); @@ -4391,6 +4393,11 @@ void MainWindow::on_EditorTabsRight_tabCloseRequested(int index) mEditorList->closeEditor(editor); } +void MainWindow::onFileSystemModelLayoutChanged() +{ + ui->treeFiles->scrollTo(ui->treeFiles->currentIndex(),QTreeView::PositionAtCenter); +} + void MainWindow::on_actionOpen_triggered() { try { diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index 5f6ec7d2..966cc5e0 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -351,6 +351,8 @@ private slots: void on_EditorTabsLeft_tabCloseRequested(int index); void on_EditorTabsRight_tabCloseRequested(int index); + void onFileSystemModelLayoutChanged(); + void on_actionOpen_triggered(); void on_actionSave_triggered(); diff --git a/RedPandaIDE/widgets/customfilesystemmodel.cpp b/RedPandaIDE/widgets/customfilesystemmodel.cpp index 1a79b7b5..08adedf6 100644 --- a/RedPandaIDE/widgets/customfilesystemmodel.cpp +++ b/RedPandaIDE/widgets/customfilesystemmodel.cpp @@ -20,6 +20,25 @@ CustomFileSystemModel::CustomFileSystemModel(QObject *parent) : QFileSystemModel(parent) { + connect(this, &QFileSystemModel::fileRenamed, + this, &CustomFileSystemModel::delaySort); + connect(this, &CustomFileSystemModel::rowsInserted, + this, &CustomFileSystemModel::delaySort); + connect(&mDelayedSortTimer, &QTimer::timeout, + this, &CustomFileSystemModel::performDelayedSort, Qt::QueuedConnection); + mDelayedSortTimer.setSingleShot(true); +} + +void CustomFileSystemModel::delaySort() +{ + if (!mDelayedSortTimer.isActive()) + mDelayedSortTimer.start(0); +} + +void CustomFileSystemModel::performDelayedSort() +{ + sort(1); + sort(0); } diff --git a/RedPandaIDE/widgets/customfilesystemmodel.h b/RedPandaIDE/widgets/customfilesystemmodel.h index c26ffcc6..032ba5c5 100644 --- a/RedPandaIDE/widgets/customfilesystemmodel.h +++ b/RedPandaIDE/widgets/customfilesystemmodel.h @@ -18,6 +18,7 @@ #define CUSTOMFILESYSTEMMODEL_H #include +#include class GitRepository; class GitManager; @@ -27,6 +28,13 @@ class CustomFileSystemModel : public QFileSystemModel public: explicit CustomFileSystemModel(QObject *parent = nullptr); +public slots: + void delaySort(); + +protected slots: + void performDelayedSort(); +private: + QTimer mDelayedSortTimer; }; #endif // CUSTOMFILESYSTEMMODEL_H