fix: delete multiple files in files view

This commit is contained in:
Roy Qu 2022-02-08 16:48:50 +08:00
parent acbbae24c6
commit cc43ac245a
3 changed files with 41 additions and 20 deletions

View File

@ -2660,7 +2660,7 @@ void MainWindow::buildContextMenus()
ui->treeFiles);
mFilesView_RemoveFile->setShortcut(Qt::Key_Delete);
connect(mFilesView_RemoveFile, &QAction::triggered,
this, &MainWindow::onFilesViewRemoveFile);
this, &MainWindow::onFilesViewRemoveFiles);
mFilesView_Open = createActionFor(
tr("Open in Editor"),
ui->treeFiles);
@ -3330,27 +3330,24 @@ void MainWindow::onFilesViewCreateFolder()
mFileSystemModel.mkdir(index,folderName);
}
void MainWindow::onFilesViewRemoveFile()
void MainWindow::onFilesViewRemoveFiles()
{
QModelIndex index = ui->treeFiles->currentIndex();
if (!index.isValid())
return;
if (QMessageBox::question(ui->treeFiles,tr("Delete")
,tr("Do you really want to delete %1?").arg(mFileSystemModel.fileName(index)),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)!=QMessageBox::Yes)
return;
if (mFileSystemModel.isDir(index)) {
QDir dir(mFileSystemModel.fileInfo(index).absoluteFilePath());
if (!dir.isEmpty() &&
QMessageBox::question(ui->treeFiles
,tr("Delete")
,tr("Folder %1 is not empty.").arg(mFileSystemModel.fileName(index))
+ tr("Do you really want to delete it?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)!=QMessageBox::Yes)
QModelIndexList indexList = ui->treeFiles->selectionModel()->selectedRows();
if (indexList.isEmpty()) {
QModelIndex index = ui->treeFiles->currentIndex();
if (QMessageBox::question(ui->treeFiles,tr("Delete")
,tr("Do you really want to delete %1?").arg(mFileSystemModel.fileName(index)),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)!=QMessageBox::Yes)
return;
dir.removeRecursively();
doFilesViewRemoveFile(index);
} else {
QFile::remove(mFileSystemModel.filePath(index));
if (QMessageBox::question(ui->treeFiles,tr("Delete")
,tr("Do you really want to delete %1 files?").arg(indexList.count()),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)!=QMessageBox::Yes)
return;
foreach (const QModelIndex& index, indexList) {
doFilesViewRemoveFile(index);
}
}
}
@ -5442,6 +5439,25 @@ void MainWindow::newProjectUnitFile()
editor->activate();
}
void MainWindow::doFilesViewRemoveFile(const QModelIndex &index)
{
if (!index.isValid())
return;
if (mFileSystemModel.isDir(index)) {
QDir dir(mFileSystemModel.fileInfo(index).absoluteFilePath());
if (!dir.isEmpty() &&
QMessageBox::question(ui->treeFiles
,tr("Delete")
,tr("Folder %1 is not empty.").arg(mFileSystemModel.fileName(index))
+ tr("Do you really want to delete it?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)!=QMessageBox::Yes)
return;
dir.removeRecursively();
} else {
QFile::remove(mFileSystemModel.filePath(index));
}
}
void MainWindow::invalidateProjectProxyModel()
{
mProjectProxyModel->invalidate();

View File

@ -248,6 +248,8 @@ private:
void prepareTabMessagesData();
void newProjectUnitFile();
void doFilesViewRemoveFile(const QModelIndex& index);
private slots:
void invalidateProjectProxyModel();
void onEditorRenamed(const QString& oldFilename, const QString& newFilename, bool firstSave);
@ -275,7 +277,7 @@ private slots:
void onShowInsertCodeSnippetMenu();
void onFilesViewCreateFolder();
void onFilesViewRemoveFile();
void onFilesViewRemoveFiles();
void on_actionNew_triggered();

View File

@ -267,6 +267,9 @@
<property name="defaultDropAction">
<enum>Qt::CopyAction</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
</layout>