#include "mainwindow.h"
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QGridLayout>
#include <QScrollArea>
#include <QFileDialog>
#include <QString>
#include <QFont>
#include <QPixmap>
#include <QRegExp>
#include <QValidator>
#include <QIODevice>
#include "filesplitutils.h"
#include "filesplitconfig.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
void MainWindow::init(){
this->setWindowFlags(Qt::FramelessWindowHint); //去掉标题栏
//this->setWindowOpacity(0.8); //半透明效果
//this->setFixedSize(400,540); //设置窗口固定大小
this->setMinimumSize(CONF_WIN_WIDTH,CONF_WIN_HEIGHT); //设置窗口的最小
QWidget *centralWidget = new QWidget;
QVBoxLayout *mainLayout = new QVBoxLayout; //窗口的布局是垂直排列
mainLayout->setMargin(CONF_WIN_MARGIN); //设置边距
//上部分的布局,包括图标,标题栏,最小化按钮和关闭按钮
QHBoxLayout *topLayout=new QHBoxLayout;
topLayout->setSpacing(CONF_WIN_TOP_SPACING);
QLabel *labelIcon = new QLabel( centralWidget);
labelIcon->setFixedSize(30,30);
//label_icon->setPixmap(QPixmap::fromImage(img1));
labelIcon->setPixmap(QPixmap(":/res/res/images/split_icon.png"));
topLayout->addWidget(labelIcon);
QLabel *label_title = new QLabel(CONF_WIN_TOP_TITLE, centralWidget);
//label_title->setFont();
topLayout->addWidget(label_title);
topLayout->addStretch();
QPushButton *btn_min = new QPushButton(centralWidget);
btn_min->setFixedSize(40,30);
btn_min->setFlat(true);
btn_min->setStyleSheet("QPushButton{background-image: url(:/res/res/images/min_normal.png);}"
"QPushButton:hover{background-image: url(:/res/res/images/min_on.png);}"
"QPushButton:pressed{background-image: url(:/res/res/images/min_press.png);}");
topLayout->addWidget(btn_min);
QPushButton *btn_close = new QPushButton( centralWidget);
btn_close->setFixedSize(45,30);
btn_close->setFlat(true);
btn_close->setStyleSheet("QPushButton{background-image: url(:/res/res/images/close_normal.png);}"
"QPushButton:hover{background-image: url(:/res/res/images/close_on.png);}"
"QPushButton:pressed{background-image: url(:/res/res/images/close_press.png);}");
topLayout->addWidget(btn_close);
mainLayout->addLayout(topLayout);
//中间的布局,显示打开文件按钮和保存文件到对应目录一栏
QGridLayout *centerLayout=new QGridLayout;
centerLayout->setMargin(CONF_DEFAULT_MARGIN); //设置边距
QLabel *label_file = new QLabel(CONF_LAB_FILE_PATH, centralWidget);
centerLayout->addWidget(label_file,0,0);
mFilePathEdit=new QLineEdit;
centerLayout->addWidget(mFilePathEdit,0,1);
QPushButton *btn_open = new QPushButton(CONF_BTN_STR_OPEN, centralWidget);
centerLayout->addWidget(btn_open,0,2);
QLabel *label_dir = new QLabel(CONF_LAB_DIR_PATH, centralWidget);
centerLayout->addWidget(label_dir,1,0);
mDirPathEdit=new QLineEdit;
centerLayout->addWidget(mDirPathEdit,1,1);
QPushButton *btn_scan = new QPushButton(CONF_BTN_STR_SCAN, centralWidget);
centerLayout->addWidget(btn_scan,1,2);
QLabel *label_size = new QLabel(CONF_LAB_SPLIT_SIZE, centralWidget);
centerLayout->addWidget(label_size,2,0);
mFileSizeEdit=new QLineEdit;
QRegExp regx("[0-9]+$");
QValidator *validator = new QRegExpValidator(regx, mFileSizeEdit);
mFileSizeEdit->setValidator(validator);
centerLayout->addWidget(mFileSizeEdit,2,1);
mSizeUnit = new QComboBox;
mSizeUnit->addItem(QWidget::tr("byte"));
mSizeUnit->addItem(QWidget::tr("KB"));
mSizeUnit->addItem(QWidget::tr("MB"));
mSizeUnit->addItem(QWidget::tr("GB"));
mSizeUnit->setCurrentIndex(2); //默认是MB
centerLayout->addWidget(mSizeUnit,2,2);
mProgressBar=new QProgressBar;
mProgressBar->setOrientation(Qt::Horizontal); // 水平方向
mProgressBar->setMinimum(0);
mProgressBar->setMaximum(100);
mProgressBar->setValue(0); // 当前进度
mProgressBar->setAlignment(Qt::AlignRight | Qt::AlignVCenter); // 对齐方式
centerLayout->addWidget(mProgressBar,3,0,1,2);
mBtnStart = new QPushButton(CONF_BTN_STR_START, centralWidget);
centerLayout->addWidget(mBtnStart,3,2,1,1);
mainLayout->addLayout(centerLayout);
QVBoxLayout *bottomLayout = new QVBoxLayout;
bottomLayout->setMargin(CONF_DEFAULT_MARGIN); //设置边距
mTextEdit=new QTextEdit;
mTextEdit->setFontWeight(QFont::Normal);
bottomLayout->addWidget(mTextEdit);
mainLayout->addLayout(bottomLayout);
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
//给按钮绑定处理函数
connect(btn_min, SIGNAL(clicked()), this, SLOT(onClickMin()));
connect(btn_close, SIGNAL(clicked()), this, SLOT(onClickClose()));
connect(btn_open, SIGNAL(clicked()), this, SLOT(onClickOpen()));
connect(btn_scan, SIGNAL(clicked()), this, SLOT(onClickScan()));
connect(mBtnStart, SIGNAL(clicked()), this, SLOT(onClickStart()));
}
void MainWindow::mousePressEvent(QMouseEvent* e){ // 鼠标的单击事件
if(e->button()==Qt::LeftButton){
mPressPos=e->pos();
mPressed=true;
}
}
void MainWindow::mouseMoveEvent(QMouseEvent* e){ // 鼠标的移动事件
if(mPressed){
move(e->globalPos()-mPressPos);
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent* e){ // 鼠标的单击释放事件
mPressed=false;
}
void MainWindow::onClickMin(){
this->showMinimized();
}
void MainWindow::onClickClose(){
this->close();
}
void MainWindow::onClickOpen(){
QString filePath = QFileDialog::getOpenFileName(NULL,CONF_FILE_DIALOG_TITLE,".","*.*"); //文件选择对话框
if(!filePath.isEmpty()){
mFilePathEdit->setText(filePath);
}
}
void MainWindow::onClickScan(){
QString dirPath = QFileDialog::getExistingDirectory(NULL,CONF_DIR_DIALOG_TITLE,".");
if(!dirPath.isEmpty()){
mDirPathEdit->setText(dirPath);
}
}
void MainWindow::onClickStart(){
QString filePath=mFilePathEdit->text();
QString dirPath=mDirPathEdit->text();
QString fileSize=mFileSizeEdit->text();
int unitIndex=mSizeUnit->currentIndex();
if(!FileSplitUtils::islegalFilePath(filePath)){
mTextEdit->append(QWidget::tr("输入文件为空或者不存在。文件:").append(filePath));
return;
}
if(!FileSplitUtils::islegalDirPath(dirPath)){
mTextEdit->append(QWidget::tr("输入目录为空或者不存在。目录:").append(dirPath));
return;
}
qint64 splitSize=FileSplitUtils::calculateFileSize(fileSize,unitIndex);
if(!FileSplitUtils::islegalSplitSize(filePath,splitSize)){
mTextEdit->append(QWidget::tr("分割的文件大小不合法,请重新输入。大小:").append(QString::number(splitSize,10)));
return;
}
WorkerThread *workerThread = new WorkerThread(this);
workerThread->setWorkerParams(filePath,dirPath,splitSize);
connect(workerThread, SIGNAL(signalUpdate(int)), this, SLOT(onHandleUpdate(int)));
connect(workerThread, SIGNAL(signalAppend(const QString&)), this, SLOT(onHandleAppend(const QString&)));
// 线程结束后,自动销毁
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
}
void MainWindow::onHandleUpdate(int value){
mProgressBar->setValue(value);
if(value==0){
mBtnStart->setEnabled(false); //按钮无法点击
}else if(value==100){
mBtnStart->setEnabled(true); //按钮恢复可以点击
}
}
void MainWindow::onHandleAppend(const QString& appendText){