diff --git a/src/detaildialog.cpp b/src/detaildialog.cpp index e2d59d45..da220d60 100644 --- a/src/detaildialog.cpp +++ b/src/detaildialog.cpp @@ -18,6 +18,7 @@ #include "core/task.h" #include "core/testcase.h" #include "judgingdialog.h" +#include "sourceviewer.h" // #include #include @@ -86,9 +87,9 @@ void DetailDialog::refreshViewer(Contest *_contest, Contestant *_contestant) { break; case CompileTimeLimitExceeded: - htmlCode += QString("  %1%2
") - .arg(tr("Source file: ")) - .arg(contestant->getSourceFile(i)); + htmlCode += QString(R"(  %1%2
)") + .arg(tr("Source file: "), contestant->getSourceFile(i).toHtmlEscaped(), + QString::number(i)); htmlCode += QString("  %1

").arg(tr("Compile time limit exceeded")); break; @@ -97,9 +98,9 @@ void DetailDialog::refreshViewer(Contest *_contest, Contestant *_contestant) { break; case CompileError: - htmlCode += QString("  %1%2
") - .arg(tr("Source file: ")) - .arg(contestant->getSourceFile(i)); + htmlCode += QString(R"(  %1%2
)") + .arg(tr("Source file: "), contestant->getSourceFile(i).toHtmlEscaped(), + QString::number(i)); htmlCode += QString("  %1").arg(tr("Compile error")); if (! contestant->getCompileMessage(i).isEmpty()) @@ -118,7 +119,8 @@ void DetailDialog::refreshViewer(Contest *_contest, Contestant *_contestant) { } htmlCode += - QString("  %1%2").arg(tr("Source file: ")).arg(contestant->getSourceFile(i)); + QString(R"(  %1%2)") + .arg(tr("Source file: "), contestant->getSourceFile(i).toHtmlEscaped(), QString::number(i)); } htmlCode += R"()"; @@ -279,6 +281,10 @@ void DetailDialog::anchorClicked(const QUrl &url) { .exec(); } + if (list[0] == "Source") { + SourceViewer::viewSource(this, contest, contestant, list[1].toInt()); + } + if (list[0] == "Message") { QList message = contestant->getMessage(list[1].toInt()); QMessageBox(QMessageBox::NoIcon, tr("Message"), diff --git a/src/resultviewer.cpp b/src/resultviewer.cpp index 40461ec6..e36027d2 100644 --- a/src/resultviewer.cpp +++ b/src/resultviewer.cpp @@ -17,6 +17,7 @@ #include "core/task.h" #include "detaildialog.h" #include "judgingdialog.h" +#include "sourceviewer.h" // #include #include @@ -71,9 +72,27 @@ void ResultViewer::contextMenuEvent(QContextMenuEvent * /*event*/) { auto *contextMenu = new QMenu(this); - if (selectionRange.size() == 1 && selectionRange[0].rowCount() == 1) { + if (curContest && selectionRange.size() == 1 && selectionRange[0].rowCount() == 1) { contextMenu->addAction(detailInformationAction); contextMenu->setDefaultAction(detailInformationAction); + + // Open the source code of the selected contestant (issue #225) + const QList taskList = curContest->getTaskList(); + QList sourceTaskIndices; + + for (int i = 0; i < taskList.size(); i++) { + if (taskList[i]->getTaskType() != Task::AnswersOnly) + sourceTaskIndices.append(i); + } + + if (! sourceTaskIndices.empty()) { + QMenu *viewSourceMenu = contextMenu->addMenu(tr("View Source")); + + for (int i : sourceTaskIndices) { + QAction *viewSourceAction = viewSourceMenu->addAction(taskList[i]->getProblemTitle()); + connect(viewSourceAction, &QAction::triggered, this, [this, i]() { viewSource(i); }); + } + } } if (! selectionRange.empty()) { @@ -436,3 +455,18 @@ void ResultViewer::detailInformation() { dialog->showDialog(); delete dialog; } + +void ResultViewer::viewSource(int taskIndex) { + QList selectionRange = selectedRanges(); + + if (selectionRange.empty() || ! curContest) + return; + + int index = selectionRange[0].topRow(); + Contestant *contestant = curContest->getContestant(item(index, 1)->text()); + + if (contestant == nullptr) + return; + + SourceViewer::viewSource(this, curContest, contestant, taskIndex); +} diff --git a/src/resultviewer.h b/src/resultviewer.h index 72c66f0a..636f1c97 100644 --- a/src/resultviewer.h +++ b/src/resultviewer.h @@ -42,6 +42,7 @@ class ResultViewer : public QTableWidget { private slots: void deleteContestant(); void detailInformation(); + void viewSource(int taskIndex); signals: void contestantDeleted(); diff --git a/src/sourceviewer.cpp b/src/sourceviewer.cpp new file mode 100644 index 00000000..36853970 --- /dev/null +++ b/src/sourceviewer.cpp @@ -0,0 +1,81 @@ +/* + * SPDX-FileCopyrightText: 2026 Project LemonLime + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "sourceviewer.h" +// +#include "base/settings.h" +#include "core/contest.h" +#include "core/contestant.h" +#include "core/task.h" +// +#include +#include +#include +#include +#include + +namespace { + +// Mirror TaskJudger::traditionalTaskPrepare()'s layout: the contestant's +// source directory gains the task's source-file-name sub-folder only when +// SubFolderCheck is enabled. +QString contestantSourceDir(Contest *contest, Contestant *contestant, int taskIndex) { + const Task *task = contest->getTask(taskIndex); + QString dir = Settings::sourcePath() + contestant->getContestantName(); + + if (task->getSubFolderCheck()) + dir += QDir::separator() + task->getSourceFileName(); + + return dir; +} + +} // namespace + +void SourceViewer::viewSource(QWidget *parent, Contest *contest, Contestant *contestant, int taskIndex) { + const Task *task = contest->getTask(taskIndex); + + if (task == nullptr || task->getTaskType() == Task::AnswersOnly) { + QMessageBox::information(parent, tr("View Source"), tr("Cannot find the source file of this task.")); + return; + } + + const QString dir = contestantSourceDir(contest, contestant, taskIndex); + + // Communication tasks: getSourceFile() holds the compiled names (e.g. + // send.cpp), while getSourceFilesPath() holds the actual submitted files. + // Ordinary tasks: getSourceFile() is one complete file name, which must not + // be split on spaces. + QStringList fileNames; + + if (task->getTaskType() == Task::Communication || task->getTaskType() == Task::CommunicationExec) + fileNames = task->getSourceFilesPath(); + else + fileNames = {contestant->getSourceFile(taskIndex)}; + + QStringList filePaths; + + for (const QString &fileName : fileNames) { + if (! fileName.isEmpty()) + filePaths.append(dir + QDir::separator() + fileName); + } + + if (filePaths.isEmpty()) { + QMessageBox::information(parent, tr("View Source"), tr("Cannot find the source file of this task.")); + return; + } + + for (const QString &path : filePaths) { + if (! QFileInfo::exists(path)) { + QMessageBox::information(parent, tr("View Source"), + tr("Source file %1 does not exist.").arg(QDir::toNativeSeparators(path))); + return; + } + } + + for (const QString &path : filePaths) + QDesktopServices::openUrl(QUrl::fromLocalFile(QDir(path).absolutePath())); +} diff --git a/src/sourceviewer.h b/src/sourceviewer.h new file mode 100644 index 00000000..42e994e5 --- /dev/null +++ b/src/sourceviewer.h @@ -0,0 +1,25 @@ +/* + * SPDX-FileCopyrightText: 2026 Project LemonLime + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once +// + +#include + +class Contest; +class Contestant; +class QWidget; + +// Opens a contestant's submitted sources with the user's own editor via the +// platform file association, instead of embedding a syntax-highlighting viewer. +class SourceViewer final : public QObject { + Q_OBJECT + + public: + /** Open the source file(s) of the contestant's taskIndex-th task in the system editor. */ + static void viewSource(QWidget *parent, Contest *contest, Contestant *contestant, int taskIndex); +}; diff --git a/translations/zh_CN.ts b/translations/zh_CN.ts index 0388869f..da35df85 100755 --- a/translations/zh_CN.ts +++ b/translations/zh_CN.ts @@ -2074,6 +2074,10 @@ Depends: Details 详情 + + View Source + 查看源代码 + Judge 测试 @@ -2188,6 +2192,21 @@ Depends: 仿 joi2020 + + SourceViewer + + View Source + 查看源代码 + + + Cannot find the source file of this task. + 找不到该任务的源文件。 + + + Source file %1 does not exist. + 源文件 %1 不存在。 + + StatisticsBrowser diff --git a/translations/zh_TW.ts b/translations/zh_TW.ts index 22070493..0585978e 100644 --- a/translations/zh_TW.ts +++ b/translations/zh_TW.ts @@ -2074,6 +2074,10 @@ Depends: Details 詳情 + + View Source + 查看原始碼 + Judge 測試 @@ -2188,6 +2192,21 @@ Depends: + + SourceViewer + + View Source + 查看原始碼 + + + Cannot find the source file of this task. + 找不到該任務的原始碼檔案。 + + + Source file %1 does not exist. + 原始碼檔案 %1 不存在。 + + StatisticsBrowser