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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_StricklerTableOp.h"
21 #include "HYDROGUI_StricklerTableDlg.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_Operations.h"
24 #include "HYDROGUI_Tool.h"
26 #include <HYDROData_Document.h>
28 #include <LightApp_UpdateFlags.h>
32 HYDROGUI_StricklerTableOp::HYDROGUI_StricklerTableOp( HYDROGUI_Module* theModule, int theType )
33 : HYDROGUI_Operation( theModule ),
36 setName( isEdit() ? tr( "EDIT_STRICKLER_TABLE" ) : tr( "IMPORT_STRICKLER_TABLE" ) );
39 HYDROGUI_StricklerTableOp::~HYDROGUI_StricklerTableOp()
43 void HYDROGUI_StricklerTableOp::startOperation()
45 HYDROGUI_Operation::startOperation();
47 HYDROGUI_StricklerTableDlg* aPanel = (HYDROGUI_StricklerTableDlg*)inputPanel();
50 if ( !isImport() && isApplyAndClose() )
51 myObject = Handle(HYDROData_StricklerTable)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
55 if ( !myObject.IsNull() )
57 // Edit selected Strickler table
58 aPanel->setTableName( myObject->GetName() );
60 // Get Strickler table data from the data model
61 HYDROGUI_StricklerTableDlg::StricklerCoefficientList aData;
62 TColStd_SequenceOfExtendedString aTypes = myObject->GetTypes();
63 for ( int i = 1; i <= aTypes.Length(); i++ )
64 aData.append( HYDROGUI_StricklerTableDlg::StricklerCoefficient( HYDROGUI_Tool::ToQString( aTypes.Value( i ) ),
65 myObject->Get( aTypes.Value( i ), 0 ) ) );
67 aPanel->setData( aData );
70 else if ( isExport() )
72 if ( !myObject.IsNull() )
73 aPanel->setTableName( myObject->GetName() );
75 aPanel->setTableNameReadOnly( isExport() );
78 void HYDROGUI_StricklerTableOp::abortOperation()
80 HYDROGUI_Operation::abortOperation();
83 void HYDROGUI_StricklerTableOp::commitOperation()
85 HYDROGUI_Operation::commitOperation();
88 HYDROGUI_InputPanel* HYDROGUI_StricklerTableOp::createInputPanel() const
90 int type = isEdit() ? HYDROGUI_StricklerTableDlg::Edit : ( isImport() ? HYDROGUI_StricklerTableDlg::Import : HYDROGUI_StricklerTableDlg::Export );
91 HYDROGUI_StricklerTableDlg* aPanel = new HYDROGUI_StricklerTableDlg( module(), getName(), type );
92 connect( aPanel, SIGNAL( fileSelected( const QString& ) ), SLOT( onFileSelected() ) );
96 bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
97 QStringList& theBrowseObjectsEntries )
99 HYDROGUI_StricklerTableDlg* aPanel = ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
106 aFilePath = aPanel->getFileName();
107 if ( aFilePath.isEmpty() )
109 theErrorMsg = tr( "SELECT_STRICKLER_TABLE_FILE" ).arg( aFilePath );
114 QString aStricklerTableName = aPanel->getTableName().simplified();
115 if ( aStricklerTableName.isEmpty() )
117 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
121 if ( isImport() || ( isEdit() && !myObject.IsNull() && myObject->GetName() != aStricklerTableName ) )
123 // check that there are no other objects with the same name in the document
124 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aStricklerTableName );
125 if( !anObject.IsNull() )
127 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aStricklerTableName );
132 Handle(HYDROData_StricklerTable) aStricklerTableObj;
135 aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( doc()->CreateObject( KIND_STRICKLER_TABLE ) );
136 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
137 theBrowseObjectsEntries.append( anEntry );
140 aStricklerTableObj = myObject;
142 if ( aStricklerTableObj.IsNull() )
148 QString aFilePath = aPanel->getFileName().simplified();
149 if ( !aFilePath.isEmpty() )
150 res = aStricklerTableObj->Export( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
156 aStricklerTableObj->SetName( aStricklerTableName );
160 // Get data from input panel's table and save it into data model object
161 aStricklerTableObj->Clear();
162 HYDROGUI_StricklerTableDlg::StricklerCoefficientList aData = aPanel->getData();
163 for ( HYDROGUI_StricklerTableDlg::StricklerCoefficientList::iterator it = aData.begin(); it != aData.end(); ++it )
165 const HYDROGUI_StricklerTableDlg::StricklerCoefficient& anInfo = *it;
166 aStricklerTableObj->Set( HYDROGUI_Tool::ToExtString( anInfo.myType ), anInfo.myCoefficient );
171 // Import data from Strickler table file into data model object
172 aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
175 aStricklerTableObj->Update();
177 commitDocOperation();
181 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj );
182 theBrowseObjectsEntries.append( anEntry );
185 theUpdateFlags |= UF_ObjBrowser;
190 void HYDROGUI_StricklerTableOp::onFileSelected()
192 HYDROGUI_StricklerTableDlg* aPanel =
193 ::qobject_cast<HYDROGUI_StricklerTableDlg*>( inputPanel() );
197 QString anStricklerTableName = aPanel->getTableName().simplified();
198 if ( anStricklerTableName.isEmpty() )
200 anStricklerTableName = aPanel->getFileName();
201 if ( !anStricklerTableName.isEmpty() ) {
202 anStricklerTableName = QFileInfo( anStricklerTableName ).baseName();
205 if ( anStricklerTableName.isEmpty() ) {
206 anStricklerTableName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
208 aPanel->setTableName( anStricklerTableName );
212 bool HYDROGUI_StricklerTableOp::isEdit() const
214 return myType == EditStricklerTableId;
217 bool HYDROGUI_StricklerTableOp::isImport() const
219 return myType == ImportStricklerTableFromFileId;
222 bool HYDROGUI_StricklerTableOp::isExport() const
224 return myType == ExportStricklerTableFromFileId;