Salome HOME
Preparation of intermediate revision
[modules/geom.git] / src / GEOMBase / GEOMBase_Skeleton.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : GEOMBase_Skeleton.cxx
24 // Author : Damien COQUERET, Open CASCADE S.A.S.
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 <SalomeApp_DoubleSpinBox.h>
34 #include <SalomeApp_Study.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_Session.h>
40 #include <SUIT_MessageBox.h>
41
42 #include <QKeyEvent>
43 #include <QSpinBox>
44 #include <QDoubleSpinBox>
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   myMainFrame->GroupBoxName->setTitle( tr( "GEOM_RESULT_NAME_GRP" ) );
70   myMainFrame->NameLabel->setText( tr( "GEOM_RESULT_NAME_LBL" ) );
71
72   myMainFrame->GroupBoxPublish->setTitle( tr( "GEOM_PUBLISH_RESULT_GRP" ) );
73   myMainFrame->CheckBoxRestoreSS->setText( tr( "GEOM_RESTORE_SUB_SHAPES" ) );
74
75   buttonCancel()->setText( tr( "GEOM_BUT_CLOSE" ) );
76   buttonOk()->setText( tr( "GEOM_BUT_APPLY_AND_CLOSE" ) );
77   buttonApply()->setText( tr( "GEOM_BUT_APPLY" ) );
78   buttonHelp()->setText( tr( "GEOM_BUT_HELP" ) );
79
80   myRBGroup = new QButtonGroup( this );
81   myRBGroup->addButton( myMainFrame->RadioButton1, 0 );
82   myRBGroup->addButton( myMainFrame->RadioButton2, 1 );
83   myRBGroup->addButton( myMainFrame->RadioButton3, 2 );
84   myRBGroup->addButton( myMainFrame->RadioButton4, 3 );
85   myRBGroup->addButton( myMainFrame->RadioButton5, 4 );
86
87   connect( myRBGroup, SIGNAL( buttonClicked( int ) ), this, SIGNAL( constructorsClicked( int ) ) );
88
89   Init();
90 }
91
92 //=================================================================================
93 // function : ~GEOMBase_Skeleton()
94 // purpose  : Destroys the object and frees any allocated resources
95 //=================================================================================
96 GEOMBase_Skeleton::~GEOMBase_Skeleton()
97 {
98   if ( myGeomGUI )
99     myGeomGUI->SetActiveDialogBox( 0 );
100 }
101
102 //=================================================================================
103 // function : Init()
104 // purpose  :
105 //=================================================================================
106 void GEOMBase_Skeleton::Init()
107 {
108   SalomeApp_Application* app = (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() );
109   if ( !myGeomGUI && app )
110     myGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
111
112   /* init variables */
113   if ( myGeomGUI )
114     myGeomGUI->SetActiveDialogBox( this );
115
116   /* signals and slots connections */
117   connect( buttonCancel(), SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
118   if ( myGeomGUI ) {
119     connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
120     connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
121   }
122
123   // connect help button on a private slot that displays help information
124   connect( buttonHelp(), SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
125
126   /* displays Dialog */
127   myMainFrame->RadioButton1->setChecked( true );
128   myMainFrame->RadioButton4->hide();
129   myMainFrame->RadioButton5->hide();
130
131   myMainFrame->CheckBoxRestoreSS->setChecked( false );
132   myMainFrame->GroupBoxPublish->hide();
133 }
134
135 //=================================================================================
136 // function : initSpinBox()
137 // purpose  : 
138 //=================================================================================
139 void GEOMBase_Skeleton::initSpinBox( QSpinBox* spinBox, 
140                                      int min,  int max, int step )
141 {
142   spinBox->setRange( min, max );
143   spinBox->setSingleStep( step );
144 }
145
146 //=================================================================================
147 // function : initSpinBox()
148 // purpose  : 
149 //=================================================================================
150 void GEOMBase_Skeleton::initSpinBox( SalomeApp_DoubleSpinBox* spinBox, 
151                                      double min,  double max, 
152                                      double step, int decimals )
153 {
154   spinBox->setPrecision( decimals );
155   spinBox->setDecimals( decimals ); // it's necessary to set decimals before the range setting,
156                                     // by default Qt rounds boundaries to 2 decimals at setRange
157   spinBox->setRange( min, max );
158   spinBox->setSingleStep( step );
159 }
160
161 //=================================================================================
162 // function : updateAttributes()
163 // purpose  : Workaround for Translation and Rotation operations with unchecked option "Create a copy".
164 //            In this case PublishInStudy isn't called, so we need to update object's attributes manually
165 //=================================================================================
166 void GEOMBase_Skeleton::updateAttributes( GEOM::GEOM_Object_ptr theObj,
167                                           const QStringList& theParameters)
168 {
169   SALOMEDS::Study_var aStudy = GeometryGUI::ClientStudyToStudy(getStudy()->studyDS());
170   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
171   SALOMEDS::SObject_var aSObject = aStudy->FindObjectID(theObj->GetStudyEntry());
172   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeString");
173   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
174
175   std::string aValue = aStringAttrib->Value();
176   /* ouv: temporarily disabled
177   if( aValue != "" )
178     aValue += "|";
179   for( int i = 0, n = theParameters.count(); i < n; i++ ) {
180     std::string aParameter = theParameters[i].toStdString();
181     if(aStudy->IsVariable(aParameter.c_str()))
182       aValue += aParameter;
183     if(i != n-1)
184       aValue += ":";
185   }
186   */
187   aStringAttrib->SetValue(aValue.c_str());
188 }
189
190 //=================================================================================
191 // function : ClickOnCancel()
192 // purpose  :
193 //=================================================================================
194 void GEOMBase_Skeleton::ClickOnCancel()
195 {
196   close();
197 }
198
199 //=================================================================================
200 // function : LineEditReturnPressed()
201 // purpose  :
202 //=================================================================================
203 void GEOMBase_Skeleton::LineEditReturnPressed()
204 {
205   if (!myEditCurrentArgument)
206     return;
207
208   /* User name of object input management                          */
209   /* If successfull the selection is changed and signal emitted... */
210   /* so SelectionIntoArgument() is automatically called.           */
211   const QString objectUserName = myEditCurrentArgument->text();
212   QWidget* thisWidget = (QWidget*)this;
213
214   SALOME_ListIO aList;
215   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
216   if (aSelMgr)
217     aSelMgr->selectedObjects(aList);
218   if (GEOMBase::SelectionByNameInDialogs(thisWidget, objectUserName, aList))
219     myEditCurrentArgument->setText(objectUserName);
220 }
221
222 //=================================================================================
223 // function : DeactivateActiveDialog()
224 // purpose  :
225 //=================================================================================
226 void GEOMBase_Skeleton::DeactivateActiveDialog()
227 {
228   this->setEnabled( false );
229   globalSelection();
230   if ( myGeomGUI ) {
231     myGeomGUI->SetActiveDialogBox( 0 );
232     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
233   }
234   erasePreview();
235 }
236
237 //=================================================================================
238 // function : ActivateThisDialog()
239 // purpose  :
240 //=================================================================================
241 void GEOMBase_Skeleton::ActivateThisDialog()
242 {
243   /* Emit a signal to deactivate the active dialog */
244   if ( myGeomGUI ) myGeomGUI->EmitSignalDeactivateDialog();
245   this->setEnabled( true );
246   if ( myGeomGUI ) myGeomGUI->SetActiveDialogBox( (QDialog*)this );
247   return;
248 }
249
250 //=================================================================================
251 // function : closeEvent()
252 // purpose  : same than click on cancel button
253 //=================================================================================
254 void GEOMBase_Skeleton::closeEvent( QCloseEvent* e )
255 {
256   if ( myGeomGUI ) {
257     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
258     myGeomGUI->getApp()->updateActions();
259   }
260   QDialog::closeEvent( e );
261 }
262
263 //=================================================================================
264 // function : initName()
265 // purpose  : initialize the Name field with a string "thePrefix_X" (Vertex_3)
266 //=================================================================================
267 void GEOMBase_Skeleton::initName( const QString& thePrefix )
268 {
269   if ( !thePrefix.isNull() )
270     setPrefix( thePrefix );
271   myMainFrame->ResultName->setText( GEOMBase::GetDefaultName( getPrefix() ) );
272 }
273
274 //=================================================================================
275 // function : getNewObjectName()
276 // purpose  : returns contents of Name field
277 //=================================================================================
278 QString GEOMBase_Skeleton::getNewObjectName() const
279 {
280   return myMainFrame->ResultName->text();
281 }
282
283 //=================================================================================
284 // function : getConstructorId()
285 // purpose  :
286 //=================================================================================
287 int GEOMBase_Skeleton::getConstructorId() const
288 {
289   if ( myRBGroup )
290     return myRBGroup->checkedId();
291   return -1;
292 }
293
294 //=================================================================================
295 // function : setConstructorId( id )
296 // purpose  :
297 //=================================================================================
298 void GEOMBase_Skeleton::setConstructorId( const int id )
299 {
300   if ( myRBGroup && myRBGroup->button( id ) )
301     myRBGroup->button( id )->setChecked( true );
302 }
303
304 //=================================================================================
305 // function : unsetConstructorId
306 // purpose  :
307 //=================================================================================
308 void GEOMBase_Skeleton::unsetConstructorId()
309 {
310   // 0020428: EDF 906 GEOM : Performance for Group creation in GEOM
311   // uncheck all buttons
312   // workaround, because setChecked( false ) does not result in Qt4
313   bool isExclusive = myRBGroup->exclusive();
314   myRBGroup->setExclusive( false );
315   QList<QAbstractButton*> btnList = myRBGroup->buttons();
316   for ( int j = 0; j < 2; j++ )
317   {
318     QList<QAbstractButton*>::const_iterator it = btnList.constBegin();
319     for ( ; it != btnList.constEnd(); ++it )
320       (*it)->setCheckable( j == 1 );
321   }
322   myRBGroup->setExclusive( isExclusive );
323 }
324
325 //=================================================================================
326 // function : ClickOnHelp()
327 // purpose  :
328 //=================================================================================
329 void GEOMBase_Skeleton::ClickOnHelp()
330 {
331   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
332   if ( app ) 
333     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString(""), myHelpFileName );
334   else {
335     QString platform;
336 #ifdef WIN32
337     platform = "winapplication";
338 #else
339     platform = "application";
340 #endif
341     SUIT_MessageBox::warning( 0, QObject::tr( "WRN_WARNING" ),
342                               QObject::tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
343                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( myHelpFileName ),
344                               QObject::tr( "BUT_OK" ) );
345   }
346 }
347
348 //=================================================================================
349 //  function : setHelpFileName()
350 //  purpose  : set name for help file html
351 //=================================================================================
352 void GEOMBase_Skeleton::setHelpFileName( const QString& theName )
353 {
354   myHelpFileName = theName;
355 }
356
357 DlgRef_Skeleton* GEOMBase_Skeleton::mainFrame()
358 {
359   return myMainFrame;
360 }
361
362 QWidget* GEOMBase_Skeleton::centralWidget()
363 {
364   return myMainFrame->GroupMedium;
365 }
366
367 QPushButton* GEOMBase_Skeleton::buttonCancel() const
368 {
369   return myMainFrame->buttonCancel;
370 }
371
372 QPushButton* GEOMBase_Skeleton::buttonOk() const
373 {
374   return myMainFrame->buttonOk;
375 }
376
377 QPushButton* GEOMBase_Skeleton::buttonApply() const
378 {
379   return myMainFrame->buttonApply;
380 }
381
382 QPushButton* GEOMBase_Skeleton::buttonHelp() const
383 {
384   return myMainFrame->buttonHelp;
385 }
386
387 //=================================================================================
388 // function : keyPressEvent()
389 // purpose  :
390 //=================================================================================
391 void GEOMBase_Skeleton::keyPressEvent( QKeyEvent* e )
392 {
393   QDialog::keyPressEvent( e );
394   if ( e->isAccepted() )
395     return;
396
397   if ( e->key() == Qt::Key_F1 ) {
398     e->accept();
399     ClickOnHelp();
400   }
401 }