2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-31 17:01:34 +08:00
|
|
|
#include "ojproblemsetmodel.h"
|
|
|
|
|
2022-11-14 19:10:32 +08:00
|
|
|
#include <QDir>
|
2021-11-01 00:40:11 +08:00
|
|
|
#include <QFile>
|
2021-11-01 20:44:08 +08:00
|
|
|
#include <QIcon>
|
2021-11-01 00:40:11 +08:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2022-09-03 20:18:32 +08:00
|
|
|
#include <QMimeData>
|
2021-11-01 00:40:11 +08:00
|
|
|
#include "../utils.h"
|
2021-11-01 20:44:08 +08:00
|
|
|
#include "../iconsmanager.h"
|
2022-01-02 10:37:00 +08:00
|
|
|
#include "../systemconsts.h"
|
2022-11-14 19:10:32 +08:00
|
|
|
#include "../settings.h"
|
2021-11-01 00:40:11 +08:00
|
|
|
|
2021-10-31 17:01:34 +08:00
|
|
|
OJProblemSetModel::OJProblemSetModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
void OJProblemSetModel::clear()
|
|
|
|
{
|
2021-12-09 21:04:53 +08:00
|
|
|
beginResetModel();
|
2021-11-01 00:40:11 +08:00
|
|
|
mProblemSet.problems.clear();
|
2022-04-11 17:38:57 +08:00
|
|
|
mProblemSet.exportFilename.clear();
|
2021-12-09 21:04:53 +08:00
|
|
|
endResetModel();
|
2021-11-01 00:40:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int OJProblemSetModel::count()
|
|
|
|
{
|
|
|
|
return mProblemSet.problems.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemSetModel::create(const QString& name)
|
|
|
|
{
|
|
|
|
mProblemSet.name = name;
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemSetModel::rename(const QString &newName)
|
|
|
|
{
|
|
|
|
if (mProblemSet.name!=newName)
|
|
|
|
mProblemSet.name = newName;
|
|
|
|
}
|
|
|
|
|
2022-04-11 17:38:57 +08:00
|
|
|
QString OJProblemSetModel::name() const
|
2021-11-01 00:40:11 +08:00
|
|
|
{
|
|
|
|
return mProblemSet.name;
|
|
|
|
}
|
|
|
|
|
2022-04-11 17:38:57 +08:00
|
|
|
QString OJProblemSetModel::exportFilename() const
|
|
|
|
{
|
|
|
|
return mProblemSet.exportFilename;
|
|
|
|
}
|
|
|
|
|
2022-12-11 19:47:43 +08:00
|
|
|
void OJProblemSetModel::addProblem(const POJProblem& problem)
|
2021-11-01 00:40:11 +08:00
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), mProblemSet.problems.count(), mProblemSet.problems.count());
|
|
|
|
mProblemSet.problems.append(problem);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-12-11 19:47:43 +08:00
|
|
|
void OJProblemSetModel::addProblems(const QList<POJProblem> &problems)
|
|
|
|
{
|
|
|
|
if (problems.isEmpty())
|
|
|
|
return;
|
|
|
|
beginInsertRows(QModelIndex(), mProblemSet.problems.count(), mProblemSet.problems.count()+problems.count()-1);
|
|
|
|
foreach( const POJProblem& p, problems)
|
|
|
|
mProblemSet.problems.append(p);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:54:02 +08:00
|
|
|
const QList<POJProblem> &OJProblemSetModel::problems() const
|
|
|
|
{
|
|
|
|
return mProblemSet.problems;
|
|
|
|
}
|
|
|
|
|
|
|
|
POJProblem OJProblemSetModel::problem(int index) const
|
2021-11-01 00:40:11 +08:00
|
|
|
{
|
|
|
|
return mProblemSet.problems[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemSetModel::removeProblem(int index)
|
|
|
|
{
|
|
|
|
Q_ASSERT(index>=0 && index < mProblemSet.problems.count());
|
2021-11-02 01:07:37 +08:00
|
|
|
beginRemoveRows(QModelIndex(),index,index);
|
2021-11-01 00:40:11 +08:00
|
|
|
mProblemSet.problems.removeAt(index);
|
2021-11-02 01:07:37 +08:00
|
|
|
endRemoveRows();
|
2021-11-01 00:40:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OJProblemSetModel::problemNameUsed(const QString &name)
|
|
|
|
{
|
|
|
|
foreach (const POJProblem& problem, mProblemSet.problems) {
|
|
|
|
if (name == problem->name)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemSetModel::removeAllProblems()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2022-11-14 19:10:32 +08:00
|
|
|
void OJProblemSetModel::saveToFile(const QString &fileName, int currentIndex)
|
2021-11-01 00:40:11 +08:00
|
|
|
{
|
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
QJsonObject obj;
|
2022-04-11 17:38:57 +08:00
|
|
|
mProblemSet.exportFilename=fileName;
|
2021-11-01 00:40:11 +08:00
|
|
|
obj["name"]=mProblemSet.name;
|
|
|
|
QJsonArray problemsArray;
|
|
|
|
foreach (const POJProblem& problem, mProblemSet.problems) {
|
|
|
|
QJsonObject problemObj;
|
|
|
|
problemObj["name"]=problem->name;
|
2021-11-02 09:29:35 +08:00
|
|
|
problemObj["url"]=problem->url;
|
2021-11-04 09:07:06 +08:00
|
|
|
problemObj["description"]=problem->description;
|
2022-12-13 08:49:20 +08:00
|
|
|
problemObj["time_limit"]=(int)problem->timeLimit;
|
|
|
|
problemObj["memory_limit"]=(int)problem->memoryLimit;
|
|
|
|
problemObj["time_limit_unit"]=(int)problem->timeLimitUnit;
|
|
|
|
problemObj["memory_limit_unit"]=(int)problem->memoryLimitUnit;
|
2022-01-02 10:37:00 +08:00
|
|
|
if (fileExists(problem->answerProgram))
|
|
|
|
problemObj["answer_program"] = problem->answerProgram;
|
2021-11-01 00:40:11 +08:00
|
|
|
QJsonArray cases;
|
|
|
|
foreach (const POJProblemCase& problemCase, problem->cases) {
|
|
|
|
QJsonObject caseObj;
|
|
|
|
caseObj["name"]=problemCase->name;
|
|
|
|
caseObj["input"]=problemCase->input;
|
2022-03-29 18:06:24 +08:00
|
|
|
QString path = problemCase->inputFileName;
|
|
|
|
QString prefix = includeTrailingPathDelimiter(extractFileDir(fileName));
|
|
|
|
if (path.startsWith(prefix, PATH_SENSITIVITY)) {
|
|
|
|
path = "%ProblemSetPath%/"+ path.mid(prefix.length());
|
|
|
|
}
|
|
|
|
caseObj["input_filename"]=path;
|
|
|
|
path = problemCase->expectedOutputFileName;
|
|
|
|
if (path.startsWith(prefix, PATH_SENSITIVITY)) {
|
|
|
|
path = "%ProblemSetPath%/"+ path.mid(prefix.length());
|
|
|
|
}
|
|
|
|
caseObj["expected_output_filename"]=path;
|
2021-11-01 00:40:11 +08:00
|
|
|
caseObj["expected"]=problemCase->expected;
|
|
|
|
cases.append(caseObj);
|
|
|
|
}
|
|
|
|
problemObj["cases"]=cases;
|
|
|
|
problemsArray.append(problemObj);
|
|
|
|
}
|
|
|
|
obj["problems"]=problemsArray;
|
2022-11-14 19:10:32 +08:00
|
|
|
obj["current_index"]=currentIndex;
|
2021-11-01 00:40:11 +08:00
|
|
|
QJsonDocument doc;
|
|
|
|
doc.setObject(obj);
|
|
|
|
file.write(doc.toJson());
|
|
|
|
file.close();
|
|
|
|
} else {
|
|
|
|
throw FileError(QObject::tr("Can't open file '%1' for read.")
|
|
|
|
.arg(fileName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 19:10:32 +08:00
|
|
|
void OJProblemSetModel::loadFromFile(const QString &fileName, int& currentIndex)
|
2021-10-31 17:01:34 +08:00
|
|
|
{
|
2021-11-01 00:40:11 +08:00
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QByteArray content = file.readAll();
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc(QJsonDocument::fromJson(content,&error));
|
|
|
|
if (error.error!=QJsonParseError::NoError) {
|
|
|
|
throw FileError(QObject::tr("Can't parse problem set file '%1':%2")
|
|
|
|
.arg(fileName)
|
|
|
|
.arg(error.errorString()));
|
|
|
|
}
|
|
|
|
beginResetModel();
|
|
|
|
QJsonObject obj = doc.object();
|
|
|
|
mProblemSet.name = obj["name"].toString();
|
2022-11-14 19:10:32 +08:00
|
|
|
currentIndex = obj["current_index"].toInt(-1);
|
2021-11-01 00:40:11 +08:00
|
|
|
mProblemSet.problems.clear();
|
|
|
|
QJsonArray problemsArray = obj["problems"].toArray();
|
|
|
|
foreach (const QJsonValue& problemVal, problemsArray) {
|
|
|
|
QJsonObject problemObj = problemVal.toObject();
|
|
|
|
POJProblem problem = std::make_shared<OJProblem>();
|
|
|
|
problem->name = problemObj["name"].toString();
|
2021-11-02 09:29:35 +08:00
|
|
|
problem->url = problemObj["url"].toString();
|
2022-12-13 08:49:20 +08:00
|
|
|
problem->timeLimit = problemObj["time_limit"].toInt();
|
|
|
|
problem->memoryLimit = problemObj["memory_limit"].toInt();
|
|
|
|
problem->timeLimitUnit = (ProblemTimeLimitUnit)problemObj["time_limit_unit"].toInt();
|
|
|
|
problem->memoryLimitUnit = (ProblemMemoryLimitUnit)problemObj["memory_limit_unit"].toInt();
|
|
|
|
|
2021-11-04 09:07:06 +08:00
|
|
|
problem->description = problemObj["description"].toString();
|
2021-11-06 14:49:11 +08:00
|
|
|
problem->answerProgram = problemObj["answer_program"].toString();
|
2021-11-01 00:40:11 +08:00
|
|
|
QJsonArray casesArray = problemObj["cases"].toArray();
|
|
|
|
foreach (const QJsonValue& caseVal, casesArray) {
|
|
|
|
QJsonObject caseObj = caseVal.toObject();
|
|
|
|
POJProblemCase problemCase = std::make_shared<OJProblemCase>();
|
|
|
|
problemCase->name = caseObj["name"].toString();
|
|
|
|
problemCase->input = caseObj["input"].toString();
|
|
|
|
problemCase->expected = caseObj["expected"].toString();
|
2022-03-29 18:06:24 +08:00
|
|
|
QString path = caseObj["input_filename"].toString();
|
|
|
|
if (path.startsWith("%ProblemSetPath%/")) {
|
|
|
|
path = includeTrailingPathDelimiter(extractFileDir(fileName))+
|
|
|
|
path.mid(QLatin1String("%ProblemSetPath%/").size());
|
|
|
|
}
|
|
|
|
problemCase->inputFileName=path;
|
|
|
|
path = caseObj["expected_output_filename"].toString();
|
|
|
|
if (path.startsWith("%ProblemSetPath%/")) {
|
|
|
|
path = includeTrailingPathDelimiter(extractFileDir(fileName))+
|
|
|
|
path.mid(QLatin1String("%ProblemSetPath%/").size());
|
|
|
|
}
|
|
|
|
problemCase->expectedOutputFileName=path;
|
2021-11-02 09:29:35 +08:00
|
|
|
problemCase->testState = ProblemCaseTestState::NotTested;
|
2021-11-01 00:40:11 +08:00
|
|
|
problem->cases.append(problemCase);
|
|
|
|
}
|
|
|
|
mProblemSet.problems.append(problem);
|
|
|
|
}
|
|
|
|
endResetModel();
|
|
|
|
} else {
|
|
|
|
throw FileError(QObject::tr("Can't open file '%1' for read.")
|
|
|
|
.arg(fileName));
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 17:01:34 +08:00
|
|
|
|
2022-11-14 19:10:32 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-01-02 10:37:00 +08:00
|
|
|
void OJProblemSetModel::updateProblemAnswerFilename(const QString &oldFilename, const QString &newFilename)
|
|
|
|
{
|
|
|
|
foreach (POJProblem problem, mProblemSet.problems) {
|
|
|
|
if (QString::compare(problem->answerProgram,oldFilename,PATH_SENSITIVITY)==0) {
|
|
|
|
problem->answerProgram = newFilename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 23:47:51 +08:00
|
|
|
int OJProblemSetModel::rowCount(const QModelIndex &) const
|
2021-11-01 00:40:11 +08:00
|
|
|
{
|
|
|
|
return mProblemSet.problems.count();
|
2021-10-31 17:01:34 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
QVariant OJProblemSetModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
2021-11-01 09:18:23 +08:00
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
2021-11-01 00:40:11 +08:00
|
|
|
return mProblemSet.problems[index.row()]->name;
|
2022-12-13 08:49:20 +08:00
|
|
|
} else if (role == Qt::ToolTipRole) {
|
|
|
|
POJProblem problem = mProblemSet.problems[index.row()];
|
|
|
|
|
|
|
|
QString s;
|
|
|
|
s=QString("<h3>%1</h3>").arg(problem->name);
|
|
|
|
if (!problem->description.isEmpty())
|
|
|
|
s+=problem->description;
|
|
|
|
|
|
|
|
return s;
|
2021-11-01 00:40:11 +08:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:18:23 +08:00
|
|
|
bool OJProblemSetModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return false;
|
|
|
|
if (role == Qt::EditRole) {
|
|
|
|
QString s = value.toString();
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
mProblemSet.problems[index.row()]->name = s;
|
2021-11-02 01:07:37 +08:00
|
|
|
emit problemNameChanged(index.row());
|
2021-11-01 09:18:23 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-03 20:18:32 +08:00
|
|
|
Qt::ItemFlags OJProblemSetModel::flags(const QModelIndex &index) const
|
2021-11-01 09:18:23 +08:00
|
|
|
{
|
2022-09-03 20:18:32 +08:00
|
|
|
Qt::ItemFlags flags = Qt::NoItemFlags;
|
|
|
|
if (index.isValid()) {
|
|
|
|
flags = Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
|
|
|
|
} else if (index.row() == -1) {
|
|
|
|
// -1 means it's a drop target?
|
|
|
|
flags = Qt::ItemIsDropEnabled;
|
|
|
|
}
|
|
|
|
return flags ;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::DropActions OJProblemSetModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::DropAction::MoveAction;
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:31 +08:00
|
|
|
bool OJProblemSetModel::moveRows(const QModelIndex &/*sourceParent*/, int sourceRow, int count, const QModelIndex &/*destinationParent*/, int destinationChild)
|
2022-09-03 20:18:32 +08:00
|
|
|
{
|
|
|
|
if (sourceRow < 0
|
|
|
|
|| sourceRow + count - 1 >= mProblemSet.problems.count()
|
|
|
|
|| destinationChild < 0
|
|
|
|
|| destinationChild > mProblemSet.problems.count()
|
|
|
|
|| sourceRow == destinationChild
|
|
|
|
|| count <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int fromRow = sourceRow;
|
|
|
|
if (destinationChild < sourceRow)
|
|
|
|
fromRow += count - 1;
|
|
|
|
else
|
|
|
|
destinationChild--;
|
|
|
|
while (count--)
|
|
|
|
mProblemSet.problems.move(fromRow, destinationChild);
|
|
|
|
endMoveRows();
|
|
|
|
return true;
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 18:06:24 +08:00
|
|
|
OJProblemModel::OJProblemModel(QObject *parent): QAbstractTableModel(parent)
|
2021-11-01 00:40:11 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const POJProblem &OJProblemModel::problem() const
|
2021-10-31 17:01:34 +08:00
|
|
|
{
|
|
|
|
return mProblem;
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
void OJProblemModel::setProblem(const POJProblem &newProblem)
|
|
|
|
{
|
|
|
|
if (newProblem!=mProblem) {
|
|
|
|
beginResetModel();
|
|
|
|
mProblem = newProblem;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemModel::addCase(POJProblemCase problemCase)
|
|
|
|
{
|
|
|
|
if (mProblem==nullptr)
|
|
|
|
return;
|
|
|
|
beginInsertRows(QModelIndex(),mProblem->cases.count(),mProblem->cases.count());
|
|
|
|
mProblem->cases.append(problemCase);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemModel::removeCase(int index)
|
2021-10-31 17:01:34 +08:00
|
|
|
{
|
2021-11-01 00:40:11 +08:00
|
|
|
if (mProblem==nullptr)
|
|
|
|
return;
|
|
|
|
Q_ASSERT(index >= 0 && index < mProblem->cases.count());
|
|
|
|
beginRemoveRows(QModelIndex(),index,index);
|
|
|
|
mProblem->cases.removeAt(index);
|
|
|
|
endRemoveRows();
|
2021-10-31 17:01:34 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 20:39:09 +08:00
|
|
|
void OJProblemModel::removeCases()
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(),0,mProblem->cases.count());
|
|
|
|
mProblem->cases.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
POJProblemCase OJProblemModel::getCase(int index)
|
2021-10-31 17:01:34 +08:00
|
|
|
{
|
2021-11-01 00:40:11 +08:00
|
|
|
if (mProblem==nullptr)
|
|
|
|
return POJProblemCase();
|
|
|
|
return mProblem->cases[index];
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:08 +08:00
|
|
|
POJProblemCase OJProblemModel::getCaseById(const QString& id)
|
|
|
|
{
|
|
|
|
if (mProblem==nullptr)
|
|
|
|
return POJProblemCase();
|
|
|
|
foreach (const POJProblemCase& problemCase, mProblem->cases){
|
|
|
|
if (problemCase->getId() == id)
|
|
|
|
return problemCase;
|
|
|
|
}
|
|
|
|
return POJProblemCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
int OJProblemModel::getCaseIndexById(const QString &id)
|
|
|
|
{
|
|
|
|
if (mProblem==nullptr)
|
|
|
|
return -1;
|
|
|
|
for (int i=0;i<mProblem->cases.size();i++) {
|
|
|
|
const POJProblemCase& problemCase = mProblem->cases[i];
|
|
|
|
if (problemCase->getId() == id)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
void OJProblemModel::clear()
|
|
|
|
{
|
|
|
|
if (mProblem==nullptr)
|
|
|
|
return;
|
2021-12-09 21:04:53 +08:00
|
|
|
beginResetModel();
|
2021-11-01 00:40:11 +08:00
|
|
|
mProblem->cases.clear();
|
2021-12-09 21:04:53 +08:00
|
|
|
endResetModel();
|
2021-11-01 00:40:11 +08:00
|
|
|
}
|
2021-10-31 17:01:34 +08:00
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
int OJProblemModel::count()
|
|
|
|
{
|
|
|
|
if (mProblem == nullptr)
|
|
|
|
return 0;
|
|
|
|
return mProblem->cases.count();
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:08 +08:00
|
|
|
void OJProblemModel::update(int row)
|
|
|
|
{
|
|
|
|
emit dataChanged(index(row,0),index(row,0));
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:29:35 +08:00
|
|
|
QString OJProblemModel::getTitle()
|
|
|
|
{
|
|
|
|
if (!mProblem)
|
|
|
|
return "";
|
|
|
|
int total = mProblem->cases.count();
|
|
|
|
int passed = 0;
|
|
|
|
foreach (const POJProblemCase& problemCase, mProblem->cases) {
|
|
|
|
if (problemCase->testState == ProblemCaseTestState::Passed)
|
|
|
|
passed ++ ;
|
|
|
|
}
|
|
|
|
QString title = QString("%1 (%2/%3)").arg(mProblem->name)
|
|
|
|
.arg(passed).arg(total);
|
|
|
|
if (!mProblem->url.isEmpty()) {
|
|
|
|
title = QString("<a href=\"%1\">%2</a>").arg(mProblem->url,title);
|
|
|
|
}
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:07:06 +08:00
|
|
|
QString OJProblemModel::getTooltip()
|
|
|
|
{
|
|
|
|
if (!mProblem)
|
|
|
|
return "";
|
|
|
|
QString s;
|
|
|
|
s=QString("<h3>%1</h3>").arg(mProblem->name);
|
|
|
|
if (!mProblem->description.isEmpty())
|
2023-07-05 11:21:20 +08:00
|
|
|
s+=mProblem->description;
|
2021-11-04 09:07:06 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
int OJProblemModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
if (mProblem==nullptr)
|
|
|
|
return 0;
|
|
|
|
return mProblem->cases.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant OJProblemModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
if (mProblem==nullptr)
|
|
|
|
return QVariant();
|
2022-03-29 18:06:24 +08:00
|
|
|
switch (index.column()) {
|
|
|
|
case 0:
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
POJProblemCase problemCase = mProblem->cases[index.row()];
|
|
|
|
return problemCase->name;
|
|
|
|
} else if (role == Qt::DecorationRole) {
|
|
|
|
switch (mProblem->cases[index.row()]->testState) {
|
|
|
|
case ProblemCaseTestState::Failed:
|
|
|
|
return pIconsManager->getIcon(IconsManager::ACTION_PROBLEM_FALIED);
|
|
|
|
case ProblemCaseTestState::Passed:
|
|
|
|
return pIconsManager->getIcon(IconsManager::ACTION_PROBLEM_PASSED);
|
|
|
|
case ProblemCaseTestState::Testing:
|
|
|
|
return pIconsManager->getIcon(IconsManager::ACTION_PROBLEM_TESTING);
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2021-11-01 20:44:08 +08:00
|
|
|
}
|
2022-03-29 18:06:24 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
POJProblemCase problemCase = mProblem->cases[index.row()];
|
|
|
|
if (problemCase->testState == ProblemCaseTestState::Passed
|
|
|
|
|| problemCase->testState == ProblemCaseTestState::Failed)
|
2022-05-21 10:44:39 +08:00
|
|
|
return problemCase->runningTime;
|
2022-03-29 18:06:24 +08:00
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
break;
|
2022-12-13 09:46:16 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2022-12-13 08:49:20 +08:00
|
|
|
case 2:
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
POJProblemCase problemCase = mProblem->cases[index.row()];
|
|
|
|
if (problemCase->testState == ProblemCaseTestState::Passed
|
|
|
|
|| problemCase->testState == ProblemCaseTestState::Failed)
|
|
|
|
return problemCase->runningMemory/1024;
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
break;
|
2022-12-13 09:46:16 +08:00
|
|
|
#endif
|
2021-11-01 00:40:11 +08:00
|
|
|
}
|
2022-03-29 18:06:24 +08:00
|
|
|
|
2021-11-01 00:40:11 +08:00
|
|
|
return QVariant();
|
2021-10-31 17:01:34 +08:00
|
|
|
}
|
2021-11-01 09:18:23 +08:00
|
|
|
|
|
|
|
bool OJProblemModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return false;
|
2022-03-29 18:06:24 +08:00
|
|
|
if (index.column()!=0)
|
|
|
|
return false;
|
2021-11-01 20:44:08 +08:00
|
|
|
if (mProblem==nullptr)
|
|
|
|
return false;
|
2022-03-29 18:06:24 +08:00
|
|
|
if (role == Qt::EditRole ) {
|
2021-11-01 20:44:08 +08:00
|
|
|
QString s = value.toString();
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
mProblem->cases[index.row()]->name = s;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 18:06:24 +08:00
|
|
|
Qt::ItemFlags OJProblemModel::flags(const QModelIndex &idx) const
|
2021-11-01 09:18:23 +08:00
|
|
|
{
|
2022-03-29 18:06:24 +08:00
|
|
|
Qt::ItemFlags flags=Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
if (idx.column()==0)
|
|
|
|
flags |= Qt::ItemIsEditable ;
|
2022-09-03 20:18:32 +08:00
|
|
|
if (idx.isValid())
|
|
|
|
flags |= Qt::ItemIsDragEnabled;
|
|
|
|
flags |= Qt::ItemIsDropEnabled;
|
2022-03-29 18:06:24 +08:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2022-07-04 11:39:06 +08:00
|
|
|
int OJProblemModel::columnCount(const QModelIndex &/*parent*/) const
|
2022-03-29 18:06:24 +08:00
|
|
|
{
|
2022-12-13 09:46:16 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2022-12-13 08:49:20 +08:00
|
|
|
return 3;
|
2022-12-13 09:46:16 +08:00
|
|
|
#else
|
|
|
|
return 2;
|
|
|
|
#endif
|
2022-03-29 18:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant OJProblemModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch (section) {
|
|
|
|
case 0:
|
|
|
|
return tr("Name");
|
|
|
|
case 1:
|
2022-05-21 10:44:39 +08:00
|
|
|
return tr("Time(ms)");
|
2022-12-13 09:46:16 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2022-12-13 08:49:20 +08:00
|
|
|
case 2:
|
|
|
|
return tr("Memory(kb)");
|
2022-12-13 09:46:16 +08:00
|
|
|
#endif
|
2022-03-29 18:06:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
2022-09-03 20:18:32 +08:00
|
|
|
|
|
|
|
Qt::DropActions OJProblemModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::DropAction::MoveAction;
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:31 +08:00
|
|
|
bool OJProblemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int /* column */, const QModelIndex &parent)
|
2022-09-03 20:18:32 +08:00
|
|
|
{
|
|
|
|
mMoveTargetRow=row;
|
2022-09-04 10:09:48 +08:00
|
|
|
if (mMoveTargetRow==-1)
|
|
|
|
mMoveTargetRow=mProblem->cases.length();
|
2022-09-03 20:18:32 +08:00
|
|
|
return QAbstractTableModel::dropMimeData(data,action,row,0,parent);
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:31 +08:00
|
|
|
bool OJProblemModel::insertRows(int /* row */, int /*count*/, const QModelIndex &/*parent*/)
|
2022-09-03 20:18:32 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:31 +08:00
|
|
|
bool OJProblemModel::removeRows(int row, int count, const QModelIndex &/*parent*/)
|
2022-09-03 20:18:32 +08:00
|
|
|
{
|
|
|
|
int sourceRow = row;
|
|
|
|
int destinationChild = mMoveTargetRow;
|
|
|
|
mMoveTargetRow=-1;
|
|
|
|
if (sourceRow < 0
|
|
|
|
|| sourceRow + count - 1 >= mProblem->cases.count()
|
|
|
|
|| destinationChild < 0
|
|
|
|
|| destinationChild > mProblem->cases.count()
|
|
|
|
|| sourceRow == destinationChild
|
|
|
|
|| count <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int fromRow = sourceRow;
|
|
|
|
if (destinationChild < sourceRow)
|
|
|
|
fromRow += count - 1;
|
|
|
|
else
|
|
|
|
destinationChild--;
|
|
|
|
while (count--)
|
|
|
|
mProblem->cases.move(fromRow, destinationChild);
|
|
|
|
endMoveRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|