zhangshuoshuo

develop
zhangshuoshuo 5 months ago
parent c34f275c6b
commit af2c0069e1

Binary file not shown.

@ -1,6 +1,7 @@
#include "nodedata.h" #include "nodedata.h"
#include <QDataStream> #include <QDataStream>
// NodeData 类的构造函数,初始化成员变量
NodeData::NodeData() NodeData::NodeData()
: m_id{ SpecialNodeID::InvalidNodeId }, : m_id{ SpecialNodeID::InvalidNodeId },
m_isModified(false), m_isModified(false),
@ -14,206 +15,247 @@ NodeData::NodeData()
{ {
} }
// 获取节点ID
int NodeData::id() const int NodeData::id() const
{ {
return m_id; return m_id;
} }
// 设置节点ID
void NodeData::setId(int id) void NodeData::setId(int id)
{ {
m_id = id; m_id = id;
} }
// 获取节点的完整标题
QString NodeData::fullTitle() const QString NodeData::fullTitle() const
{ {
return m_fullTitle; return m_fullTitle;
} }
// 设置节点的完整标题
void NodeData::setFullTitle(const QString &fullTitle) void NodeData::setFullTitle(const QString &fullTitle)
{ {
m_fullTitle = fullTitle; m_fullTitle = fullTitle;
} }
// 获取最后修改时间
QDateTime NodeData::lastModificationdateTime() const QDateTime NodeData::lastModificationdateTime() const
{ {
return m_lastModificationDateTime; return m_lastModificationDateTime;
} }
// 设置最后修改时间
void NodeData::setLastModificationDateTime(const QDateTime &lastModificationdateTime) void NodeData::setLastModificationDateTime(const QDateTime &lastModificationdateTime)
{ {
m_lastModificationDateTime = lastModificationdateTime; m_lastModificationDateTime = lastModificationdateTime;
} }
// 获取节点内容
QString NodeData::content() const QString NodeData::content() const
{ {
return m_content; return m_content;
} }
// 设置节点内容
void NodeData::setContent(const QString &content) void NodeData::setContent(const QString &content)
{ {
m_content = content; m_content = content;
} }
// 检查节点是否被修改过
bool NodeData::isModified() const bool NodeData::isModified() const
{ {
return m_isModified; return m_isModified;
} }
// 设置节点的修改状态
void NodeData::setModified(bool isModified) void NodeData::setModified(bool isModified)
{ {
m_isModified = isModified; m_isModified = isModified;
} }
// 检查节点是否被选中
bool NodeData::isSelected() const bool NodeData::isSelected() const
{ {
return m_isSelected; return m_isSelected;
} }
// 设置节点的选中状态
void NodeData::setSelected(bool isSelected) void NodeData::setSelected(bool isSelected)
{ {
m_isSelected = isSelected; m_isSelected = isSelected;
} }
// 获取滚动条位置
int NodeData::scrollBarPosition() const int NodeData::scrollBarPosition() const
{ {
return m_scrollBarPosition; return m_scrollBarPosition;
} }
// 设置滚动条位置
void NodeData::setScrollBarPosition(int scrollBarPosition) void NodeData::setScrollBarPosition(int scrollBarPosition)
{ {
m_scrollBarPosition = scrollBarPosition; m_scrollBarPosition = scrollBarPosition;
} }
// 获取删除时间
QDateTime NodeData::deletionDateTime() const QDateTime NodeData::deletionDateTime() const
{ {
return m_deletionDateTime; return m_deletionDateTime;
} }
// 设置删除时间
void NodeData::setDeletionDateTime(const QDateTime &deletionDateTime) void NodeData::setDeletionDateTime(const QDateTime &deletionDateTime)
{ {
m_deletionDateTime = deletionDateTime; m_deletionDateTime = deletionDateTime;
} }
// 获取节点类型
NodeData::Type NodeData::nodeType() const NodeData::Type NodeData::nodeType() const
{ {
return m_nodeType; return m_nodeType;
} }
// 设置节点类型
void NodeData::setNodeType(NodeData::Type newNodeType) void NodeData::setNodeType(NodeData::Type newNodeType)
{ {
m_nodeType = newNodeType; m_nodeType = newNodeType;
} }
// 获取父节点ID
int NodeData::parentId() const int NodeData::parentId() const
{ {
return m_parentId; return m_parentId;
} }
// 设置父节点ID
void NodeData::setParentId(int newParentId) void NodeData::setParentId(int newParentId)
{ {
m_parentId = newParentId; m_parentId = newParentId;
} }
// 获取相对位置
int NodeData::relativePosition() const int NodeData::relativePosition() const
{ {
return m_relativePosition; return m_relativePosition;
} }
// 设置相对位置
void NodeData::setRelativePosition(int newRelativePosition) void NodeData::setRelativePosition(int newRelativePosition)
{ {
m_relativePosition = newRelativePosition; m_relativePosition = newRelativePosition;
} }
// 获取绝对路径
const QString &NodeData::absolutePath() const const QString &NodeData::absolutePath() const
{ {
return m_absolutePath; return m_absolutePath;
} }
// 设置绝对路径
void NodeData::setAbsolutePath(const QString &newAbsolutePath) void NodeData::setAbsolutePath(const QString &newAbsolutePath)
{ {
m_absolutePath = newAbsolutePath; m_absolutePath = newAbsolutePath;
} }
// 获取标签ID集
const QSet<int> &NodeData::tagIds() const const QSet<int> &NodeData::tagIds() const
{ {
return m_tagIds; return m_tagIds;
} }
// 设置标签ID集
void NodeData::setTagIds(const QSet<int> &newTagIds) void NodeData::setTagIds(const QSet<int> &newTagIds)
{ {
m_tagIds = newTagIds; m_tagIds = newTagIds;
} }
// 检查是否为临时节点
bool NodeData::isTempNote() const bool NodeData::isTempNote() const
{ {
return m_isTempNote; return m_isTempNote;
} }
// 设置临时节点状态
void NodeData::setIsTempNote(bool newIsTempNote) void NodeData::setIsTempNote(bool newIsTempNote)
{ {
m_isTempNote = newIsTempNote; m_isTempNote = newIsTempNote;
} }
// 获取父节点名称
const QString &NodeData::parentName() const const QString &NodeData::parentName() const
{ {
return m_parentName; return m_parentName;
} }
// 设置父节点名称
void NodeData::setParentName(const QString &newParentName) void NodeData::setParentName(const QString &newParentName)
{ {
m_parentName = newParentName; m_parentName = newParentName;
} }
// 检查是否为固定节点
bool NodeData::isPinnedNote() const bool NodeData::isPinnedNote() const
{ {
return m_isPinnedNote; return m_isPinnedNote;
} }
// 设置固定节点状态
void NodeData::setIsPinnedNote(bool newIsPinnedNote) void NodeData::setIsPinnedNote(bool newIsPinnedNote)
{ {
m_isPinnedNote = newIsPinnedNote; m_isPinnedNote = newIsPinnedNote;
} }
// 获取标签列表滚动条位置
int NodeData::tagListScrollBarPos() const int NodeData::tagListScrollBarPos() const
{ {
return m_tagListScrollBarPos; return m_tagListScrollBarPos;
} }
// 设置标签列表滚动条位置
void NodeData::setTagListScrollBarPos(int newTagListScrollBarPos) void NodeData::setTagListScrollBarPos(int newTagListScrollBarPos)
{ {
m_tagListScrollBarPos = newTagListScrollBarPos; m_tagListScrollBarPos = newTagListScrollBarPos;
} }
// 获取相对位置AN
int NodeData::relativePosAN() const int NodeData::relativePosAN() const
{ {
return m_relativePosAN; return m_relativePosAN;
} }
// 设置相对位置AN
void NodeData::setRelativePosAN(int newRelativePosAN) void NodeData::setRelativePosAN(int newRelativePosAN)
{ {
m_relativePosAN = newRelativePosAN; m_relativePosAN = newRelativePosAN;
} }
// 获取子节点计数
int NodeData::childNotesCount() const int NodeData::childNotesCount() const
{ {
return m_childNotesCount; return m_childNotesCount;
} }
// 设置子节点计数
void NodeData::setChildNotesCount(int newChildCount) void NodeData::setChildNotesCount(int newChildCount)
{ {
m_childNotesCount = newChildCount; m_childNotesCount = newChildCount;
} }
// 获取创建时间
QDateTime NodeData::creationDateTime() const QDateTime NodeData::creationDateTime() const
{ {
return m_creationDateTime; return m_creationDateTime;
} }
// 设置创建时间
void NodeData::setCreationDateTime(const QDateTime &creationDateTime) void NodeData::setCreationDateTime(const QDateTime &creationDateTime)
{ {
m_creationDateTime = creationDateTime; m_creationDateTime = creationDateTime;
} }
// 重载流提取运算符,读取 NodeData 对象
QDataStream &operator>>(QDataStream &stream, NodeData &nodeData) QDataStream &operator>>(QDataStream &stream, NodeData &nodeData)
{ {
int id; int id;
@ -230,6 +272,7 @@ QDataStream &operator>>(QDataStream &stream, NodeData &nodeData)
return stream; return stream;
} }
// 重载流提取运算符,读取 NodeData 指针对象
QDataStream &operator>>(QDataStream &stream, NodeData *&nodeData) QDataStream &operator>>(QDataStream &stream, NodeData *&nodeData)
{ {
nodeData = new NodeData(); nodeData = new NodeData();

@ -14,6 +14,7 @@ enum Value {
}; };
} }
// NodeData类用于表示节点数据
class NodeData class NodeData
{ {
public: public:
@ -21,64 +22,104 @@ public:
enum Type { Note = 0, Folder }; enum Type { Note = 0, Folder };
// 获取节点ID
int id() const; int id() const;
// 设置节点ID
void setId(int id); void setId(int id);
// 获取完整标题
QString fullTitle() const; QString fullTitle() const;
// 设置完整标题
void setFullTitle(const QString &fullTitle); void setFullTitle(const QString &fullTitle);
// 获取最后修改时间
QDateTime lastModificationdateTime() const; QDateTime lastModificationdateTime() const;
// 设置最后修改时间
void setLastModificationDateTime(const QDateTime &lastModificationdateTime); void setLastModificationDateTime(const QDateTime &lastModificationdateTime);
// 获取创建时间
QDateTime creationDateTime() const; QDateTime creationDateTime() const;
// 设置创建时间
void setCreationDateTime(const QDateTime &creationDateTime); void setCreationDateTime(const QDateTime &creationDateTime);
// 获取内容
QString content() const; QString content() const;
// 设置内容
void setContent(const QString &content); void setContent(const QString &content);
// 判断是否被修改
bool isModified() const; bool isModified() const;
// 设置修改状态
void setModified(bool isModified); void setModified(bool isModified);
// 判断是否被选中
bool isSelected() const; bool isSelected() const;
// 设置选中状态
void setSelected(bool isSelected); void setSelected(bool isSelected);
// 获取滚动条位置
int scrollBarPosition() const; int scrollBarPosition() const;
// 设置滚动条位置
void setScrollBarPosition(int scrollBarPosition); void setScrollBarPosition(int scrollBarPosition);
// 获取删除时间
QDateTime deletionDateTime() const; QDateTime deletionDateTime() const;
// 设置删除时间
void setDeletionDateTime(const QDateTime &deletionDateTime); void setDeletionDateTime(const QDateTime &deletionDateTime);
// 获取节点类型
NodeData::Type nodeType() const; NodeData::Type nodeType() const;
// 设置节点类型
void setNodeType(NodeData::Type newNodeType); void setNodeType(NodeData::Type newNodeType);
// 获取父节点ID
int parentId() const; int parentId() const;
// 设置父节点ID
void setParentId(int newParentId); void setParentId(int newParentId);
// 获取相对位置
int relativePosition() const; int relativePosition() const;
// 设置相对位置
void setRelativePosition(int newRelativePosition); void setRelativePosition(int newRelativePosition);
// 获取绝对路径
const QString &absolutePath() const; const QString &absolutePath() const;
// 设置绝对路径
void setAbsolutePath(const QString &newAbsolutePath); void setAbsolutePath(const QString &newAbsolutePath);
// 获取标签ID集合
const QSet<int> &tagIds() const; const QSet<int> &tagIds() const;
// 设置标签ID集合
void setTagIds(const QSet<int> &newTagIds); void setTagIds(const QSet<int> &newTagIds);
// 判断是否为临时笔记
bool isTempNote() const; bool isTempNote() const;
// 设置临时笔记状态
void setIsTempNote(bool newIsTempNote); void setIsTempNote(bool newIsTempNote);
// 获取父节点名称
const QString &parentName() const; const QString &parentName() const;
// 设置父节点名称
void setParentName(const QString &newParentName); void setParentName(const QString &newParentName);
// 判断是否为固定笔记
bool isPinnedNote() const; bool isPinnedNote() const;
// 设置固定笔记状态
void setIsPinnedNote(bool newIsPinnedNote); void setIsPinnedNote(bool newIsPinnedNote);
// 获取标签列表滚动条位置
int tagListScrollBarPos() const; int tagListScrollBarPos() const;
// 设置标签列表滚动条位置
void setTagListScrollBarPos(int newTagListScrollBarPos); void setTagListScrollBarPos(int newTagListScrollBarPos);
// 获取相对位置AN
int relativePosAN() const; int relativePosAN() const;
// 设置相对位置AN
void setRelativePosAN(int newRelativePosAN); void setRelativePosAN(int newRelativePosAN);
// 获取子笔记数量
int childNotesCount() const; int childNotesCount() const;
// 设置子笔记数量
void setChildNotesCount(int newChildCount); void setChildNotesCount(int newChildCount);
private: private:
@ -104,9 +145,12 @@ private:
int m_childNotesCount; int m_childNotesCount;
}; };
// 用于QMetaType的声明
Q_DECLARE_METATYPE(NodeData) Q_DECLARE_METATYPE(NodeData)
// 重载输入流运算符以读取NodeData对象
QDataStream &operator>>(QDataStream &stream, NodeData &nodeData); QDataStream &operator>>(QDataStream &stream, NodeData &nodeData);
// 重载输入流运算符以读取NodeData指针对象
QDataStream &operator>>(QDataStream &stream, NodeData *&nodeData); QDataStream &operator>>(QDataStream &stream, NodeData *&nodeData);
#endif // NODEDATA_H #endif // NODEDATA_H

@ -1,8 +1,10 @@
#include "nodepath.h" #include "nodepath.h"
#include "nodedata.h" #include "nodedata.h"
// NodePath类的构造函数初始化路径
NodePath::NodePath(const QString &path) : m_path(path) { } NodePath::NodePath(const QString &path) : m_path(path) { }
// 分离路径为QStringList
QStringList NodePath::separate() const QStringList NodePath::separate() const
{ {
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
@ -12,11 +14,13 @@ QStringList NodePath::separate() const
#endif #endif
} }
// 获取当前路径
QString NodePath::path() const QString NodePath::path() const
{ {
return m_path; return m_path;
} }
// 获取父路径
NodePath NodePath::parentPath() const NodePath NodePath::parentPath() const
{ {
auto s = separate(); auto s = separate();
@ -24,11 +28,13 @@ NodePath NodePath::parentPath() const
return s.join(PATH_SEPARATOR); return s.join(PATH_SEPARATOR);
} }
// 获取所有笔记文件夹的路径
QString NodePath::getAllNoteFolderPath() QString NodePath::getAllNoteFolderPath()
{ {
return PATH_SEPARATOR + QString::number(SpecialNodeID::RootFolder); return PATH_SEPARATOR + QString::number(SpecialNodeID::RootFolder);
} }
// 获取回收站文件夹的路径
QString NodePath::getTrashFolderPath() QString NodePath::getTrashFolderPath()
{ {
return PATH_SEPARATOR + QString::number(SpecialNodeID::RootFolder) + PATH_SEPARATOR return PATH_SEPARATOR + QString::number(SpecialNodeID::RootFolder) + PATH_SEPARATOR

@ -9,19 +9,30 @@
#define TAG_MIME "application/x-tagnode" #define TAG_MIME "application/x-tagnode"
#define NOTE_MIME "application/x-notenode" #define NOTE_MIME "application/x-notenode"
// NodePath 类用于处理节点路径
class NodePath class NodePath
{ {
public: public:
// 构造函数,初始化节点路径
NodePath(const QString &path); NodePath(const QString &path);
// 将路径分割为字符串列表
QStringList separate() const; QStringList separate() const;
// 获取当前路径
QString path() const; QString path() const;
// 获取父路径
NodePath parentPath() const; NodePath parentPath() const;
// 获取所有笔记文件夹的路径
static QString getAllNoteFolderPath(); static QString getAllNoteFolderPath();
// 获取回收站文件夹的路径
static QString getTrashFolderPath(); static QString getTrashFolderPath();
private: private:
QString m_path; QString m_path; // 存储节点路径
}; };
#endif // NODEPATH_H #endif // NODEPATH_H

@ -1,20 +1,22 @@
#include "singleinstance.h" #include "singleinstance.h"// 包含SingleInstance类的头文件
SingleInstance::SingleInstance(QObject *parent) : QObject(parent) SingleInstance::SingleInstance(QObject *parent) : QObject(parent)// SingleInstance类的构造函数
{ {
// 连接QLocalServer的newConnection信号到一个lambda表达式槽
// 当有新连接时发出newInstance信号
connect(&m_server, &QLocalServer::newConnection, [this]() { emit newInstance(); }); connect(&m_server, &QLocalServer::newConnection, [this]() { emit newInstance(); });
} }
void SingleInstance::listen(const QString &name) void SingleInstance::listen(const QString &name)// 监听指定名称的本地服务器
{ {
m_server.removeServer(name); m_server.removeServer(name);// 移除已存在的同名服务器
m_server.listen(name); m_server.listen(name);// 开始监听指定名称的本地服务器
} }
bool SingleInstance::hasPrevious(const QString &name) bool SingleInstance::hasPrevious(const QString &name)// 检查是否有其他实例正在运行
{ {
QLocalSocket socket; QLocalSocket socket; // 创建一个本地套接字对象
socket.connectToServer(name, QLocalSocket::ReadOnly); socket.connectToServer(name, QLocalSocket::ReadOnly);// 尝试连接到指定名称的服务器
return socket.waitForConnected(); return socket.waitForConnected();// 等待连接结果
} }

@ -1,25 +1,25 @@
#ifndef SINGLEINSTANCE_H #ifndef SINGLEINSTANCE_H
#define SINGLEINSTANCE_H #define SINGLEINSTANCE_H
#include <QObject> #include <QObject> // 包含QObject类的头文件QObject是所有Qt对象的基类
#include <QLocalServer> #include <QLocalServer> // 包含QLocalServer类的头文件用于创建本地服务器
#include <QLocalSocket> #include <QLocalSocket> // 包含QLocalSocket类的头文件用于创建本地套接字
class SingleInstance : public QObject class SingleInstance : public QObject// 定义SingleInstance类继承自QObject
{ {
Q_OBJECT Q_OBJECT// 宏,用于支持信号和槽机制
public: public:
explicit SingleInstance(QObject *parent = 0); explicit SingleInstance(QObject *parent = 0);// 构造函数,允许传入父对象指针,默认为空
void listen(const QString &name); void listen(const QString &name);// 监听指定名称的本地服务器
bool hasPrevious(const QString &name); bool hasPrevious(const QString &name);// 检查是否有其他实例正在运行
signals: signals:
void newInstance(); void newInstance(); // 当有新实例连接时发出的信号
private: private:
QLocalSocket *m_socket; QLocalSocket *m_socket;// 私有成员变量,用于存储本地套接字指针
QLocalServer m_server; QLocalServer m_server;// 私有成员变量,用于存储本地服务器对象
}; };
#endif // SINGLEINSTANCE_H #endif // SINGLEINSTANCE_H

@ -1,79 +1,87 @@
#include "taglistmodel.h" #include "taglistmodel.h"
#include "tagpool.h" #include "tagpool.h"//获取、检查和更新标签数据
#include <QDebug> #include <QDebug>//用于调试输出
#include "nodepath.h" #include "nodepath.h"
// TagListModel类的构造函数
TagListModel::TagListModel(QObject *parent) : QAbstractListModel(parent), m_tagPool{ nullptr } { } TagListModel::TagListModel(QObject *parent) : QAbstractListModel(parent), m_tagPool{ nullptr } { }
// 初始化父类QAbstractListModel并将m_tagPool初始化为nullptr
// 设置标签池的函数
void TagListModel::setTagPool(TagPool *tagPool) void TagListModel::setTagPool(TagPool *tagPool)
{ {
beginResetModel(); beginResetModel(); // 开始重置模型,通知视图模型即将改变
m_tagPool = tagPool; m_tagPool = tagPool; // 设置标签池指针
connect(tagPool, &TagPool::dataReset, this, [this] { connect(tagPool, &TagPool::dataReset, this,
beginResetModel(); [this] { // 连接标签池的dataReset信号到一个lambda表达式槽
updateTagData(); beginResetModel(); // 标签池数据重置时,开始重置模型
endResetModel(); updateTagData(); // 更新标签数据
}); endResetModel(); // 结束重置模型,通知视图模型已改变
endResetModel(); });
endResetModel(); // 结束重置模型
} }
// 设置模型数据的函数
void TagListModel::setModelData(const QSet<int> &data) void TagListModel::setModelData(const QSet<int> &data)
{ {
if (!m_tagPool) { if (!m_tagPool) { // 如果标签池未初始化
qDebug() << __FUNCTION__ << "Tag pool is not init yet"; qDebug() << __FUNCTION__ << "Tag pool is not init yet"; // 输出调试信息
return; return;
} }
beginResetModel(); beginResetModel(); // 开始重置模型
m_ids = data; m_ids = data; // 设置标签ID集合
updateTagData(); updateTagData(); // 更新标签数据
endResetModel(); endResetModel(); // 结束重置模型
} }
// 添加标签的函数
void TagListModel::addTag(int tagId) void TagListModel::addTag(int tagId)
{ {
if (!m_tagPool) { if (!m_tagPool) { // 如果标签池未初始化
qDebug() << __FUNCTION__ << "Tag pool is not init yet"; qDebug() << __FUNCTION__ << "Tag pool is not init yet"; // 输出调试信息
return; return;
} }
if (m_tagPool->contains(tagId)) { if (m_tagPool->contains(tagId)) { // 如果标签池包含该标签ID
beginInsertRows(QModelIndex(), rowCount(), rowCount()); beginInsertRows(QModelIndex(), rowCount(), rowCount()); // 开始插入行,通知视图即将插入新行
m_data.append(m_tagPool->getTag(tagId)); m_data.append(m_tagPool->getTag(tagId)); // 将标签添加到数据向量中
endInsertRows(); endInsertRows(); // 结束插入行,通知视图已插入新行
} else { } else {
qDebug() << __FUNCTION__ << "Tag is not in pool:" << tagId; qDebug() << __FUNCTION__ << "Tag is not in pool:" << tagId; // 输出调试信息
} }
} }
// 重载rowCount函数返回模型的行数
int TagListModel::rowCount(const QModelIndex &parent) const int TagListModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent); // 声明parent参数未使用避免编译器警告
return m_data.count(); return m_data.count(); // 返回标签数据向量的大小
} }
QVariant TagListModel::data(const QModelIndex &index, int role) const QVariant TagListModel::data(const QModelIndex &index, int role) const// 返回指定索引和角色的数据
{ {
if (!index.isValid()) { if (!index.isValid()) { // 如果索引无效
return QVariant(); return QVariant(); // 返回空的QVariant对象
} }
const TagData &tag = m_data[index.row()]; const TagData &tag = m_data[index.row()]; // 获取对应索引行的标签数据
if (role == IdRole) { if (role == IdRole) { // 如果角色是IdRole
return tag.id(); return tag.id(); // 返回标签的ID
} else if (role == NameRole) { } else if (role == NameRole) { // 如果角色是NameRole
return tag.name(); return tag.name(); // 返回标签的名称
} else if (role == ColorRole) { } else if (role == ColorRole) { // 如果角色是ColorRole
return tag.color(); return tag.color(); // 返回标签的颜色
} }
return QVariant(); return QVariant(); // 如果角色不匹配返回空的QVariant对象
} }
void TagListModel::updateTagData()
void TagListModel::updateTagData()// 更新标签数据的函数
{ {
m_data.clear(); m_data.clear(); // 清空标签数据向量
for (const auto &id : qAsConst(m_ids)) { for (const auto &id : qAsConst(m_ids)) { // 遍历标签ID集合
if (m_tagPool->contains(id)) { if (m_tagPool->contains(id)) { // 如果标签池包含该标签ID
m_data.append(m_tagPool->getTag(id)); m_data.append(m_tagPool->getTag(id)); // 将标签添加到数据向量中
} else { } else {
qDebug() << __FUNCTION__ << "Tag is not in pool:" << id; qDebug() << __FUNCTION__ << "Tag is not in pool:" << id; // 输出调试信息
} }
} }
} }

@ -1,28 +1,47 @@
#ifndef TAGLISTMODEL_H #ifndef TAGLISTMODEL_H
#define TAGLISTMODEL_H #define TAGLISTMODEL_H
#include <QAbstractListModel> #include <QAbstractListModel> // 包含QAbstractListModel类的头文件QAbstractListModel是Qt中用于实现自定义列表模型的基类
#include <QSet> #include <QSet> // 包含QSet类的头文件QSet是一个集合容器用于存储不重复的元素
#include "tagdata.h" #include "tagdata.h"
class TagPool; class TagPool;
// 定义TagListModel类继承自QAbstractListModel
class TagListModel : public QAbstractListModel class TagListModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT // 宏,用于支持信号和槽机制
public: public :
enum TagListRoles { IdRole = Qt::UserRole + 1, NameRole, ColorRole }; // 定义角色枚举,用于指定数据模型中的不同角色
enum TagListRoles {
IdRole = Qt::UserRole + 1,
NameRole,
ColorRole
};
// 构造函数,允许传入父对象指针,默认为空
TagListModel(QObject *parent = nullptr); TagListModel(QObject *parent = nullptr);
// 设置标签池的函数接受一个TagPool指针
void setTagPool(TagPool *tagPool); void setTagPool(TagPool *tagPool);
// 设置模型数据的函数接受一个QSet<int>类型的集合
void setModelData(const QSet<int> &data); void setModelData(const QSet<int> &data);
// 添加标签的函数接受一个标签ID
void addTag(int tagId); void addTag(int tagId);
// 重载rowCount函数返回模型的行数
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
// 重载data函数返回指定索引和角色的数据
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
private: private:
TagPool *m_tagPool; TagPool *m_tagPool; // 私有成员变量,用于存储标签池指针
QVector<TagData> m_data; QVector<TagData> m_data; // 私有成员变量,用于存储标签数据的向量
QSet<int> m_ids; QSet<int> m_ids; // 私有成员变量用于存储标签ID的集合
// 更新标签数据的函数
void updateTagData(); void updateTagData();
}; };

@ -1,86 +1,93 @@
#include "taglistview.h" #include "taglistview.h"
#include <QFile> #include <QFile> // 包含QFile类的头文件用于文件操作
#include <QDebug> #include <QDebug> // 包含QDebug类的头文件用于调试输出
#include <QMouseEvent> #include <QMouseEvent> // 包含QMouseEvent类的头文件用于处理鼠标事件
// TagListView类的构造函数
TagListView::TagListView(QWidget *parent) : QListView(parent) TagListView::TagListView(QWidget *parent) : QListView(parent)
{ {
setFlow(QListView::LeftToRight); setFlow(QListView::LeftToRight); // 设置列表项的排列方向为从左到右
setSpacing(3); setSpacing(3); // 设置列表项之间的间距为3像素
setWrapping(true); setWrapping(true); // 设置列表项可以自动换行
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 设置水平滚动条始终不显示
QFile file(":/styles/taglistview.css"); QFile file(":/styles/taglistview.css"); // 创建QFile对象用于读取CSS样式文件
file.open(QFile::ReadOnly); file.open(QFile::ReadOnly); // 打开文件,设置为只读模式
setStyleSheet(file.readAll()); setStyleSheet(file.readAll()); // 读取文件内容,并将其设置为视图的样式表
setTheme(Theme::Light); setTheme(Theme::Light); // 设置视图的主题为浅色主题
} }
// 设置主题的函数
void TagListView::setTheme(Theme::Value theme) void TagListView::setTheme(Theme::Value theme)
{ {
setCSSThemeAndUpdate(this, theme); setCSSThemeAndUpdate(this, theme); // 调用setCSSThemeAndUpdate函数更新视图的CSS样式和主题
} }
// 设置背景颜色的函数
void TagListView::setBackground(const QColor color) void TagListView::setBackground(const QColor color)
{ {
if (m_backgroundColor != color) { if (m_backgroundColor != color) { // 如果传入的颜色与当前背景颜色不同
m_backgroundColor = color; m_backgroundColor = color; // 更新背景颜色
QString ss = QStringLiteral( QString ss = QStringLiteral( // 定义一个字符串用于存储CSS样式
R"(QListView { background: %1; } )" R"(QListView { background: %1; } )" // 设置QListView的背景颜色
R"(QScrollBar::handle:vertical:hover { background: rgb(170, 170, 171); } )" R"(QScrollBar::handle:vertical:hover { background: rgb(170, 170, 171); } )" // 设置垂直滚动条手柄在鼠标悬停时的背景颜色
R"(QScrollBar::handle:vertical:pressed { background: rgb(149, 149, 149); } )" R"(QScrollBar::handle:vertical:pressed { background: rgb(149, 149, 149); } )" // 设置垂直滚动条手柄在按下时的背景颜色
R"(QScrollBar::handle:vertical { border-radius: 4px; background: rgb(188, 188, 188); min-height: 20px; } )" R"(QScrollBar::handle:vertical { border-radius: 4px; background: rgb(188, 188, 188); min-height: 20px; } )" // 设置垂直滚动条手柄的样式
R"(QScrollBar::vertical {border-radius: 4px; width: 8px; color: rgba(255, 255, 255,0);} )" R"(QScrollBar::vertical {border-radius: 4px; width: 8px; color: rgba(255, 255, 255,0);} )" // 设置垂直滚动条的样式
R"(QScrollBar {margin: 0; background: transparent;} )" R"(QScrollBar {margin: 0; background: transparent;} )" // 设置滚动条的样式
R"(QScrollBar:hover { background-color: rgb(217, 217, 217);})" R"(QScrollBar:hover { background-color: rgb(217, 217, 217);})" // 设置滚动条在鼠标悬停时的背景颜色
R"(QScrollBar::add-line:vertical { width:0px; height: 0px; subcontrol-position: bottom; subcontrol-origin: margin; } )" R"(QScrollBar::add-line:vertical { width:0px; height: 0px; subcontrol-position: bottom; subcontrol-origin: margin; } )" // 设置垂直滚动条增加按钮的样式
R"(QScrollBar::sub-line:vertical { width:0px; height: 0px; subcontrol-position: top; subcontrol-origin: margin; })"); R"(QScrollBar::sub-line:vertical { width:0px; height: 0px; subcontrol-position: top; subcontrol-origin: margin; })"); // 设置垂直滚动条减少按钮的样式
setStyleSheet(ss.arg(m_backgroundColor.name())); setStyleSheet(
ss.arg(m_backgroundColor
.name())); // 将背景颜色插入到样式字符串中,并将其设置为视图的样式表
} }
} }
// 重载reset函数
void TagListView::reset() void TagListView::reset()
{ {
QListView::reset(); QListView::reset(); // 调用基类的reset函数
auto sz = sizeHint(); auto sz = sizeHint(); // 获取视图的尺寸提示
if (!model() || model()->rowCount() == 0) { if (!model() || model()->rowCount() == 0) { // 如果视图没有模型或者模型的行数为0
sz.setHeight(0); sz.setHeight(0); // 将尺寸提示的高度设置为0
} else { } else {
auto firstIndex = model()->index(0, 0); auto firstIndex = model()->index(0, 0); // 获取模型的第一个索引
auto lastIndex = model()->index(model()->rowCount() - 1, 0); auto lastIndex = model()->index(model()->rowCount() - 1, 0); // 获取模型的最后一个索引
auto fr = visualRect(firstIndex); auto fr = visualRect(firstIndex); // 获取第一个索引的可视矩形
fr.setBottom(visualRect(lastIndex).bottom()); fr.setBottom(visualRect(lastIndex)
if (fr.height() < 80) { .bottom()); // 将可视矩形的底部设置为最后一个索引的可视矩形的底部
sz.setHeight(fr.height() + 10); if (fr.height() < 80) { // 如果可视矩形的高度小于80像素
sz.setHeight(fr.height() + 10); // 将尺寸提示的高度设置为可视矩形的高度加上10像素
} else { } else {
sz.setHeight(80); sz.setHeight(80); // 否则将尺寸提示的高度设置为80像素
} }
} }
setFixedHeight(sz.height()); setFixedHeight(sz.height()); // 将视图的高度设置为尺寸提示的高度
} }
void TagListView::resizeEvent(QResizeEvent *event) void TagListView::resizeEvent(QResizeEvent *event)
{ {
QListView::resizeEvent(event); QListView::resizeEvent(event); // 调用基类的resizeEvent函数
setWrapping(true); setWrapping(true); // 设置列表项可以自动换行
} }
void TagListView::mousePressEvent(QMouseEvent *event) void TagListView::mousePressEvent(QMouseEvent *event)
{ {
event->ignore(); event->ignore();// 忽略鼠标按下事件
} }
void TagListView::mouseReleaseEvent(QMouseEvent *event) void TagListView::mouseReleaseEvent(QMouseEvent *event)
{ {
event->ignore(); event->ignore();// 忽略鼠标释放事件
} }
void TagListView::mouseDoubleClickEvent(QMouseEvent *event) void TagListView::mouseDoubleClickEvent(QMouseEvent *event)
{ {
event->ignore(); event->ignore();// 忽略鼠标双击事件
} }
void TagListView::mouseMoveEvent(QMouseEvent *event) void TagListView::mouseMoveEvent(QMouseEvent *event)
{ {
event->ignore(); event->ignore();// 忽略鼠标移动事件
} }

@ -1,28 +1,29 @@
#pragma once #pragma once
#include <QListView> #include <QListView>// 包含QListView的定义QListView是Qt框架中的一个列表视图控件
#include "editorsettingsoptions.h" #include "editorsettingsoptions.h"
class TagListView : public QListView class TagListView : public QListView// 定义TagListView类继承自QListView
{ {
Q_OBJECT Q_OBJECT// 宏,用于支持信号和槽机制
public: public:
explicit TagListView(QWidget *parent = nullptr); explicit TagListView(QWidget *parent = nullptr); // 构造函数,允许传入父窗口指针,默认为空
void setTheme(Theme::Value theme); void setTheme(Theme::Value theme); // 设置主题的函数接受Theme枚举类型中的一个值
void setBackground(const QColor color); void setBackground(const QColor color);// 设置背景颜色的方法接受一个QColor对象
signals: signals:
// QAbstractItemView interface // QAbstractItemView interface QAbstractItemView接口的信号声明
public slots: public slots:
virtual void reset() override; virtual void reset() override;// 重载reset函数用于重置视图状态
// QWidget interface // QWidget interface 下面的函数重载自QWidget接口
protected: protected:
virtual void resizeEvent(QResizeEvent *event) override; virtual void resizeEvent(QResizeEvent *event) override;// 重载resizeEvent函数处理窗口大小变化事件
virtual void mousePressEvent(QMouseEvent *event) override; virtual void mousePressEvent(QMouseEvent *event) override;// 重载mousePressEvent函数处理鼠标按下事件
virtual void mouseReleaseEvent(QMouseEvent *event) override; virtual void mouseReleaseEvent(QMouseEvent *event) override;// 重载mouseReleaseEvent函数处理鼠标释放事件
virtual void mouseDoubleClickEvent(QMouseEvent *event) override; virtual void mouseDoubleClickEvent(
virtual void mouseMoveEvent(QMouseEvent *event) override; QMouseEvent *event) override; // 重载mouseDoubleClickEvent函数处理鼠标双击事件
virtual void mouseMoveEvent(QMouseEvent *event) override;// 重载mouseMoveEvent函数处理鼠标移动事件
private: private:
QColor m_backgroundColor; QColor m_backgroundColor;// 私有成员变量,用于存储背景颜色
}; };

Loading…
Cancel
Save