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