Salome HOME
#571 - Land Cover: calculation of Strickler coefficient
[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             QStringList aTypes = myObject->GetTypes();
64             for ( QStringList::iterator it = aTypes.begin(); it != aTypes.end(); ++it )
65                 aData.append( HYDROGUI_StricklerTableDlg::StricklerCoefficient( *it, myObject->Get( *it, 0 ) ) );
66
67             aPanel->setData( aData );
68         }
69     }
70     else if ( isExport() )
71     {
72         if ( !myObject.IsNull() )
73             aPanel->setTableName( myObject->GetName() );
74     }
75     aPanel->setTableNameReadOnly( isExport() );
76 }
77
78 void HYDROGUI_StricklerTableOp::abortOperation()
79 {
80     HYDROGUI_Operation::abortOperation();
81 }
82
83 void HYDROGUI_StricklerTableOp::commitOperation()
84 {
85     HYDROGUI_Operation::commitOperation();
86 }
87
88 HYDROGUI_InputPanel* HYDROGUI_StricklerTableOp::createInputPanel() const
89 {
90     int type = isEdit() ? HYDROGUI_StricklerTableDlg::Edit : ( isImport() ? HYDROGUI_StricklerTableDlg::Import : HYDROGUI_StricklerTableDlg::Export );
91     HYDROGUI_StricklerTableDlg* aPanel = new HYDROGUI_StricklerTableDlg( module(), getName(), type );
92     connect( aPanel, SIGNAL( fileSelected( const QString& ) ), SLOT( onFileSelected() ) );
93     return aPanel;
94 }
95
96 bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
97     QStringList& theBrowseObjectsEntries )
98 {
99     HYDROGUI_StricklerTableDlg* aPanel = ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
100     if ( !aPanel )
101         return false;
102
103     QString aFilePath;
104     if( !isEdit() )
105     {
106         aFilePath = aPanel->getFileName();
107         if ( aFilePath.isEmpty() )
108         {
109             theErrorMsg = tr( "SELECT_STRICKLER_TABLE_FILE" ).arg( aFilePath );
110             return false;
111         }
112     }
113
114     QString aStricklerTableName = aPanel->getTableName().simplified();
115     if ( aStricklerTableName.isEmpty() )
116     {
117         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
118         return false;
119     }
120
121     if ( isImport() || ( isEdit() && !myObject.IsNull() && myObject->GetName() != aStricklerTableName ) )
122     {
123         // check that there are no other objects with the same name in the document
124         Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aStricklerTableName );
125         if( !anObject.IsNull() )
126         {
127             theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aStricklerTableName );
128             return false;
129         }
130     }
131
132     Handle(HYDROData_StricklerTable) aStricklerTableObj;
133     if ( isImport() )
134     {
135         aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( doc()->CreateObject( KIND_STRICKLER_TABLE ) );
136         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
137         theBrowseObjectsEntries.append( anEntry );
138     }
139     else
140         aStricklerTableObj = myObject;
141
142     if ( aStricklerTableObj.IsNull() )
143         return false;
144
145     if ( isExport() )
146     {
147         bool res = false;
148         QString aFilePath = aPanel->getFileName().simplified();
149         if ( !aFilePath.isEmpty() )
150             res = aStricklerTableObj->Export( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
151         return res;
152     }
153
154     startDocOperation();
155
156     aStricklerTableObj->SetName( aStricklerTableName );
157
158     if( isEdit() )
159     {
160         // Get data from input panel's table and save it into data model object
161         aStricklerTableObj->Clear();
162         HYDROGUI_StricklerTableDlg::StricklerCoefficientList aData = aPanel->getData();
163         for ( HYDROGUI_StricklerTableDlg::StricklerCoefficientList::iterator it = aData.begin(); it != aData.end(); ++it )
164         {
165             const HYDROGUI_StricklerTableDlg::StricklerCoefficient& anInfo = *it;
166             aStricklerTableObj->Set( anInfo.myType, anInfo.myCoefficient );
167         }
168     }
169     else
170     {
171         // Import data from Strickler table file into data model object
172         aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
173     }
174
175     aStricklerTableObj->Update();
176
177     commitDocOperation();
178
179     if( !isEdit() )
180     {
181         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
182         theBrowseObjectsEntries.append( anEntry );
183     }
184
185     theUpdateFlags |= UF_ObjBrowser;
186
187     return true;
188 }
189
190 void HYDROGUI_StricklerTableOp::onFileSelected()
191 {
192     HYDROGUI_StricklerTableDlg* aPanel = 
193         ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
194     if ( !aPanel )
195         return;
196
197     QString anStricklerTableName = aPanel->getTableName().simplified();
198     if ( anStricklerTableName.isEmpty() )
199     {
200         anStricklerTableName = aPanel->getFileName();
201         if ( !anStricklerTableName.isEmpty() ) {
202             anStricklerTableName = QFileInfo( anStricklerTableName ).baseName();
203         }
204
205         if ( anStricklerTableName.isEmpty() ) {
206             anStricklerTableName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
207         }
208         aPanel->setTableName( anStricklerTableName );
209     }
210 }
211
212 bool HYDROGUI_StricklerTableOp::isEdit() const
213 {
214     return myType == EditStricklerTableId;
215 }
216
217 bool HYDROGUI_StricklerTableOp::isImport() const
218 {
219     return myType == ImportStricklerTableFromFileId;
220 }
221
222 bool HYDROGUI_StricklerTableOp::isExport() const
223 {
224     return myType == ExportStricklerTableFromFileId;
225 }