NotebookSelector: support dynamic icons for notebooks

This commit is contained in:
Le Tan 2022-08-26 20:45:18 +08:00
parent fe4b6ca72e
commit cc0689d946
7 changed files with 83 additions and 20 deletions

View File

@ -27,7 +27,6 @@
<file>icons/export_menu.svg</file> <file>icons/export_menu.svg</file>
<file>icons/flash_page_menu.svg</file> <file>icons/flash_page_menu.svg</file>
<file>icons/quick_access_menu.svg</file> <file>icons/quick_access_menu.svg</file>
<file>icons/native_notebook_default.svg</file>
<file>icons/notebook_default.svg</file> <file>icons/notebook_default.svg</file>
<file>icons/file_node.svg</file> <file>icons/file_node.svg</file>
<file>icons/folder_node.svg</file> <file>icons/folder_node.svg</file>

View File

@ -1 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?><svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 8C5 6.89543 5.89543 6 7 6H19L24 12H41C42.1046 12 43 12.8954 43 14V40C43 41.1046 42.1046 42 41 42H7C5.89543 42 5 41.1046 5 40V8Z" fill="none" stroke="#000000" stroke-width="4" stroke-linejoin="round"/><path d="M43 22H5" stroke="#000000" stroke-width="4" stroke-linejoin="round"/><path d="M5 16V28" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M43 16V28" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>

Before

Width:  |  Height:  |  Size: 633 B

View File

@ -1 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M8 6C8 4.89543 8.89543 4 10 4H38C39.1046 4 40 4.89543 40 6V42C40 43.1046 39.1046 44 38 44H10C8.89543 44 8 43.1046 8 42V6Z" fill="none" stroke="#000000" stroke-width="4" stroke-linejoin="round"/><path d="M16 4V44" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M24 12H32" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M24 20H32" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M10 4H22" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M10 44H22" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg> <?xml version="1.0" encoding="UTF-8"?>
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M10 6C10 4.89543 10.8954 4 12 4H40C41.1046 4 42 4.89543 42 6V42C42 43.1046 41.1046 44 40 44H12C10.8954 44 10 43.1046 10 42V6Z" fill="none" stroke="#000000" stroke-width="4" stroke-linejoin="round"/>
<path d="M6 14H14" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M6 24H14" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M6 34H14" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

Before

Width:  |  Height:  |  Size: 851 B

After

Width:  |  Height:  |  Size: 672 B

View File

@ -132,6 +132,17 @@ QIcon IconUtils::fetchIconWithDisabledState(const QString &p_iconFile)
QIcon IconUtils::drawTextIcon(const QString &p_text, QIcon IconUtils::drawTextIcon(const QString &p_text,
const QString &p_fg, const QString &p_fg,
const QString &p_border) 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; const int wid = 64;
QPixmap pixmap(wid, wid); QPixmap pixmap(wid, wid);
@ -140,22 +151,33 @@ QIcon IconUtils::drawTextIcon(const QString &p_text,
QPainter painter(&pixmap); QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
auto pen = painter.pen(); QPainterPath bgPath;
pen.setColor(p_border); bgPath.addRoundedRect(QRect((wid - p_rectWidth) / 2, (wid - p_rectHeight) / 2, p_rectWidth, p_rectHeight),
pen.setWidth(3); p_rectRadius,
painter.setPen(pen); 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()) { if (!p_text.isEmpty()) {
pen.setColor(p_fg); QPen pen(QColor(p_fg), strokeWidth);
painter.setPen(pen); painter.setPen(pen);
auto font = painter.font(); auto font = painter.font();
font.setPointSize(36); font.setPointSize(36);
font.setBold(true);
painter.setFont(font); painter.setFont(font);
auto requriedRect = painter.boundingRect(4, 4, wid - 8, wid - 8, auto requriedRect = painter.boundingRect(bgPath.boundingRect(),
Qt::AlignCenter, Qt::AlignCenter,
p_text); p_text);
painter.drawText(requriedRect, p_text); painter.drawText(requriedRect, p_text);

View File

@ -51,6 +51,14 @@ namespace vnotex
const QString &p_fg, const QString &p_fg,
const QString &p_border); 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: private:
static QString replaceForegroundOfIcon(const QString &p_iconContent, const QString &p_foreground); static QString replaceForegroundOfIcon(const QString &p_iconContent, const QString &p_foreground);

View File

@ -62,22 +62,49 @@ void NotebookSelector::addNotebookItem(const QSharedPointer<Notebook> &p_noteboo
setItemToolTip(idx, generateItemToolTip(p_notebook.data())); setItemToolTip(idx, generateItemToolTip(p_notebook.data()));
} }
void NotebookSelector::fetchIconColor(const QString &p_name, QString &p_fg, QString &p_bg)
{
static QVector<QString> 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) QIcon NotebookSelector::generateItemIcon(const Notebook *p_notebook)
{ {
if (!p_notebook->getIcon().isNull()) { if (!p_notebook->getIcon().isNull()) {
return p_notebook->getIcon(); return p_notebook->getIcon();
} }
const auto &themeMgr = VNoteX::getInst().getThemeMgr(); QString fg, bg;
QString iconFile; fetchIconColor(p_notebook->getName(), fg, bg);
const auto &type = p_notebook->getType(); return IconUtils::drawTextRectIcon(p_notebook->getName().at(0).toUpper(),
if (type == "native.vnotex") { fg,
iconFile = themeMgr.getIconFile("native_notebook_default.svg"); bg,
} else { "",
iconFile = themeMgr.getIconFile("notebook_default.svg"); 50,
} 58);
return IconUtils::fetchIcon(iconFile);
} }
QString NotebookSelector::generateItemToolTip(const Notebook *p_notebook) QString NotebookSelector::generateItemToolTip(const Notebook *p_notebook)

View File

@ -53,6 +53,8 @@ namespace vnotex
int findNotebook(ID p_id) const; int findNotebook(ID p_id) const;
static void fetchIconColor(const QString &p_name, QString &p_fg, QString &p_bg);
bool m_notebooksInitialized = false; bool m_notebooksInitialized = false;
QVector<QModelIndex> m_navigationIndexes; QVector<QModelIndex> m_navigationIndexes;