Salome HOME
2e05ab91315e0ec50dbc81b7e49b457f92ddecaf
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportProfilesOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ImportProfilesOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29
30 #include <HYDROData_Profile.h>
31
32 #include <LightApp_Application.h>
33 #include <LightApp_UpdateFlags.h>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_FileDlg.h>
37 #include <SUIT_MessageBox.h>
38
39 #include <QApplication>
40 #include <QFileInfo>
41
42 HYDROGUI_ImportProfilesOp::HYDROGUI_ImportProfilesOp( HYDROGUI_Module* theModule )
43 : HYDROGUI_Operation( theModule )
44 {
45   setName( tr( "IMPORT_PROFILES" ) );
46 }
47
48 HYDROGUI_ImportProfilesOp::~HYDROGUI_ImportProfilesOp()
49 {
50 }
51
52 void HYDROGUI_ImportProfilesOp::startOperation()
53 {
54   HYDROGUI_Operation::startOperation();
55
56   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
57   myFileDlg->setWindowTitle( getName() );
58   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
59   myFileDlg->setFilter( tr("PROFILE_FILTER") );
60
61   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
62   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
63
64   myFileDlg->exec();
65 }
66
67 void HYDROGUI_ImportProfilesOp::onApply()
68 {
69   if ( !myFileDlg )
70   {
71     abort();
72     return;
73   }
74
75   // Get the selected files
76   QStringList aFileNames = myFileDlg->selectedFiles();
77   if ( aFileNames.isEmpty() )
78   {
79     abort();
80     return;
81   }
82
83   QApplication::setOverrideCursor( Qt::WaitCursor );
84
85   startDocOperation();
86
87   // Import each of the selected files, fill the list of bad imported files
88   QString aBadImportedFiles;
89   int aTotalNbImported = 0;
90
91   foreach ( const QString& aFileName, aFileNames )
92   {
93     NCollection_Sequence<int> aBadProfilesIds;
94     TCollection_AsciiString anAsciiFileName = HYDROGUI_Tool::ToAsciiString( aFileName );
95     
96     int aNbImported = HYDROData_Profile::ImportFromFile( doc(), anAsciiFileName, aBadProfilesIds );
97     if ( aNbImported == 0 || !aBadProfilesIds.IsEmpty() )
98     {
99       aBadImportedFiles += QFileInfo( aFileName ).fileName();
100       if ( !aBadProfilesIds.IsEmpty() && aNbImported > 0 )
101       {
102         aBadImportedFiles += ", " + tr( "BAD_PROFILE_IDS" ) + " : ";
103         for ( int i = 1, n = aBadProfilesIds.Length(); i <= n; ++i )
104           aBadImportedFiles += QString::number( aBadProfilesIds.Value( i ) ) + ", ";
105         aBadImportedFiles.remove( aBadImportedFiles.length() - 2, 2 );
106       }
107       aBadImportedFiles += ";\n";
108     }
109
110     aTotalNbImported += aNbImported;
111   }
112
113   if ( aTotalNbImported == 0 )
114   {
115     QApplication::restoreOverrideCursor();
116   
117     SUIT_MessageBox::critical( module()->getApp()->desktop(),
118                                tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
119                                tr( "NO_ONE_PROFILE_IMPORTED" ) );
120
121     QApplication::setOverrideCursor( Qt::WaitCursor );
122
123     abortDocOperation();
124     abort();
125   }
126   else
127   {
128     if ( !aBadImportedFiles.isEmpty() )
129     {
130       QApplication::restoreOverrideCursor();
131
132       aBadImportedFiles = aBadImportedFiles.simplified();
133       SUIT_MessageBox::warning( module()->getApp()->desktop(),
134                                 tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
135                                 tr( "BAD_IMPORTED_PROFILES_FILES" ).arg( aBadImportedFiles ) );
136
137       QApplication::setOverrideCursor( Qt::WaitCursor );
138     }
139
140     commitDocOperation();
141     commit();
142
143     module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
144   }
145
146   QApplication::restoreOverrideCursor();
147 }
148