-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotelistwidget.cpp
More file actions
246 lines (208 loc) · 6.92 KB
/
notelistwidget.cpp
File metadata and controls
246 lines (208 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "notelistwidget.h"
#include "ui_notelistwidget.h"
#include <QDateTime>
#include <QIcon>
#include <QFont>
#include <QBrush>
#include <QTextDocument>
#include <QColor>
#include <QStringList>
#include <QHBoxLayout>
#include <QLabel>
#include <QAction>
NoteListWidget::NoteListWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::NoteListWidget),
m_database(new NoteDatabase(this)),
m_searchTimer(new QTimer(this))
{
ui->setupUi(this);
// 设置列表项显示两行
ui->noteListWidget->setItemDelegate(new NoteItemDelegate(this));
// 设置列表控件样式 - 透明背景,更好地展示卡片效果
ui->noteListWidget->setStyleSheet(
"QListWidget { "
" background-color: #F5F5F5; "
" border: none; "
" outline: none; "
" padding: 5px; "
"}"
"QListWidget::item { "
" background-color: transparent; "
" border: none; "
" margin: 3px 0px; "
"}"
"QListWidget::item:selected { "
" background-color: transparent; "
" border: none; "
"}"
// 添加滚动条样式
"QScrollBar:vertical {"
" border: none;"
" background: #F5F5F5;"
" width: 8px;"
" margin: 0px 0px 0px 0px;"
"}"
"QScrollBar::handle:vertical {"
" background: #BDBDBD;"
" border-radius: 4px;"
" min-height: 20px;"
"}"
"QScrollBar::handle:vertical:hover {"
" background: #9E9E9E;"
"}"
"QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
" height: 0px;"
"}"
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
" background: none;"
"}"
);
// 设置搜索框样式
ui->searchLineEdit->setStyleSheet(
"QLineEdit { "
" background-color: #FFFFFF; "
" border: 1px solid #E0E0E0; "
" border-radius: 15px; "
" padding: 5px 10px; "
" margin: 5px; "
"}"
);
// 设置添加按钮样式
ui->addButton->setStyleSheet(
"QPushButton { "
" background-color: #2196F3; "
" color: white; "
" border: none; "
" border-radius: 15px; "
" font-weight: bold; "
" font-size: 16px; "
"}"
"QPushButton:hover { background-color: #1E88E5; }"
"QPushButton:pressed { background-color: #1976D2; }"
);
// 给搜索框添加搜索图标
QAction *searchAction = new QAction(this);
searchAction->setIcon(QIcon::fromTheme("edit-find", QIcon(":/icons/search.png")));
ui->searchLineEdit->addAction(searchAction, QLineEdit::LeadingPosition);
// 设置搜索延迟
m_searchTimer->setSingleShot(true);
m_searchTimer->setInterval(300); // 300ms延迟
// 设置主窗口样式
setStyleSheet("QWidget { background-color: #F5F5F5; }");
// 连接信号和槽
connect(ui->addButton, &QPushButton::clicked, this, &NoteListWidget::onAddButtonClicked);
connect(ui->noteListWidget, &QListWidget::itemClicked, this, &NoteListWidget::onNoteItemClicked);
connect(ui->searchLineEdit, &QLineEdit::textChanged, this, &NoteListWidget::onSearchTextChanged);
connect(m_searchTimer, &QTimer::timeout, this, &NoteListWidget::performSearch);
// 打开数据库
if (!m_database->open()) {
// 处理数据库打开失败
ui->emptyTextLabel->setText("无法打开数据库");
}
// 初始化界面
refreshNoteList();
}
NoteListWidget::~NoteListWidget()
{
delete ui;
}
void NoteListWidget::refreshNoteList()
{
ui->noteListWidget->clear();
QList<Note> notes = m_database->getAllNotes();
for (const Note ¬e : notes) {
addNoteToList(note);
}
updateEmptyStateVisibility();
}
Note NoteListWidget::getCurrentNote() const
{
QListWidgetItem *currentItem = ui->noteListWidget->currentItem();
if (currentItem) {
return Note::fromVariant(currentItem->data(Qt::UserRole));
}
return Note();
}
void NoteListWidget::onAddButtonClicked()
{
emit createNewNote();
}
void NoteListWidget::onNoteItemClicked(QListWidgetItem *item)
{
if (item) {
Note note = Note::fromVariant(item->data(Qt::UserRole));
emit noteSelected(note);
}
}
void NoteListWidget::onSearchTextChanged(const QString &text)
{
m_lastSearchText = text;
m_searchTimer->start(); // 启动搜索延迟计时器
}
void NoteListWidget::performSearch()
{
ui->noteListWidget->clear();
if (m_lastSearchText.isEmpty()) {
refreshNoteList();
return;
}
QList<Note> notes = m_database->searchNotes(m_lastSearchText);
for (const Note ¬e : notes) {
addNoteToList(note);
}
updateEmptyStateVisibility();
}
void NoteListWidget::addNoteToList(const Note ¬e)
{
QListWidgetItem *item = new QListWidgetItem(ui->noteListWidget);
// 设置显示内容 - 使用纯文本,不使用HTML
// 对标题进行处理:如果内容为HTML则提取纯文本,限制长度,防止过长
QString content = note.content();
QString title = note.title();
// 如果标题为空,尝试从内容中提取第一行作为标题
if (title.isEmpty()) {
// 如果内容是HTML,尝试提取纯文本
if (content.contains("<html>") || content.contains("<body>")) {
QTextDocument doc;
doc.setHtml(content);
content = doc.toPlainText();
}
// 使用内容的第一行作为标题
title = content.split("\n").first().trimmed();
// 如果标题仍然为空,使用"无标题"
if (title.isEmpty()) {
title = "无标题";
}
}
// 限制标题长度
if (title.length() > 30) {
title = title.left(30) + "...";
}
// 设置时间格式
QString timeStr = note.updateTime().toString("MM-dd HH:mm");
// 设置纯文本标题
item->setText(title);
// 设置时间为子项
item->setData(Qt::UserRole + 1, timeStr);
item->setData(Qt::UserRole, note.toVariant());
// 设置项目高度 - 增加高度以容纳卡片和阴影
item->setSizeHint(QSize(ui->noteListWidget->width(), 75));
ui->noteListWidget->addItem(item);
}
void NoteListWidget::updateEmptyStateVisibility()
{
bool isEmpty = ui->noteListWidget->count() == 0;
if (isEmpty) {
// 如果列表为空,根据搜索状态显示不同的提示
if (!m_lastSearchText.isEmpty()) {
// 搜索无结果
ui->emptyTextLabel->setText("没有找到匹配的便签\n尝试使用其他关键词");
} else {
// 没有便签
ui->emptyTextLabel->setText("点击上方的\"+\"按钮\n创建您的第一个便签");
}
}
ui->emptyStateWidget->setVisible(isEmpty);
ui->noteListWidget->setVisible(!isEmpty);
}