Salome HOME
ad5d365d01fad75c413fc096e987ed8c309ac706
[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_Study.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_Session.h>
40 #include <SUIT_MessageBox.h>
41
42 #include <QKeyEvent>
43 #include <QSpinBox>
44 #include <QDoubleSpinBox>
45
46 //=================================================================================
47 // class    : GEOMBase_Skeleton()
48 // purpose  : Constructs a GEOMBase_Skeleton which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            true to construct a modal dialog.
52 //=================================================================================
53 GEOMBase_Skeleton::GEOMBase_Skeleton( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                       bool modal, Qt::WindowFlags fl )
55   : QDialog( parent, fl ), 
56     GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) ),
57     myGeomGUI( theGeometryGUI ),
58     myRBGroup( 0 )
59 {
60   setAttribute( Qt::WA_DeleteOnClose );
61
62   setModal( modal );
63
64   myMainFrame = new DlgRef_Skeleton( this );
65   QVBoxLayout* topLayout = new QVBoxLayout( this );
66   topLayout->setMargin( 0 ); topLayout->setSpacing( 0 );
67   topLayout->addWidget( myMainFrame );
68
69   myMainFrame->GroupBoxName->setTitle( tr( "GEOM_RESULT_NAME_GRP" ) );
70   myMainFrame->NameLabel->setText( tr( "GEOM_RESULT_NAME_LBL" ) );
71
72   myMainFrame->GroupBoxPublish->setTitle( tr( "GEOM_PUBLISH_RESULT_GRP" ) );
73   myMainFrame->CheckBoxRestoreSS->setText( tr( "GEOM_RESTORE_SUB_SHAPES" ) );
74
75   buttonCancel()->setText( tr( "GEOM_BUT_CLOSE" ) );
76   buttonOk()->setText( tr( "GEOM_BUT_APPLY_AND_CLOSE" ) );
77   buttonApply()->setText( tr( "GEOM_BUT_APPLY" ) );
78   buttonHelp()->setText( tr( "GEOM_BUT_HELP" ) );
79
80   myRBGroup = new QButtonGroup( this );
81   myRBGroup->addButton( myMainFrame->RadioButton1, 0 );
82   myRBGroup->addButton( myMainFrame->RadioButton2, 1 );
83   myRBGroup->addButton( myMainFrame->RadioButton3, 2 );
84   myRBGroup->addButton( myMainFrame->RadioButton4, 3 );
85   myRBGroup->addButton( myMainFrame->RadioButton5, 4 );
86
87   connect( myRBGroup, SIGNAL( buttonClicked( int ) ), this, SIGNAL( constructorsClicked( int ) ) );
88
89   Init();
90 }
91
92 //=================================================================================
93 // function : ~GEOMBase_Skeleton()
94 // purpose  : Destroys the object and frees any allocated resources
95 //=================================================================================
96 GEOMBase_Skeleton::~GEOMBase_Skeleton()
97 {
98   if ( myGeomGUI )
99     myGeomGUI->SetActiveDialogBox( 0 );
100 }
101
102 //=================================================================================
103 // function : Init()
104 // purpose  :
105 //=================================================================================
106 void GEOMBase_Skeleton::Init()
107 {
108   SalomeApp_Application* app = (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() );
109   if ( !myGeomGUI && app )
110     myGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
111
112   /* init variables */
113   if ( myGeomGUI )
114     myGeomGUI->SetActiveDialogBox( this );
115
116   /* signals and slots connections */
117   connect( buttonCancel(), SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
118   if ( myGeomGUI ) {
119     connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
120     connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
121   }
122
123   // connect help button on a private slot that displays help information
124   connect( buttonHelp(), SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
125
126   /* displays Dialog */
127   myMainFrame->RadioButton1->setChecked( true );
128   myMainFrame->RadioButton4->hide();
129   myMainFrame->RadioButton5->hide();
130
131   myMainFrame->CheckBoxRestoreSS->setChecked( false );
132   myMainFrame->GroupBoxPublish->hide();
133 }
134
135 //=================================================================================
136 // function : initSpinBox()
137 // purpose  : 
138 //=================================================================================
139 void GEOMBase_Skeleton::initSpinBox( QSpinBox* spinBox, 
140                                      int min,  int max, int step )
141 {
142   spinBox->setRange( min, max );
143   spinBox->setSingleStep( step );
144 }
145
146 //=================================================================================
147 // function : initSpinBox()
148 // purpose  : 
149 //=================================================================================
150 void GEOMBase_Skeleton::initSpinBox( SalomeApp_DoubleSpinBox* spinBox, 
151                                      double min,  double max, 
152                                      double step, int decimals )
153 {
154   spinBox->setPrecision( decimals );
155   spinBox->setDecimals( decimals ); // it's necessary to set decimals before the range setting,
156                                     // by default Qt rounds boundaries to 2 decimals at setRange
157   spinBox->setRange( min, max );
158   spinBox->setSingleStep( step );
159 }
160
161 //=================================================================================
162 // function : updateAttributes()
163 // purpose  : Workaround for Translation and Rotation operations with unchecked option "Create a copy".
164 //            In this case PublishInStudy isn't called, so we need to update object's attributes manually
165 //=================================================================================
166 void GEOMBase_Skeleton::updateAttributes( GEOM::GEOM_Object_ptr theObj,
167                                           const QStringList& theParameters)
168 {
169   SALOMEDS::Study_var aStudy = GeometryGUI::ClientStudyToStudy(getStudy()->studyDS());
170   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
171   SALOMEDS::SObject_var aSObject = aStudy->FindObjectID(theObj->GetStudyEntry());
172   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeString");
173   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
174
175   std::string aValue = aStringAttrib->Value();
176   if( aValue != "" )
177     aValue += "|";
178   for( int i = 0, n = theParameters.count(); i < n; i++ ) {
179     std::string aParameter = theParameters[i].toStdString();
180     if(aStudy->IsVariable(aParameter.c_str()))
181       aValue += aParameter;
182     if(i != n-1)
183       aValue += ":";
184   }
185   aStringAttrib->SetValue(aValue.c_str());
186 }
187
188 //=================================================================================
189 // function : ClickOnCancel()
190 // purpose  :
191 //=================================================================================
192 void GEOMBase_Skeleton::ClickOnCancel()
193 {
194   close();
195 }
196
197 //=================================================================================
198 // function : LineEditReturnPressed()
199 // purpose  :
200 //=================================================================================
201 void GEOMBase_Skeleton::LineEditReturnPressed()
202 {
203   if (!myEditCurrentArgument)
204     return;
205
206   /* User name of object input management                          */
207   /* If successfull the selection is changed and signal emitted... */
208   /* so SelectionIntoArgument() is automatically called.           */
209   const QString objectUserName = myEditCurrentArgument->text();
210   QWidget* thisWidget = (QWidget*)this;
211
212   SALOME_ListIO aList;
213   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
214   if (aSelMgr)
215     aSelMgr->selectedObjects(aList);
216   if (GEOMBase::SelectionByNameInDialogs(thisWidget, objectUserName, aList))
217     myEditCurrentArgument->setText(objectUserName);
218 }
219
220 //=================================================================================
221 // function : DeactivateActiveDialog()
222 // purpose  :
223 //=================================================================================
224 void GEOMBase_Skeleton::DeactivateActiveDialog()
225 {
226   this->setEnabled( false );
227   globalSelection();
228   if ( myGeomGUI ) {
229     myGeomGUI->SetActiveDialogBox( 0 );
230     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
231   }
232   erasePreview();
233 }
234
235 //=================================================================================
236 // function : ActivateThisDialog()
237 // purpose  :
238 //=================================================================================
239 void GEOMBase_Skeleton::ActivateThisDialog()
240 {
241   /* Emit a signal to deactivate the active dialog */
242   if ( myGeomGUI ) myGeomGUI->EmitSignalDeactivateDialog();
243   this->setEnabled( true );
244   if ( myGeomGUI ) myGeomGUI->SetActiveDialogBox( (QDialog*)this );
245   return;
246 }
247
248 //=================================================================================
249 // function : closeEvent()
250 // purpose  : same than click on cancel button
251 //=================================================================================
252 void GEOMBase_Skeleton::closeEvent( QCloseEvent* e )
253 {
254   if ( myGeomGUI ) {
255     disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
256     myGeomGUI->getApp()->updateActions();
257   }
258   QDialog::closeEvent( e );
259 }
260
261 //=================================================================================
262 // function : initName()
263 // purpose  : initialize the Name field with a string "thePrefix_X" (Vertex_3)
264 //=================================================================================
265 void GEOMBase_Skeleton::initName( const QString& thePrefix )
266 {
267   if ( !thePrefix.isNull() )
268     setPrefix( thePrefix );
269   myMainFrame->ResultName->setText( GEOMBase::GetDefaultName( getPrefix() ) );
270 }
271
272 //=================================================================================
273 // function : getNewObjectName()
274 // purpose  : returns contents of Name field
275 //=================================================================================
276 QString GEOMBase_Skeleton::getNewObjectName() const
277 {
278   return myMainFrame->ResultName->text();
279 }
280
281 //=================================================================================
282 // function : getConstructorId()
283 // purpose  :
284 //=================================================================================
285 int GEOMBase_Skeleton::getConstructorId() const
286 {
287   if ( myRBGroup )
288     return myRBGroup->checkedId();
289   return -1;
290 }
291
292 void GEOMBase_Skeleton::setConstructorId( const int id )
293 {
294   if ( myRBGroup && myRBGroup->button( id ) )
295     myRBGroup->button( id )->setChecked( true );
296 }
297
298 //=================================================================================
299 // function : ClickOnHelp()
300 // purpose  :
301 //=================================================================================
302 void GEOMBase_Skeleton::ClickOnHelp()
303 {
304   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
305   if ( app ) 
306     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString(""), myHelpFileName );
307   else {
308     QString platform;
309 #ifdef WIN32
310     platform = "winapplication";
311 #else
312     platform = "application";
313 #endif
314     SUIT_MessageBox::warning( 0, QObject::tr( "WRN_WARNING" ),
315                               QObject::tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
316                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( myHelpFileName ),
317                               QObject::tr( "BUT_OK" ) );
318   }
319 }
320
321 //=================================================================================
322 //  function : setHelpFileName()
323 //  purpose  : set name for help file html
324 //=================================================================================
325 void GEOMBase_Skeleton::setHelpFileName( const QString& theName )
326 {
327   myHelpFileName = theName;
328 }
329
330 DlgRef_Skeleton* GEOMBase_Skeleton::mainFrame()
331 {
332   return myMainFrame;
333 }
334
335 QWidget* GEOMBase_Skeleton::centralWidget()
336 {
337   return myMainFrame->GroupMedium;
338 }
339
340 QPushButton* GEOMBase_Skeleton::buttonCancel() const
341 {
342   return myMainFrame->buttonCancel;
343 }
344
345 QPushButton* GEOMBase_Skeleton::buttonOk() const
346 {
347   return myMainFrame->buttonOk;
348 }
349
350 QPushButton* GEOMBase_Skeleton::buttonApply() const
351 {
352   return myMainFrame->buttonApply;
353 }
354
355 QPushButton* GEOMBase_Skeleton::buttonHelp() const
356 {
357   return myMainFrame->buttonHelp;
358 }
359
360 //=================================================================================
361 // function : keyPressEvent()
362 // purpose  :
363 //=================================================================================
364 void GEOMBase_Skeleton::keyPressEvent( QKeyEvent* e )
365 {
366   QDialog::keyPressEvent( e );
367   if ( e->isAccepted() )
368     return;
369
370   if ( e->key() == Qt::Key_F1 ) {
371     e->accept();
372     ClickOnHelp();
373   }
374 }