From e764c1428631da67dcf89d2de3a745abedf10cfb Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Mon, 14 Nov 2022 19:10:32 +0800 Subject: [PATCH] - enhancement: auto save / load problem set --- Contributors.md | 2 +- NEWS.md | 3 +- RedPandaIDE/mainwindow.cpp | 41 +++++++++++++++++++++-- RedPandaIDE/systemconsts.h | 2 ++ RedPandaIDE/widgets/ojproblemsetmodel.cpp | 23 +++++++++++-- RedPandaIDE/widgets/ojproblemsetmodel.h | 6 ++-- 6 files changed, 69 insertions(+), 8 deletions(-) diff --git a/Contributors.md b/Contributors.md index d029170c..843595e0 100644 --- a/Contributors.md +++ b/Contributors.md @@ -31,4 +31,4 @@ author: Alan-CRL ### Monokai -author: СDev \ No newline at end of file +author: СDev(XiaoLoong@github) \ No newline at end of file diff --git a/NEWS.md b/NEWS.md index e7cad0f3..c4c5a3b7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,8 @@ Red Panda C++ Version 2.5 - - enhancement: new color scheme Monokai (contributed by 小龙Dev) + - enhancement: new color scheme Monokai (contributed by 小龙Dev(XiaoLoong@github)) - enhancemnet: add "Reserve word for Types" item in color scheme + - enhancement: auto save / load problem set Red Panda C++ Version 2.4 diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 8e3d2b9b..1337e478 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -321,9 +321,11 @@ MainWindow::MainWindow(QWidget *parent) //problem set mOJProblemSetNameCounter=1; mOJProblemSetModel.rename(tr("Problem Set %1").arg(mOJProblemSetNameCounter)); + m=ui->lstProblemSet->selectionModel(); ui->lstProblemSet->setModel(&mOJProblemSetModel); delete m; + m=ui->tblProblemCases->selectionModel(); ui->tblProblemCases->setModel(&mOJProblemModel); delete m; @@ -341,6 +343,19 @@ MainWindow::MainWindow(QWidget *parent) connect(&mOJProblemModel, &OJProblemModel::dataChanged, this, &MainWindow::updateProblemTitle); + try { + int currentIndex=-1; + mOJProblemSetModel.load(currentIndex); + if (currentIndex>=0) { + QModelIndex index = mOJProblemSetModel.index(currentIndex,0); + ui->lstProblemSet->setCurrentIndex(index); + ui->lstProblemSet->scrollTo(index); + } + } catch (FileError& e) { + QMessageBox::warning(nullptr, + tr("Error"), + e.reason()); + } //files view m=ui->treeFiles->selectionModel(); @@ -4919,6 +4934,17 @@ void MainWindow::closeEvent(QCloseEvent *event) { e.reason()); } + try { + int currentIndex=-1; + if (ui->lstProblemSet->currentIndex().isValid()) + currentIndex = ui->lstProblemSet->currentIndex().row(); + mOJProblemSetModel.save(currentIndex); + } catch (FileError& e) { + QMessageBox::warning(nullptr, + tr("Save Error"), + e.reason()); + } + if (pSettings->debugger().autosave()) { try { mDebugger->saveForNonproject(includeTrailingPathDelimiter(pSettings->dirs().config()) @@ -7694,7 +7720,10 @@ void MainWindow::on_btnSaveProblemSet_clicked() QDir::setCurrent(extractFileDir(fileName)); try { applyCurrentProblemCaseChanges(); - mOJProblemSetModel.saveToFile(fileName); + int currentIndex=-1; + if (ui->lstProblemSet->currentIndex().isValid()) + currentIndex = ui->lstProblemSet->currentIndex().row(); + mOJProblemSetModel.saveToFile(fileName,currentIndex); } catch (FileError& error) { QMessageBox::critical(this,tr("Save Error"), error.reason()); @@ -7713,7 +7742,15 @@ void MainWindow::on_btnLoadProblemSet_clicked() if (!fileName.isEmpty()) { QDir::setCurrent(extractFileDir(fileName)); try { - mOJProblemSetModel.loadFromFile(fileName); + int currentIndex; + mOJProblemSetModel.loadFromFile(fileName,currentIndex); + if (currentIndex>=0) { + if (currentIndex>=0) { + QModelIndex index = mOJProblemSetModel.index(currentIndex,0); + ui->lstProblemSet->setCurrentIndex(index); + ui->lstProblemSet->scrollTo(index); + } + } } catch (FileError& error) { QMessageBox::critical(this,tr("Load Error"), error.reason()); diff --git a/RedPandaIDE/systemconsts.h b/RedPandaIDE/systemconsts.h index 66696a13..4a642487 100644 --- a/RedPandaIDE/systemconsts.h +++ b/RedPandaIDE/systemconsts.h @@ -92,6 +92,8 @@ #define DEV_BOOKMARK_FILE "bookmarks.json" #define DEV_DEBUGGER_FILE "debugger.json" #define DEV_HISTORY_FILE "history.json" +#define DEV_PROBLEM_SET_FILE "problemset.json" + #ifdef Q_OS_WIN # define PATH_SENSITIVITY Qt::CaseInsensitive diff --git a/RedPandaIDE/widgets/ojproblemsetmodel.cpp b/RedPandaIDE/widgets/ojproblemsetmodel.cpp index 6e9a03b5..0b0344cc 100644 --- a/RedPandaIDE/widgets/ojproblemsetmodel.cpp +++ b/RedPandaIDE/widgets/ojproblemsetmodel.cpp @@ -16,6 +16,7 @@ */ #include "ojproblemsetmodel.h" +#include #include #include #include @@ -25,6 +26,7 @@ #include "../utils.h" #include "../iconsmanager.h" #include "../systemconsts.h" +#include "../settings.h" OJProblemSetModel::OJProblemSetModel(QObject *parent) : QAbstractListModel(parent) { @@ -100,7 +102,7 @@ void OJProblemSetModel::removeAllProblems() clear(); } -void OJProblemSetModel::saveToFile(const QString &fileName) +void OJProblemSetModel::saveToFile(const QString &fileName, int currentIndex) { QFile file(fileName); if (file.open(QFile::WriteOnly | QFile::Truncate)) { @@ -138,6 +140,7 @@ void OJProblemSetModel::saveToFile(const QString &fileName) problemsArray.append(problemObj); } obj["problems"]=problemsArray; + obj["current_index"]=currentIndex; QJsonDocument doc; doc.setObject(obj); file.write(doc.toJson()); @@ -148,7 +151,7 @@ void OJProblemSetModel::saveToFile(const QString &fileName) } } -void OJProblemSetModel::loadFromFile(const QString &fileName) +void OJProblemSetModel::loadFromFile(const QString &fileName, int& currentIndex) { QFile file(fileName); if (file.open(QFile::ReadOnly)) { @@ -163,6 +166,7 @@ void OJProblemSetModel::loadFromFile(const QString &fileName) beginResetModel(); QJsonObject obj = doc.object(); mProblemSet.name = obj["name"].toString(); + currentIndex = obj["current_index"].toInt(-1); mProblemSet.problems.clear(); QJsonArray problemsArray = obj["problems"].toArray(); foreach (const QJsonValue& problemVal, problemsArray) { @@ -203,6 +207,21 @@ void OJProblemSetModel::loadFromFile(const QString &fileName) } } +void OJProblemSetModel::load(int ¤tIndex) +{ + QDir dir(pSettings->dirs().config()); + QString filename=dir.filePath(DEV_PROBLEM_SET_FILE); + if (fileExists(filename)) + loadFromFile(filename,currentIndex); +} + +void OJProblemSetModel::save(int currentIndex) +{ + QDir dir(pSettings->dirs().config()); + QString filename=dir.filePath(DEV_PROBLEM_SET_FILE); + saveToFile(filename,currentIndex); +} + void OJProblemSetModel::updateProblemAnswerFilename(const QString &oldFilename, const QString &newFilename) { foreach (POJProblem problem, mProblemSet.problems) { diff --git a/RedPandaIDE/widgets/ojproblemsetmodel.h b/RedPandaIDE/widgets/ojproblemsetmodel.h index fd0da6b1..e5b4d04e 100644 --- a/RedPandaIDE/widgets/ojproblemsetmodel.h +++ b/RedPandaIDE/widgets/ojproblemsetmodel.h @@ -85,8 +85,10 @@ public: void removeProblem(int index); bool problemNameUsed(const QString& name); void removeAllProblems(); - void saveToFile(const QString& fileName); - void loadFromFile(const QString& fileName); + void saveToFile(const QString& fileName, int currentIndex=-1); + void loadFromFile(const QString& fileName, int& currentIndex); + void load(int& currentIndex); + void save(int currentIndex); void updateProblemAnswerFilename(const QString& oldFilename, const QString& newFilename); signals: