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