使用QTreeView只是做为显示数据使用
#ifndef MIRALLTREEVIEW_H
#define MIRALLTREEVIEW_H
#include <QTreeView>
#include <QStandardItemModel>
//---------------------------------------------
#include <QStyledItemDelegate>
#include <QPainter>
#include <QHeaderView>
//---------------------------------------------
#include <QDir>
#include <QFileIconProvider>
#include <QDateTime>
#include <QDebug>
#include <QMouseEvent>
#include <QDesktopServices>
#include <QUrl>
#include <QMenu>
#include <QTableView>
#include <QToolButton>
#include <QLineEdit>
#include <QFrame>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QSpacerItem>
#include <QSizePolicy>
#include <QLabel>
#include <QThread>
#include <QDir>
#include <QDialog>
#include <QTimer>
#include <QEventLoop>
#include <QMessageBox>
#include <QPushButton>
class MirallWidget;
class ProgressBarDlg;
class MirallDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MirallDelegate(){}
virtual ~MirallDelegate(){}
enum datarole {
fileIconRole,
fileNameRole,
fileSizeRole,
fileStautsRole
};
void paint( QPainter*painter, const QStyleOptionViewItem&option, const QModelIndex& index ) const
{
QStyledItemDelegate::paint(painter,option,index);
QStyleOptionViewItemV4 opt(option);
painter->save();
if(index.column() == 0)
{
bool fileStatus = qvariant_cast<bool>(index.data(fileStautsRole));
QIcon fileIcon = qvariant_cast<QIcon>(index.data(fileIconRole));
QString fileText = qvariant_cast<QString>(index.data(fileNameRole));
if(fileStatus)
{
QIcon folderIcon(":/mainPage/resources/mainPage/folder.png");
QPixmap pm = folderIcon.pixmap(32,32);
painter->drawPixmap(QPoint(option.rect.left()+20,option.rect.top()+4),pm);
}
else
{
QIcon mainIcon(":/mainPage/resources/mainPage/page_white.png");
QPixmap pm = mainIcon.pixmap(32,32);
painter->drawPixmap(QPoint(option.rect.left()+20,option.rect.top()+4),pm);
QPixmap filePm = fileIcon.pixmap(16,16);
painter->drawPixmap(QPoint(option.rect.left()+33,option.rect.top()+18),filePm);
}
painter->drawText(QPoint(option.rect.left()+56,option.rect.top()+25), fileText);
}
painter->restore();
}
QSize sizeHint( const QStyleOptionViewItem&, const QModelIndex& ) const
{
return QSize(0,40);
}
bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option,
const QModelIndex& index )
{
return false;
}
};
class fileClass : public QObject
{
public:
fileClass(){}
fileClass(const fileClass& file)
{
m_fileName = file.m_fileName;
m_fileType = file.m_fileType;
m_fileIcon = file.m_fileIcon;
m_fileTime = file.m_fileTime;
m_fileSize = file.m_fileSize;
}
void operator =( const fileClass & file)
{
m_fileName = file.m_fileName;
m_fileType = file.m_fileType;
m_fileIcon = file.m_fileIcon;
m_fileTime = file.m_fileTime;
m_fileSize = file.m_fileSize;
}
QString m_fileName;
qint16 m_fileType;
QIcon m_fileIcon;
QString m_fileTime;
QString m_fileSize;
enum fileType
{
IS_FILE,
IS_DIR
};
};
typedef QList<fileClass> DirList;
typedef QList<fileClass> FileList;
class MirallTreeView : public QTreeView
{
Q_OBJECT
public:
explicit MirallTreeView(const QString & curDirPath,MirallWidget *parent = 0);
~MirallTreeView()
{
}
static bool removeDir(QString &);
signals:
void G_sndProgressValue(qint64,qint64);
void G_curPathChange(QString);
void G_sendCutStat(bool);
void G_sndDirName( const QString &,QFileInfo &);
void G_sndDirName(QFileInfo);
void G_sndDirList(DirList);
void G_rfhDir();
public slots:
void S_backFolder();
void S_headFolder();
void S_searchFile(QString);
void S_refreshFile();
void S_openFile();
void S_copyFile();
void S_cutFile();
void S_delFile();
void S_pasteFile();
void S_renameFile();
void S_rnameFile();
// void S_dataChanage(QModelIndex,QModelIndex);
void S_chgeCurrDir(QString);
void S_chgeSyncCurrDir(QString);
protected:
void mouseDoubleClickEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void contextMenuEvent(QContextMenuEvent *event);
void keyPressEvent(QKeyEvent *event);
private:
FileList L_File;
DirList L_Sfile;
public:
QString m_curDirPath;
QString m_syncDirPath;
//----------
bool b_srhBtnClk; //
bool b_pasteBnt;
bool b_isCutFile;
bool b_isSelect; //
QModelIndex m_lastIndex;
QString fileSearchStr;
QDialog *chgFnameDlg;
QLabel *titleName;
QLineEdit *dstFileName;
//FileModel *model;
//-------
void traverDir( QString &);
bool getFileInfo( const QFileInfo &);
void insertToTable(DirList &);
//文件操作
QAction *openAction;
QAction *copyAction;
QAction *delAction;
QAction *moveAction;
QAction *cutAction;
QAction *pasteAction;
QAction *renameAction;
public:
QStringList pathList; //存储访问过的路径(返回按钮使用)
QStringList headPathList;
MirallWidget *_parent;
QString curFielName; //copy rename cut
QStandardItemModel *model;
DirList L_Dir;
int currRowCount;
};
class CopyFileThread : public QThread
{
Q_OBJECT
public:
CopyFileThread(const QString &fileName,const QString &currDir):
m_fileName(fileName),
m_currDir(currDir),
dirSize(0),
fileCount(0),
transCount(0)
{}
~CopyFileThread(){}
private:
qint64 getDirSize( const QString &dirName, QString dstDirPath = "",bool isSize = true );
void transFile(const QString &_srcFile,QString _dstDir,qint64 fileSize);
protected:
void run();
signals:
void G_sndProgressValue(qint64 _done,qint64 _total);
void G_quitThread();
void G_sndFileCount(int,int);
private:
QString m_fileName;
QString m_currDir;
quint64 dirSize;
int fileCount;
int transCount;
public:
QFile file;
QFile file2;
};
#include <QProgressBar>
//
class ProgressBarDlg :public QDialog
{
Q_OBJECT
public:
explicit ProgressBarDlg(const QString &srcPath, const QString &dstPath,QDialog *parent = 0);
~ProgressBarDlg()
{
}
QToolButton *cancelBtn;
public slots:
void S_updateProgress(qint64 _done,qint64 _total);
void S_setFileCount(int,int);
void S_closeDlg();
void S_getCutFileStat(bool);
private:
CopyFileThread *cpyThread;
MirallTreeView *mirallView;
QProgressBar *progress;
qint64 hadDone;
QString dstFileName;
bool b_isCutFile;
QLabel *label2;
signals:
void G_sndDirName(QFileInfo);
protected:
void closeEvent(QCloseEvent *e);
};
class LineEdit :public QLineEdit
{
Q_OBJECT
public:
explicit LineEdit(QWidget *parent = 0):
QLineEdit(parent){}
protected:
void mousePressEvent(QMouseEvent * event)
{
if(event->button() == Qt::LeftButton)
{
if(!text().compare(tr("Search Files ...")))
this->clear();
}
QLineEdit::mousePressEvent(event);
}
};
class MirallWidget : public QWidget
{
Q_OBJECT
public:
explicit MirallWidget(const QString &filePath,QWidget *parent = 0);
~MirallWidget()
{
delete fileList;
delete searchButton;
}
QString m_curDirPath;
QString m_syncDirPath;
QString m_synDirName;
QToolButton *searchButton;
QToolButton *refreshButton;
QToolButton *layChanageButton;
QToolButton *backButton;
QToolButton *goHeadButton;
QFrame *editFrame;
QWidget *folder_bg_Widget;
LineEdit *searchEdit;
QWidget *buttonWidget;
QHBoxLayout *hSearchLayout;
QHBoxLayout *bgLayout;
QHBoxLayout *hButtonLayout;
QVBoxLayout *vWidgetLayout;
QLabel *dirPathLabel;
// QSpacerItem *verSpacer;
QSpacerItem *horSpacer;
MirallTreeView *fileList;
public slots:
void S_getSearchStr();
void S_getSearchStr(QString);
void S_getCurPath(QString);
signals:
void G_sndSearchStr(QString);
void G_sndDirName(const QString &,QFileInfo &);
void G_sndCurDir(QString);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
};
#endif // MIRALLTREEVIEW_H
cpp实现:
/****************************************************************************\
* @作用:遍历本地指定文件夹下的文件
/***************************************************************************/
#include "miralltreeview.h"
#include <QTextCodec>
//MirallTreeView----------------------------------------------------------
MirallTreeView::MirallTreeView(const QString &curDirPath,MirallWidget *parent) :
QTreeView(parent),
m_curDirPath(curDirPath),
m_syncDirPath(curDirPath),
b_srhBtnClk(false),
b_pasteBnt(false),
b_isCutFile(false),
b_isSelect(false),
_parent(parent)
{
pathList.clear();
currRowCount = 0;
model = new QStandardItemModel(5,3);
model->setHorizontalHeaderLabels(QStringList()<<tr("文件名")<<tr("文件大小")<<tr("修改时间"));
this->header()->setFixedHeight(30);
this->setModel(model);
this->setColumnWidth(0,300);
this->setColumnWidth(1,100);
this->setColumnWidth(2,200);
this->setStyleSheet( "QTreeView::branch {image:none;}" ); //去掉虚线
this->setRootIsDecorated( false ); //取消根部空余部分
this->setEditTriggers( QAbstractItemView::NoEditTriggers ); //设置item不可编辑
MirallDelegate *delegate = new MirallDelegate;
this->setItemDelegate(delegate);
this->setSortingEnabled(true); //允许头排序
this->header()->setMovable(false); //禁用头字段移动
openAction = new QAction(tr("打开"),this);
copyAction = new QAction(tr("复制"),this);
delAction = new QAction(tr("删除"),this);
moveAction = new QAction(tr("移动到"),this);
cutAction = new QAction(tr("剪切"),this);
pasteAction = new QAction(tr("粘贴"),this);
renameAction = new QAction(tr("重命名"),this);
moveAction->setVisible(false);
connect(openAction,SIGNAL(triggered()),SLOT(S_openFile()));
connect(copyAction,SIGNAL(triggered()),SLOT(S_copyFile()));
connect(delAction,SIGNAL(triggered()),SLOT(S_delFile()));
connect(cutAction,SIGNAL(triggered()),SLOT(S_cutFile()));
connect(pasteAction,SIGNAL(triggered()),SLOT(S_pasteFile()));
connect(renameAction,SIGNAL(triggered()),SLOT(S_renameFile()));
traverDir(m_curDirPath);
insertToTable(L_Dir);
pathList.append(m_curDirPath);
this->setAcceptDrops(true); //设置允许拖放动作
connect(parent,SIGNAL(G_sndSearchStr(QString)),SLOT(S_searchFile(QString)));
}
//目录遍历
void MirallTreeView::traverDir(QString &filePath)
{
L_Dir.clear();
L_File.clear();
QDir curDir( filePath );
foreach( QFileInfo fileInfor, curDir.entryInfoList() ) //遍历当前文档下的文件
{
if(!getFileInfo(fileInfor))
{
continue;
}
}
qDebug() <<"+++++++++++++";
L_Dir.append(L_File); //此时L_Dir中包含所有文件
}
//获取文件或文件夹的信息
bool MirallTreeView::getFileInfo( const QFileInfo & fileInfor)
{
QFileIconProvider icon_provider;
QIcon icon = icon_provider.icon( fileInfor );
QString fileName = fileInfor.fileName();
QString lastModify = fileInfor.lastModified().toLocalTime().toString("yyyy-MM-dd hh:mm:ss");
if( fileName.left(1) == "." )
{
return false;
}
else if( fileInfor.isDir() )
{
fileClass m_dir;
m_dir.m_fileIcon = icon;
m_dir.m_fileName = fileName;
m_dir.m_fileType = fileClass::IS_DIR;
m_dir.m_fileTime = lastModify;
m_dir.m_fileSize = "_";
L_Dir.append( m_dir );
}
else if( fileInfor.isFile() )
{
QString fileSize = fileInfor.size() / 1024 /1024?
QString::number(fileInfor.size() / 1024 /1024)+="MB":
fileInfor.size() / 1024 ?
QString::number(fileInfor.size() / 1024)+="KB" : QString::number(fileInfor.size())+="B";
fileClass m_file;
m_file.m_fileIcon = icon;
m_file.m_fileName = fileName;
m_file.m_fileType = fileClass::IS_DIR;
m_file.m_fileTime = lastModify;
m_file.m_fileSize = fileSize;
L_File.append( m_file );
}
return true;
}
//数据掺入
void MirallTreeView::insertToTable(DirList & L_Dir)
{
for(int i= 0;i < model->rowCount();i++)
{
QList<QStandardItem *> itemList = model->takeRow(i);
for(int index = 0;index< itemList.count();index++)
{
delete itemList.at(index);
}
}
currRowCount = L_Dir.count();
model->setRowCount(currRowCount);
qDebug() << currRowCount;
for(int index= 0;index <L_Dir.count();index++)
{
for(int i = 0; i< 3; i++)
{
QStandardItem *item;
if(i == 0)
{
item = new QStandardItem;
item->setData(L_Dir.at(index).m_fileName,MirallDelegate::fileNameRole);
item->setData(L_Dir.at(index).m_fileIcon,MirallDelegate::fileIconRole);
if(L_Dir.at(index).m_fileSize =="_")
item->setData(true,MirallDelegate::fileStautsRole);
else
item->setData(false,MirallDelegate::fileStautsRole);
}
else if(i == 1)
{
item = new QStandardItem( L_Dir.at(index).m_fileSize );
}
else
{
item = new QStandardItem( L_Dir.at(index).m_fileTime);
}
model->setItem( index, i,item );
}
}
}
//文件夹删除
bool MirallTreeView::removeDir(QString & dirPath)
{
QDir dir(dirPath);
QStringList files = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
QList<QString>::iterator f = files.begin();
bool error = true;
for (; f != files.end(); f++)
{
QString filePath = QDir::convertSeparators(dir.path() + '/' + (*f));
QFileInfo fi(filePath);
if (fi.isFile() || fi.isSymLink())
{
QFile::setPermissions(filePath, QFile::WriteOwner);
if (!QFile::remove(filePath))
{
qDebug() << "Global::deleteDir 1" << filePath << "faild";
error = false;
}
}
else if (fi.isDir())
{
if ( !removeDir(filePath) )
{
error = false;
}
}
}
if(!dir.rmdir(QDir::convertSeparators(dir.path())))
{
qDebug() << dir.path();
qDebug() << "Global::deleteDir 3" << dir.path() << "faild";
error = false;
}
return error;
}
//鼠标双击事件(可以使用信号与槽函数代替)
void MirallTreeView::mouseDoubleClickEvent(QMouseEvent *event)
{
QPoint point(event->pos()); //获取鼠标点击的坐标
QModelIndex index = indexAt( point ); //获取鼠标点击处 的index
if( index.isValid() ) //如果index是有效的
{
if( event->button() == Qt::LeftButton )
{
S_openFile();
}
}
QTreeView::mouseDoubleClickEvent(event);
}
void MirallTreeView::mousePressEvent(QMouseEvent *event)
{
QPoint point(event->pos()); //获取鼠标点击的坐标
QModelIndex index = indexAt( point ); //获取鼠标点击处 的index
if(index.isValid())
{
b_isSelect = true;
}
else
b_isSelect = false;
QTreeView::mousePressEvent(event);
}
//右键
void MirallTreeView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = new QMenu();
QFont thisFont;
thisFont.setPointSize(10);
QModelIndex index = indexAt(QPoint(event->pos()));
if(index.isValid())
{
b_isSelect = true; //被选中
}
openAction->setEnabled(index.isValid());
copyAction->setEnabled(index.isValid());
cutAction->setEnabled(index.isValid());
delAction->setEnabled(index.isValid());
renameAction->setEnabled(index.isValid());
moveAction->setEnabled(index.isValid());
pasteAction->setEnabled(b_pasteBnt);
menu->setFont(thisFont);
menu->addAction(openAction);
menu->addSeparator();
menu->addAction(copyAction);
menu->addAction(cutAction);
menu->addAction(pasteAction);
menu->addSeparator();
menu->addAction(moveAction);
menu->addAction(renameAction);
menu->addAction(delAction);
menu->exec(QCursor::pos());
}
//键盘事件
void MirallTreeView::keyPressEvent(QKeyEvent *event)
{
if((event->key() == Qt::Key_Delete) && b_isSelect)
{
S_delFile();
b_isSelect = false;
}
if((event->modifiers() == Qt::ControlModifier )&& (event->key() == Qt::Key_C) && b_isSelect)
{
S_copyFile();
b_isSelect = false;
}
if((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_D) && b_isSelect)
{
S_delFile();
}
if((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_V) && b_pasteBnt)
{
S_pasteFile();
}
if((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_X) && b_isSelect)
{
S_cutFile();
}
QTreeView::keyPressEvent(event);
}
//目录返回
void MirallTreeView::S_backFolder()
{
_parent->goHeadButton->setEnabled(true);
m_curDirPath.clear();
m_curDirPath = pathList.at(pathList.count() - 2);
headPathList.append(pathList.at(pathList.count() - 1));
pathList.takeLast();
traverDir(m_curDirPath);
insertToTable(L_Dir);
if(pathList.count() == 1)
{
_parent->backButton->setEnabled(false);
}
emit G_curPathChange(m_curDirPath);
b_isSelect = false;
}
#include <QMessageBox>
//目录前进
void MirallTreeView::S_headFolder()
{
_parent->backButton->setEnabled(true);
m_curDirPath.clear();
m_curDirPath = headPathList.at(headPathList.count() - 1);
pathList.append(m_curDirPath);
headPathList.takeLast();
if(headPathList.count() == 0)
{
_parent->goHeadButton->setEnabled(false);
}
traverDir(m_curDirPath);
insertToTable(L_Dir);
emit G_curPathChange(m_curDirPath);
b_isSelect = false;
}
//文件查询
void MirallTreeView::S_searchFile(QString _str)
{
if(!_str.isEmpty() && _str.compare(tr("Search Files ...")))
{
L_Sfile.clear();
b_srhBtnClk = true;
if(_str.contains(" "))
{
_str = _str.trimmed();
}
qDebug()<<_str<<_str.length();
if(!_str.isEmpty())
{
fileSearchStr = _str; //将要搜索的字符串保存,在刷新按钮中使用
for(int i = 0;i < L_Dir.count();i++)
{
if(L_Dir.at(i).m_fileName.toLower().contains(_str.toLower())) //不区分大消息查询
// if(L_Dir.at(i)->m_fileName.contains(_str))
{
L_Sfile.append(L_Dir.at(i));
}
}
insertToTable(L_Sfile);
}
}
else
{
if(b_srhBtnClk)
{
b_srhBtnClk = false;
insertToTable(L_Dir);
}
}
}
//文件刷新
void MirallTreeView::S_refreshFile()
{
traverDir(m_curDirPath);
if(b_srhBtnClk)
{
L_Sfile.clear();
for(int i = 0;i < L_Dir.count();i++)
{
if(L_Dir.at(i).m_fileName.contains(fileSearchStr))
{
L_Sfile.append(L_Dir.at(i));
}
}
insertToTable(L_Sfile);
}
else
insertToTable(L_Dir);
}
//文件操作-----------------------------------------------------------------
void MirallTreeView::S_openFile()
{
QModelIndex index = model->index(currentIndex().row(),0);
QString tmppath;
if(m_curDirPath.endsWith("/"))
tmppath = m_curDirPath + index.data(MirallDelegate::fileNameRole).toString();
else
tmppath = m_curDirPath + "/" + index.data(MirallDelegate::fileNameRole).toString();
if(QFileInfo(tmppath).isDir())
{
m_curDirPath.clear();
m_curDirPath = tmppath;
pathList.append(m_curDirPath);
headPathList.clear();
_parent->goHeadButton->setEnabled(false);
_parent->backButton->setEnabled(true);
traverDir(m_curDirPath);
insertToTable(L_Dir);
emit G_curPathChange(m_curDirPath);
}
else
{
QDesktopServices::openUrl(QUrl::fromLocalFile(tmppath));
}
}
//文件拷贝
void MirallTreeView::S_copyFile()
{
QModelIndex index = model->index(currentIndex().row(),0);
if(m_curDirPath.endsWith("/"))
curFielName = m_curDirPath + index.data(MirallDelegate::fileNameRole).toString();
else
curFielName = m_curDirPath + "/" + index.data(MirallDelegate::fileNameRole).toString();
b_pasteBnt = true;
b_isCutFile = false;
}
//文件粘贴
void MirallTreeView::S_pasteFile()
{
if(m_curDirPath.startsWith(m_syncDirPath+"/Shared") || m_curDirPath.startsWith(m_syncDirPath+"/public"))
{
QMessageBox::warning(this,tr("提示信息"),tr("不可将文件复制到公共空间或者个人共享以及其子目录下!"));
return;
}
if(m_curDirPath.contains(curFielName)) //不能将目录拷贝到自身或者其子目录中
{
if( m_curDirPath == curFielName )
QMessageBox::warning(this, tr("提示信息"),tr("不能将目录拷贝到自身目录下"));
else
QMessageBox::warning(this,tr("提示信息"),tr("不能将目录拷贝到其子目录下"));
return;
}
if( QFileInfo(curFielName).absoluteDir() == m_curDirPath )
{
QMessageBox::warning(this, tr("提示信息"),tr("不能将文件复制到同目录下"));
return;
}
CopyFileThread *cypThread = new CopyFileThread(curFielName,m_curDirPath);
ProgressBarDlg *progress = new ProgressBarDlg(curFielName,m_curDirPath);
connect(this,SIGNAL(G_sendCutStat(bool)),progress,SLOT(S_getCutFileStat(bool)));
if(b_isCutFile) //剪切
{
emit G_sendCutStat(true);
b_isCutFile = false;
}
//插入list
QFileInfo fileInfor(curFielName);
int index = 0;
for(;index < L_Dir.count();index++)
{
if(fileInfor.fileName() == L_Dir.at(index).m_fileName)
{
if(QMessageBox::No == QMessageBox::information(this,tr("Information"),
tr("是否覆盖当前目录下的%1").arg(fileInfor.fileName()),
QMessageBox::Ok,QMessageBox::No))
{
return;
}
else
break;
}
}
if(index == L_Dir.count())
{
//insert data
for(int i = 0; i< 3; i++)
{
QStandardItem *item;
if(i == 0)
{
QFileIconProvider icon_provider;
item = new QStandardItem;
item->setData(fileInfor.fileName(),MirallDelegate::fileNameRole);
item->setData(icon_provider.icon( fileInfor ),MirallDelegate::fileIconRole);;
if(fileInfor.isDir())
item->setData(true,MirallDelegate::fileStautsRole);
else
item->setData(false,MirallDelegate::fileStautsRole);
}
else if(i == 1)
{
if(fileInfor.isFile())
{
QString fileSize = fileInfor.size() / 1024 /1024?
QString::number(fileInfor.size() / 1024 /1024)+="MB":
fileInfor.size() / 1024 ?
QString::number(fileInfor.size() / 1024)+="KB" : QString::number(fileInfor.size())+="B";
item = new QStandardItem(fileSize); //
}
else
item = new QStandardItem("_");
}
else
{
QString lastModify = fileInfor.lastModified().toLocalTime().toString("yyyy-MM-dd hh:mm:ss");
item = new QStandardItem( lastModify);
}
model->setItem( currRowCount, i,item );
}
currRowCount++;
if( (m_curDirPath.endsWith("public") || m_curDirPath.endsWith("private") || m_curDirPath.endsWith("Shared")))
{
if(fileInfor.isDir())
emit G_sndDirName(m_curDirPath + "/" + fileInfor.fileName(),fileInfor);
}
}
//插入list
progress->show();
connect(cypThread,SIGNAL(G_sndProgressValue(qint64,qint64)),progress,SLOT(S_updateProgress(qint64,qint64)));
connect(cypThread,SIGNAL(G_sndFileCount(int,int)),progress,SLOT(S_setFileCount(int,int)));
cypThread->start();
b_pasteBnt = false;
}
//文件剪切
void MirallTreeView::S_cutFile()
{
curFielName = model->index(currentIndex().row(),0).data(MirallDelegate::fileNameRole).toString();
if(m_curDirPath.endsWith("/"))
curFielName = m_curDirPath + curFielName;
else
curFielName = m_curDirPath + "/" + curFielName;
b_pasteBnt = true;
b_isCutFile = true;
}
//文件删除
void MirallTreeView::S_delFile()
{
curFielName = model->index(currentIndex().row(),0).data(MirallDelegate::fileNameRole).toString();
int i = 0;
for(;i < L_Dir.count();i++ )
{
if(!curFielName.compare(L_Dir.at(i).m_fileName))
{
L_Dir.takeAt(i);
break;
}
}
if(m_curDirPath.endsWith("/"))
curFielName = m_curDirPath + curFielName;
else
curFielName = m_curDirPath + "/" + curFielName;
if(QFileInfo(curFielName).isFile())
{
if(!QFile::remove(curFielName))
{
QMessageBox::warning(this,tr("提示信息"),tr("删除文件失败!"));
return;
}
}
else
{
if(!removeDir(curFielName))
{
QMessageBox::warning(this,tr("提示信息"),tr("删除文件夹失败!"));
return;
}
if( (m_curDirPath.endsWith("public") || m_curDirPath.endsWith("private") || m_curDirPath.endsWith("Shared")))
{
emit G_sndDirName(QFileInfo(curFielName));
curFielName.clear();
}
}
QList<QStandardItem *> itemList = model->takeRow(currentIndex().row());
for(int index = 0;index< itemList.count();index++)
{
delete itemList.at(index);
}
currRowCount--;
this->setCurrentIndex(QModelIndex());
}
//文件重命名
void MirallTreeView::S_renameFile()
{
QModelIndex index = model->index(currentIndex().row(),0);
m_lastIndex = index;
if(m_curDirPath.endsWith("/"))
curFielName = m_curDirPath + index.data(MirallDelegate::fileNameRole).toString();
else
curFielName = m_curDirPath + "/" + index.data(MirallDelegate::fileNameRole).toString();
chgFnameDlg = new QDialog;
chgFnameDlg->setWindowFlags(Qt::WindowContextHelpButtonHint);
chgFnameDlg->setAttribute(Qt::WA_DeleteOnClose);
chgFnameDlg->setWindowTitle(tr("文件重命名"));
chgFnameDlg->resize(280,150);
QFrame *line = new QFrame(chgFnameDlg);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
QLineEdit *srcFileName = new QLineEdit(chgFnameDlg);
srcFileName->setEnabled(false);
srcFileName->setText(index.data(MirallDelegate::fileNameRole).toString());
dstFileName = new QLineEdit(chgFnameDlg);
dstFileName->setMaxLength(255);
QSpacerItem *space = new QSpacerItem(40,20,QSizePolicy::Expanding, QSizePolicy::Minimum);
QPushButton *btCancel = new QPushButton(chgFnameDlg);
btCancel->setText(tr("取消"));
QPushButton *btCommit = new QPushButton(chgFnameDlg);
btCommit->setText(tr("修改"));
btCommit->setDefault(true);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addSpacerItem(space);
hLayout->addWidget(btCancel);
hLayout->addWidget(btCommit);
hLayout->setSpacing(6);
QVBoxLayout *vLayout = new QVBoxLayout(chgFnameDlg);
vLayout->addWidget(srcFileName);
vLayout->addWidget(dstFileName);
vLayout->addWidget(line);
vLayout->addLayout(hLayout);
vLayout->setContentsMargins(20,9,20,9);
vLayout->setSpacing(10);
connect(btCancel,SIGNAL(clicked()),chgFnameDlg,SLOT(close()));
connect(btCommit,SIGNAL(clicked()),SLOT(S_rnameFile()));
chgFnameDlg->exec();
//connect(model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(S_dataChanage(QModelIndex,QModelIndex)));
}
void MirallTreeView::S_rnameFile()
{
QString tmpFilePath,tmpFileName = dstFileName->text();
if(tmpFileName.isEmpty())
{
chgFnameDlg->close();
return;
}
int index = 0,tmpNum = 0;
bool isSearch = true;
for(;index<L_Dir.count();index++) //判断是否存在
{
if(!tmpFileName.compare(L_Dir.at(index).m_fileName))
{
QMessageBox::warning(chgFnameDlg,"提示信息:",tr("%1 已存在当前文件夹中,重命名失败!").arg(tmpFilePath));
chgFnameDlg->close();
return;
}
if(isSearch && !QFileInfo(curFielName).fileName().compare(L_Dir.at(index).m_fileName))
{
tmpNum = index;
isSearch = false;
}
}
fileClass m_File = L_Dir.at(tmpNum);
m_File.m_fileName = tmpFileName;
L_Dir.replace(tmpNum,m_File);
// L_Dir.at(tmpNum).m_fileName = tmpFileName;
if(m_curDirPath.endsWith("/"))
tmpFilePath = m_curDirPath + tmpFileName;
else
tmpFilePath = m_curDirPath + "/" + tmpFileName;
if(QFileInfo(curFielName).isFile())
{
if(!QFile(curFielName).rename(tmpFilePath))
{
QMessageBox::warning(chgFnameDlg,tr("提示信息"),tr("文件重命名失败!"));
}
else
{
model->setData(model->index(m_lastIndex.row(),0),tmpFileName,MirallDelegate::fileNameRole);
QMessageBox::warning(chgFnameDlg,tr("提示信息"),tr("文件重命名成功!"));
}
}
else
{
if(!QDir(m_curDirPath).rename(curFielName,tmpFilePath))
{
QMessageBox::warning(chgFnameDlg,tr("提示信息"),tr("文件重命名失败!"));
}
else
{
model->setData(model->index(m_lastIndex.row(),0),tmpFileName,MirallDelegate::fileNameRole);
QMessageBox::warning(chgFnameDlg,tr("提示信息"),tr("文件重命名成功!"));
}
if( (m_curDirPath.endsWith("public") || m_curDirPath.endsWith("private") || m_curDirPath.endsWith("Shared")))
{
emit G_rfhDir();
}
}
chgFnameDlg->close();
}
主要用于修改文件名
//void MirallTreeView::S_dataChanage(QModelIndex topLeft, QModelIndex rightBottom)
//{
// Q_UNUSED(topLeft)
// Q_UNUSED(rightBottom)
// disconnect(model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(S_dataChanage(QModelIndex,QModelIndex)));
// if(m_lastIndex != currentIndex()) //排错
// {
// return;
// }
// QString tmpFilePath, tmpFileName = model->index(currentIndex().row(),0).data(MirallDelegate::fileNameRole).toString();
// int tmpNum = 0;
// for(int i = 0;i < L_Dir.count();i++) //定位
// {
// if(!QFileInfo(curFielName).fileName().compare(L_Dir.at(i)->m_fileName))
// {
// tmpNum = i;
// break;
// }
// }
// L_Dir.at(tmpNum)->m_fileName = tmpFileName;
// if(m_curDirPath.endsWith("/"))
// tmpFilePath = m_curDirPath + tmpFileName;
// else
// tmpFilePath = m_curDirPath + "/" + tmpFileName;
// if(QFileInfo(curFielName).isFile())
// {
// if(!QFile(curFielName).rename(tmpFilePath))
// {
// QMessageBox::warning(this,tr("提示信息"),tr("文件重命名失败!"));
// model->setData(model->index(m_lastIndex.row(),0),QFileInfo(curFielName).fileName());
// }
// }
// else
// {
// if(!QDir(m_curDirPath).rename(curFielName,tmpFilePath))
// {
// QMessageBox::warning(this,tr("提示信息"),tr("文件重命名失败!"));
// model->setData(model->index(m_lastIndex.row(),0),QFileInfo(curFielName).fileName());
// }
// if( (m_curDirPath.endsWith("public") || m_curDirPath.endsWith("private") || m_curDirPath.endsWith("Shared")))
// {
// emit G_rfhDir();
// }
// }
//}
//点击主界面左侧的item调用
void MirallTreeView::S_chgeCurrDir(QString dirPath)
{
_parent->backButton->setEnabled(true);
_parent->goHeadButton->setEnabled(false);
headPathList.clear();
m_curDirPath.clear();
m_curDirPath = dirPath;
pathList.append(m_curDirPath);
traverDir(m_curDirPath);
insertToTable(L_Dir);
b_isSelect = false;
emit G_curPathChange(m_curDirPath);
this->setCurrentIndex(QModelIndex());
}
//修改同步目录或者切换用户的时候调用
void MirallTreeView::S_chgeSyncCurrDir(QString synDirPath)
{
pathList.clear();
_parent->backButton->setEnabled(false);
_parent->goHeadButton->setEnabled(false);
pathList.append(synDirPath);
m_curDirPath.clear();
m_curDirPath = synDirPath;
m_syncDirPath.clear();
m_syncDirPath = synDirPath;
traverDir(m_curDirPath);
insertToTable(L_Dir);
b_isSelect = false;
emit G_curPathChange(m_curDirPath);
this->setCurrentIndex(QModelIndex());
}
//MirallWidget------------------------------------------------------------
MirallWidget::MirallWidget(const QString & filePath,QWidget *parent):
QWidget(parent),
m_curDirPath(filePath),
m_syncDirPath(filePath)
{
Q_INIT_RESOURCE(mirall);
// QTextCodec *codec=QTextCodec::codecForName("UTF-8");
// if(codec==NULL)
// codec=QTextCodec::codecForLocale();
// QTextCodec::setCodecForTr(codec);
// QTextCodec::setCodecForLocale(codec);
// QTextCodec::setCodecForCStrings(codec);
buttonWidget = new QWidget(this);
buttonWidget->setFixedHeight(40);
refreshButton = new QToolButton(buttonWidget);
refreshButton->setText(tr("刷新"));
refreshButton->setStyleSheet(QString::fromUtf8("border-image: url(:/mainPage/resources/mainPage/refresh.png);\ncolor:white;\nheight:20;"));
layChanageButton = new QToolButton(buttonWidget);
layChanageButton->setText(tr("隐藏左侧"));
layChanageButton->setStyleSheet(QString::fromUtf8("border-image: url(:/mainPage/resources/mainPage/hide_left.png);\ncolor:white;\nheight:20;"));
folder_bg_Widget = new QWidget(buttonWidget);
folder_bg_Widget->setFixedSize(66,24);
backButton = new QToolButton(folder_bg_Widget);
backButton->setText(tr("返回"));
backButton->setToolTip(tr("后退"));
backButton->setEnabled(false);
backButton->setIconSize(QSize(33,24));
backButton->setFixedSize(33,24);
backButton->setStyleSheet(QString::fromUtf8("border-radius:0px;"));
backButton->setIcon(QIcon(":/mainPage/resources/mainPage/back2.png"));
goHeadButton = new QToolButton(folder_bg_Widget);
goHeadButton->setText("前进");
goHeadButton->setEnabled(false);
goHeadButton->setIconSize(QSize(33,24));
goHeadButton->setFixedSize(33,24);
goHeadButton->setToolTip(tr("前进"));
goHeadButton->setIcon(QIcon(":/mainPage/resources/mainPage/goahead2.png"));
goHeadButton->setStyleSheet(QString::fromUtf8("border-radius:0px;"));
bgLayout = new QHBoxLayout(folder_bg_Widget);
bgLayout->addWidget(backButton);
bgLayout->addWidget(goHeadButton);
bgLayout->setContentsMargins(0,0,0,0);
bgLayout->setSpacing(0);
m_synDirName = "/" +QFileInfo(filePath).fileName();
dirPathLabel = new QLabel(m_synDirName,buttonWidget);
dirPathLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Maximum);
horSpacer = new QSpacerItem(40,20,QSizePolicy::Expanding, QSizePolicy::Minimum);
editFrame = new QFrame(buttonWidget);
searchEdit = new LineEdit(editFrame);
searchEdit->setFixedWidth(140);
searchEdit->setText(tr("Search Files ..."));
searchEdit->setFrame(false);
searchEdit->setMaxLength(21);
searchButton = new QToolButton(editFrame);
searchButton->setText(tr("搜索"));
searchButton->setIcon(QIcon(":/mainPage/resources/mainPage/search.png"));
searchButton->setIconSize(QSize(20,20));
searchButton->setFixedSize(20,20);
searchButton->setStyleSheet(QString::fromUtf8("border-radius:0px;"));
searchButton->setShortcut(Qt::Key_Return);
hSearchLayout = new QHBoxLayout(editFrame);
hSearchLayout->addWidget(searchEdit);
hSearchLayout->addWidget(searchButton);
hSearchLayout->setContentsMargins(0,0,0,0);
hSearchLayout->setSpacing(0);
QSizePolicy searchSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
searchSizePolicy.setHorizontalStretch(0);
searchSizePolicy.setVerticalStretch(0);
searchSizePolicy.setHeightForWidth(editFrame->sizePolicy().hasHeightForWidth());
editFrame->setSizePolicy(searchSizePolicy);
editFrame->setFrameStyle(QFrame::StyledPanel|QFrame::Sunken);
//editFrame->setStyleSheet(QString::fromUtf8("QFrame{background-color:rgb(229,229,229);}"));
hButtonLayout = new QHBoxLayout(buttonWidget);
hButtonLayout->addWidget(folder_bg_Widget);
hButtonLayout->addWidget(dirPathLabel);
hButtonLayout->addSpacerItem(horSpacer);
hButtonLayout->addWidget(editFrame);
hButtonLayout->addWidget(refreshButton);
hButtonLayout->addWidget(layChanageButton);
hButtonLayout->setContentsMargins(20,10,20,10);
hButtonLayout->setSpacing(4);
QSizePolicy buttonSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
buttonSizePolicy.setHorizontalStretch(0);
buttonSizePolicy.setVerticalStretch(0);
buttonSizePolicy.setHeightForWidth(buttonWidget->sizePolicy().hasHeightForWidth());
buttonWidget->setSizePolicy(buttonSizePolicy);
buttonWidget->setObjectName(tr("buttonWidget"));
buttonWidget->setStyleSheet(QString::fromUtf8("#buttonWidget{border-image: url(:/mirall/resources/menuBack.png);}"));
vWidgetLayout = new QVBoxLayout(this);
fileList = new MirallTreeView(m_curDirPath,this);
fileList->setFrameStyle(QFrame::NoFrame);
vWidgetLayout->addWidget(buttonWidget);
vWidgetLayout->addWidget(fileList);
vWidgetLayout->setContentsMargins(0,0,5,2);
vWidgetLayout->setSpacing(0);
fileList->setMouseTracking(true);
buttonWidget->setMouseTracking(true);
this->setCursor(Qt::ArrowCursor);
this->setAcceptDrops(true);
fileList->setAcceptDrops(true);
QSizePolicy widgetSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
widgetSizePolicy.setHorizontalStretch(0);
widgetSizePolicy.setVerticalStretch(0);
widgetSizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
this->setSizePolicy(buttonSizePolicy);
connect(backButton,SIGNAL(clicked()),fileList,SLOT(S_backFolder()));
connect(goHeadButton,SIGNAL(clicked()),fileList,SLOT(S_headFolder()));
connect(searchButton,SIGNAL(clicked()),SLOT(S_getSearchStr()));
connect(refreshButton,SIGNAL(clicked()),fileList,SLOT(S_refreshFile()));
connect(searchEdit,SIGNAL(textChanged(QString)),SLOT(S_getSearchStr(QString)));
connect(fileList,SIGNAL(G_curPathChange(QString)),SLOT(S_getCurPath(QString)));
setAttribute(Qt::WA_DeleteOnClose,true);
}
//发送搜索信号
void MirallWidget::S_getSearchStr()
{
emit G_sndSearchStr(searchEdit->text());
}
//搜索指定字符串
void MirallWidget::S_getSearchStr(QString _tmpStr)
{
emit G_sndSearchStr(_tmpStr);
}
//更新当前路径
void MirallWidget::S_getCurPath(QString curFile)
{
m_curDirPath = curFile;
QString tmpStr = curFile.replace(m_syncDirPath,m_synDirName);
if(tmpStr.length() > 40 )
{
QString str = tmpStr.mid(19,(tmpStr.length() - 37));
tmpStr = tmpStr.replace(str,"...");
}
dirPathLabel->setText(tmpStr);
emit G_sndCurDir(QDir::toNativeSeparators(m_curDirPath));
}
//文件拖放事件
void MirallWidget::dragEnterEvent(QDragEnterEvent *event)
{
if(event->mimeData()->hasFormat("text/uri-list"))
{
event->acceptProposedAction();
}
}
//文件拖放事件
void MirallWidget::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
{
return;
}
if(m_curDirPath == m_syncDirPath)
{
QMessageBox::warning(this,tr("提示信息"),tr("不可将文件拖放同步根目录下!"));
return;
}
if(m_curDirPath.startsWith(m_syncDirPath+"/Shared") || m_curDirPath.startsWith(m_syncDirPath+"/public"))
{
QMessageBox::warning(this,tr("提示信息"),tr("不可将文件拖放到公共空间或者个人共享以及其子目录下!"));
return;
}
QString fileName = urls.first().toLocalFile();
if(fileName.isEmpty())
return;
else
{
qDebug() << fileName;
QFileInfo fileInfor(fileName);
int index = 0;
for(;index < fileList->L_Dir.count();index++)
{
if(fileInfor.fileName() == fileList->L_Dir.at(index).m_fileName)
{
if(QMessageBox::No == QMessageBox::information(this,tr("Information"),
tr("是否覆盖当前目录下的%1").arg(fileInfor.fileName()),
QMessageBox::Ok,QMessageBox::No))
{
return;
}
else
break;
}
}
if(index == fileList->L_Dir.count())
{
for(int i = 0; i< 3; i++)
{
QStandardItem *item;
if(i == 0)
{
QFileIconProvider icon_provider;
item = new QStandardItem( fileInfor.fileName() );
item->setData(fileInfor.fileName(),MirallDelegate::fileNameRole);
item->setData(icon_provider.icon( fileInfor ),MirallDelegate::fileIconRole);
if(fileInfor.isFile())
{
item->setData(false,MirallDelegate::fileStautsRole);
}
else
{
item->setData(true,MirallDelegate::fileStautsRole);
}
}
else if(i == 1)
{
if(fileInfor.isFile())
{
QString fileSize = fileInfor.size() / 1024 /1024?
QString::number(fileInfor.size() / 1024 /1024)+="MB":
fileInfor.size() / 1024 ?
QString::number(fileInfor.size() / 1024)+="KB" : QString::number(fileInfor.size())+="B";
item = new QStandardItem(fileSize); //
}
else
item = new QStandardItem("_");
}
else
{
QString lastModify = fileInfor.lastModified().toLocalTime().toString("yyyy-MM-dd hh:mm:ss");
item = new QStandardItem( lastModify);
}
fileList->model->setItem( fileList->currRowCount, i,item );
}
qDebug() << fileList->currRowCount;
fileList->currRowCount++;
if( (m_curDirPath.endsWith("public") || m_curDirPath.endsWith("private") || m_curDirPath.endsWith("Shared")))
{
if(fileInfor.isDir())
emit G_sndDirName(m_curDirPath + "/" +fileInfor.fileName(),fileInfor);
}
}
CopyFileThread *cypThread = new CopyFileThread(fileName,m_curDirPath);
ProgressBarDlg *progress = new ProgressBarDlg(fileName,m_curDirPath);
progress->show();
connect(cypThread,SIGNAL(G_sndProgressValue(qint64,qint64)),progress,SLOT(S_updateProgress(qint64,qint64)));
connect(cypThread,SIGNAL(G_sndFileCount(int,int)),progress,SLOT(S_setFileCount(int,int)));
cypThread->start();
}
}
//CopyFileThread------------------------------------------------------------
//文件传输过程
void CopyFileThread ::run()
{
QFileInfo fileinfor(m_fileName);
QDir fileDir = fileinfor.absoluteDir();
if(fileDir.path() == m_currDir)
{
//do nothing //!!!!
}
else
{
if(fileinfor.isFile())
{
qint64 fileSize = fileinfor.size();
qDebug() << fileSize;
fileCount = 1;
transFile(m_fileName,m_currDir,fileSize);
}
else
{
fileCount++;
qDebug() << getDirSize(m_fileName);
getDirSize(m_fileName,m_currDir,false);
}
}
}
//获取文件夹大小、传送文件夹
qint64 CopyFileThread::getDirSize(const QString &dirName, QString dstDirPath,bool isSize)
{
QString dstDir;
if(!isSize)
{
dstDir = dstDirPath + "/"+QFileInfo(dirName).fileName();
QDir dir(dstDirPath);
dir.mkpath(dstDir);
//如果文件夹内部为空,直接发送进度条处理信号
if(1 == fileCount)
{
emit G_sndProgressValue(100,100);
usleep(100);
transCount++;
emit G_sndFileCount(transCount,fileCount);
return 0;
}
if(fileCount != 1 && dirSize == 0 )
{
emit G_sndProgressValue(100,100);
}
transCount++;
emit G_sndFileCount(transCount,fileCount);
}
QDir dir(dirName);
foreach(QFileInfo fileInfor,dir.entryInfoList())
{
if(fileInfor.fileName()=="."||fileInfor.fileName()=="..")
{
continue;
}
if(fileInfor.isFile())
{
if(isSize)
{
dirSize += fileInfor.size();
fileCount++;
}
else
transFile(fileInfor.absoluteFilePath(),dstDir,dirSize);
}
else if(fileInfor.isDir())
{
if(isSize)
{
fileCount++;
getDirSize(fileInfor.absoluteFilePath());
}
else
{
getDirSize(fileInfor.absoluteFilePath(),dstDir,false);
}
}
}
return dirSize;
}
//传送文件
void CopyFileThread::transFile(const QString &_srcFile, QString _dstDir,qint64 fileSize)
{
file.setFileName(_srcFile);
if(!_dstDir.endsWith("/"))
_dstDir+="/";
file2.setFileName(_dstDir + QFileInfo(_srcFile).fileName());
if(file.open(QIODevice::ReadOnly) && file2.open(QIODevice::WriteOnly))
{
if(0 == fileSize)
{
if( fileCount ==1 )
emit G_sndProgressValue(100,100);
}
else
{
while(!file.atEnd())
{
char strBuf[1024*1024] = {0};
qint64 res = file.read(strBuf,1024*1024);
file2.write(strBuf,res);
file2.flush();
msleep(10);
emit G_sndProgressValue(res,fileSize);
}
}
file.close();
file2.close();
transCount++;
emit G_sndFileCount(transCount,fileCount);
}
else
{
qDebug()<<tr("文件打开失败");
}
}
//ProgressBarDlg------------------------------------------------------------
ProgressBarDlg::ProgressBarDlg(const QString &fileNamePath, const QString &dstPath, QDialog *parent):
QDialog(parent),
hadDone(0),
b_isCutFile(false)
{
progress = new QProgressBar(this);
progress->setValue(0);
progress->setRange(0,100);
QFileInfo fileInfo(fileNamePath);
QString fileName = fileInfo.fileName();
QString filePath = fileInfo.absolutePath();
dstFileName = dstPath + "/" + fileName;
QLabel *label = new QLabel(tr("从%1(<strong>%2</strong>) 到 %3(<strong>%4<strong>).")
.arg(filePath).arg(fileName).arg(dstPath).arg(fileName));
label2 = new QLabel(tr("fileCount..."));
QSpacerItem * item = new QSpacerItem(20,40,QSizePolicy::Maximum,QSizePolicy::Expanding);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(label);
layout->addWidget(label2);
layout->addItem(item);
layout->addWidget(progress);
layout->setContentsMargins(20,20,20,20);
layout->setSpacing(10);
QHBoxLayout *hLayout = new QHBoxLayout;
QSpacerItem * item2 = new QSpacerItem(40,20,QSizePolicy::Expanding,QSizePolicy::Maximum);
cancelBtn = new QToolButton(this);
cancelBtn->setText(tr("取消(&Q)"));
cancelBtn->setFixedHeight(25);
hLayout->addItem(item2);
hLayout->addWidget(cancelBtn);
layout->addLayout(hLayout);
this->setAttribute(Qt::WA_DeleteOnClose,true);
this->setFixedSize(400,150);
this->setWindowTitle(tr("复制文件"));
connect(cancelBtn,SIGNAL(clicked()),SLOT(S_closeDlg()));
}
//更新进度条
void ProgressBarDlg::S_updateProgress(qint64 _done, qint64 _total)
{
cpyThread = dynamic_cast<CopyFileThread *> (QObject::sender ());
hadDone += _done;
int value = (hadDone/(double)_total)*100;
progress->setValue(value);
}
void ProgressBarDlg::S_setFileCount(int fileDone,int fileCount)
{
label2->setText(QString::number(fileDone) + " / " +QString::number(fileCount));
if(fileDone == fileCount)
{
QTimer::singleShot(1000,this,SLOT(close()));
}
}
//关闭进度对话框
void ProgressBarDlg::S_closeDlg()
{
close();
if(QFileInfo(dstFileName).isFile())
{
QFile(dstFileName).remove(); //删除文件
}
else
{
if(!MirallTreeView::removeDir(dstFileName))
qDebug() << "12222222222222222222";
}
}
//判断是否是文件剪切
void ProgressBarDlg::S_getCutFileStat(bool _bol)
{
mirallView = dynamic_cast<MirallTreeView *> (QObject::sender ());
connect(this,SIGNAL(G_sndDirName(QFileInfo)),mirallView,SIGNAL(G_sndDirName(QFileInfo)));
b_isCutFile = _bol;
}
//进度关闭事件
void ProgressBarDlg::closeEvent(QCloseEvent *e)
{
if(cpyThread->file.isOpen())
cpyThread->file.close();
if(cpyThread->file2.isOpen())
cpyThread->file2.close();
if(cpyThread->isRunning())
cpyThread->terminate();
if(b_isCutFile)
{
if(QFileInfo(mirallView->curFielName).isFile())
{
QFile::remove(mirallView->curFielName);
}
else
{
if(MirallTreeView::removeDir(mirallView->curFielName))
{
QFileInfo fileInfor(mirallView->curFielName);
if( (fileInfor.path().endsWith("public") || fileInfor.path().endsWith("private")
|| fileInfor.path().endsWith("Shared")))
{
emit G_sndDirName(fileInfor);
}
mirallView->curFielName.clear();
}
}
}
QDialog::closeEvent(e);
}