Split competitivecompenionhandler from mainwindow

This commit is contained in:
Roy Qu 2024-05-22 11:18:39 +08:00
parent 5cbdd2bcda
commit 7e70607f27
4 changed files with 23 additions and 92 deletions

View File

@ -136,6 +136,7 @@ SOURCES += \
parser/cpptokenizer.cpp \
parser/parserutils.cpp \
parser/statementmodel.cpp \
problems/competitivecompenionhandler.cpp \
problems/freeprojectsetformat.cpp \
problems/ojproblemset.cpp \
problems/problemcasevalidator.cpp \
@ -271,6 +272,7 @@ HEADERS += \
parser/cpptokenizer.h \
parser/parserutils.h \
parser/statementmodel.h \
problems/competitivecompenionhandler.h \
problems/freeprojectsetformat.h \
problems/ojproblemset.h \
problems/problemcasevalidator.h \

View File

@ -39,6 +39,7 @@
#include "widgets/choosethemedialog.h"
#include "thememanager.h"
#include "utils/font.h"
#include "problems/ojproblemset.h"
#ifdef Q_OS_WIN
#include <QTemporaryFile>
@ -341,6 +342,7 @@ int main(int argc, char *argv[])
app.installTranslator(&transQt);
}
}
qRegisterMetaType<POJProblem>("POJProblem");
qRegisterMetaType<PCompileIssue>("PCompileIssue");
qRegisterMetaType<PCompileIssue>("PCompileIssue&");
qRegisterMetaType<QVector<int>>("QVector<int>");

View File

@ -366,8 +366,8 @@ MainWindow::MainWindow(QWidget *parent)
connect(&mOJProblemSetModel, &OJProblemSetModel::problemNameChanged,
this , &MainWindow::onProblemNameChanged);
ui->pbProblemCases->setVisible(false);
connect(&mTcpServer,&QTcpServer::newConnection,
this, &MainWindow::onNewProblemConnection);
connect(&mCCHandler, &CompetitiveCompanionHandler::newProblemReceived,
this, &MainWindow::onNewProblemReceived);
connect(&mOJProblemModel, &OJProblemModel::dataChanged,
this, &MainWindow::updateProblemTitle);
@ -1057,20 +1057,8 @@ void MainWindow::applySettings()
ui->txtProblemCaseExpected->setFont(caseEditorFont);
ui->lblProblemCaseExpected->setFont(caseEditorFont);
mTcpServer.close();
if (pSettings->executor().enableProblemSet()) {
if (pSettings->executor().enableCompetitiveCompanion()) {
if (!mTcpServer.listen(QHostAddress::LocalHost,pSettings->executor().competivieCompanionPort())) {
// QMessageBox::critical(nullptr,
// tr("Listen failed"),
// tr("Can't listen to port %1 form Competitive Companion.").arg(10045)
// + "<BR/>"
// +tr("You can turn off competitive companion support in the Problem Set options.")
// + "<BR/>"
// +tr("Or You can choose a different port number and try again."));
}
}
}
mCCHandler.stop();
mCCHandler.start();
showHideInfosTab(ui->tabProblemSet,pSettings->ui().showProblemSet()
&& pSettings->executor().enableProblemSet());
@ -4408,71 +4396,11 @@ void MainWindow::onProblemBatchSetCases()
}
}
void MainWindow::onNewProblemConnection()
void MainWindow::onNewProblemReceived(POJProblem newProblem)
{
QTcpSocket* clientConnection = mTcpServer.nextPendingConnection();
connect(clientConnection, &QAbstractSocket::disconnected,
clientConnection, &QObject::deleteLater);
QByteArray content;
int unreadCount = 0;
while (clientConnection->state() == QTcpSocket::ConnectedState) {
clientConnection->waitForReadyRead(100);
QByteArray readed = clientConnection->readAll();
if (readed.isEmpty()) {
unreadCount ++;
if (!content.isEmpty() || unreadCount>30)
break;
} else {
unreadCount = 0;
}
content += readed;
}
content += clientConnection->readAll();
clientConnection->write("HTTP/1.1 200 OK");
clientConnection->disconnectFromHost();
// qDebug()<<"---------";
// qDebug()<<content;
content = getHTTPBody(content);
// qDebug()<<"*********";
// qDebug()<<content;
if (content.isEmpty()) {
if (mOJProblemSetModel.problemNameUsed(newProblem->name))
return;
}
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(content,&error);
if (error.error!=QJsonParseError::NoError) {
qDebug()<<"Read http content failed!";
qDebug()<<error.errorString();
return;
}
QJsonObject obj=doc.object();
QString name = obj["name"].toString();
if (!mOJProblemSetModel.problemNameUsed(name)) {
POJProblem problem = std::make_shared<OJProblem>();
problem->name = name;
problem->url = obj["url"].toString();
QJsonArray caseArray = obj["tests"].toArray();
foreach ( const QJsonValue& val, caseArray) {
QJsonObject caseObj = val.toObject();
POJProblemCase problemCase = std::make_shared<OJProblemCase>();
problemCase->testState = ProblemCaseTestState::NotTested;
problemCase->name = tr("Problem Case %1").arg(problem->cases.count()+1);
if (pSettings->executor().convertHTMLToTextForInput()) {
QTextDocument doc;
doc.setHtml(caseObj["input"].toString());
problemCase->input = doc.toPlainText();
} else
problemCase->input = caseObj["input"].toString();
if (pSettings->executor().convertHTMLToTextForExpected()) {
QTextDocument doc;
doc.setHtml(caseObj["output"].toString());
problemCase->expected = doc.toPlainText();
} else
problemCase->expected = caseObj["output"].toString();
problem->cases.append(problemCase);
}
mOJProblemSetModel.addProblem(problem);
mOJProblemSetModel.addProblem(newProblem);
ui->tabExplorer->setCurrentWidget(ui->tabProblemSet);
ui->lstProblemSet->setCurrentIndex(mOJProblemSetModel.index(
mOJProblemSetModel.count()-1
@ -4482,7 +4410,6 @@ void MainWindow::onNewProblemConnection()
raise(); // for mac OS?
activateWindow();
}
}
void MainWindow::updateProblemTitle()
{
@ -5793,7 +5720,7 @@ void MainWindow::closeEvent(QCloseEvent *event) {
// }
// }
mTcpServer.close();
mCCHandler.stop();
mCompilerManager->stopAllRunners();
mCompilerManager->stopCompile();
mCompilerManager->stopRun();

View File

@ -21,7 +21,6 @@
#include <QMainWindow>
#include <QTimer>
#include <QFileSystemModel>
#include <QTcpServer>
#include <QElapsedTimer>
#include <QSortFilterProxyModel>
#include "common.h"
@ -40,6 +39,7 @@
#include "widgets/ojproblemsetmodel.h"
#include "widgets/customfilesystemmodel.h"
#include "customfileiconprovider.h"
#include "problems/competitivecompenionhandler.h"
QT_BEGIN_NAMESPACE
@ -374,7 +374,7 @@ private slots:
void onProblemNameChanged(int index);
void onProblemRunCurrentCase();
void onProblemBatchSetCases();
void onNewProblemConnection();
void onNewProblemReceived(POJProblem newProblem);
void updateProblemTitle();
void onEditorClosed();
void onToolsOutputClear();
@ -941,7 +941,7 @@ private:
bool mClosingAll;
bool mOpenningFiles;
bool mSystemTurnedOff;
QTcpServer mTcpServer;
CompetitiveCompanionHandler mCCHandler;
QColor mErrorColor;
CompileIssuesState mCompileIssuesState;