From 2dc2a133919068829076314972b6e923aeb33554 Mon Sep 17 00:00:00 2001
From: Olivier Langella <Olivier.Langella@moulon.inra.fr>
Date: Sat, 10 Jun 2017 09:00:24 +0200
Subject: [PATCH] first working progress bar

---
 src/core/project.cpp                                 |  4 ++--
 src/core/project.h                                   |  3 ++-
 src/files/xpipfile.cpp                               |  4 ++--
 src/files/xpipfile.h                                 |  3 ++-
 .../waiting_message_dialog/waitingmessagedialog.cpp  |  1 +
 src/gui/workerthread.cpp                             |  2 +-
 src/input/xpipsaxhandler.cpp                         | 10 +++++++---
 src/input/xpipsaxhandler.h                           |  7 +++++--
 src/utils/workmonitor.cpp                            | 12 ++++++++++--
 src/utils/workmonitor.h                              |  8 ++++++--
 10 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/src/core/project.cpp b/src/core/project.cpp
index 925d5cc2..cb5d7b6a 100644
--- a/src/core/project.cpp
+++ b/src/core/project.cpp
@@ -107,10 +107,10 @@ IdentificationGroup* Project::newIdentificationGroup() {
     _identification_goup_list.push_back(_p_current_identification_group);
     return _p_current_identification_group;
 }
-void Project::readXpipFile(QFileInfo xpip_fileinfo) {
+void Project::readXpipFile(WorkMonitorInterface * p_monitor, QFileInfo xpip_fileinfo) {
     qDebug() << "Project::readXpipFile begin";
 
-    XpipSaxHandler * parser = new XpipSaxHandler(this);
+    XpipSaxHandler * parser = new XpipSaxHandler(p_monitor, this);
 
     QXmlSimpleReader simplereader;
     simplereader.setContentHandler(parser);
diff --git a/src/core/project.h b/src/core/project.h
index d2903c89..d42647d3 100644
--- a/src/core/project.h
+++ b/src/core/project.h
@@ -31,6 +31,7 @@
 #include "../utils/proteinstore.h"
 #include "../utils/identificationdatasourcestore.h"
 #include "../utils/msrunstore.h"
+#include "../utils/workmonitor.h"
 #include "labeling/labelingmethod.h"
 
 class Project;
@@ -53,7 +54,7 @@ public:
     FastaFileStore & getFastaFileStore();
     IdentificationDataSourceStore & getIdentificationDataSourceStore();
     const IdentificationDataSourceStore & getIdentificationDataSourceStore() const;
-    void readXpipFile(QFileInfo xpip_source);
+    void readXpipFile(WorkMonitorInterface * p_monitor, QFileInfo xpip_source);
     IdentificationGroup* newIdentificationGroup();
 
     /** @brief validate or invalidate peptides and proteins based automatic filters and manual checks
diff --git a/src/files/xpipfile.cpp b/src/files/xpipfile.cpp
index ef650971..e433e0b4 100644
--- a/src/files/xpipfile.cpp
+++ b/src/files/xpipfile.cpp
@@ -40,10 +40,10 @@ XpipFile::~XpipFile()
 
 }
 
-ProjectSp XpipFile::getProjectSp() const {
+ProjectSp XpipFile::getProjectSp(WorkMonitorInterface * p_monitor) const {
     ProjectSp project_sp = Project().makeProjectSp();
 
-    project_sp.get()->readXpipFile(QFileInfo(_xpip_source.toLocalFile()));
+    project_sp.get()->readXpipFile(p_monitor, QFileInfo(_xpip_source.toLocalFile()));
 
     return (project_sp);
 
diff --git a/src/files/xpipfile.h b/src/files/xpipfile.h
index 12ee8cff..566071a1 100644
--- a/src/files/xpipfile.h
+++ b/src/files/xpipfile.h
@@ -25,6 +25,7 @@
 #include <QUrl>
 #include <QFileInfo>
 #include "../core/project.h"
+#include "../utils/workmonitor.h"
 
 class XpipFile
 {
@@ -34,7 +35,7 @@ public:
     XpipFile(const XpipFile & other);
     ~XpipFile();
     
-    ProjectSp getProjectSp() const;
+    ProjectSp getProjectSp(WorkMonitorInterface * p_monitor) const;
 
 private :
     const QUrl _xpip_source;
diff --git a/src/gui/waiting_message_dialog/waitingmessagedialog.cpp b/src/gui/waiting_message_dialog/waitingmessagedialog.cpp
index 838cdce6..146785f5 100644
--- a/src/gui/waiting_message_dialog/waitingmessagedialog.cpp
+++ b/src/gui/waiting_message_dialog/waitingmessagedialog.cpp
@@ -61,6 +61,7 @@ void WaitingMessageDialog::message(const QString & message) {
 }
 
 void WaitingMessageDialog::message(const QString & message, int progress_value) {
+    qDebug() << "WaitingMessageDialog::message " << progress_value;
     ui->progress_bar->setVisible(true);
     ui->progress_bar->setValue(progress_value);
     ui->message_label->setText(message);
diff --git a/src/gui/workerthread.cpp b/src/gui/workerthread.cpp
index ca3fa705..8e179cd4 100644
--- a/src/gui/workerthread.cpp
+++ b/src/gui/workerthread.cpp
@@ -126,7 +126,7 @@ void WorkerThread::doXpipFileLoad(QString filename) {
         emit loadingMessage(tr("loading XPIP file"));
 
         XpipFile xpip_file(new_xpip_file);
-        ProjectSp project_sp = xpip_file.getProjectSp();
+        ProjectSp project_sp = xpip_file.getProjectSp(_p_work_monitor);
 
         emit projectReady(project_sp);
 
diff --git a/src/input/xpipsaxhandler.cpp b/src/input/xpipsaxhandler.cpp
index f6d4affe..73685984 100644
--- a/src/input/xpipsaxhandler.cpp
+++ b/src/input/xpipsaxhandler.cpp
@@ -26,9 +26,9 @@
 #include "../utils/peptidestore.h"
 #include "../utils/proteinstore.h"
 
-XpipSaxHandler::XpipSaxHandler(Project * p_project):_p_project(p_project)
+XpipSaxHandler::XpipSaxHandler(WorkMonitorInterface * p_monitor, Project * p_project):_p_project(p_project)
 {
-
+    _p_monitor = p_monitor;
 }
 
 XpipSaxHandler::~XpipSaxHandler()
@@ -155,8 +155,9 @@ bool XpipSaxHandler::startElement_information(QXmlAttributes attributes) {
     if (attributes.value("Data_Type").simplified() == "indiv") {
       _p_project->setCombineMode(false);
     }
-    _count_protein_match = attributes.value("match_number").toUInt();
+    _total_protein_match = attributes.value("match_number").toUInt();
     
+    _p_monitor->setProgressMaximumValue(_total_protein_match);
     qDebug() << "startElement_information end" ;
     return true;
 }
@@ -312,6 +313,8 @@ bool XpipSaxHandler::endElement_identification() {
 }
 
 bool XpipSaxHandler::endElement_match() {
+    _count_protein_match++;
+    _p_monitor->message(QString("readind match %1").arg(_count_protein_match), _count_protein_match);
     _current_identification_group_p->addProteinMatch(_p_protein_match);
     _p_protein_match = nullptr;
     return true;
@@ -346,6 +349,7 @@ bool XpipSaxHandler::endDocument() {
 }
 
 bool XpipSaxHandler::startDocument() {
+    _count_protein_match=0;
     return true;
 }
 
diff --git a/src/input/xpipsaxhandler.h b/src/input/xpipsaxhandler.h
index 10cbbf5b..658328f1 100644
--- a/src/input/xpipsaxhandler.h
+++ b/src/input/xpipsaxhandler.h
@@ -30,11 +30,12 @@
 #include <pappsomspp/amino_acid/aamodification.h>
 #include "../core/project.h"
 #include "../core/proteinmatch.h"
+#include "../utils/workmonitor.h"
 
 class XpipSaxHandler: public QXmlDefaultHandler
 {
 public:
-    XpipSaxHandler(Project * p_project);
+    XpipSaxHandler(WorkMonitorInterface * p_monitor, Project * p_project);
     ~XpipSaxHandler();
 
     bool startElement(const QString & namespaceURI, const QString & localName,
@@ -74,6 +75,7 @@ private:
     pappso::AaModificationP getAaModificationP(pappso::mz mass) const;
 
 private:
+    WorkMonitorInterface * _p_monitor;
     std::vector<QString> _tag_stack;
     QString _errorStr;
     QString _current_text;
@@ -88,7 +90,8 @@ private:
     FastaFileSp _current_fasta_file_sp;
     
     QMap<QString, pappso::AaModificationP> _map_massstr_aamod;
-    uint _count_protein_match;
+    uint _count_protein_match=0;
+    uint _total_protein_match;
 };
 
 #endif // XTANDEMRESULTSHANDLER_H
diff --git a/src/utils/workmonitor.cpp b/src/utils/workmonitor.cpp
index 738be3c5..7959a503 100644
--- a/src/utils/workmonitor.cpp
+++ b/src/utils/workmonitor.cpp
@@ -29,11 +29,19 @@
 ******************************************************************************/
 
 #include "workmonitor.h"
+#include <QDebug>
 
 void WorkMonitor::message(const QString & message) {
     emit workerMessage(message);
 }
 void WorkMonitor::message(const QString & message, int value) {
-    
-    emit workerMessage(message,value);
+    qDebug() << "WorkMonitor::message " << value << " " << _max_value << " " << (value/_max_value)*100;
+    int percent = ((float)value/(float)_max_value)*(float)100;
+    if (percent != _percent) {
+        _percent = percent;
+    emit workerMessage(message,percent);
+    }
+}
+void WorkMonitor::setProgressMaximumValue(int max_value) {
+    _max_value = max_value;
 }
diff --git a/src/utils/workmonitor.h b/src/utils/workmonitor.h
index 3b857ca3..75e6e7b7 100644
--- a/src/utils/workmonitor.h
+++ b/src/utils/workmonitor.h
@@ -38,20 +38,24 @@ class WorkMonitorInterface
 public:
     virtual void message(const QString & message) = 0;
     virtual void message(const QString & message, int value) = 0;
+    virtual void setProgressMaximumValue(int max_value) = 0;
 };
 
-class WorkMonitor: public QObject, WorkMonitorInterface
+class WorkMonitor: public QObject, public WorkMonitorInterface
 {
     Q_OBJECT
 public:
     void message(const QString & message) override;
     void message(const QString & message, int value) override;
+    void setProgressMaximumValue(int max_value) override;
     
     signals:
     void workerMessage(QString message);
     void workerMessage(QString message, int value);
 
-
+private :
+    int _max_value = 100;
+    int _percent = 0;
 };
 
 #endif // WORKMONITOR_H
-- 
GitLab