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