-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoteswidget.cpp
159 lines (144 loc) · 4.27 KB
/
noteswidget.cpp
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
#include "noteswidget.h"
NotesWidget::NotesWidget(QWidget *parent) : QWidget(parent)
{
QVBoxLayout* whole_layout = new QVBoxLayout;
addnotes_button = new QPushButton;
addnotes_button->setText("+添加便签");
menu = new QMenu;
menu->addAction("删除便签");
menu->addAction("置顶");
notes_list = new QListWidget;
notes_list->setContextMenuPolicy(Qt::CustomContextMenu);
init_list();
whole_layout->addWidget(addnotes_button);
whole_layout->addWidget(notes_list);
whole_layout->addStretch(1);
this->setLayout(whole_layout);
//connect(addnotes_button,&QPushButton::clicked,this,&NotesWidget::add_note);
connect(addnotes_button,&QPushButton::clicked,[=](){
QTimer::singleShot(500,this,[=](){
this->NotesWidget::add_note();
});
});
connect(notes_list,&QListWidget::customContextMenuRequested,this,&NotesWidget::show_menu);//右键单击
connect(menu,&QMenu::triggered,this,&NotesWidget::action_reflect);
notes_list->setStyleSheet("QListWidget{\
outline:none;\
border:none;\
font-family:KaiTi;\
}\
QListWidget::item{\
padding-top:24px;\
color:rgba(121,112,52,1);\
margin-left:40px;\
}\
QListWidget::item:hover{\
background-color:transparent;\
border-bottom:1px solid rgb(121,112,52);\
}\
QListWidget::item:selected{\
border-bottom:1px solid rgb(121,112,52);\
padding:0px;\
margin:0px;\
color:red;\
}\
QListWidget::item:selected:!active{\
border-width:0px;\
}\
");
addnotes_button->setStyleSheet("QPushButton{\
border:2px groove gray;\
border-radius:10px;\
padding:2px 4px;\
border-style:inset;\
color:rgb(255,235,205);\
}\
QPushButton:pressed{\
background-color:rgb(200,200,200);\
border-style:inset;\
color:rgb(121,112,52);\
}\
");
}
void NotesWidget::get_notes()
{
notes.clear();
notes = cfm.filter_file([&](CourseFile file){return file.get_type() == "sticky";});
auto cmp = [&](const CourseFile& a , const CourseFile& b){
return a.get_prior() <b.get_prior();};
std::sort(notes.begin() , notes.end() , cmp);
for(int i = 0 ; i < notes.size() ; i ++)
{
auto filt = [&](CourseFile file){
return file.get_type() == "sticky" && file.get_name() == notes[i].get_name();};
auto func = [=](CourseFile file){
file.set_prior(i);
return file;};
cfm.transform_file(func,filt);
}
}
void NotesWidget::init_list()
{
get_notes();
notes_list->clear();
for(auto note:notes)
{
QListWidgetItem* note_item = new QListWidgetItem;
note_item->setText("✍ " + note.get_name());
notes_list->setWordWrap(true);
note_item->setTextAlignment(Qt::AlignCenter);
notes_list->addItem(note_item);
}
}
void NotesWidget::show_menu(QPoint pos)
{
curr_item = 0;
QListWidgetItem* item = notes_list->itemAt(pos);
if(item)
{
curr_item = item;
}
menu->move(this->cursor().pos());
menu->show();
}
void NotesWidget::add_note()
{
bool ok;
QString name = QInputDialog::getText(this, tr("新建便签"),"输入便签内容", QLineEdit::Normal,"程设大作业ddl...", &ok);
if(ok && !name.isEmpty())
{
string type("sticky");
int priority = notes.size();
CourseFile cf("whatever",type,name.toStdString(),priority);
cfm.new_file(cf);
init_list();
qDebug() << (notes.end()-1)->get_name();
}
}
void NotesWidget::action_reflect(QAction* action)
{
if(!curr_item) return;
QString name = curr_item->text().split(' ')[1];
qDebug() <<name;
CourseFile note = cfm.filter_file([&](CourseFile file){
return file.get_name() == name && file.get_type() == "sticky";})[0];
qDebug() <<"OK?";
if(action->text() == "删除便签")
{
cfm.erase_file(note);
init_list();
}
else if(action->text() == "置顶")
{
auto filt = [&](CourseFile file){
return file.get_type() == "sticky" && file.get_name() == note.get_name();};
auto func = [=](CourseFile file){
file.set_prior(-1);
return file;};
cfm.transform_file(func,filt);
init_list();
}
}
NotesWidget::~NotesWidget()
{
}