Salome HOME
1) HYDRO feature 6: Update of objects (T 1.2)
[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   myIsEdit( false ),
42   myEditedObject( 0 )
43 {
44   if( myType == Edit )
45   {
46     myIsEdit = true;
47     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
48     if( !myEditedObject.IsNull() )
49     {
50       if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
51       {
52         if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
53         {
54           if( dynamic_cast<ImageComposer_FuseOperator*>( anOperator ) )
55             myType = Fuse;
56           else if( dynamic_cast<ImageComposer_CutOperator*>( anOperator ) )
57             myType = Cut;
58         }
59       }
60     }
61   }
62
63   QString aName;
64   switch( myType )
65   {
66     case Fuse: aName = tr( "FUSE" ); break;
67     case Cut: aName = tr( "CUT" ); break;
68     default: break;
69   }
70   setName( aName );
71 }
72
73 HYDROGUI_TwoImagesOp::~HYDROGUI_TwoImagesOp()
74 {
75 }
76
77 HYDROGUI_InputPanel* HYDROGUI_TwoImagesOp::createInputPanel() const
78 {
79   return new HYDROGUI_TwoImagesDlg( module(), getName() );
80 }
81
82 void HYDROGUI_TwoImagesOp::startOperation()
83 {
84   HYDROGUI_Operation::startOperation();
85
86   HYDROGUI_TwoImagesDlg* aPanel = (HYDROGUI_TwoImagesDlg*)inputPanel();
87   aPanel->reset();
88
89   QString anImageName;
90   if( myIsEdit )
91   {
92     if( !myEditedObject.IsNull() )
93       anImageName = myEditedObject->GetName();
94   }
95   else
96     anImageName = HYDROGUI_Tool::GenerateObjectName( module(), getName() );
97   aPanel->setImageName( anImageName );
98
99   QString aSelectedName1, aSelectedName2;
100   if( myIsEdit && !myEditedObject.IsNull() )
101   {
102     if( myEditedObject->NbReferences() > 0 )
103     {
104       Handle(HYDROData_Image) anImage1 = myEditedObject->Reference( 0 );
105       if( !anImage1.IsNull() )
106         aSelectedName1 = anImage1->GetName();
107     }
108     if( myEditedObject->NbReferences() > 1 )
109     {
110       Handle(HYDROData_Image) anImage2 = myEditedObject->Reference( 1 );
111       if( !anImage2.IsNull() )
112         aSelectedName2 = anImage2->GetName();
113     }
114     aPanel->setSelectedImages( aSelectedName1, aSelectedName2 );
115   }
116 }
117
118 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
119                                          QString& theErrorMsg )
120 {
121   HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
122
123   QString anImageName = aPanel->getImageName();
124   if( anImageName.isEmpty() )
125     return false;
126
127   QString aSelectedName1, aSelectedName2;
128   if( !aPanel->getSelectedImages( aSelectedName1, aSelectedName2 ) )
129     return false;
130
131   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
132   {
133     // check that there are no other objects with the same name in the document
134     Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
135     if( !anObject.IsNull() )
136     {
137       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
138       return false;
139     }
140   }
141
142   Handle(HYDROData_Image) anImage1 = Handle(HYDROData_Image)::DownCast(
143     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_IMAGE ) );
144   Handle(HYDROData_Image) anImage2 = Handle(HYDROData_Image)::DownCast(
145     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_IMAGE ) );
146   if( anImage1.IsNull() || anImage2.IsNull() )
147     return false;
148
149   HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
150   Handle(HYDROData_Image) aResult = 0;
151   if( myIsEdit )
152   {
153     aResult = myEditedObject;
154     aResult->ClearReferences();
155   }
156   else
157   {
158     ImageComposer_Operator* anOperator = 0;
159     switch( myType )
160     {
161       case Fuse: anOperator = new ImageComposer_FuseOperator(); break;
162       case Cut: anOperator = new ImageComposer_CutOperator(); break;
163       default: break;
164     }
165
166     if( !anOperator )
167       return false;
168
169     aResult = aFactory->CreateImage( doc(), anOperator );
170   }
171
172   if( aResult.IsNull() )
173     return false;
174
175   aResult->SetName( anImageName );
176   aResult->AppendReference( anImage1 );
177   aResult->AppendReference( anImage2 );
178   aFactory->UpdateImage( doc(), aResult );
179
180   if( !myIsEdit )
181   {
182     size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
183     anImage1->SetVisible( aViewId, false );
184     anImage2->SetVisible( aViewId, false );
185     aResult->SetVisible( aViewId, true );
186   }
187
188   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
189   return true;
190 }