Skip to content

Commit

Permalink
feat(mainwindow): choice of new session on competitive companion
Browse files Browse the repository at this point in the history
It sometimes crashes when request is received, but I haven't found the reason.
  • Loading branch information
ouuan committed Dec 14, 2019
1 parent 0a64b2a commit 22af606
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
5 changes: 3 additions & 2 deletions include/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ class MainWindow : public QMainWindow {
void saveSettings();
void restoreSettings();
void setupCore();
void launchSession();
void launchCompanionSession(Network::CompanionData);
void clearTests();
void launchSession(bool);
void applyCompanion(Network::CompanionData);
void checkUpdates();
void createAndAttachServer();
void updateVerdict(Core::Verdict, int);
Expand Down
82 changes: 49 additions & 33 deletions src/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ MainWindow::MainWindow(QString filePath, QWidget* parent)
runEditorDiagonistics();
setupCore();
runner->removeExecutable();
launchSession();
launchSession(false);
checkUpdates();

if (!filePath.isEmpty()) {
Expand Down Expand Up @@ -287,7 +287,28 @@ void MainWindow::setupCore() {
SLOT(onSaveTimerElapsed()));
}

void MainWindow::launchSession() {
void MainWindow::clearTests() {
ui->in1->clear();
ui->in2->clear();
ui->in3->clear();

ui->out1->clear();
ui->out2->clear();
ui->out3->clear();

expected1->clear();
expected2->clear();
expected3->clear();

updateVerdict(Core::Verdict::UNKNOWN, 1);
updateVerdict(Core::Verdict::UNKNOWN, 2);
updateVerdict(Core::Verdict::UNKNOWN, 3);
}

void MainWindow::launchSession(bool confirm) {
if (confirm && !closeChangedConfirm())
return;

if (openFile != nullptr) {
if (openFile->isOpen()) {
openFile->close();
Expand All @@ -313,21 +334,8 @@ void MainWindow::launchSession() {
}

this->window()->setWindowTitle("CP Editor: Temporary buffer");
ui->in1->clear();
ui->in2->clear();
ui->in3->clear();

ui->out1->clear();
ui->out2->clear();
ui->out3->clear();

expected1->clear();
expected2->clear();
expected3->clear();

updateVerdict(Core::Verdict::UNKNOWN, 1);
updateVerdict(Core::Verdict::UNKNOWN, 2);
updateVerdict(Core::Verdict::UNKNOWN, 3);
clearTests();
}

void MainWindow::updateVerdict(Core::Verdict verdict, int target) {
Expand Down Expand Up @@ -372,18 +380,27 @@ void MainWindow::createAndAttachServer() {
}
}

void MainWindow::launchCompanionSession(Network::CompanionData data) {
launchSession();
QString meta = data.toMetaString();
meta.prepend("\n");
meta.append("Powered by CP Editor (https://github.com/coder3101/cp-editor2)");
void MainWindow::applyCompanion(Network::CompanionData data) {
auto res = QMessageBox::warning(this, tr("New Session?"),
tr("A request from competitive companion received,\nopen a new session?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);

if (language == "Python")
meta.replace('\n', "\n# ");
else
meta.replace('\n', "\n// ");
if (res == QMessageBox::Yes) {
launchSession(true);
QString meta = data.toMetaString();
meta.prepend("\n");
meta.append("Powered by CP Editor (https://github.com/coder3101/cp-editor2)");

if (language == "Python")
meta.replace('\n', "\n# ");
else
meta.replace('\n', "\n// ");

editor->setPlainText(meta + "\n\n" + editor->toPlainText());
}

editor->setPlainText(meta + "\n\n" + editor->toPlainText());
clearTests();

if (data.testcases.size() > 3) {
Log::MessageLogger::warn(
Expand All @@ -409,9 +426,9 @@ void MainWindow::launchCompanionSession(Network::CompanionData data) {

// ******************* STATUS::ACTIONS FILE **************************
void MainWindow::on_actionNew_triggered() {
if (closeChangedConfirm())
launchSession();
launchSession(true);
}

void MainWindow::on_actionOpen_triggered() {
auto fileName = QFileDialog::getOpenFileName(
this, tr("Open File"), "",
Expand Down Expand Up @@ -819,7 +836,7 @@ void MainWindow::onSaveTimerElapsed() {
}

void MainWindow::onCompanionRequest(Network::CompanionData data) {
launchCompanionSession(data);
applyCompanion(data);
Log::MessageLogger::info("Companion",
"Established the testcases. Start Coding");
}
Expand Down Expand Up @@ -1117,14 +1134,13 @@ bool MainWindow::isTextChanged() {
f.open(QIODevice::ReadOnly | QFile::Text);
return editor->toPlainText() != f.readAll();
}
else {
return !editor->toPlainText().isEmpty();
}
return !editor->toPlainText().isEmpty();
}
else {
if (openFile->isOpen()) {
openFile->seek(0);
return openFile->readAll() != editor->toPlainText();
}
return true;
}

bool MainWindow::closeChangedConfirm() {
Expand Down

0 comments on commit 22af606

Please sign in to comment.