Back to index

salome-geom  6.5.0
PrimitiveGUI_CylinderDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : PrimitiveGUI_CylinderDlg.cxx
00025 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
00026 //
00027 #include "PrimitiveGUI_CylinderDlg.h"
00028 
00029 #include <DlgRef.h>
00030 #include <GeometryGUI.h>
00031 #include <GEOMBase.h>
00032 
00033 #include <SUIT_Session.h>
00034 #include <SUIT_ResourceMgr.h>
00035 #include <SalomeApp_Application.h>
00036 #include <LightApp_SelectionMgr.h>
00037 
00038 // OCCT Includes
00039 #include <TopoDS_Shape.hxx>
00040 #include <TopoDS_Edge.hxx>
00041 #include <TopoDS.hxx>
00042 #include <TopExp.hxx>
00043 #include <TColStd_IndexedMapOfInteger.hxx>
00044 #include <TopTools_IndexedMapOfShape.hxx>
00045 
00046 #include <GEOMImpl_Types.hxx>
00047 
00048 //=================================================================================
00049 // class    : PrimitiveGUI_CylinderDlg()
00050 // purpose  : Constructs a PrimitiveGUI_CylinderDlg which is a child of 'parent', with the
00051 //            name 'name' and widget flags set to 'f'.
00052 //            The dialog will by default be modeless, unless you set 'modal' to
00053 //            TRUE to construct a modal dialog.
00054 //=================================================================================
00055 PrimitiveGUI_CylinderDlg::PrimitiveGUI_CylinderDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
00056                                                     bool modal, Qt::WindowFlags fl)
00057   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
00058     myInitial(true)
00059 {
00060   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CYLINDER_PV")));
00061   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CYLINDER_DXYZ")));
00062   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
00063 
00064   setWindowTitle(tr("GEOM_CYLINDER_TITLE"));
00065 
00066   /***************************************************************/
00067   mainFrame()->GroupConstructors->setTitle(tr("GEOM_CYLINDER"));
00068   mainFrame()->RadioButton1->setIcon(image0);
00069   mainFrame()->RadioButton2->setIcon(image1);
00070   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
00071   mainFrame()->RadioButton3->close();
00072 
00073   GroupPoints = new DlgRef_2Sel2Spin(centralWidget());
00074   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
00075   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
00076   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
00077   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS"));
00078   GroupPoints->TextLabel4->setText(tr("GEOM_HEIGHT"));
00079   GroupPoints->PushButton1->setIcon(image2);
00080   GroupPoints->PushButton2->setIcon(image2);
00081 
00082   GroupDimensions = new DlgRef_2Spin(centralWidget());
00083   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
00084   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
00085   GroupDimensions->TextLabel2->setText(tr("GEOM_HEIGHT"));
00086 
00087   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
00088   layout->setMargin(0); layout->setSpacing(6);
00089   layout->addWidget(GroupPoints);
00090   layout->addWidget(GroupDimensions);
00091   /***************************************************************/
00092 
00093   setHelpFileName("create_cylinder_page.html");
00094 
00095   Init();
00096 }
00097 
00098 //=================================================================================
00099 // function : ~PrimitiveGUI_CylinderDlg()
00100 // purpose  : Destroys the object and frees any allocated resources
00101 //=================================================================================
00102 PrimitiveGUI_CylinderDlg::~PrimitiveGUI_CylinderDlg()
00103 {
00104   // no need to delete child widgets, Qt does it all for us
00105 }
00106 
00107 //=================================================================================
00108 // function : Init()
00109 // purpose  :
00110 //=================================================================================
00111 void PrimitiveGUI_CylinderDlg::Init()
00112 {
00113   // Get setting of step value from file configuration
00114   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00115   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
00116 
00117   // min, max, step and decimals for spin boxes & initial values
00118   initSpinBox(GroupPoints->SpinBox_DX, 0.00001, COORD_MAX, step, "length_precision" );
00119   initSpinBox(GroupPoints->SpinBox_DY, 0.00001, COORD_MAX, step, "length_precision" );
00120   initSpinBox(GroupDimensions->SpinBox_DX, 0.00001, COORD_MAX, step, "length_precision" );
00121   initSpinBox(GroupDimensions->SpinBox_DY, 0.00001, COORD_MAX, step, "length_precision" );
00122 
00123   // init variables
00124   myEditCurrentArgument = GroupPoints->LineEdit1;
00125   GroupPoints->LineEdit1->setReadOnly(true);
00126   GroupPoints->LineEdit2->setReadOnly(true);
00127 
00128   GroupPoints->LineEdit1->setText("");
00129   GroupPoints->LineEdit2->setText("");
00130   myPoint.nullify();
00131   myDir.nullify();
00132 
00133   double aRadius(100.0), aHeight(300.0);
00134   GroupPoints->SpinBox_DX->setValue(aRadius);
00135   GroupPoints->SpinBox_DY->setValue(aHeight);
00136   GroupDimensions->SpinBox_DX->setValue(aRadius);
00137   GroupDimensions->SpinBox_DY->setValue(aHeight);
00138 
00139   // signals and slots connections
00140   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
00141   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
00142 
00143   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
00144 
00145   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
00146   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
00147 
00148   connect(GroupPoints->SpinBox_DX,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00149   connect(GroupPoints->SpinBox_DY,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00150   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00151   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00152 
00153   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
00154 
00155   initName(tr("GEOM_CYLINDER"));
00156 
00157   setConstructorId(1); // simplest constructor
00158   ConstructorsClicked(1);
00159 }
00160 
00161 //=================================================================================
00162 // function : SetDoubleSpinBoxStep()
00163 // purpose  : Double spin box management
00164 //=================================================================================
00165 void PrimitiveGUI_CylinderDlg::SetDoubleSpinBoxStep (double step)
00166 {
00167   GroupPoints->SpinBox_DX->setSingleStep(step);
00168   GroupPoints->SpinBox_DY->setSingleStep(step);
00169   GroupDimensions->SpinBox_DX->setSingleStep(step);
00170   GroupDimensions->SpinBox_DY->setSingleStep(step);
00171 }
00172 
00173 //=================================================================================
00174 // function : ConstructorsClicked()
00175 // purpose  : Radio button management
00176 //=================================================================================
00177 void PrimitiveGUI_CylinderDlg::ConstructorsClicked (int constructorId)
00178 {
00179   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00180 
00181   switch (constructorId) {
00182   case 0:
00183     {
00184       GroupDimensions->hide();
00185       GroupPoints->show();
00186 
00187       GroupPoints->PushButton1->click();
00188       break;
00189     }
00190   case 1:
00191     {
00192       GroupPoints->hide();
00193       GroupDimensions->show();
00194       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00195       globalSelection(); // close local contexts, if any
00196       break;
00197     }
00198   }
00199 
00200   qApp->processEvents();
00201   updateGeometry();
00202   resize(minimumSizeHint());
00203   SelectionIntoArgument();
00204 
00205   displayPreview(true);
00206 }
00207 
00208 //=================================================================================
00209 // function : ClickOnOk()
00210 // purpose  :
00211 //=================================================================================
00212 void PrimitiveGUI_CylinderDlg::ClickOnOk()
00213 {
00214   setIsApplyAndClose( true );
00215   if (ClickOnApply())
00216     ClickOnCancel();
00217 }
00218 
00219 //=================================================================================
00220 // function : ClickOnApply()
00221 // purpose  :
00222 //=================================================================================
00223 bool PrimitiveGUI_CylinderDlg::ClickOnApply()
00224 {
00225   if (!onAccept())
00226     return false;
00227 
00228   initName();
00229   // activate selection and connect selection manager
00230   ConstructorsClicked(getConstructorId());
00231   return true;
00232 }
00233 
00234 //=================================================================================
00235 // function : SelectionIntoArgument()
00236 // purpose  : Called when selection is changed or on dialog initialization or activation
00237 //=================================================================================
00238 void PrimitiveGUI_CylinderDlg::SelectionIntoArgument()
00239 {
00240   if (getConstructorId() != 0)
00241     return;
00242 
00243   erasePreview();
00244   myEditCurrentArgument->setText("");
00245 
00246   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00247   SALOME_ListIO aSelList;
00248   aSelMgr->selectedObjects(aSelList);
00249 
00250   if (aSelList.Extent() != 1) {
00251     if (myEditCurrentArgument == GroupPoints->LineEdit1)
00252       myPoint.nullify();
00253     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
00254       myDir.nullify();
00255     return;
00256   }
00257 
00258   TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_EDGE : TopAbs_VERTEX;
00259   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
00260   TopoDS_Shape aShape;
00261   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
00262     QString aName = GEOMBase::GetName( aSelectedObject.get() );
00263 
00264     myEditCurrentArgument->setText(aName);
00265     
00266     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
00267       myPoint = aSelectedObject;
00268       if (myPoint && !myDir)
00269         GroupPoints->PushButton2->click();
00270     }
00271     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
00272       myDir = aSelectedObject;
00273       if (myDir && !myPoint)
00274         GroupPoints->PushButton1->click();
00275     }
00276     // clear selection
00277     if ((myDir && !myPoint) || (myPoint && !myDir)) {
00278       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00279       myGeomGUI->getApp()->selectionMgr()->clearSelected();
00280       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00281              this, SLOT(SelectionIntoArgument()));
00282     }
00283   }
00284 
00285   displayPreview(true);
00286 }
00287 
00288 //=================================================================================
00289 // function : SetEditCurrentArgument()
00290 // purpose  :
00291 //=================================================================================
00292 void PrimitiveGUI_CylinderDlg::SetEditCurrentArgument()
00293 {
00294   QPushButton* send = (QPushButton*)sender();
00295 
00296   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00297   if (send == GroupPoints->PushButton1) {
00298     myEditCurrentArgument = GroupPoints->LineEdit1;
00299 
00300     GroupPoints->PushButton2->setDown(false);
00301     GroupPoints->LineEdit2->setEnabled(false);
00302 
00303     globalSelection(GEOM_POINT); // to break previous local selection
00304     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
00305   }
00306   else if (send == GroupPoints->PushButton2) {
00307     myEditCurrentArgument = GroupPoints->LineEdit2;
00308 
00309     GroupPoints->PushButton1->setDown(false);
00310     GroupPoints->LineEdit1->setEnabled(false);
00311 
00312     globalSelection(GEOM_LINE);  // to break previous local selection
00313     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
00314   }
00315   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00316           this, SLOT(SelectionIntoArgument()));
00317 
00318   // enable line edit
00319   myEditCurrentArgument->setEnabled(true);
00320   myEditCurrentArgument->setFocus();
00321   // after setFocus(), because it will be setDown(false) when loses focus
00322   send->setDown(true);
00323 
00324   // seems we need it only to avoid preview disappearing, caused by selection mode change
00325   displayPreview(true);
00326 }
00327 
00328 //=================================================================================
00329 // function : ActivateThisDialog()
00330 // purpose  :
00331 //=================================================================================
00332 void PrimitiveGUI_CylinderDlg::ActivateThisDialog()
00333 {
00334   GEOMBase_Skeleton::ActivateThisDialog();
00335 
00336   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
00337            this, SLOT( SelectionIntoArgument() ) );
00338 
00339   ConstructorsClicked( getConstructorId() );
00340 }
00341 
00342 //=================================================================================
00343 // function : enterEvent()
00344 // purpose  :
00345 //=================================================================================
00346 void PrimitiveGUI_CylinderDlg::enterEvent (QEvent*)
00347 {
00348   if (!mainFrame()->GroupConstructors->isEnabled())
00349     ActivateThisDialog();
00350 }
00351 
00352 //=================================================================================
00353 // function : ValueChangedInSpinBox()
00354 // purpose  :
00355 //=================================================================================
00356 void PrimitiveGUI_CylinderDlg::ValueChangedInSpinBox()
00357 {
00358   displayPreview(true);
00359 }
00360 
00361 //=================================================================================
00362 // function : createOperation
00363 // purpose  :
00364 //=================================================================================
00365 GEOM::GEOM_IOperations_ptr PrimitiveGUI_CylinderDlg::createOperation()
00366 {
00367   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
00368 }
00369 
00370 //=================================================================================
00371 // function : isValid
00372 // purpose  :
00373 //=================================================================================
00374 bool PrimitiveGUI_CylinderDlg::isValid (QString& msg)
00375 {
00376   bool ok = false;
00377   if( getConstructorId() == 0 )
00378   {
00379     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) &&
00380          GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) &&
00381          myPoint && myDir;
00382   }
00383   else if( getConstructorId() == 1 )
00384   {
00385     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
00386          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() );
00387   }
00388   ok = qAbs( getHeight() ) > Precision::Confusion() && ok;
00389   return ok;
00390 }
00391 
00392 //=================================================================================
00393 // function : execute
00394 // purpose  :
00395 //=================================================================================
00396 bool PrimitiveGUI_CylinderDlg::execute (ObjectList& objects)
00397 {
00398   bool res = false;
00399 
00400   GEOM::GEOM_Object_var anObj;
00401 
00402   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
00403 
00404   switch (getConstructorId()) {
00405   case 0:
00406     if ( myPoint && myDir ) {
00407       anObj = anOper->MakeCylinderPntVecRH(myPoint.get(), myDir.get(), getRadius(), getHeight());
00408       if (!anObj->_is_nil() && !IsPreview())
00409       {
00410         QStringList aParameters;
00411         aParameters << GroupPoints->SpinBox_DX->text();
00412         aParameters << GroupPoints->SpinBox_DY->text();
00413         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
00414       }
00415       res = true;
00416     }
00417     break;
00418   case 1:
00419     anObj = anOper->MakeCylinderRH(getRadius(), getHeight());
00420     if (!anObj->_is_nil() && !IsPreview())
00421     {
00422       QStringList aParameters;
00423       aParameters << GroupDimensions->SpinBox_DX->text();
00424       aParameters << GroupDimensions->SpinBox_DY->text();
00425       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
00426     }
00427     res = true;
00428     break;
00429   }
00430 
00431   if (!anObj->_is_nil())
00432     objects.push_back(anObj._retn());
00433 
00434   return res;
00435 }
00436 
00437 //=================================================================================
00438 // function : getRadius()
00439 // purpose  :
00440 //=================================================================================
00441 double PrimitiveGUI_CylinderDlg::getRadius() const
00442 {
00443   int aConstructorId = getConstructorId();
00444   if (aConstructorId == 0)
00445     return GroupPoints->SpinBox_DX->value();
00446   else if (aConstructorId == 1)
00447     return GroupDimensions->SpinBox_DX->value();
00448   return 0;
00449 }
00450 
00451 //=================================================================================
00452 // function : getHeight()
00453 // purpose  :
00454 //=================================================================================
00455 double PrimitiveGUI_CylinderDlg::getHeight() const
00456 {
00457   int aConstructorId = getConstructorId();
00458   if (aConstructorId == 0)
00459     return GroupPoints->SpinBox_DY->value();
00460   else if (aConstructorId == 1)
00461     return GroupDimensions->SpinBox_DY->value();
00462   return 0;
00463 }
00464 
00465 //=================================================================================
00466 // function : addSubshapeToStudy
00467 // purpose  : virtual method to add new SubObjects if local selection
00468 //=================================================================================
00469 void PrimitiveGUI_CylinderDlg::addSubshapesToStudy()
00470 {
00471   if ( getConstructorId() == 0 ) {
00472     GEOMBase::PublishSubObject( myPoint.get() );
00473     GEOMBase::PublishSubObject( myDir.get() );
00474   }
00475 }