Skip to content

Commit

Permalink
never end up with null skin pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolcha committed Sep 29, 2024
1 parent dbe97f0 commit 787c4b3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ qt_add_executable(${PROJECT_NAME}
core/translation.hpp
gui/overlay_widget.cpp
gui/overlay_widget.hpp
skin/error_skin.cpp
skin/error_skin.hpp
)

set(APP_IDENTIFIER "com.github.kolcha.DigitalClock5")
Expand Down
4 changes: 3 additions & 1 deletion app/core/skin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "skin/font_skin.hpp"
#include "skin/legacy_skin.hpp"

#include "skin/error_skin.hpp"

struct SkinManager::Impl {
QHash<QString, LegacySkinLoader> loaders;
QSet<QString> user_skins;
Expand Down Expand Up @@ -75,7 +77,7 @@ std::unique_ptr<Skin> SkinManager::loadSkin(const QString& id) const
for (auto iter = _impl->loaders.begin(); iter != _impl->loaders.end(); ++iter)
if (iter->title() == id)
return iter->skin();
return nullptr;
return std::make_unique<ErrorSkin>();
}

QStringList SkinManager::availableSkins() const
Expand Down
61 changes: 61 additions & 0 deletions app/skin/error_skin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* SPDX-FileCopyrightText: 2024 Nick Korotysh <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include "error_skin.hpp"

#include <QFontDatabase>
#include <QPainter>

namespace {

class ErrorGlyph : public Skin::Glyph
{
public:
explicit ErrorGlyph(bool vis) noexcept
: _r(0, 0, 240, 100)
, _vis(vis)
{}

QRectF rect() const override { return _r; }
QPointF advance() const override { return {_r.width(), -_r.height()}; }

void draw(QPainter* p) const override;

private:
QRectF _r;
bool _vis = true;
};

void ErrorGlyph::draw(QPainter* p) const
{
p->save();
p->drawRoundedRect(_r.marginsRemoved({5, 5, 5, 5}), 8, 8);
auto fnt = QFontDatabase::systemFont(QFontDatabase::FixedFont);
p->setPen(QColor(0x8C, 0x8A, 0xFF));
fnt.setPointSize(14);
p->setFont(fnt);
p->drawText(_r.adjusted(0, 10, 0, -(_r.height() - 10 - 20)), "Digital Clock 5", {Qt::AlignCenter});
if (_vis) {
fnt.setPointSize(36);
p->setPen(QColor(0xF0, 0x40, 0x58));
p->setFont(fnt);
p->drawText(_r, "ERROR", {Qt::AlignCenter});
}
fnt.setPointSize(14);
p->setFont(fnt);
p->drawText(_r.adjusted(0, _r.height() - 10 - 20, 0, -10), "Skin Not Loaded", {Qt::AlignCenter});
p->restore();
}

} // namespace

std::shared_ptr<Skin::Glyph> ErrorSkin::create(char32_t c) const
{
if (c == ':' || c == ' ') {
return std::make_shared<ErrorGlyph>(c == ':');
}
return nullptr;
}
19 changes: 19 additions & 0 deletions app/skin/error_skin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-FileCopyrightText: 2024 Nick Korotysh <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#pragma once

#include "skin/skin.hpp"

class ErrorSkin : public Skin
{
public:
bool hasAlternateSeparator() const override { return true; }
bool supportsCustomSeparator() const override { return false; }

protected:
std::shared_ptr<Glyph> create(char32_t c) const override;
};

0 comments on commit 787c4b3

Please sign in to comment.