Salome HOME
Create goups for stream.
[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   connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
47                                             const QString& ) ),
48            theModule->application(), SLOT( onHelpContextModule( const QString&,
49                                             const QString&, const QString& ) ) );
50 }
51
52 HYDROGUI_Operation::~HYDROGUI_Operation()
53 {
54 }
55
56 void HYDROGUI_Operation::setName( const QString& theName )
57 {
58   myName = theName;
59 }
60
61 const QString& HYDROGUI_Operation::getName() const
62 {
63   return myName;
64 }
65
66 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
67 {
68   if( !myPanel )
69   {
70     ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
71     connect( myPanel, SIGNAL( panelApply() ),  this, SLOT( onApply() ) );
72     connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
73     connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
74   }
75   return myPanel;
76 }
77
78 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
79 {
80   return myModule->getApp()->selectionMgr();
81 }
82
83 HYDROGUI_Module* HYDROGUI_Operation::module() const
84 {
85   return myModule;
86 }
87
88 void HYDROGUI_Operation::startOperation()
89 {
90   LightApp_Operation::startOperation();
91
92   if( inputPanel() )
93   {
94     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
95     inputPanel()->show();
96   }
97 }
98
99 void HYDROGUI_Operation::abortOperation()
100 {
101   LightApp_Operation::abortOperation();
102   closeInputPanel();
103 }
104
105 void HYDROGUI_Operation::commitOperation()
106 {
107   LightApp_Operation::commitOperation();
108   closeInputPanel();
109 }
110
111 void HYDROGUI_Operation::setDialogActive( const bool active )
112 {
113   LightApp_Operation::setDialogActive( active );
114   if( myPanel )
115   {
116     if( active )
117     {
118       myPanel->show();
119     }
120   }
121 }
122
123 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
124 {
125   return NULL;
126 }
127
128 void HYDROGUI_Operation::closeInputPanel()
129 {
130   if( myPanel )
131   {
132     myModule->getApp()->desktop()->removeDockWidget( myPanel );
133     delete myPanel;
134     myPanel = 0;
135   }
136 }
137
138 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
139                                        QString& theErrorMsg )
140 {
141   return false;
142 }
143
144 void HYDROGUI_Operation::processCancel()
145 {
146 }
147
148 void HYDROGUI_Operation::startDocOperation()
149 {
150   // Open transaction in the model document
151   if ( !doc()->IsOperation() )
152     doc()->StartOperation();
153 }
154
155 void HYDROGUI_Operation::abortDocOperation()
156 {
157   // Abort transaction in the model document
158   if ( doc()->IsOperation() )
159     doc()->AbortOperation();
160 }
161
162 void HYDROGUI_Operation::commitDocOperation()
163 {
164   // Commit transaction in the model document
165   if ( doc()->IsOperation() )
166     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
167 }
168
169 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
170 {
171   return HYDROData_Document::Document( myModule->getStudyId() );
172 }
173
174 void HYDROGUI_Operation::onApply()
175 {
176   QApplication::setOverrideCursor( Qt::WaitCursor );
177
178   startDocOperation();
179
180   int anUpdateFlags = 0;
181   QString anErrorMsg;
182
183   bool aResult = false;
184   
185   try
186   {
187     aResult = processApply( anUpdateFlags, anErrorMsg );
188   }
189   catch ( Standard_Failure )
190   {
191     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
192     anErrorMsg = aFailure->GetMessageString();
193     aResult = false;
194   }
195   catch ( ... )
196   {
197     aResult = false;
198   }
199   
200   QApplication::restoreOverrideCursor();
201
202   if ( aResult )
203   {
204     module()->update( anUpdateFlags );
205     commitDocOperation();
206     commit();
207   }
208   else
209   {
210     abortDocOperation();
211     QString aMsg = tr( "INPUT_VALID_DATA" );
212     if( !anErrorMsg.isEmpty() )
213       aMsg.prepend( anErrorMsg + "\n" );
214     SUIT_MessageBox::critical( module()->getApp()->desktop(),
215                                tr( "INSUFFICIENT_INPUT_DATA" ),
216                                aMsg ); 
217   }
218 }
219
220 void HYDROGUI_Operation::onCancel()
221 {
222   processCancel();
223   abort();
224 }
225
226 void HYDROGUI_Operation::onHelp()
227 {
228    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
229 }
230
231 QString HYDROGUI_Operation::getHelpComponent() const
232 {
233    return module()->moduleName();
234 }
235
236 QString HYDROGUI_Operation::getHelpFile() const
237 {
238    QString aFileName = ((myName.isEmpty())? operationName() : myName);
239    return aFileName.replace(QRegExp("\\s"), "_").append(".html");
240 }
241
242 QString HYDROGUI_Operation::getHelpContext() const
243 {
244    return QString();
245 }
246
247