- enhancement: auto sort files in the project view

This commit is contained in:
Roy Qu 2022-01-07 19:20:42 +08:00
parent bcd4a1ff67
commit 13ee2d7f33
5 changed files with 45 additions and 35 deletions

View File

@ -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

View File

@ -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<FolderNode*>(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<FolderNode*>(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<FolderNode*>(index.internalPointer());
QModelIndex realIndex = mProjectProxyModel.mapToSource(index);
FolderNode * node = static_cast<FolderNode*>(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<FolderNode*>(current.internalPointer());

View File

@ -23,6 +23,7 @@
#include <QFileSystemModel>
#include <QTcpServer>
#include <QElapsedTimer>
#include <QSortFilterProxyModel>
#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;

View File

@ -85,7 +85,7 @@
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>4</number>
<number>0</number>
</property>
<property name="usesScrollButtons">
<bool>true</bool>
@ -98,7 +98,7 @@
<attribute name="title">
<string>Project</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_13">
<layout class="QVBoxLayout" name="verticalLayout_9">
<property name="leftMargin">
<number>0</number>
</property>

View File

@ -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()<<unit->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