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