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