Salome HOME
Merge remote-tracking branch 'origin/BR_SINUSX_FORMAT' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StricklerTableOp.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_StricklerTableOp.h"
20
21 #include "HYDROGUI_StricklerTableDlg.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_Operations.h"
24 #include "HYDROGUI_Tool.h"
25
26 #include <HYDROData_Document.h>
27
28 #include <LightApp_UpdateFlags.h>
29
30 #include <QFileInfo>
31
32 HYDROGUI_StricklerTableOp::HYDROGUI_StricklerTableOp( HYDROGUI_Module* theModule, int theType )
33     : HYDROGUI_Operation( theModule ), 
34     myType( theType )
35 {
36     setName( isEdit() ? tr( "EDIT_STRICKLER_TABLE" ) :
37                         ( isImport() ? tr( "IMPORT_STRICKLER_TABLE" ) : tr( "EXPORT_STRICKLER_TABLE" ) ) );
38 }
39
40 HYDROGUI_StricklerTableOp::~HYDROGUI_StricklerTableOp()
41 {
42 }
43
44 void HYDROGUI_StricklerTableOp::startOperation()
45 {   
46     HYDROGUI_Operation::startOperation();
47
48     HYDROGUI_StricklerTableDlg* aPanel = (HYDROGUI_StricklerTableDlg*)inputPanel();
49     aPanel->reset();
50
51     if ( !isImport() && isApplyAndClose() )
52         myObject = Handle(HYDROData_StricklerTable)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
53
54     if ( isEdit() )
55     {
56         if ( !myObject.IsNull() )
57         {
58             // Edit selected Strickler table
59             aPanel->setTableName( myObject->GetName() );
60
61             // Get Strickler table data from the data model
62             HYDROGUI_StricklerTableDlg::StricklerCoefficientList aData;
63             TColStd_SequenceOfExtendedString aTypes = myObject->GetTypes();
64             for ( int i = 1; i <= aTypes.Length(); i++ )
65                 aData.append( HYDROGUI_StricklerTableDlg::StricklerCoefficient( HYDROGUI_Tool::ToQString( aTypes.Value( i ) ),
66                 myObject->Get( aTypes.Value( i ), 0 ) ) );
67
68             aPanel->setData( aData );
69         }
70     }
71     else if ( isExport() )
72     {
73         if ( !myObject.IsNull() )
74             aPanel->setTableName( myObject->GetName() );
75     }
76     aPanel->setTableNameReadOnly( isExport() );
77 }
78
79 void HYDROGUI_StricklerTableOp::abortOperation()
80 {
81     HYDROGUI_Operation::abortOperation();
82 }
83
84 void HYDROGUI_StricklerTableOp::commitOperation()
85 {
86     HYDROGUI_Operation::commitOperation();
87 }
88
89 HYDROGUI_InputPanel* HYDROGUI_StricklerTableOp::createInputPanel() const
90 {
91     int type = isEdit() ? HYDROGUI_StricklerTableDlg::Edit : ( isImport() ? HYDROGUI_StricklerTableDlg::Import : HYDROGUI_StricklerTableDlg::Export );
92     HYDROGUI_StricklerTableDlg* aPanel = new HYDROGUI_StricklerTableDlg( module(), getName(), type );
93     connect( aPanel, SIGNAL( fileSelected( const QString& ) ), SLOT( onFileSelected() ) );
94     return aPanel;
95 }
96
97 bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
98     QStringList& theBrowseObjectsEntries )
99 {
100     HYDROGUI_StricklerTableDlg* aPanel = ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
101     if ( !aPanel )
102         return false;
103
104     QString aFilePath;
105     if( !isEdit() )
106     {
107         aFilePath = aPanel->getFileName();
108         if ( aFilePath.isEmpty() )
109         {
110             theErrorMsg = tr( "SELECT_STRICKLER_TABLE_FILE" ).arg( aFilePath );
111             return false;
112         }
113     }
114
115     QString aStricklerTableName = aPanel->getTableName().simplified();
116     if ( aStricklerTableName.isEmpty() )
117     {
118         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
119         return false;
120     }
121
122     if ( isImport() || ( isEdit() && !myObject.IsNull() && myObject->GetName() != aStricklerTableName ) )
123     {
124         // check that there are no other objects with the same name in the document
125         Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aStricklerTableName );
126         if( !anObject.IsNull() )
127         {
128             theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aStricklerTableName );
129             return false;
130         }
131     }
132
133     Handle(HYDROData_StricklerTable) aStricklerTableObj;
134     if ( isImport() )
135     {
136         aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( doc()->CreateObject( KIND_STRICKLER_TABLE ) );
137         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
138         theBrowseObjectsEntries.append( anEntry );
139     }
140     else
141         aStricklerTableObj = myObject;
142
143     if ( aStricklerTableObj.IsNull() )
144         return false;
145
146     if ( isExport() )
147     {
148         bool res = false;
149         QString aFilePath = aPanel->getFileName().simplified();
150         if ( !aFilePath.isEmpty() )
151             res = aStricklerTableObj->Export( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
152         return res;
153     }
154
155     startDocOperation();
156
157     aStricklerTableObj->SetName( aStricklerTableName );
158
159     if( isEdit() )
160     {
161         // Get data from input panel's table and save it into data model object
162         aStricklerTableObj->Clear();
163         HYDROGUI_StricklerTableDlg::StricklerCoefficientList aData = aPanel->getData();
164         for ( HYDROGUI_StricklerTableDlg::StricklerCoefficientList::iterator it = aData.begin(); it != aData.end(); ++it )
165         {
166             const HYDROGUI_StricklerTableDlg::StricklerCoefficient& anInfo = *it;
167             aStricklerTableObj->Set( HYDROGUI_Tool::ToExtString( anInfo.myType ), anInfo.myCoefficient );
168         }
169     }
170     else
171     {
172         // Import data from Strickler table file into data model object
173         aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
174     }
175
176     aStricklerTableObj->Update();
177
178     commitDocOperation();
179
180     if( !isEdit() )
181     {
182         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
183         theBrowseObjectsEntries.append( anEntry );
184     }
185
186     theUpdateFlags |= UF_ObjBrowser;
187
188     return true;
189 }
190
191 void HYDROGUI_StricklerTableOp::onFileSelected()
192 {
193     HYDROGUI_StricklerTableDlg* aPanel = 
194         ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
195     if ( !aPanel )
196         return;
197
198     QString anStricklerTableName = aPanel->getTableName().simplified();
199     if ( anStricklerTableName.isEmpty() )
200     {
201         anStricklerTableName = aPanel->getFileName();
202         if ( !anStricklerTableName.isEmpty() ) {
203             anStricklerTableName = QFileInfo( anStricklerTableName ).baseName();
204         }
205
206         if ( anStricklerTableName.isEmpty() ) {
207             anStricklerTableName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
208         }
209         aPanel->setTableName( anStricklerTableName );
210     }
211 }
212
213 bool HYDROGUI_StricklerTableOp::isEdit() const
214 {
215     return myType == EditStricklerTableId;
216 }
217
218 bool HYDROGUI_StricklerTableOp::isImport() const
219 {
220     return myType == ImportStricklerTableFromFileId;
221 }
222
223 bool HYDROGUI_StricklerTableOp::isExport() const
224 {
225     return myType == ExportStricklerTableFromFileId;
226 }