Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.cxx
1 // Copyright (C) 2007-2013  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.h"
28 #include "MeasureGUI_CheckCompoundOfBlocksDlg.h"
29 #include "MeasureGUI_Widgets.h"
30
31 #include <SUIT_Session.h>
32 #include <SUIT_ResourceMgr.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
41 #include <DlgRef.h>
42 #include <GeometryGUI.h>
43 #include <GEOMBase.h>
44 #include <GEOMImpl_Types.hxx>
45
46 #define TEXTEDIT_FONT_FAMILY "Courier"
47 #define TEXTEDIT_FONT_SIZE 11
48
49 //=================================================================================
50 // class    : MeasureGUI_CheckCompoundOfBlocksDlg()
51 // purpose  : Constructs a MeasureGUI_CheckCompoundOfBlocksDlg which is a child of 'parent', with the
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            true to construct a modal dialog.
55 //=================================================================================
56 MeasureGUI_CheckCompoundOfBlocksDlg::MeasureGUI_CheckCompoundOfBlocksDlg( GeometryGUI* GUI, QWidget* parent )
57   : GEOMBase_Skeleton( GUI, parent, false )
58 {
59   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
60   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_CHECK_COMPOUND_OF_BLOCKS" ) ) );
61   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
62
63   setWindowTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
64
65   /***************************************************************/
66   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
67   mainFrame()->RadioButton1->setIcon( image0 );
68   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
69   mainFrame()->RadioButton2->close();
70   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
71   mainFrame()->RadioButton3->close();
72
73   myGrp = new MeasureGUI_1Sel1TextView2ListBox( centralWidget() );
74   myGrp->GroupBox1->setTitle( tr( "GEOM_CHECK_INFOS" ) );
75   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
76
77   myGrp->TextView1->setReadOnly( true );
78   QFont aFont( TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE );
79   aFont.setStyleHint( QFont::TypeWriter, QFont::PreferAntialias );
80   myGrp->TextView1->setFont( aFont );
81
82   myGrp->PushButton1->setIcon( image1 );
83   myGrp->LineEdit1->setReadOnly( true );
84
85   myGrp->TextLabel2->setText( tr( "GEOM_CHECK_BLOCKS_COMPOUND_ERRORS" ) );
86   myGrp->TextLabel3->setText( tr( "GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES" ) );
87
88   myGrp->ListBox2->setSelectionMode( QAbstractItemView::ExtendedSelection );
89
90   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
91   layout->setMargin( 0 ); layout->setSpacing( 6 );
92   layout->addWidget( myGrp );
93
94   connect( myGrp->ListBox1, SIGNAL( itemSelectionChanged() ), SLOT( onErrorsListSelectionChanged() ) );
95   connect( myGrp->ListBox2, SIGNAL( itemSelectionChanged() ), SLOT( onSubShapesListSelectionChanged() ) );
96
97   /***************************************************************/
98
99   myHelpFileName = "using_measurement_tools_page.html#check_compound_anchor";
100
101   /* Initialisation */
102   Init();
103 }
104
105 //=================================================================================
106 // function : ~MeasureGUI_CheckCompoundOfBlocksDlg()
107 // purpose  : Destroys the object and frees any allocated resources
108 //=================================================================================
109 MeasureGUI_CheckCompoundOfBlocksDlg::~MeasureGUI_CheckCompoundOfBlocksDlg()
110 {
111 }
112
113 //=================================================================================
114 // function : Init()
115 // purpose  :
116 //=================================================================================
117 void MeasureGUI_CheckCompoundOfBlocksDlg::Init()
118 {
119   myEditCurrentArgument = myGrp->LineEdit1;
120
121   // signals and slots connections
122   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
123   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
124
125   connect( myGrp->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
126   connect( myGrp->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
127
128   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
129            this, SLOT( SelectionIntoArgument() ) );
130
131   initName( tr( "GEOM_BLOCKS_COMPOUND") );
132   buttonOk()->setEnabled( false );
133   buttonApply()->setEnabled( false );
134   activateSelection();
135   SelectionIntoArgument();
136 }
137
138 //=================================================================================
139 // function : ClickOnOk()
140 // purpose  :
141 //=================================================================================
142 void MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnOk()
143 {
144   if ( ClickOnApply() )
145     ClickOnCancel();
146 }
147
148 //=================================================================================
149 // function : ClickOnApply()
150 // purpose  :
151 //=================================================================================
152 bool MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnApply()
153 {
154   if ( !onAccept() )
155     return false;
156
157   initName();
158   return true;
159 }
160
161 //=================================================================================
162 // function : SelectionIntoArgument
163 // purpose  :
164 //=================================================================================
165 void MeasureGUI_CheckCompoundOfBlocksDlg::SelectionIntoArgument()
166 {
167   erasePreview();
168   myObj = GEOM::GEOM_Object::_nil();
169
170   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
171   SALOME_ListIO aSelList;
172   aSelMgr->selectedObjects(aSelList);
173
174   if (aSelList.Extent() != 1) {
175     myGrp->LineEdit1->setText( "" );
176     processObject();
177     return;
178   }
179
180   GEOM::GEOM_Object_var aSelectedObject =
181     GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
182
183   if ( 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   DISPLAY_PREVIEW_MACRO;
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   DISPLAY_PREVIEW_MACRO
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   QStringList aSubShapeList;
366   TopoDS_Shape aSelShape;
367   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
368     TopTools_IndexedMapOfShape anIndices;
369     TopExp::MapShapes( aSelShape, anIndices );
370     for ( int i = 0, n = aObjLst.length(); i < n; i++ ) {
371       TopoDS_Shape aSubShape = anIndices.FindKey( aObjLst[i] );
372       QString aType = GEOMBase::GetShapeTypeString( aSubShape );
373       if ( !aType.isEmpty() )
374         aSubShapeList.append( QString( "%1_%2" ).arg( aType ).arg( aObjLst[i] ) );
375     }
376   }
377   myGrp->ListBox2->clear();
378   myGrp->ListBox2->addItems( aSubShapeList );
379 }
380
381 //=================================================================================
382 // function : onSubShapesListSelectionChanged
383 // purpose  :
384 //=================================================================================
385 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
386 {
387   erasePreview();
388   int aErrCurItem = myGrp->ListBox1->currentRow();
389   if ( aErrCurItem < 0 )
390     return;
391   QList<int> aIds;
392   for ( int i = 0, n = myGrp->ListBox2->count(); i < n; i++ ) {
393     if ( myGrp->ListBox2->item( i )->isSelected() )
394       aIds.append( i );
395   }
396   if ( aIds.count() < 1 )
397     return;
398   bool isCompoundOfBlocks;
399   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
400   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
401     myGrp->TextView1->setText( "" );
402     myGrp->ListBox1->clear();
403     myGrp->ListBox2->clear();
404     return;
405   }
406
407   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
408   GEOM::ListOfLong aObjLst = aErr.incriminated;
409   TopoDS_Shape aSelShape;
410   TopoDS_Shape aSubShape;
411   TopTools_IndexedMapOfShape anIndices;
412   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
413     QString aMess;
414     if ( !isValid( aMess ) ) {
415       return;
416     }
417     SALOME_Prs* aPrs = 0;
418     TopExp::MapShapes( aSelShape, anIndices);
419     QList<int>::iterator it;
420     for ( it = aIds.begin(); it != aIds.end(); ++it ) {
421       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
422       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
423       int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
424       try {
425         getDisplayer()->SetColor( Quantity_NOC_RED );
426         getDisplayer()->SetWidth( w );
427         getDisplayer()->SetToActivate( false );
428         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
429         if ( aPrs )
430           displayPreview( aPrs, true );
431       }
432       catch ( const SALOME::SALOME_Exception& e ) {
433         SalomeApp_Tools::QtCatchCorbaException( e );
434       }
435     }
436   }
437 }
438
439 //=================================================================================
440 // function : activateSelection
441 // purpose  : activate selection of faces, shells, and solids
442 //=================================================================================
443 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
444 {
445   TColStd_MapOfInteger aMap;
446   aMap.Add( GEOM_SOLID );
447   aMap.Add( GEOM_COMPOUND );
448   globalSelection( aMap );
449 }
450
451 //=================================================================================
452 // function : enterEvent()
453 // purpose  :
454 //=================================================================================
455 void MeasureGUI_CheckCompoundOfBlocksDlg::enterEvent( QEvent* )
456 {
457   if ( !mainFrame()->GroupConstructors->isEnabled() )
458     ActivateThisDialog();
459 }
460
461 //=================================================================================
462 // function : isValid
463 // purpose  :
464 //=================================================================================
465 bool MeasureGUI_CheckCompoundOfBlocksDlg::isValid( QString& )
466 {
467   return !myObj->_is_nil();
468 }
469
470 //=================================================================================
471 // function : execute
472 // purpose  :
473 //=================================================================================
474 bool MeasureGUI_CheckCompoundOfBlocksDlg::execute( ObjectList& objects )
475 {
476   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
477   GEOM::GEOM_Object_var anObj = anOper->CheckAndImprove( myObj );
478
479   if ( !anObj->_is_nil() )
480     objects.push_back( anObj._retn() );
481
482   return true;
483 }