From 0424f7cf459cf706a5837bada1e0c238d5dadc26 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Mon, 27 Sep 2021 20:17:24 +0800 Subject: [PATCH] - enhancement: auto load last open files at start --- NEWS.md | 1 + RedPandaIDE/main.cpp | 2 + RedPandaIDE/mainwindow.cpp | 102 +++++++++++++++++++++++++++++++++++-- RedPandaIDE/mainwindow.h | 9 ++-- RedPandaIDE/systemconsts.h | 1 + 5 files changed, 107 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index da276eb5..832133a0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ Version 0.2 - fix : header file completion stop work when input '.' - change: continue to run / debug if there are compiling warnings (but no errors) + - enhancement: auto load last open files at start - enhancement: class browser syntax colors and icons - enhancement: function tips - enhancement: project support diff --git a/RedPandaIDE/main.cpp b/RedPandaIDE/main.cpp index 17dffeb3..99461fee 100644 --- a/RedPandaIDE/main.cpp +++ b/RedPandaIDE/main.cpp @@ -95,6 +95,8 @@ int main(int argc, char *argv[]) MainWindow mainWindow; pMainWindow = &mainWindow; + if (pSettings->editor().autoLoadLastFiles()) + pMainWindow->loadLastOpens(); mainWindow.show(); int retCode = app.exec(); // save settings diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index c30f113a..10cb82de 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -169,6 +169,7 @@ MainWindow::MainWindow(QWidget *parent) buildContextMenus(); updateEditorColorSchemes(); + } MainWindow::~MainWindow() @@ -1447,12 +1448,96 @@ void MainWindow::scanActiveProject(bool parse) void MainWindow::saveLastOpens() { + QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_LASTOPENS_FILE; + if (fileExists(filename)) { + if (!QFile::remove(filename)) { + QMessageBox::critical(this, + tr("Save last open info error"), + tr("Can't remove old last open information file '%1'") + .arg(filename), + QMessageBox::Ok); + return; + } + } + SimpleIni lastOpenIni; + lastOpenIni.SetLongValue("LastOpens","Count", mEditorList->pageCount()); + if (mProject) { + lastOpenIni.SetValue("LastOpens","Project",mProject->filename().toLocal8Bit()); + } + for (int i=0;ipageCount();i++) { + Editor * editor = (*mEditorList)[i]; + QByteArray sectionName = QString("Editor_%1").arg(i).toLocal8Bit(); + lastOpenIni.SetValue(sectionName,"FileName", editor->filename().toLocal8Bit()); + lastOpenIni.SetBoolValue(sectionName, "OnLeft",editor->pageControl() != ui->EditorTabsRight); + lastOpenIni.SetBoolValue(sectionName, "Focused",editor->hasFocus()); + lastOpenIni.SetLongValue(sectionName, "CursorCol", editor->caretX()); + lastOpenIni.SetLongValue(sectionName, "CursorRow", editor->caretY()); + lastOpenIni.SetLongValue(sectionName, "TopLine", editor->topLine()); + lastOpenIni.SetLongValue(sectionName, "LeftChar", editor->leftChar()); + } + if (lastOpenIni.SaveFile(filename.toLocal8Bit())!=SI_Error::SI_OK) { + QMessageBox::critical(this, + tr("Save last open info error"), + tr("Can't save last open info file '%1'") + .arg(filename), + QMessageBox::Ok); + return; + } } void MainWindow::loadLastOpens() { - + QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_LASTOPENS_FILE; + if (!fileExists(filename)) + return; + SimpleIni lastOpenIni; + if (lastOpenIni.LoadFile(filename.toLocal8Bit())!=SI_Error::SI_OK) { + QMessageBox::critical(this, + tr("Load last open info error"), + tr("Can't load last open info file '%1'") + .arg(filename), + QMessageBox::Ok); + return; + } + Editor * focusedEditor = nullptr; + int count = lastOpenIni.GetLongValue("LastOpens","Count",0); + for (int i=0;inewEditor(editorFilename,ENCODING_AUTO_DETECT,false,false); + if (!editor) + continue; + BufferCoord pos; + pos.Char = lastOpenIni.GetLongValue(sectionName,"CursorCol", 1); + pos.Line = lastOpenIni.GetLongValue(sectionName,"CursorRow", 1); + editor->setCaretXY(pos); + editor->setTopLine( + lastOpenIni.GetLongValue(sectionName,"TopLine", 1) + ); + editor->setLeftChar( + lastOpenIni.GetLongValue(sectionName,"LeftChar", 1) + ); + if (lastOpenIni.GetBoolValue(sectionName,"Focused",false)) + focusedEditor = editor; + pSettings->history().removeFile(editorFilename); + } + QString projectFilename = lastOpenIni.GetValue("LastOpens", "Project",""); + if (fileExists(projectFilename)) { + openProject(filename); + } else { + updateEditorActions(); + updateForEncodingInfo(); + } + if (!focusedEditor) + focusedEditor->activate(); } void MainWindow::buildContextMenus() @@ -2240,8 +2325,13 @@ void MainWindow::closeEvent(QCloseEvent *event) { settings.setLeftPanelOpenned(mLeftPanelOpenned); settings.save(); - if (mProject) { - closeProject(false); + if (pSettings->editor().autoLoadLastFiles()) { + saveLastOpens(); + } else { + //if don't save last open files, close project before editors, to save project openned editors; + if (mProject) { + closeProject(false); + } } if (!mEditorList->closeAll(false)) { @@ -2249,6 +2339,12 @@ void MainWindow::closeEvent(QCloseEvent *event) { return ; } + if (pSettings->editor().autoLoadLastFiles()) { + if (mProject) { + closeProject(false); + } + } + mCompilerManager->stopCompile(); mCompilerManager->stopRun(); diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index 6f3cd66d..65eac368 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -81,13 +81,14 @@ public: void addDebugOutput(const QString& text); void changeDebugOutputLastline(const QString& text); void updateDebugEval(const QString& value); - void rebuildOpenedFileHisotryMenu(); - void updateClassBrowserForEditor(Editor* editor); - void resetAutoSaveTimer(); + void saveLastOpens(); + void loadLastOpens(); + + QPlainTextEdit* txtLocals(); CPUDialog *cpuDialog() const; @@ -154,8 +155,6 @@ private: QWidget* parent, QKeySequence shortcut=QKeySequence()); void scanActiveProject(bool parse=false); - void saveLastOpens(); - void loadLastOpens(); private slots: void onAutoSaveTimeout(); diff --git a/RedPandaIDE/systemconsts.h b/RedPandaIDE/systemconsts.h index a4bd4382..2eb50a3b 100644 --- a/RedPandaIDE/systemconsts.h +++ b/RedPandaIDE/systemconsts.h @@ -29,6 +29,7 @@ #define ICON_EXT "ico" #define TEMPLATE_EXT "template" #define DEV_INTERNAL_OPEN "$__DEV_INTERNAL_OPEN" +#define DEV_LASTOPENS_FILE "lastopens.ini" #ifdef Q_OS_WIN # define PATH_SENSITIVITY Qt::CaseInsensitive