-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweblist.cpp
141 lines (124 loc) · 3.57 KB
/
weblist.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
#include "weblist.h"
LinkInfo::LinkInfo(QString _name,QString _link):name(_name),link(_link){}
bool operator == (const LinkInfo&x,const LinkInfo&y){
return x.name==y.name && x.link==y.link;
}
WebList::WebList(QWidget *parent) : QWidget(parent)
{
whole_layout = new QVBoxLayout;
menu = new QMenu;
menu->addAction("删除网址");
web_list = new QListWidget;
web_list->setContextMenuPolicy(Qt::CustomContextMenu);
web_list->setWordWrap(true);
but=new QPushButton;
but->setText(tr("新建链接"));
link_file=new QFile("links.txt");
if(link_file->open(QIODevice::ReadOnly|QIODevice::Text)){
QTextStream fin(link_file);
while(!fin.atEnd()){
QString x,y;
fin>>x>>y;
info_list.append(LinkInfo(x,y));
}
link_file->close();
}
refresh();
whole_layout->addWidget(but);
whole_layout->addWidget(web_list);
this->setLayout(whole_layout);
//connect(but,&QPushButton::clicked,this,&WebList::new_link);
connect(but,&QPushButton::clicked,[=](){
QTimer::singleShot(500,this,[=](){
this->WebList::new_link();
});
});
connect(web_list,&QListWidget::customContextMenuRequested,this,&WebList::show_menu);
connect(menu,&QMenu::triggered,this,&WebList::del_link);
connect(web_list,&QListWidget::itemClicked,this,&WebList::open_link);
web_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;\
}\
");
but->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 WebList::refresh()
{
web_list->clear();
for(auto info:info_list){
auto item = new QListWidgetItem(info.name,web_list);
item->setToolTip(info.link);
item->setTextAlignment(Qt::AlignCenter);
}
}
void WebList::new_link(){
bool ok;
QString name=QInputDialog::getText(this,tr("new link"),tr("enter link name:"),QLineEdit::Normal,"",&ok);
if(!ok) return;
QString link=QInputDialog::getText(this,tr("new link"),tr("enter link url:"),QLineEdit::Normal,"",&ok);
if(ok && !name.isEmpty()){
info_list.append(LinkInfo(name,link));
refresh();
}
}
void WebList::open_link(QListWidgetItem* item){
QDesktopServices::openUrl(QUrl(item->toolTip(), QUrl::TolerantMode));
}
void WebList::del_link(){
if(curr_item) info_list.removeAll(LinkInfo(curr_item->text(),curr_item->toolTip()));
refresh();
}
void WebList::show_menu(QPoint pos)
{
curr_item = 0;
QListWidgetItem* item = web_list->itemAt(pos);
if(item)
{
//qDebug() << "1"<<item->text();
curr_item = item;
}
menu->move(this->cursor().pos());
menu->show();
}
WebList::~WebList()
{
qout<<"destruct weblist";
link_file->open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream fout(link_file);
for(auto info:info_list){
fout<<info.name<<' '<<info.link<<'\n';
}
link_file->close();
}