Salome HOME
Porting to OCCT development version: Standard_PI -> M_PI
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.cxx
1 // Copyright (C) 2007-2011  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   GEOM::GEOM_Object_var aSelectedObject =
180     GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
181
182   if ( 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(true);
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(true);
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     GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
245     try {
246       GEOM::GEOM_IBlocksOperations::BCErrors_var aErrs;
247       theIsCompoundOfBlocks = anOper->CheckCompoundOfBlocks( myObj, aErrs );
248       if (anOper->IsDone() && aErrs->length() > 0)
249       //if (anOper->IsDone() && !aErrs._is_nil())
250         theErrors = aErrs;
251     }
252     catch ( const SALOME::SALOME_Exception& e ) {
253       SalomeApp_Tools::QtCatchCorbaException( e );
254       return false;
255     }
256
257     return anOper->IsDone();
258   }
259 }
260
261 //=================================================================================
262 // function : processObject
263 // purpose  :
264 //=================================================================================
265 void MeasureGUI_CheckCompoundOfBlocksDlg::processObject()
266 {
267   QString aMsg ( "" );
268   bool isCompoundOfBlocks;
269   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
270   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
271     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_FAILED" );
272     myGrp->TextView1->setText( aMsg );
273     myGrp->ListBox1->clear();
274     myGrp->ListBox2->clear();
275     erasePreview();
276     return;
277   }
278
279   if ( isCompoundOfBlocks ) {
280     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_NO_ERRORS" );
281     buttonOk()->setEnabled( false );
282     buttonApply()->setEnabled( false );
283   }
284   else {
285     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_ERRORS" );
286     buttonOk()->setEnabled( true );
287     buttonApply()->setEnabled( true );
288   }
289   myGrp->TextView1->setText( aMsg );
290
291   QStringList aErrList;
292   QString aErrStr( "" );
293   QString aConSfx( " # " );
294   QString aGluedSfx( " # " );
295   int aConNum = 1;
296   int aGluedNum = 1;
297   for ( int i = 0, n = aErrs.length(); i < n; i++ ) {
298     aErrStr = "";
299     switch ( aErrs[i].error ) {
300       case GEOM::GEOM_IBlocksOperations::NOT_BLOCK :
301         aErrStr = "Not a Block";
302         break;
303       case GEOM::GEOM_IBlocksOperations::EXTRA_EDGE :
304         aErrStr = "Extra Edge";
305         break;
306       case GEOM::GEOM_IBlocksOperations::INVALID_CONNECTION :
307         aErrStr = "Invalid Connection";
308         aErrStr += aConSfx;
309         aErrStr += QString::number( aConNum );
310         aConNum++;
311         break;
312       case GEOM::GEOM_IBlocksOperations::NOT_CONNECTED :
313         aErrStr = "Not Connected";
314         break;
315       case GEOM::GEOM_IBlocksOperations::NOT_GLUED :
316         aErrStr = "Not Glued";
317         aErrStr += aGluedSfx;
318         aErrStr += QString::number( aGluedNum );
319         aGluedNum++;
320         break;
321       default :
322         aErrStr = "";
323         break;
324     }
325     if ( !aErrStr.isEmpty() )
326       aErrList.append( aErrStr );
327   }
328
329   myGrp->ListBox1->clear();
330   myGrp->ListBox2->clear();
331   myGrp->ListBox1->addItems( aErrList );
332 }
333
334 //=================================================================================
335 // function : createOperation
336 // purpose  :
337 //=================================================================================
338 GEOM::GEOM_IOperations_ptr MeasureGUI_CheckCompoundOfBlocksDlg::createOperation()
339 {
340   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
341 }
342
343 //=================================================================================
344 // function : onErrorsListSelectionChanged
345 // purpose  :
346 //=================================================================================
347 void MeasureGUI_CheckCompoundOfBlocksDlg::onErrorsListSelectionChanged()
348 {
349   erasePreview();
350   int aCurItem = myGrp->ListBox1->currentRow();
351   if ( aCurItem < 0 )
352     return;
353   bool isCompoundOfBlocks;
354   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
355   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
356     myGrp->TextView1->setText( "" );
357     myGrp->ListBox1->clear();
358     myGrp->ListBox2->clear();
359     return;
360   }
361
362   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aCurItem];
363   GEOM::ListOfLong aObjLst = aErr.incriminated;
364   QStringList aSubShapeList;
365   TopoDS_Shape aSelShape;
366   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
367     TopTools_IndexedMapOfShape anIndices;
368     TopExp::MapShapes( aSelShape, anIndices );
369     for ( int i = 0, n = aObjLst.length(); i < n; i++ ) {
370       TopoDS_Shape aSubShape = anIndices.FindKey( aObjLst[i] );
371       QString aType = GEOMBase::GetShapeTypeString( aSubShape );
372       if ( !aType.isEmpty() )
373         aSubShapeList.append( QString( "%1_%2" ).arg( aType ).arg( aObjLst[i] ) );
374     }
375   }
376   myGrp->ListBox2->clear();
377   myGrp->ListBox2->addItems( aSubShapeList );
378 }
379
380 //=================================================================================
381 // function : onSubShapesListSelectionChanged
382 // purpose  :
383 //=================================================================================
384 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
385 {
386   erasePreview();
387   int aErrCurItem = myGrp->ListBox1->currentRow();
388   if ( aErrCurItem < 0 )
389     return;
390   QList<int> aIds;
391   for ( int i = 0, n = myGrp->ListBox2->count(); i < n; i++ ) {
392     if ( myGrp->ListBox2->item( i )->isSelected() )
393       aIds.append( i );
394   }
395   if ( aIds.count() < 1 )
396     return;
397   bool isCompoundOfBlocks;
398   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
399   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
400     myGrp->TextView1->setText( "" );
401     myGrp->ListBox1->clear();
402     myGrp->ListBox2->clear();
403     return;
404   }
405
406   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
407   GEOM::ListOfLong aObjLst = aErr.incriminated;
408   TopoDS_Shape aSelShape;
409   TopoDS_Shape aSubShape;
410   TopTools_IndexedMapOfShape anIndices;
411   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
412     QString aMess;
413     if ( !isValid( aMess ) ) {
414       return;
415     }
416     SALOME_Prs* aPrs = 0;
417     TopExp::MapShapes( aSelShape, anIndices);
418     QList<int>::iterator it;
419     for ( it = aIds.begin(); it != aIds.end(); ++it ) {
420       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
421       try {
422         getDisplayer()->SetColor( Quantity_NOC_RED );
423         getDisplayer()->SetWidth( 3 );
424         getDisplayer()->SetToActivate( false );
425         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
426         if ( aPrs )
427           displayPreview( aPrs, true );
428       }
429       catch ( const SALOME::SALOME_Exception& e ) {
430         SalomeApp_Tools::QtCatchCorbaException( e );
431       }
432     }
433   }
434 }
435
436 //=================================================================================
437 // function : activateSelection
438 // purpose  : activate selection of faces, shells, and solids
439 //=================================================================================
440 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
441 {
442   TColStd_MapOfInteger aMap;
443   aMap.Add( GEOM_SOLID );
444   aMap.Add( GEOM_COMPOUND );
445   globalSelection( aMap );
446 }
447
448 //=================================================================================
449 // function : enterEvent()
450 // purpose  :
451 //=================================================================================
452 void MeasureGUI_CheckCompoundOfBlocksDlg::enterEvent( QEvent* )
453 {
454   if ( !mainFrame()->GroupConstructors->isEnabled() )
455     ActivateThisDialog();
456 }
457
458 //=================================================================================
459 // function : isValid
460 // purpose  :
461 //=================================================================================
462 bool MeasureGUI_CheckCompoundOfBlocksDlg::isValid( QString& )
463 {
464   return !myObj->_is_nil();
465 }
466
467 //=================================================================================
468 // function : execute
469 // purpose  :
470 //=================================================================================
471 bool MeasureGUI_CheckCompoundOfBlocksDlg::execute( ObjectList& objects )
472 {
473   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
474   GEOM::GEOM_Object_var anObj = anOper->CheckAndImprove( myObj );
475
476   if ( !anObj->_is_nil() )
477     objects.push_back( anObj._retn() );
478
479   return true;
480 }