Salome HOME
704372635e11196ce05fdaafce27ec6401f21023
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TwoImagesOp.cxx
1 // Copyright (C) 2007-2013  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.
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
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Image.h>
32
33 #include <HYDROOperations_Factory.h>
34
35 #include <ImageComposer_CutOperator.h>
36 #include <ImageComposer_FuseOperator.h>
37
38 HYDROGUI_TwoImagesOp::HYDROGUI_TwoImagesOp( HYDROGUI_Module* theModule, const int theType )
39 : HYDROGUI_Operation( theModule ),
40   myType( theType )
41 {
42   QString aName;
43   switch( myType )
44   {
45     case Fuse: aName = tr( "FUSE" ); break;
46     case Cut: aName = tr( "CUT" ); break;
47     default: break;
48   }
49   setName( aName );
50 }
51
52 HYDROGUI_TwoImagesOp::~HYDROGUI_TwoImagesOp()
53 {
54 }
55
56 HYDROGUI_InputPanel* HYDROGUI_TwoImagesOp::createInputPanel() const
57 {
58   return new HYDROGUI_TwoImagesDlg( module(), getName() );
59 }
60
61 void HYDROGUI_TwoImagesOp::startOperation()
62 {
63   HYDROGUI_Operation::startOperation();
64
65   HYDROGUI_TwoImagesDlg* aPanel = (HYDROGUI_TwoImagesDlg*)inputPanel();
66   aPanel->reset();
67
68   QString anImageName = HYDROGUI_Tool::GenerateObjectName( module(), getName() );
69   aPanel->setImageName( anImageName );
70 }
71
72 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
73                                          QString& theErrorMsg )
74 {
75   HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
76
77   QString anImageName = aPanel->getImageName();
78   if( anImageName.isEmpty() )
79     return false;
80
81   QString aSelectedName1, aSelectedName2;
82   if( !aPanel->getSelectedImages( aSelectedName1, aSelectedName2 ) )
83     return false;
84
85   // check that there are no other objects with the same name in the document
86   Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
87   if( !anObject.IsNull() )
88   {
89     theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
90     return false;
91   }
92
93   Handle(HYDROData_Image) anImage1 = Handle(HYDROData_Image)::DownCast(
94     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_IMAGE ) );
95   Handle(HYDROData_Image) anImage2 = Handle(HYDROData_Image)::DownCast(
96     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_IMAGE ) );
97   if( anImage1.IsNull() || anImage2.IsNull() )
98     return false;
99
100   HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
101   ImageComposer_Operator* anOperator = 0;
102   switch( myType )
103   {
104     case Fuse: anOperator = new ImageComposer_FuseOperator(); break;
105     case Cut: anOperator = new ImageComposer_CutOperator(); break;
106     default: break;
107   }
108
109   if( !anOperator )
110     return false;
111
112   Handle(HYDROData_Image) aResult = aFactory->CreateImage( doc(), anOperator );
113   if( aResult.IsNull() )
114     return false;
115
116   aResult->SetName( anImageName );
117   aResult->AppendReference( anImage1 );
118   aResult->AppendReference( anImage2 );
119   aFactory->UpdateImage( doc(), aResult );
120
121   size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
122   anImage1->SetVisible( aViewId, false );
123   anImage2->SetVisible( aViewId, false );
124   aResult->SetVisible( aViewId, true );
125
126   theUpdateFlags = UF_Model | UF_Viewer;
127   return true;
128 }