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