From 7b6978b01c7f4f8e5cabd5817642e4a53a3d40e4 Mon Sep 17 00:00:00 2001
From: Olivier Langella <Olivier.Langella@moulon.inra.fr>
Date: Thu, 21 Sep 2017 15:26:53 +0200
Subject: [PATCH] parse command line argument, associate XPIP mime type to
 xtpcpp

---
 src/gui/mainwindow.cpp                        | 49 +++++++++++++++++++
 src/gui/mainwindow.h                          |  2 +
 src/main.cpp                                  |  6 +++
 .../share/applications/xtpcpp.desktop.cmake   |  3 +-
 4 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp
index 224713ac..44520a82 100644
--- a/src/gui/mainwindow.cpp
+++ b/src/gui/mainwindow.cpp
@@ -25,6 +25,7 @@
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QDebug>
+#include <QCommandLineParser>
 #include "../config.h"
 #include "mainwindow.h"
 
@@ -51,6 +52,7 @@ MainWindow::MainWindow(QWidget *parent):
     QMainWindow(parent),
     ui(new Ui::Main)
 {
+    _p_app = QCoreApplication::instance();
     ui->setupUi(this);
     setWindowTitle(QString("%1 %2").arg(SOFTWARE_NAME).arg(XTPCPP_VERSION));
 
@@ -456,3 +458,50 @@ void MainWindow::doActionAbout() {
 
     qDebug() << "MainWindow::doActionAbout end";
 }
+
+
+
+
+// 10ms after the application starts this method will run
+// all QT messaging is running at this point so threads, signals and slots
+// will all work as expected.
+void MainWindow::run()
+{
+
+    QTextStream errorStream(stderr, QIODevice::WriteOnly);
+
+    try {
+        qDebug() << "MainWindow::run() begin";
+        QCommandLineParser parser;
+
+        //throw pappso::PappsoException("test");
+        parser.setApplicationDescription(QString(SOFTWARE_NAME).append(" ").append(XTPCPP_VERSION));
+        parser.addHelpOption();
+        parser.addVersionOption();
+        parser.addPositionalArgument("XPIP project file", QCoreApplication::translate("main", "Project file to open."));
+
+        qDebug() << "MainWindow::run() 1";
+
+        // Process the actual command line arguments given by the user
+        parser.process(*_p_app);
+
+
+        QStringList xpip_list = parser.positionalArguments();
+        if (xpip_list.size() > 0) {
+            showWaitingMessage(tr("Loading %1 XPIP file").arg(xpip_list.at(0)));
+            emit  operateXpipFile(xpip_list.at(0));
+        }
+
+    }
+    catch (pappso::PappsoException& error)
+    {
+        viewError(tr("Oops! an error occurred in X!TandemPipeline. Dont Panic :\n%1").arg(error.qwhat()));
+    }
+
+    catch (std::exception& error)
+    {
+        viewError(tr("Oops! an error occurred in X!TandemPipeline. Dont Panic :\n%1").arg(error.what()));
+    }
+
+
+}
diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h
index b1e56cef..0c8adc9d 100644
--- a/src/gui/mainwindow.h
+++ b/src/gui/mainwindow.h
@@ -62,6 +62,7 @@ public:
     bool stopWorkerThread();
 
 public slots:
+    void run();
     void loadResults();
     void selectXpipFile();
     void doActionAbout();
@@ -112,6 +113,7 @@ private:
     void viewError(QString error);
 
 private :
+    QCoreApplication * _p_app;
     Ui::Main *ui;
     QThread _worker_thread;
     ProjectSp _project_sp=nullptr;
diff --git a/src/main.cpp b/src/main.cpp
index baafd20d..45a4f7bf 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -22,6 +22,7 @@
 ******************************************************************************/
 
 #include <QApplication>
+#include <QTimer>
 #include <iostream>
 #include <pappsomspp/pappsoexception.h>
 #include "config.h"
@@ -35,6 +36,7 @@ int main(int argc, char *argv[])
     QApplication app(argc, argv);
 
     qRegisterMetaType<TandemRunBatch>("TandemRunBatch");
+
     try {
         QCoreApplication::setOrganizationName("PAPPSO");
         QCoreApplication::setOrganizationDomain("pappso.inra.fr");
@@ -42,6 +44,10 @@ int main(int argc, char *argv[])
         MainWindow window;
         window.show();
 
+        // This code will start the messaging engine in QT and in
+        // 10ms it will start the execution in the MainClass.run routine;
+        QTimer::singleShot(10, &window, SLOT(run()));
+
         return app.exec();
     }
     catch (pappso::PappsoException& error)
diff --git a/templates/share/applications/xtpcpp.desktop.cmake b/templates/share/applications/xtpcpp.desktop.cmake
index 88ea5912..cf13b214 100644
--- a/templates/share/applications/xtpcpp.desktop.cmake
+++ b/templates/share/applications/xtpcpp.desktop.cmake
@@ -4,9 +4,10 @@ Version=1.0
 Name=X!TandemPipeline C++
 Categories=Science;Biology;Education;Qt;
 Comment=Protein inference tool
-Exec=${CMAKE_INSTALL_PREFIX}/bin/xtpcpp
+Exec=${CMAKE_INSTALL_PREFIX}/bin/xtpcpp %U
 Icon=${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/scalable/apps/xtpcpp.svg
 Terminal=false
 Type=Application
 StartupNotify=true
 Keywords=Mass spectrometry;Protein inference;Peptide;
+MimeType=application/x-xtandempipeline;
-- 
GitLab