Salome HOME
The code refactoring since the ImageComposer API has been changed (Feature #6).
[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 <HYDROOperations_Factory.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_Object) anObject1 = myEditedObject->Reference( 0 );
107       if( !anObject1.IsNull() )
108         aSelectedName1 = anObject1->GetName();
109     }
110     if( myEditedObject->NbReferences() > 1 )
111     {
112       Handle(HYDROData_Object) anObject2 = myEditedObject->Reference( 1 );
113       if( !anObject2.IsNull() )
114         aSelectedName2 = anObject2->GetName();
115     }
116     aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
117
118     HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
119     if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
120     {
121       QColor aColor;
122       anOperator->getArgs( aColor );
123       aPanel->setColor( aColor );
124     }
125   }
126 }
127
128 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
129                                          QString& theErrorMsg )
130 {
131   HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
132
133   bool anIsModifySelected = myType == Split && aPanel->isModifySelected();
134
135   QString anImageName = aPanel->getImageName();
136   if( !anIsModifySelected && anImageName.isEmpty() )
137     return false;
138
139   QString aSelectedName1, aSelectedName2;
140   if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
141     return false;
142
143   if( !anIsModifySelected &&
144       ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) ) )
145   {
146     // check that there are no other objects with the same name in the document
147     Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
148     if( !anObject.IsNull() )
149     {
150       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
151       return false;
152     }
153   }
154
155   Handle(HYDROData_Object) anObject1 = Handle(HYDROData_Object)::DownCast(
156     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_UNKNOWN ) );
157   Handle(HYDROData_Object) anObject2 = Handle(HYDROData_Object)::DownCast(
158     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_UNKNOWN ) );
159   if( anObject1.IsNull() || anObject2.IsNull() )
160     return false;
161
162   HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
163
164   Handle(HYDROData_Image) aResult;
165   ImageComposer_Operator* anOperator = 0;
166   if( myIsEdit )
167   {
168     aResult = myEditedObject;
169     aResult->ClearReferences();
170     anOperator = aFactory->Operator( aResult );
171   }
172   else
173   {
174     QString anOperatorName;
175     switch( myType )
176     {
177       case Fuse:  anOperatorName = ImageComposer_FuseOperator::Type(); break;
178       case Cut:   anOperatorName = ImageComposer_CutOperator::Type(); break;
179       case Split: anOperatorName = ImageComposer_CropOperator::Type(); break;
180       default: break;
181     }
182
183     anOperator = aFactory->Operator( anOperatorName );
184
185     aResult = aFactory->CreateImage( doc(), anOperator );
186   }
187
188   if( aResult.IsNull() || !anOperator )
189     return false;
190
191   // Setting the operator arguments 
192   anOperator->setArgs( aPanel->getColor() );
193   aResult->SetArgs( anOperator->getBinArgs() );
194
195   aResult->SetName( anImageName );
196   aResult->AppendReference( anObject1 );
197   aResult->AppendReference( anObject2 );
198
199   aResult->Update();
200
201   if( anIsModifySelected )
202   {
203     Handle(HYDROData_Image) aSelectedImage = Handle(HYDROData_Image)::DownCast( anObject1 );
204     if( !aSelectedImage.IsNull() )
205     {
206       aSelectedImage->SetIsSelfSplitted( true );
207       aSelectedImage->SetImage( aResult->Image() );
208       aSelectedImage->SetTrsf( aResult->Trsf() );
209       aResult->Remove();
210     }
211   }
212
213   if( !myIsEdit && !anIsModifySelected )
214   {
215     size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
216     module()->setObjectVisible( aViewId, anObject1, false );
217     module()->setObjectVisible( aViewId, anObject2, false );
218     module()->setObjectVisible( aViewId, aResult, true );
219   }
220
221   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
222   return true;
223 }