Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ build-debug/
.idea/
cmake-build-debug/
.vscode/

# Local tooling
.mimosa
.zcode/
17 changes: 17 additions & 0 deletions src/base/LemonBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//
#include <QCoreApplication>
#include <QDir>
#include <QFont>
#include <QMap>
#include <QStandardPaths>

Expand All @@ -22,6 +23,22 @@
#define LEMON_BUILD_EXTRA_INFO QString(LEMON_BUILD_EXTRA_INFO_STR)

namespace Lemon {
// The CJK-capable UI font of the application, shared by the main window
// (main.cpp) and the source code viewer. Missing families fall back to the
// platform default font, so listing them unconditionally is safe.
inline QFont uiFont() {
QFont font;
#ifdef Q_OS_WIN32
font.setFamilies({"Microsoft YaHei"});
font.setHintingPreference(QFont::PreferNoHinting);
#elif defined(Q_OS_LINUX)
font.setFamilies({"Noto Sans CJK SC", "WenQuanYi Micro Hei"});
#elif defined(Q_OS_MAC)
font.setFamilies({"PingFang SC"});
#endif
return font;
}

inline QStringList LemonAssetsPaths(const QString &dirName) {
#define makeAbs(p) QDir(p).absolutePath()
// Configuration Path
Expand Down
143 changes: 119 additions & 24 deletions src/component/exportutil/exportutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ auto ExportUtil::getContestantHtmlCode(Contest *contest, Contestant *contestant,
htmlCode += "</table><br></p></div>";
}

htmlCode += QString("<p><a href=\"#top\">%1</a></p>").arg(tr("Return to top"));
return htmlCode;
}
/*
Expand Down Expand Up @@ -269,7 +268,7 @@ void ExportUtil::writeHtml(QTextStream &out, Contest *contest) {
out << R"(<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />)";

// Style sheet
out << "<style type=\"text/css\">"
out << R"(<style type="text/css" id="lemon-style">)"
"th, td {padding-left: 1em; padding-right: 1em; white-space: nowrap; "
"text-align: center; verticle-align: middle;}"
".td-0 {border-style: none solid solid none; border-width: 1px 3px; border-color: #ccc;}"
Expand All @@ -287,14 +286,73 @@ void ExportUtil::writeHtml(QTextStream &out, Contest *contest) {
}
out << "</style>";

// The popup helper is emitted in <head> so that it is already defined while
// the rest of a large (tens of MB) document is still parsing; everything it
// touches at call time (style sheet, templates, body dataset) is looked up
// lazily. Clicking a link before the per-contestant templates have been
// parsed shows a "still loading" alert instead of doing nothing silently.
out << R"LEMONJS(
<script>
(function () {
"use strict";
function styleDetailDocument(doc) {
doc.querySelectorAll("div[id^='c'] th").forEach(e => e.classList.add("td-0"));
doc.querySelectorAll("div[id^='c'] td").forEach(e => e.classList.add("td-0"));
doc.querySelectorAll("div[id^='c']>p>span").forEach(e => {e.style.fontWeight = "bold"; e.style.fontSize = "large";});
doc.querySelectorAll("div[id^='c']>p>table").forEach(e => e.style.border = "solid");
doc.querySelectorAll("div[id^='c']>p>table th").forEach(e => e.setAttribute("scope", "col"));
}
window.lemonOpenDetail = function (contestantIndex, taskIndex) {
const template = document.getElementById("c" + contestantIndex);
if (!template) {
alert(document.body.dataset.popupLoading || "The details are still loading.");
return false;
}
// The "popup" feature plus a stable window name opens a real popup
// window (not a browser tab) and reuses it across contestants
const popup = window.open("", "LemonDetail",
"popup=yes,width=960,height=760,scrollbars=yes,resizable=yes");
if (!popup) {
alert(document.body.dataset.popupBlocked || "Popup blocked");
return false;
}
popup.document.open();
// The inline window.focus() raises the popup while it parses; the
// opener-side focus() alone is not enough on Chromium-based browsers.
// Note: the closing tag is written as <\/script> so the exported
// page's own script block is not terminated early.
popup.document.write("<html><head><meta charset=\"utf-8\"><style>" +
document.getElementById("lemon-style").textContent +
"</style></head><body>" + template.textContent +
"<script>window.focus()<\/script></body></html>");
popup.document.close();
popup.document.title = document.title;
styleDetailDocument(popup.document);
if (typeof taskIndex === "number") {
const target = popup.document.getElementById("c" + contestantIndex + "p" + taskIndex);
if (target)
target.scrollIntoView();
}
popup.focus();
return false;
};
})();
</script>
)LEMONJS";

out << "<title>" << contest->getContestTitle().toHtmlEscaped() << " : " << tr("Contest Result")
<< "</title>";
out << "</head><body>";
out << QString(R"(</head><body data-popup-blocked="%1" data-popup-loading="%2">)")
.arg(tr("The browser blocked the popup window. Please allow popups for this page and try again.")
.toHtmlEscaped(),
tr("The details are still loading. Please try again in a moment.")
.toHtmlEscaped());

out << "<p><span class=\"d\">";
out << "<a name=\"top\"></a>" << contest->getContestTitle().toHtmlEscaped() << " : " << tr("Rank List")
<< "</span></p>";
out << "<p>" << tr("Click names or task scores to jump to details. Judged By LemonLime") << "</p>";
out << "<p>"
<< tr("Click names or task scores to view details in a popup window. Judged By LemonLime") << "</p>";
out << R"(<p><table cellpadding="1" style="border-style: solid;"><tr>)";
out << QString(R"(<th class="th-0" scope="col">%1</th>)").arg(tr("Rank"));
out << QString(R"(<th class="th-0" scope="col">%1</th>)").arg(tr("Name"));
Expand All @@ -314,7 +372,7 @@ void ExportUtil::writeHtml(QTextStream &out, Contest *contest) {
for (const auto &[contestant, allScore, sourceIndex, rank] : rankContestants(contestantList)) {
out << "<tr>";
out << QString("<td class=\"td-0\">%1</td>").arg(rank);
out << QString(R"(<td class="td-0"><a href="#c%1" class="a-0">%2</a></td>)")
out << QString(R"LEMON(<td class="td-0"><a href="#" onclick="return lemonOpenDetail(%1)" class="a-0">%2</a></td>)LEMON")
.arg(sourceIndex)
.arg(contestant->getContestantName().toHtmlEscaped());

Expand Down Expand Up @@ -368,15 +426,15 @@ void ExportUtil::writeHtml(QTextStream &out, Contest *contest) {

h *= 360, s *= 100, l *= 100;
out << QString(
R"(<td class="td-3" style="background: hsl(%2,%3%,%4%);"><a href="#c%5p%6" class="a-0">%1</a></td>)")
R"LEMON(<td class="td-3" style="background: hsl(%2,%3%,%4%);"><a href="#" onclick="return lemonOpenDetail(%5,%6)" class="a-0">%1</a></td>)LEMON")
.arg(score)
.arg(h)
.arg(s)
.arg(l)
.arg(sourceIndex)
.arg(j);
} else {
out << QString(R"(<td class="td-3"><a href="#c%2p%3" class="a-0">%1</a></td>)")
out << QString(R"LEMON(<td class="td-3"><a href="#" onclick="return lemonOpenDetail(%2,%3)" class="a-0">%1</a></td>)LEMON")
.arg(tr("Invalid"))
.arg(sourceIndex)
.arg(j);
Expand All @@ -387,25 +445,20 @@ void ExportUtil::writeHtml(QTextStream &out, Contest *contest) {

out << "</table></p>";

// Contestant details are stored as text templates instead of DOM nodes, so that
// browsers only render the details actually opened in a popup window (#235)
for (int i = 0; i < contestantList.size(); i++) {
out << QString("<a name=\"c%1\"><hr></a>").arg(i) << "<span class=\"d\">";
out << QString(R"(<script type="text/template" id="c%1">)").arg(i);
out << "<span class=\"d\">";
out << tr("Contestant: %1").arg(contestantList[i]->getContestantName().toHtmlEscaped()) << "</span>";
out << getContestantHtmlCode(contest, contestantList[i], i);
out << "</script>";
}

out << QString(R"(<footer><p><i>Lemonlime Version %1:%2</i></p></footer>)")
.arg(LEMON_VERSION_STRING)
.arg(LEMON_VERSION_BUILD);

out << R"(
<script>
document.querySelectorAll("div[id^='c'] th").forEach(e=>e.classList.add("td-0"));
document.querySelectorAll("div[id^='c'] td").forEach(e=>e.classList.add("td-0"));
document.querySelectorAll("div[id^='c']>p>span").forEach(e=>{e.style.fontWeight="bold";e.style.fontSize="large";});
document.querySelectorAll("div[id^='c']>p>table").forEach(e=>e.style.border="solid");
document.querySelectorAll("div[id^='c']>p>table th").forEach(e=>e.setAttribute("scope","col"));
</script>
)";
out << "</body>";
out << "</html>";
}
Expand All @@ -431,7 +484,7 @@ auto ExportUtil::getSmallerContestantHtmlCode(Contest *contest, Contestant *cont
if (contestant->getCompileState(i) != CompileSuccessfully) {
switch (contestant->getCompileState(i)) {
case NoValidGraderFile:
htmlCode += QString("&nbsp;&nbsp;%1</p></a>")
htmlCode += QString("&nbsp;&nbsp;%1</p>")
.arg(tr("Main grader (grader.*) cannot be found"));
break;

Expand Down Expand Up @@ -567,7 +620,6 @@ auto ExportUtil::getSmallerContestantHtmlCode(Contest *contest, Contestant *cont
htmlCode += "</table><br></p>";
}

htmlCode += QString("<p><a href=\"#top\">%1</a></p>").arg(tr("Return to top"));
return htmlCode;
}

Expand All @@ -592,20 +644,59 @@ void ExportUtil::writeSmallerHtml(QTextStream &out, Contest *contest) {
QList<Task *> taskList = contest->getTaskList();
out << "<html><head>";
out << R"(<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />)";
out << "<style type=\"text/css\">th, td {padding-left: 1em; padding-right: 1em; white-space: nowrap; "
out << "<style type=\"text/css\" id=\"lemon-style\">th, td {padding-left: 1em; padding-right: 1em; white-space: nowrap; "
"text-align: center; verticle-align: middle;} .a {border-style: none solid solid none; "
"border-width: 1px 3px; border-color: #ccc;} .b {border-style: none solid solid none; "
"border-width: 3px 3px; border-color: #000;} .c {font-weight: bold; font-size: large;} "
".d {font-size:x-large; font-weight:bold;} .e {font-size: small;} .f {font-weight: bold;}"
".g {border-style: none solid solid none; border-width: 3px 2px; border-color: #000;}</style>";

// Same rationale as in writeHtml(): define the popup helper in <head>
out << R"LEMONJS(
<script>
(function () {
"use strict";
window.lemonOpenDetail = function (contestantIndex) {
const template = document.getElementById("c" + contestantIndex);
if (!template) {
alert(document.body.dataset.popupLoading || "The details are still loading.");
return false;
}
// The "popup" feature plus a stable window name opens a real popup
// window (not a browser tab) and reuses it across contestants
const popup = window.open("", "LemonDetail",
"popup=yes,width=960,height=760,scrollbars=yes,resizable=yes");
if (!popup) {
alert(document.body.dataset.popupBlocked || "Popup blocked");
return false;
}
popup.document.open();
popup.document.write("<html><head><meta charset=\"utf-8\"><style>" +
document.getElementById("lemon-style").textContent +
"</style></head><body>" + template.textContent +
"<script>window.focus()<\/script></body></html>");
popup.document.close();
popup.document.title = document.title;
popup.focus();
return false;
};
})();
</script>
)LEMONJS";

out << "<title>" << contest->getContestTitle().toHtmlEscaped() << " : " << tr("Contest Result")
<< "</title>";
out << "</head><body>";
out << QString(R"(</head><body data-popup-blocked="%1" data-popup-loading="%2">)")
.arg(tr("The browser blocked the popup window. Please allow popups for this page and try again.")
.toHtmlEscaped(),
tr("The details are still loading. Please try again in a moment.")
.toHtmlEscaped());

out << "<p><span class=\"d\">";
out << "<a name=\"top\"></a>" << contest->getContestTitle().toHtmlEscaped() << " : " << tr("Rank List")
<< "</span></p>";
out << "<p class=\"e\">" << tr("Judged By LemonLime") << "</p>";
out << "<p class=\"e\">"
<< tr("Click names to view details in a popup window. Judged By LemonLime") << "</p>";
out << R"(<p><table border="1" cellpadding="1"><tr>)";
out << QString("<th scope=\"col\">%1</th>").arg(tr("Rank"));
out << QString("<th scope=\"col\">%1</th>").arg(tr("Name"));
Expand All @@ -617,7 +708,7 @@ void ExportUtil::writeSmallerHtml(QTextStream &out, Contest *contest) {
out << QString("</tr>");
for (const auto &[contestant, allScore, sourceIndex, rank] : rankContestants(contestantList)) {
out << QString("<tr><td>%1</td>").arg(rank);
out << QString("<td><a href=\"#c%1\">%2</a></td>")
out << QString(R"LEMON(<td><a href="#" onclick="return lemonOpenDetail(%1)">%2</a></td>)LEMON")
.arg(sourceIndex)
.arg(contestant->getContestantName().toHtmlEscaped());

Expand All @@ -640,10 +731,14 @@ void ExportUtil::writeSmallerHtml(QTextStream &out, Contest *contest) {

out << "</table></p>";

// Contestant details are stored as text templates instead of DOM nodes, so that
// browsers only render the details actually opened in a popup window (#235)
for (int i = 0; i < contestantList.size(); i++) {
out << QString("<a name=\"c%1\"><hr><a>").arg(i) << "<span class=\"d\">";
out << QString(R"(<script type="text/template" id="c%1">)").arg(i);
out << "<span class=\"d\">";
out << tr("Contestant: %1").arg(contestantList[i]->getContestantName().toHtmlEscaped()) << "</span>";
out << getSmallerContestantHtmlCode(contest, contestantList[i]);
out << "</script>";
}

out << "</body></html>";
Expand Down
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") {
SourceViewerDialog::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
13 changes: 1 addition & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,7 @@ int main(int argc, char *argv[]) {
return 0;
}

#ifdef Q_OS_LINUX
// fonts.setFamily("Noto Sans CJK SC");
#endif
#ifdef Q_OS_WIN32
QFont fonts;
fonts.setFamily("Microsoft YaHei");
fonts.setHintingPreference(QFont::PreferNoHinting);
SingleApplication::setFont(fonts);
#endif
#ifdef Q_OS_MAC
// fonts.setFamily("PingFangSC-Regular");
#endif
SingleApplication::setFont(Lemon::uiFont());
Q_INIT_RESOURCE(resource);
QPixmap pixmap(":/logo/splash2.png");
QSplashScreen screen(pixmap.scaled(450, 191, Qt::KeepAspectRatio, Qt::SmoothTransformation));
Expand Down
Loading