| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- #include "dialogfilecompare.h"
- #include "ui_dialogfilecompare.h"
- #include "FileCompare.h"
- #include <QDebug>
- DialogFileCompare::DialogFileCompare(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::DialogFileCompare)
- {
- ui->setupUi(this);
- threadsFinished = true;
- QString dir = SNCDataBase::getExportDir();
- if(dir.isEmpty())
- {
- this->ui->exportDirEdit->setText(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
- }
- else
- {
- this->ui->exportDirEdit->setText(dir);
- }
- this->ui->exportDirEdit->setReadOnly(true);
- this->ui->primaryEdit->setReadOnly(true);
- this->ui->augEdit->setReadOnly(true);
- this->ui->lblProgress->setVisible(false);
- movie = new QMovie(":/working.gif");
- this->ui->lblProgress->setMovie(movie);
- movie->start();
- emptyModel = new NumberListModel();
- }
- DialogFileCompare::~DialogFileCompare()
- {
- delete ui;
- delete movie;
- delete emptyModel;
- qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
- <<"DialogFileComapre Destroyed.";
- }
- void DialogFileCompare::closeEvent(QCloseEvent *event)
- {
- if(threadsFinished)
- event->accept();
- else
- {
- QMessageBox::information(this, tr("提示"),QString(tr("任务很快结束,请等待任务结束后再关闭本窗口!")),QMessageBox::Ok );
- event->ignore();
- }
- }
- void DialogFileCompare::on_primaryPB_clicked()
- {
- QString fileName = getOpenFileName();
- if (!fileName.isEmpty())
- {
- this->ui->primaryEdit->setText(fileName);
- }
- }
- void DialogFileCompare::on_augPB_clicked()
- {
- QString fileName = getOpenFileName();
- if (!fileName.isEmpty())
- {
- this->ui->augEdit->setText(fileName);
- }
- }
- void DialogFileCompare::on_addFilePB_clicked()
- {
- QStringList files = getOpenFileNames();
- for(int i=0;i<files.size();i++)
- {
- QListWidgetItem *item = new QListWidgetItem(files.at(i));
- ui->listWidget1->addItem(item);
- }
- }
- void DialogFileCompare::on_delFilePB_clicked()
- {
- QList<QListWidgetItem*> itemList = ui->listWidget1->selectedItems();
- QListIterator<QListWidgetItem*> it( itemList );
- while (it.hasNext() )
- {
- QListWidgetItem *item = it.next();
- int row = ui->listWidget1->row( item );
- ui->listWidget1->takeItem(row);
- }
- }
- void DialogFileCompare::on_export2PB_clicked()
- {
- if(!checkThreadFinished())
- return ;
- if(!checkFileName())
- return ;
- this->fileNames.clear();
- fileNames<<ui->primaryEdit->text()<<ui->augEdit->text();
- if(!checkCurrentSystemAvailable(fileNames))
- return ;
- this->stepFetchFileProgress = 60;
- this->stepCompareProgress = 40;
- this->ui->lblProgress->setVisible(true);
- allocateFetchProgress();
- fetchFile();
- compareFile(Export2File);
- }
- void DialogFileCompare::on_export3PB_clicked()
- {
- if(!checkThreadFinished())
- return ;
- if(!checkFileName())
- return ;
- this->fileNames.clear();
- fileNames<<ui->primaryEdit->text()<<ui->augEdit->text();
- if(!checkCurrentSystemAvailable(fileNames))
- return ;
- this->stepFetchFileProgress = 60;
- this->stepCompareProgress = 40;
- this->ui->lblProgress->setVisible(true);
- allocateFetchProgress();
- fetchFile();//这里虽然是线程在执行,但要同步等待所有的线程结束后,才能进行文件比较
- compareFile(Export3File);
- }
- void DialogFileCompare::fetchFile()
- {
- for(int i=0;i<modelList.size();i++)
- {
- if(modelList[i])
- {
- modelList[i]->clear();
- delete modelList[i];
- }
- }
- modelList.clear();
- this->filesFetced = 0;
- for(int i=0;i<fileNames.size();i++)
- {
- ImportProxy *proxy = new ImportProxy(emptyModel);
- QStringList files;
- files<<fileNames.at(i);
- proxy->setFetchList(files);
- proxy->setBuildType(NumberFromFileImport);
- QObject::connect(proxy,SIGNAL(numberBuilt(NumberListModel*)),this,SLOT(onNumberBuilt(NumberListModel*)));
- QObject::connect(proxy,SIGNAL(progressMaxUpdated(int)),this,SLOT(onFetchFileProgressMaxUpdated(int)));
- QObject::connect(proxy,SIGNAL(progressUpdated(int)),this,SLOT(onFetchFileProgressUpdated(int)));
- proxy->start();
- loop.exec();//等待
- proxy->wait();
- delete proxy;
- proxy = 0;
- }
- }
- void DialogFileCompare::onNumberBuilt(NumberListModel *model)
- {
- modelList.append(model);
- this->filesFetced++;
- loop.quit();
- }
- void DialogFileCompare::compareFile(FileCompareType compareType)
- {
- FileCompare *compare = new FileCompare(fileNames,compareType);
- compare->setModel(this->modelList);
- compare->setExportDir(this->ui->exportDirEdit->text());
- QObject::connect(compare,SIGNAL(progressUpdated(int)),this,SLOT(onCompareProgressUpdated(int)));
- QObject::connect(compare,SIGNAL(progressMaxUpdated(int)),this,SLOT(onCompareFileProgressMaxUpdated(int)));
- QObject::connect(compare,SIGNAL(fileCompared(QStringList)),this,SLOT(onFileCompared(QStringList)));
- compare->start();
- loop.exec();
- compare->wait();
- delete compare;
- compare = 0;
- }
- void DialogFileCompare::onFileCompared(QStringList resultList)
- {
- loop.quit();
- for(int i=0;i<resultList.size();i++)
- {
- QListWidgetItem *item = new QListWidgetItem(resultList.at(i));
- ui->listWidget2->addItem(item);
- }
- for(int i=0;i<modelList.size();i++)
- {
- if(modelList[i])
- {
- modelList[i]->clear();
- delete modelList[i];
- }
- }
- modelList.clear();
- this->ui->progressBar->setValue(0);
- this->ui->lblProgress->setVisible(false);
- threadsFinished = true;
- QMessageBox::information(this,"结果","文件比对完成。", QMessageBox::Ok);
- }
- void DialogFileCompare::on_integrationPB_clicked()
- {
- if(!checkThreadFinished())
- return ;
- this->fileNames.clear();
- for(int i=0;i<ui->listWidget1->count();i++)
- {
- fileNames<<ui->listWidget1->item(i)->text();
- }
- if(fileNames.isEmpty())
- {
- threadsFinished = true ;
- return ;
- }
- if(!checkCurrentSystemAvailable(fileNames))
- return ;
- this->stepFetchFileProgress = 70;
- this->stepIntegrateProgress = 30;
- this->ui->lblProgress->setVisible(true);
- integrateFile();
- FileCompare *compare = new FileCompare(fileNames,IntegrateFile);
- compare->setModel(this->modelList);
- compare->setExportDir(this->ui->exportDirEdit->text());
- QObject::connect(compare,SIGNAL(progressUpdated(int)),this,SLOT(onCompareProgressUpdated(int)));
- QObject::connect(compare,SIGNAL(progressMaxUpdated(int)),this,SLOT(onCompareFileProgressMaxUpdated(int)));
- QObject::connect(compare,SIGNAL(fileIntegrated(QStringList)),this,SLOT(onFileIntegrated(QStringList)));
- compare->start();
- loop.exec();
- compare->wait();
- delete compare;
- compare = 0;
- }
- void DialogFileCompare::integrateFile()
- {
- for(int i=0;i<modelList.size();i++)
- {
- if(modelList[i])
- {
- modelList[i]->clear();
- delete modelList[i];
- }
- }
- modelList.clear();
- this->filesFetced = 0;
- ImportProxy *proxy = new ImportProxy(emptyModel);
- proxy->setFetchList(fileNames);
- proxy->setBuildType(NumberFromFileImport);
- QObject::connect(proxy,SIGNAL(numberBuilt(NumberListModel*)),this,SLOT(onNumberBuilt(NumberListModel*)));
- QObject::connect(proxy,SIGNAL(progressMaxUpdated(int)),this,SLOT(onProgressMaxUpdated(int)));
- QObject::connect(proxy,SIGNAL(progressUpdated(int)),this,SLOT(onProgressUpdated(int)));
- proxy->start();
- loop.exec();
- proxy->wait();
- delete proxy;
- proxy = 0;
- }
- void DialogFileCompare::onFileIntegrated(QStringList resultList)
- {
- loop.quit();
- for(int i=0;i<resultList.size();i++)
- {
- QListWidgetItem *item = new QListWidgetItem(resultList.at(i));
- ui->listWidget2->addItem(item);
- }
- for(int i=0;i<modelList.size();i++)
- {
- if(modelList[i])
- {
- modelList[i]->clear();
- delete modelList[i];
- }
- }
- modelList.clear();
- threadsFinished = true;
- this->ui->progressBar->setValue(0);
- this->ui->lblProgress->setVisible(false);
- QMessageBox::information(this,"结果","文件整合完成!", QMessageBox::Ok);
- }
- QStringList DialogFileCompare::getOpenFileNames()
- {
- QFileDialog::Options options;
- QString selectedFilter;
- QString dir = SNCDataBase::getImportDir();
- if(dir.isEmpty())
- {
- dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
- }
- QStringList files;
- files = QFileDialog::getOpenFileNames(this,"打开",dir,
- tr("Text Files (*.txt)"),&selectedFilter,options);
- if(!files.isEmpty())
- {
- QFileInfo fileInfo(files.at(0));
- dir = fileInfo.absolutePath();
- SNCDataBase::updateImportDir(dir);
- }
- return files;
- }
- bool DialogFileCompare::checkThreadFinished()
- {
- if(!threadsFinished)
- {
- QMessageBox::information(this,"提示","当前任务暂未结束,请等待当前任务结束后再执行下一操作",
- QMessageBox::Ok);
- return false;
- }
- threadsFinished = false;
- return true;
- }
- bool DialogFileCompare::checkFileName()
- {
- if(ui->primaryEdit->text().isEmpty()|| ui->augEdit->text().isEmpty())
- {
- QMessageBox::critical(this,"提示","要比较的文件不能为空!",QMessageBox::Ok);
- threadsFinished = true;
- return false ;
- }
- if(ui->primaryEdit->text()==ui->augEdit->text())
- {
- QMessageBox::critical(this,"提示","要比较的文件必须是不同的文件!",QMessageBox::Ok);
- threadsFinished = true;
- return false ;
- }
- return true;
- }
- void DialogFileCompare::on_joinButton_clicked()
- {
- if(!checkThreadFinished())
- return ;
- QList<QListWidgetItem*> itemList = ui->listWidget2->selectedItems();
- QStringList files;
- for(int i=0;i<itemList.size();i++)
- {
- files.append(itemList.at(i)->text());
- }
- if(files.isEmpty())
- {
- threadsFinished = true ;
- return ;
- }
- if(!checkCurrentSystemAvailable(files))
- return ;
- emit joinSource(files);
- threadsFinished = true;
- this->close();
- }
- bool DialogFileCompare::checkCurrentSystemAvailable(QStringList files)
- {
- int count=0;
- int all_files_size=0;
- for(int i=0;i<files.size();i++)
- {
- QFileInfo fileinfo(files.at(i));
- all_files_size+=fileinfo.size();
- }
- count =all_files_size/1024/127;
- if(avaiableCount+count>1500)
- {
- threadsFinished = true;
- QMessageBox::critical(this,"提示","您要处理的号码已经超过系统处理能力!",QMessageBox::Ok);
- return false;
- }
- else
- {
- return true;
- }
- }
- void DialogFileCompare::setAvailableCount(int count)
- {
- this->avaiableCount = count;
- }
- void DialogFileCompare::onProgressUpdated(int current)
- {
- this->ui->progressBar->setValue(current);
- }
- void DialogFileCompare::onProgressMaxUpdated(int max)
- {
- this->ui->progressBar->setMaximum(max);
- this->ui->progressBar->setMinimum(0);
- this->ui->progressBar->setValue(0);
- }
- void DialogFileCompare::onCompareFileProgressMaxUpdated(int max)
- {
- this->compareProgressMax = max;
- }
- void DialogFileCompare::onCompareProgressUpdated(int current)
- {
- double s1 = current;
- double s2 = this->compareProgressMax;
- this->ui->progressBar->setValue(this->stepFetchFileProgress + this->stepCompareProgress * s1/s2);
- }
- void DialogFileCompare::onIntegrateFileProgressMaxUpdated(int max)
- {
- this->integrateProgressMax = max;
- }
- void DialogFileCompare::onIntegrateProgressUpdated(int current)
- {
- double s1 = current;
- double s2 = this->integrateProgressMax;
- this->ui->progressBar->setValue(this->stepFetchFileProgress + this->stepIntegrateProgress * s1/s2);
- }
- void DialogFileCompare::onImportFileProgressMaxUpdated(int max)
- {
- this->fetchFileProgressMax = max;
- }
- void DialogFileCompare::onImportFileProgressUpdated(int current)
- {
- double s1 = current;
- double s2 = this->fetchFileProgressMax;
- this->ui->progressBar->setValue(this->stepFetchFileProgress * s1/s2);
- }
- void DialogFileCompare::onFetchFileProgressMaxUpdated(int max)
- {
- this->current_file_max = max;
- }
- void DialogFileCompare::onFetchFileProgressUpdated(int current)
- {
- int last_file_progress = 0;
- for(int i=0;i<this->filesFetced;i++)
- {
- last_file_progress += fetchProgress[i];
- }
- double s1 = current;
- double s2 = this->current_file_max;
- this->ui->progressBar->setValue(last_file_progress+fetchProgress[filesFetced]*s1/s2);
- }
- void DialogFileCompare::allocateFetchProgress()
- {
- double total_file_size = 0;
- fetchProgress.clear();
- for(int i=0;i<fileNames.size();i++)
- {
- QFileInfo fileinfo(this->fileNames.at(i));
- double cur_file_size = fileinfo.size();
- total_file_size += cur_file_size;
- }
- for(int i=0;i<fileNames.size();i++)
- {
- QFileInfo fileinfo(this->fileNames.at(i));
- double cur_file_size = fileinfo.size();
- fetchProgress.append(this->stepFetchFileProgress*cur_file_size/total_file_size);
- }
- }
- void DialogFileCompare::on_setDirPB_clicked()
- {
- QFileDialog::Options options = QFileDialog::ShowDirsOnly;
- QString dir;
- dir = QFileDialog::getExistingDirectory(this,"设定目录",this->ui->exportDirEdit->text(),options);
- if(!dir.isEmpty())
- {
- this->ui->exportDirEdit->setText(dir);
- SNCDataBase::updateExportDir(dir);
- }
- }
- QString DialogFileCompare::getSaveFileName()
- {
- QFileDialog::Options options;
- QString selectedFilter;
- QString dir = SNCDataBase::getExportDir();
- if(dir.isEmpty())
- {
- dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
- }
- QString fileName = QFileDialog::getSaveFileName(this,
- tr("保存"),dir,
- tr("Text Files (*.txt)"),
- &selectedFilter,
- options);
- if(!fileName.isEmpty())
- {
- QFileInfo fileInfo(fileName);
- dir = fileInfo.absolutePath();
- SNCDataBase::updateExportDir(dir);
- }
- return fileName;
- }
- QString DialogFileCompare::getOpenFileName()
- {
- QFileDialog::Options options;
- QString selectedFilter;
- QString dir = SNCDataBase::getImportDir();
- if(dir.isEmpty())
- {
- dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
- }
- QString fileName = QFileDialog::getOpenFileName(this,
- tr("打开"),dir,
- tr("Text Files (*.txt)"),
- &selectedFilter,
- options);
- if(!fileName.isEmpty())
- {
- QFileInfo fileInfo(fileName);
- dir = fileInfo.absolutePath();
- SNCDataBase::updateImportDir(dir);
- }
- return fileName;
- }
|