Back to index

salome-geom  6.5.0
PrimitiveGUI_BoxDlg.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_BoxDlg.cxx
00025 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
00026 //
00027 #include "PrimitiveGUI_BoxDlg.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.hxx>
00041 #include <TopExp.hxx>
00042 #include <TColStd_IndexedMapOfInteger.hxx>
00043 #include <TopTools_IndexedMapOfShape.hxx>
00044 
00045 #include <GEOMImpl_Types.hxx>
00046 
00047 //=================================================================================
00048 // class    : PrimitiveGUI_BoxDlg()
00049 // purpose  : Constructs a PrimitiveGUI_BoxDlg which is a child of 'parent', with the
00050 //            name 'name' and widget flags set to 'f'.
00051 //            The dialog will by default be modeless, unless you set 'modal' to
00052 //            TRUE to construct a modal dialog.
00053 //=================================================================================
00054 PrimitiveGUI_BoxDlg::PrimitiveGUI_BoxDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
00055                                           bool modal, Qt::WindowFlags fl)
00056   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
00057 {
00058   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_2P")));
00059   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_DXYZ")));
00060   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
00061 
00062   setWindowTitle(tr("GEOM_BOX_TITLE"));
00063 
00064   /***************************************************************/
00065   mainFrame()->GroupConstructors->setTitle(tr("GEOM_BOX"));
00066   mainFrame()->RadioButton1->setIcon(image0);
00067   mainFrame()->RadioButton2->setIcon(image1);
00068   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
00069   mainFrame()->RadioButton3->close();
00070 
00071   GroupPoints = new DlgRef_2Sel(centralWidget());
00072   GroupPoints->GroupBox1->setTitle(tr("GEOM_DIAGONAL_POINTS"));
00073   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg(1));
00074   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(2));
00075   GroupPoints->PushButton1->setIcon(image2);
00076   GroupPoints->PushButton2->setIcon(image2);
00077 
00078   GroupDimensions = new DlgRef_3Spin(centralWidget());
00079   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
00080   GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
00081   GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
00082   GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
00083 
00084   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
00085   layout->setMargin(0); layout->setSpacing(6);
00086   layout->addWidget(GroupPoints);
00087   layout->addWidget(GroupDimensions);
00088   /***************************************************************/
00089 
00090   setHelpFileName("create_box_page.html");
00091 
00092   Init();
00093 }
00094 
00095 //=================================================================================
00096 // function : ~DialogBox_Box()
00097 // purpose  : Destroys the object and frees any allocated resources
00098 //=================================================================================
00099 PrimitiveGUI_BoxDlg::~PrimitiveGUI_BoxDlg()
00100 {
00101   // no need to delete child widgets, Qt does it all for us
00102 }
00103 
00104 //=================================================================================
00105 // function : Init()
00106 // purpose  :
00107 //=================================================================================
00108 void PrimitiveGUI_BoxDlg::Init()
00109 {
00110   // Get setting of step value from file configuration
00111   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00112   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
00113 
00114   // min, max, step and decimals for spin boxes
00115   initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
00116   initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
00117   initSpinBox(GroupDimensions->SpinBox_DZ, 0.000001, COORD_MAX, step, "length_precision" );
00118 
00119   // init variables
00120   GroupPoints->LineEdit1->setReadOnly(true);
00121   GroupPoints->LineEdit2->setReadOnly(true);
00122 
00123   GroupPoints->LineEdit1->setText("");
00124   GroupPoints->LineEdit2->setText("");
00125   myPoint1.nullify();
00126   myPoint2.nullify();
00127 
00128   double initValue = 200.0;
00129   GroupDimensions->SpinBox_DX->setValue(initValue);
00130   GroupDimensions->SpinBox_DY->setValue(initValue);
00131   GroupDimensions->SpinBox_DZ->setValue(initValue);
00132 
00133   // signals and slots connections
00134   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
00135   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
00136 
00137   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
00138 
00139   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
00140   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
00141 
00142   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00143   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00144   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
00145 
00146   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
00147 
00148   initName(tr("GEOM_BOX"));
00149 
00150   setConstructorId(1); // simplest constructor
00151   ConstructorsClicked(1);
00152 }
00153 
00154 //=================================================================================
00155 // function : SetDoubleSpinBoxStep()
00156 // purpose  : Double spin box management
00157 //=================================================================================
00158 void PrimitiveGUI_BoxDlg::SetDoubleSpinBoxStep (double step)
00159 {
00160   GroupDimensions->SpinBox_DX->setSingleStep(step);
00161   GroupDimensions->SpinBox_DY->setSingleStep(step);
00162   GroupDimensions->SpinBox_DZ->setSingleStep(step);
00163 }
00164 
00165 //=================================================================================
00166 // function : ConstructorsClicked()
00167 // purpose  : Radio button management
00168 //=================================================================================
00169 void PrimitiveGUI_BoxDlg::ConstructorsClicked (int constructorId)
00170 {
00171   switch (constructorId) {
00172   case 0:
00173     {
00174       GroupDimensions->hide();
00175       GroupPoints->show();
00176 
00177       GroupPoints->PushButton1->click();
00178       break;
00179     }
00180   case 1:
00181     {
00182       GroupPoints->hide();
00183       GroupDimensions->show();
00184       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00185       globalSelection(); // close local contexts, if any
00186 
00187       displayPreview(true);
00188       break;
00189     }
00190   }
00191 
00192   qApp->processEvents();
00193   updateGeometry();
00194   resize(minimumSizeHint());
00195   SelectionIntoArgument();
00196 
00197   displayPreview(true);
00198 }
00199 
00200 //=================================================================================
00201 // function : ClickOnOk()
00202 // purpose  :
00203 //=================================================================================
00204 void PrimitiveGUI_BoxDlg::ClickOnOk()
00205 {
00206   setIsApplyAndClose( true );
00207   if (ClickOnApply())
00208     ClickOnCancel();
00209 }
00210 
00211 //=================================================================================
00212 // function : ClickOnApply()
00213 // purpose  :
00214 //=================================================================================
00215 bool PrimitiveGUI_BoxDlg::ClickOnApply()
00216 {
00217   if (!onAccept())
00218     return false;
00219 
00220   initName();
00221   // activate selection and connect selection manager
00222   ConstructorsClicked(getConstructorId());
00223   return true;
00224 }
00225 
00226 //=================================================================================
00227 // function : SelectionIntoArgument()
00228 // purpose  : Called when selection is changed or on dialog initialization or activation
00229 //=================================================================================
00230 void PrimitiveGUI_BoxDlg::SelectionIntoArgument()
00231 {
00232   if (getConstructorId() != 0)
00233     return;
00234 
00235   erasePreview();
00236   myEditCurrentArgument->setText("");
00237 
00238   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00239   SALOME_ListIO aSelList;
00240   aSelMgr->selectedObjects(aSelList);
00241 
00242   if (aSelList.Extent() != 1) {
00243     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1.nullify();
00244     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2.nullify();
00245     return;
00246   }
00247 
00248   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_VERTEX );
00249   TopoDS_Shape aShape;
00250   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
00251     QString aName = GEOMBase::GetName( aSelectedObject.get() );
00252 
00253     myEditCurrentArgument->setText(aName);
00254  
00255     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
00256       myPoint1 = aSelectedObject;
00257       if (myPoint1 && !myPoint2)
00258         GroupPoints->PushButton2->click();
00259     }
00260     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
00261       myPoint2 = aSelectedObject;
00262       if (myPoint2 && !myPoint1)
00263         GroupPoints->PushButton1->click();
00264     }
00265     if ((myPoint1 && !myPoint2) || (myPoint2 && !myPoint1)) {
00266       // clear selection
00267       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00268       myGeomGUI->getApp()->selectionMgr()->clearSelected();
00269       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00270         this, SLOT(SelectionIntoArgument()));
00271     }
00272   }
00273   displayPreview(true);
00274 }
00275 
00276 //=================================================================================
00277 // function : SetEditCurrentArgument()
00278 // purpose  :
00279 //=================================================================================
00280 void PrimitiveGUI_BoxDlg::SetEditCurrentArgument()
00281 {
00282   QPushButton* send = (QPushButton*)sender();
00283 
00284   // ?? Commented, because we need this flag in ConstructorsClicked, because
00285   // SelectionIntoArgument must be called only on dialog creation, and must not be called on
00286   // simple switch between constructors (as we need to keep old values in fields in this case)
00287 
00288   // clear selection
00289   //disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00290 
00291   if (send == GroupPoints->PushButton1) {
00292     myEditCurrentArgument = GroupPoints->LineEdit1;
00293     GroupPoints->PushButton2->setDown(false);
00294     GroupPoints->LineEdit2->setEnabled(false);
00295   }
00296   else if (send == GroupPoints->PushButton2) {
00297     myEditCurrentArgument = GroupPoints->LineEdit2;
00298     GroupPoints->PushButton1->setDown(false);
00299     GroupPoints->LineEdit1->setEnabled(false);
00300   }
00301 
00302   // enable line edit
00303   myEditCurrentArgument->setEnabled(true);
00304   myEditCurrentArgument->setFocus();
00305   // after setFocus(), because it will be setDown(false) when loses focus
00306   send->setDown(true);
00307 
00308   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00309   globalSelection(); // close local contexts, if any
00310   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
00311   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00312           this, SLOT(SelectionIntoArgument()));
00313 
00314   // seems we need it only to avoid preview disappearing, caused by selection mode change
00315   displayPreview(true);
00316 }
00317 
00318 //=================================================================================
00319 // function : ActivateThisDialog()
00320 // purpose  :
00321 //=================================================================================
00322 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
00323 {
00324   GEOMBase_Skeleton::ActivateThisDialog();
00325   if (getConstructorId() == 0) {
00326     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
00327     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00328             this, SLOT(SelectionIntoArgument()));
00329   }
00330   displayPreview(true);
00331 }
00332 
00333 //=================================================================================
00334 // function : enterEvent [REDEFINED]
00335 // purpose  :
00336 //=================================================================================
00337 void PrimitiveGUI_BoxDlg::enterEvent (QEvent*)
00338 {
00339   if (!mainFrame()->GroupConstructors->isEnabled())
00340     ActivateThisDialog();
00341 }
00342 
00343 //=================================================================================
00344 // function : ValueChangedInSpinBox()
00345 // purpose  :
00346 //=================================================================================
00347 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
00348 {
00349   displayPreview(true);
00350 }
00351 
00352 //=================================================================================
00353 // function : createOperation
00354 // purpose  :
00355 //=================================================================================
00356 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
00357 {
00358   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
00359 }
00360 
00361 //=================================================================================
00362 // function : isValid
00363 // purpose  :
00364 //=================================================================================
00365 bool PrimitiveGUI_BoxDlg::isValid (QString& msg)
00366 {
00367   bool ok = false;
00368   if( getConstructorId() == 1 )
00369   {
00370     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
00371          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) &&
00372          GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() ) &&
00373          qAbs( GroupDimensions->SpinBox_DX->value() ) > Precision::Confusion() &&
00374          qAbs( GroupDimensions->SpinBox_DY->value() ) > Precision::Confusion() &&
00375          qAbs( GroupDimensions->SpinBox_DZ->value() ) > Precision::Confusion();
00376   }
00377   else
00378   {
00379     ok = myPoint1 && myPoint2 && myPoint1 != myPoint2;
00380   }
00381   return ok;
00382 }
00383 
00384 //=================================================================================
00385 // function : execute
00386 // purpose  :
00387 //=================================================================================
00388 bool PrimitiveGUI_BoxDlg::execute (ObjectList& objects)
00389 {
00390   bool res = false;
00391 
00392   GEOM::GEOM_Object_var anObj;
00393 
00394   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
00395   
00396   switch (getConstructorId()) {
00397   case 0:
00398     {
00399       if ( myPoint1 && myPoint2 ) {
00400         anObj = anOper->MakeBoxTwoPnt(myPoint1.get(), myPoint2.get());
00401         res = true;
00402       }
00403     }
00404     break;
00405   case 1:
00406     {
00407       double x = GroupDimensions->SpinBox_DX->value();
00408       double y = GroupDimensions->SpinBox_DY->value();
00409       double z = GroupDimensions->SpinBox_DZ->value();
00410 
00411       anObj = anOper->MakeBoxDXDYDZ(x, y, z);
00412       if (!anObj->_is_nil() && !IsPreview())
00413       {
00414         QStringList aParameters;
00415         aParameters << GroupDimensions->SpinBox_DX->text();
00416         aParameters << GroupDimensions->SpinBox_DY->text();
00417         aParameters << GroupDimensions->SpinBox_DZ->text();
00418         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
00419       }
00420       res = true;
00421     }
00422     break;
00423   }
00424 
00425   if (!anObj->_is_nil())
00426     objects.push_back(anObj._retn());
00427 
00428   return res;
00429 }
00430 
00431 //=================================================================================
00432 // function : addSubshapesToStudy
00433 // purpose  : virtual method to add new SubObjects if local selection
00434 //=================================================================================
00435 void PrimitiveGUI_BoxDlg::addSubshapesToStudy()
00436 {
00437   if (getConstructorId() == 0) {
00438     GEOMBase::PublishSubObject( myPoint1.get() );
00439     GEOMBase::PublishSubObject( myPoint2.get() );
00440   }
00441 }