Repository navigation
Expand file tree
/
Copy pathtaskitem.cpp
More file actions
135 lines (108 loc) · 3.44 KB
/
Copy pathtaskitem.cpp
File metadata and controls
135 lines (108 loc) · 3.44 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
#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("border: 0;");
QPushButton::connect(deleteButton, &QPushButton::clicked, this, [=]{
emit remove(item);
});
}
void TaskItem::paintEvent(QPaintEvent* Event)
{
QPainter painter(this);
checkBox->setGeometry(0, height() / 2 - 14, 25, 25);
checkBox->show();//选框
if(checkBox->checkState() == Qt::Checked){
text->setStyleSheet("text-decoration: line-through");
QColor color;
color.setRgb(0x88, 0x88, 0x88);
QPalette pal = text->palette();
pal.setColor(QPalette::Text, color);
text->setPalette(pal);
}
else text->setStyleSheet("text-decoration: none");
text->move(25, 3);
text->show();//待办事项
deleteButton->move(width() - 30, height() / 2 - 12);
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 - 50);
text->adjustSize();
resize(width, text->height() + 5);
}