Salome HOME
Merge from OCC_development_generic_2006
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : MeasureGUI_CheckCompoundOfBlocksDlg.cxx
25 //  Author : VKN
26 //  Module : GEOM
27 //  $Header$
28
29 #include "MeasureGUI_CheckCompoundOfBlocksDlg.h"
30 #include "MeasureGUI_1Sel1TextView_QTD.h"
31
32 #include "utilities.h"
33 #include "SUIT_Session.h"
34 #include "SalomeApp_Tools.h"
35
36 #include <TopTools_IndexedMapOfShape.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_MapOfInteger.hxx>
39 #include "GEOMBase.h"
40 #include "GEOMImpl_Types.hxx"
41
42 #include <qtextedit.h>
43 #include <qlineedit.h>
44 #include <qlayout.h>
45 #include <qpushbutton.h>
46 #include <qradiobutton.h>
47 #include <qbuttongroup.h>
48 // QT Includes
49 #include <qgroupbox.h>
50 #include <qlabel.h>
51 #include <qvaluelist.h>
52
53 //VRV: porting on Qt 3.0.5
54 #if QT_VERSION >= 0x030005
55 #include <qlistbox.h>
56 #endif
57 //VRV: porting on Qt 3.0.5
58
59 #define TEXTEDIT_FONT_FAMILY "Courier"
60 #define TEXTEDIT_FONT_SIZE 11
61
62 //=================================================================================
63 // class    : MeasureGUI_CheckCompoundOfBlocksDlg()
64 // purpose  : Constructs a MeasureGUI_CheckCompoundOfBlocksDlg which is a child of 'parent', with the
65 //            name 'name' and widget flags set to 'f'.
66 //            The dialog will by default be modeless, unless you set 'modal' to
67 //            TRUE to construct a modal dialog.
68 //=================================================================================
69 MeasureGUI_CheckCompoundOfBlocksDlg::MeasureGUI_CheckCompoundOfBlocksDlg( GeometryGUI* GUI, QWidget* parent )
70 : MeasureGUI_Skeleton( GUI, parent, "MeasureGUI_CheckCompoundOfBlocksDlg" )
71 {
72   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
73     "GEOM",tr( "ICON_DLG_CHECK_COMPOUND_OF_BLOCKS" ) ) );
74   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
75     "GEOM",tr( "ICON_SELECT" ) ) );
76
77   setCaption( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
78
79   /***************************************************************/
80
81   GroupConstructors->setTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
82   RadioButton1->setPixmap( image0 );
83
84   myGrp = new MeasureGUI_1Sel1TextView_QTD( this, "myGrp" );
85   myGrp->GroupBox1->setTitle( tr( "GEOM_CHECK_INFOS" ) );
86   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
87   myGrp->TextEdit1->setReadOnly( TRUE );
88   
89   QFont aFont( TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE );
90   aFont.setStyleHint( QFont::TypeWriter, QFont::PreferAntialias );
91   myGrp->TextEdit1->setFont( aFont );
92   myGrp->PushButton1->setPixmap( image1 );
93   myGrp->LineEdit1->setReadOnly( true );
94
95
96   /***************************************************************/
97   QGridLayout* aGBLayout = new QGridLayout( myGrp->GroupBox1->layout() );
98   aGBLayout->setAlignment( Qt::AlignTop );
99   QGridLayout* Layout2 = new QGridLayout( 0, 1, 1, 0, 6, "Layout2"); 
100   myErrorsLbl = new QLabel( tr( "GEOM_CHECK_BLOCKS_COMPOUND_ERRORS" ), myGrp, "Errors" );
101   Layout2->addWidget( myErrorsLbl, 0, 0 );
102
103   myErrorsLBox = new QListBox( myGrp, "ListBlockCompoundErrors" );
104   myErrorsLBox->setMinimumSize( 100, 100 );
105   Layout2->addWidget( myErrorsLBox, 1, 0 );
106
107
108   mySubShapesLbl = new QLabel( tr( "GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES" ), myGrp, "BlockCompoundSubShapes" );
109   Layout2->addWidget( mySubShapesLbl, 0, 1 );
110
111   mySubShapesLBox = new QListBox( myGrp, "ListSubShapes" );
112   mySubShapesLBox->setMinimumSize( 100, 100 );
113   mySubShapesLBox->setSelectionMode(QListBox::Extended);
114   Layout2->addWidget( mySubShapesLBox, 1, 1 );
115   aGBLayout->addLayout( Layout2, 1, 0 );
116
117   Layout1->addWidget( myGrp, 1, 0 );
118   connect( myErrorsLBox, SIGNAL( selectionChanged() ), SLOT( onErrorsListSelectionChanged() ) );
119   connect( mySubShapesLBox, SIGNAL( selectionChanged() ), SLOT( onSubShapesListSelectionChanged() ) );
120   /* Initialisation */
121   Init();
122 }
123
124
125 //=================================================================================
126 // function : ~MeasureGUI_CheckCompoundOfBlocksDlg()
127 // purpose  : Destroys the object and frees any allocated resources
128 //=================================================================================
129 MeasureGUI_CheckCompoundOfBlocksDlg::~MeasureGUI_CheckCompoundOfBlocksDlg()
130 {
131 }
132
133
134 //=================================================================================
135 // function : Init()
136 // purpose  :
137 //=================================================================================
138 void MeasureGUI_CheckCompoundOfBlocksDlg::Init()
139 {
140   activateSelection();
141   mySelBtn = myGrp->PushButton1;
142   mySelEdit = myGrp->LineEdit1;
143   MeasureGUI_Skeleton::Init();
144 }
145
146 //=================================================================================
147 // function : getBCErrors
148 // purpose  :
149 //=================================================================================
150 bool MeasureGUI_CheckCompoundOfBlocksDlg::getBCErrors( bool& theIsCompoundOfBlocks,
151                                                        GEOM::GEOM_IBlocksOperations::BCErrors& theErrors)
152 {
153   if ( myObj->_is_nil() )
154     return false;
155   else
156   {
157     try
158     {
159       GEOM::GEOM_IBlocksOperations::BCErrors_var aErrs;
160       theIsCompoundOfBlocks =
161         GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->CheckCompoundOfBlocks( myObj, aErrs );
162       theErrors = aErrs;
163     }
164     catch( const SALOME::SALOME_Exception& e )
165     {
166       SalomeApp_Tools::QtCatchCorbaException( e );
167       return false;
168     }
169
170     return getOperation()->IsDone();
171   }
172 }
173
174 //=================================================================================
175 // function : processObject
176 // purpose  :
177 //=================================================================================
178 void MeasureGUI_CheckCompoundOfBlocksDlg::processObject()
179 {
180   QString aMsg ("");
181   bool isCompoundOfBlocks;
182   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
183   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) )
184   {
185     myGrp->TextEdit1->setText( aMsg );
186     myErrorsLBox->clear();
187     mySubShapesLBox->clear();
188     erasePreview();
189     return;
190   }
191
192   if (isCompoundOfBlocks) {
193     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_NO_ERRORS" );
194   } else {
195     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_ERRORS" );
196   }
197   myGrp->TextEdit1->setText(aMsg);
198   QStringList aErrList;
199   QString aErrStr( "" );
200   QString aConSfx( " # " );
201   QString aGluedSfx( " # " );
202   int aConNum = 1;
203   int aGluedNum = 1;
204   for ( int i = 0, n = aErrs.length(); i < n; i++ )
205   {
206     aErrStr = "";
207     switch ( aErrs[i].error )
208     {
209       case GEOM::GEOM_IBlocksOperations::NOT_BLOCK :
210         aErrStr = "Not a Block";
211         break;
212       case GEOM::GEOM_IBlocksOperations::EXTRA_EDGE :
213         aErrStr = "Extra Edge";
214         break;
215       case GEOM::GEOM_IBlocksOperations::INVALID_CONNECTION :
216         aErrStr = "Invalid Connection";
217         aErrStr += aConSfx;
218         aErrStr += QString::number(aConNum);
219         aConNum++;
220         break;
221       case GEOM::GEOM_IBlocksOperations::NOT_CONNECTED :
222         aErrStr = "Not Connected";
223         break;
224       case GEOM::GEOM_IBlocksOperations::NOT_GLUED :
225         aErrStr = "Not Glued";
226         aErrStr += aGluedSfx;
227         aErrStr += QString::number(aGluedNum);
228         aGluedNum++;
229         break;
230       default :
231         aErrStr = "";
232         break;
233     }
234     if ( !aErrStr.isEmpty() )
235       aErrList.append(aErrStr);
236   }
237   myErrorsLBox->clear();
238   mySubShapesLBox->clear();
239   myErrorsLBox->insertStringList(aErrList);
240   return;
241 }
242
243 //=================================================================================
244 // function : createOperation
245 // purpose  :
246 //=================================================================================
247 GEOM::GEOM_IOperations_ptr MeasureGUI_CheckCompoundOfBlocksDlg::createOperation()
248 {
249   return getGeomEngine()->GetIBlocksOperations(getStudyId());
250 }
251
252 //=================================================================================
253 // function : onErrorsListSelectionChanged
254 // purpose  :
255 //=================================================================================
256 void MeasureGUI_CheckCompoundOfBlocksDlg::onErrorsListSelectionChanged()
257 {
258   erasePreview();
259   int aCurItem = myErrorsLBox->currentItem();
260   if ( aCurItem < 0 )
261     return;
262   bool isCompoundOfBlocks;
263   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
264   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) )
265   {
266     myGrp->TextEdit1->setText( "" );
267     myErrorsLBox->clear();
268     mySubShapesLBox->clear();
269     return;
270   }
271   
272   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aCurItem];
273   GEOM::ListOfLong aObjLst = aErr.incriminated;
274   TopoDS_Shape aSelShape;
275   TopoDS_Shape aSubShape; 
276   TopTools_IndexedMapOfShape anIndices;
277   QStringList aSubShapeList;
278   QString aSubShapeName("");
279   Standard_CString aTypeString;
280   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) )
281   {
282     TopExp::MapShapes( aSelShape, anIndices);
283     for ( int i = 0, n = aObjLst.length(); i < n; i++ )
284     {
285       aSubShapeName = "";
286       aSubShape = anIndices.FindKey(aObjLst[i]);
287       if ( GEOMBase::GetShapeTypeString( aSubShape, aTypeString ) )     
288         aSubShapeName = QString(aTypeString) + QString("_") + QString::number(aObjLst[i]);      
289       if ( !aSubShapeName.isEmpty() )
290       aSubShapeList.append(aSubShapeName);
291     }
292   }
293   mySubShapesLBox->clear();
294   mySubShapesLBox->insertStringList(aSubShapeList);
295   return;
296 }
297
298 //=================================================================================
299 // function : onSubShapesListSelectionChanged
300 // purpose  :
301 //=================================================================================
302 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
303 {
304   erasePreview();
305   int aErrCurItem = myErrorsLBox->currentItem();
306   if ( aErrCurItem < 0 )
307     return;
308   QValueList<int> aIds;
309   for ( int i = 0, n = mySubShapesLBox->count(); i < n; i++ )
310   {
311     if ( mySubShapesLBox->isSelected( i ) ) 
312       aIds.append( i );
313   }
314   if ( aIds.count() < 1 )
315     return;
316   bool isCompoundOfBlocks;
317   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
318   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) )
319   {
320     myGrp->TextEdit1->setText( "" );
321     myErrorsLBox->clear();
322     mySubShapesLBox->clear();
323     return;
324   }
325   
326   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
327   GEOM::ListOfLong aObjLst = aErr.incriminated;
328   TopoDS_Shape aSelShape;
329   TopoDS_Shape aSubShape; 
330   TopTools_IndexedMapOfShape anIndices;
331   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) )
332   {
333     QString aMess;
334     if ( !isValid( aMess ) )
335     {
336       return;
337     }
338     SALOME_Prs* aPrs = 0;
339     TopExp::MapShapes( aSelShape, anIndices);
340     QValueList<int>::iterator it;
341     for ( it = aIds.begin(); it != aIds.end(); ++it )
342     {
343       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
344       try
345       {
346         getDisplayer()->SetColor( Quantity_NOC_RED );
347         getDisplayer()->SetWidth( 3 );
348         getDisplayer()->SetToActivate( false );
349         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
350         if ( aPrs )
351           displayPreview( aPrs, true );
352       }
353       catch( const SALOME::SALOME_Exception& e )
354       {
355         SalomeApp_Tools::QtCatchCorbaException( e );
356       }
357     }
358   }
359   return;
360 }
361
362 //=================================================================================
363 // function : activateSelection
364 // purpose  : activate selection of faces, shells, and solids
365 //=================================================================================
366 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
367 {
368   TColStd_MapOfInteger aMap;
369   aMap.Add( GEOM_SOLID );
370   aMap.Add( GEOM_COMPOUND );
371   globalSelection( aMap );
372 }