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