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