diff --git a/source/src/trashbuttondelegateeditor.cpp b/source/src/trashbuttondelegateeditor.cpp index f5cb987..099f2e6 100644 --- a/source/src/trashbuttondelegateeditor.cpp +++ b/source/src/trashbuttondelegateeditor.cpp @@ -1,3 +1,4 @@ +//包含头文件 #include "trashbuttondelegateeditor.h" #include #include @@ -7,6 +8,8 @@ #include "notelistview.h" #include "fontloader.h" +// TrashButtonDelegateEditor 类的构造函数 +// 参数包括视图、样式选项、索引、列表视图和父窗口 TrashButtonDelegateEditor::TrashButtonDelegateEditor(QTreeView *view, const QStyleOptionViewItem &option, const QModelIndex &index, QListView *listView, @@ -15,19 +18,24 @@ TrashButtonDelegateEditor::TrashButtonDelegateEditor(QTreeView *view, m_option(option), m_index(index), #ifdef __APPLE__ + // 在苹果系统上使用 SF Pro Text 字体,如果没有则使用 Roboto m_displayFont(QFont(QStringLiteral("SF Pro Text")).exactMatch() ? QStringLiteral("SF Pro Text") : QStringLiteral("Roboto")), #elif _WIN32 + // 在 Windows 系统上使用 Segoe UI 字体,如果没有则使用 Roboto m_displayFont(QFont(QStringLiteral("Segoe UI")).exactMatch() ? QStringLiteral("Segoe UI") : QStringLiteral("Roboto")), #else + // 在其他系统上使用 Roboto 字体 m_displayFont(QStringLiteral("Roboto")), #endif #ifdef __APPLE__ + // 在苹果系统上设置标题和笔记数量的字体大小 m_titleFont(m_displayFont, 13, QFont::DemiBold), m_numberOfNotesFont(m_displayFont, 12, QFont::DemiBold), #else + // 在其他系统上设置标题和笔记数量的字体大小 m_titleFont(m_displayFont, 10, QFont::DemiBold), m_numberOfNotesFont(m_displayFont, 9, QFont::DemiBold), #endif @@ -41,18 +49,22 @@ TrashButtonDelegateEditor::TrashButtonDelegateEditor(QTreeView *view, m_view(view), m_listView(listView) { + // 设置内容边距为0 setContentsMargins(0, 0, 0, 0); } +// 绘制事件处理函数 void TrashButtonDelegateEditor::paintEvent(QPaintEvent *event) { QPainter painter(this); + // 绘制图标区域 auto iconRect = QRect(rect().x() + 22, rect().y() + 2 + (rect().height() - 20) / 2, 18, 20); auto iconPath = m_index.data(NodeItem::Roles::Icon).toString(); auto displayName = m_index.data(NodeItem::Roles::DisplayText).toString(); QRect nameRect(rect()); nameRect.setLeft(iconRect.x() + iconRect.width() + 5); nameRect.setWidth(nameRect.width() - 22 - 40); + // 根据是否选中设置背景颜色和文本颜色 if (m_view->selectionModel()->isSelected(m_index)) { painter.fillRect(rect(), QBrush(m_activeColor)); painter.setPen(m_titleSelectedColor); @@ -74,10 +86,12 @@ void TrashButtonDelegateEditor::paintEvent(QPaintEvent *event) #else int iconPointSizeOffset = -4; #endif + // 设置图标字体 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)) { painter.setPen(m_titleSelectedColor); } else { @@ -86,10 +100,12 @@ void TrashButtonDelegateEditor::paintEvent(QPaintEvent *event) painter.setFont(m_titleFont); painter.drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, displayName); + // 绘制子项数量区域 auto childCountRect = rect(); childCountRect.setLeft(nameRect.right() + 22); childCountRect.setWidth(childCountRect.width() - 5); auto childCount = m_index.data(NodeItem::Roles::ChildCount).toInt(); + // 根据是否选中设置子项数量文本颜色 if (m_view->selectionModel()->isSelected(m_index)) { painter.setPen(m_numberOfNotesSelectedColor); } else { @@ -101,6 +117,7 @@ void TrashButtonDelegateEditor::paintEvent(QPaintEvent *event) QWidget::paintEvent(event); } +// 设置主题函数 void TrashButtonDelegateEditor::setTheme(Theme::Value theme) { m_theme = theme; diff --git a/source/src/trashbuttondelegateeditor.h b/source/src/trashbuttondelegateeditor.h index 1d2236c..3040037 100644 --- a/source/src/trashbuttondelegateeditor.h +++ b/source/src/trashbuttondelegateeditor.h @@ -10,13 +10,16 @@ class QTreeView; class QListView; +// TrashButtonDelegateEditor 类声明 class TrashButtonDelegateEditor : public QWidget { Q_OBJECT public: + // 构造函数 explicit TrashButtonDelegateEditor(QTreeView *view, const QStyleOptionViewItem &option, const QModelIndex &index, QListView *listView, QWidget *parent = nullptr); + // 设置主题函数 void setTheme(Theme::Value theme); private: @@ -36,7 +39,9 @@ private: QListView *m_listView; Theme::Value m_theme; // QWidget interface + // QWidget 接口 protected: + // 绘制事件处理函数 virtual void paintEvent(QPaintEvent *event) override; }; diff --git a/source/src/treeviewlogic.cpp b/source/src/treeviewlogic.cpp index 67bce01..7b5871f 100644 --- a/source/src/treeviewlogic.cpp +++ b/source/src/treeviewlogic.cpp @@ -1,3 +1,4 @@ +// 包含头文件 #include "treeviewlogic.h" #include "nodetreeview.h" #include "nodetreemodel.h" @@ -11,6 +12,7 @@ #include #include "customapplicationstyle.h" +// TreeViewLogic 类的构造函数 TreeViewLogic::TreeViewLogic(NodeTreeView *treeView, NodeTreeModel *treeModel, DBManager *dbManager, NoteListView *listView, QObject *parent) : QObject(parent), @@ -24,12 +26,16 @@ TreeViewLogic::TreeViewLogic(NodeTreeView *treeView, NodeTreeModel *treeModel, D m_lastSelectTags{}, m_expandedFolder{} { + // 初始化树视图代理 m_treeDelegate = new NodeTreeDelegate(m_treeView, m_treeView, m_listView); m_treeView->setItemDelegate(m_treeDelegate); + // 连接数据库管理器信号 connect(m_dbManager, &DBManager::nodesTagTreeReceived, this, &TreeViewLogic::loadTreeModel, Qt::QueuedConnection); + // 连接树模型信号 connect(m_treeModel, &NodeTreeModel::topLevelItemLayoutChanged, this, &TreeViewLogic::updateTreeViewSeparator); + // 连接树视图信号 connect(m_treeView, &NodeTreeView::addFolderRequested, this, [this] { onAddFolderRequested(false); }); connect(m_treeDelegate, &NodeTreeDelegate::addFolderRequested, this, @@ -78,19 +84,23 @@ TreeViewLogic::TreeViewLogic(NodeTreeView *treeView, NodeTreeModel *treeModel, D &TreeViewLogic::onChildNoteCountChangedFolder); connect(m_dbManager, &DBManager::childNotesCountUpdatedTag, this, &TreeViewLogic::onChildNotesCountChangedTag); + // 设置应用样式 m_style = new CustomApplicationStyle(); qApp->setStyle(m_style); } +// 更新树视图分隔符 void TreeViewLogic::updateTreeViewSeparator() { m_treeView->setTreeSeparator(m_treeModel->getSeparatorIndex(), m_treeModel->getDefaultNotesIndex()); } +// 加载树模型 void TreeViewLogic::loadTreeModel(const NodeTagTreeData &treeData) { m_treeModel->setTreeData(treeData); + // 获取根节点的子节点数量 { NodeData node; QMetaObject::invokeMethod(m_dbManager, "getChildNotesCountFolder", @@ -101,6 +111,7 @@ void TreeViewLogic::loadTreeModel(const NodeTagTreeData &treeData) m_treeModel->setData(index, node.childNotesCount(), NodeItem::Roles::ChildCount); } } + // 获取垃圾箱节点的子节点数量 { NodeData node; QMetaObject::invokeMethod(m_dbManager, "getChildNotesCountFolder", @@ -111,6 +122,7 @@ void TreeViewLogic::loadTreeModel(const NodeTagTreeData &treeData) m_treeModel->setData(index, node.childNotesCount(), NodeItem::Roles::ChildCount); } } + // 加载保存的状态 if (m_needLoadSavedState) { m_needLoadSavedState = false; m_treeView->reExpandC(m_expandedFolder); @@ -142,6 +154,7 @@ void TreeViewLogic::loadTreeModel(const NodeTagTreeData &treeData) updateTreeViewSeparator(); } +// 处理添加文件夹请求 void TreeViewLogic::onAddFolderRequested(bool fromPlusButton) { QModelIndex currentIndex; @@ -162,7 +175,7 @@ void TreeViewLogic::onAddFolderRequested(bool fromPlusButton) static_cast(currentIndex.data(NodeItem::Roles::ItemType).toInt()); if (type == NodeItem::FolderItem) { parentId = currentIndex.data(NodeItem::Roles::NodeId).toInt(); - // we don't allow subfolder under default notes folder + // 不允许在默认笔记文件夹下创建子文件夹 if (parentId == SpecialNodeID::DefaultNotesFolder) { parentId = SpecialNodeID::RootFolder; } @@ -235,12 +248,13 @@ void TreeViewLogic::onAddFolderRequested(bool fromPlusButton) } } +// 处理添加标签请求 void TreeViewLogic::onAddTagRequested() { int newlyCreatedTagId; TagData newTag; newTag.setName(m_treeModel->getNewTagPlaceholderName()); - // random color generator + // 随机颜色生成器 const double lower_bound = 0; const double upper_bound = 1; static std::uniform_real_distribution unif(lower_bound, upper_bound); @@ -264,6 +278,7 @@ void TreeViewLogic::onAddTagRequested() m_treeModel->appendChildNodeToParent(m_treeModel->rootIndex(), hs); } +// 处理重命名节点请求 void TreeViewLogic::onRenameNodeRequestedFromTreeView(const QModelIndex &index, const QString &newName) { @@ -272,6 +287,7 @@ void TreeViewLogic::onRenameNodeRequestedFromTreeView(const QModelIndex &index, emit requestRenameNodeInDB(id, newName); } +// 处理删除文件夹请求 void TreeViewLogic::onDeleteFolderRequested(const QModelIndex &index) { auto btn = QMessageBox::question(nullptr, "Are you sure you want to delete this folder", @@ -303,6 +319,7 @@ void TreeViewLogic::onDeleteFolderRequested(const QModelIndex &index) } } +// 处理重命名标签请求 void TreeViewLogic::onRenameTagRequestedFromTreeView(const QModelIndex &index, const QString &newName) { @@ -311,6 +328,7 @@ void TreeViewLogic::onRenameTagRequestedFromTreeView(const QModelIndex &index, emit requestRenameTagInDB(id, newName); } +// 处理修改标签颜色请求 void TreeViewLogic::onChangeTagColorRequested(const QModelIndex &index) { if (index.isValid()) { @@ -324,6 +342,7 @@ void TreeViewLogic::onChangeTagColorRequested(const QModelIndex &index) } } +// 处理删除标签请求 void TreeViewLogic::onDeleteTagRequested(const QModelIndex &index) { auto id = index.data(NodeItem::Roles::NodeId).toInt(); @@ -332,6 +351,7 @@ void TreeViewLogic::onDeleteTagRequested(const QModelIndex &index) m_treeView->setCurrentIndexC(m_treeModel->getAllNotesButtonIndex()); } +// 处理标签子节点数量变化 void TreeViewLogic::onChildNotesCountChangedTag(int tagId, int notesCount) { auto index = m_treeModel->tagIndexFromId(tagId); @@ -340,6 +360,7 @@ void TreeViewLogic::onChildNotesCountChangedTag(int tagId, int notesCount) } } +// 处理文件夹子节点数量变化 void TreeViewLogic::onChildNoteCountChangedFolder(int folderId, const QString &absPath, int notesCount) { @@ -356,6 +377,7 @@ void TreeViewLogic::onChildNoteCountChangedFolder(int folderId, const QString &a } } +// 打开文件夹 void TreeViewLogic::openFolder(int id) { NodeData target; @@ -379,6 +401,7 @@ void TreeViewLogic::openFolder(int id) } } +// 处理移动节点请求 void TreeViewLogic::onMoveNodeRequested(int nodeId, int targetId) { NodeData target; @@ -391,6 +414,7 @@ void TreeViewLogic::onMoveNodeRequested(int nodeId, int targetId) emit requestMoveNodeInDB(nodeId, target); } +// 设置主题 void TreeViewLogic::setTheme(Theme::Value theme) { m_treeView->setTheme(theme); @@ -398,6 +422,7 @@ void TreeViewLogic::setTheme(Theme::Value theme) m_style->setTheme(theme); } +// 设置最后保存的状态 void TreeViewLogic::setLastSavedState(bool isLastSelectFolder, const QString &lastSelectFolder, const QSet &lastSelectTag, const QStringList &expandedFolder) diff --git a/source/src/treeviewlogic.h b/source/src/treeviewlogic.h index 4931dc6..fa37b4a 100644 --- a/source/src/treeviewlogic.h +++ b/source/src/treeviewlogic.h @@ -12,47 +12,72 @@ class DBManager; class CustomApplicationStyle; class NoteListView; +// TreeViewLogic 类负责管理树视图逻辑 class TreeViewLogic : public QObject { Q_OBJECT public: - explicit TreeViewLogic(NodeTreeView *treeView, NodeTreeModel *treeModel, DBManager *dbManager, - NoteListView *listView, QObject *parent = nullptr); + // 构造函数 + explicit TreeViewLogic(NodeTreeView* treeView, NodeTreeModel* treeModel, DBManager* dbManager, + NoteListView* listView, QObject* parent = nullptr); + // 打开文件夹 void openFolder(int id); + // 处理移动节点请求 void onMoveNodeRequested(int nodeId, int targetId); + // 设置主题 void setTheme(Theme::Value theme); - void setLastSavedState(bool isLastSelectFolder, const QString &lastSelectFolder, - const QSet &lastSelectTag, const QStringList &expandedFolder); + // 设置最后保存的状态 + void setLastSavedState(bool isLastSelectFolder, const QString& lastSelectFolder, + const QSet& lastSelectTag, const QStringList& expandedFolder); + private slots: + // 更新树视图分隔符 void updateTreeViewSeparator(); - void loadTreeModel(const NodeTagTreeData &treeData); + // 加载树模型 + void loadTreeModel(const NodeTagTreeData& treeData); + // 处理添加标签请求 void onAddTagRequested(); - void onRenameNodeRequestedFromTreeView(const QModelIndex &index, const QString &newName); - void onDeleteFolderRequested(const QModelIndex &index); - void onRenameTagRequestedFromTreeView(const QModelIndex &index, const QString &newName); - void onChangeTagColorRequested(const QModelIndex &index); - void onDeleteTagRequested(const QModelIndex &index); + // 处理重命名节点请求 + void onRenameNodeRequestedFromTreeView(const QModelIndex& index, const QString& newName); + // 处理删除文件夹请求 + void onDeleteFolderRequested(const QModelIndex& index); + // 处理重命名标签请求 + void onRenameTagRequestedFromTreeView(const QModelIndex& index, const QString& newName); + // 处理修改标签颜色请求 + void onChangeTagColorRequested(const QModelIndex& index); + // 处理删除标签请求 + void onDeleteTagRequested(const QModelIndex& index); + // 处理标签子节点数量变化 void onChildNotesCountChangedTag(int tagId, int notesCount); - void onChildNoteCountChangedFolder(int folderId, const QString &absPath, int notesCount); + // 处理文件夹子节点数量变化 + void onChildNoteCountChangedFolder(int folderId, const QString& absPath, int notesCount); signals: - void requestRenameNodeInDB(int id, const QString &newName); - void requestRenameTagInDB(int id, const QString &newName); - void requestChangeTagColorInDB(int id, const QString &newColor); - void requestMoveNodeInDB(int id, const NodeData &target); + // 请求重命名节点 + void requestRenameNodeInDB(int id, const QString& newName); + // 请求重命名标签 + void requestRenameTagInDB(int id, const QString& newName); + // 请求修改标签颜色 + void requestChangeTagColorInDB(int id, const QString& newColor); + // 请求移动节点 + void requestMoveNodeInDB(int id, const NodeData& target); + // 添加笔记到标签 void addNoteToTag(int noteId, int tagId); + // 节点移动 void noteMoved(int nodeId, int targetId); private: + // 处理添加文件夹请求 void onAddFolderRequested(bool fromPlusButton); private: - NodeTreeView *m_treeView; - NodeTreeModel *m_treeModel; - NoteListView *m_listView; - NodeTreeDelegate *m_treeDelegate; - DBManager *m_dbManager; - CustomApplicationStyle *m_style; + // 成员变量 + NodeTreeView* m_treeView; + NodeTreeModel* m_treeModel; + NoteListView* m_listView; + NodeTreeDelegate* m_treeDelegate; + DBManager* m_dbManager; + CustomApplicationStyle* m_style; bool m_needLoadSavedState; bool m_isLastSelectFolder; QString m_lastSelectFolder; @@ -60,4 +85,4 @@ private: QStringList m_expandedFolder; }; -#endif // TREEVIEWLOGIC_H +#endif // TREEVIEWLOGIC_H \ No newline at end of file