Salome HOME
The data model has been redesigned to new format.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TwoImagesOp.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_TwoImagesOp.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_TwoImagesDlg.h"
28 #include "HYDROGUI_UpdateFlags.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Image.h>
32 #include <HYDROData_Polyline.h>
33
34 #include <HYDROData_OperationsFactory.h>
35
36 #include <ImageComposer_CutOperator.h>
37 #include <ImageComposer_CropOperator.h>
38 #include <ImageComposer_FuseOperator.h>
39
40 HYDROGUI_TwoImagesOp::HYDROGUI_TwoImagesOp( HYDROGUI_Module* theModule,
41                                             const int theType,
42                                             const bool theIsEdit )
43 : HYDROGUI_Operation( theModule ),
44   myType( theType ),
45   myIsEdit( theIsEdit ),
46   myEditedObject( 0 )
47 {
48   QString aName;
49   switch( myType )
50   {
51     case Fuse: aName = theIsEdit ? tr( "EDIT_FUSED_IMAGE" ) : tr( "FUSE_IMAGES" ); break;
52     case Cut: aName = theIsEdit ? tr( "EDIT_CUT_IMAGE" ) : tr( "CUT_IMAGES" ); break;
53     case Split: aName = theIsEdit ? tr( "EDIT_SPLITTED_IMAGE" ) : tr( "SPLIT_IMAGE" ); break;
54     default: break;
55   }
56   setName( aName );
57 }
58
59 HYDROGUI_TwoImagesOp::~HYDROGUI_TwoImagesOp()
60 {
61 }
62
63 HYDROGUI_InputPanel* HYDROGUI_TwoImagesOp::createInputPanel() const
64 {
65   return new HYDROGUI_TwoImagesDlg( module(), getName() );
66 }
67
68 void HYDROGUI_TwoImagesOp::startOperation()
69 {
70   HYDROGUI_Operation::startOperation();
71
72   HYDROGUI_TwoImagesDlg* aPanel = (HYDROGUI_TwoImagesDlg*)inputPanel();
73   aPanel->reset();
74
75   if( myType == Fuse || myType == Cut )
76     aPanel->setMode( HYDROGUI_TwoImagesDlg::TwoImages, myIsEdit );
77   else if( myType == Split )
78     aPanel->setMode( HYDROGUI_TwoImagesDlg::ImageAndPolyline, myIsEdit );
79
80   QString anImageName;
81   if( myIsEdit )
82   {
83     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
84     if( !myEditedObject.IsNull() )
85       anImageName = myEditedObject->GetName();
86   }
87   else
88   {
89     QString aPrefix;
90     switch( myType )
91     {
92       case Fuse: aPrefix = tr( "FUSE" ); break;
93       case Cut: aPrefix = tr( "CUT" ); break;
94       case Split: aPrefix = tr( "SPLIT" ); break;
95       default: break;
96     }
97     anImageName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix );
98   }
99   aPanel->setImageName( anImageName );
100
101   QString aSelectedName1, aSelectedName2;
102   if( myIsEdit && !myEditedObject.IsNull() )
103   {
104     if( myEditedObject->NbReferences() > 0 )
105     {
106       Handle(HYDROData_Entity) anObject1 = myEditedObject->Reference( 0 );
107       if( !anObject1.IsNull() )
108         aSelectedName1 = anObject1->GetName();
109     }
110     if( myEditedObject->NbReferences() > 1 )
111     {
112       Handle(HYDROData_Entity) anObject2 = myEditedObject->Reference( 1 );
113       if( !anObject2.IsNull() )
114         aSelectedName2 = anObject2->GetName();
115     }
116     aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
117
118     HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
119     if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
120     {
121       QColor aColor;
122       anOperator->getArgs( aColor );
123       aPanel->setColor( aColor );
124     }
125   }
126   else if( !myIsEdit )
127   {
128     Handle(HYDROData_Image) aSelectedImage =
129       Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
130     if( !aSelectedImage.IsNull() )
131     {
132       QString aSelectedName = aSelectedImage->GetName();
133       aPanel->setPreselectedObject( aSelectedName );
134     }
135   }
136 }
137
138 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
139                                          QString& theErrorMsg )
140 {
141   HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
142
143   bool anIsModifySelected = myType == Split && aPanel->isModifySelected();
144
145   QString anImageName = aPanel->getImageName();
146   if( !anIsModifySelected && anImageName.isEmpty() )
147     return false;
148
149   QString aSelectedName1, aSelectedName2;
150   if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
151     return false;
152
153   if( !anIsModifySelected &&
154       ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) ) )
155   {
156     // check that there are no other objects with the same name in the document
157     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
158     if( !anObject.IsNull() )
159     {
160       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
161       return false;
162     }
163   }
164
165   Handle(HYDROData_Entity) anObject1 =
166     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_UNKNOWN ) ;
167   Handle(HYDROData_Entity) anObject2 =
168     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_UNKNOWN );
169   if( anObject1.IsNull() || anObject2.IsNull() )
170     return false;
171
172   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
173
174   Handle(HYDROData_Image) aResult;
175   ImageComposer_Operator* anOperator = 0;
176   if( myIsEdit )
177   {
178     aResult = myEditedObject;
179     aResult->ClearReferences();
180     anOperator = aFactory->Operator( aResult );
181   }
182   else
183   {
184     QString anOperatorName;
185     switch( myType )
186     {
187       case Fuse:  anOperatorName = ImageComposer_FuseOperator::Type(); break;
188       case Cut:   anOperatorName = ImageComposer_CutOperator::Type(); break;
189       case Split: anOperatorName = ImageComposer_CropOperator::Type(); break;
190       default: break;
191     }
192
193     anOperator = aFactory->Operator( anOperatorName );
194
195     aResult = aFactory->CreateImage( doc(), anOperator );
196   }
197
198   if( aResult.IsNull() || !anOperator )
199     return false;
200
201   // Setting the operator arguments 
202   anOperator->setArgs( aPanel->getColor() );
203   aResult->SetArgs( anOperator->getBinArgs() );
204
205   aResult->SetName( anImageName );
206   aResult->AppendReference( anObject1 );
207   aResult->AppendReference( anObject2 );
208
209   aResult->Update();
210
211   if( anIsModifySelected )
212   {
213     Handle(HYDROData_Image) aSelectedImage = Handle(HYDROData_Image)::DownCast( anObject1 );
214     if( !aSelectedImage.IsNull() )
215     {
216       aSelectedImage->SetIsSelfSplitted( true );
217       aSelectedImage->SetImage( aResult->Image() );
218       aSelectedImage->SetTrsf( aResult->Trsf() );
219       aResult->Remove();
220     }
221   }
222
223   if( !myIsEdit && !anIsModifySelected )
224   {
225     size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
226     module()->setObjectVisible( aViewId, anObject1, false );
227     module()->setObjectVisible( aViewId, anObject2, false );
228     module()->setObjectVisible( aViewId, aResult, true );
229   }
230
231   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
232   return true;
233 }