From 7e70607f275dc787515a82c2bfdab7fd8f4938b6 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 22 May 2024 11:18:39 +0800 Subject: [PATCH] Split competitivecompenionhandler from mainwindow --- RedPandaIDE/RedPandaIDE.pro | 2 + RedPandaIDE/main.cpp | 2 + RedPandaIDE/mainwindow.cpp | 105 ++++++------------------------------ RedPandaIDE/mainwindow.h | 6 +-- 4 files changed, 23 insertions(+), 92 deletions(-) diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index 1ee7944e..a90ec332 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -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 \ diff --git a/RedPandaIDE/main.cpp b/RedPandaIDE/main.cpp index 843129c6..1e37d936 100644 --- a/RedPandaIDE/main.cpp +++ b/RedPandaIDE/main.cpp @@ -39,6 +39,7 @@ #include "widgets/choosethemedialog.h" #include "thememanager.h" #include "utils/font.h" +#include "problems/ojproblemset.h" #ifdef Q_OS_WIN #include @@ -341,6 +342,7 @@ int main(int argc, char *argv[]) app.installTranslator(&transQt); } } + qRegisterMetaType("POJProblem"); qRegisterMetaType("PCompileIssue"); qRegisterMetaType("PCompileIssue&"); qRegisterMetaType>("QVector"); diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 98938cf0..6192515b 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -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) -// + "
" -// +tr("You can turn off competitive companion support in the Problem Set options.") -// + "
" -// +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,80 +4396,19 @@ 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()<name)) return; - } - QJsonParseError error; - QJsonDocument doc = QJsonDocument::fromJson(content,&error); - if (error.error!=QJsonParseError::NoError) { - qDebug()<<"Read http content failed!"; - qDebug()<(); - 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(); - 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); - ui->tabExplorer->setCurrentWidget(ui->tabProblemSet); - ui->lstProblemSet->setCurrentIndex(mOJProblemSetModel.index( - mOJProblemSetModel.count()-1 - ,0)); - if (isMinimized()) - showNormal(); - raise(); // for mac OS? - activateWindow(); - } + mOJProblemSetModel.addProblem(newProblem); + ui->tabExplorer->setCurrentWidget(ui->tabProblemSet); + ui->lstProblemSet->setCurrentIndex(mOJProblemSetModel.index( + mOJProblemSetModel.count()-1 + ,0)); + if (isMinimized()) + showNormal(); + 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(); diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index 0dc8dff7..223cdae5 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #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;