Salome HOME
refs #430: incorrect coordinates in dump polyline
[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   QStringList aBrowseObjectsEntries;
85   //TODO: to implement the addition of imported profiles' entries to the list
86
87   startDocOperation();
88
89   // Import each of the selected files, fill the list of bad imported files
90   QString aBadImportedFiles;
91   int aTotalNbImported = 0;
92
93   foreach ( const QString& aFileName, aFileNames )
94   {
95     NCollection_Sequence<int> aBadProfilesIds;
96     TCollection_AsciiString anAsciiFileName = HYDROGUI_Tool::ToAsciiString( aFileName );
97     
98     int aNbImported = HYDROData_Profile::ImportFromFile( doc(), anAsciiFileName, aBadProfilesIds );
99     if ( aNbImported == 0 || !aBadProfilesIds.IsEmpty() )
100     {
101       aBadImportedFiles += QFileInfo( aFileName ).fileName();
102       if ( !aBadProfilesIds.IsEmpty() && aNbImported > 0 )
103       {
104         aBadImportedFiles += ", " + tr( "BAD_PROFILE_IDS" ) + " : ";
105         for ( int i = 1, n = aBadProfilesIds.Length(); i <= n; ++i )
106           aBadImportedFiles += QString::number( aBadProfilesIds.Value( i ) ) + ", ";
107         aBadImportedFiles.remove( aBadImportedFiles.length() - 2, 2 );
108       }
109       aBadImportedFiles += ";\n";
110     }
111
112     aTotalNbImported += aNbImported;
113   }
114
115   if ( aTotalNbImported == 0 )
116   {
117     QApplication::restoreOverrideCursor();
118   
119     SUIT_MessageBox::critical( module()->getApp()->desktop(),
120                                tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
121                                tr( "NO_ONE_PROFILE_IMPORTED" ) );
122
123     QApplication::setOverrideCursor( Qt::WaitCursor );
124
125     abortDocOperation();
126     abort();
127   }
128   else
129   {
130     if ( !aBadImportedFiles.isEmpty() )
131     {
132       QApplication::restoreOverrideCursor();
133
134       aBadImportedFiles = aBadImportedFiles.simplified();
135       SUIT_MessageBox::warning( module()->getApp()->desktop(),
136                                 tr( "BAD_IMPORTED_PROFILES_FILES_TLT" ),
137                                 tr( "BAD_IMPORTED_PROFILES_FILES" ).arg( aBadImportedFiles ) );
138
139       QApplication::setOverrideCursor( Qt::WaitCursor );
140     }
141
142     commitDocOperation();
143     commit();
144
145     module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
146     browseObjects( aBrowseObjectsEntries );
147   }
148
149   QApplication::restoreOverrideCursor();
150 }
151