]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMBase/GEOMBase_Skeleton.cxx
Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / GEOMBase / GEOMBase_Skeleton.cxx
1 //  Copyright (C) 2007-2010  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
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   /* displays Dialog */
129   myMainFrame->RadioButton1->setChecked( true );
130   myMainFrame->RadioButton4->hide();
131   myMainFrame->RadioButton5->hide();
132
133   myMainFrame->CheckBoxRestoreSS->setChecked( false );
134   myMainFrame->CheckBoxAddPrefix->setChecked( true );
135   myMainFrame->GroupBoxPublish->hide();
136 }
137
138 //=================================================================================
139 // function : initSpinBox()
140 // purpose  : 
141 //=================================================================================
142 void GEOMBase_Skeleton::initSpinBox( QSpinBox* spinBox, 
143                                      int min,  int max, int step )
144 {
145   spinBox->setRange( min, max );
146   spinBox->setSingleStep( step );
147 }
148
149 //=================================================================================
150 // function : initSpinBox()
151 // purpose  : 
152 //=================================================================================
153 void GEOMBase_Skeleton::initSpinBox( SalomeApp_DoubleSpinBox* spinBox, 
154                                      double min,  double max, 
155                                      double step, const char* quantity )
156 {
157   // Obtain precision from preferences
158   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
159   int aPrecision = resMgr->integerValue( "Geometry", quantity, 6 );
160   
161   spinBox->setPrecision( aPrecision );
162   spinBox->setDecimals( qAbs( aPrecision ) ); // it's necessary to set decimals before the range setting,
163                                     // by default Qt rounds boundaries to 2 decimals at setRange
164   spinBox->setRange( min, max );
165   spinBox->setSingleStep( step );
166   
167   // Add a hint for the user saying how to tune precision
168   QString userPropName = QObject::tr( QString( "GEOM_PREF_%1" ).arg( quantity ).toLatin1().constData() );
169   spinBox->setProperty( "validity_tune_hint", 
170                         QVariant( QObject::tr( "GEOM_PRECISION_HINT" ).arg( userPropName ) ) );
171 }
172
173 //=================================================================================
174 // function : updateAttributes()
175 // purpose  : Workaround for Translation and Rotation operations with unchecked option "Create a copy".
176 //            In this case PublishInStudy isn't called, so we need to update object's attributes manually
177 //=================================================================================
178 void GEOMBase_Skeleton::updateAttributes( GEOM::GEOM_Object_ptr theObj,
179                                           const QStringList& theParameters)
180 {
181   SALOMEDS::Study_var aStudy = GeometryGUI::ClientStudyToStudy(getStudy()->studyDS());
182   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
183   SALOMEDS::SObject_var aSObject = aStudy->FindObjectID(theObj->GetStudyEntry());
184   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeString");
185   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
186
187   std::string aValue = aStringAttrib->Value();
188   if( aValue != "" )
189     aValue += "|";
190   for( int i = 0, n = theParameters.count(); i < n; i++ ) {
191     std::string aParameter = theParameters[i].toStdString();
192     if(aStudy->IsVariable(aParameter.c_str()))
193       aValue += aParameter;
194     if(i != n-1)
195       aValue += ":";
196   }
197   aStringAttrib->SetValue(aValue.c_str());
198 }
199
200 //=================================================================================
201 // function : ClickOnCancel()
202 // purpose  :
203 //=================================================================================
204 void GEOMBase_Skeleton::ClickOnCancel()
205 {
206   close();
207 }
208
209 //=================================================================================
210 // function : LineEditReturnPressed()
211 // purpose  :
212 //=================================================================================
213 void GEOMBase_Skeleton::LineEditReturnPressed()
214 {
215   if (!myEditCurrentArgument)
216     return;
217
218   /* User name of object input management                          */
219   /* If successfull the selection is changed and signal emitted... */
220   /* so SelectionIntoArgument() is automatically called.           */
221   const QString objectUserName = myEditCurrentArgument->text();
222   QWidget* thisWidget = (QWidget*)this;
223
224   SALOME_ListIO aList;
225   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
226   if (aSelMgr)
227     aSelMgr->selectedObjects(aList);
228   if (GEOMBase::SelectionByNameInDialogs(thisWidget, objectUserName, aList))
229     myEditCurrentArgument->setText(objectUserName);
230 }
231
232 //=================================================================================
233 // function : DeactivateActiveDialog()
234 // purpose  :
235 //=================================================================================
236 void GEOMBase_Skeleton::DeactivateActiveDialog()
237 {
238   this->setEnabled( false );
239   globalSelection();
240   if ( myGeomGUI ) {
241     myGeomGUI->SetActiveDialogBox( 0 );
242     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
243   }
244   erasePreview();
245 }
246
247 //=================================================================================
248 // function : ActivateThisDialog()
249 // purpose  :
250 //=================================================================================
251 void GEOMBase_Skeleton::ActivateThisDialog()
252 {
253   /* Emit a signal to deactivate the active dialog */
254   if ( myGeomGUI ) myGeomGUI->EmitSignalDeactivateDialog();
255   this->setEnabled( true );
256   if ( myGeomGUI ) myGeomGUI->SetActiveDialogBox( (QDialog*)this );
257   return;
258 }
259
260 //=================================================================================
261 // function : closeEvent()
262 // purpose  : same than click on cancel button
263 //=================================================================================
264 void GEOMBase_Skeleton::closeEvent( QCloseEvent* e )
265 {
266   if ( myGeomGUI ) {
267     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
268     myGeomGUI->getApp()->updateActions();
269   }
270   QDialog::closeEvent( e );
271 }
272
273 //=================================================================================
274 // function : initName()
275 // purpose  : initialize the Name field with a string "thePrefix_X" (Vertex_3)
276 //=================================================================================
277 void GEOMBase_Skeleton::initName( const QString& thePrefix )
278 {
279   if ( !thePrefix.isNull() )
280     setPrefix( thePrefix );
281   myMainFrame->ResultName->setText( GEOMBase::GetDefaultName( getPrefix() ) );
282 }
283
284 //=================================================================================
285 // function : getNewObjectName()
286 // purpose  : returns contents of Name field
287 //=================================================================================
288 QString GEOMBase_Skeleton::getNewObjectName() const
289 {
290   return myMainFrame->ResultName->text();
291 }
292
293 //=================================================================================
294 // function : getConstructorId()
295 // purpose  :
296 //=================================================================================
297 int GEOMBase_Skeleton::getConstructorId() const
298 {
299   if ( myRBGroup )
300     return myRBGroup->checkedId();
301   return -1;
302 }
303
304 //=================================================================================
305 // function : setConstructorId( id )
306 // purpose  :
307 //=================================================================================
308 void GEOMBase_Skeleton::setConstructorId( const int id )
309 {
310   if ( myRBGroup && myRBGroup->button( id ) )
311     myRBGroup->button( id )->setChecked( true );
312 }
313
314 //=================================================================================
315 // function : unsetConstructorId
316 // purpose  :
317 //=================================================================================
318 void GEOMBase_Skeleton::unsetConstructorId()
319 {
320   // 0020428: EDF 906 GEOM : Performance for Group creation in GEOM
321   // uncheck all buttons
322   // workaround, because setChecked( false ) does not result in Qt4
323   bool isExclusive = myRBGroup->exclusive();
324   myRBGroup->setExclusive( false );
325   QList<QAbstractButton*> btnList = myRBGroup->buttons();
326   for ( int j = 0; j < 2; j++ )
327   {
328     QList<QAbstractButton*>::const_iterator it = btnList.constBegin();
329     for ( ; it != btnList.constEnd(); ++it )
330       (*it)->setCheckable( j == 1 );
331   }
332   myRBGroup->setExclusive( isExclusive );
333 }
334
335 //=================================================================================
336 // function : ClickOnHelp()
337 // purpose  :
338 //=================================================================================
339 void GEOMBase_Skeleton::ClickOnHelp()
340 {
341   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
342   if ( app ) 
343     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString(""), myHelpFileName );
344   else {
345     QString platform;
346 #ifdef WIN32
347     platform = "winapplication";
348 #else
349     platform = "application";
350 #endif
351     SUIT_MessageBox::warning( 0, QObject::tr( "WRN_WARNING" ),
352                               QObject::tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
353                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( myHelpFileName ),
354                               QObject::tr( "BUT_OK" ) );
355   }
356 }
357
358 //=================================================================================
359 //  function : setHelpFileName()
360 //  purpose  : set name for help file html
361 //=================================================================================
362 void GEOMBase_Skeleton::setHelpFileName( const QString& theName )
363 {
364   myHelpFileName = theName;
365 }
366
367 DlgRef_Skeleton* GEOMBase_Skeleton::mainFrame()
368 {
369   return myMainFrame;
370 }
371
372 QWidget* GEOMBase_Skeleton::centralWidget()
373 {
374   return myMainFrame->GroupMedium;
375 }
376
377 QPushButton* GEOMBase_Skeleton::buttonCancel() const
378 {
379   return myMainFrame->buttonCancel;
380 }
381
382 QPushButton* GEOMBase_Skeleton::buttonOk() const
383 {
384   return myMainFrame->buttonOk;
385 }
386
387 QPushButton* GEOMBase_Skeleton::buttonApply() const
388 {
389   return myMainFrame->buttonApply;
390 }
391
392 QPushButton* GEOMBase_Skeleton::buttonHelp() const
393 {
394   return myMainFrame->buttonHelp;
395 }
396
397 //=================================================================================
398 // function : keyPressEvent()
399 // purpose  :
400 //=================================================================================
401 void GEOMBase_Skeleton::keyPressEvent( QKeyEvent* e )
402 {
403   QDialog::keyPressEvent( e );
404   if ( e->isAccepted() )
405     return;
406
407   if ( e->key() == Qt::Key_F1 ) {
408     e->accept();
409     ClickOnHelp();
410   }
411 }