-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtaskitem.cpp
More file actions
194 lines (162 loc) · 5.19 KB
/
Copy pathtaskitem.cpp
File metadata and controls
194 lines (162 loc) · 5.19 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
#include "taskitem.h"
#include <QLabel>
#include <QPaintEvent>
#include <QPainter>
#include <QFontMetrics>
#include <QPainterPath>
TaskItem::TaskItem(QWidget *parent) : QWidget(parent), deleteButton(new QPushButton(this))
{
setAttribute(Qt::WA_StyledBackground);
// 设置删除按钮样式
deleteButton->setIcon(QIcon(":/more/img/delete.png"));
deleteButton->setIconSize(QSize(20, 20));
deleteButton->setStyleSheet(R"(
QPushButton {
border: none;
background: transparent;
}
QPushButton:hover {
background-color: rgba(239, 68, 68, 0.1);
border-radius: 4px;
}
)");
deleteButton->setCursor(Qt::PointingHandCursor);
// 连接删除信号
QPushButton::connect(deleteButton, &QPushButton::clicked, this, [=]{
emit remove(item);
});
}
void TaskItem::paintEvent(QPaintEvent* Event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// 先绘制整体背景
painter.fillRect(rect(), QColor(232, 245, 253)); // 使用淡蓝色填充整个背景
// 绘制卡片背景
QPainterPath path;
path.addRoundedRect(QRectF(0, 0, width(), height()), 10, 10);
painter.fillPath(path, QColor(255, 255, 255));
checkBox->setGeometry(4, height() / 2 - 14, 28, 28); // 调整整体大小为28x28
checkBox->setStyleSheet(R"(
QCheckBox {
spacing: 0px; // 将spacing改为0,去除额外间距
}
QCheckBox::indicator {
width: 24px; // 宽高保持一致
height: 24px; // 宽高保持一致
border: 2px solid #E5E7EB;
border-radius: 4px;
background-color: white;
}
QCheckBox::indicator:checked {
background-color: #3B82F6;
border-color: #3B82F6;
image: url(:/more/img/check.png);
}
QCheckBox::indicator:hover {
border-color: #3B82F6;
}
)");
checkBox->show();//选框
if(checkBox->checkState() == Qt::Checked){
text->setStyleSheet(R"(
QLabel {
color: #9CA3AF;
text-decoration: line-through;
font-family: "Microsoft YaHei UI";
padding: 2px;
}
)");
QColor color;
color.setRgb(0x88, 0x88, 0x88);
QPalette pal = text->palette();
pal.setColor(QPalette::Text, color);
text->setPalette(pal);
}
else {
text->setStyleSheet(R"(
QLabel {
color: #1F2937;
font-family: "Microsoft YaHei UI";
padding: 4px;
}
)");
}
text->move(40, 3);
text->show();//待办事项
deleteButton->move(width() - 35, height() / 2 - 12); // 从30改为35,让按钮往右移一点
deleteButton->show();//删除
painter.drawLine(QPointF(5, height()), QPointF(width() - 5, height()));
QWidget::paintEvent(Event);
}
bool TaskItem::event(QEvent *event)
{
switch (event->type()) {
case QEvent::Enter:
this->setStyleSheet("background-color:#84c4e2;");
break;
case QEvent::Leave:
this->setStyleSheet("background-color:transparent;");
break;
default:
break;
}
return QWidget::event(event);
}
void TaskItem::init(QDateTime &deadline, QString &text, int priority, QListWidgetItem* item)
{
this->item = item;
this->deadline = deadline;
this->priority = priority;
checkBox = new QCheckBox(this);
setText(text);
this->text->setWordWrap(true);
this->text->setAttribute(Qt::WA_TransparentForMouseEvents, true);
}
void TaskItem::init(const QJsonObject& json, QListWidgetItem* item)
{
this->item = item;
deadline = QDateTime::fromString(json["deadline"].toString(), "yyyy-MM-ddThh:mm:ss.zzzZ");
priority = json["priority"].toInt();
id = json["id"].toInt();
checkBox = new QCheckBox(this);
if(json["finish"].toBool()) checkBox->setCheckState(Qt::Checked);
setText(json["content"].toString());
text->setWordWrap(true);
text->setAttribute(Qt::WA_TransparentForMouseEvents, true);
}
void TaskItem::setData(QDateTime &deadline, QString &text, int priority)
{
this->deadline = deadline;
this->priority = priority;
setText(text);
}
QJsonObject TaskItem::toJson()
{
QJsonObject json;
json["deadline"] = deadline.toString("yyyy-MM-ddThh:mm:ss.zzzZ");
json["id"] = id;
json["priority"] = priority;
json["finish"] = (checkBox->checkState() == Qt::Checked);
json["content"] = getText();
return json;
}
void TaskItem::setText(const QString& text)
{
QString prefix = "<font style = 'font-size:10px;'>" +
deadline.toString("yyyy-MM-dd hh:mm") + "截止</font> <br/>";
for(int i = 0; i <= priority; i++)
{
prefix += '!';
}
prefix += ' ';
prefixlen = prefix.length();
if(this->text == nullptr) this->text = new QLabel(prefix + text, this);
else this->text->setText(prefix + text);
}
void TaskItem::setWidth(int width)
{
text->setFixedWidth(width - 70); // 从50改为70,给右边留更多空间
text->adjustSize();
resize(width, text->height() + 5);
}