Back to index

salome-geom  6.5.0
PrimitiveGUI_TorusDlg.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_TorusDlg.cxx
00025 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
00026 //
00027 #include "PrimitiveGUI_TorusDlg.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_TorusDlg()
00050 // purpose  : Constructs a PrimitiveGUI_TorusDlg 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_TorusDlg::PrimitiveGUI_TorusDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
00056                                               bool modal, Qt::WindowFlags fl)
00057   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
00058 {
00059   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
00060   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_PV")));
00061   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_DXYZ")));
00062   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
00063 
00064   setWindowTitle(tr("GEOM_TORUS_TITLE"));
00065 
00066   /***************************************************************/
00067   mainFrame()->GroupConstructors->setTitle(tr("GEOM_TORUS"));
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_I").arg(1));
00078   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg(2));
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_I").arg(1));
00085   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg(2));
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_torus_page.html");
00094 
00095   Init();
00096 }
00097 
00098 //=================================================================================
00099 // function : ~PrimitiveGUI_TorusDlg()
00100 // purpose  : Destroys the object and frees any allocated resources
00101 //=================================================================================
00102 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
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_TorusDlg::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.000001, COORD_MAX, step, "length_precision" );
00119   initSpinBox(GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
00120   initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
00121   initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
00122 
00123   // init variables
00124   GroupPoints->LineEdit1->setReadOnly(true);
00125   GroupPoints->LineEdit2->setReadOnly(true);
00126 
00127   GroupPoints->LineEdit1->setText("");
00128   GroupPoints->LineEdit2->setText("");
00129   myPoint.nullify();
00130   myDir.nullify();
00131 
00132   GroupPoints->SpinBox_DX->setValue(300.0);
00133   GroupPoints->SpinBox_DY->setValue(100.0);
00134   GroupDimensions->SpinBox_DX->setValue(300.0);
00135   GroupDimensions->SpinBox_DY->setValue(100.0);
00136 
00137   // signals and slots connections
00138   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
00139   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
00140 
00141   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
00142 
00143   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
00144   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
00145 
00146   connect(GroupPoints->SpinBox_DX,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00147   connect(GroupPoints->SpinBox_DY,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00148   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00149   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00150 
00151   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
00152 
00153   initName(tr("GEOM_TORUS"));
00154 
00155   setConstructorId(1); // simplest constructor
00156   ConstructorsClicked(1);
00157 }
00158 
00159 //=================================================================================
00160 // function : SetDoubleSpinBoxStep()
00161 // purpose  : Double spin box management
00162 //=================================================================================
00163 void PrimitiveGUI_TorusDlg::SetDoubleSpinBoxStep (double step)
00164 {
00165   GroupPoints->SpinBox_DX->setSingleStep(step);
00166   GroupPoints->SpinBox_DY->setSingleStep(step);
00167   GroupDimensions->SpinBox_DX->setSingleStep(step);
00168   GroupDimensions->SpinBox_DY->setSingleStep(step);
00169 }
00170 
00171 //=================================================================================
00172 // function : ConstructorsClicked()
00173 // purpose  : Radio button management
00174 //=================================================================================
00175 void PrimitiveGUI_TorusDlg::ConstructorsClicked (int constructorId)
00176 {
00177   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00178 
00179   switch (constructorId) {
00180   case 0:
00181     {
00182       GroupDimensions->hide();
00183       GroupPoints->show();
00184 
00185       GroupPoints->PushButton1->click();
00186       break;
00187     }
00188   case 1:
00189     {
00190       GroupPoints->hide();
00191       GroupDimensions->show();
00192       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00193       globalSelection(); // close local contexts, if any
00194       break;
00195     }
00196   }
00197 
00198   qApp->processEvents();
00199   updateGeometry();
00200   resize(minimumSizeHint());
00201   SelectionIntoArgument();
00202 
00203   displayPreview(true);
00204 }
00205 
00206 //=================================================================================
00207 // function : ClickOnOk()
00208 // purpose  :
00209 //=================================================================================
00210 void PrimitiveGUI_TorusDlg::ClickOnOk()
00211 {
00212   setIsApplyAndClose( true );
00213   if (ClickOnApply())
00214     ClickOnCancel();
00215 }
00216 
00217 //=================================================================================
00218 // function : ClickOnApply()
00219 // purpose  :
00220 //=================================================================================
00221 bool PrimitiveGUI_TorusDlg::ClickOnApply()
00222 {
00223   if (!onAccept())
00224     return false;
00225 
00226   initName();
00227   // activate selection and connect selection manager
00228   ConstructorsClicked(getConstructorId());
00229   return true;
00230 }
00231 
00232 //=================================================================================
00233 // function : SelectionIntoArgument()
00234 // purpose  : Called when selection is changed or on dialog initialization or activation
00235 //=================================================================================
00236 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
00237 {
00238   if (getConstructorId() != 0)
00239     return;
00240 
00241   erasePreview();
00242   myEditCurrentArgument->setText("");
00243 
00244   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00245   SALOME_ListIO aSelList;
00246   aSelMgr->selectedObjects(aSelList);
00247 
00248   if (aSelList.Extent() != 1) {
00249     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint.nullify();
00250     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir.nullify();
00251     return;
00252   }
00253 
00254   TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_EDGE : TopAbs_VERTEX;
00255   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
00256   TopoDS_Shape aShape;
00257   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
00258     QString aName = GEOMBase::GetName( aSelectedObject.get() );
00259 
00260     myEditCurrentArgument->setText(aName);
00261     
00262     // clear selection
00263     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00264     myGeomGUI->getApp()->selectionMgr()->clearSelected();
00265     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00266            this, SLOT(SelectionIntoArgument()));
00267     
00268     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
00269       myPoint = aSelectedObject;
00270       if (myPoint && !myDir)
00271        GroupPoints->PushButton2->click();
00272     }
00273     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
00274       myDir = aSelectedObject;
00275       if (myDir && !myPoint)
00276        GroupPoints->PushButton1->click();
00277     }
00278   }
00279 
00280   displayPreview(true);
00281 }
00282 
00283 //=================================================================================
00284 // function : SetEditCurrentArgument()
00285 // purpose  :
00286 //=================================================================================
00287 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
00288 {
00289   QPushButton* send = (QPushButton*)sender();
00290 
00291   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00292   if (send == GroupPoints->PushButton1) {
00293     myEditCurrentArgument = GroupPoints->LineEdit1;
00294 
00295     GroupPoints->PushButton2->setDown(false);
00296     GroupPoints->LineEdit2->setEnabled(false);
00297 
00298     globalSelection(GEOM_POINT); // to break previous local selection
00299     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
00300   }
00301   else if (send == GroupPoints->PushButton2) {
00302     myEditCurrentArgument = GroupPoints->LineEdit2;
00303 
00304     GroupPoints->PushButton1->setDown(false);
00305     GroupPoints->LineEdit1->setEnabled(false);
00306 
00307     globalSelection(GEOM_LINE);  // to break previous local selection
00308     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
00309   }
00310   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00311           this, SLOT(SelectionIntoArgument()));
00312 
00313   // enable line edit
00314   myEditCurrentArgument->setEnabled(true);
00315   myEditCurrentArgument->setFocus();
00316   // after setFocus(), because it will be setDown(false) when loses focus
00317   send->setDown(true);
00318 
00319   // seems we need it only to avoid preview disappearing, caused by selection mode change
00320   displayPreview(true);
00321 }
00322 
00323 //=================================================================================
00324 // function : ActivateThisDialog()
00325 // purpose  :
00326 //=================================================================================
00327 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
00328 {
00329   GEOMBase_Skeleton::ActivateThisDialog();
00330 
00331   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
00332            this, SLOT( SelectionIntoArgument() ) );
00333   
00334   ConstructorsClicked( getConstructorId() );
00335 }
00336 
00337 //=================================================================================
00338 // function : enterEvent()
00339 // purpose  :
00340 //=================================================================================
00341 void PrimitiveGUI_TorusDlg::enterEvent (QEvent*)
00342 {
00343   if (!mainFrame()->GroupConstructors->isEnabled())
00344     ActivateThisDialog();
00345 }
00346 
00347 //=================================================================================
00348 // function : ValueChangedInSpinBox()
00349 // purpose  :
00350 //=================================================================================
00351 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
00352 {
00353   displayPreview(true);
00354 }
00355 
00356 //=================================================================================
00357 // function : createOperation
00358 // purpose  :
00359 //=================================================================================
00360 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
00361 {
00362   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
00363 }
00364 
00365 //=================================================================================
00366 // function : isValid
00367 // purpose  :
00368 //=================================================================================
00369 bool PrimitiveGUI_TorusDlg::isValid (QString& msg)
00370 {
00371   bool ok = false;
00372   if( getConstructorId() == 0 )
00373   {
00374     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) &&
00375          GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) &&
00376          myPoint && myDir;
00377   }
00378   else if( getConstructorId() == 1 )
00379   {
00380     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
00381          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() );
00382   }
00383   return ok;
00384 }
00385 
00386 //=================================================================================
00387 // function : execute
00388 // purpose  :
00389 //=================================================================================
00390 bool PrimitiveGUI_TorusDlg::execute (ObjectList& objects)
00391 {
00392   bool res = false;
00393 
00394   GEOM::GEOM_Object_var anObj;
00395 
00396   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
00397 
00398   switch (getConstructorId()) {
00399   case 0:
00400     if ( myPoint && myDir ) {
00401       anObj = anOper->MakeTorusPntVecRR(myPoint.get(), myDir.get(), getRadius1(), getRadius2());
00402       if (!anObj->_is_nil() && !IsPreview())
00403       {
00404         QStringList aParameters;
00405         aParameters << GroupPoints->SpinBox_DX->text();
00406         aParameters << GroupPoints->SpinBox_DY->text();
00407         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
00408       }
00409       res = true;
00410     }
00411     break;
00412   case 1:
00413     anObj = anOper->MakeTorusRR(getRadius1(), getRadius2());
00414     if (!anObj->_is_nil() && !IsPreview())
00415     {
00416       QStringList aParameters;
00417       aParameters << GroupDimensions->SpinBox_DX->text();
00418       aParameters << GroupDimensions->SpinBox_DY->text();
00419       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
00420     }
00421     res = true;
00422     break;
00423   }
00424 
00425   if (!anObj->_is_nil())
00426     objects.push_back(anObj._retn());
00427 
00428   return res;
00429 }
00430 
00431 //=================================================================================
00432 // function : getRadius1()
00433 // purpose  :
00434 //=================================================================================
00435 double PrimitiveGUI_TorusDlg::getRadius1() const
00436 {
00437   int aConstructorId = getConstructorId();
00438   if (aConstructorId == 0)
00439     return GroupPoints->SpinBox_DX->value();
00440   else if (aConstructorId == 1)
00441     return GroupDimensions->SpinBox_DX->value();
00442   return 0;
00443 }
00444 
00445 //=================================================================================
00446 // function : getRadius2()
00447 // purpose  :
00448 //=================================================================================
00449 double PrimitiveGUI_TorusDlg::getRadius2() const
00450 {
00451   int aConstructorId = getConstructorId();
00452   if (aConstructorId == 0)
00453     return GroupPoints->SpinBox_DY->value();
00454   else if (aConstructorId == 1)
00455     return GroupDimensions->SpinBox_DY->value();
00456   return 0;
00457 }
00458 
00459 //=================================================================================
00460 // function : addSubshapeToStudy
00461 // purpose  : virtual method to add new SubObjects if local selection
00462 //=================================================================================
00463 void PrimitiveGUI_TorusDlg::addSubshapesToStudy()
00464 {
00465   if ( getConstructorId() == 0 ) {
00466     GEOMBase::PublishSubObject( myPoint.get() );
00467     GEOMBase::PublishSubObject( myDir.get() );
00468   }
00469 }