]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_TwoImagesOp.cxx
Salome HOME
copyrights are updated
[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_Tool.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_SPLITTED_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     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
88     if( !myEditedObject.IsNull() )
89       anImageName = myEditedObject->GetName();
90   }
91   else
92   {
93     QString aPrefix;
94     switch( myType )
95     {
96       case Fuse: aPrefix = tr( "FUSE" ); break;
97       case Cut: aPrefix = tr( "CUT" ); break;
98       case Split: aPrefix = tr( "SPLIT" ); break;
99       default: break;
100     }
101     anImageName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix );
102   }
103   aPanel->setImageName( anImageName );
104
105   QString aSelectedName1, aSelectedName2;
106   if( myIsEdit && !myEditedObject.IsNull() )
107   {
108     if( myEditedObject->NbReferences() > 0 )
109     {
110       Handle(HYDROData_Entity) anObject1 = myEditedObject->Reference( 0 );
111       if( !anObject1.IsNull() )
112         aSelectedName1 = anObject1->GetName();
113     }
114     if( myEditedObject->NbReferences() > 1 )
115     {
116       Handle(HYDROData_Entity) anObject2 = myEditedObject->Reference( 1 );
117       if( !anObject2.IsNull() )
118         aSelectedName2 = anObject2->GetName();
119     }
120     aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
121
122     HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
123     if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
124     {
125       QColor aColor;
126       anOperator->getArgs( aColor );
127       aPanel->setColor( aColor );
128     }
129   }
130   else if( !myIsEdit )
131   {
132     Handle(HYDROData_Image) aSelectedImage =
133       Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
134     if( !aSelectedImage.IsNull() )
135     {
136       QString aSelectedName = aSelectedImage->GetName();
137       aPanel->setPreselectedObject( aSelectedName );
138     }
139   }
140   connect( aPanel, SIGNAL( alreadySelected( const QString& ) ), SLOT( onAlreadySelected( const QString& ) ) );
141 }
142
143 void HYDROGUI_TwoImagesOp::onAlreadySelected( const QString& theName )
144 {
145   QString aTitle = tr( "INSUFFICIENT_INPUT_DATA" );
146   QString aMessage = tr( "OBJECT_ALREADY_SELECTED" ).arg( theName );
147   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
148 }
149
150 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
151                                          QString& theErrorMsg,
152                                          QStringList& theBrowseObjectsEntries )
153 {
154   HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
155
156   bool anIsModifySelected = myType == Split && aPanel->isModifySelected();
157
158   QString anImageName = aPanel->getImageName();
159   if( !anIsModifySelected && anImageName.isEmpty() )
160     return false;
161
162   QString aSelectedName1, aSelectedName2;
163   if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
164     return false;
165
166   if( !anIsModifySelected &&
167       ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) ) )
168   {
169     // check that there are no other objects with the same name in the document
170     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
171     if( !anObject.IsNull() )
172     {
173       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
174       return false;
175     }
176   }
177
178   Handle(HYDROData_Entity) anObject1 =
179     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_UNKNOWN ) ;
180   Handle(HYDROData_Entity) anObject2 =
181     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_UNKNOWN );
182   if( anObject1.IsNull() || anObject2.IsNull() )
183     return false;
184
185   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
186
187   Handle(HYDROData_Image) aResult;
188   ImageComposer_Operator* anOperator = 0;
189   if( myIsEdit )
190   {
191     aResult = myEditedObject;
192     aResult->ClearReferences();
193     anOperator = aFactory->Operator( aResult );
194   }
195   else
196   {
197     QString anOperatorName;
198     switch( myType )
199     {
200       case Fuse:  anOperatorName = ImageComposer_FuseOperator::Type(); break;
201       case Cut:   anOperatorName = ImageComposer_CutOperator::Type(); break;
202       case Split: anOperatorName = ImageComposer_CropOperator::Type(); break;
203       default: break;
204     }
205
206     anOperator = aFactory->Operator( anOperatorName );
207
208     aResult = aFactory->CreateImage( doc(), anOperator );
209     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aResult );
210     theBrowseObjectsEntries.append( anEntry );
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 }