Salome HOME
Python console has been added for HYDRO module (Bug #22).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_CalculationOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_CalculationDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Polyline.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_UpdateFlags.h>
35
36 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
37 : HYDROGUI_Operation( theModule ),
38   myIsEdit( theIsEdit )
39 {
40   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
41 }
42
43 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
44 {
45 }
46
47 void HYDROGUI_CalculationOp::startOperation()
48 {
49   HYDROGUI_Operation::startOperation();
50
51   HYDROGUI_CalculationDlg* aPanel = 
52     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
53   if ( !aPanel )
54     return;
55
56   aPanel->reset();
57
58   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
59
60   myEditedObject.Nullify();
61   if ( myIsEdit )
62   {
63     myEditedObject = Handle(HYDROData_Calculation)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
64     if ( !myEditedObject.IsNull() )
65     {
66       anObjectName = myEditedObject->GetName();
67
68       Handle(HYDROData_Polyline) aBoundaryPolyline = myEditedObject->GetBoundaryPolyline();
69       if ( !aBoundaryPolyline.IsNull() )
70       {
71         QString aPolylineName = aBoundaryPolyline->GetName();
72         aPanel->setPolylineName( aPolylineName );
73       }
74     }
75   }
76
77   aPanel->setObjectName( anObjectName );
78 }
79
80 void HYDROGUI_CalculationOp::abortOperation()
81 {
82   HYDROGUI_Operation::abortOperation();
83 }
84
85 void HYDROGUI_CalculationOp::commitOperation()
86 {
87   HYDROGUI_Operation::commitOperation();
88 }
89
90 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
91 {
92   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
93   return aPanel;
94 }
95
96 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
97                                            QString& theErrorMsg )
98 {
99   HYDROGUI_CalculationDlg* aPanel = 
100     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
101   if ( !aPanel )
102     return false;
103
104   QString anObjectName = aPanel->getObjectName().simplified();
105   if ( anObjectName.isEmpty() )
106   {
107     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
108     return false;
109   }
110
111   // check that there are no other objects with the same name in the document
112   Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
113   if ( !anObject.IsNull() )
114   {
115     theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
116     return false;
117   }
118
119   Handle(HYDROData_Calculation) aCalculObj = myIsEdit ? myEditedObject :
120     Handle(HYDROData_Calculation)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
121   if ( aCalculObj.IsNull() )
122     return false;
123
124   aCalculObj->SetName( anObjectName );
125
126   Handle(HYDROData_Polyline) aBndPolyline;
127
128   QString aPolylineName = aPanel->getPolylineName();
129   if ( !aPolylineName.isEmpty() )
130   {
131     aBndPolyline = Handle(HYDROData_Polyline)::DownCast(
132       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
133   }
134
135   aCalculObj->SetBoundaryPolyline( aBndPolyline );
136
137   theUpdateFlags = UF_Model;
138
139   return true;
140 }
141
142