Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/detaildialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "core/task.h"
#include "core/testcase.h"
#include "judgingdialog.h"
#include "sourceviewer.h"
//
#include <QMessageBox>
#include <QScrollBar>
Expand Down Expand Up @@ -86,9 +87,9 @@ void DetailDialog::refreshViewer(Contest *_contest, Contestant *_contestant) {
break;

case CompileTimeLimitExceeded:
htmlCode += QString("&nbsp;&nbsp;%1%2<br>")
.arg(tr("Source file: "))
.arg(contestant->getSourceFile(i));
htmlCode += QString(R"(&nbsp;&nbsp;%1<a href="Source %3" style="text-decoration: none">%2</a><br>)")
.arg(tr("Source file: "), contestant->getSourceFile(i).toHtmlEscaped(),
QString::number(i));
htmlCode += QString("&nbsp;&nbsp;%1</p>").arg(tr("Compile time limit exceeded"));
break;

Expand All @@ -97,9 +98,9 @@ void DetailDialog::refreshViewer(Contest *_contest, Contestant *_contestant) {
break;

case CompileError:
htmlCode += QString("&nbsp;&nbsp;%1%2<br>")
.arg(tr("Source file: "))
.arg(contestant->getSourceFile(i));
htmlCode += QString(R"(&nbsp;&nbsp;%1<a href="Source %3" style="text-decoration: none">%2</a><br>)")
.arg(tr("Source file: "), contestant->getSourceFile(i).toHtmlEscaped(),
QString::number(i));
htmlCode += QString("&nbsp;&nbsp;%1").arg(tr("Compile error"));

if (! contestant->getCompileMessage(i).isEmpty())
Expand All @@ -118,7 +119,8 @@ void DetailDialog::refreshViewer(Contest *_contest, Contestant *_contestant) {
}

htmlCode +=
QString("&nbsp;&nbsp;%1%2").arg(tr("Source file: ")).arg(contestant->getSourceFile(i));
QString(R"(&nbsp;&nbsp;%1<a href="Source %3" style="text-decoration: none">%2</a>)")
.arg(tr("Source file: "), contestant->getSourceFile(i).toHtmlEscaped(), QString::number(i));
}

htmlCode += R"(<table width="100%" cellpadding="1" border="1"><tr>)";
Expand Down Expand Up @@ -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<QStringList> message = contestant->getMessage(list[1].toInt());
QMessageBox(QMessageBox::NoIcon, tr("Message"),
Expand Down
36 changes: 35 additions & 1 deletion src/resultviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "core/task.h"
#include "detaildialog.h"
#include "judgingdialog.h"
#include "sourceviewer.h"
//
#include <QApplication>
#include <QCheckBox>
Expand Down Expand Up @@ -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<Task *> taskList = curContest->getTaskList();
QList<int> 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()) {
Expand Down Expand Up @@ -436,3 +455,18 @@ void ResultViewer::detailInformation() {
dialog->showDialog();
delete dialog;
}

void ResultViewer::viewSource(int taskIndex) {
QList<QTableWidgetSelectionRange> 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);
}
1 change: 1 addition & 0 deletions src/resultviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ResultViewer : public QTableWidget {
private slots:
void deleteContestant();
void detailInformation();
void viewSource(int taskIndex);

signals:
void contestantDeleted();
Expand Down
81 changes: 81 additions & 0 deletions src/sourceviewer.cpp
Original file line number Diff line number Diff line change
@@ -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 <QDesktopServices>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
#include <QUrl>

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()));
}
25 changes: 25 additions & 0 deletions src/sourceviewer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-FileCopyrightText: 2026 Project LemonLime
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/

#pragma once
//

#include <QObject>

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

过度封装

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);
};
19 changes: 19 additions & 0 deletions translations/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,10 @@ Depends: </source>
<source>Details</source>
<translation>详情</translation>
</message>
<message>
<source>View Source</source>
<translation>查看源代码</translation>
</message>
<message>
<source>Judge</source>
<translation>测试</translation>
Expand Down Expand Up @@ -2188,6 +2192,21 @@ Depends: </source>
<translation>仿 joi2020</translation>
</message>
</context>
<context>
<name>SourceViewer</name>
<message>
<source>View Source</source>
<translation>查看源代码</translation>
</message>
<message>
<source>Cannot find the source file of this task.</source>
<translation>找不到该任务的源文件。</translation>
</message>
<message>
<source>Source file %1 does not exist.</source>
<translation>源文件 %1 不存在。</translation>
</message>
</context>
<context>
<name>StatisticsBrowser</name>
<message>
Expand Down
19 changes: 19 additions & 0 deletions translations/zh_TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,10 @@ Depends: </source>
<source>Details</source>
<translation>詳情</translation>
</message>
<message>
<source>View Source</source>
<translation>查看原始碼</translation>
</message>
<message>
<source>Judge</source>
<translation>測試</translation>
Expand Down Expand Up @@ -2188,6 +2192,21 @@ Depends: </source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SourceViewer</name>
<message>
<source>View Source</source>
<translation>查看原始碼</translation>
</message>
<message>
<source>Cannot find the source file of this task.</source>
<translation>找不到該任務的原始碼檔案。</translation>
</message>
<message>
<source>Source file %1 does not exist.</source>
<translation>原始碼檔案 %1 不存在。</translation>
</message>
</context>
<context>
<name>StatisticsBrowser</name>
<message>
Expand Down