From 5617222a44e709aa05f81f868300462d8a6b30b2 Mon Sep 17 00:00:00 2001 From: adv Date: Thu, 26 Dec 2013 10:41:03 +0000 Subject: [PATCH] Import profiles protection (Bug #203). --- src/HYDROData/HYDROData_Profile.cxx | 49 ++++++++++--- src/HYDROData/HYDROData_Profile.h | 15 ++-- src/HYDROGUI/HYDROGUI_ImportProfilesOp.cxx | 85 ++++++++++++++++++---- src/HYDROGUI/HYDROGUI_ImportProfilesOp.h | 2 +- src/HYDROGUI/resources/HYDROGUI_msg_en.ts | 14 +++- 5 files changed, 131 insertions(+), 34 deletions(-) diff --git a/src/HYDROData/HYDROData_Profile.cxx b/src/HYDROData/HYDROData_Profile.cxx index a3cbe730..b5ddec6a 100755 --- a/src/HYDROData/HYDROData_Profile.cxx +++ b/src/HYDROData/HYDROData_Profile.cxx @@ -6,6 +6,8 @@ #include "HYDROData_Tool.h" #include "HYDROData_PolylineXY.h" +#include + #include #include #include @@ -346,33 +348,40 @@ TCollection_AsciiString HYDROData_Profile::GetFilePath() const return aRes; } -bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc, - const TCollection_AsciiString& theFileName ) +int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc, + const TCollection_AsciiString& theFileName, + NCollection_Sequence& theBadProfilesIds ) { if ( theDoc.IsNull() || theFileName.IsEmpty() ) - return false; + return 0; OSD_File aFile( theFileName ); if ( !aFile.IsReadable() ) - return false; + return 0; aFile.Open( OSD_ReadOnly, OSD_Protection() ); if ( !aFile.IsOpen() ) - return false; + return 0; NCollection_Sequence aCreatedProfiles; + int aProfileId = 1; Handle(HYDROData_Profile) aNewProfile; - while ( !aFile.IsAtEnd() ) + for ( ; !aFile.IsAtEnd(); ++aProfileId ) { if ( aNewProfile.IsNull() ) aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) ); - if ( aNewProfile->ImportFromFile( aFile ) ) + bool anIsRead = false; + if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) ) { aCreatedProfiles.Append( aNewProfile ); aNewProfile.Nullify(); } + else if ( anIsRead ) + { + theBadProfilesIds.Append( aProfileId ); + } } if ( !aNewProfile.IsNull() ) @@ -393,11 +402,15 @@ bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() ); } - return !aCreatedProfiles.IsEmpty(); + return aCreatedProfiles.Length(); } -bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName ) +bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName, + bool* theIsRead ) { + if ( theIsRead ) + *theIsRead = false; + // Try to open the file OSD_File aFile( theFileName ); if ( !aFile.IsReadable() ) @@ -407,7 +420,7 @@ bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileNa if ( !aFile.IsOpen() ) return false; - bool aRes = ImportFromFile( aFile ); + bool aRes = ImportFromFile( aFile, theIsRead ); // Close the file aFile.Close(); @@ -421,8 +434,12 @@ bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileNa return aRes; } -bool HYDROData_Profile::ImportFromFile( OSD_File& theFile ) +bool HYDROData_Profile::ImportFromFile( OSD_File& theFile, + bool* theIsRead ) { + if ( theIsRead ) + *theIsRead = false; + if ( !theFile.IsOpen() ) return false; @@ -450,6 +467,10 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile ) break; // Next profile started } + // Set flag of read status to true + if ( theIsRead ) + *theIsRead = true; + TCollection_AsciiString aValX = aLine.Token( " \t", 1 ); TCollection_AsciiString aValY = aLine.Token( " \t", 2 ); TCollection_AsciiString aValZ = aLine.Token( " \t", 3 ); @@ -470,6 +491,10 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile ) double aCoordX = aValX.RealValue(); double aCoordY = aValY.RealValue(); + if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) || + boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) ) + aRes = false; + if ( anIsParametric ) { if ( aCoordX < aPrevVal ) @@ -493,6 +518,8 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile ) } double aCoordZ = aValZ.RealValue(); + if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) ) + aRes = false; ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ ); aPointsXYZ.Append( aPoint ); diff --git a/src/HYDROData/HYDROData_Profile.h b/src/HYDROData/HYDROData_Profile.h index 48c944b6..62ecc741 100644 --- a/src/HYDROData/HYDROData_Profile.h +++ b/src/HYDROData/HYDROData_Profile.h @@ -189,24 +189,29 @@ public: * - georeferenced presentation of profile (3 points in line X,Y,Z) * Create as many objects as many profiles in the file are defined. * \param theFileName the path to file - * \return \c true if file has been successfully read + * \return \c number of successfully imported profiles */ - HYDRODATA_EXPORT static bool ImportFromFile( const Handle(HYDROData_Document)& theDoc, - const TCollection_AsciiString& theFileName ); + HYDRODATA_EXPORT static int ImportFromFile( const Handle(HYDROData_Document)& theDoc, + const TCollection_AsciiString& theFileName, + NCollection_Sequence& theBadProfilesIds ); /** * Imports Profile data from file. * \param theFileName the path to file + * \param theIsRead set to true if at least one non empty string was read from file * \return \c true if file has been successfully read */ - HYDRODATA_EXPORT virtual bool ImportFromFile( const TCollection_AsciiString& theFileName ); + HYDRODATA_EXPORT virtual bool ImportFromFile( const TCollection_AsciiString& theFileName, + bool* theIsRead = 0 ); /** * Imports Profile data from file. * \param theFile file to read + * \param theIsRead set to true if at least one non empty string was read from file * \return \c true if file has been successfully read */ - HYDRODATA_EXPORT virtual bool ImportFromFile( OSD_File& theFile ); + HYDRODATA_EXPORT virtual bool ImportFromFile( OSD_File& theFile, + bool* theIsRead = 0 ); protected: diff --git a/src/HYDROGUI/HYDROGUI_ImportProfilesOp.cxx b/src/HYDROGUI/HYDROGUI_ImportProfilesOp.cxx index 10f16236..2e05ab91 100644 --- a/src/HYDROGUI/HYDROGUI_ImportProfilesOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ImportProfilesOp.cxx @@ -25,6 +25,7 @@ #include "HYDROGUI_DataModel.h" #include "HYDROGUI_Module.h" #include "HYDROGUI_UpdateFlags.h" +#include "HYDROGUI_Tool.h" #include @@ -33,7 +34,9 @@ #include #include +#include +#include #include HYDROGUI_ImportProfilesOp::HYDROGUI_ImportProfilesOp( HYDROGUI_Module* theModule ) @@ -61,35 +64,85 @@ 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 ); + + 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 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 | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init; + commitDocOperation(); + commit(); - return aRes; + module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init ); + } + + QApplication::restoreOverrideCursor(); } + diff --git a/src/HYDROGUI/HYDROGUI_ImportProfilesOp.h b/src/HYDROGUI/HYDROGUI_ImportProfilesOp.h index 4779988d..b7c876c8 100644 --- a/src/HYDROGUI/HYDROGUI_ImportProfilesOp.h +++ b/src/HYDROGUI/HYDROGUI_ImportProfilesOp.h @@ -38,7 +38,7 @@ public: protected: virtual void startOperation(); - virtual bool processApply( int& theUpdateFlags, QString& theErrorMsg ); + virtual void onApply(); private: SUIT_FileDlg* myFileDlg; diff --git a/src/HYDROGUI/resources/HYDROGUI_msg_en.ts b/src/HYDROGUI/resources/HYDROGUI_msg_en.ts index 9882e9b6..462d0c50 100644 --- a/src/HYDROGUI/resources/HYDROGUI_msg_en.ts +++ b/src/HYDROGUI/resources/HYDROGUI_msg_en.ts @@ -1678,9 +1678,21 @@ file cannot be correctly imported for an Obstacle definition. PROFILE_FILTER All files (*.* *) + + BAD_PROFILE_IDS + numbers of bad profiles + + + BAD_IMPORTED_PROFILES_FILES_TLT + Profiles import error + + + NO_ONE_PROFILE_IMPORTED + No one profile has been import from selected file(s). + BAD_IMPORTED_PROFILES_FILES - The following files cannot be correctly imported for a Profile definition: + The data from following files cannot be completely imported: %1 -- 2.39.2