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