From 9b09b4263f9cd8fc5349ce746f2f0760c8f88baf Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 23 Sep 2022 10:27:44 +0800 Subject: [PATCH] - fix: crash when create non C/C++ source file in project - fix: can't open text project file in the editor - change: when create non-text project file, don't auto open it --- NEWS.md | 3 +++ RedPandaIDE/mainwindow.cpp | 7 ++++--- RedPandaIDE/project.cpp | 7 ++----- RedPandaIDE/project.h | 2 +- RedPandaIDE/utils.cpp | 6 ++++++ 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6a8be917..7e9f0d66 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,9 @@ Red Panda C++ Version 1.4 - fix: "Encode in UTF-8" is not correctly checked, when the editor is openned using UTF-8 encoding. + - fix: crash when create non C/C++ source file in project + - fix: can't open text project file in the editor + - change: when create non-text project file, don't auto open it Red Panda C++ Version 1.3 diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 66e74f38..cd14066f 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -6179,7 +6179,7 @@ void MainWindow::newProjectUnitFile() newProjectUnitDialog.setSuffix("c"); break; default: - newProjectUnitDialog.setSuffix(""); + newProjectUnitDialog.setSuffix("txt"); } } QString folder = mProject->fileSystemNodeFolderPath(pNode); @@ -6221,10 +6221,11 @@ void MainWindow::newProjectUnitFile() mProject->saveAll(); updateProjectView(); idx = mProject->units().count()-1; - Editor * editor = mProject->openUnit(idx); + Editor * editor = mProject->openUnit(idx, false); //editor->setUseCppSyntax(mProject->options().useGPP); //editor->setModified(true); - editor->activate(); + if (editor) + editor->activate(); QString branch; if (pSettings->vcs().gitOk() && mProject->model()->iconProvider()->VCSRepository()->hasRepository(branch)) { QString output; diff --git a/RedPandaIDE/project.cpp b/RedPandaIDE/project.cpp index 0bb10102..1377f68b 100644 --- a/RedPandaIDE/project.cpp +++ b/RedPandaIDE/project.cpp @@ -309,7 +309,7 @@ PProjectUnit Project::newUnit(PProjectModelNode parentNode, const QString& custo return newUnit; } -Editor *Project::openUnit(int index) +Editor *Project::openUnit(int index, bool forceOpen) { if ((index < 0) || (index >= mUnits.count())) return nullptr; @@ -318,11 +318,8 @@ Editor *Project::openUnit(int index) if (!unit->fileName().isEmpty() && fileExists(unit->fileName())) { if (getFileType(unit->fileName())==FileType::Other) { - QMimeDatabase db; - QMimeType mimeType=db.mimeTypeForFile(unit->fileName()); - if (!mimeType.isValid() || !mimeType.name().startsWith("text/")) { + if (forceOpen) QDesktopServices::openUrl(QUrl::fromLocalFile(unit->fileName())); - } return nullptr; } diff --git a/RedPandaIDE/project.h b/RedPandaIDE/project.h index dbddbeaa..499b1c39 100644 --- a/RedPandaIDE/project.h +++ b/RedPandaIDE/project.h @@ -186,7 +186,7 @@ public: int indexInUnits(const Editor* editor) const; PProjectUnit newUnit(PProjectModelNode parentNode, const QString& customFileName=""); - Editor* openUnit(int index); + Editor* openUnit(int index, bool forceOpen=true); Editor* unitEditor(const PProjectUnit& unit) const; Editor* unitEditor(const ProjectUnit* unit) const; Editor* unitEditor(int index) const { diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index f6bd5901..a2472f68 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -40,6 +40,7 @@ #include "project.h" #include "compiler/executablerunner.h" #ifdef Q_OS_WIN +#include #include #endif @@ -276,6 +277,11 @@ FileType getFileType(const QString &filename) if (filename.endsWith(".dat",PATH_SENSITIVITY)) { return FileType::Text; } + QMimeDatabase db; + QMimeType mimeType=db.mimeTypeForFile(filename); + if (mimeType.isValid() && mimeType.name().startsWith("text/")) { + return FileType::Text; + } return FileType::Other; }