From a400bda077c7c4f6c9d1f245bb605af9c04954a3 Mon Sep 17 00:00:00 2001 From: vsr Date: Wed, 2 Jul 2008 08:39:18 +0000 Subject: [PATCH] IMP 001989: multi selection in "Import" dialog boxes --- src/GEOMGUI/GEOM_msg_en.po | 11 +- src/GEOMToolsGUI/GEOMToolsGUI.cxx | 210 +++++++++++++++++++++--------- 2 files changed, 156 insertions(+), 65 deletions(-) diff --git a/src/GEOMGUI/GEOM_msg_en.po b/src/GEOMGUI/GEOM_msg_en.po index b88e55e8b..9d252109d 100644 --- a/src/GEOMGUI/GEOM_msg_en.po +++ b/src/GEOMGUI/GEOM_msg_en.po @@ -3343,9 +3343,14 @@ msgstr "Compound" msgid "MEN_ALL_SEL_ONLY" msgstr "Select All" +msgid "GEOM_ALL_IMPORT_FILES" +msgstr "All supported formats ( %1 )" +msgid "GEOM_UNSUPPORTED_TYPE" +msgstr "Unsupported format for the file" +msgid "GEOM_UNKNOWN_IMPORT_ERROR" +msgstr "Unknown error" - - - +msgid "GEOM_IMPORT_ERRORS" +msgstr "Import operation has finished with errors:" diff --git a/src/GEOMToolsGUI/GEOMToolsGUI.cxx b/src/GEOMToolsGUI/GEOMToolsGUI.cxx index 460617bf4..d94543ad1 100644 --- a/src/GEOMToolsGUI/GEOMToolsGUI.cxx +++ b/src/GEOMToolsGUI/GEOMToolsGUI.cxx @@ -58,6 +58,7 @@ // QT Includes #include #include +#include // OCCT Includes #include @@ -65,6 +66,7 @@ using namespace std; typedef QMap FilterMap; +static QString lastUsedFilter; //======================================================================= // function : getFileName @@ -80,7 +82,6 @@ static QString getFileName( QWidget* parent, QString& format, bool showCurrentDirInitially = false) { - static QString lastUsedFilter; //QStringList filters; QString aBrepFilter; for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) { @@ -120,6 +121,63 @@ static QString getFileName( QWidget* parent, return filename; } +//======================================================================= +// function : getFileNames +// purpose : Select list of files for Import operation. Returns also +// the selected file type code through argument. +//======================================================================= +static QStringList getFileNames( QWidget* parent, + const QString& initial, + const FilterMap& filterMap, + const QString& caption, + QString& format, + bool showCurrentDirInitially = false) +{ + QString aBrepFilter; + QStringList allFilters; + QStringList filters; + QRegExp re( "\\((.*)\\)" ); + re.setMinimal( true ); + for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) { + if ( it.data().contains( "BREP", false ) && aBrepFilter.isEmpty() ) + aBrepFilter = it.key(); + filters.append( it.key() ); + int pos = 0; + while ( re.search( it.key(), pos ) >= 0 ) { + QString f = re.cap(1); + pos = re.pos() + f.length() + 2; + allFilters.append( f.simplifyWhiteSpace() ); + } + } + filters.append( QObject::tr( "GEOM_ALL_IMPORT_FILES" ).arg( allFilters.join( " " ) ) ); + + SUIT_FileDlg fd( parent, true, true, true ); + fd.setMode( SUIT_FileDlg::ExistingFiles ); + if ( !caption.isEmpty() ) + fd.setCaption( caption ); + if ( !initial.isEmpty() ) + fd.setSelection( initial ); + + if ( showCurrentDirInitially && SUIT_FileDlg::getLastVisitedPath().isEmpty() ) + fd.setDir( QDir::currentDirPath() ); + + fd.setFilters( filters ); + + if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) ) + fd.setSelectedFilter( lastUsedFilter ); + else if ( !aBrepFilter.isEmpty() ) + fd.setSelectedFilter( aBrepFilter ); + + QStringList filenames; + if ( fd.exec() ) { + filenames = fd.selectedFiles(); + format = filterMap.contains( fd.selectedFilter() ) ? filterMap[ fd.selectedFilter() ] : QString(); + lastUsedFilter = fd.selectedFilter(); + } + qApp->processEvents(); + return filenames; +} + //======================================================================= // function : getParentComponent // purpose : Get object's parent component entry @@ -543,7 +601,6 @@ void GEOMToolsGUI::OnEditCopy() bool GEOMToolsGUI::Import() { SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() ); - //SUIT_Application* app = getGeometryGUI()->getApp(); if (! app) return false; SalomeApp_Study* stud = dynamic_cast ( app->activeStudy() ); @@ -553,106 +610,135 @@ bool GEOMToolsGUI::Import() } _PTR(Study) aStudy = stud->studyDS(); + // check if study is locked bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked(); if ( aLocked ) { SUIT_MessageBox::warn1 ( app->desktop(), - QObject::tr("WRN_WARNING"), - QObject::tr("WRN_STUDY_LOCKED"), - QObject::tr("BUT_OK") ); + QObject::tr("WRN_WARNING"), + QObject::tr("WRN_STUDY_LOCKED"), + QObject::tr("BUT_OK") ); return false; } + // check if GEOM engine is available GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen(); if ( CORBA::is_nil( eng ) ) { SUIT_MessageBox::error1( app->desktop(), - QObject::tr("WRN_WARNING"), - QObject::tr( "GEOM Engine is not started" ), - QObject::tr("BUT_OK") ); - return false; - } + QObject::tr("WRN_WARNING"), + QObject::tr( "GEOM Engine is not started" ), + QObject::tr("BUT_OK") ); + return false; + } GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() ); if ( aInsOp->_is_nil() ) return false; - GEOM::GEOM_Object_var anObj; - - // Obtain a list of available import formats + // obtain a list of available import formats FilterMap aMap; - QStringList filters; GEOM::string_array_var aFormats, aPatterns; aInsOp->ImportTranslators( aFormats, aPatterns ); - for ( int i = 0, n = aFormats->length(); i < n; i++ ) { + for ( int i = 0, n = aFormats->length(); i < n; i++ ) aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] ); - filters.push_back( (char*)aPatterns[i] ); - } + // select files to be imported QString fileType; - - QString fileName = getFileName(app->desktop(), "", aMap, filters, - tr("GEOM_MEN_IMPORT"), true, fileType, true); + QStringList fileNames = getFileNames( app->desktop(), "", aMap, + tr( "GEOM_MEN_IMPORT" ), fileType, true ); - if (fileType.isEmpty() ) - { - // Trying to detect file type - QFileInfo aFileInfo( fileName ); - QString aPossibleType = (aFileInfo.extension(false)).upper() ; + // set Wait cursor + SUIT_OverrideCursor wc; - if ( (aMap.values()).contains(aPossibleType) ) - fileType = aPossibleType; - } - - if (fileName.isEmpty() || fileType.isEmpty()) - return false; + if ( fileNames.count() == 0 ) + return false; // nothing selected, return - GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in()); - try { - SUIT_OverrideCursor wc; + QStringList errors; - app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true))); + QValueList< GEOM::GEOM_Object_var > objsForDisplay; + + // iterate through all selected files + for ( QStringList::ConstIterator it = fileNames.begin(); it != fileNames.end(); ++it ) { + QString fileName = *it; - anOp->start(); + if ( fileName.isEmpty() ) + continue; - CORBA::String_var fileN = fileName.latin1(); - CORBA::String_var fileT = fileType.latin1(); - anObj = aInsOp->Import(fileN, fileT); + QString aCurrentType; + if ( fileType.isEmpty() ) { + // file type is not defined, try to detect + QString ext = QFileInfo( fileName ).extension( false ).upper(); + QRegExp re( "\\*\\.(\\w+)" ); + for ( FilterMap::const_iterator it = aMap.begin(); + it != aMap.end() && aCurrentType.isEmpty(); ++it ) { + int pos = 0; + while ( re.search( it.key(), pos ) >= 0 ) { + QString f = re.cap(1).stripWhiteSpace().upper(); + if ( ext == f ) { aCurrentType = it.data(); break; } + pos = re.pos() + re.cap(1).length() + 2; + } + } + } + else { + aCurrentType = fileType; + } - if ( !anObj->_is_nil() && aInsOp->IsDone() ) { - QString aPublishObjName = - GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true)); + if ( aCurrentType.isEmpty() ) { + errors.append( QString( "%1 : %2" ).arg( fileName ).arg( tr( "GEOM_UNSUPPORTED_TYPE" ) ) ); + continue; + } - SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy); - GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy, - SALOMEDS::SObject::_nil(), - anObj, - aPublishObjName); + GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() ); + try { + app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, /*withExten=*/true ) ) ); + anOp->start(); - GEOM_Displayer( stud ).Display( anObj.in() ); + CORBA::String_var fileN = fileName.latin1(); + CORBA::String_var fileT = aCurrentType.latin1(); + GEOM::GEOM_Object_var anObj = aInsOp->Import( fileN, fileT ); - // update data model and object browser - getGeometryGUI()->updateObjBrowser( true ); + if ( !anObj->_is_nil() && aInsOp->IsDone() ) { + QString aPublishObjName = + GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, /*withExten=*/true ) ); + + SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy ); + GeometryGUI::GetGeomGen()->PublishInStudy( aDSStudy, + SALOMEDS::SObject::_nil(), + anObj, + aPublishObjName ); - anOp->commit(); + objsForDisplay.append( anObj ); + + anOp->commit(); + } + else { + anOp->abort(); + errors.append( QString( "%1 : %2" ).arg( fileName ).arg( aInsOp->GetErrorCode() ) ); + } } - else { + catch( const SALOME::SALOME_Exception& S_ex ) { anOp->abort(); - wc.suspend(); - SUIT_MessageBox::error1( app->desktop(), - QObject::tr( "GEOM_ERROR" ), - QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ), - QObject::tr("BUT_OK") ); + errors.append( QString( "%1 : %2" ).arg( fileName ).arg( tr( "GEOM_UNKNOWN_IMPORT_ERROR" ) ) ); } } - catch( const SALOME::SALOME_Exception& S_ex ) { - //QtCatchCorbaException(S_ex); - anOp->abort(); - return false; + + // update object browser + getGeometryGUI()->updateObjBrowser( true ); + + // display imported model (if only one file is selected) + if ( objsForDisplay.count() == 1 ) + GEOM_Displayer( stud ).Display( objsForDisplay[0].in() ); + + if ( errors.count() > 0 ) { + SUIT_MessageBox::error1( app->desktop(), + QObject::tr( "GEOM_ERROR" ), + QObject::tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ), + QObject::tr( "BUT_OK" ) ); } app->updateActions(); //SRN: To update a Save button in the toolbar - return true; + return objsForDisplay.count() > 0; } -- 2.39.2