Salome HOME
portage V8_5_0
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportProfilesOp.cxx
index e41732b2fa0eced39bb63ebd4f2916990ebbf5ba..9e12b9eebd004777213c2423e9d64751b93129c5 100644 (file)
@@ -1,12 +1,8 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -24,6 +20,8 @@
 
 #include "HYDROGUI_DataModel.h"
 #include "HYDROGUI_Module.h"
+#include "HYDROGUI_UpdateFlags.h"
+#include "HYDROGUI_Tool.h"
 
 #include <HYDROData_Profile.h>
 
@@ -32,7 +30,9 @@
 
 #include <SUIT_Desktop.h>
 #include <SUIT_FileDlg.h>
+#include <SUIT_MessageBox.h>
 
+#include <QApplication>
 #include <QFileInfo>
 
 HYDROGUI_ImportProfilesOp::HYDROGUI_ImportProfilesOp( HYDROGUI_Module* theModule )
@@ -52,7 +52,7 @@ void HYDROGUI_ImportProfilesOp::startOperation()
   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
   myFileDlg->setWindowTitle( getName() );
   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
-  myFileDlg->setFilter( tr("PROFILE_FILTER") );
+  myFileDlg->setNameFilter( tr("PROFILE_FILTER") );
 
   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
@@ -60,35 +60,88 @@ void HYDROGUI_ImportProfilesOp::startOperation()
   myFileDlg->exec();
 }
 
-bool HYDROGUI_ImportProfilesOp::processApply( int& theUpdateFlags,
-                                              QString& theErrorMsg )
+void HYDROGUI_ImportProfilesOp::onApply()
 {
-  if ( !myFileDlg ) {
-    return false;
+  if ( !myFileDlg )
+  {
+    abort();
+    return;
   }
 
-  bool aRes = false;
-
   // Get the selected files
   QStringList aFileNames = myFileDlg->selectedFiles();
+  if ( aFileNames.isEmpty() )
+  {
+    abort();
+    return;
+  }
+
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+  QStringList aBrowseObjectsEntries;
+  //TODO: to implement the addition of imported profiles' entries to the list
+
+  startDocOperation();
 
   // Import each of the selected files, fill the list of bad imported files
   QString aBadImportedFiles;
-  foreach ( const QString& aFileName, aFileNames ) {
-    if ( !HYDROData_Profile::ImportFromFile( doc(), qPrintable( aFileName ) ) ) {
-      aBadImportedFiles += aFileName + "\n";
+  int aTotalNbImported = 0;
+
+  foreach ( const QString& aFileName, aFileNames )
+  {
+    NCollection_Sequence<int> aBadProfilesIds;
+    TCollection_AsciiString anAsciiFileName = HYDROGUI_Tool::ToAsciiString( aFileName );
+    
+    int aNbImported = HYDROData_Profile::ImportFromFile( doc(), anAsciiFileName, aBadProfilesIds );
+    if ( aNbImported == 0 || !aBadProfilesIds.IsEmpty() )
+    {
+      aBadImportedFiles += QFileInfo( aFileName ).fileName();
+      if ( !aBadProfilesIds.IsEmpty() && aNbImported > 0 )
+      {
+        aBadImportedFiles += ", " + tr( "BAD_PROFILE_IDS" ) + " : ";
+        for ( int i = 1, n = aBadProfilesIds.Length(); i <= n; ++i )
+          aBadImportedFiles += QString::number( aBadProfilesIds.Value( i ) ) + ", ";
+        aBadImportedFiles.remove( aBadImportedFiles.length() - 2, 2 );
+      }
+      aBadImportedFiles += ";\n";
     }
+
+    aTotalNbImported += aNbImported;
   }
 
-  // Operation is successful if all the selected files imported succesfully
-  if ( aBadImportedFiles.isEmpty() ) {
-    aRes = true;
-  } else {
-    theErrorMsg = tr( "BAD_IMPORTED_PROFILES_FILES" ).arg( aBadImportedFiles );
+  if ( aTotalNbImported == 0 )
+  {
+    QApplication::restoreOverrideCursor();
+  
+    SUIT_MessageBox::critical( module()->getApp()->desktop(),
+                               tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
+                               tr( "NO_ONE_PROFILE_IMPORTED" ) );
+
+    QApplication::setOverrideCursor( Qt::WaitCursor );
+
+    abortDocOperation();
     abort();
   }
+  else
+  {
+    if ( !aBadImportedFiles.isEmpty() )
+    {
+      QApplication::restoreOverrideCursor();
+
+      aBadImportedFiles = aBadImportedFiles.simplified();
+      SUIT_MessageBox::warning( module()->getApp()->desktop(),
+                                tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
+                                tr( "BAD_IMPORTED_PROFILES_FILES" ).arg( aBadImportedFiles ) );
+
+      QApplication::setOverrideCursor( Qt::WaitCursor );
+    }
 
-  theUpdateFlags = UF_Model;
+    commitDocOperation();
+    commit();
 
-  return aRes;
+    module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+    browseObjects( aBrowseObjectsEntries );
+  }
+
+  QApplication::restoreOverrideCursor();
 }
+