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