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