Salome HOME
LOT 15
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TwoImagesOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_TwoImagesOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_Tool2.h"
23 #include "HYDROGUI_TwoImagesDlg.h"
24 #include "HYDROGUI_UpdateFlags.h"
25 #include <HYDROGUI_DataObject.h>
26
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Image.h>
29
30 #include <HYDROData_OperationsFactory.h>
31
32 #include <ImageComposer_CutOperator.h>
33 #include <ImageComposer_CropOperator.h>
34 #include <ImageComposer_FuseOperator.h>
35
36 #include <LightApp_Application.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.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_SPLIT_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   int aMode;
76   if( myType == Fuse )
77     aMode = HYDROGUI_TwoImagesDlg::TwoFuseImage;
78   if ( myType == Cut )
79     aMode = HYDROGUI_TwoImagesDlg::TwoCutImage;
80   else if( myType == Split )
81     aMode = HYDROGUI_TwoImagesDlg::ImageAndPolyline;
82   aPanel->setMode( aMode, myIsEdit );
83
84   QString anImageName;
85   if( myIsEdit )
86   {
87     if ( isApplyAndClose() )
88       myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
89     if( !myEditedObject.IsNull() )
90       anImageName = myEditedObject->GetName();
91   }
92   else
93   {
94     QString aPrefix;
95     switch( myType )
96     {
97       case Fuse: aPrefix = tr( "FUSE" ); break;
98       case Cut: aPrefix = tr( "CUT" ); break;
99       case Split: aPrefix = tr( "SPLIT" ); break;
100       default: break;
101     }
102     anImageName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix );
103   }
104   aPanel->setImageName( anImageName );
105
106   QString aSelectedName1, aSelectedName2;
107   if( myIsEdit && !myEditedObject.IsNull() )
108   {
109     if( myEditedObject->NbReferences() > 0 )
110     {
111       Handle(HYDROData_Entity) anObject1 = myEditedObject->Reference( 0 );
112       if( !anObject1.IsNull() )
113         aSelectedName1 = anObject1->GetName();
114     }
115     if( myEditedObject->NbReferences() > 1 )
116     {
117       Handle(HYDROData_Entity) anObject2 = myEditedObject->Reference( 1 );
118       if( !anObject2.IsNull() )
119         aSelectedName2 = anObject2->GetName();
120     }
121     aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
122
123     HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
124     if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
125     {
126       QColor aColor;
127       anOperator->getArgs( aColor );
128       aPanel->setColor( aColor );
129     }
130   }
131   else if( !myIsEdit )
132   {
133     Handle(HYDROData_Image) aSelectedImage =
134       Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
135     if( !aSelectedImage.IsNull() )
136     {
137       QString aSelectedName = aSelectedImage->GetName();
138       aPanel->setPreselectedObject( aSelectedName );
139     }
140   }
141   connect( aPanel, SIGNAL( alreadySelected( const QString& ) ), SLOT( onAlreadySelected( const QString& ) ) );
142 }
143
144 void HYDROGUI_TwoImagesOp::onAlreadySelected( const QString& theName )
145 {
146   QString aTitle = tr( "INSUFFICIENT_INPUT_DATA" );
147   QString aMessage = tr( "OBJECT_ALREADY_SELECTED" ).arg( theName );
148   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
149 }
150
151 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
152                                          QString& theErrorMsg,
153                                          QStringList& theBrowseObjectsEntries )
154 {
155   HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
156
157   bool anIsModifySelected = myType == Split && aPanel->isModifySelected();
158
159   QString anImageName = aPanel->getImageName();
160   if( !anIsModifySelected && anImageName.isEmpty() )
161     return false;
162
163   QString aSelectedName1, aSelectedName2;
164   if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
165     return false;
166
167   if( !anIsModifySelected &&
168       ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) ) )
169   {
170     // check that there are no other objects with the same name in the document
171     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
172     if( !anObject.IsNull() )
173     {
174       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
175       return false;
176     }
177   }
178
179   Handle(HYDROData_Entity) anObject1 =
180     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_UNKNOWN ) ;
181   Handle(HYDROData_Entity) anObject2 =
182     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_UNKNOWN );
183   if( anObject1.IsNull() || anObject2.IsNull() )
184     return false;
185
186   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
187
188   Handle(HYDROData_Image) aResult;
189   ImageComposer_Operator* anOperator = 0;
190   if( myIsEdit )
191   {
192     aResult = myEditedObject;
193     aResult->ClearReferences();
194     anOperator = aFactory->Operator( aResult );
195   }
196   else
197   {
198     QString anOperatorName;
199     switch( myType )
200     {
201       case Fuse:  anOperatorName = ImageComposer_FuseOperator::Type(); break;
202       case Cut:   anOperatorName = ImageComposer_CutOperator::Type(); break;
203       case Split: anOperatorName = ImageComposer_CropOperator::Type(); break;
204       default: break;
205     }
206
207     anOperator = aFactory->Operator( anOperatorName );
208
209     aResult = aFactory->CreateImage( doc(), anOperator );
210     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aResult );
211     theBrowseObjectsEntries.append( anEntry );
212   }
213
214   if( aResult.IsNull() || !anOperator )
215     return false;
216
217   // Setting the operator arguments 
218   anOperator->setArgs( aPanel->getColor() );
219   aResult->SetArgs( anOperator->getBinArgs() );
220
221   aResult->SetName( anImageName );
222   aResult->AppendReference( anObject1 );
223   aResult->AppendReference( anObject2 );
224
225   aResult->Update();
226
227   if( anIsModifySelected )
228   {
229     Handle(HYDROData_Image) aSelectedImage = Handle(HYDROData_Image)::DownCast( anObject1 );
230     if( !aSelectedImage.IsNull() )
231     {
232       aSelectedImage->SetIsSelfSplit( true );
233       aSelectedImage->SetImage( aResult->Image() );
234       aSelectedImage->SetTrsf( aResult->Trsf() );
235       aResult->Remove();
236     }
237   }
238
239   if( !myIsEdit && !anIsModifySelected )
240   {
241     size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
242     module()->setObjectVisible( aViewId, anObject1, false );
243     module()->setObjectVisible( aViewId, anObject2, false );
244     module()->setObjectVisible( aViewId, aResult, true );
245   }
246
247   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
248   return true;
249 }