Skip to content

feat(ui): view contestant source code from judging results (#225) - #338

Open
zhuyanyu1234 wants to merge 11 commits into
Project-LemonLime:masterfrom
zhuyanyu1234:feat/225-view-source
Open

zhuyanyu1234 wants to merge 11 commits into
Project-LemonLime:masterfrom
zhuyanyu1234:feat/225-view-source

Conversation

@zhuyanyu1234

Copy link
Copy Markdown

Closes #225

功能

评测结束后可直接查看选手源代码,无需切换到资源管理器查找文件(适用于教师讲评场景)。

  • 结果列表右键 →「查看源代码」子菜单,按题目列出;AnswersOnly 题目不提供该项
  • 详情窗口中的「源程序: xxx」文件名改为可点击链接,沿用现有 anchorClicked 伪 URL 机制
  • 新增只读 SourceViewerDialog:
    • 简易语法高亮:按扩展名区分 C 系 / Pascal,覆盖关键字、字符串、注释、预处理行、数字;VS Code 风格配色,亮/暗主题下均清晰(显式 stylesheet 着色,不随应用主题发灰)
    • 行号栏、当前行高亮、10.5pt 字体、4 空格制表位
    • 代码区使用等宽字体 + CJK 回退,保证代码列对齐且中文注释可读(详见「实现」)
    • 编码:UTF-8 解码失败时回退系统本地编码(GBK)
    • 通信题多文件以标签页展示;源文件路径解析与 TaskJudger 完全一致(含子文件夹模式)

实现

  • 新增 src/sourceviewer.{h,cpp}(由 aux_source_directory 自动收录)
  • src/resultviewer.{h,cpp}:右键子菜单 + viewSource(int) 槽
  • src/detaildialog.cpp:三处 Source file 文本改为链接,并接入 anchorClicked 处理
  • src/base/LemonBase.hpp:
    • 新增 Lemon::uiFont():跨平台 UI 字体(Windows 微软雅黑 / Linux Noto Sans CJK SC / macOS PingFang SC);main.cpp 原有的 #ifdef 字体块收敛为一行,并激活原本注释掉的 Linux/macOS 分支(缺字体自动回退默认)
    • 新增 Lemon::codeFont():等宽代码字体 —— Windows Consolas + 微软雅黑、Linux DejaVu Sans Mono + Noto Sans Mono CJK SC、macOS Menlo + PingFang SC,并设置 QFont::Monospace 样式提示;sourceviewer.cpp 编辑器改用此字体,全局 UI 字体不受影响
  • 翻译:zh_CN / zh_TW 补充新字符串
  • 兼容性:Qt 6.11 已移除 QString::fromLocal8bit,本地编码回退改用 QStringDecoder(System)

测试

  • MSVC 2026 + Qt 6.11.1 构建通过(lemon 目标)
  • ctest 5/6:仅 windows-sandbox 因本机未启用 Windows Sandbox 失败,与本 PR 无关
  • 手动验证:右键查看、详情内链接、未评测与文件缺失提示;等宽字体下代码对齐与中文注释显示

附带

  • chore(test):unix/test/scripts/redirect.py 改用 pathlib 并补充临时文件清理(行为等价,如不需要可去掉该提交)

Comment thread src/base/LemonBase.hpp Outdated
font.setFamilies({"Microsoft YaHei"});
font.setHintingPreference(QFont::PreferNoHinting);
#elif defined(Q_OS_LINUX)
font.setFamilies({"Noto Sans CJK SC", "WenQuanYi Micro Hei"});

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.

Don't assume user-installed fonts, leave it empty or use QFont::System to use user-chose fonts via system font fallback mechanism like Fontconfig.

Comment thread src/base/LemonBase.hpp Outdated
#ifdef Q_OS_WIN32
font.setFamilies({"Consolas", "Microsoft YaHei"});
#elif defined(Q_OS_LINUX)
font.setFamilies({"DejaVu Sans Mono", "Noto Sans Mono CJK SC"});

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.

Don't assume user-installed fonts, use QFont::Monospace to use user-chose monospace fonts via system font fallback mechanism.

Comment thread src/sourceviewer.cpp Outdated

} // namespace

class SourceHighlighter final : public QSyntaxHighlighter {

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.

Don't do parser by yourself, use a library like KSyntaxHighlighting.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

等等,这是不是有些重量级了?确认加吗?

@ZnPdCo

ZnPdCo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

我觉得应该直接打开吧,用用户的文本编辑器打开就好

@zhuyanyu1234

Copy link
Copy Markdown
Author

直接打开的话...会不会用户只有记事本?对于这种情况怎么处理呢?(或许可以加一个外部打开的选项)

@alphagocc

Copy link
Copy Markdown
Member

项目组不愿意维护代码高亮,这个工作量肯定是要外包出去的。我觉得只有 notepad 的话应该让机房管理员装一个什么 dev cpp 之类的。

@zhuyanyu1234

Copy link
Copy Markdown
Author

改了,现在在系统查看器里看

zhuyanyu123 and others added 10 commits November 27, 2026 19:55
…emonLime#225)

Add a read-only SourceViewerDialog with lightweight C-like/Pascal syntax
highlighting and UTF-8 fallback to local 8-bit encoding. Reach it from
the ResultViewer context menu (View Source, per task) and from clickable
source file names in DetailDialog. Submitted source paths are resolved
the same way TaskJudger does, including subfolder and communication
multi-file tasks.
Include QTabBar/QString explicitly and replace QString::fromLocal8bit
(removed in Qt 6.11) with a QStringDecoder using the System codec for
the non-UTF-8 fallback.
Fix the inverted light/dark theme detection that rendered light-theme
highlight colors on the dark editor background. Use self-contained
VS Code-inspired palettes with an explicit editor stylesheet so keywords,
strings, comments and identifiers stay readable on both themes, and add
a line-number gutter, a current-line highlight, a larger font and
4-space tab stops.
CJK comments in contestant code rendered via an ugly bitmap fallback
with the monospace system font; Microsoft YaHei keeps them crisp, with
CJK-aware fallback families for other platforms.
Extract the font setup previously inlined in main.cpp's #ifdef blocks
into Lemon::uiFont(), activate the Linux (Noto Sans CJK SC) and macOS
(PingFang SC) branches that had been left commented out, and let the
source code viewer use the same font so CJK comments stay crisp on
every platform. Missing families fall back to the platform default.
@zhuyanyu1234

Copy link
Copy Markdown
Author

等一下,那之前加的字体回退要删吗?

Comment thread src/sourceviewer.h

// 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.

过度封装

Comment thread src/sourceviewer.cpp Outdated
return {};

// Communication tasks store a space-separated list of submitted files
const QStringList files = contestant->getSourceFile(taskIndex).split(' ', Qt::SkipEmptyParts);

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.

通信题的 getSourceFile() 保存编译时使用的名称。选手提交 sender.cc、题目配置将其复制为 send.cpp 时,此处会在选手目录查找 send.cpp,可能报缺失,也可能打开同名的其他文件。通信题应从 Task::getSourceFilesPath() 取得提交文件列表。普通题的 getSourceFile() 则保存单个完整文件名,按空格拆分会使 sum pairs.cpp 被误查为 sum。

Comment thread src/base/LemonBase.hpp Outdated
// glyphs automatically.
inline QFont uiFont() {
QFont font;
font.setStyleHint(QFont::System);

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.

这个默认的是啥,默认就是 QFont::System?

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.

感觉不需要了。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]想在评测结果上直接查看选手的源代码

4 participants