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