From 2795e21377e47eadf2d15516d837e0bc7a1bc252 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Mon, 13 Aug 2012 14:15:38 +0000
Subject: [PATCH] more refactoring

---
 Common/onelabUtils.cpp | 60 ++++++++++++++++++++++++++++++++++++++
 Common/onelabUtils.h   |  1 +
 Fltk/onelabWindow.cpp  | 66 ++++++------------------------------------
 3 files changed, 70 insertions(+), 57 deletions(-)

diff --git a/Common/onelabUtils.cpp b/Common/onelabUtils.cpp
index 47d9484dc6..ba76aac2cc 100644
--- a/Common/onelabUtils.cpp
+++ b/Common/onelabUtils.cpp
@@ -10,6 +10,8 @@
 #include "GmshDefines.h"
 #include "GModel.h"
 #include "Context.h"
+#include "OS.h"
+#include "OpenFile.h"
 #include "CreateFile.h"
 #include "StringUtils.h"
 #include "onelabUtils.h"
@@ -258,6 +260,64 @@ namespace onelabUtils {
     return changed;
   }
 
+  bool runGmshClient(const std::string &action, bool meshAuto)
+  {
+    bool redraw = false;
+
+    onelab::server::citer it = onelab::server::instance()->findClient("Gmsh");
+    if(it == onelab::server::instance()->lastClient()) return redraw;
+
+    onelab::client *c = it->second;
+    std::string mshFileName = onelabUtils::getMshFileName(c);
+    if(action == "initialize") return redraw;
+
+    static std::string modelName = "";
+    if(modelName.empty()){
+      // first pass is special to prevent model reload, as well as
+      // remeshing if a mesh file already exists on disk
+      modelName = GModel::current()->getName();
+      if(!StatFile(mshFileName))
+        onelab::server::instance()->setChanged(false, "Gmsh");
+    }
+
+    if(action == "check"){
+      if(onelab::server::instance()->getChanged("Gmsh") ||
+         modelName != GModel::current()->getName()){
+        // reload geometry if Gmsh parameters have been modified or if
+        // the model name has changed
+        modelName = GModel::current()->getName();
+        redraw = true;
+        OpenProject(GModel::current()->getFileName());
+      }
+    }
+    else if(action == "compute"){
+      if(onelab::server::instance()->getChanged("Gmsh") ||
+         modelName != GModel::current()->getName()){
+        // reload the geometry, mesh it and save the mesh if Gmsh
+        // parameters have been modified or if the model name has
+        // changed
+        modelName = GModel::current()->getName();
+        redraw = true;
+        OpenProject(GModel::current()->getFileName());
+        if(!GModel::current()->empty() && meshAuto){
+          GModel::current()->mesh(3);
+          CreateOutputFile(mshFileName, CTX::instance()->mesh.fileFormat);
+        }
+      }
+      else if(StatFile(mshFileName)){
+        // mesh+save if the mesh file does not exist
+        if(meshAuto){
+          redraw = true;
+          GModel::current()->mesh(3);
+          CreateOutputFile(mshFileName, CTX::instance()->mesh.fileFormat);
+        }
+      }
+      onelab::server::instance()->setChanged(false, "Gmsh");
+    }
+
+    return redraw;
+  }
+
 }
 
 #endif
diff --git a/Common/onelabUtils.h b/Common/onelabUtils.h
index 165e473abf..3203316ed0 100644
--- a/Common/onelabUtils.h
+++ b/Common/onelabUtils.h
@@ -19,6 +19,7 @@ namespace onelabUtils {
   bool incrementLoop(const std::string &level);
   std::vector<double> getRange(onelab::number &p);
   bool updateGraph(const std::string &graphNum);
+  bool runGmshClient(const std::string &action, bool meshAuto);
 }
 
 #endif
diff --git a/Fltk/onelabWindow.cpp b/Fltk/onelabWindow.cpp
index ae0d1bcfdc..36dbc47afc 100644
--- a/Fltk/onelabWindow.cpp
+++ b/Fltk/onelabWindow.cpp
@@ -380,58 +380,6 @@ static void updateGraphs()
   }
 }
 
-static void runGmshClient(const std::string &action)
-{
-  onelab::server::citer it = onelab::server::instance()->findClient("Gmsh");
-  if(it == onelab::server::instance()->lastClient()) return;
-
-  onelab::client *c = it->second;
-  std::string mshFileName = onelabUtils::getMshFileName(c);
-  if(action == "initialize") return;
-
-  static std::string modelName = "";
-  if(modelName.empty()){
-    // first pass is special to prevent model reload, as well as
-    // remeshing if a mesh file already exists on disk
-    modelName = GModel::current()->getName();
-    if(!StatFile(mshFileName))
-      onelab::server::instance()->setChanged(false, "Gmsh");
-  }
-
-  if(action == "check"){
-    if(onelab::server::instance()->getChanged("Gmsh") ||
-       modelName != GModel::current()->getName()){
-      // reload geometry if Gmsh parameters have been modified or if
-      // the model name has changed
-      modelName = GModel::current()->getName();
-      geometry_reload_cb(0, 0);
-    }
-  }
-  else if(action == "compute"){
-    if(onelab::server::instance()->getChanged("Gmsh") ||
-       modelName != GModel::current()->getName()){
-      // reload the geometry, mesh it and save the mesh if Gmsh
-      // parameters have been modified or if the model name has
-      // changed
-      modelName = GModel::current()->getName();
-      geometry_reload_cb(0, 0);
-      if(!GModel::current()->empty() &&
-         FlGui::instance()->onelab->meshAuto()){
-        mesh_3d_cb(0, 0);
-        CreateOutputFile(mshFileName, CTX::instance()->mesh.fileFormat);
-      }
-    }
-    else if(StatFile(mshFileName)){
-      // mesh+save if the mesh file does not exist
-      if(FlGui::instance()->onelab->meshAuto()){
-        mesh_3d_cb(0, 0);
-        CreateOutputFile(mshFileName, CTX::instance()->mesh.fileFormat);
-      }
-    }
-    onelab::server::instance()->setChanged(false, "Gmsh");
-  }
-}
-
 void onelab_cb(Fl_Widget *w, void *data)
 {
   if(!data) return;
@@ -509,13 +457,16 @@ void onelab_cb(Fl_Widget *w, void *data)
 
   do{ // enter loop
 
-    //check whether the client is a onelab Metamodel
+    // check whether the client is a onelab Metamodel
     std::vector<onelab::number> n;
-    onelab::server::instance()->get(n,"HasGmsh");
+    onelab::server::instance()->get(n, "HasGmsh");
     bool metamodel = (n.size() && n[0].getValue());
 
-    // If the client is a NOT a metamodel Gmsh gets executed
-    if(!metamodel) runGmshClient(action);
+    // if the client is a not a metamodel, run Gmsh
+    if(!metamodel){
+      if(onelabUtils::runGmshClient(action, FlGui::instance()->onelab->meshAuto()))
+        drawContext::global()->draw();
+    }
 
     if(action == "compute")
       FlGui::instance()->onelab->checkForErrors("Gmsh");
@@ -539,7 +490,8 @@ void onelab_cb(Fl_Widget *w, void *data)
       if(FlGui::instance()->onelab->stop()) break;
     }
 
-    // update geometry in Gmsh window which might have been by the metamodel
+    // update geometry in Gmsh window which might have been changed by the
+    // metamodel
     if(metamodel)
       geometry_reload_cb(0, 0);
 
-- 
GitLab