]> SALOME platform Git repositories - modules/geom.git/blob - src/MeasureGUI/MeasureGUI_CheckCompoundOfBlocksDlg.cxx
Salome HOME
Fix issue 0020479: EDF 1116 GEOM: Create a group, "Add" button is inactive but "Selec...
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.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   : MeasureGUI_CheckCompoundOfBlocksDlg.cxx
24 //  Author : VKN
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "MeasureGUI_CheckCompoundOfBlocksDlg.h"
29 #include "MeasureGUI_1Sel1TextView_QTD.h"
30
31 #include "utilities.h"
32 #include "SUIT_Session.h"
33 #include "LightApp_SelectionMgr.h"
34 #include "SalomeApp_Tools.h"
35 #include "SalomeApp_Application.h"
36
37 #include <TopTools_IndexedMapOfShape.hxx>
38 #include <TopExp.hxx>
39 #include <TColStd_MapOfInteger.hxx>
40 #include "GEOMBase.h"
41 #include "GEOMImpl_Types.hxx"
42
43 #include <qtextedit.h>
44 #include <qlineedit.h>
45 #include <qlayout.h>
46 #include <qpushbutton.h>
47 #include <qradiobutton.h>
48 #include <qbuttongroup.h>
49 // QT Includes
50 #include <qgroupbox.h>
51 #include <qlabel.h>
52 #include <qvaluelist.h>
53
54 //VRV: porting on Qt 3.0.5
55 #if QT_VERSION >= 0x030005
56 #include <qlistbox.h>
57 #endif
58 //VRV: porting on Qt 3.0.5
59
60 #define TEXTEDIT_FONT_FAMILY "Courier"
61 #define TEXTEDIT_FONT_SIZE 11
62
63 //=================================================================================
64 // class    : MeasureGUI_CheckCompoundOfBlocksDlg()
65 // purpose  : Constructs a MeasureGUI_CheckCompoundOfBlocksDlg which is a child of 'parent', with the
66 //            name 'name' and widget flags set to 'f'.
67 //            The dialog will by default be modeless, unless you set 'modal' to
68 //            TRUE to construct a modal dialog.
69 //=================================================================================
70 MeasureGUI_CheckCompoundOfBlocksDlg::MeasureGUI_CheckCompoundOfBlocksDlg( GeometryGUI* GUI, QWidget* parent )
71   : GEOMBase_Skeleton(GUI, parent, "MeasureGUI_CheckCompoundOfBlocksDlg", false, WStyle_Customize |
72                       WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose)
73 {
74   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
75   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_CHECK_COMPOUND_OF_BLOCKS")));
76   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
77
78   setCaption( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
79
80   /***************************************************************/
81   GroupConstructors->setTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
82   RadioButton1->setPixmap( image0 );
83   RadioButton2->close( TRUE );
84   RadioButton3->close( TRUE );
85
86   myGrp = new MeasureGUI_1Sel1TextView_QTD( this, "myGrp" );
87   myGrp->GroupBox1->setTitle( tr( "GEOM_CHECK_INFOS" ) );
88   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
89
90   myGrp->TextEdit1->setReadOnly( TRUE );
91   QFont aFont( TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE );
92   aFont.setStyleHint( QFont::TypeWriter, QFont::PreferAntialias );
93   myGrp->TextEdit1->setFont( aFont );
94
95   myGrp->PushButton1->setPixmap( image1 );
96   myGrp->LineEdit1->setReadOnly( true );
97
98   /***************************************************************/
99   QGridLayout* aGBLayout = new QGridLayout( myGrp->GroupBox1->layout() );
100   aGBLayout->setAlignment( Qt::AlignTop );
101   QGridLayout* Layout2 = new QGridLayout( 0, 1, 1, 0, 6, "Layout2"); 
102   myErrorsLbl = new QLabel( tr( "GEOM_CHECK_BLOCKS_COMPOUND_ERRORS" ), myGrp, "Errors" );
103   Layout2->addWidget( myErrorsLbl, 0, 0 );
104
105   myErrorsLBox = new QListBox( myGrp, "ListBlockCompoundErrors" );
106   myErrorsLBox->setMinimumSize( 100, 100 );
107   Layout2->addWidget( myErrorsLBox, 1, 0 );
108
109   mySubShapesLbl = new QLabel( tr( "GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES" ),
110                                myGrp, "BlockCompoundSubShapes" );
111   Layout2->addWidget( mySubShapesLbl, 0, 1 );
112
113   mySubShapesLBox = new QListBox( myGrp, "ListSubShapes" );
114   mySubShapesLBox->setMinimumSize( 100, 100 );
115   mySubShapesLBox->setSelectionMode(QListBox::Extended);
116   Layout2->addWidget( mySubShapesLBox, 1, 1 );
117   aGBLayout->addLayout( Layout2, 1, 0 );
118
119   Layout1->addWidget( myGrp, 2, 0 );
120
121   connect( myErrorsLBox, SIGNAL( selectionChanged() ), SLOT( onErrorsListSelectionChanged() ) );
122   connect( mySubShapesLBox, SIGNAL( selectionChanged() ), SLOT( onSubShapesListSelectionChanged() ) );
123   /***************************************************************/
124
125   myHelpFileName = "using_measurement_tools_page.html#check_compound_anchor";
126
127   /* Initialisation */
128   Init();
129 }
130
131 //=================================================================================
132 // function : ~MeasureGUI_CheckCompoundOfBlocksDlg()
133 // purpose  : Destroys the object and frees any allocated resources
134 //=================================================================================
135 MeasureGUI_CheckCompoundOfBlocksDlg::~MeasureGUI_CheckCompoundOfBlocksDlg()
136 {
137 }
138
139 //=================================================================================
140 // function : Init()
141 // purpose  :
142 //=================================================================================
143 void MeasureGUI_CheckCompoundOfBlocksDlg::Init()
144 {
145   myEditCurrentArgument = myGrp->LineEdit1;
146
147   // signals and slots connections
148   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
149   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
150
151   connect( myGrp->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
152   connect( myGrp->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
153
154   connect(myGeomGUI->getApp()->selectionMgr(), 
155           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
156
157   initName( tr( "GEOM_BLOCKS_COMPOUND") );
158   buttonOk->setEnabled(false);
159   buttonApply->setEnabled(false);
160   activateSelection();
161   SelectionIntoArgument();
162
163   // displays Dialog
164   this->show();
165 }
166
167 //=================================================================================
168 // function : ClickOnOk()
169 // purpose  :
170 //=================================================================================
171 void MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnOk()
172 {
173   if (ClickOnApply())
174     ClickOnCancel();
175 }
176
177 //=================================================================================
178 // function : ClickOnApply()
179 // purpose  :
180 //=================================================================================
181 bool MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnApply()
182 {
183   if ( !onAccept() )
184     return false;
185
186   initName();
187   return true;
188 }
189
190 //=================================================================================
191 // function : SelectionIntoArgument
192 // purpose  :
193 //=================================================================================
194 void MeasureGUI_CheckCompoundOfBlocksDlg::SelectionIntoArgument()
195 {
196   erasePreview();
197   myObj = GEOM::GEOM_Object::_nil();
198
199   if (IObjectCount() != 1) {
200     myGrp->LineEdit1->setText("");
201     processObject();
202     return;
203   }
204
205   Standard_Boolean testResult = Standard_False;
206   GEOM::GEOM_Object_var aSelectedObject =
207     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
208
209   if (!testResult || aSelectedObject->_is_nil()) {
210     myGrp->LineEdit1->setText("");
211     processObject();
212     return;
213   }
214
215   myObj = aSelectedObject;
216   myGrp->LineEdit1->setText(GEOMBase::GetName(myObj));
217   processObject();
218   displayPreview();
219 }
220
221 //=================================================================================
222 // function : SetEditCurrentArgument
223 // purpose  :
224 //=================================================================================
225 void MeasureGUI_CheckCompoundOfBlocksDlg::SetEditCurrentArgument()
226 {
227   myGrp->LineEdit1->setFocus();
228   myEditCurrentArgument = myGrp->LineEdit1;
229   SelectionIntoArgument();
230 }
231
232 //=================================================================================
233 // function : LineEditReturnPressed()
234 // purpose  :
235 //=================================================================================
236 void MeasureGUI_CheckCompoundOfBlocksDlg::LineEditReturnPressed()
237 {
238   QLineEdit* send = (QLineEdit*)sender();
239   if (send == myGrp->LineEdit1) {
240     myEditCurrentArgument = myGrp->LineEdit1;
241     GEOMBase_Skeleton::LineEditReturnPressed();
242   }
243 }
244
245 //=================================================================================
246 // function : ActivateThisDialog()
247 // purpose  :
248 //=================================================================================
249 void MeasureGUI_CheckCompoundOfBlocksDlg::ActivateThisDialog()
250 {
251   GEOMBase_Skeleton::ActivateThisDialog();
252
253   LightApp_SelectionMgr* aSel = myGeomGUI->getApp()->selectionMgr();
254   if (aSel)
255     connect(aSel, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
256
257   activateSelection();
258   displayPreview();
259 }
260
261 //=================================================================================
262 // function : getBCErrors
263 // purpose  :
264 //=================================================================================
265 bool MeasureGUI_CheckCompoundOfBlocksDlg::getBCErrors( bool& theIsCompoundOfBlocks,
266                                                        GEOM::GEOM_IBlocksOperations::BCErrors& theErrors)
267 {
268   if ( myObj->_is_nil() )
269     return false;
270   else
271   {
272     try
273     {
274       GEOM::GEOM_IBlocksOperations::BCErrors_var aErrs;
275       theIsCompoundOfBlocks =
276         GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->CheckCompoundOfBlocks( myObj, aErrs );
277       theErrors = aErrs;
278     }
279     catch( const SALOME::SALOME_Exception& e )
280     {
281       SalomeApp_Tools::QtCatchCorbaException( e );
282       return false;
283     }
284
285     return getOperation()->IsDone();
286   }
287 }
288
289 //=================================================================================
290 // function : processObject
291 // purpose  :
292 //=================================================================================
293 void MeasureGUI_CheckCompoundOfBlocksDlg::processObject()
294 {
295   QString aMsg ("");
296   bool isCompoundOfBlocks;
297   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
298   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) )
299   {
300     myGrp->TextEdit1->setText( aMsg );
301     myErrorsLBox->clear();
302     mySubShapesLBox->clear();
303     erasePreview();
304     return;
305   }
306
307   if (isCompoundOfBlocks) {
308     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_NO_ERRORS" );
309     buttonOk->setEnabled(false);
310     buttonApply->setEnabled(false);
311   } else {
312     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_ERRORS" );
313     buttonOk->setEnabled(true);
314     buttonApply->setEnabled(true);
315   }
316   myGrp->TextEdit1->setText(aMsg);
317
318   QStringList aErrList;
319   QString aErrStr( "" );
320   QString aConSfx( " # " );
321   QString aGluedSfx( " # " );
322   int aConNum = 1;
323   int aGluedNum = 1;
324   for ( int i = 0, n = aErrs.length(); i < n; i++ )
325   {
326     aErrStr = "";
327     switch ( aErrs[i].error )
328     {
329       case GEOM::GEOM_IBlocksOperations::NOT_BLOCK :
330         aErrStr = "Not a Block";
331         break;
332       case GEOM::GEOM_IBlocksOperations::EXTRA_EDGE :
333         aErrStr = "Extra Edge";
334         break;
335       case GEOM::GEOM_IBlocksOperations::INVALID_CONNECTION :
336         aErrStr = "Invalid Connection";
337         aErrStr += aConSfx;
338         aErrStr += QString::number(aConNum);
339         aConNum++;
340         break;
341       case GEOM::GEOM_IBlocksOperations::NOT_CONNECTED :
342         aErrStr = "Not Connected";
343         break;
344       case GEOM::GEOM_IBlocksOperations::NOT_GLUED :
345         aErrStr = "Not Glued";
346         aErrStr += aGluedSfx;
347         aErrStr += QString::number(aGluedNum);
348         aGluedNum++;
349         break;
350       default :
351         aErrStr = "";
352         break;
353     }
354     if ( !aErrStr.isEmpty() )
355       aErrList.append(aErrStr);
356   }
357
358   myErrorsLBox->clear();
359   mySubShapesLBox->clear();
360   myErrorsLBox->insertStringList(aErrList);
361 }
362
363 //=================================================================================
364 // function : createOperation
365 // purpose  :
366 //=================================================================================
367 GEOM::GEOM_IOperations_ptr MeasureGUI_CheckCompoundOfBlocksDlg::createOperation()
368 {
369   return getGeomEngine()->GetIBlocksOperations(getStudyId());
370 }
371
372 //=================================================================================
373 // function : onErrorsListSelectionChanged
374 // purpose  :
375 //=================================================================================
376 void MeasureGUI_CheckCompoundOfBlocksDlg::onErrorsListSelectionChanged()
377 {
378   erasePreview();
379   int aCurItem = myErrorsLBox->currentItem();
380   if ( aCurItem < 0 )
381     return;
382   bool isCompoundOfBlocks;
383   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
384   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) )
385   {
386     myGrp->TextEdit1->setText( "" );
387     myErrorsLBox->clear();
388     mySubShapesLBox->clear();
389     return;
390   }
391   
392   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aCurItem];
393   GEOM::ListOfLong aObjLst = aErr.incriminated;
394   TopoDS_Shape aSelShape;
395   TopoDS_Shape aSubShape; 
396   TopTools_IndexedMapOfShape anIndices;
397   QStringList aSubShapeList;
398   QString aSubShapeName("");
399   Standard_CString aTypeString;
400   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) )
401   {
402     TopExp::MapShapes( aSelShape, anIndices);
403     for ( int i = 0, n = aObjLst.length(); i < n; i++ )
404     {
405       aSubShapeName = "";
406       aSubShape = anIndices.FindKey(aObjLst[i]);
407       if ( GEOMBase::GetShapeTypeString( aSubShape, aTypeString ) )     
408         aSubShapeName = QString(aTypeString) + QString("_") + QString::number(aObjLst[i]);      
409       if ( !aSubShapeName.isEmpty() )
410       aSubShapeList.append(aSubShapeName);
411     }
412   }
413   mySubShapesLBox->clear();
414   mySubShapesLBox->insertStringList(aSubShapeList);
415 }
416
417 //=================================================================================
418 // function : onSubShapesListSelectionChanged
419 // purpose  :
420 //=================================================================================
421 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
422 {
423   erasePreview();
424   int aErrCurItem = myErrorsLBox->currentItem();
425   if ( aErrCurItem < 0 )
426     return;
427   QValueList<int> aIds;
428   for ( int i = 0, n = mySubShapesLBox->count(); i < n; i++ )
429   {
430     if ( mySubShapesLBox->isSelected( i ) ) 
431       aIds.append( i );
432   }
433   if ( aIds.count() < 1 )
434     return;
435   bool isCompoundOfBlocks;
436   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
437   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) )
438   {
439     myGrp->TextEdit1->setText( "" );
440     myErrorsLBox->clear();
441     mySubShapesLBox->clear();
442     return;
443   }
444   
445   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
446   GEOM::ListOfLong aObjLst = aErr.incriminated;
447   TopoDS_Shape aSelShape;
448   TopoDS_Shape aSubShape; 
449   TopTools_IndexedMapOfShape anIndices;
450   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) )
451   {
452     QString aMess;
453     if ( !isValid( aMess ) )
454     {
455       return;
456     }
457     SALOME_Prs* aPrs = 0;
458     TopExp::MapShapes( aSelShape, anIndices);
459     QValueList<int>::iterator it;
460     for ( it = aIds.begin(); it != aIds.end(); ++it )
461     {
462       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
463       try
464       {
465         getDisplayer()->SetColor( Quantity_NOC_RED );
466         getDisplayer()->SetWidth( 3 );
467         getDisplayer()->SetToActivate( false );
468         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
469         if ( aPrs )
470           displayPreview( aPrs, true );
471       }
472       catch( const SALOME::SALOME_Exception& e )
473       {
474         SalomeApp_Tools::QtCatchCorbaException( e );
475       }
476     }
477   }
478 }
479
480 //=================================================================================
481 // function : activateSelection
482 // purpose  : activate selection of faces, shells, and solids
483 //=================================================================================
484 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
485 {
486   TColStd_MapOfInteger aMap;
487   aMap.Add( GEOM_SOLID );
488   aMap.Add( GEOM_COMPOUND );
489   globalSelection( aMap );
490 }
491
492 //=================================================================================
493 // function : enterEvent()
494 // purpose  :
495 //=================================================================================
496 void MeasureGUI_CheckCompoundOfBlocksDlg::enterEvent(QEvent* e)
497 {
498   if (!GroupConstructors->isEnabled())
499     ActivateThisDialog();
500 }
501
502 //=================================================================================
503 // function : isValid
504 // purpose  :
505 //=================================================================================
506 bool MeasureGUI_CheckCompoundOfBlocksDlg::isValid( QString& )
507 {
508   return !myObj->_is_nil();
509 }
510
511 //=================================================================================
512 // function : execute
513 // purpose  :
514 //=================================================================================
515 bool MeasureGUI_CheckCompoundOfBlocksDlg::execute( ObjectList& objects )
516 {
517   GEOM::GEOM_Object_var anObj;
518
519   anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->CheckAndImprove(myObj);
520
521   if (!anObj->_is_nil())
522     objects.push_back(anObj._retn());
523
524   return true;
525 }