Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed loading of custom font icons #636

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ set(SOURCES
${PROJECT_SOURCE_DIR}/src/elidedlabel.h
${PROJECT_SOURCE_DIR}/src/foldertreedelegateeditor.cpp
${PROJECT_SOURCE_DIR}/src/foldertreedelegateeditor.h
${PROJECT_SOURCE_DIR}/src/fontloader.cpp
${PROJECT_SOURCE_DIR}/src/fontloader.h
${PROJECT_SOURCE_DIR}/src/labeledittype.cpp
${PROJECT_SOURCE_DIR}/src/labeledittype.h
${PROJECT_SOURCE_DIR}/src/listviewlogic.cpp
Expand Down
4 changes: 3 additions & 1 deletion src/allnotebuttontreedelegateeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "nodetreeview.h"
#include "notelistview.h"
#include "editorsettingsoptions.h"
#include "fontloader.h"

AllNoteButtonTreeDelegateEditor::AllNoteButtonTreeDelegateEditor(QTreeView *view,
const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -75,7 +76,8 @@ void AllNoteButtonTreeDelegateEditor::paintEvent(QPaintEvent *event)
#else
int iconPointSizeOffset = -4;
#endif
painter.setFont(QFont("Material Symbols Outlined", 16 + iconPointSizeOffset));
painter.setFont(FontLoader::getInstance().loadFont("Material Symbols Outlined", "",
16 + iconPointSizeOffset));
painter.drawText(iconRect, iconPath); // folder

if (m_view->selectionModel()->isSelected(m_index)) {
Expand Down
4 changes: 3 additions & 1 deletion src/defaultnotefolderdelegateeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nodetreemodel.h"
#include "notelistview.h"
#include "editorsettingsoptions.h"
#include "fontloader.h"

DefaultNoteFolderDelegateEditor::DefaultNoteFolderDelegateEditor(QTreeView *view,
const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -74,7 +75,8 @@ void DefaultNoteFolderDelegateEditor::paintEvent(QPaintEvent *event)
#else
int iconPointSizeOffset = -4;
#endif
painter.setFont(QFont("Material Symbols Outlined", 16 + iconPointSizeOffset));
painter.setFont(FontLoader::getInstance().loadFont("Material Symbols Outlined", "",
16 + iconPointSizeOffset));
painter.drawText(folderIconRect, u8"\ue2c7"); // folder

QRect nameRect(rect());
Expand Down
7 changes: 5 additions & 2 deletions src/foldertreedelegateeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nodetreeview.h"
#include "notelistview.h"
#include "labeledittype.h"
#include "fontloader.h"

FolderTreeDelegateEditor::FolderTreeDelegateEditor(QTreeView *view,
const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -62,7 +63,8 @@ FolderTreeDelegateEditor::FolderTreeDelegateEditor(QTreeView *view,
#else
int iconPointSizeOffset = -4;
#endif
m_expandIcon->setFont(QFont("Font Awesome 6 Free Solid", 10 + iconPointSizeOffset));
m_expandIcon->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
10 + iconPointSizeOffset));

m_expandIcon->setScaledContents(true);
layout->addWidget(m_expandIcon);
Expand Down Expand Up @@ -136,7 +138,8 @@ FolderTreeDelegateEditor::FolderTreeDelegateEditor(QTreeView *view,
#else
int pointSizeOffset = -4;
#endif
m_contextButton->setFont(QFont("Font Awesome 6 Free Solid", 14 + pointSizeOffset));
m_contextButton->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
14 + pointSizeOffset));
m_contextButton->setText(u8"\uf141"); // fa-ellipsis-h

connect(m_contextButton, &QPushButton::clicked, m_view, [this](bool) {
Expand Down
14 changes: 14 additions & 0 deletions src/fontloader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "fontloader.h"

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
FontLoader::FontLoader() : m_fontDatabase() { }
#endif

QFont FontLoader::loadFont(const QString &family, const QString &style, int pointSize)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return m_fontDatabase.font(family, style, pointSize);
#else
return QFontDatabase::font(family, style, pointSize);
#endif
}
26 changes: 26 additions & 0 deletions src/fontloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <QFont>
#include <QString>
#include <QFontDatabase>

class FontLoader
{
public:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QFontDatabase m_fontDatabase;
FontLoader();
#else
FontLoader() = default;
#endif

~FontLoader() = default;

static FontLoader &getInstance()
{
static FontLoader instance;
return instance;
}

QFont loadFont(const QString &family, const QString &style, int pointSize);
};
6 changes: 4 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "tagpool.h"
#include "splitterstyle.h"
#include "editorsettingsoptions.h"
#include "fontloader.h"

#include <QScrollBar>
#include <QShortcut>
Expand Down Expand Up @@ -683,8 +684,9 @@ void MainWindow::setupButtons()
m_yellowMinimizeButton->installEventFilter(this);
m_greenMaximizeButton->installEventFilter(this);

QFont fontAwesomeIcon("Font Awesome 6 Free Solid");
QFont materialSymbols("Material Symbols Outlined");
QFont fontAwesomeIcon = FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "", 16);
QFont materialSymbols = FontLoader::getInstance().loadFont("Material Symbols Outlined", "", 30);

#if defined(Q_OS_MACOS)
int pointSizeOffset = 0;
#else
Expand Down
25 changes: 17 additions & 8 deletions src/nodetreedelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "defaultnotefolderdelegateeditor.h"
#include "allnotebuttontreedelegateeditor.h"
#include <QFontMetrics>
#include "fontloader.h"

NodeTreeDelegate::NodeTreeDelegate(QTreeView *view, QObject *parent, QListView *listView)
: QStyledItemDelegate{ parent },
Expand Down Expand Up @@ -130,21 +131,25 @@ void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
}
if (m_theme == Theme::Dark) {
if (itemType == NodeItem::Type::AllNoteButton) {
painter->setFont(QFont("Material Symbols Outlined", 16 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Material Symbols Outlined", "",
16 + iconPointSizeOffset));
painter->drawText(iconRect, u8"\ue2c7"); // folder
} else if (itemType == NodeItem::Type::TrashButton) {
iconRect.setY(iconRect.y() + 2);
painter->setFont(QFont("Font Awesome 6 Free Solid", 16 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
16 + iconPointSizeOffset));
painter->drawText(iconRect, u8"\uf1f8"); // fa-trash
}
} else {
auto iconPath = index.data(NodeItem::Roles::Icon).toString();
if (itemType == NodeItem::Type::AllNoteButton) {
painter->setFont(QFont("Material Symbols Outlined", 16 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Material Symbols Outlined", "",
16 + iconPointSizeOffset));
painter->drawText(iconRect, iconPath); // folder
} else if (itemType == NodeItem::Type::TrashButton) {
iconRect.setY(iconRect.y() + 2);
painter->setFont(QFont("Font Awesome 6 Free Solid", 16 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
16 + iconPointSizeOffset));
painter->drawText(iconRect, iconPath); // fa-trash
}
}
Expand Down Expand Up @@ -190,7 +195,8 @@ void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
auto iconRect = QRect(option.rect.x() + 10,
option.rect.y() + (option.rect.height() - 12) / 2, 12, 12);
QString iconPath;
painter->setFont(QFont("Font Awesome 6 Free Solid", 10 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
10 + iconPointSizeOffset));
if (m_theme == Theme::Dark) {
painter->setPen(QColor(169, 160, 172));
if ((option.state & QStyle::State_Open) == QStyle::State_Open) {
Expand Down Expand Up @@ -221,7 +227,8 @@ void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
} else {
painter->setPen(m_folderIconColor);
}
painter->setFont(QFont("Material Symbols Outlined", 16 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Material Symbols Outlined", "",
16 + iconPointSizeOffset));
painter->drawText(folderIconRect, u8"\ue2c7"); // folder

QRect nameRect(option.rect);
Expand Down Expand Up @@ -274,7 +281,8 @@ void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
option.rect.y() + (option.rect.height() - 14) / 2, 16, 16);
auto tagColor = index.data(NodeItem::Roles::TagColor).toString();
painter->setPen(QColor(tagColor));
painter->setFont(QFont("Font Awesome 6 Free Solid", 16 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
16 + iconPointSizeOffset));
painter->drawText(iconRect, u8"\uf111"); // fa-circle
painter->setBrush(Qt::black);
painter->setPen(Qt::black);
Expand Down Expand Up @@ -375,7 +383,8 @@ QWidget *NodeTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
#else
int iconPointSizeOffset = -4;
#endif
addButton->setFont(QFont("Font Awesome 6 Free Solid", 16 + iconPointSizeOffset));
addButton->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
16 + iconPointSizeOffset));
addButton->setText(u8"\uf067"); // fa_plus
addButton->setStyleSheet(QStringLiteral(R"(QPushButton { )"
R"( border: none; )"
Expand Down
11 changes: 7 additions & 4 deletions src/notelistdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#include <QEvent>
#include <QDebug>
#include <QApplication>
#include <QFontDatabase>
#include <QtMath>
#include <QPainterPath>
#include "notelistmodel.h"
#include "noteeditorlogic.h"
#include "tagpool.h"
#include "nodepath.h"
#include "notelistdelegateeditor.h"
#include "fontloader.h"

NoteListDelegate::NoteListDelegate(NoteListView *view, TagPool *tagPool, QObject *parent)
: QStyledItemDelegate(parent),
Expand Down Expand Up @@ -632,7 +632,8 @@ void NoteListDelegate::paintLabels(QPainter *painter, const QStyleOptionViewItem
#else
int iconPointSizeOffset = -4;
#endif
painter->setFont(QFont("Font Awesome 6 Free Solid", 14 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
14 + iconPointSizeOffset));
painter->setPen(QColor(68, 138, 201));
if (m_view->isPinnedNotesCollapsed()) {
painter->drawText(QRect(headerRect.right() - 25, headerRect.y() + 5, 16, 16),
Expand Down Expand Up @@ -691,7 +692,8 @@ void NoteListDelegate::paintLabels(QPainter *painter, const QStyleOptionViewItem
#else
int iconPointSizeOffset = -4;
#endif
painter->setFont(QFont("Font Awesome 6 Free Solid", 14 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
14 + iconPointSizeOffset));
painter->setPen(QColor(68, 138, 201));
if (m_view->isPinnedNotesCollapsed()) {
painter->drawText(QRect(headerRect.right() - 25, headerRect.y() + 5, 16, 16),
Expand Down Expand Up @@ -841,7 +843,8 @@ void NoteListDelegate::paintTagList(int top, QPainter *painter, const QStyleOpti
#else
int iconPointSizeOffset = -4;
#endif
painter->setFont(QFont("Font Awesome 6 Free Solid", 14 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
14 + iconPointSizeOffset));
painter->drawText(iconRect, u8"\uf111"); // fa-circle
painter->setBrush(m_titleColor);
painter->setPen(m_titleColor);
Expand Down
5 changes: 3 additions & 2 deletions src/notelistdelegateeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <QEvent>
#include <QDebug>
#include <QApplication>
#include <QFontDatabase>
#include <QtMath>
#include <QPainterPath>
#include <QScrollBar>
Expand All @@ -17,6 +16,7 @@
#include "taglistview.h"
#include "taglistmodel.h"
#include "taglistdelegate.h"
#include "fontloader.h"

NoteListDelegateEditor::NoteListDelegateEditor(const NoteListDelegate *delegate, NoteListView *view,
const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -290,7 +290,8 @@ void NoteListDelegateEditor::paintLabels(QPainter *painter, const QStyleOptionVi
#else
int iconPointSizeOffset = -4;
#endif
painter->setFont(QFont("Font Awesome 6 Free Solid", 14 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
14 + iconPointSizeOffset));
painter->setPen(QColor(68, 138, 201));
if (m_view->isPinnedNotesCollapsed()) {
painter->drawText(QRect(headerRect.right() - 25, headerRect.y() + 5, 16, 16),
Expand Down
4 changes: 3 additions & 1 deletion src/notelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "dbmanager.h"
#include "notelistview_p.h"
#include "notelistdelegateeditor.h"
#include "fontloader.h"

NoteListView::NoteListView(QWidget *parent)
: QListView(parent),
Expand Down Expand Up @@ -720,7 +721,8 @@ void NoteListView::onCustomContextMenu(QPoint point)
#else
int iconPointSizeOffset = -4;
#endif
painter.setFont(QFont("Font Awesome 6 Free Solid", 24 + iconPointSizeOffset));
painter.setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
24 + iconPointSizeOffset));
painter.drawText(iconRect, u8"\uf111"); // fa-circle
return QIcon{ pix };
};
Expand Down
4 changes: 3 additions & 1 deletion src/taglistdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "taglistmodel.h"
#include <QPainter>
#include <QPainterPath>
#include "fontloader.h"

TagListDelegate::TagListDelegate(QObject *parent)
: QStyledItemDelegate(parent),
Expand Down Expand Up @@ -48,7 +49,8 @@ void TagListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optio
#else
int iconPointSizeOffset = -4;
#endif
painter->setFont(QFont("Font Awesome 6 Free Solid", 12 + iconPointSizeOffset));
painter->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
12 + iconPointSizeOffset));
painter->drawText(iconRect, u8"\uf111"); // fa-circle
painter->setBrush(m_titleColor);
painter->setPen(m_titleColor);
Expand Down
7 changes: 5 additions & 2 deletions src/tagtreedelegateeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nodetreeview.h"
#include "labeledittype.h"
#include "notelistview.h"
#include "fontloader.h"

TagTreeDelegateEditor::TagTreeDelegateEditor(QTreeView *view, const QStyleOptionViewItem &option,
const QModelIndex &index, QListView *listView,
Expand Down Expand Up @@ -100,7 +101,8 @@ TagTreeDelegateEditor::TagTreeDelegateEditor(QTreeView *view, const QStyleOption
#else
int pointSizeOffset = -4;
#endif
m_contextButton->setFont(QFont("Font Awesome 6 Free Solid", 14 + pointSizeOffset));
m_contextButton->setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
14 + pointSizeOffset));
m_contextButton->setText(u8"\uf141"); // fa-ellipsis-h

connect(m_contextButton, &QPushButton::clicked, m_view, [this](bool) {
Expand Down Expand Up @@ -163,7 +165,8 @@ void TagTreeDelegateEditor::paintEvent(QPaintEvent *event)
#else
int iconPointSizeOffset = -4;
#endif
painter.setFont(QFont("Font Awesome 6 Free Solid", 16 + iconPointSizeOffset));
painter.setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
16 + iconPointSizeOffset));
painter.drawText(iconRect, u8"\uf111"); // fa-circle
QWidget::paintEvent(event);
}
Expand Down
4 changes: 3 additions & 1 deletion src/trashbuttondelegateeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nodetreemodel.h"
#include "nodetreeview.h"
#include "notelistview.h"
#include "fontloader.h"

TrashButtonDelegateEditor::TrashButtonDelegateEditor(QTreeView *view,
const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -73,7 +74,8 @@ void TrashButtonDelegateEditor::paintEvent(QPaintEvent *event)
#else
int iconPointSizeOffset = -4;
#endif
painter.setFont(QFont("Font Awesome 6 Free Solid", 16 + iconPointSizeOffset));
painter.setFont(FontLoader::getInstance().loadFont("Font Awesome 6 Free Solid", "",
16 + iconPointSizeOffset));
painter.drawText(iconRect, iconPath); // fa-trash

if (m_view->selectionModel()->isSelected(m_index)) {
Expand Down