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