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