Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportProfilesOp.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImportProfilesOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_UpdateFlags.h"
24 #include "HYDROGUI_Tool.h"
25
26 #include <HYDROData_Profile.h>
27
28 #include <LightApp_Application.h>
29 #include <LightApp_UpdateFlags.h>
30
31 #include <SUIT_Desktop.h>
32 #include <SUIT_FileDlg.h>
33 #include <SUIT_MessageBox.h>
34
35 #include <QApplication>
36 #include <QFileInfo>
37
38 HYDROGUI_ImportProfilesOp::HYDROGUI_ImportProfilesOp( HYDROGUI_Module* theModule )
39 : HYDROGUI_Operation( theModule )
40 {
41   setName( tr( "IMPORT_PROFILES" ) );
42 }
43
44 HYDROGUI_ImportProfilesOp::~HYDROGUI_ImportProfilesOp()
45 {
46 }
47
48 void HYDROGUI_ImportProfilesOp::startOperation()
49 {
50   HYDROGUI_Operation::startOperation();
51
52   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
53   myFileDlg->setWindowTitle( getName() );
54   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
55   myFileDlg->setNameFilter( tr("PROFILE_FILTER") );
56
57   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
58   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
59
60   myFileDlg->exec();
61 }
62
63 void HYDROGUI_ImportProfilesOp::onApply()
64 {
65   if ( !myFileDlg )
66   {
67     abort();
68     return;
69   }
70
71   // Get the selected files
72   QStringList aFileNames = myFileDlg->selectedFiles();
73   if ( aFileNames.isEmpty() )
74   {
75     abort();
76     return;
77   }
78
79   QApplication::setOverrideCursor( Qt::WaitCursor );
80   QStringList aBrowseObjectsEntries;
81   //TODO: to implement the addition of imported profiles' entries to the list
82
83   startDocOperation();
84
85   // Import each of the selected files, fill the list of bad imported files
86   QString aBadImportedFiles;
87   int aTotalNbImported = 0;
88
89   foreach ( const QString& aFileName, aFileNames )
90   {
91     NCollection_Sequence<int> aBadProfilesIds;
92     TCollection_AsciiString anAsciiFileName = HYDROGUI_Tool::ToAsciiString( aFileName );
93     
94     int aNbImported = HYDROData_Profile::ImportFromFile( doc(), anAsciiFileName, aBadProfilesIds );
95     if ( aNbImported == 0 || !aBadProfilesIds.IsEmpty() )
96     {
97       aBadImportedFiles += QFileInfo( aFileName ).fileName();
98       if ( !aBadProfilesIds.IsEmpty() && aNbImported > 0 )
99       {
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 );
104       }
105       aBadImportedFiles += ";\n";
106     }
107
108     aTotalNbImported += aNbImported;
109   }
110
111   if ( aTotalNbImported == 0 )
112   {
113     QApplication::restoreOverrideCursor();
114   
115     SUIT_MessageBox::critical( module()->getApp()->desktop(),
116                                tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
117                                tr( "NO_ONE_PROFILE_IMPORTED" ) );
118
119     QApplication::setOverrideCursor( Qt::WaitCursor );
120
121     abortDocOperation();
122     abort();
123   }
124   else
125   {
126     if ( !aBadImportedFiles.isEmpty() )
127     {
128       QApplication::restoreOverrideCursor();
129
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 ) );
134
135       QApplication::setOverrideCursor( Qt::WaitCursor );
136     }
137
138     commitDocOperation();
139     commit();
140
141     module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
142     browseObjects( aBrowseObjectsEntries );
143   }
144
145   QApplication::restoreOverrideCursor();
146 }
147