dialogfilecompare.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "dialogfilecompare.h"
  2. #include "ui_dialogfilecompare.h"
  3. #include "FileCompare.h"
  4. #include <QDebug>
  5. DialogFileCompare::DialogFileCompare(QWidget *parent) :
  6. QDialog(parent),
  7. ui(new Ui::DialogFileCompare)
  8. {
  9. ui->setupUi(this);
  10. threadsFinished = true;
  11. QString dir = SNCDataBase::getExportDir();
  12. if(dir.isEmpty())
  13. {
  14. this->ui->exportDirEdit->setText(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
  15. }
  16. else
  17. {
  18. this->ui->exportDirEdit->setText(dir);
  19. }
  20. this->ui->exportDirEdit->setReadOnly(true);
  21. this->ui->primaryEdit->setReadOnly(true);
  22. this->ui->augEdit->setReadOnly(true);
  23. this->ui->lblProgress->setVisible(false);
  24. movie = new QMovie(":/working.gif");
  25. this->ui->lblProgress->setMovie(movie);
  26. movie->start();
  27. emptyModel = new NumberListModel();
  28. }
  29. DialogFileCompare::~DialogFileCompare()
  30. {
  31. delete ui;
  32. delete movie;
  33. delete emptyModel;
  34. qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
  35. <<"DialogFileComapre Destroyed.";
  36. }
  37. void DialogFileCompare::closeEvent(QCloseEvent *event)
  38. {
  39. if(threadsFinished)
  40. event->accept();
  41. else
  42. {
  43. QMessageBox::information(this, tr("提示"),QString(tr("任务很快结束,请等待任务结束后再关闭本窗口!")),QMessageBox::Ok );
  44. event->ignore();
  45. }
  46. }
  47. void DialogFileCompare::on_primaryPB_clicked()
  48. {
  49. QString fileName = getOpenFileName();
  50. if (!fileName.isEmpty())
  51. {
  52. this->ui->primaryEdit->setText(fileName);
  53. }
  54. }
  55. void DialogFileCompare::on_augPB_clicked()
  56. {
  57. QString fileName = getOpenFileName();
  58. if (!fileName.isEmpty())
  59. {
  60. this->ui->augEdit->setText(fileName);
  61. }
  62. }
  63. void DialogFileCompare::on_addFilePB_clicked()
  64. {
  65. QStringList files = getOpenFileNames();
  66. for(int i=0;i<files.size();i++)
  67. {
  68. QListWidgetItem *item = new QListWidgetItem(files.at(i));
  69. ui->listWidget1->addItem(item);
  70. }
  71. }
  72. void DialogFileCompare::on_delFilePB_clicked()
  73. {
  74. QList<QListWidgetItem*> itemList = ui->listWidget1->selectedItems();
  75. QListIterator<QListWidgetItem*> it( itemList );
  76. while (it.hasNext() )
  77. {
  78. QListWidgetItem *item = it.next();
  79. int row = ui->listWidget1->row( item );
  80. ui->listWidget1->takeItem(row);
  81. }
  82. }
  83. void DialogFileCompare::on_export2PB_clicked()
  84. {
  85. if(!checkThreadFinished())
  86. return ;
  87. if(!checkFileName())
  88. return ;
  89. this->fileNames.clear();
  90. fileNames<<ui->primaryEdit->text()<<ui->augEdit->text();
  91. if(!checkCurrentSystemAvailable(fileNames))
  92. return ;
  93. this->stepFetchFileProgress = 60;
  94. this->stepCompareProgress = 40;
  95. this->ui->lblProgress->setVisible(true);
  96. allocateFetchProgress();
  97. fetchFile();
  98. compareFile(Export2File);
  99. }
  100. void DialogFileCompare::on_export3PB_clicked()
  101. {
  102. if(!checkThreadFinished())
  103. return ;
  104. if(!checkFileName())
  105. return ;
  106. this->fileNames.clear();
  107. fileNames<<ui->primaryEdit->text()<<ui->augEdit->text();
  108. if(!checkCurrentSystemAvailable(fileNames))
  109. return ;
  110. this->stepFetchFileProgress = 60;
  111. this->stepCompareProgress = 40;
  112. this->ui->lblProgress->setVisible(true);
  113. allocateFetchProgress();
  114. fetchFile();//这里虽然是线程在执行,但要同步等待所有的线程结束后,才能进行文件比较
  115. compareFile(Export3File);
  116. }
  117. void DialogFileCompare::fetchFile()
  118. {
  119. for(int i=0;i<modelList.size();i++)
  120. {
  121. if(modelList[i])
  122. {
  123. modelList[i]->clear();
  124. delete modelList[i];
  125. }
  126. }
  127. modelList.clear();
  128. this->filesFetced = 0;
  129. for(int i=0;i<fileNames.size();i++)
  130. {
  131. ImportProxy *proxy = new ImportProxy(emptyModel);
  132. QStringList files;
  133. files<<fileNames.at(i);
  134. proxy->setFetchList(files);
  135. proxy->setBuildType(NumberFromFileImport);
  136. QObject::connect(proxy,SIGNAL(numberBuilt(NumberListModel*)),this,SLOT(onNumberBuilt(NumberListModel*)));
  137. QObject::connect(proxy,SIGNAL(progressMaxUpdated(int)),this,SLOT(onFetchFileProgressMaxUpdated(int)));
  138. QObject::connect(proxy,SIGNAL(progressUpdated(int)),this,SLOT(onFetchFileProgressUpdated(int)));
  139. proxy->start();
  140. loop.exec();//等待
  141. proxy->wait();
  142. delete proxy;
  143. proxy = 0;
  144. }
  145. }
  146. void DialogFileCompare::onNumberBuilt(NumberListModel *model)
  147. {
  148. modelList.append(model);
  149. this->filesFetced++;
  150. loop.quit();
  151. }
  152. void DialogFileCompare::compareFile(FileCompareType compareType)
  153. {
  154. FileCompare *compare = new FileCompare(fileNames,compareType);
  155. compare->setModel(this->modelList);
  156. compare->setExportDir(this->ui->exportDirEdit->text());
  157. QObject::connect(compare,SIGNAL(progressUpdated(int)),this,SLOT(onCompareProgressUpdated(int)));
  158. QObject::connect(compare,SIGNAL(progressMaxUpdated(int)),this,SLOT(onCompareFileProgressMaxUpdated(int)));
  159. QObject::connect(compare,SIGNAL(fileCompared(QStringList)),this,SLOT(onFileCompared(QStringList)));
  160. compare->start();
  161. loop.exec();
  162. compare->wait();
  163. delete compare;
  164. compare = 0;
  165. }
  166. void DialogFileCompare::onFileCompared(QStringList resultList)
  167. {
  168. loop.quit();
  169. for(int i=0;i<resultList.size();i++)
  170. {
  171. QListWidgetItem *item = new QListWidgetItem(resultList.at(i));
  172. ui->listWidget2->addItem(item);
  173. }
  174. for(int i=0;i<modelList.size();i++)
  175. {
  176. if(modelList[i])
  177. {
  178. modelList[i]->clear();
  179. delete modelList[i];
  180. }
  181. }
  182. modelList.clear();
  183. this->ui->progressBar->setValue(0);
  184. this->ui->lblProgress->setVisible(false);
  185. threadsFinished = true;
  186. QMessageBox::information(this,"结果","文件比对完成。", QMessageBox::Ok);
  187. }
  188. void DialogFileCompare::on_integrationPB_clicked()
  189. {
  190. if(!checkThreadFinished())
  191. return ;
  192. this->fileNames.clear();
  193. for(int i=0;i<ui->listWidget1->count();i++)
  194. {
  195. fileNames<<ui->listWidget1->item(i)->text();
  196. }
  197. if(fileNames.isEmpty())
  198. {
  199. threadsFinished = true ;
  200. return ;
  201. }
  202. if(!checkCurrentSystemAvailable(fileNames))
  203. return ;
  204. this->stepFetchFileProgress = 70;
  205. this->stepIntegrateProgress = 30;
  206. this->ui->lblProgress->setVisible(true);
  207. integrateFile();
  208. FileCompare *compare = new FileCompare(fileNames,IntegrateFile);
  209. compare->setModel(this->modelList);
  210. compare->setExportDir(this->ui->exportDirEdit->text());
  211. QObject::connect(compare,SIGNAL(progressUpdated(int)),this,SLOT(onCompareProgressUpdated(int)));
  212. QObject::connect(compare,SIGNAL(progressMaxUpdated(int)),this,SLOT(onCompareFileProgressMaxUpdated(int)));
  213. QObject::connect(compare,SIGNAL(fileIntegrated(QStringList)),this,SLOT(onFileIntegrated(QStringList)));
  214. compare->start();
  215. loop.exec();
  216. compare->wait();
  217. delete compare;
  218. compare = 0;
  219. }
  220. void DialogFileCompare::integrateFile()
  221. {
  222. for(int i=0;i<modelList.size();i++)
  223. {
  224. if(modelList[i])
  225. {
  226. modelList[i]->clear();
  227. delete modelList[i];
  228. }
  229. }
  230. modelList.clear();
  231. this->filesFetced = 0;
  232. ImportProxy *proxy = new ImportProxy(emptyModel);
  233. proxy->setFetchList(fileNames);
  234. proxy->setBuildType(NumberFromFileImport);
  235. QObject::connect(proxy,SIGNAL(numberBuilt(NumberListModel*)),this,SLOT(onNumberBuilt(NumberListModel*)));
  236. QObject::connect(proxy,SIGNAL(progressMaxUpdated(int)),this,SLOT(onProgressMaxUpdated(int)));
  237. QObject::connect(proxy,SIGNAL(progressUpdated(int)),this,SLOT(onProgressUpdated(int)));
  238. proxy->start();
  239. loop.exec();
  240. proxy->wait();
  241. delete proxy;
  242. proxy = 0;
  243. }
  244. void DialogFileCompare::onFileIntegrated(QStringList resultList)
  245. {
  246. loop.quit();
  247. for(int i=0;i<resultList.size();i++)
  248. {
  249. QListWidgetItem *item = new QListWidgetItem(resultList.at(i));
  250. ui->listWidget2->addItem(item);
  251. }
  252. for(int i=0;i<modelList.size();i++)
  253. {
  254. if(modelList[i])
  255. {
  256. modelList[i]->clear();
  257. delete modelList[i];
  258. }
  259. }
  260. modelList.clear();
  261. threadsFinished = true;
  262. this->ui->progressBar->setValue(0);
  263. this->ui->lblProgress->setVisible(false);
  264. QMessageBox::information(this,"结果","文件整合完成!", QMessageBox::Ok);
  265. }
  266. QStringList DialogFileCompare::getOpenFileNames()
  267. {
  268. QFileDialog::Options options;
  269. QString selectedFilter;
  270. QString dir = SNCDataBase::getImportDir();
  271. if(dir.isEmpty())
  272. {
  273. dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
  274. }
  275. QStringList files;
  276. files = QFileDialog::getOpenFileNames(this,"打开",dir,
  277. tr("Text Files (*.txt)"),&selectedFilter,options);
  278. if(!files.isEmpty())
  279. {
  280. QFileInfo fileInfo(files.at(0));
  281. dir = fileInfo.absolutePath();
  282. SNCDataBase::updateImportDir(dir);
  283. }
  284. return files;
  285. }
  286. bool DialogFileCompare::checkThreadFinished()
  287. {
  288. if(!threadsFinished)
  289. {
  290. QMessageBox::information(this,"提示","当前任务暂未结束,请等待当前任务结束后再执行下一操作",
  291. QMessageBox::Ok);
  292. return false;
  293. }
  294. threadsFinished = false;
  295. return true;
  296. }
  297. bool DialogFileCompare::checkFileName()
  298. {
  299. if(ui->primaryEdit->text().isEmpty()|| ui->augEdit->text().isEmpty())
  300. {
  301. QMessageBox::critical(this,"提示","要比较的文件不能为空!",QMessageBox::Ok);
  302. threadsFinished = true;
  303. return false ;
  304. }
  305. if(ui->primaryEdit->text()==ui->augEdit->text())
  306. {
  307. QMessageBox::critical(this,"提示","要比较的文件必须是不同的文件!",QMessageBox::Ok);
  308. threadsFinished = true;
  309. return false ;
  310. }
  311. return true;
  312. }
  313. void DialogFileCompare::on_joinButton_clicked()
  314. {
  315. if(!checkThreadFinished())
  316. return ;
  317. QList<QListWidgetItem*> itemList = ui->listWidget2->selectedItems();
  318. QStringList files;
  319. for(int i=0;i<itemList.size();i++)
  320. {
  321. files.append(itemList.at(i)->text());
  322. }
  323. if(files.isEmpty())
  324. {
  325. threadsFinished = true ;
  326. return ;
  327. }
  328. if(!checkCurrentSystemAvailable(files))
  329. return ;
  330. emit joinSource(files);
  331. threadsFinished = true;
  332. this->close();
  333. }
  334. bool DialogFileCompare::checkCurrentSystemAvailable(QStringList files)
  335. {
  336. int count=0;
  337. int all_files_size=0;
  338. for(int i=0;i<files.size();i++)
  339. {
  340. QFileInfo fileinfo(files.at(i));
  341. all_files_size+=fileinfo.size();
  342. }
  343. count =all_files_size/1024/127;
  344. if(avaiableCount+count>1500)
  345. {
  346. threadsFinished = true;
  347. QMessageBox::critical(this,"提示","您要处理的号码已经超过系统处理能力!",QMessageBox::Ok);
  348. return false;
  349. }
  350. else
  351. {
  352. return true;
  353. }
  354. }
  355. void DialogFileCompare::setAvailableCount(int count)
  356. {
  357. this->avaiableCount = count;
  358. }
  359. void DialogFileCompare::onProgressUpdated(int current)
  360. {
  361. this->ui->progressBar->setValue(current);
  362. }
  363. void DialogFileCompare::onProgressMaxUpdated(int max)
  364. {
  365. this->ui->progressBar->setMaximum(max);
  366. this->ui->progressBar->setMinimum(0);
  367. this->ui->progressBar->setValue(0);
  368. }
  369. void DialogFileCompare::onCompareFileProgressMaxUpdated(int max)
  370. {
  371. this->compareProgressMax = max;
  372. }
  373. void DialogFileCompare::onCompareProgressUpdated(int current)
  374. {
  375. double s1 = current;
  376. double s2 = this->compareProgressMax;
  377. this->ui->progressBar->setValue(this->stepFetchFileProgress + this->stepCompareProgress * s1/s2);
  378. }
  379. void DialogFileCompare::onIntegrateFileProgressMaxUpdated(int max)
  380. {
  381. this->integrateProgressMax = max;
  382. }
  383. void DialogFileCompare::onIntegrateProgressUpdated(int current)
  384. {
  385. double s1 = current;
  386. double s2 = this->integrateProgressMax;
  387. this->ui->progressBar->setValue(this->stepFetchFileProgress + this->stepIntegrateProgress * s1/s2);
  388. }
  389. void DialogFileCompare::onImportFileProgressMaxUpdated(int max)
  390. {
  391. this->fetchFileProgressMax = max;
  392. }
  393. void DialogFileCompare::onImportFileProgressUpdated(int current)
  394. {
  395. double s1 = current;
  396. double s2 = this->fetchFileProgressMax;
  397. this->ui->progressBar->setValue(this->stepFetchFileProgress * s1/s2);
  398. }
  399. void DialogFileCompare::onFetchFileProgressMaxUpdated(int max)
  400. {
  401. this->current_file_max = max;
  402. }
  403. void DialogFileCompare::onFetchFileProgressUpdated(int current)
  404. {
  405. int last_file_progress = 0;
  406. for(int i=0;i<this->filesFetced;i++)
  407. {
  408. last_file_progress += fetchProgress[i];
  409. }
  410. double s1 = current;
  411. double s2 = this->current_file_max;
  412. this->ui->progressBar->setValue(last_file_progress+fetchProgress[filesFetced]*s1/s2);
  413. }
  414. void DialogFileCompare::allocateFetchProgress()
  415. {
  416. double total_file_size = 0;
  417. fetchProgress.clear();
  418. for(int i=0;i<fileNames.size();i++)
  419. {
  420. QFileInfo fileinfo(this->fileNames.at(i));
  421. double cur_file_size = fileinfo.size();
  422. total_file_size += cur_file_size;
  423. }
  424. for(int i=0;i<fileNames.size();i++)
  425. {
  426. QFileInfo fileinfo(this->fileNames.at(i));
  427. double cur_file_size = fileinfo.size();
  428. fetchProgress.append(this->stepFetchFileProgress*cur_file_size/total_file_size);
  429. }
  430. }
  431. void DialogFileCompare::on_setDirPB_clicked()
  432. {
  433. QFileDialog::Options options = QFileDialog::ShowDirsOnly;
  434. QString dir;
  435. dir = QFileDialog::getExistingDirectory(this,"设定目录",this->ui->exportDirEdit->text(),options);
  436. if(!dir.isEmpty())
  437. {
  438. this->ui->exportDirEdit->setText(dir);
  439. SNCDataBase::updateExportDir(dir);
  440. }
  441. }
  442. QString DialogFileCompare::getSaveFileName()
  443. {
  444. QFileDialog::Options options;
  445. QString selectedFilter;
  446. QString dir = SNCDataBase::getExportDir();
  447. if(dir.isEmpty())
  448. {
  449. dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
  450. }
  451. QString fileName = QFileDialog::getSaveFileName(this,
  452. tr("保存"),dir,
  453. tr("Text Files (*.txt)"),
  454. &selectedFilter,
  455. options);
  456. if(!fileName.isEmpty())
  457. {
  458. QFileInfo fileInfo(fileName);
  459. dir = fileInfo.absolutePath();
  460. SNCDataBase::updateExportDir(dir);
  461. }
  462. return fileName;
  463. }
  464. QString DialogFileCompare::getOpenFileName()
  465. {
  466. QFileDialog::Options options;
  467. QString selectedFilter;
  468. QString dir = SNCDataBase::getImportDir();
  469. if(dir.isEmpty())
  470. {
  471. dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
  472. }
  473. QString fileName = QFileDialog::getOpenFileName(this,
  474. tr("打开"),dir,
  475. tr("Text Files (*.txt)"),
  476. &selectedFilter,
  477. options);
  478. if(!fileName.isEmpty())
  479. {
  480. QFileInfo fileInfo(fileName);
  481. dir = fileInfo.absolutePath();
  482. SNCDataBase::updateImportDir(dir);
  483. }
  484. return fileName;
  485. }