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