Salome HOME
Creat\Edit stream operation.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operation.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_Operation.h"
24
25 #include "HYDROGUI_InputPanel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Iterator.h>
31
32 #include <LightApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Study.h>
38
39 #include <QApplication>
40
41 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
42 : LightApp_Operation(),
43   myModule( theModule ),
44   myPanel( 0 )
45 {
46 }
47
48 HYDROGUI_Operation::~HYDROGUI_Operation()
49 {
50 }
51
52 void HYDROGUI_Operation::setName( const QString& theName )
53 {
54   myName = theName;
55 }
56
57 const QString& HYDROGUI_Operation::getName() const
58 {
59   return myName;
60 }
61
62 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
63 {
64   if( !myPanel )
65   {
66     ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
67     connect( myPanel, SIGNAL( panelApply() ),  this, SLOT( onApply() ) );
68     connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
69   }
70   return myPanel;
71 }
72
73 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
74 {
75   return myModule->getApp()->selectionMgr();
76 }
77
78 HYDROGUI_Module* HYDROGUI_Operation::module() const
79 {
80   return myModule;
81 }
82
83 void HYDROGUI_Operation::startOperation()
84 {
85   LightApp_Operation::startOperation();
86
87   if( inputPanel() )
88   {
89     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
90     inputPanel()->show();
91   }
92 }
93
94 void HYDROGUI_Operation::abortOperation()
95 {
96   LightApp_Operation::abortOperation();
97   closeInputPanel();
98 }
99
100 void HYDROGUI_Operation::commitOperation()
101 {
102   LightApp_Operation::commitOperation();
103   closeInputPanel();
104 }
105
106 void HYDROGUI_Operation::setDialogActive( const bool active )
107 {
108   LightApp_Operation::setDialogActive( active );
109   if( myPanel )
110   {
111     if( active )
112     {
113       myPanel->show();
114     }
115   }
116 }
117
118 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
119 {
120   return NULL;
121 }
122
123 void HYDROGUI_Operation::closeInputPanel()
124 {
125   if( myPanel )
126   {
127     myModule->getApp()->desktop()->removeDockWidget( myPanel );
128     delete myPanel;
129     myPanel = 0;
130   }
131 }
132
133 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
134                                        QString& theErrorMsg )
135 {
136   return false;
137 }
138
139 void HYDROGUI_Operation::processCancel()
140 {
141 }
142
143 void HYDROGUI_Operation::startDocOperation()
144 {
145   // Open transaction in the model document
146   if ( !doc()->IsOperation() )
147     doc()->StartOperation();
148 }
149
150 void HYDROGUI_Operation::abortDocOperation()
151 {
152   // Abort transaction in the model document
153   if ( doc()->IsOperation() )
154     doc()->AbortOperation();
155 }
156
157 void HYDROGUI_Operation::commitDocOperation()
158 {
159   // Commit transaction in the model document
160   if ( doc()->IsOperation() )
161     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
162 }
163
164 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
165 {
166   return HYDROData_Document::Document( myModule->getStudyId() );
167 }
168
169 void HYDROGUI_Operation::onApply()
170 {
171   QApplication::setOverrideCursor( Qt::WaitCursor );
172
173   startDocOperation();
174
175   int anUpdateFlags = 0;
176   QString anErrorMsg;
177
178   bool aResult = false;
179   
180   try
181   {
182     aResult = processApply( anUpdateFlags, anErrorMsg );
183   }
184   catch ( Standard_Failure )
185   {
186     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
187     anErrorMsg = aFailure->GetMessageString();
188     aResult = false;
189   }
190   catch ( ... )
191   {
192     aResult = false;
193   }
194   
195   QApplication::restoreOverrideCursor();
196
197   if ( aResult )
198   {
199     module()->update( anUpdateFlags );
200     commitDocOperation();
201     commit();
202   }
203   else
204   {
205     abortDocOperation();
206     QString aMsg = tr( "INPUT_VALID_DATA" );
207     if( !anErrorMsg.isEmpty() )
208       aMsg.prepend( anErrorMsg + "\n" );
209     SUIT_MessageBox::critical( module()->getApp()->desktop(),
210                                tr( "INSUFFICIENT_INPUT_DATA" ),
211                                aMsg ); 
212   }
213 }
214
215 void HYDROGUI_Operation::onCancel()
216 {
217   processCancel();
218   abort();
219 }