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