Skip to content

Commit

Permalink
feat(ui): auto dark mode in result viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
alphagocc committed Jul 14, 2024
1 parent 25382be commit c1bf2b4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion makespec/BUILDVERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
268
269
11 changes: 10 additions & 1 deletion src/base/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ QColor ColorTheme::getColorNf() const { return colorNf; }

QColor ColorTheme::getColorCe() const { return colorCe; }

void ColorTheme::invertLightness() {
mxColor.l = 100 - mxColor.l;
miColor.l = 100 - miColor.l;
nfColor.l = 100 - nfColor.l;
ceColor.l = 100 - ceColor.l;
colorNf = nfColor.toHsl();
colorCe = ceColor.toHsl();
}

void ColorTheme::copyFrom(ColorTheme *others) {
setName(others->getName());
setColor(others->getMxColor(), others->getMiColor(), others->getNfColor(), others->getCeColor(),
Expand Down Expand Up @@ -131,7 +140,7 @@ auto Settings::getCompilerList() const -> const QList<Compiler *> & { return com

const QList<ColorTheme *> &Settings::getColorThemeList() const { return colorThemeList; }

const ColorTheme *Settings::getCurrentColorTheme() const { return colorThemeList[currentColorTheme]; }
ColorTheme Settings::getCurrentColorTheme() const { return *colorThemeList[currentColorTheme]; }

auto Settings::getUiLanguage() const -> const QString & { return uiLanguage; }

Expand Down
8 changes: 7 additions & 1 deletion src/base/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ class ColorTheme {
QColor getColorPer(double, double) const;
QColor getColorGrand(double, double) const;

void invertLightness();

private:
QString name;
// mxColor maxColor
// miColor minColor
// nfColor nofileColor
// ceColor compileErrorColor
hslTuple mxColor, miColor, nfColor, ceColor;
QColor colorNf, colorCe;
dddTuple grandComp, grandRate;
Expand Down Expand Up @@ -116,7 +122,7 @@ class Settings {
void addColorTheme(ColorTheme *);
void deleteColorTheme(int);
ColorTheme *getColorTheme(int);
const ColorTheme *getCurrentColorTheme() const;
ColorTheme getCurrentColorTheme() const;
int getCurrentColorThemeIndex() const;

void setColorTheme(ColorTheme *, int);
Expand Down
11 changes: 5 additions & 6 deletions src/component/exportutil/exportutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ auto ExportUtil::getContestantHtmlCode(Contest *contest, Contestant *contestant,
*/
void ExportUtil::exportHtml(QWidget *widget, Contest *contest, const QString &fileName) {
Settings settings;
const ColorTheme *colors;
contest->copySettings(settings);
colors = settings.getCurrentColorTheme();
ColorTheme colors = settings.getCurrentColorTheme();

QFile file(fileName);

Expand Down Expand Up @@ -343,7 +342,7 @@ void ExportUtil::exportHtml(QWidget *widget, Contest *contest, const QString &fi
double s = NAN;
double l = NAN;
#endif
colors->getColorGrand(allScore, sfullScore).getHslF(&h, &s, &l);
colors.getColorGrand(allScore, sfullScore).getHslF(&h, &s, &l);
h *= 360, s *= 100, l *= 100;
out << QString("<td class=\"td-2\" style=\"background: hsl(%2,%3%,%4%); border: 2px solid "
"hsl(%2,%3%,%5%);\">%1</td>")
Expand All @@ -369,15 +368,15 @@ void ExportUtil::exportHtml(QWidget *widget, Contest *contest, const QString &fi
double s = NAN;
double l = NAN;
#endif
QColor col = colors->getColorPer(score, fullScore[j]);
QColor col = colors.getColorPer(score, fullScore[j]);
col.getHslF(&h, &s, &l);

if (taskList[j]->getTaskType() != Task::AnswersOnly &&
contestant->getCompileState(j) != CompileSuccessfully) {
if (contestant->getCompileState(j) == NoValidSourceFile) {
colors->getColorNf().getHslF(&h, &s, &l);
colors.getColorNf().getHslF(&h, &s, &l);
} else {
colors->getColorCe().getHslF(&h, &s, &l);
colors.getColorCe().getHslF(&h, &s, &l);
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/resultviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QtGlobal>
#include <algorithm>

#define LEMON_MODULE_NAME "ResultViewer"
Expand Down Expand Up @@ -107,8 +108,14 @@ void ResultViewer::refreshViewer() {
QList<Task *> taskList = curContest->getTaskList();
Settings setting;
curContest->copySettings(setting);
const ColorTheme *colors = setting.getCurrentColorTheme();
ColorTheme colors = setting.getCurrentColorTheme();

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
colors.invertLightness();
LOG("Auto dark mode has been set");
}
#endif
for (auto &i : taskList) {
headerList << i->getProblemTitle();
}
Expand Down Expand Up @@ -144,11 +151,11 @@ void ResultViewer::refreshViewer() {
if (taskList[j]->getTaskType() != Task::AnswersOnly &&
contestantList[i]->getCompileState(j) != CompileSuccessfully) {
if (contestantList[i]->getCompileState(j) == NoValidSourceFile)
bg = colors->getColorNf();
bg = colors.getColorNf();
else
bg = colors->getColorCe();
bg = colors.getColorCe();
} else
bg = colors->getColorPer(score, fullScore[j]);
bg = colors.getColorPer(score, fullScore[j]);

item(i, j + 3)->setBackground(bg);

Expand All @@ -166,7 +173,7 @@ void ResultViewer::refreshViewer() {

if (totalScore != -1) {
item(i, 2)->setData(Qt::DisplayRole, totalScore);
item(i, 2)->setBackground(colors->getColorGrand(totalScore, sfullScore));
item(i, 2)->setBackground(colors.getColorGrand(totalScore, sfullScore));
QFont font;
font.setBold(true);
item(i, 2)->setFont(font);
Expand Down

0 comments on commit c1bf2b4

Please sign in to comment.