From cc0689d94683bd9fe260a4207efe5b20b6d1b272 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 26 Aug 2022 20:45:18 +0800 Subject: [PATCH] NotebookSelector: support dynamic icons for notebooks --- src/data/core/core.qrc | 1 - .../core/icons/native_notebook_default.svg | 1 - src/data/core/icons/notebook_default.svg | 8 +++- src/utils/iconutils.cpp | 36 +++++++++++--- src/utils/iconutils.h | 8 ++++ src/widgets/notebookselector.cpp | 47 +++++++++++++++---- src/widgets/notebookselector.h | 2 + 7 files changed, 83 insertions(+), 20 deletions(-) delete mode 100644 src/data/core/icons/native_notebook_default.svg diff --git a/src/data/core/core.qrc b/src/data/core/core.qrc index 5d8aed59..3319e91c 100644 --- a/src/data/core/core.qrc +++ b/src/data/core/core.qrc @@ -27,7 +27,6 @@ icons/export_menu.svg icons/flash_page_menu.svg icons/quick_access_menu.svg - icons/native_notebook_default.svg icons/notebook_default.svg icons/file_node.svg icons/folder_node.svg diff --git a/src/data/core/icons/native_notebook_default.svg b/src/data/core/icons/native_notebook_default.svg deleted file mode 100644 index 7861b18c..00000000 --- a/src/data/core/icons/native_notebook_default.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/data/core/icons/notebook_default.svg b/src/data/core/icons/notebook_default.svg index b97ab66a..f3d53b07 100644 --- a/src/data/core/icons/notebook_default.svg +++ b/src/data/core/icons/notebook_default.svg @@ -1 +1,7 @@ - + + + + + + + diff --git a/src/utils/iconutils.cpp b/src/utils/iconutils.cpp index a634cb6c..eda90ec1 100644 --- a/src/utils/iconutils.cpp +++ b/src/utils/iconutils.cpp @@ -132,6 +132,17 @@ QIcon IconUtils::fetchIconWithDisabledState(const QString &p_iconFile) QIcon IconUtils::drawTextIcon(const QString &p_text, const QString &p_fg, const QString &p_border) +{ + return drawTextRectIcon(p_text, p_fg, "", p_border, 56, 56, 8); +} + +QIcon IconUtils::drawTextRectIcon(const QString &p_text, + const QString &p_fg, + const QString &p_bg, + const QString &p_border, + int p_rectWidth, + int p_rectHeight, + int p_rectRadius) { const int wid = 64; QPixmap pixmap(wid, wid); @@ -140,22 +151,33 @@ QIcon IconUtils::drawTextIcon(const QString &p_text, QPainter painter(&pixmap); painter.setRenderHint(QPainter::Antialiasing); - auto pen = painter.pen(); - pen.setColor(p_border); - pen.setWidth(3); - painter.setPen(pen); + QPainterPath bgPath; + bgPath.addRoundedRect(QRect((wid - p_rectWidth) / 2, (wid - p_rectHeight) / 2, p_rectWidth, p_rectHeight), + p_rectRadius, + p_rectRadius); - painter.drawRoundedRect(4, 4, wid - 8, wid - 8, 8, 8); + if (!p_bg.isEmpty()) { + painter.fillPath(bgPath, QColor(p_bg)); + } + + const int strokeWidth = 3; + + if (!p_border.isEmpty()) { + QPen pen(QColor(p_border), strokeWidth); + painter.setPen(pen); + painter.drawPath(bgPath); + } if (!p_text.isEmpty()) { - pen.setColor(p_fg); + QPen pen(QColor(p_fg), strokeWidth); painter.setPen(pen); auto font = painter.font(); font.setPointSize(36); + font.setBold(true); painter.setFont(font); - auto requriedRect = painter.boundingRect(4, 4, wid - 8, wid - 8, + auto requriedRect = painter.boundingRect(bgPath.boundingRect(), Qt::AlignCenter, p_text); painter.drawText(requriedRect, p_text); diff --git a/src/utils/iconutils.h b/src/utils/iconutils.h index 64fdd83d..f65ed7d1 100644 --- a/src/utils/iconutils.h +++ b/src/utils/iconutils.h @@ -51,6 +51,14 @@ namespace vnotex const QString &p_fg, const QString &p_border); + static QIcon drawTextRectIcon(const QString &p_text, + const QString &p_fg, + const QString &p_bg, + const QString &p_border, + int p_rectWidth = 56, + int p_rectHeight = 56, + int p_rectRadius = 0); + private: static QString replaceForegroundOfIcon(const QString &p_iconContent, const QString &p_foreground); diff --git a/src/widgets/notebookselector.cpp b/src/widgets/notebookselector.cpp index 27dec9d5..9a6570fe 100644 --- a/src/widgets/notebookselector.cpp +++ b/src/widgets/notebookselector.cpp @@ -62,22 +62,49 @@ void NotebookSelector::addNotebookItem(const QSharedPointer &p_noteboo setItemToolTip(idx, generateItemToolTip(p_notebook.data())); } +void NotebookSelector::fetchIconColor(const QString &p_name, QString &p_fg, QString &p_bg) +{ + static QVector backgroundColors = { + "#80558c", + "#df7861", + "#f65a83", + "#3b9ae1", + "#277bc0", + "#42855b", + "#a62349", + "#a66cff", + "#9c9efe", + "#54bab9", + "#79b4b7", + "#57cc99", + "#916bbf", + "#5c7aea", + "#6867ac", + }; + + int hashVal = 0; + for (int i = 0; i < p_name.size(); ++i) { + hashVal += p_name[i].unicode(); + } + + p_fg = "#ffffff"; + p_bg = backgroundColors[hashVal % backgroundColors.size()]; +} + QIcon NotebookSelector::generateItemIcon(const Notebook *p_notebook) { if (!p_notebook->getIcon().isNull()) { return p_notebook->getIcon(); } - const auto &themeMgr = VNoteX::getInst().getThemeMgr(); - QString iconFile; - const auto &type = p_notebook->getType(); - if (type == "native.vnotex") { - iconFile = themeMgr.getIconFile("native_notebook_default.svg"); - } else { - iconFile = themeMgr.getIconFile("notebook_default.svg"); - } - - return IconUtils::fetchIcon(iconFile); + QString fg, bg; + fetchIconColor(p_notebook->getName(), fg, bg); + return IconUtils::drawTextRectIcon(p_notebook->getName().at(0).toUpper(), + fg, + bg, + "", + 50, + 58); } QString NotebookSelector::generateItemToolTip(const Notebook *p_notebook) diff --git a/src/widgets/notebookselector.h b/src/widgets/notebookselector.h index 74380a5c..e84ccde7 100644 --- a/src/widgets/notebookselector.h +++ b/src/widgets/notebookselector.h @@ -53,6 +53,8 @@ namespace vnotex int findNotebook(ID p_id) const; + static void fetchIconColor(const QString &p_name, QString &p_fg, QString &p_bg); + bool m_notebooksInitialized = false; QVector m_navigationIndexes;