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