Salome HOME
Allow saving groups with non-ascii names.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI.cxx
index 82aaf6581b499911a53051168011a0d8a97a52c6..35e2c4850859bdc09d4b07bd8e6c24179925fe7c 100644 (file)
@@ -655,6 +655,9 @@ namespace
     // Get parameters of export operation
 
     QString aFilename;
+    int aFormat =-1;         // for MED minor versions
+    bool isOkToWrite = true; // to check MED file version compatibility before adding a mesh in an existing file
+
     // Init the parameters with the default values
     bool aIsASCII_STL   = true;
     bool toCreateGroups = false;
@@ -706,7 +709,7 @@ namespace
 
       if ( fd->exec() )
         aFilename = fd->selectedFile();
-      toOverwrite    = fv->isOverwrite();
+      toOverwrite    = fv->isOverwrite(aFilename);
       toCreateGroups = fd->IsChecked(0);
       SMESHGUI::resourceMgr()->setValue("SMESH", theByTypeResource, toCreateGroups );
 
@@ -741,14 +744,40 @@ namespace
     }
     else if ( isMED || isSAUV ) // Export to MED or SAUV
     {
-      QStringList filters;
+      QMap<QString, int> aFilterMap;
       if ( isMED ) {
-        filters << QObject::tr( "MED_FILES_FILTER" ) + " (*.med)";
+        //filters << QObject::tr( "MED_FILES_FILTER" ) + " (*.med)";
+        QString vmed (aMesh->GetVersionString(-1, 2));
+        //MESSAGE("MED version: " << vmed.toStdString());
+        int minor = vmed.split(".").last().toInt();
+        //MESSAGE("MED version minor: "<< minor);
+        //minor +=3;                  // TODO remove: test multiple minor
+        aFilterMap.insert( QObject::tr( "MED_VX_FILES_FILTER" ).arg( vmed ) + " (*.med)", minor );
+        for (int ii=0; ii<minor; ii++)
+          {
+            QString vs = aMesh->GetVersionString(ii, 2);
+            //std::ostringstream vss; // TODO remove: test multiple minor
+            //vss << "4.";            // TODO remove: test multiple minor
+            //vss << ii;              // TODO remove: test multiple minor
+            //vs = vss.str().c_str(); // TODO remove: test multiple minor
+            //MESSAGE("MED version: " << vs.toStdString());
+            aFilterMap.insert( QObject::tr( "MED_VX_FILES_FILTER" ).arg( vs ) + " (*.med)",  ii);
+          }
       }
       else { // isSAUV
-        filters << QObject::tr( "SAUV_FILES_FILTER" ) + " (*.sauv *.sauve)";
+        aFilterMap.insert("All files (*)", -1 );
+        aFilterMap.insert("SAUV files (*.sauv)", -1 );
+        aFilterMap.insert("SAUV files (*.sauve)", -1 );
       }
 
+      QStringList filters;
+      QString aDefaultFilter;
+      QMap<QString, int>::const_iterator it = aFilterMap.begin();
+      for ( ; it != aFilterMap.end(); ++it ) {
+        filters.push_back( it.key() );
+        if (it.key() == 0)
+          aDefaultFilter = it.key();
+      }
       QStringList checkBoxes;
       checkBoxes << QObject::tr("SMESH_AUTO_GROUPS") << QObject::tr("SMESH_AUTO_DIM");
 
@@ -782,18 +811,24 @@ namespace
 
       bool is_ok = false;
       while (!is_ok) {
+        MESSAGE("******* Loop on file dialog ***********");
+        isOkToWrite =true;
         if ( fd->exec() )
           aFilename = fd->selectedFile();
         else {
           aFilename = QString::null;
           break;
         }
-        toOverwrite = fv->isOverwrite();
+        aFormat = aFilterMap[fd->selectedNameFilter()];
+        MESSAGE("selected minor: " << aFormat << " file: " << aFilename.toUtf8().constData());
+        toOverwrite = fv->isOverwrite(aFilename);
+        MESSAGE("toOverwrite:" << toOverwrite);
         is_ok = true;
         if ( !aFilename.isEmpty() ) {
           if( !toOverwrite ) {
             // can't append to an existing using other format
-            bool isVersionOk = SMESHGUI::GetSMESHGen()->CheckCompatibility( aFilename.toUtf8().constData() );
+            bool isVersionOk = SMESHGUI::GetSMESHGen()->CheckWriteCompatibility( aFilename.toUtf8().constData() );
+            MESSAGE("Append check, isVersionOk:" << isVersionOk);
             if ( !isVersionOk ) {
               int aRet = SUIT_MessageBox::warning(SMESHGUI::desktop(),
                                                   QObject::tr("SMESH_WRN_WARNING"),
@@ -801,11 +836,17 @@ namespace
                                                   QObject::tr("SMESH_BUT_YES"),
                                                   QObject::tr("SMESH_BUT_NO"), 0, 1);
               if (aRet == 0)
-                toOverwrite = true;
+                {
+                  toOverwrite = true;
+                  MESSAGE("incompatible MED file version for add, overwrite accepted");
+                }
               else
-                is_ok = false;
+                {
+                  isOkToWrite = false;
+                  is_ok = false;
+                  MESSAGE("incompatible MED file version for add, overwrite refused");
+                }
             }
-
             QStringList aMeshNamesCollisionList;
             SMESH::string_array_var aMeshNames = SMESHGUI::GetSMESHGen()->GetMeshNames( aFilename.toUtf8().constData() );
             for( int i = 0, n = aMeshNames->length(); i < n; i++ ) {
@@ -818,7 +859,8 @@ namespace
                 }
               }
             }
-            if( !aMeshNamesCollisionList.isEmpty() ) {
+           if( !aMeshNamesCollisionList.isEmpty() ) {
+              isOkToWrite = false;
               QString aMeshNamesCollisionString = aMeshNamesCollisionList.join( ", " );
               int aRet = SUIT_MessageBox::warning(SMESHGUI::desktop(),
                                                   QObject::tr("SMESH_WRN_WARNING"),
@@ -826,14 +868,18 @@ namespace
                                                   QObject::tr("SMESH_BUT_YES"),
                                                   QObject::tr("SMESH_BUT_NO"),
                                                   QObject::tr("SMESH_BUT_CANCEL"), 0, 2);
-              if (aRet == 0)
+             MESSAGE("answer collision name " << aRet);
+             if (aRet == 0) {
                 toOverwrite = true;
+                isOkToWrite = true;
+              }
               else if (aRet == 2)
                 is_ok = false;
             }
           }
         }
       }
+      MESSAGE(" ****** end of file dialog loop, toOverwrite:" << toOverwrite << " isOkToWrite:" << isOkToWrite);
       toCreateGroups = fd->IsChecked(0);
       toFindOutDim   = fd->IsChecked(1);
       fieldSelWdg->GetSelectedFields();
@@ -868,8 +914,9 @@ namespace
 //           if ( SMESHGUI::automaticUpdate() )
 //             SMESH::UpdateView();
 //         }
-        if ( isMED )
+        if ( isMED && isOkToWrite)
         {
+          MESSAGE("OK to write MED file "<< aFilename.toUtf8().constData());
           aMeshIter = aMeshList.begin();
           for( int aMeshIndex = 0; aMeshIter != aMeshList.end(); aMeshIter++, aMeshIndex++ )
           {
@@ -879,10 +926,10 @@ namespace
             const QString&            geoAssFields = aFieldList[ aMeshIndex ].second;
             const bool                   hasFields = ( fields.length() || !geoAssFields.isEmpty() );
             if ( !hasFields && aMeshOrGroup->_is_equivalent( aMeshItem ))
-              aMeshItem->ExportMED( aFilename.toUtf8().data(), toCreateGroups,
+              aMeshItem->ExportMED( aFilename.toUtf8().data(), toCreateGroups, aFormat,
                                     toOverwrite && aMeshIndex == 0, toFindOutDim );
             else
-              aMeshItem->ExportPartToMED( aMeshOrGroup, aFilename.toUtf8().data(), toCreateGroups,
+              aMeshItem->ExportPartToMED( aMeshOrGroup, aFilename.toUtf8().data(), toCreateGroups, aFormat,
                                           toOverwrite && aMeshIndex == 0, toFindOutDim,
                                           fields, geoAssFields.toLatin1().data() );
           }
@@ -6214,7 +6261,7 @@ void SMESHGUI::restoreVisualParameters (int savePoint)
 
     // Check that the entry corresponds to a real object in the Study
     // as the object may be deleted or modified after the visual state is saved.
-    _PTR(SObject) so = studyDS->FindObjectID(entry.toLatin1().data());
+    _PTR(SObject) so = studyDS->FindObjectID(entry.toUtf8().data());
     if (!so) continue; //Skip the not existent entry
 
     std::vector<std::string> paramNames = ip->getAllParameterNames( *entIt );
@@ -6281,7 +6328,7 @@ void SMESHGUI::restoreVisualParameters (int savePoint)
                 SMESH_Actor* aGeomAc = SMESH_Actor::SafeDownCast(ac);
                 if (aGeomAc->hasIO()) {
                   Handle(SALOME_InteractiveObject) io = aGeomAc->getIO();
-                  if (io->hasEntry() && strcmp(io->getEntry(), entry.toLatin1().data()) == 0) {
+                  if (io->hasEntry() && strcmp(io->getEntry(), entry.toUtf8().data()) == 0) {
                     isFound = true;
                     vtkActors.Bind(viewIndex, aGeomAc);
                   }
@@ -6967,7 +7014,7 @@ void SMESHGUI::message( const QString& msg )
       // get study
       _PTR(Study) study = dynamic_cast<SalomeApp_Study*>( application()->activeStudy() )->studyDS();
       // get mesh name
-      _PTR(SObject) obj = study->FindObjectID( entry.toLatin1().constData() );
+      _PTR(SObject) obj = study->FindObjectID( entry.toUtf8().constData() );
       QString name;
       if ( obj )
         name = SMESH::fromUtf8(obj->GetName());
@@ -7076,7 +7123,7 @@ bool SMESHGUI::renameObject( const QString& entry, const QString& name) {
           aType == SMESH::SUBMESH_EDGE || aType == SMESH::SUBMESH_VERTEX ||
           aType == SMESH::HYPOTHESIS || aType == SMESH::ALGORITHM) {
         if ( !name.isEmpty() ) {
-          SMESHGUI::GetSMESHGen()->SetName(obj->GetIOR().c_str(), qPrintable(name) );
+          SMESHGUI::GetSMESHGen()->SetName(obj->GetIOR().c_str(), qUtf8Printable(name) );
 
           // update name of group object and its actor
           Handle(SALOME_InteractiveObject) IObject =
@@ -7084,9 +7131,9 @@ bool SMESHGUI::renameObject( const QString& entry, const QString& name) {
 
           SMESH::SMESH_GroupBase_var aGroupObject = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IObject);
           if( !aGroupObject->_is_nil() ) {
-            aGroupObject->SetName( qPrintable(name) );
+            aGroupObject->SetName( qUtf8Printable(name) );
             if ( SMESH_Actor *anActor = SMESH::FindActorByEntry( qPrintable(entry) ) )
-              anActor->setName( qPrintable(name) );
+              anActor->setName( qUtf8Printable(name) );
           }
           return true;
         }