Browse Source

NotebookSelector: support dynamic icons for notebooks

Le Tan 3 years ago
parent
commit
cc0689d946

+ 0 - 1
src/data/core/core.qrc

@@ -27,7 +27,6 @@
         <file>icons/export_menu.svg</file>
         <file>icons/flash_page_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/file_node.svg</file>
         <file>icons/folder_node.svg</file>

+ 0 - 1
src/data/core/icons/native_notebook_default.svg

@@ -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>

+ 7 - 1
src/data/core/icons/notebook_default.svg

@@ -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>

+ 29 - 7
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);
+
+    if (!p_bg.isEmpty()) {
+        painter.fillPath(bgPath, QColor(p_bg));
+    }
+
+    const int strokeWidth = 3;
 
-    painter.drawRoundedRect(4, 4, wid - 8, wid - 8, 8, 8);
+    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);

+ 8 - 0
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);
 

+ 37 - 10
src/widgets/notebookselector.cpp

@@ -62,22 +62,49 @@ void NotebookSelector::addNotebookItem(const QSharedPointer<Notebook> &p_noteboo
     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)
 {
     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)

+ 2 - 0
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<QModelIndex> m_navigationIndexes;