]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx
Salome HOME
refs #568: Land Cover: a draft of data model and implementation of dialog box and...
[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_Tool.h"
23 #include "HYDROGUI_DataObject.h"
24
25 #include <HYDROData_Document.h>
26
27 #include <LightApp_UpdateFlags.h>
28
29 #include <QFileInfo>
30
31 HYDROGUI_StricklerTableOp::HYDROGUI_StricklerTableOp( HYDROGUI_Module* theModule, bool theIsEdit )
32 : HYDROGUI_Operation( theModule ), 
33   myIsEdit( theIsEdit )
34 {
35   setName( theIsEdit ? tr( "EDIT_STRICKLER_TABLE" ) : tr( "IMPORT_STRICKLER_TABLE" ) );
36 }
37
38
39 HYDROGUI_StricklerTableOp::~HYDROGUI_StricklerTableOp()
40 {
41 }
42
43 void HYDROGUI_StricklerTableOp::startOperation()
44 {   
45   HYDROGUI_Operation::startOperation();
46
47   HYDROGUI_StricklerTableDlg* aPanel = (HYDROGUI_StricklerTableDlg*)inputPanel();
48   aPanel->reset();
49   aPanel->setIsEdit( myIsEdit );
50
51   if( myIsEdit )
52   {
53     if ( isApplyAndClose() )
54           myEditedObject = Handle(HYDROData_StricklerTable)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
55     if ( !myEditedObject.IsNull() )
56     {
57       // Edit selected Strickler table
58
59       aPanel->setStricklerTableName( myEditedObject->GetName() );
60
61       // Get Strickler table data from the data model
62       // ...
63
64       // The code below is a sample of data filling Strickler table
65       HYDROGUI_StricklerTableDlg::StricklerCoefficientList aData;
66       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef1( "Zones de champs, prairies, sans cultures", 20.0 );
67       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef2( "Zones de champs cultivé à végétation basse", 17.5 );
68       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef3( "Zones de champs cultivé à végétation haute", 12.5 );
69       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef4( "Zones d'arbustes, de sous-bois", 10.0 );
70       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef5( "Zones à faible urbanization (bourg)", 9.0 );
71       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef6( "Zones à forte urbanization (agglomération)", 9.0 );
72       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef7( "Canaux naturels", 35.0 );
73       HYDROGUI_StricklerTableDlg::StricklerCoefficient aStricklerCoef8( "Canaux artificiels en béton", 65.0 );
74       aData.append( aStricklerCoef1 );
75       aData.append( aStricklerCoef2 );
76       aData.append( aStricklerCoef3 );
77       aData.append( aStricklerCoef4 );
78       aData.append( aStricklerCoef5 );
79       aData.append( aStricklerCoef6 );
80       aData.append( aStricklerCoef7 );
81       aData.append( aStricklerCoef8 );
82       aPanel->setData(aData);
83     }
84     else
85     {
86       // Import Strickler table from file
87     }
88   }
89 }
90
91 void HYDROGUI_StricklerTableOp::abortOperation()
92 {
93   //...
94
95   HYDROGUI_Operation::abortOperation();
96 }
97
98 void HYDROGUI_StricklerTableOp::commitOperation()
99 {
100   //...
101
102   HYDROGUI_Operation::commitOperation();
103 }
104
105 HYDROGUI_InputPanel* HYDROGUI_StricklerTableOp::createInputPanel() const
106 {
107   HYDROGUI_StricklerTableDlg* aPanel = new HYDROGUI_StricklerTableDlg( module(), getName() );  
108   connect( aPanel, SIGNAL( fileSelected( const QString& ) ), SLOT( onFileSelected() ) );
109   return aPanel;
110 }
111
112 bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
113                                               QStringList& theBrowseObjectsEntries )
114 {
115   HYDROGUI_StricklerTableDlg* aPanel = ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
116   if ( !aPanel )
117     return false;
118
119   QString aFilePath;
120   if( !myIsEdit )
121   {
122     aFilePath = aPanel->getFileName();
123     if ( aFilePath.isEmpty() )
124     {
125       theErrorMsg = tr( "SELECT_STRICKLER_TABLE_FILE" ).arg( aFilePath );
126       return false;
127     }
128   }
129
130   QString aStricklerTableName = aPanel->getStricklerTableName().simplified();
131   if ( aStricklerTableName.isEmpty() )
132   {
133     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
134     return false;
135   }
136
137   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aStricklerTableName ) )
138   {
139     // check that there are no other objects with the same name in the document
140     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aStricklerTableName );
141     if( !anObject.IsNull() )
142     {
143       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aStricklerTableName );
144       return false;
145     }
146   }
147
148   Handle(HYDROData_StricklerTable) aStricklerTableObj;
149   if( myIsEdit )
150     aStricklerTableObj = myEditedObject;
151   else
152   {
153     aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( doc()->CreateObject( KIND_STRICKLER_TABLE ) );
154     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
155     theBrowseObjectsEntries.append( anEntry );
156   }
157
158   if( aStricklerTableObj.IsNull() )
159     return false;
160
161   aStricklerTableObj->SetName( aStricklerTableName );
162
163   if( myIsEdit )
164   {
165     // Get data from input panel's table and save it into data model object
166     
167     // Check, that type names are unique within the current
168     // Strickler table and show error message, if needed
169
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   if( !myIsEdit )
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->getStricklerTableName().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->setStricklerTableName( anStricklerTableName );
210   }
211 }