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