- fix: New non-saved filenames is wrongly saved in the last openfiles list.

- fix: File is parsed before editor is fully created.
  - enhancement: New GAS File in the File Menu
  - change: rename "New File" to "New C/C++ File"
This commit is contained in:
Roy Qu 2023-02-12 22:33:34 +08:00
parent 9976ee248a
commit 57cbf3a49a
15 changed files with 535 additions and 419 deletions

View File

@ -11,6 +11,10 @@ Red Panda C++ Version 2.12
- enhancement: Compile/Debug GAS source files in project.
- enhancement: Keyword completion for asm/GAS files.
- enhancement: If GAS source file has "_start" label, compile it with "-nostartfiles".
- fix: New non-saved filenames is wrongly saved in the last openfiles list.
- fix: File is parsed before editor is fully created.
- enhancement: New GAS File in the File Menu
- change: rename "New File" to "New C/C++ File"
Red Panda C++ Version 2.11

View File

@ -67,8 +67,9 @@ Editor::Editor(QWidget *parent, const QString& filename,
Project* pProject, bool isNew,
QTabWidget* parentPageControl):
QSynEdit(parent),
mInited(false),
mEncodingOption(encoding),
mFilename(QFileInfo(filename).absoluteFilePath()),
mFilename(filename),
mParentPageControl(parentPageControl),
mProject(pProject),
mIsNew(isNew),
@ -85,6 +86,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
mSaving(false),
mHoverModifiedLine(-1)
{
mInited=false;
mBackupFile=nullptr;
mHighlightCharPos1 = QSynedit::BufferCoord{0,0};
mHighlightCharPos2 = QSynedit::BufferCoord{0,0};
@ -107,10 +109,8 @@ Editor::Editor(QWidget *parent, const QString& filename,
tr("Error Load File"),
e.reason());
}
syntaxer = syntaxerManager.getSyntaxer(mFilename);
} else {
syntaxer=syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
}
syntaxer = syntaxerManager.getSyntaxer(mFilename);
resolveAutoDetectEncodingOption();
if (syntaxer) {
setSyntaxer(syntaxer);
@ -166,7 +166,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
pMainWindow, &MainWindow::onEditorContextMenu);
mCanAutoSave = false;
if (isNew && parentPageControl!=nullptr) {
if (isNew && parentPageControl!=nullptr ) {
QString fileTemplate = pMainWindow->codeSnippetManager()->newFileTemplate();
if (!fileTemplate.isEmpty()) {
insertCodeSnippet(fileTemplate);
@ -201,6 +201,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
this, &Editor::onScrollBarValueChanged);
connect(verticalScrollBar(), &QScrollBar::valueChanged,
this, &Editor::onScrollBarValueChanged);
mInited=true;
}
Editor::~Editor() {
@ -365,12 +366,18 @@ bool Editor::saveAs(const QString &name, bool fromProject){
if (name.isEmpty()) {
QString selectedFileFilter;
QString defaultExt;
if (pSettings->editor().defaultFileCpp()) {
selectedFileFilter = pSystemConsts->defaultCPPFileFilter();
defaultExt = "cpp";
defaultExt=QFileInfo(oldName).suffix();
qDebug()<<defaultExt;
if (defaultExt.isEmpty()) {
if (pSettings->editor().defaultFileCpp()) {
selectedFileFilter = pSystemConsts->defaultCPPFileFilter();
defaultExt = "cpp";
} else {
selectedFileFilter = pSystemConsts->defaultCFileFilter();
defaultExt = "c";
}
} else {
selectedFileFilter = pSystemConsts->defaultCFileFilter();
defaultExt = "c";
selectedFileFilter = pSystemConsts->fileFilterFor(defaultExt);
}
QFileDialog dialog(this,tr("Save As"),extractFilePath(mFilename),
pSystemConsts->defaultFileFilters().join(";;"));
@ -379,28 +386,6 @@ bool Editor::saveAs(const QString &name, bool fromProject){
dialog.selectFile(mFilename);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
// connect(&dialog, &QFileDialog::filterSelected,
// [&dialog](const QString &filter){
// if (filter.indexOf("*.*")) {
// dialog.setDefaultSuffix("");
// qDebug()<<"lllll";
// return;
// }
// int pos = filter.indexOf("*.");
// if (pos>=0) {
// QString suffix;
// pos+=2;
// while (pos<filter.length()) {
// if (filter[pos] == ';' || filter[pos] ==' ' || filter[pos] == ')')
// break;
// suffix+=filter[pos];
// pos++;
// }
// //dialog.setDefaultSuffix(suffix);
// } else {
// dialog.setDefaultSuffix("");
// }
// });
if (dialog.exec()!=QFileDialog::Accepted) {
return false;
@ -2957,6 +2942,8 @@ Editor::QuoteStatus Editor::getQuoteStatus()
void Editor::reparse(bool resetParser)
{
if (!mInited)
return;
if (!mParentPageControl)
return;
if (!pSettings->codeCompletion().enabled())

View File

@ -292,6 +292,7 @@ private:
QSynedit::PTokenAttribute &attr);
void onScrollBarValueChanged();
private:
bool mInited;
QDateTime mBackupTime;
QFile* mBackupFile;
QByteArray mEncodingOption; // the encoding type set by the user

View File

@ -395,14 +395,11 @@ Editor* EditorList::getOpenedEditorByFilename(QString filename)
{
if (filename.isEmpty())
return nullptr;
QFileInfo fileInfo(filename);
QString fullname = fileInfo.absoluteFilePath();
for (int i=0;i<mLeftPageWidget->count();i++) {
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
if (!e)
continue;
if (e->filename().compare(filename, PATH_SENSITIVITY)==0 ||
e->filename().compare(fullname, PATH_SENSITIVITY)==0) {
if (e->filename().compare(filename, PATH_SENSITIVITY)==0) {
return e;
}
}
@ -410,7 +407,7 @@ Editor* EditorList::getOpenedEditorByFilename(QString filename)
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
if (!e)
continue;
if (e->filename().compare(filename)==0 || e->filename().compare(fullname)==0) {
if (e->filename().compare(filename)==0) {
return e;
}
}

View File

@ -216,6 +216,7 @@ MainWindow::MainWindow(QWidget *parent)
mMenuNew = new QMenu();
mMenuNew->setTitle(tr("New"));
mMenuNew->addAction(ui->actionNew);
mMenuNew->addAction(ui->actionNew_GAS_File);
mMenuNew->addAction(ui->actionNew_Project);
mMenuNew->addSeparator();
mMenuNew->addAction(ui->actionNew_Template);
@ -3031,7 +3032,7 @@ void MainWindow::onBookmarkContextMenu(const QPoint &pos)
menu.exec(ui->tableBookmark->mapToGlobal(pos));
}
void MainWindow::saveLastOpens()
bool MainWindow::saveLastOpens()
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_LASTOPENS_FILE;
QFile file(filename);
@ -3042,7 +3043,7 @@ void MainWindow::saveLastOpens()
tr("Can't open last open information file '%1' for write!")
.arg(filename),
QMessageBox::Ok);
return;
return true;
}
QJsonObject rootObj;
if (mProject) {
@ -3052,6 +3053,25 @@ void MainWindow::saveLastOpens()
for (int i=0;i<mEditorList->pageCount();i++) {
Editor * editor = (*mEditorList)[i];
QJsonObject fileObj;
if (editor->isNew()) {
if (!editor->modified())
continue;
QMessageBox::StandardButton reply;
reply = QMessageBox::question(editor,QObject::tr("Save"),
QString(QObject::tr("Save changes to %1?")).arg(editor->filename()),
QMessageBox::Yes|QMessageBox::No|QMessageBox::Yes|QMessageBox::Cancel);
if (reply == QMessageBox::No) {
editor->setModified(false);
continue;
} else if (reply == QMessageBox::Yes) {
if (!editor->save(false,false)) {
return false;
}
} else {
return false;
}
}
fileObj["filename"] = editor->filename();
fileObj["onLeft"] = (editor->pageControl() != mEditorList->rightPageWidget());
fileObj["focused"] = editor->hasFocus();
@ -3071,9 +3091,10 @@ void MainWindow::saveLastOpens()
tr("Can't save last open info file '%1'")
.arg(filename),
QMessageBox::Ok);
return;
return true;
}
file.close();
return true;
}
void MainWindow::loadLastOpens()
@ -3223,10 +3244,18 @@ void MainWindow::updateTools()
}
}
void MainWindow::newEditor()
void MainWindow::newEditor(const QString& suffix)
{
try {
Editor * editor=mEditorList->newEditor("",
QString filename=QString("untitled%1").arg(getNewFileNumber());
if (suffix.isEmpty()) {
if (pSettings->editor().defaultFileCpp())
filename+=".cpp";
else
filename+=".c";
} else
filename+= "." + suffix;
Editor * editor=mEditorList->newEditor(filename,
pSettings->editor().defaultEncoding(),
nullptr,true);
editor->activate();
@ -5198,7 +5227,10 @@ void MainWindow::on_actionNew_triggered()
tr("New Project File?"),
tr("Do you want to add the new file to the project?"),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
newProjectUnitFile();
if (mProject->options().isCpp)
newProjectUnitFile("cpp");
else
newProjectUnitFile("c");
return;
}
}
@ -5313,7 +5345,12 @@ void MainWindow::closeEvent(QCloseEvent *event) {
}
if (!mShouldRemoveAllSettings && pSettings->editor().autoLoadLastFiles()) {
saveLastOpens();
if (!saveLastOpens()) { //canceled
mClosingAll=false;
mQuitting = false;
event->ignore();
return ;
}
} /*else {
//if don't save last open files, close project before editors, to save project openned editors;
@ -7062,7 +7099,7 @@ void MainWindow::prepareTabMessagesData()
}
}
void MainWindow::newProjectUnitFile()
void MainWindow::newProjectUnitFile(const QString& suffix)
{
if (!mProject)
return;
@ -7092,24 +7129,28 @@ void MainWindow::newProjectUnitFile()
modelTypeNode = mProject->rootNode();
}
NewProjectUnitDialog newProjectUnitDialog;
if (modelTypeNode == mProject->rootNode()) {
if (mProject->options().isCpp)
newProjectUnitDialog.setSuffix("cpp");
else
newProjectUnitDialog.setSuffix("c");
if (!suffix.isEmpty()) {
newProjectUnitDialog.setSuffix(suffix);
} else {
switch (modelTypeNode->folderNodeType) {
case ProjectModelNodeType::DUMMY_HEADERS_FOLDER:
newProjectUnitDialog.setSuffix("h");
break;
case ProjectModelNodeType::DUMMY_SOURCES_FOLDER:
if (modelTypeNode == mProject->rootNode()) {
if (mProject->options().isCpp)
newProjectUnitDialog.setSuffix("cpp");
else
newProjectUnitDialog.setSuffix("c");
break;
default:
newProjectUnitDialog.setSuffix("txt");
} else {
switch (modelTypeNode->folderNodeType) {
case ProjectModelNodeType::DUMMY_HEADERS_FOLDER:
newProjectUnitDialog.setSuffix("h");
break;
case ProjectModelNodeType::DUMMY_SOURCES_FOLDER:
if (mProject->options().isCpp)
newProjectUnitDialog.setSuffix("cpp");
else
newProjectUnitDialog.setSuffix("c");
break;
default:
newProjectUnitDialog.setSuffix("txt");
}
}
}
QString folder = mProject->fileSystemNodeFolderPath(pNode);
@ -7124,10 +7165,14 @@ void MainWindow::newProjectUnitFile()
} else {
do {
newFileName = QString("untitled")+QString("%1").arg(getNewFileNumber());
if (mProject->options().isCpp)
newFileName += ".cpp";
else
newFileName += ".c";
if (!suffix.isEmpty()) {
newFileName += "." + suffix;
} else {
if (mProject->options().isCpp)
newFileName += ".cpp";
else
newFileName += ".c";
}
} while (QDir(mProject->directory()).exists(newFileName));
newFileName = QInputDialog::getText(
this,
@ -9436,3 +9481,18 @@ void MainWindow::on_actionDocument_triggered()
QDesktopServices::openUrl(QUrl("https://royqh1979.gitee.io/redpandacpp/docsy/docs/"));
}
void MainWindow::on_actionNew_GAS_File_triggered()
{
if (mProject) {
if (QMessageBox::question(this,
tr("New Project File?"),
tr("Do you want to add the new file to the project?"),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
newProjectUnitFile("s");
return;
}
}
newEditor("s");
}

View File

@ -160,13 +160,13 @@ public:
void updateClassBrowserForEditor(Editor* editor);
void resetAutoSaveTimer();
void updateShortcuts();
void saveLastOpens();
bool saveLastOpens();
void loadLastOpens();
void updateTools();
void openFiles(const QStringList& files);
void newEditor();
void newEditor(const QString& suffix="");
QPlainTextEdit* txtLocals();
@ -299,7 +299,7 @@ private:
void showHideMessagesTab(QWidget *widget, bool show);
void prepareTabInfosData();
void prepareTabMessagesData();
void newProjectUnitFile();
void newProjectUnitFile(const QString& suffix="");
void fillProblemCaseInputAndExpected(const POJProblemCase &problemCase);
void doFilesViewRemoveFile(const QModelIndex& index);
@ -775,6 +775,8 @@ private slots:
void on_actionDocument_triggered();
void on_actionNew_GAS_File_triggered();
private:
Ui::MainWindow *ui;
bool mFullInitialized;

View File

@ -2063,7 +2063,7 @@
</iconset>
</property>
<property name="text">
<string>New File</string>
<string>New C/C++ File</string>
</property>
<property name="toolTip">
<string>New Source File</string>
@ -3327,6 +3327,11 @@
<string>F1</string>
</property>
</action>
<action name="actionNew_GAS_File">
<property name="text">
<string>New GAS File</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -17,7 +17,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="tabSnippet">
<attribute name="title">

View File

@ -107,6 +107,27 @@ const QString &SystemConsts::defaultAllFileFilter() const noexcept
return mDefaultFileFilters[0];
}
QString SystemConsts::fileFilterFor(const QString &suffix)
{
QString t="*."+suffix;
qDebug()<<" * "<<t;
foreach(const QString filter,mDefaultFileFilters) {
qDebug()<<filter;
int pos = filter.lastIndexOf("(");
int pos2 = filter.lastIndexOf(")");
if (pos<0 || pos2<=pos)
continue;
QString suffixes=filter.mid(pos+1,pos2-pos-1);
QStringList suffixList = suffixes.split(" ");
foreach( const QString& s, suffixList) {
qDebug()<<" - "<<s;
if (s.trimmed()==t)
return filter;
}
}
return "";
}
void SystemConsts::addDefaultFileFilter(const QString &name, const QString &fileExtensions)
{
addFileFilter(mDefaultFileFilters,name,fileExtensions);

View File

@ -142,6 +142,7 @@ public:
const QString& defaultCFileFilter() const noexcept;
const QString& defaultCPPFileFilter() const noexcept;
const QString& defaultAllFileFilter() const noexcept;
QString fileFilterFor(const QString& suffix);
void addDefaultFileFilter(const QString& name, const QString& fileExtensions);
const QStringList &iconFileFilters() const;
const QString& iconFileFilter() const;

View File

@ -4976,6 +4976,14 @@
<source>Document</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New C/C++ File</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New GAS File</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>

File diff suppressed because it is too large Load Diff

View File

@ -4789,6 +4789,14 @@
<source>Document</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New C/C++ File</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New GAS File</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>

View File

@ -3910,6 +3910,15 @@ void QSynEdit::setBackgroundColor(const QColor &newBackgroundColor)
mBackgroundColor = newBackgroundColor;
}
bool QSynEdit::isEmpty()
{
if (mDocument->count()>1)
return false;
if (mDocument->count()==1)
return mDocument->getLine(0).isEmpty();
return true;
}
const QColor &QSynEdit::foregroundColor() const
{
return mForegroundColor;

View File

@ -425,6 +425,8 @@ public:
const QColor &backgroundColor() const;
void setBackgroundColor(const QColor &newBackgroundColor);
bool isEmpty();
signals:
void linesDeleted(int FirstLine, int Count);
void linesInserted(int FirstLine, int Count);