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