1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImportProfilesOp.h"
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_UpdateFlags.h"
24 #include "HYDROGUI_Tool.h"
26 #include <HYDROData_Profile.h>
28 #include <LightApp_Application.h>
29 #include <LightApp_UpdateFlags.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_FileDlg.h>
33 #include <SUIT_MessageBox.h>
35 #include <QApplication>
38 HYDROGUI_ImportProfilesOp::HYDROGUI_ImportProfilesOp( HYDROGUI_Module* theModule )
39 : HYDROGUI_Operation( theModule )
41 setName( tr( "IMPORT_PROFILES" ) );
44 HYDROGUI_ImportProfilesOp::~HYDROGUI_ImportProfilesOp()
48 void HYDROGUI_ImportProfilesOp::startOperation()
50 HYDROGUI_Operation::startOperation();
52 myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
53 myFileDlg->setWindowTitle( getName() );
54 myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
55 myFileDlg->setFilter( tr("PROFILE_FILTER") );
57 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
58 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
63 void HYDROGUI_ImportProfilesOp::onApply()
71 // Get the selected files
72 QStringList aFileNames = myFileDlg->selectedFiles();
73 if ( aFileNames.isEmpty() )
79 QApplication::setOverrideCursor( Qt::WaitCursor );
80 QStringList aBrowseObjectsEntries;
81 //TODO: to implement the addition of imported profiles' entries to the list
85 // Import each of the selected files, fill the list of bad imported files
86 QString aBadImportedFiles;
87 int aTotalNbImported = 0;
89 foreach ( const QString& aFileName, aFileNames )
91 NCollection_Sequence<int> aBadProfilesIds;
92 TCollection_AsciiString anAsciiFileName = HYDROGUI_Tool::ToAsciiString( aFileName );
94 int aNbImported = HYDROData_Profile::ImportFromFile( doc(), anAsciiFileName, aBadProfilesIds );
95 if ( aNbImported == 0 || !aBadProfilesIds.IsEmpty() )
97 aBadImportedFiles += QFileInfo( aFileName ).fileName();
98 if ( !aBadProfilesIds.IsEmpty() && aNbImported > 0 )
100 aBadImportedFiles += ", " + tr( "BAD_PROFILE_IDS" ) + " : ";
101 for ( int i = 1, n = aBadProfilesIds.Length(); i <= n; ++i )
102 aBadImportedFiles += QString::number( aBadProfilesIds.Value( i ) ) + ", ";
103 aBadImportedFiles.remove( aBadImportedFiles.length() - 2, 2 );
105 aBadImportedFiles += ";\n";
108 aTotalNbImported += aNbImported;
111 if ( aTotalNbImported == 0 )
113 QApplication::restoreOverrideCursor();
115 SUIT_MessageBox::critical( module()->getApp()->desktop(),
116 tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
117 tr( "NO_ONE_PROFILE_IMPORTED" ) );
119 QApplication::setOverrideCursor( Qt::WaitCursor );
126 if ( !aBadImportedFiles.isEmpty() )
128 QApplication::restoreOverrideCursor();
130 aBadImportedFiles = aBadImportedFiles.simplified();
131 SUIT_MessageBox::warning( module()->getApp()->desktop(),
132 tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
133 tr( "BAD_IMPORTED_PROFILES_FILES" ).arg( aBadImportedFiles ) );
135 QApplication::setOverrideCursor( Qt::WaitCursor );
138 commitDocOperation();
141 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
142 browseObjects( aBrowseObjectsEntries );
145 QApplication::restoreOverrideCursor();