use Qt Signals & Slots for progress tracking
[mussa.git] / qui / MussaWindow.cpp
index 454c7253aa2d0f26c892ce70f8d7fc376de4e2b9..d12e982d8061326cd83d32fcbb821c41ed1a6746 100644 (file)
@@ -1,4 +1,5 @@
 #include <QAction>
+#include <QApplication>
 #include <QAssistantClient>
 #include <QDir>
 #include <QFileDialog>
@@ -30,6 +31,7 @@ MussaWindow::MussaWindow(Mussa *analysis_, QWidget *parent) :
   mussaViewTB("Path Views"),
   zoom(),
   threshold(),
+  progress_dialog(0),
   aboutAction(0),
   closeAction(0),
   createNewAnalysisAction(0),
@@ -81,6 +83,8 @@ MussaWindow::MussaWindow(Mussa *analysis_, QWidget *parent) :
   addToolBar(&mussaViewTB);
 
   statusBar()->showMessage("Welcome to mussa", 2000);
+  connect(analysis, SIGNAL(progress(const std::string&, int, int)),
+          this, SLOT(updateProgress(const std::string&, int, int)));
   updateAnalysis();
 }
 
@@ -89,6 +93,7 @@ MussaWindow::~MussaWindow()
   if (analysis != 0) delete analysis;
   aligned_windows.clear();
   if (motif_editor != 0) delete motif_editor;
+  if (progress_dialog != 0) delete progress_dialog;
 
   if (aboutAction != 0) delete aboutAction;
   if (closeAction != 0) delete closeAction;
@@ -351,6 +356,8 @@ void MussaWindow::loadMupa()
   try {
     Mussa *m = new Mussa;
     fs::path converted_path(mupa_path.toStdString(), fs::native);
+    connect(m, SIGNAL(progress(const std::string&, int, int)),
+            this, SLOT(updateProgress(const std::string&, int, int)));
     m->load_mupa_file(converted_path);
     m->analyze();
     setAnalysis(m);
@@ -378,6 +385,8 @@ void MussaWindow::loadSavedAnalysis()
   try {
     Mussa *m = new Mussa;
     fs::path converted_path(muway_dir.toStdString(), fs::native);
+    connect(m, SIGNAL(progress(const std::string&, int, int)),
+            this, SLOT(updateProgress(const std::string&, int, int)));
     m->load(converted_path);
     // only switch mussas if we loaded without error
     setAnalysis(m);
@@ -530,3 +539,29 @@ void MussaWindow::updateLinks()
   browser.update();
 }
 
+void 
+MussaWindow::updateProgress(const string& description, int current, int max)
+{  
+  // if we're done  
+  if (current == max) {
+    cout << "done with dialog" << endl;
+    if (progress_dialog != 0) {
+      progress_dialog->hide();
+      delete progress_dialog;
+      progress_dialog = 0;
+    }
+  } else {
+    // if we're starting, create the dialog
+    if (progress_dialog == 0) {
+      cout << "creating dialog" << endl;
+      QString desc(description.c_str());
+      QString cancel("Cancel");
+      progress_dialog = new QProgressDialog(desc, cancel, current, max, this);
+      progress_dialog->show();
+    } else {
+      // just update the dialog
+      progress_dialog->setValue(current);
+    }
+  }
+  qApp->processEvents();
+}