nodetreedelegate.cpp

nodetreedelegate.h
develop
zhangshuoshuo 5 months ago
parent 798d8d6476
commit 85254c8e29

@ -15,8 +15,9 @@
#include <QFontMetrics> #include <QFontMetrics>
#include "fontloader.h" #include "fontloader.h"
NodeTreeDelegate::NodeTreeDelegate(QTreeView *view, QObject *parent, QListView *listView) NodeTreeDelegate::NodeTreeDelegate(QTreeView *view, QObject *parent, QListView *listView)// 构造函数初始化NodeTreeDelegate
: QStyledItemDelegate{ parent }, : QStyledItemDelegate{ parent },
// 根据平台选择合适的字体
#ifdef __APPLE__ #ifdef __APPLE__
m_displayFont(QFont(QStringLiteral("SF Pro Text")).exactMatch() m_displayFont(QFont(QStringLiteral("SF Pro Text")).exactMatch()
? QStringLiteral("SF Pro Text") ? QStringLiteral("SF Pro Text")
@ -27,6 +28,7 @@ NodeTreeDelegate::NodeTreeDelegate(QTreeView *view, QObject *parent, QListView *
#else #else
m_displayFont(QStringLiteral("Roboto")), m_displayFont(QStringLiteral("Roboto")),
#endif #endif
// 根据平台设置字体大小和样式
#ifdef __APPLE__ #ifdef __APPLE__
m_titleFont(m_displayFont, 13, QFont::DemiBold), m_titleFont(m_displayFont, 13, QFont::DemiBold),
m_titleSelectedFont(m_displayFont, 13), m_titleSelectedFont(m_displayFont, 13),
@ -39,7 +41,7 @@ NodeTreeDelegate::NodeTreeDelegate(QTreeView *view, QObject *parent, QListView *
m_dateFont(m_displayFont, 10), m_dateFont(m_displayFont, 10),
m_separatorFont(m_displayFont, 9, QFont::Normal), m_separatorFont(m_displayFont, 9, QFont::Normal),
m_numberOfNotesFont(m_displayFont, 9, QFont::DemiBold), m_numberOfNotesFont(m_displayFont, 9, QFont::DemiBold),
#endif #endif// 初始化颜色设置
m_titleColor(26, 26, 26), m_titleColor(26, 26, 26),
m_titleSelectedColor(255, 255, 255), m_titleSelectedColor(255, 255, 255),
m_dateColor(132, 132, 132), m_dateColor(132, 132, 132),
@ -57,15 +59,15 @@ NodeTreeDelegate::NodeTreeDelegate(QTreeView *view, QObject *parent, QListView *
m_view(view), m_view(view),
m_listView(listView), m_listView(listView),
m_theme(Theme::Light) m_theme(Theme::Light)
{ {// 构造函数体为空
} }
// 设置主题
void NodeTreeDelegate::setTheme(Theme::Value theme) void NodeTreeDelegate::setTheme(Theme::Value theme)
{ {
emit themeChanged(theme); emit themeChanged(theme); // 发出主题变化信号
m_theme = theme; m_theme = theme; // 更新当前主题
switch (theme) { switch (theme) {
case Theme::Light: { case Theme::Light: {// 设置浅色主题的颜色
m_titleColor = QColor(26, 26, 26); m_titleColor = QColor(26, 26, 26);
m_dateColor = QColor(26, 26, 26); m_dateColor = QColor(26, 26, 26);
m_defaultColor = QColor(247, 247, 247); m_defaultColor = QColor(247, 247, 247);
@ -76,7 +78,7 @@ void NodeTreeDelegate::setTheme(Theme::Value theme)
m_numberOfNotesColor = QColor(26, 26, 26, 127); m_numberOfNotesColor = QColor(26, 26, 26, 127);
break; break;
} }
case Theme::Dark: { case Theme::Dark: {// 设置深色主题的颜色
m_titleColor = QColor(212, 212, 212); m_titleColor = QColor(212, 212, 212);
m_dateColor = QColor(212, 212, 212); m_dateColor = QColor(212, 212, 212);
m_defaultColor = QColor(25, 25, 25); m_defaultColor = QColor(25, 25, 25);
@ -87,7 +89,7 @@ void NodeTreeDelegate::setTheme(Theme::Value theme)
m_numberOfNotesColor = QColor(212, 212, 212, 127); m_numberOfNotesColor = QColor(212, 212, 212, 127);
break; break;
} }
case Theme::Sepia: { case Theme::Sepia: {// 设置棕褐色主题的颜色
m_titleColor = QColor(26, 26, 26); m_titleColor = QColor(26, 26, 26);
m_dateColor = QColor(26, 26, 26); m_dateColor = QColor(26, 26, 26);
m_defaultColor = QColor(251, 240, 217); m_defaultColor = QColor(251, 240, 217);
@ -100,27 +102,28 @@ void NodeTreeDelegate::setTheme(Theme::Value theme)
} }
} }
} }
// 绘制函数
void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);// 开启抗锯齿
auto itemType = static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt()); auto itemType = static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt());
// 根据平台设置图标字体大小偏移
#ifdef __APPLE__ #ifdef __APPLE__
int iconPointSizeOffset = 0; int iconPointSizeOffset = 0;
#else #else
int iconPointSizeOffset = -4; int iconPointSizeOffset = -4;
#endif #endif
painter->fillRect(option.rect, m_currentBackgroundColor); painter->fillRect(option.rect, m_currentBackgroundColor);// 填充背景色
switch (itemType) { switch (itemType) {
// 绘制逻辑根据不同的项类型进行分支处理
case NodeItem::Type::RootItem: { case NodeItem::Type::RootItem: {
break; break;
} }
case NodeItem::Type::AllNoteButton: case NodeItem::Type::AllNoteButton:
case NodeItem::Type::TrashButton: { case NodeItem::Type::TrashButton: {
paintBackgroundSelectable(painter, option, index); paintBackgroundSelectable(painter, option, index);// 绘制可选择背景
auto iconRect = QRect(option.rect.x() + 22, auto iconRect = QRect(option.rect.x() + 22,
option.rect.y() + (option.rect.height() - 20) / 2, 18, 20); option.rect.y() + (option.rect.height() - 20) / 2, 18, 20);
QFont previousPainterFont = painter->font(); QFont previousPainterFont = painter->font();
@ -276,7 +279,7 @@ void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
break; break;
} }
case NodeItem::Type::TagItem: { case NodeItem::Type::TagItem: {
paintBackgroundSelectable(painter, option, index); paintBackgroundSelectable(painter, option, index);// 绘制可选择的背景
auto iconRect = QRect(option.rect.x() + 22, auto iconRect = QRect(option.rect.x() + 22,
option.rect.y() + (option.rect.height() - 14) / 2, 16, 16); option.rect.y() + (option.rect.height() - 14) / 2, 16, 16);
auto tagColor = index.data(NodeItem::Roles::TagColor).toString(); auto tagColor = index.data(NodeItem::Roles::TagColor).toString();
@ -315,45 +318,45 @@ void NodeTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
} }
} }
} }
// 绘制可选择的背景
void NodeTreeDelegate::paintBackgroundSelectable(QPainter *painter, void NodeTreeDelegate::paintBackgroundSelectable(QPainter *painter,
const QStyleOptionViewItem &option, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {
if ((option.state & QStyle::State_Selected) == QStyle::State_Selected) { if ((option.state & QStyle::State_Selected) == QStyle::State_Selected) {
painter->fillRect(option.rect, QBrush(m_ActiveColor)); painter->fillRect(option.rect, QBrush(m_ActiveColor));// 如果选中,填充活动颜色
} else if ((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver) { } else if ((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver) {
auto treeView = dynamic_cast<NodeTreeView *>(m_view); auto treeView = dynamic_cast<NodeTreeView *>(m_view);
auto itemType = static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt()); auto itemType = static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt());
if (itemType == NodeItem::Type::TrashButton) { if (itemType == NodeItem::Type::TrashButton) {
return; return;// 垃圾桶按钮不绘制悬停背景
} }
if (!treeView->isDragging()) { if (!treeView->isDragging()) {
painter->fillRect(option.rect, QBrush(m_hoverColor)); painter->fillRect(option.rect, QBrush(m_hoverColor));// 如果悬停,填充悬停颜色
} }
} }
} }
// 计算项的大小提示
QSize NodeTreeDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const QSize NodeTreeDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
QSize result = QStyledItemDelegate::sizeHint(option, index); QSize result = QStyledItemDelegate::sizeHint(option, index);
auto itemType = static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt()); auto itemType = static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt());
if (itemType == NodeItem::Type::FolderSeparator) { if (itemType == NodeItem::Type::FolderSeparator) {
result.setHeight(NoteTreeConstant::folderLabelHeight); result.setHeight(NoteTreeConstant::folderLabelHeight);// 文件夹分隔符的高度
} else if (itemType == NodeItem::Type::TagSeparator) { } else if (itemType == NodeItem::Type::TagSeparator) {
result.setHeight(NoteTreeConstant::tagLabelHeight); result.setHeight(NoteTreeConstant::tagLabelHeight);// 标签分隔符的高度
} else if (itemType == NodeItem::Type::FolderItem || itemType == NodeItem::Type::TrashButton } else if (itemType == NodeItem::Type::FolderItem || itemType == NodeItem::Type::TrashButton
|| itemType == NodeItem::Type::AllNoteButton) { || itemType == NodeItem::Type::AllNoteButton) {
result.setHeight(NoteTreeConstant::folderItemHeight); result.setHeight(NoteTreeConstant::folderItemHeight);// 文件夹项、垃圾桶按钮、所有笔记按钮的高度
} else if (itemType == NodeItem::Type::TagItem) { } else if (itemType == NodeItem::Type::TagItem) {
result.setHeight(NoteTreeConstant::tagItemHeight); result.setHeight(NoteTreeConstant::tagItemHeight);// 标签项的高度
} else { } else {
result.setHeight(30); result.setHeight(30);
} }
return result; return result;
} }
// 创建编辑器
QWidget *NodeTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, QWidget *NodeTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {
@ -445,7 +448,7 @@ QWidget *NodeTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
} }
return nullptr; return nullptr;
} }
// 更新编辑器的几何形状
void NodeTreeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, void NodeTreeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {

@ -7,56 +7,80 @@
class QTreeView; class QTreeView;
class QListView; class QListView;
class NodeTreeDelegate : public QStyledItemDelegate class NodeTreeDelegate : public QStyledItemDelegate// NodeTreeDelegate 类继承自 QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT// Qt 元对象宏,用于信号和槽机制
public: public:// 构造函数,初始化 NodeTreeDelegate
explicit NodeTreeDelegate(QTreeView *view, QObject *parent = nullptr, explicit NodeTreeDelegate(QTreeView *view, QObject *parent = nullptr,
QListView *listView = nullptr); QListView *listView = nullptr);
void setTheme(Theme::Value theme); void setTheme(Theme::Value theme);// 设置主题
signals: signals:
void addFolderRequested(); void addFolderRequested();// 信号:请求添加文件夹
void addTagRequested(); void addTagRequested();// 信号:请求添加标签
void themeChanged(Theme::Value theme); void themeChanged(Theme::Value theme);// 信号:主题变化
// QAbstractItemDelegate interface // QAbstractItemDelegate interface
// 实现 QAbstractItemDelegate 接口
public: public:
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,// 绘制项
const QModelIndex &index) const override; const QModelIndex &index) const override;
virtual QSize sizeHint(const QStyleOptionViewItem &option, virtual QSize sizeHint(const QStyleOptionViewItem &option,// 计算项的大小提示
const QModelIndex &index) const override; const QModelIndex &index) const override;
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override; const QModelIndex &index) const override;// 创建编辑器
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const override; const QModelIndex &index) const override;// 更新编辑器的几何形状
private: private:
void paintBackgroundSelectable(QPainter *painter, const QStyleOptionViewItem &option, void paintBackgroundSelectable(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const; const QModelIndex &index) const;// 绘制可选择的背景
private: private:
// 显示字体名称
QString m_displayFont; QString m_displayFont;
// 标题字体
QFont m_titleFont; QFont m_titleFont;
// 选中标题字体
QFont m_titleSelectedFont; QFont m_titleSelectedFont;
// 日期字体
QFont m_dateFont; QFont m_dateFont;
// 分隔符字体
QFont m_separatorFont; QFont m_separatorFont;
// 笔记数量字体
QFont m_numberOfNotesFont; QFont m_numberOfNotesFont;
// 标题颜色
QColor m_titleColor; QColor m_titleColor;
// 选中标题颜色
QColor m_titleSelectedColor; QColor m_titleSelectedColor;
// 日期颜色
QColor m_dateColor; QColor m_dateColor;
// 活动颜色
QColor m_ActiveColor; QColor m_ActiveColor;
// 非活动颜色
QColor m_notActiveColor; QColor m_notActiveColor;
// 悬停颜色
QColor m_hoverColor; QColor m_hoverColor;
// 应用程序非活动颜色
QColor m_applicationInactiveColor; QColor m_applicationInactiveColor;
// 分隔符颜色
QColor m_separatorColor; QColor m_separatorColor;
// 默认颜色
QColor m_defaultColor; QColor m_defaultColor;
// 分隔符文本颜色
QColor m_separatorTextColor; QColor m_separatorTextColor;
// 当前背景颜色
QColor m_currentBackgroundColor; QColor m_currentBackgroundColor;
// 笔记数量颜色
QColor m_numberOfNotesColor; QColor m_numberOfNotesColor;
// 选中笔记数量颜色
QColor m_numberOfNotesSelectedColor; QColor m_numberOfNotesSelectedColor;
// 文件夹图标颜色
QColor m_folderIconColor; QColor m_folderIconColor;
// 树视图指针
QTreeView *m_view; QTreeView *m_view;
// 列表视图指针
QListView *m_listView; QListView *m_listView;
// 主题值
Theme::Value m_theme; Theme::Value m_theme;
}; };

Loading…
Cancel
Save