-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileList.cpp
More file actions
282 lines (252 loc) · 8.8 KB
/
Copy pathFileList.cpp
File metadata and controls
282 lines (252 loc) · 8.8 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "FileList.h"
#include<QMessageBox>
#include<QFileDialog>
#include<QTextStream>
#include<QDebug>
#include<QToolTip>
FileList::FileList(QTreeWidget *parent) : QTreeWidget(parent)
{
QFont fileListFont("Microsoft YaHei", 10, 50);
this->setHeaderHidden(true);
this->setMouseTracking(true);
connect(this,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(OnFileChanged(QTreeWidgetItem*,int)));
connect(this,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(DoubleClickedItem(QTreeWidgetItem*,int)));
connect(this,SIGNAL(itemEntered(QTreeWidgetItem*,int)),this,SLOT(ShowPath(QTreeWidgetItem*,int)));
this->setFont(fileListFont);
}
bool FileList::MaybeSave(){
if(!curIsModified)//没有做出修改
return true;
//已做出修改,选择是否保存
const QMessageBox::StandardButton mesgbutton
=QMessageBox::warning(this,tr("Warning"),tr("Not saved yet"),
QMessageBox::Save|QMessageBox::Cancel);
switch (mesgbutton) {
case QMessageBox::Save:
return Save();//保存
break;
case QMessageBox::Cancel://不保存
return true;
default:
break;
}
return false;
}
bool FileList::loadFile(const QString &filename){
QFile file(filename);
if(!file.open(QFile::ReadOnly|QFile::Text)){//打开文件失败
QMessageBox::warning(this,tr("Error"),tr("Open File failed"));
return false;
}
QTextStream streamin(&file);
emit(LoadData(streamin.readAll()));//向编辑区发送从文件中获取的内容
setCurrentFile(filename);//重置当前文件信息
return true;
}
bool FileList::OpenFile(){
if(MaybeSave()){//打开文件前先判断是否保存之前文件内容
QString path=QFileDialog::getOpenFileName(this,tr("打开文件"),tr(""),tr("C source files(*.c)"));
if(!path.isEmpty())//已选择文件
return loadFile(path);
}
return false;
}
bool FileList::openStartFile(){
QString path=QFileDialog::getOpenFileName(this,tr("打开文件"),tr(""),tr("C source files(*.c)"));
if(!path.isEmpty())//已选择文件
return loadFile(path);
return false;
}
bool FileList::Save(){
if(currentFile.isEmpty()){//新建文件,先另存
return SaveAs();
}else{
return SaveFile(currentFile);
}
}
bool FileList::SaveFile(const QString &filename){
QFile file(filename);
if(!file.open(QFile::WriteOnly|QFile::Text)){//保存失败
QMessageBox::warning(this,tr("Error"),tr("Save File failed"));
return false;
}
QTextStream out(&file);
emit(GetText());//从编辑区获取文本
out<<curPlainText;
setCurrentFile(filename);//重置当前编辑文件状态
return true;
}
bool FileList::NewFile(){
if(MaybeSave()){//新建文件前先保存
emit(ClearText());//发送清空编辑区信号
QString path=QFileDialog::getSaveFileName(this,tr("新建文件"),tr(""),tr("C source files(*.c)"));
if(!path.isEmpty()) return SaveFile(path);
}
return false;
}
bool FileList::newStartFile()
{
emit(ClearText());//发送清空编辑区信号
QString path=QFileDialog::getSaveFileName(this,tr("新建文件"),tr(""),tr("C source files(*.c)"));
if(!path.isEmpty()) return SaveFile(path);
return false;
}
bool FileList::SaveAs(){
QTreeWidgetItem * curItem;
if(!currentFile.isEmpty()) curItem=files.value(currentFile);
QString path=QFileDialog::getSaveFileName(this,tr("另存文件"),tr(""),tr("C source files(*.c)"));
bool flag;
if(!path.isEmpty()) {
flag=SaveFile(path);
}
else flag=false;
if(flag){//未选择另存文件或另存失败
if(curItem!=NULL){
QTreeWidgetItem *parentItem=curItem->parent();
files.remove(files.key(curItem));
this->removeItemWidget(curItem,1);
delete curItem;
int chiladNum=parentItem->childCount();
if(chiladNum==0){
dirs.remove(dirs.key(parentItem));
this->removeItemWidget(parentItem,1);
delete parentItem;
}
}
return true;
}
else return false;
}
void FileList::setCurrentFile(const QString &filename){
currentFile= filename;
curIsModified=false;//设置为未修改过
if(currentFile.isEmpty())
shownName="未命名";
else shownName=QFileInfo(currentFile).fileName();
QString dirPath=QFileInfo(currentFile).absolutePath();
QString dirName=QFileInfo(dirPath).fileName();
if(!files.contains(filename)){
if(!dirs.contains(dirPath)){
QTreeWidgetItem * dirItem=new QTreeWidgetItem(this);
dirItem->setText(0,dirName);
dirs.insert(dirPath,dirItem);
}
QTreeWidgetItem * fileItem=new QTreeWidgetItem(dirs.value(dirPath));
fileItem->setText(0,shownName);
files.insert(filename,fileItem);
this->setCurrentItem(fileItem);
}else{
QTreeWidgetItem * nowItem=files.value(currentFile);
this->setCurrentItem(nowItem);
}
emit(SetTitle(shownName));
}
bool FileList::OpenDirectory(){
QString dirPath=QFileDialog::getExistingDirectory();
if(dirPath.isEmpty()) return false;
QTreeWidgetItem * dirItem;
if(!dirs.contains(dirPath)){
dirItem=new QTreeWidgetItem(this);
dirItem->setText(0,QFileInfo(dirPath).fileName());
dirs.insert(dirPath,dirItem);
}
QDir dir(dirPath);
//遍历文件夹
//列出dir(path)目录文件下所有文件和目录信息,存储到file_list容器
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
//进行筛选
foreach (QFileInfo fileinfo, fileInfoList)
{
QString filter=fileinfo.suffix();
if(filter!="c")
{
continue;
}
QString filePath=fileinfo.absoluteFilePath();
QString fileName=fileinfo.fileName();
if(!files.contains(filePath)){
QTreeWidgetItem * fileItem=new QTreeWidgetItem(dirs.value(dirPath));
fileItem->setText(0,fileName);
files.insert(filePath,fileItem);
}
}
}
void FileList::OnFileChanged(QTreeWidgetItem *Item,int colum){
QTreeWidgetItem * preItem;
if(!currentFile.isEmpty()) {
preItem=files.value(currentFile);
}
if(!functions.key(Item).isEmpty()){//点击到函数名,发送所点所点函数名信号
QString funstr=functions.key(Item);
emit Sendfunstr(funstr);
return;
}
QString after=files.key(Item);
QString dir;
if(!after.isEmpty()){
if(!currentFile.isEmpty()){
Save();
if(preItem->childCount()!=0) qDeleteAll(preItem->takeChildren());
}
emit setedit();
loadFile(after);
emit showFun();
}
else {
// if(!currentFile.isEmpty()) setCurrentItem(preItem,1);
// dir=dirs.key(Item);
// if(!dir.isEmpty()){
// if(Item->isExpanded()) Item->setExpanded(false);
// else Item->setExpanded(true);
// }
}
}
void FileList::ShowPath(QTreeWidgetItem *item, int column){
QString path=files.key(item);
if(path.isEmpty()) path=dirs.key(item);
if(!path.isEmpty()){
QToolTip::showText(QCursor::pos(),path);
}
}
void FileList::DoubleClickedItem(QTreeWidgetItem *Item, int column){
}
void FileList::Close(){//关闭当前文件
if(MaybeSave()){
QTreeWidgetItem * curItem=files.value(currentFile);
files.remove(currentFile);
QTreeWidgetItem * parentItem=curItem->parent();
this->removeItemWidget(curItem,1);
delete curItem;
int chiladNum=parentItem->childCount();
if(chiladNum==0){
dirs.remove(dirs.key(parentItem));
this->removeItemWidget(parentItem,1);
delete parentItem;
}
if(dirs.size()==0&&files.size()==0){
}else{
this->setCurrentItem(files.begin().value());
loadFile(files.begin().key());
}
}
}
void FileList::SetCurrentFilemodified(){//编辑区做出修改
curIsModified=true;
}
void FileList::GetPlainText(QString Text){//从编辑区收到文本内容
curPlainText=Text;
}
void FileList::OnDispalyFun(QStringList funs){//接收到函数名List
QTreeWidgetItem * curItem=files.value(currentFile);
if(curItem!=NULL){
if(curItem->childCount()!=0) {
qDeleteAll(curItem->takeChildren());
functions.clear();
}
foreach (QString fun, funs) {
QTreeWidgetItem* funItem=new QTreeWidgetItem(curItem);
funItem->setText(0,fun);
functions.insert(fun,funItem);
}
}
}