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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_TwoImagesOp.h"
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>
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Image.h>
30 #include <HYDROData_OperationsFactory.h>
32 #include <ImageComposer_CutOperator.h>
33 #include <ImageComposer_CropOperator.h>
34 #include <ImageComposer_FuseOperator.h>
36 #include <LightApp_Application.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
40 HYDROGUI_TwoImagesOp::HYDROGUI_TwoImagesOp( HYDROGUI_Module* theModule,
42 const bool theIsEdit )
43 : HYDROGUI_Operation( theModule ),
45 myIsEdit( theIsEdit ),
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;
59 HYDROGUI_TwoImagesOp::~HYDROGUI_TwoImagesOp()
63 HYDROGUI_InputPanel* HYDROGUI_TwoImagesOp::createInputPanel() const
65 return new HYDROGUI_TwoImagesDlg( module(), getName() );
68 void HYDROGUI_TwoImagesOp::startOperation()
70 HYDROGUI_Operation::startOperation();
72 HYDROGUI_TwoImagesDlg* aPanel = (HYDROGUI_TwoImagesDlg*)inputPanel();
77 aMode = HYDROGUI_TwoImagesDlg::TwoFuseImage;
79 aMode = HYDROGUI_TwoImagesDlg::TwoCutImage;
80 else if( myType == Split )
81 aMode = HYDROGUI_TwoImagesDlg::ImageAndPolyline;
82 aPanel->setMode( aMode, myIsEdit );
87 if ( isApplyAndClose() )
88 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
89 if( !myEditedObject.IsNull() )
90 anImageName = myEditedObject->GetName();
97 case Fuse: aPrefix = tr( "FUSE" ); break;
98 case Cut: aPrefix = tr( "CUT" ); break;
99 case Split: aPrefix = tr( "SPLIT" ); break;
102 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix );
104 aPanel->setImageName( anImageName );
106 QString aSelectedName1, aSelectedName2;
107 if( myIsEdit && !myEditedObject.IsNull() )
109 if( myEditedObject->NbReferences() > 0 )
111 Handle(HYDROData_Entity) anObject1 = myEditedObject->Reference( 0 );
112 if( !anObject1.IsNull() )
113 aSelectedName1 = anObject1->GetName();
115 if( myEditedObject->NbReferences() > 1 )
117 Handle(HYDROData_Entity) anObject2 = myEditedObject->Reference( 1 );
118 if( !anObject2.IsNull() )
119 aSelectedName2 = anObject2->GetName();
121 aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
123 HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
124 if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
127 anOperator->getArgs( aColor );
128 aPanel->setColor( aColor );
133 Handle(HYDROData_Image) aSelectedImage =
134 Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
135 if( !aSelectedImage.IsNull() )
137 QString aSelectedName = aSelectedImage->GetName();
138 aPanel->setPreselectedObject( aSelectedName );
141 connect( aPanel, SIGNAL( alreadySelected( const QString& ) ), SLOT( onAlreadySelected( const QString& ) ) );
144 void HYDROGUI_TwoImagesOp::onAlreadySelected( const QString& theName )
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 );
151 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
152 QString& theErrorMsg,
153 QStringList& theBrowseObjectsEntries )
155 HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
157 bool anIsModifySelected = myType == Split && aPanel->isModifySelected();
159 QString anImageName = aPanel->getImageName();
160 if( !anIsModifySelected && anImageName.isEmpty() )
163 QString aSelectedName1, aSelectedName2;
164 if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
167 if( !anIsModifySelected &&
168 ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) ) )
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() )
174 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
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() )
186 HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
188 Handle(HYDROData_Image) aResult;
189 ImageComposer_Operator* anOperator = 0;
192 aResult = myEditedObject;
193 aResult->ClearReferences();
194 anOperator = aFactory->Operator( aResult );
198 QString anOperatorName;
201 case Fuse: anOperatorName = ImageComposer_FuseOperator::Type(); break;
202 case Cut: anOperatorName = ImageComposer_CutOperator::Type(); break;
203 case Split: anOperatorName = ImageComposer_CropOperator::Type(); break;
207 anOperator = aFactory->Operator( anOperatorName );
209 aResult = aFactory->CreateImage( doc(), anOperator );
210 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aResult );
211 theBrowseObjectsEntries.append( anEntry );
214 if( aResult.IsNull() || !anOperator )
217 // Setting the operator arguments
218 anOperator->setArgs( aPanel->getColor() );
219 aResult->SetArgs( anOperator->getBinArgs() );
221 aResult->SetName( anImageName );
222 aResult->AppendReference( anObject1 );
223 aResult->AppendReference( anObject2 );
227 if( anIsModifySelected )
229 Handle(HYDROData_Image) aSelectedImage = Handle(HYDROData_Image)::DownCast( anObject1 );
230 if( !aSelectedImage.IsNull() )
232 aSelectedImage->SetIsSelfSplit( true );
233 aSelectedImage->SetImage( aResult->Image() );
234 aSelectedImage->SetTrsf( aResult->Trsf() );
239 if( !myIsEdit && !anIsModifySelected )
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 );
247 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;