Salome HOME
IPAL18573
[modules/geom.git] / src / GEOMBase / GEOMBase_Skeleton.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  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 // File   : GEOMBase_Skeleton.cxx
23 // Author : Damien COQUERET, Open CASCADE S.A.S.
24 //
25
26 #include "GEOMBase_Skeleton.h"
27 #include "GEOMBase.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31
32 #include <SalomeApp_Application.h>
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_MessageBox.h>
39
40 #include <QKeyEvent>
41 #include <QSpinBox>
42 #include <QDoubleSpinBox>
43
44 using namespace std;
45
46 //=================================================================================
47 // class    : GEOMBase_Skeleton()
48 // purpose  : Constructs a GEOMBase_Skeleton which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            true to construct a modal dialog.
52 //=================================================================================
53 GEOMBase_Skeleton::GEOMBase_Skeleton( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                       bool modal, Qt::WindowFlags fl )
55   : QDialog( parent, fl ), 
56     GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) ),
57     myGeomGUI( theGeometryGUI ),
58     myRBGroup( 0 )
59 {
60   setAttribute( Qt::WA_DeleteOnClose );
61
62   setModal( modal );
63
64   myMainFrame = new DlgRef_Skeleton( this );
65   QVBoxLayout* topLayout = new QVBoxLayout( this );
66   topLayout->setMargin( 0 ); topLayout->setSpacing( 0 );
67   topLayout->addWidget( myMainFrame );
68
69   buttonCancel()->setText( tr( "GEOM_BUT_CLOSE" ) );
70   buttonOk()->setText( tr( "GEOM_BUT_OK" )) ;
71   buttonApply()->setText( tr( "GEOM_BUT_APPLY" ) );
72   buttonHelp()->setText( tr( "GEOM_BUT_HELP" ) );
73
74   myRBGroup = new QButtonGroup( this );
75   myRBGroup->addButton( myMainFrame->RadioButton1, 0 );
76   myRBGroup->addButton( myMainFrame->RadioButton2, 1 );
77   myRBGroup->addButton( myMainFrame->RadioButton3, 2 );
78   myRBGroup->addButton( myMainFrame->RadioButton4, 3 );
79
80   connect( myRBGroup, SIGNAL( buttonClicked( int ) ), this, SIGNAL( constructorsClicked( int ) ) );
81
82   Init();
83 }
84
85
86 //=================================================================================
87 // function : ~GEOMBase_Skeleton()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 GEOMBase_Skeleton::~GEOMBase_Skeleton()
91 {
92   if ( myGeomGUI )
93     myGeomGUI->SetActiveDialogBox( 0 );
94 }
95
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void GEOMBase_Skeleton::Init()
102 {
103   SalomeApp_Application* app = (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() );
104   if ( !myGeomGUI && app )
105     myGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
106
107   /* init variables */
108   myGeomGUI->SetActiveDialogBox( this );
109
110   /* signals and slots connections */
111   connect( buttonCancel(), SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
112   if ( myGeomGUI ) {
113     connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
114     connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
115   }
116
117   // connect help button on a private slot that displays help information
118   connect( buttonHelp(), SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
119
120   /* displays Dialog */
121   myMainFrame->RadioButton1->setChecked( true );
122   myMainFrame->RadioButton4->hide();
123 }
124
125 void GEOMBase_Skeleton::initSpinBox( QSpinBox* spinBox, 
126                                      int min,  int max, int step )
127 {
128   spinBox->setRange( min, max );
129   spinBox->setSingleStep( step );
130 }
131
132 void GEOMBase_Skeleton::initSpinBox( QDoubleSpinBox* spinBox, 
133                                      double min,  double max, 
134                                      double step, int decimals )
135 {
136   spinBox->setRange( min, max );
137   spinBox->setSingleStep( step );
138   spinBox->setDecimals( decimals );
139 }
140
141 //=================================================================================
142 // function : ClickOnCancel()
143 // purpose  :
144 //=================================================================================
145 void GEOMBase_Skeleton::ClickOnCancel()
146 {
147   close();
148 }
149
150
151 //=================================================================================
152 // function : LineEditReturnPressed()
153 // purpose  :
154 //=================================================================================
155 void GEOMBase_Skeleton::LineEditReturnPressed()
156 {
157   if ( !myEditCurrentArgument )
158     return;
159
160   /* User name of object input management                          */
161   /* If successfull the selection is changed and signal emitted... */
162   /* so SelectionIntoArgument() is automatically called.           */
163   const QString objectUserName = myEditCurrentArgument->text();
164   QWidget* thisWidget = (QWidget*)this;
165   
166   if ( GEOMBase::SelectionByNameInDialogs( thisWidget, objectUserName, selectedIO() ) )
167      myEditCurrentArgument->setText( objectUserName );
168 }
169
170
171 //=================================================================================
172 // function : DeactivateActiveDialog()
173 // purpose  :
174 //=================================================================================
175 void GEOMBase_Skeleton::DeactivateActiveDialog()
176 {
177   this->setEnabled( false );
178   globalSelection();
179   disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 0, this, 0 );
180   if ( myGeomGUI ) myGeomGUI->SetActiveDialogBox( 0 );
181 }
182
183
184 //=================================================================================
185 // function : ActivateThisDialog()
186 // purpose  :
187 //=================================================================================
188 void GEOMBase_Skeleton::ActivateThisDialog()
189 {
190   /* Emit a signal to deactivate the active dialog */
191   if ( myGeomGUI ) myGeomGUI->EmitSignalDeactivateDialog();
192   this->setEnabled( true );
193   if ( myGeomGUI ) myGeomGUI->SetActiveDialogBox( (QDialog*)this );
194   return;
195 }
196
197
198 //=================================================================================
199 // function : closeEvent()
200 // purpose  : same than click on cancel button
201 //=================================================================================
202 void GEOMBase_Skeleton::closeEvent( QCloseEvent* e )
203 {
204   SalomeApp_Application* app = (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() );
205   if( app ) {
206     disconnect( app->selectionMgr(), 0, this, 0 );
207     app->updateActions();
208   }
209   QDialog::closeEvent( e );
210 }
211
212 //=================================================================================
213 // function : initName()
214 // purpose  : initialize the Name field with a string "thePrefix_X" (Vertex_3)
215 //=================================================================================
216 void GEOMBase_Skeleton::initName( const QString& thePrefix )
217 {
218   if ( !thePrefix.isNull() )
219     setPrefix( thePrefix );
220   myMainFrame->ResultName->setText( GEOMBase::GetDefaultName( getPrefix() ) );
221 }
222
223 //=================================================================================
224 // function : getNewObjectName()
225 // purpose  : returns contents of Name field
226 //=================================================================================
227 const char* GEOMBase_Skeleton::getNewObjectName() const
228 {
229   return myMainFrame->ResultName->text().toLatin1().constData();
230 }
231
232 //=================================================================================
233 // function : getConstructorId()
234 // purpose  :
235 //=================================================================================
236 int GEOMBase_Skeleton::getConstructorId() const
237 {
238   if ( myRBGroup )
239     return myRBGroup->checkedId();
240   return -1;
241 }
242
243 void GEOMBase_Skeleton::setConstructorId( const int id )
244 {
245   if ( myRBGroup && myRBGroup->button( id ) )
246     myRBGroup->button( id )->setChecked( true );
247 }
248
249 //=================================================================================
250 // function : ClickOnHelp()
251 // purpose  :
252 //=================================================================================
253 void GEOMBase_Skeleton::ClickOnHelp()
254 {
255   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
256   if ( app ) 
257     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString(""), myHelpFileName );
258   else {
259     QString platform;
260 #ifdef WIN32
261     platform = "winapplication";
262 #else
263     platform = "application";
264 #endif
265     SUIT_MessageBox::warning( 0, QObject::tr( "WRN_WARNING" ),
266                               QObject::tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
267                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( myHelpFileName ),
268                               QObject::tr( "BUT_OK" ) );
269   }
270 }
271 //=================================================================================
272 //  function : setHelpFileName()
273 //  purpose  : set name for help file html
274 //=================================================================================
275
276 void GEOMBase_Skeleton::setHelpFileName( const QString& theName )
277 {
278   myHelpFileName = theName;
279 }
280
281 DlgRef_Skeleton* GEOMBase_Skeleton::mainFrame()
282 {
283   return myMainFrame;
284 }
285
286 QWidget* GEOMBase_Skeleton::centralWidget()
287 {
288   return myMainFrame->GroupMedium;
289 }
290
291 QPushButton* GEOMBase_Skeleton::buttonCancel() const
292 {
293   return myMainFrame->buttonCancel;
294 }
295
296 QPushButton* GEOMBase_Skeleton::buttonOk() const
297 {
298   return myMainFrame->buttonOk;
299 }
300
301 QPushButton* GEOMBase_Skeleton::buttonApply() const
302 {
303   return myMainFrame->buttonApply;
304 }
305
306 QPushButton* GEOMBase_Skeleton::buttonHelp() const
307 {
308   return myMainFrame->buttonHelp;
309 }
310
311 //=================================================================================
312 // function : keyPressEvent()
313 // purpose  :
314 //=================================================================================
315 void GEOMBase_Skeleton::keyPressEvent( QKeyEvent* e )
316 {
317   QDialog::keyPressEvent( e );
318   if ( e->isAccepted() )
319     return;
320
321   if ( e->key() == Qt::Key_F1 ) {
322     e->accept();
323     ClickOnHelp();
324   }
325 }