Salome HOME
Corrections of examples path after install with scbi
[modules/hydro.git] / 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 <QFileInfo>
28
29 HYDROGUI_StricklerTableOp::HYDROGUI_StricklerTableOp( HYDROGUI_Module* theModule, bool theIsEdit )
30 : HYDROGUI_Operation( theModule ), 
31   myIsEdit( theIsEdit )
32 {
33   setName( theIsEdit ? tr( "EDIT_STRICKLER_TABLE" ) : tr( "IMPORT_STRICKLER_TABLE" ) );
34 }
35
36
37 HYDROGUI_StricklerTableOp::~HYDROGUI_StricklerTableOp()
38 {
39 }
40
41 void HYDROGUI_StricklerTableOp::startOperation()
42 {   
43   HYDROGUI_Operation::startOperation();
44
45   HYDROGUI_StricklerTableDlg* aPanel = (HYDROGUI_StricklerTableDlg*)inputPanel();
46   //aPanel->reset();
47   //aPanel->setIsEdit( myIsEdit );
48
49   if( myIsEdit )
50   {
51     if ( isApplyAndClose() )
52           myEditedObject = Handle(HYDROData_StricklerTable)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
53     if ( !myEditedObject.IsNull() )
54     {
55       // Edit selected Strickler table
56     }
57     else
58     {
59       // Import Strickler table from file
60     }
61   }
62 }
63
64 void HYDROGUI_StricklerTableOp::abortOperation()
65 {
66   //...
67
68   HYDROGUI_Operation::abortOperation();
69 }
70
71 void HYDROGUI_StricklerTableOp::commitOperation()
72 {
73   //...
74
75   HYDROGUI_Operation::commitOperation();
76 }
77
78 HYDROGUI_InputPanel* HYDROGUI_StricklerTableOp::createInputPanel() const
79 {
80   HYDROGUI_StricklerTableDlg* aPanel = new HYDROGUI_StricklerTableDlg( module(), getName() );  
81   connect( aPanel, SIGNAL( fileSelected( const QString& ) ), SLOT( onFileSelected() ) );
82   return aPanel;
83 }
84
85 bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
86                                               QStringList& theBrowseObjectsEntries )
87 {
88   HYDROGUI_StricklerTableDlg* aPanel = ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
89   if ( !aPanel )
90     return false;
91
92   QString aFilePath;
93   if( !myIsEdit )
94   {
95     aFilePath = aPanel->getFileName();
96     if ( aFilePath.isEmpty() )
97     {
98       theErrorMsg = tr( "SELECT_STRICKLER_TABLE_FILE" ).arg( aFilePath );
99       return false;
100     }
101   }
102
103   QString aStricklerTableName = aPanel->getStricklerTableName().simplified();
104   if ( aStricklerTableName.isEmpty() )
105   {
106     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
107     return false;
108   }
109
110   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aStricklerTableName ) )
111   {
112     // check that there are no other objects with the same name in the document
113     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aStricklerTableName );
114     if( !anObject.IsNull() )
115     {
116       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aStricklerTableName );
117       return false;
118     }
119   }
120
121   Handle(HYDROData_StricklerTable) aStricklerTableObj;
122   if( myIsEdit )
123     aStricklerTableObj = myEditedObject;
124   else
125   {
126     aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( doc()->CreateObject( KIND_STRICKLER_TABLE ) );
127     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
128     theBrowseObjectsEntries.append( anEntry );
129   }
130
131   if( aStricklerTableObj.IsNull() )
132     return false;
133
134   aStricklerTableObj->SetName( aStricklerTableName );
135
136   if( myIsEdit )
137   {
138     // Get data from input panel's table and save it into data model object
139   }
140   else
141   {
142     // Import data from Strickler table file into data model model object
143     aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
144   }
145
146   aStricklerTableObj->Update();
147
148   if( !myIsEdit )
149   {
150     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
151     theBrowseObjectsEntries.append( anEntry );
152   }
153
154   return true;
155 }
156
157 void HYDROGUI_StricklerTableOp::onFileSelected()
158 {
159   HYDROGUI_StricklerTableDlg* aPanel = 
160     ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
161   if ( !aPanel )
162     return;
163
164   QString anStricklerTableName = aPanel->getStricklerTableName().simplified();
165   if ( anStricklerTableName.isEmpty() )
166   {
167     anStricklerTableName = aPanel->getFileName();
168     if ( !anStricklerTableName.isEmpty() ) {
169         anStricklerTableName = QFileInfo( anStricklerTableName ).baseName();
170     }
171
172     if ( anStricklerTableName.isEmpty() ) {
173       anStricklerTableName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
174     }
175     aPanel->setStricklerTableName( anStricklerTableName );
176   }
177 }