#include "filemanager.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QInputDialog>
#include <QMessageBox>
//构造函数
FileManager::FileManager(QWidget *parent)
: QWidget(parent)
{
QLabel *pathLabel = new QLabel(this);
pathLabel->setText(tr("path"));
initSystem();
connect(treeView, SIGNAL(doubleClicked(QModelIndex )), table, SLOT(setRootIndex(QModelIndex )));
connect(table, SIGNAL(doubleClicked(QModelIndex )), this,SLOT(showChild(QModelIndex)));
connect(treeView, SIGNAL(doubleClicked(QModelIndex )),this,SLOT(showPath(QModelIndex)));
connect(table, SIGNAL(doubleClicked(QModelIndex )), this,SLOT(showPath(QModelIndex)));
connect(backPtn,SIGNAL(clicked()),SLOT(GoBack()));
connect(advancePtn,SIGNAL(clicked()),SLOT(GoAhead()));
connect(table,SIGNAL(clicked(QModelIndex)),this,SLOT(getPath(QModelIndex)));
QSplitter *splitter = new QSplitter(this);
splitter->addWidget(treeView);
splitter->addWidget(table);
splitter->show();
QHBoxLayout *hlayout = new QHBoxLayout;
QVBoxLayout *vlayout = new QVBoxLayout;
hlayout->addWidget(advancePtn);
hlayout->addWidget(backPtn);
hlayout->addWidget(pathLabel);
hlayout->addWidget(pathLine);
vlayout->addLayout(hlayout);
vlayout->addWidget(splitter);
setLayout(vlayout);
}
//析构函数
FileManager::~FileManager()
{
delete model;
delete treeView;
delete table;
delete pathLine;
delete backPtn;
delete advancePtn;
}
//新建文本文档
void FileManager::newTxtFile(void)
{
QString path = model->fileInfo(DirIndex).absoluteFilePath();//获取程序当前文件路径
#ifdef _WIN32
path.replace("/","\\");//将路径中的'/'替换为windows中的'\\'
path = path + "\\";
#else
path = path + "/";//LINUX路径
#endif
if(!findSameFile(tr("NewNotepad.txt"),path))
{
QFile file(path+tr("NewNotepad.txt"));
file.open(QIODevice::WriteOnly);
file.close();
}
else
{
int i = 1;
QString num;
while(1)
{
QString filename = tr("NewNotepad")+num.setNum(i)+".txt";
if(!findSameFile(filename,path))
{
QFile file(path+filename);
file.open(QIODevice::WriteOnly);
file.close();
return;
}
i++;
}
}
}
//新建excel表格
void FileManager::newExelFile(void)
{
QString path = model->fileInfo(DirIndex).absoluteFilePath();//获取程序当前文件路径
#ifdef _WIN32
path.replace("/","\\");//将路径中的'/'替换为windows中的'\\'
path = path + "\\";
#else
path = path + "/";
#endif
if(!findSameFile(tr("Microsoft Excel.xls"),path))
{
QFile file(path+tr("Microsoft Excel.xls"));
file.open(QIODevice::WriteOnly);
file.close();
}
else
{
int i = 1;
QString num;
while(1)
{
QString filename = tr("Microsoft Excel")+num.setNum(i)+".xls";
if(!findSameFile(filename,path))
{
QFile file(path+filename);
file.open(QIODevice::WriteOnly);
file.close();
return;
}
i++;
}
}
}
//新建word文件
void FileManager::newWordFile(void)
{
QString path = model->fileInfo(DirIndex).absoluteFilePath();//获取程序当前文件路径
#ifdef _WIN32
path.replace("/","\\");//将路径中的'/'替换为windows中的'\\'
path = path + "\\";
#else
path = path + "/";
#endif
if(!findSameFile(tr("Microsoft Word.doc"),path))
{
QFile file(path+tr("Microsoft Word.doc"));
file.open(QIODevice::WriteOnly);
file.close();
}
else
{
int i = 1;
QString num;
while(1)
{
QString filename = tr("Microsoft Word")+num.setNum(i)+".doc";
if(!findSameFile(filename,path))
{
QFile file(path+filename);
file.open(QIODevice::WriteOnly);
file.close();
return;
}
i++;
}
}
}
//复制文件
void FileManager::copyFile(void)
{
//copyPath = model->fileInfo(selectFileIndex).absoluteFilePath();
//copyPath.replace("/","\\");
copyIdex = selectFileIndex;
isCopy = true;
copyPath = model->fileInfo(copyIdex).absoluteFilePath();
#ifdef _WIN32
copyPath.replace("/","\\");
int begin = copyPath.lastIndexOf("\\")+1;
#else
int begin = copyPath.lastIndexOf("/")+1;
#endif
int end = copyPath.lastIndexOf(".")-1;
copyPostfixName = copyPath.mid(end+1);
copyFileName = copyPath.mid(begin,end-begin+1);
isCopyFile = false;
if(model->fileInfo(copyIdex).isFile()){
isCopyFile = true;
}
}
//粘贴
void FileManager::pastFile(void)
{
newPath = model->fileInfo(DirIndex).absoluteFilePath();
#ifdef _WIN32
newPath.replace("/","\\");
#endif
QString filename = copyFileName + copyPostfixName;
if(isCopyFile){
if(findSameFile(filename,newPath)){//文件存在
int i = 1;
QString num;
while(1){
filename = copyFileName+tr("_Copy") + num.setNum(i)+copyPostfixName;
if(!findSameFile(filename,newPath))
{
break;
}
i++;
}//while(1)
}
#ifdef _WIN32
newPath = newPath + "\\" + filename;
#else
newPath = newPath + "/" + filename;
#endif
QFile::copy(copyPath,newPath);
}else{
#ifdef _WIN32
int begin = copyPath.lastIndexOf("\\")+1;
#else
int begin = copyPath.lastIndexOf("/")+1;
#endif
QString filename = copyPath.mid(begin);
if(findSameFile(filename,newPath)){//文件夹存在
int i = 1;
QString num;
while(1){
QString filename1 = filename+tr("_Copy") + num.setNum(i);
if(!findSameFile(filename1,newPath))
{
filename = filename1;
break;
}
i++;
}//while(1)
}
#ifdef _WIN32
newPath = newPath+"\\"+filename;
#else
newPath = newPath+"/"+filename;
#endif
if(!copyDirectoryFiles(copyPath,newPath))
qDebug()<<"no!";
}
}
//新建目录
void FileManager::newDFile()
{
if (!DirIndex.isValid())
{
return;
}
QString path = model->fileInfo(DirIndex).absoluteFilePath();//获取程序当前文件路径
#ifdef _WIN32
path.replace("/","\\");//将路径中的'/'替换为windows中的'\\'
path = path + "\\";
#else
path = path + "/";
#endif
QString newpathname(tr("NewFolder"));
if(findSameFile(newpathname,path)){
int i = 1;
QString num;
while(1){
QString filename1 = newpathname + num.setNum(i);
if(!findSameFile(filename1,path))
{
newpathname = filename1;
break;
}
i++;
}//while(1)
}
if (!model->mkdir(DirIndex, newpathname).isValid())
{
QMessageBox::information(this, tr("Create Directory"), tr("Failed to create the directory"));
}
}
//删除文件
void FileManager::deleteFile()
{
if (!selectFileIndex.isValid())
{
return;
}
bool ok1,ok;//ok2;
if (model->fileInfo(selectFileIndex).isDir())//删除文件夹
{
ok1 = removeFolderContent(model->fileInfo(selectFileIndex).absoluteFilePath());
// ok2 = model->rmdir(selectFileIndex);
ok = ok1;// && ok2;
- 1
- 2
前往页