#include "qftpuploaddlg.h"
#include "ui_qftpuploaddlg.h"
#include <QStandardItemModel>
#include <QMessageBox>
QFtpUploadDlg::QFtpUploadDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::QFtpUploadDlg)
{
ui->setupUi(this);
ui->connFtpBtn->setHidden(true); //暂不用connect按钮
m_pFtpManager = new FtpManager(this);
m_model = new QFileSystemModel;
connect(m_pFtpManager,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(OnError(QNetworkReply::NetworkError)));
connect(m_pFtpManager,SIGNAL(uploadProgress(qint64, qint64)),this,SLOT(OnUploadProgress(qint64,qint64)));
//设置要显示的根目录
m_model->setRootPath(tr("D:/"));
//设置数据源
ui->treeViewFile->setModel(m_model);
//设置标题
m_model->setHeaderData(0,Qt::Orientation::Horizontal,tr("文件名称"));
ui->treeViewFile->setColumnWidth(0,400);
ui->treeViewFile->setColumnWidth(1,200);
ui->treeViewFile->setColumnWidth(2,200);
ui->treeViewFile->setColumnWidth(3,350);
//设置树形视图的根节点
ui->treeViewFile->setRootIndex(m_model->index(tr("D:\\MyPrj")));
connect(ui->treeViewFile,&QTreeView::clicked,this,&QFtpUploadDlg::TreeClicked);
ui->progressBar->setRange(0,100);
ui->progressBar->setValue(0);
}
QFtpUploadDlg::~QFtpUploadDlg()
{
delete ui;
}
void QFtpUploadDlg::resizeDlg(int x, int y)
{
m_iWidth=x;
m_iHeigh =y;
setFixedWidth(m_iWidth);
setFixedHeight(m_iHeigh);
resizeCtrl(x,y);
}
void QFtpUploadDlg::resizeCtrl(int x, int y)
{
int ctrlWidth = x -20;
int treeViewHeidh = y - 650;
int ibtnHeigh = 80;
int iInterval = 15;
int itop =0;
ui->treeViewFile->setGeometry(10,100,ctrlWidth,treeViewHeidh);
itop= 100 + iInterval +treeViewHeidh;
ui->labSelectFile->setGeometry(10,itop,ctrlWidth,ibtnHeigh);
itop= 100 + iInterval*2 +treeViewHeidh+ibtnHeigh;
ui->progressBar->setGeometry(10,itop,ctrlWidth,ibtnHeigh);
itop= 100 + iInterval*3 +treeViewHeidh+ibtnHeigh*2;
ui->connFtpBtn->setGeometry(10,itop,ctrlWidth,ibtnHeigh);
itop= 100 + iInterval*4 +treeViewHeidh+ibtnHeigh*3;
ui->uploadFileBtn->setGeometry(10,itop,ctrlWidth,ibtnHeigh);
itop= 100 + iInterval*5 +treeViewHeidh+ibtnHeigh*4;
ui->exitBtn->setGeometry(10,itop,ctrlWidth,ibtnHeigh);
}
void QFtpUploadDlg::on_exitBtn_clicked()
{
hide();
}
void QFtpUploadDlg::on_connFtpBtn_clicked()
{
}
void QFtpUploadDlg::SetFtpLoginInfo(QString strIP, int iPort, QString strUser, QString strPsw)
{
m_strIP =strIP;
m_iPort = iPort;
m_strUser = strUser;
m_strPsw = strPsw;
}
void QFtpUploadDlg::on_uploadFileBtn_clicked()
{
if(!m_strFile.isEmpty())
{
m_pFtpManager->setHostPort(m_strIP,m_iPort);
m_pFtpManager->setUserInfo(m_strUser,m_strPsw);
m_pFtpManager->put(m_strFullName,"/test");
}
}
QString QFtpUploadDlg::getFilePath(QString filename)
{
QString strPath;
char szPath[256] ={0};
std::string str= filename.toStdString();
const char *tmpPath = str.c_str();
for(int i = filename.length(); i > 0; --i)
{
if(tmpPath[i-1] == '/')
{
memcpy(szPath,tmpPath,i-1);
break;
}
}
strPath.sprintf("%s",szPath);
return strPath;
}
void QFtpUploadDlg::TreeClicked(const QModelIndex &index)
{
//获取被点击的文件的信息并显示
if(!m_model->isDir(index))
{
m_strFullName= m_model->filePath(index);
m_strFile = m_model->fileName(index);
ui->labSelectFile->setText(m_strFile);
m_strPath ="/" +m_strFile;
//m_strPath = getFilePath(m_strFullName);
//QMessageBox::information(this,"select file",m_strPath,QMessageBox::Ok);
}else{
m_strFullName ="";
m_strFile ="";
}
}
void QFtpUploadDlg::OnError(QNetworkReply::NetworkError err)
{
if(err != QNetworkReply::NoError)
{
QString strErr = QString::asprintf("%d",err);
QMessageBox::warning(this,"ftp error",strErr);
}
}
void QFtpUploadDlg::OnUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
ui->progressBar->setRange(0,(int)bytesTotal);
ui->progressBar->setValue((int)bytesSent);
}