Salome HOME
56768d709d266b6430bf7ef043dffb4c0602ebbc
[modules/geom.git] / src / GEOMBase / GEOMBase_Skeleton.cxx
1 //  Copyright (C) 2007-2008  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_Notebook.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     myNoteBook( 0 ),
60     myRBGroup( 0 )
61 {
62   setAttribute( Qt::WA_DeleteOnClose );
63
64   setModal( modal );
65
66   myMainFrame = new DlgRef_Skeleton( 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
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   if( myNoteBook )
104   {
105     delete myNoteBook;
106     myNoteBook = 0;
107   }
108 }
109
110 //=================================================================================
111 // function : Init()
112 // purpose  :
113 //=================================================================================
114 void GEOMBase_Skeleton::Init()
115 {
116   SalomeApp_Application* app = (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() );
117   if ( !myGeomGUI && app )
118     myGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
119
120   if ( !myNoteBook && app )
121     if( SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
122       myNoteBook = new SalomeApp_Notebook( appStudy );
123
124   /* init variables */
125   if ( myGeomGUI )
126     myGeomGUI->SetActiveDialogBox( this );
127
128   /* signals and slots connections */
129   connect( buttonCancel(), SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
130   if ( myGeomGUI ) {
131     connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
132     connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
133   }
134
135   // connect help button on a private slot that displays help information
136   connect( buttonHelp(), SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
137
138   /* displays Dialog */
139   myMainFrame->RadioButton1->setChecked( true );
140   myMainFrame->RadioButton4->hide();
141   myMainFrame->RadioButton5->hide();
142
143   myMainFrame->CheckBoxRestoreSS->setChecked( false );
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, int decimals )
165 {
166   spinBox->setPrecision( decimals );
167   spinBox->setDecimals( decimals ); // it's necessary to set decimals before the range setting,
168                                     // by default Qt rounds boundaries to 2 decimals at setRange
169   spinBox->setRange( min, max );
170   spinBox->setSingleStep( step );
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   /* ouv: temporarily disabled
189   if( aValue != "" )
190     aValue += "|";
191   for( int i = 0, n = theParameters.count(); i < n; i++ ) {
192     std::string aParameter = theParameters[i].toStdString();
193     if(aStudy->IsVariable(aParameter.c_str()))
194       aValue += aParameter;
195     if(i != n-1)
196       aValue += ":";
197   }
198   */
199   aStringAttrib->SetValue(aValue.c_str());
200 }
201
202 //=================================================================================
203 // function : ClickOnCancel()
204 // purpose  :
205 //=================================================================================
206 void GEOMBase_Skeleton::ClickOnCancel()
207 {
208   close();
209 }
210
211 //=================================================================================
212 // function : LineEditReturnPressed()
213 // purpose  :
214 //=================================================================================
215 void GEOMBase_Skeleton::LineEditReturnPressed()
216 {
217   if (!myEditCurrentArgument)
218     return;
219
220   /* User name of object input management                          */
221   /* If successfull the selection is changed and signal emitted... */
222   /* so SelectionIntoArgument() is automatically called.           */
223   const QString objectUserName = myEditCurrentArgument->text();
224   QWidget* thisWidget = (QWidget*)this;
225
226   SALOME_ListIO aList;
227   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
228   if (aSelMgr)
229     aSelMgr->selectedObjects(aList);
230   if (GEOMBase::SelectionByNameInDialogs(thisWidget, objectUserName, aList))
231     myEditCurrentArgument->setText(objectUserName);
232 }
233
234 //=================================================================================
235 // function : DeactivateActiveDialog()
236 // purpose  :
237 //=================================================================================
238 void GEOMBase_Skeleton::DeactivateActiveDialog()
239 {
240   this->setEnabled( false );
241   globalSelection();
242   if ( myGeomGUI ) {
243     myGeomGUI->SetActiveDialogBox( 0 );
244     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
245   }
246   erasePreview();
247 }
248
249 //=================================================================================
250 // function : ActivateThisDialog()
251 // purpose  :
252 //=================================================================================
253 void GEOMBase_Skeleton::ActivateThisDialog()
254 {
255   /* Emit a signal to deactivate the active dialog */
256   if ( myGeomGUI ) myGeomGUI->EmitSignalDeactivateDialog();
257   this->setEnabled( true );
258   if ( myGeomGUI ) myGeomGUI->SetActiveDialogBox( (QDialog*)this );
259   return;
260 }
261
262 //=================================================================================
263 // function : closeEvent()
264 // purpose  : same than click on cancel button
265 //=================================================================================
266 void GEOMBase_Skeleton::closeEvent( QCloseEvent* e )
267 {
268   if ( myGeomGUI ) {
269     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
270     myGeomGUI->getApp()->updateActions();
271   }
272   QDialog::closeEvent( e );
273 }
274
275 //=================================================================================
276 // function : initName()
277 // purpose  : initialize the Name field with a string "thePrefix_X" (Vertex_3)
278 //=================================================================================
279 void GEOMBase_Skeleton::initName( const QString& thePrefix )
280 {
281   if ( !thePrefix.isNull() )
282     setPrefix( thePrefix );
283   myMainFrame->ResultName->setText( GEOMBase::GetDefaultName( getPrefix() ) );
284 }
285
286 //=================================================================================
287 // function : getNewObjectName()
288 // purpose  : returns contents of Name field
289 //=================================================================================
290 QString GEOMBase_Skeleton::getNewObjectName() const
291 {
292   return myMainFrame->ResultName->text();
293 }
294
295 //=================================================================================
296 // function : getConstructorId()
297 // purpose  :
298 //=================================================================================
299 int GEOMBase_Skeleton::getConstructorId() const
300 {
301   if ( myRBGroup )
302     return myRBGroup->checkedId();
303   return -1;
304 }
305
306 //=================================================================================
307 // function : setConstructorId( id )
308 // purpose  :
309 //=================================================================================
310 void GEOMBase_Skeleton::setConstructorId( const int id )
311 {
312   if ( myRBGroup && myRBGroup->button( id ) )
313     myRBGroup->button( id )->setChecked( true );
314 }
315
316 //=================================================================================
317 // function : unsetConstructorId
318 // purpose  :
319 //=================================================================================
320 void GEOMBase_Skeleton::unsetConstructorId()
321 {
322   // 0020428: EDF 906 GEOM : Performance for Group creation in GEOM
323   // uncheck all buttons
324   // workaround, because setChecked( false ) does not result in Qt4
325   bool isExclusive = myRBGroup->exclusive();
326   myRBGroup->setExclusive( false );
327   QList<QAbstractButton*> btnList = myRBGroup->buttons();
328   for ( int j = 0; j < 2; j++ )
329   {
330     QList<QAbstractButton*>::const_iterator it = btnList.constBegin();
331     for ( ; it != btnList.constEnd(); ++it )
332       (*it)->setCheckable( j == 1 );
333   }
334   myRBGroup->setExclusive( isExclusive );
335 }
336
337 //=================================================================================
338 // function : ClickOnHelp()
339 // purpose  :
340 //=================================================================================
341 void GEOMBase_Skeleton::ClickOnHelp()
342 {
343   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
344   if ( app ) 
345     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString(""), myHelpFileName );
346   else {
347     QString platform;
348 #ifdef WIN32
349     platform = "winapplication";
350 #else
351     platform = "application";
352 #endif
353     SUIT_MessageBox::warning( 0, QObject::tr( "WRN_WARNING" ),
354                               QObject::tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
355                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( myHelpFileName ),
356                               QObject::tr( "BUT_OK" ) );
357   }
358 }
359
360 //=================================================================================
361 //  function : setHelpFileName()
362 //  purpose  : set name for help file html
363 //=================================================================================
364 void GEOMBase_Skeleton::setHelpFileName( const QString& theName )
365 {
366   myHelpFileName = theName;
367 }
368
369 DlgRef_Skeleton* GEOMBase_Skeleton::mainFrame()
370 {
371   return myMainFrame;
372 }
373
374 QWidget* GEOMBase_Skeleton::centralWidget()
375 {
376   return myMainFrame->GroupMedium;
377 }
378
379 QPushButton* GEOMBase_Skeleton::buttonCancel() const
380 {
381   return myMainFrame->buttonCancel;
382 }
383
384 QPushButton* GEOMBase_Skeleton::buttonOk() const
385 {
386   return myMainFrame->buttonOk;
387 }
388
389 QPushButton* GEOMBase_Skeleton::buttonApply() const
390 {
391   return myMainFrame->buttonApply;
392 }
393
394 QPushButton* GEOMBase_Skeleton::buttonHelp() const
395 {
396   return myMainFrame->buttonHelp;
397 }
398
399 //=================================================================================
400 // function : keyPressEvent()
401 // purpose  :
402 //=================================================================================
403 void GEOMBase_Skeleton::keyPressEvent( QKeyEvent* e )
404 {
405   QDialog::keyPressEvent( e );
406   if ( e->isAccepted() )
407     return;
408
409   if ( e->key() == Qt::Key_F1 ) {
410     e->accept();
411     ClickOnHelp();
412   }
413 }