- enhancement: auto save / load problem set

This commit is contained in:
Roy Qu 2022-11-14 19:10:32 +08:00
parent 0e76769c3b
commit e764c14286
6 changed files with 69 additions and 8 deletions

View File

@ -31,4 +31,4 @@ author: Alan-CRL
### Monokai ### Monokai
author: СÁúDev author: СÁúDev(XiaoLoong@github)

View File

@ -1,7 +1,8 @@
Red Panda C++ Version 2.5 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 - enhancemnet: add "Reserve word for Types" item in color scheme
- enhancement: auto save / load problem set
Red Panda C++ Version 2.4 Red Panda C++ Version 2.4

View File

@ -321,9 +321,11 @@ MainWindow::MainWindow(QWidget *parent)
//problem set //problem set
mOJProblemSetNameCounter=1; mOJProblemSetNameCounter=1;
mOJProblemSetModel.rename(tr("Problem Set %1").arg(mOJProblemSetNameCounter)); mOJProblemSetModel.rename(tr("Problem Set %1").arg(mOJProblemSetNameCounter));
m=ui->lstProblemSet->selectionModel(); m=ui->lstProblemSet->selectionModel();
ui->lstProblemSet->setModel(&mOJProblemSetModel); ui->lstProblemSet->setModel(&mOJProblemSetModel);
delete m; delete m;
m=ui->tblProblemCases->selectionModel(); m=ui->tblProblemCases->selectionModel();
ui->tblProblemCases->setModel(&mOJProblemModel); ui->tblProblemCases->setModel(&mOJProblemModel);
delete m; delete m;
@ -341,6 +343,19 @@ MainWindow::MainWindow(QWidget *parent)
connect(&mOJProblemModel, &OJProblemModel::dataChanged, connect(&mOJProblemModel, &OJProblemModel::dataChanged,
this, &MainWindow::updateProblemTitle); 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 //files view
m=ui->treeFiles->selectionModel(); m=ui->treeFiles->selectionModel();
@ -4919,6 +4934,17 @@ void MainWindow::closeEvent(QCloseEvent *event) {
e.reason()); 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()) { if (pSettings->debugger().autosave()) {
try { try {
mDebugger->saveForNonproject(includeTrailingPathDelimiter(pSettings->dirs().config()) mDebugger->saveForNonproject(includeTrailingPathDelimiter(pSettings->dirs().config())
@ -7694,7 +7720,10 @@ void MainWindow::on_btnSaveProblemSet_clicked()
QDir::setCurrent(extractFileDir(fileName)); QDir::setCurrent(extractFileDir(fileName));
try { try {
applyCurrentProblemCaseChanges(); applyCurrentProblemCaseChanges();
mOJProblemSetModel.saveToFile(fileName); int currentIndex=-1;
if (ui->lstProblemSet->currentIndex().isValid())
currentIndex = ui->lstProblemSet->currentIndex().row();
mOJProblemSetModel.saveToFile(fileName,currentIndex);
} catch (FileError& error) { } catch (FileError& error) {
QMessageBox::critical(this,tr("Save Error"), QMessageBox::critical(this,tr("Save Error"),
error.reason()); error.reason());
@ -7713,7 +7742,15 @@ void MainWindow::on_btnLoadProblemSet_clicked()
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
QDir::setCurrent(extractFileDir(fileName)); QDir::setCurrent(extractFileDir(fileName));
try { 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) { } catch (FileError& error) {
QMessageBox::critical(this,tr("Load Error"), QMessageBox::critical(this,tr("Load Error"),
error.reason()); error.reason());

View File

@ -92,6 +92,8 @@
#define DEV_BOOKMARK_FILE "bookmarks.json" #define DEV_BOOKMARK_FILE "bookmarks.json"
#define DEV_DEBUGGER_FILE "debugger.json" #define DEV_DEBUGGER_FILE "debugger.json"
#define DEV_HISTORY_FILE "history.json" #define DEV_HISTORY_FILE "history.json"
#define DEV_PROBLEM_SET_FILE "problemset.json"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# define PATH_SENSITIVITY Qt::CaseInsensitive # define PATH_SENSITIVITY Qt::CaseInsensitive

View File

@ -16,6 +16,7 @@
*/ */
#include "ojproblemsetmodel.h" #include "ojproblemsetmodel.h"
#include <QDir>
#include <QFile> #include <QFile>
#include <QIcon> #include <QIcon>
#include <QJsonArray> #include <QJsonArray>
@ -25,6 +26,7 @@
#include "../utils.h" #include "../utils.h"
#include "../iconsmanager.h" #include "../iconsmanager.h"
#include "../systemconsts.h" #include "../systemconsts.h"
#include "../settings.h"
OJProblemSetModel::OJProblemSetModel(QObject *parent) : QAbstractListModel(parent) OJProblemSetModel::OJProblemSetModel(QObject *parent) : QAbstractListModel(parent)
{ {
@ -100,7 +102,7 @@ void OJProblemSetModel::removeAllProblems()
clear(); clear();
} }
void OJProblemSetModel::saveToFile(const QString &fileName) void OJProblemSetModel::saveToFile(const QString &fileName, int currentIndex)
{ {
QFile file(fileName); QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Truncate)) { if (file.open(QFile::WriteOnly | QFile::Truncate)) {
@ -138,6 +140,7 @@ void OJProblemSetModel::saveToFile(const QString &fileName)
problemsArray.append(problemObj); problemsArray.append(problemObj);
} }
obj["problems"]=problemsArray; obj["problems"]=problemsArray;
obj["current_index"]=currentIndex;
QJsonDocument doc; QJsonDocument doc;
doc.setObject(obj); doc.setObject(obj);
file.write(doc.toJson()); 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); QFile file(fileName);
if (file.open(QFile::ReadOnly)) { if (file.open(QFile::ReadOnly)) {
@ -163,6 +166,7 @@ void OJProblemSetModel::loadFromFile(const QString &fileName)
beginResetModel(); beginResetModel();
QJsonObject obj = doc.object(); QJsonObject obj = doc.object();
mProblemSet.name = obj["name"].toString(); mProblemSet.name = obj["name"].toString();
currentIndex = obj["current_index"].toInt(-1);
mProblemSet.problems.clear(); mProblemSet.problems.clear();
QJsonArray problemsArray = obj["problems"].toArray(); QJsonArray problemsArray = obj["problems"].toArray();
foreach (const QJsonValue& problemVal, problemsArray) { foreach (const QJsonValue& problemVal, problemsArray) {
@ -203,6 +207,21 @@ void OJProblemSetModel::loadFromFile(const QString &fileName)
} }
} }
void OJProblemSetModel::load(int &currentIndex)
{
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) void OJProblemSetModel::updateProblemAnswerFilename(const QString &oldFilename, const QString &newFilename)
{ {
foreach (POJProblem problem, mProblemSet.problems) { foreach (POJProblem problem, mProblemSet.problems) {

View File

@ -85,8 +85,10 @@ public:
void removeProblem(int index); void removeProblem(int index);
bool problemNameUsed(const QString& name); bool problemNameUsed(const QString& name);
void removeAllProblems(); void removeAllProblems();
void saveToFile(const QString& fileName); void saveToFile(const QString& fileName, int currentIndex=-1);
void loadFromFile(const QString& fileName); void loadFromFile(const QString& fileName, int& currentIndex);
void load(int& currentIndex);
void save(int currentIndex);
void updateProblemAnswerFilename(const QString& oldFilename, const QString& newFilename); void updateProblemAnswerFilename(const QString& oldFilename, const QString& newFilename);
signals: signals: