Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ComputeDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : SMESHGUI_ComputeDlg.cxx
21 // Author : Edward AGAPOV, Open CASCADE S.A.S.
22 // SMESH includes
23 //
24 #include "SMESHGUI_ComputeDlg.h"
25
26 #include "SMESHGUI.h"
27 #include "SMESHGUI_GEOMGenUtils.h"
28 #include "SMESHGUI_MeshUtils.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_MeshInfosBox.h"
31 #include "SMESHGUI_HypothesesUtils.h"
32 #include "SMESHGUI_MeshEditPreview.h"
33 #include "SMESHGUI_MeshOrderOp.h"
34 #include "SMESHGUI_MeshOrderDlg.h"
35
36 #include "SMESH_ActorUtils.h"
37
38 #include <SMDS_SetIterator.hxx>
39 #include <SMDS_Mesh.hxx>
40
41 // SALOME GEOM includes
42 #include <GEOMBase.h>
43 #include <GEOM_Actor.h>
44
45 // SALOME GUI includes
46 #include <LightApp_SelectionMgr.h>
47 #include <LightApp_UpdateFlags.h>
48 #include <SALOME_ListIO.hxx>
49 #include <SVTK_ViewWindow.h>
50 #include <SVTK_ViewModel.h>
51 #include <SalomeApp_Application.h>
52 #include <SUIT_ResourceMgr.h>
53 #include <SUIT_OverrideCursor.h>
54 #include <SUIT_MessageBox.h>
55 #include <SUIT_Desktop.h>
56 #include <QtxComboBox.h>
57
58 // SALOME KERNEL includes
59 #include <SALOMEDS_SObject.hxx>
60 #include <SALOMEDSClient_SObject.hxx>
61
62 // OCCT includes
63 #include <BRep_Tool.hxx>
64 #include <TopExp.hxx>
65 #include <TopExp_Explorer.hxx>
66 #include <TopTools_IndexedMapOfShape.hxx>
67 #include <TopoDS.hxx>
68
69 #include <TopLoc_Location.hxx>
70 #include <Poly_Triangulation.hxx>
71 #include <Bnd_Box.hxx>
72 #include <BRepBndLib.hxx>
73 #include <BRepMesh_IncrementalMesh.hxx>
74
75 #include <Standard_ErrorHandler.hxx>
76
77 // Qt includes
78 #include <QFrame>
79 #include <QPushButton>
80 #include <QLabel>
81 #include <QRadioButton>
82 #include <QTableWidget>
83 #include <QHeaderView>
84 #include <QGridLayout>
85 #include <QHBoxLayout>
86 #include <QVBoxLayout>
87 #include <QButtonGroup>
88 #include <QCloseEvent>
89 #include <QTimerEvent>
90
91 // VTK includes
92 #include <vtkProperty.h>
93
94 // STL includes
95 #include <vector>
96 #include <set>
97
98 #define SPACING 6
99 #define MARGIN  11
100
101 #define COLONIZE(str)   (QString(str).contains(":") > 0 ? QString(str) : QString(str) + " :" )
102
103 /* OBSOLETE
104 static void addSeparator( QWidget* parent )
105 {
106   QGridLayout* l = qobject_cast<QGridLayout*>( parent->layout() );
107   int row  = l->rowCount();
108   int cols = l->columnCount();
109   for ( int i = 0; i < cols; i++ ) {
110     QFrame* hline = new QFrame( parent );
111     hline->setFrameStyle( QFrame::HLine | QFrame::Sunken );
112     l->addWidget( hline, row, i );
113   }
114 }
115 */
116
117 enum TCol {
118   COL_ALGO = 0, COL_SHAPE, COL_ERROR, COL_SHAPEID, COL_PUBLISHED, COL_BAD_MESH, NB_COLUMNS
119 };
120
121 //using namespace SMESH;
122
123 namespace SMESH
124 {
125   //=============================================================================
126   /*!
127    * \brief Allocate some memory at construction and release it at destruction.
128    * Is used to be able to continue working after mesh generation or visualization
129    * break due to lack of memory
130    */
131   //=============================================================================
132
133   struct MemoryReserve
134   {
135     char* myBuf;
136     MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
137     void release() { delete [] myBuf; myBuf = 0; }
138     ~MemoryReserve() { release(); }
139   };
140
141   // =========================================================================================
142   /*!
143    * \brief Class showing shapes without publishing
144    */
145   // =========================================================================================
146
147   class TShapeDisplayer
148   {
149   public:
150     // -----------------------------------------------------------------------
151     TShapeDisplayer(): myViewWindow(0)
152     {
153       myProperty = vtkProperty::New();
154       myProperty->SetRepresentationToWireframe();
155       myProperty->SetColor( 250, 0, 250 );
156       myProperty->SetAmbientColor( 250, 0, 250 );
157       myProperty->SetDiffuseColor( 250, 0, 250 );
158       //myProperty->SetSpecularColor( 250, 0, 250 );
159       myProperty->SetLineWidth( 5 );
160     }
161     // -----------------------------------------------------------------------
162     ~TShapeDisplayer()
163     {
164       DeleteActors();
165       myProperty->Delete();
166     }
167     // -----------------------------------------------------------------------
168     void DeleteActors()
169     {
170       if ( hasViewWindow() ) {
171         TActorIterator actorIt = actorIterator();
172         while ( actorIt.more() )
173           if (VTKViewer_Actor* anActor = actorIt.next()) {
174             myViewWindow->RemoveActor( anActor );
175             //anActor->Delete();
176           }
177       }
178       myIndexToShape.Clear();
179       myActors.clear();
180       myShownActors.clear();
181       myBuiltSubs.clear();
182     }
183     // -----------------------------------------------------------------------
184     void SetVisibility (bool theVisibility)
185     {
186       TActorIterator actorIt = shownIterator();
187       while ( actorIt.more() )
188         if (VTKViewer_Actor* anActor = actorIt.next())
189           anActor->SetVisibility(theVisibility);
190       SMESH::RepaintCurrentView();
191     }
192     // -----------------------------------------------------------------------
193     bool HasReadyActorsFor (int subShapeID, GEOM::GEOM_Object_var aMainShape )
194     {
195       std::string mainEntry;
196       if ( !aMainShape->_is_nil() )
197         mainEntry = aMainShape->GetStudyEntry();
198       return ( myMainEntry == mainEntry &&
199                myBuiltSubs.find( subShapeID ) != myBuiltSubs.end() );
200     }
201     // -----------------------------------------------------------------------
202     void Show( int subShapeID, GEOM::GEOM_Object_var aMainShape, bool only = false)
203     {
204       SVTK_ViewWindow* aViewWindow  = SMESH::GetViewWindow( SMESHGUI::GetSMESHGUI() );
205       std::string mainEntry;
206       if ( !aMainShape->_is_nil() )
207         mainEntry = aMainShape->GetStudyEntry();
208       if ( myMainEntry != mainEntry || aViewWindow != myViewWindow ) { // remove actors
209         DeleteActors();
210         TopoDS_Shape aShape;
211         if ( !aMainShape->_is_nil() && GEOMBase::GetShape(aMainShape, aShape)) {
212           checkTriangulation( aShape );
213           TopExp::MapShapes(aShape, myIndexToShape);
214           myActors.resize( myIndexToShape.Extent(), 0 );
215           myShownActors.reserve( myIndexToShape.Extent() );
216         }
217         myMainEntry  = mainEntry;
218         myViewWindow = aViewWindow;
219       }
220       if ( only ) { // hide shown actors
221         TActorIterator actorIt = shownIterator();
222         while ( actorIt.more() )
223           if (VTKViewer_Actor* anActor = actorIt.next())
224             anActor->SetVisibility(false);
225         myShownActors.clear();
226       }
227       // find actors to show
228       TopoDS_Shape aShape = myIndexToShape( subShapeID );
229       if ( !aShape.IsNull() ) {
230         TopAbs_ShapeEnum type( aShape.ShapeType() >= TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE );
231         for ( TopExp_Explorer exp( aShape, type ); exp.More(); exp.Next() ) {
232           //checkTriangulation( exp.Current() );
233           if ( GEOM_Actor* anActor = getActor( exp.Current() ))
234             myShownActors.push_back( anActor );
235         }
236         if ( type == TopAbs_FACE ) {
237           for ( TopExp_Explorer exp( aShape, TopAbs_EDGE ); exp.More(); exp.Next() ) {
238             const TopoDS_Edge & edge = TopoDS::Edge( exp.Current() );
239             if ( !BRep_Tool::Degenerated( edge ))
240               if ( GEOM_Actor* anActor = getActor( exp.Current() ))
241                 myShownActors.push_back( anActor );
242           }
243         }
244       }
245       myBuiltSubs.insert( subShapeID );
246       SetVisibility(true);
247     }
248     // -----------------------------------------------------------------------
249
250   private:
251
252     typedef std::vector<GEOM_Actor*> TActorVec;
253     TActorVec                  myActors;
254     TActorVec                  myShownActors;
255     TopTools_IndexedMapOfShape myIndexToShape;
256     std::string                myMainEntry;
257     SVTK_ViewWindow*           myViewWindow;
258     vtkProperty*               myProperty;
259     std::set<int>              myBuiltSubs;
260
261     // -----------------------------------------------------------------------
262     typedef SMDS_SetIterator< GEOM_Actor*, TActorVec::const_iterator> TActorIterator;
263     TActorIterator actorIterator() {
264       return TActorIterator( myActors.begin(), myActors.end() );
265     }
266     TActorIterator shownIterator() {
267       return TActorIterator( myShownActors.begin(), myShownActors.end() );
268     }
269     // -----------------------------------------------------------------------
270     GEOM_Actor* getActor(const TopoDS_Shape& shape)
271     {
272       int index = myIndexToShape.FindIndex( shape ) - 1;
273       if ( index < 0 || index >= myActors.size() )
274         return 0;
275       GEOM_Actor* & actor = myActors[ index ];
276       if ( !actor ) {
277         actor = GEOM_Actor::New();
278         if ( actor ) {
279           actor->SetShape(shape,0,0);
280           actor->SetProperty(myProperty);
281           actor->SetShadingProperty(myProperty);
282           actor->SetWireframeProperty(myProperty);
283           actor->SetPreviewProperty(myProperty);
284           actor->PickableOff();
285           //         if ( shape.ShapeType() == TopAbs_EDGE )
286           //           actor->SubShapeOn();
287           myViewWindow->AddActor( actor );
288         }
289       }
290       return actor;
291     }
292     // -----------------------------------------------------------------------
293     void checkTriangulation(const TopoDS_Shape& shape)
294     {
295       TopLoc_Location aLoc;
296       Standard_Boolean alreadymesh = Standard_True;
297       TopExp_Explorer ex(shape, TopAbs_FACE);
298       if ( ex.More() )
299         for ( ; ex.More(); ex.Next()) {
300           const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
301           Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
302           if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
303         }
304       else
305         for (ex.Init(shape, TopAbs_EDGE); ex.More(); ex.Next()) {
306           const TopoDS_Edge& edge = TopoDS::Edge(ex.Current());
307           Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(edge, aLoc);
308           if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
309         }
310       if (alreadymesh) return;
311       // Compute default deflection
312       Bnd_Box B;
313       BRepBndLib::Add(shape, B);
314       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
315       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
316       double deflection = Max( aXmax-aXmin, Max ( aYmax-aYmin, aZmax-aZmin)) * 0.01 *4;
317       BRepMesh_IncrementalMesh MESH(shape,deflection);
318     }
319     // -----------------------------------------------------------------------
320     bool hasViewWindow() const
321     {
322       if ( !myViewWindow ) return false;
323
324       if ( SalomeApp_Application* anApp = SMESHGUI::GetSMESHGUI()->getApp() )
325         return FindVtkViewWindow( anApp->getViewManager(SVTK_Viewer::Type(), false ),
326                                   myViewWindow );
327       return false;
328     }
329   };
330
331   // =========================================================================================
332   /*!
333    * \brief Return text describing an error
334    */
335 #define CASE2TEXT(enum) case SMESH::enum: text = QObject::tr( #enum ); break;
336   QString errorText(int errCode, const char* comment)
337   {
338     QString text;
339     switch ( errCode ) {
340       CASE2TEXT( COMPERR_OK            );
341       CASE2TEXT( COMPERR_BAD_INPUT_MESH);
342       CASE2TEXT( COMPERR_STD_EXCEPTION );
343       CASE2TEXT( COMPERR_OCC_EXCEPTION );
344     case SMESH::COMPERR_SLM_EXCEPTION: break; // avoid double "Salome exception"
345       CASE2TEXT( COMPERR_EXCEPTION     );
346       CASE2TEXT( COMPERR_MEMORY_PB     );
347       CASE2TEXT( COMPERR_BAD_SHAPE     );
348     case SMESH::COMPERR_ALGO_FAILED:
349       if ( strlen(comment) == 0 )
350         text = QObject::tr("COMPERR_ALGO_FAILED");
351       break;
352     default:
353       text = QString("#%1").arg( -errCode );
354     }
355     if ( text.length() > 0 ) text += ". ";
356     return text + comment;
357   }
358   // -----------------------------------------------------------------------
359   /*!
360    * \brief Return SO of a subshape
361    */
362   _PTR(SObject) getSubShapeSO( int subShapeID, GEOM::GEOM_Object_var aMainShape)
363   {
364     _PTR(SObject) so = SMESH::FindSObject(aMainShape);
365     if ( subShapeID == 1 || !so )
366       return so;
367     _PTR(ChildIterator) it;
368     if (_PTR(Study) study = SMESH::GetActiveStudyDocument())
369       it =  study->NewChildIterator(so);
370     _PTR(SObject) subSO;
371     if ( it ) {
372       for ( it->InitEx(true); !subSO && it->More(); it->Next() ) {
373         GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( it->Value() );
374         if ( !geom->_is_nil() ) {
375           GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
376           if ( list->length() == 1 && list[0] == subShapeID )
377             subSO = it->Value();
378         }
379       }
380     }
381     return subSO;
382   }
383   // -----------------------------------------------------------------------
384   /*!
385    * \brief Return subshape by ID
386    */
387   GEOM::GEOM_Object_ptr getSubShape( int subShapeID, GEOM::GEOM_Object_var aMainShape)
388   {
389     GEOM::GEOM_Object_var aSubShape;
390     if ( subShapeID == 1 )
391       aSubShape = aMainShape;
392     else if ( _PTR(SObject) so = getSubShapeSO( subShapeID, aMainShape ))
393       aSubShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>( so );
394     else
395       aSubShape = SMESH::GetSubShape( aMainShape, subShapeID );
396     return aSubShape._retn();
397   }
398   // -----------------------------------------------------------------------
399   /*!
400    * \brief Return shape type name
401    */
402 #define CASE2NAME(enum) case GEOM::enum: name = QObject::tr( "GEOM_" #enum ); break;
403   QString shapeTypeName(GEOM::GEOM_Object_var aShape, const char* dflt = "" )
404   {
405     QString name = dflt;
406     if ( !aShape->_is_nil() ) {
407       switch ( aShape->GetShapeType() ) {
408       CASE2NAME( VERTEX    );
409       CASE2NAME( EDGE      );
410       CASE2NAME( WIRE      );
411       CASE2NAME( FACE      );
412       CASE2NAME( SHELL     );
413       CASE2NAME( SOLID     );
414       CASE2NAME( COMPSOLID );
415       CASE2NAME( COMPOUND  );
416       default:;
417       }
418     }
419     return name;
420   }
421   // -----------------------------------------------------------------------
422   /*!
423    * \brief Return text describing a subshape
424    */
425   QString shapeText(int subShapeID, GEOM::GEOM_Object_var aMainShape )
426   {
427     QString text;
428     if ( _PTR(SObject) aSO = getSubShapeSO( subShapeID, aMainShape ))
429       text = aSO->GetName().c_str();
430     else {
431       text = QString("#%1").arg( subShapeID );
432       QString typeName = shapeTypeName( getSubShape( subShapeID, aMainShape ));
433       if ( typeName.length() )
434         text += QString(" (%1)").arg(typeName);
435     }
436     return text;
437   }
438   // -----------------------------------------------------------------------
439   /*!
440    * \brief Return a list of selected rows
441    */
442   int getSelectedRows(QTableWidget* table, QList<int>& rows)
443   {
444     rows.clear();
445     QList<QTableWidgetSelectionRange> selRanges = table->selectedRanges();
446     QTableWidgetSelectionRange range;
447     foreach( range, selRanges )
448     {
449       for ( int row = range.topRow(); row <= range.bottomRow(); ++row )
450         rows.append( row );
451     }
452     if ( rows.isEmpty() && table->currentRow() > -1 )
453       rows.append( table->currentRow() );
454
455     return rows.count();
456   }
457
458 } // namespace SMESH
459
460
461 // =========================================================================================
462 /*!
463  * \brief Dialog to compute a mesh and show computation errors
464  */
465 //=======================================================================
466
467 SMESHGUI_ComputeDlg::SMESHGUI_ComputeDlg( QWidget* parent, bool ForEval )
468  : SMESHGUI_Dialog( parent, false, true, Close/* | Help*/ )
469 {
470   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
471   aDlgLay->setMargin( 0 );
472   aDlgLay->setSpacing( SPACING );
473
474   QFrame* aMainFrame = createMainFrame(mainFrame(),ForEval);
475
476   aDlgLay->addWidget(aMainFrame);
477
478   aDlgLay->setStretchFactor(aMainFrame, 1);
479 }
480
481 // =========================================================================================
482 /*!
483  * \brief Destructor
484  */
485 //=======================================================================
486
487 SMESHGUI_ComputeDlg::~SMESHGUI_ComputeDlg()
488 {
489 }
490
491 //=======================================================================
492 // function : createMainFrame()
493 // purpose  : Create frame containing dialog's fields
494 //=======================================================================
495
496 QFrame* SMESHGUI_ComputeDlg::createMainFrame (QWidget* theParent, bool ForEval)
497 {
498   QFrame* aFrame = new QFrame(theParent);
499
500   SUIT_ResourceMgr* rm = resourceMgr();
501   QPixmap iconCompute (rm->loadPixmap("SMESH", tr("ICON_COMPUTE")));
502
503   // constructor
504
505   QGroupBox* aPixGrp;
506   if(ForEval) {
507     aPixGrp = new QGroupBox(tr("EVAL_DLG"), aFrame);
508   }
509   else {
510     aPixGrp = new QGroupBox(tr("CONSTRUCTOR"), aFrame);
511   }
512   QButtonGroup* aBtnGrp = new QButtonGroup(this);
513   QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
514   aPixGrpLayout->setMargin(MARGIN); aPixGrpLayout->setSpacing(SPACING);
515
516   QRadioButton* aRBut = new QRadioButton(aPixGrp);
517   aRBut->setIcon(iconCompute);
518   aRBut->setChecked(true);
519   aPixGrpLayout->addWidget(aRBut);
520   aBtnGrp->addButton(aRBut, 0);
521
522   // Mesh name
523
524   QGroupBox* nameBox = new QGroupBox(tr("SMESH_MESHINFO_NAME"), aFrame );
525   QHBoxLayout* nameBoxLayout = new QHBoxLayout(nameBox);
526   nameBoxLayout->setMargin(MARGIN); nameBoxLayout->setSpacing(SPACING);
527   myMeshName = new QLabel(nameBox);
528   nameBoxLayout->addWidget(myMeshName);
529
530   // Mesh Info
531
532   myBriefInfo = new SMESHGUI_MeshInfosBox(false, aFrame);
533   myFullInfo  = new SMESHGUI_MeshInfosBox(true,  aFrame);
534
535   // Computation errors
536
537   myCompErrorGroup = new QGroupBox(tr("ERRORS"), aFrame);
538   myTable      = new QTableWidget( 1, NB_COLUMNS, myCompErrorGroup);
539   myShowBtn    = new QPushButton(tr("SHOW_SHAPE"), myCompErrorGroup);
540   myPublishBtn = new QPushButton(tr("PUBLISH_SHAPE"), myCompErrorGroup);
541   myBadMeshBtn = new QPushButton(tr("SHOW_BAD_MESH"), myCompErrorGroup);
542
543   //myTable->setReadOnly( true ); // VSR: check
544   myTable->setEditTriggers( QAbstractItemView::NoEditTriggers );
545   myTable->hideColumn( COL_PUBLISHED );
546   myTable->hideColumn( COL_SHAPEID );
547   myTable->hideColumn( COL_BAD_MESH );
548   myTable->horizontalHeader()->setResizeMode( COL_ERROR, QHeaderView::Interactive );
549
550   QStringList headers;
551   headers << tr( "COL_ALGO_HEADER" );
552   headers << tr( "COL_SHAPE_HEADER" );
553   headers << tr( "COL_ERROR_HEADER" );
554   headers << tr( "COL_SHAPEID_HEADER" );
555   headers << tr( "COL_PUBLISHED_HEADER" );
556
557   myTable->setHorizontalHeaderLabels( headers );
558
559   // layouting
560   QGridLayout* grpLayout = new QGridLayout(myCompErrorGroup);
561   grpLayout->setSpacing(SPACING);
562   grpLayout->setMargin(MARGIN);
563   grpLayout->addWidget( myTable,      0, 0, 4, 1 );
564   grpLayout->addWidget( myShowBtn,    0, 1 );
565   grpLayout->addWidget( myPublishBtn, 1, 1 );
566   grpLayout->addWidget( myBadMeshBtn, 2, 1 );
567   grpLayout->setRowStretch( 3, 1 );
568
569   // Hypothesis definition errors
570
571   myHypErrorGroup = new QGroupBox(tr("SMESH_WRN_MISSING_PARAMETERS"), aFrame);
572   QHBoxLayout* myHypErrorGroupLayout = new QHBoxLayout(myHypErrorGroup);
573   myHypErrorGroupLayout->setMargin(MARGIN);
574   myHypErrorGroupLayout->setSpacing(SPACING);
575   myHypErrorLabel = new QLabel(myHypErrorGroup);
576   myHypErrorGroupLayout->addWidget(myHypErrorLabel);
577
578   // Memory Lack Label
579
580   myMemoryLackGroup = new QGroupBox(tr("ERRORS"), aFrame);
581   QVBoxLayout* myMemoryLackGroupLayout = new QVBoxLayout(myMemoryLackGroup);
582   myMemoryLackGroupLayout->setMargin(MARGIN);
583   myMemoryLackGroupLayout->setSpacing(SPACING);
584   QLabel* memLackLabel = new QLabel(tr("MEMORY_LACK"), myMemoryLackGroup);
585   QFont bold = memLackLabel->font(); bold.setBold(true);
586   memLackLabel->setFont( bold );
587   memLackLabel->setMinimumWidth(300);
588   myMemoryLackGroupLayout->addWidget(memLackLabel);
589
590   // add all widgets to aFrame
591   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
592   aLay->setMargin( 0 );
593   aLay->setSpacing( 0 );
594   aLay->addWidget( aPixGrp );
595   aLay->addWidget( nameBox );
596   aLay->addWidget( myBriefInfo );
597   aLay->addWidget( myFullInfo );
598   aLay->addWidget( myHypErrorGroup );
599   aLay->addWidget( myCompErrorGroup );
600   aLay->addWidget( myMemoryLackGroup );
601   aLay->setStretchFactor( myCompErrorGroup, 1 );
602
603   ((QPushButton*) button( OK ))->setDefault( true );
604
605   return aFrame;
606 }
607
608 //================================================================================
609 /*!
610  * \brief Constructor
611 */
612 //================================================================================
613
614 SMESHGUI_BaseComputeOp::SMESHGUI_BaseComputeOp()
615   : SMESHGUI_Operation(), myCompDlg( 0 )
616 {
617   myTShapeDisplayer = new SMESH::TShapeDisplayer();
618   myBadMeshDisplayer = 0;
619
620   //myHelpFileName = "/files/about_meshes.htm"; // V3
621   myHelpFileName = "about_meshes_page.html"; // V4
622 }
623
624 SMESH::SMESH_Mesh_ptr SMESHGUI_BaseComputeOp::getMesh()
625 {
626   LightApp_SelectionMgr* Sel = selectionMgr();
627   SALOME_ListIO selected; Sel->selectedObjects( selected );
628   Handle(SALOME_InteractiveObject) anIO = selected.First();
629   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(anIO);
630   return myMesh->_is_nil() ? aMesh._retn() : SMESH::SMESH_Mesh::_duplicate( myMesh );
631 }
632
633 //================================================================================
634 /*!
635  * \brief Start operation
636  * \purpose Init dialog fields, connect signals and slots, show dialog
637  */
638 //================================================================================
639
640 void SMESHGUI_BaseComputeOp::startOperation()
641 {
642   // create compute dialog if not created before
643   computeDlg();
644
645   myMesh      = SMESH::SMESH_Mesh::_nil();
646   myMainShape = GEOM::GEOM_Object::_nil();
647
648   // check selection
649   LightApp_SelectionMgr *Sel = selectionMgr();
650   SALOME_ListIO selected; Sel->selectedObjects( selected );
651
652   int nbSel = selected.Extent();
653   if (nbSel != 1) {
654     SUIT_MessageBox::warning(desktop(),
655                              tr("SMESH_WRN_WARNING"),
656                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
657     onCancel();
658     return;
659   }
660
661   myIObject = selected.First();
662   myMesh = SMESH::GetMeshByIO(myIObject);
663   if (myMesh->_is_nil()) {
664     SUIT_MessageBox::warning(desktop(),
665                              tr("SMESH_WRN_WARNING"),
666                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
667     onCancel();
668     return;
669   }
670   myMainShape = myMesh->GetShapeToMesh();
671
672   SMESHGUI_Operation::startOperation();
673 }
674
675 //================================================================================
676 //================================================================================
677
678 SMESHGUI_ComputeDlg_QThread::SMESHGUI_ComputeDlg_QThread(SMESH::SMESH_Gen_var gen,
679                                                          SMESH::SMESH_Mesh_var mesh,
680                                                          GEOM::GEOM_Object_var mainShape)
681 {
682   myResult = false;
683   myGen = gen;
684   myMesh = mesh;
685   myMainShape = mainShape;
686 }
687
688 void SMESHGUI_ComputeDlg_QThread::run()
689 {
690   myResult = myGen->Compute(myMesh, myMainShape);
691 }
692
693 bool SMESHGUI_ComputeDlg_QThread::result()
694 {
695   return myResult;
696 }
697
698 void SMESHGUI_ComputeDlg_QThread::cancel()
699 {
700   myGen->CancelCompute(myMesh, myMainShape);
701 }
702
703 //================================================================================
704 //================================================================================
705
706 SMESHGUI_ComputeDlg_QThreadQDialog::SMESHGUI_ComputeDlg_QThreadQDialog(QWidget *parent,
707                                                                        SMESH::SMESH_Gen_var gen,
708                                                                        SMESH::SMESH_Mesh_var mesh,
709                                                                        GEOM::GEOM_Object_var mainShape)
710   : QDialog(parent),
711     qthread(gen, mesh, mainShape)
712 {
713   // --
714   setWindowTitle(tr("Compute"));
715   cancelButton = new QPushButton(tr("Cancel"));
716   cancelButton->setDefault(true);
717   connect(cancelButton, SIGNAL(clicked()), this, SLOT(onCancel()));
718   QHBoxLayout *layout = new QHBoxLayout;
719   layout->addWidget(cancelButton);
720   setLayout(layout);
721   resize(200, 50);
722   // --
723   startTimer(30); // 30 millisecs
724   qthread.start();
725 }
726
727 bool SMESHGUI_ComputeDlg_QThreadQDialog::result()
728 {
729   return qthread.result();
730 }
731
732 void SMESHGUI_ComputeDlg_QThreadQDialog::onCancel()
733 {
734   qthread.cancel();
735 }  
736
737 void SMESHGUI_ComputeDlg_QThreadQDialog::timerEvent(QTimerEvent *event)
738 {
739   if(qthread.isFinished())
740     {
741       close();
742     }
743   event->accept();
744 }
745
746 void SMESHGUI_ComputeDlg_QThreadQDialog::closeEvent(QCloseEvent *event)
747 {
748   if(qthread.isRunning())
749     {
750       event->ignore();
751       return;
752     }
753   event->accept();
754 }
755
756 //================================================================================
757 /*!
758  * \brief computeMesh()
759 */
760 //================================================================================
761
762 void SMESHGUI_BaseComputeOp::computeMesh()
763 {
764   // COMPUTE MESH
765
766   SMESH::MemoryReserve aMemoryReserve;
767
768   SMESH::compute_error_array_var aCompErrors;
769   QString                        aHypErrors;
770
771   bool computeFailed = true, memoryLack = false;
772
773   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
774   if ( !aMeshSObj ) // IPAL 21340
775     return;
776   bool hasShape = myMesh->HasShapeToMesh();
777   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
778   if ( shapeOK )
779   {
780     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
781     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
782     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
783     if ( errors->length() > 0 ) {
784       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
785     }
786     if ( myMesh->HasModificationsToDiscard() && // issue 0020693
787          SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
788                                     tr( "FULL_RECOMPUTE_QUESTION" ),
789                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 1, 0 ) == 0 )
790       myMesh->Clear();
791     SUIT_OverrideCursor aWaitCursor;
792     try {
793 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
794       OCC_CATCH_SIGNALS;
795 #endif
796       //SMESH::UpdateNulData(myIObject, true);
797       bool res;
798 #ifdef WITH_SMESH_CANCEL_COMPUTE
799       SMESHGUI_ComputeDlg_QThreadQDialog qthreaddialog(desktop(), gen, myMesh, myMainShape);
800       qthreaddialog.exec();
801       res = qthreaddialog.result();
802 #else
803       res = gen->Compute(myMesh, myMainShape);
804 #endif
805       if (res)
806         computeFailed = false;
807     }
808     catch(const SALOME::SALOME_Exception & S_ex){
809       memoryLack = true;
810     }
811     try {
812 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
813       OCC_CATCH_SIGNALS;
814 #endif
815       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
816       // check if there are memory problems
817       for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
818         memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
819     }
820     catch(const SALOME::SALOME_Exception & S_ex){
821       memoryLack = true;
822     }
823
824     if ( !memoryLack && !SMDS_Mesh::CheckMemory(true) ) { // has memory to show dialog boxes?
825       memoryLack = true;
826     }
827
828     // NPAL16631: if ( !memoryLack )
829     {
830       SMESH::ModifiedMesh(aMeshSObj, !computeFailed, myMesh->NbNodes() == 0);
831       update( UF_ObjBrowser | UF_Model );
832
833       // SHOW MESH
834       // NPAL16631: if ( getSMESHGUI()->automaticUpdate() )
835       SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
836       long newSize = myMesh->NbElements();
837       bool limitExceeded;
838       if ( !memoryLack )
839       {
840         if ( getSMESHGUI()->automaticUpdate( newSize, &limitExceeded ) )
841         {
842           try {
843 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
844             OCC_CATCH_SIGNALS;
845 #endif
846             SMESH::Update(myIObject, true);
847           }
848           catch (...) {
849 #ifdef _DEBUG_
850             MESSAGE ( "Exception thrown during mesh visualization" );
851 #endif
852             if ( SMDS_Mesh::CheckMemory(true) ) { // has memory to show warning?
853               SMESH::OnVisuException();
854             }
855             else {
856               memoryLack = true;
857             }
858           }
859         }
860         else if ( limitExceeded )
861         {
862           long limitSize = resMgr->integerValue( "SMESH", "update_limit", 500000 );
863           SUIT_MessageBox::warning( desktop(),
864                                     tr( "SMESH_WRN_WARNING" ),
865                                     tr( "SMESH_WRN_SIZE_LIMIT_EXCEEDED" ).arg( newSize ).arg( limitSize ) );
866         }
867       }
868       LightApp_SelectionMgr *Sel = selectionMgr();
869       if ( Sel )
870       {
871         SALOME_ListIO selected;
872         selected.Append( myIObject );
873         Sel->setSelectedObjects( selected );
874       }
875     }
876   }
877
878   if ( memoryLack )
879     aMemoryReserve.release();
880
881   myCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
882
883   // SHOW ERRORS
884   
885   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
886   bool noHypoError = ( aHypErrors.isEmpty() );
887
888   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
889   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
890
891   bool isShowResultDlg = true;
892   switch( aNotifyMode ) {
893   case 0: // show the mesh computation result dialog NEVER
894     isShowResultDlg = false;
895     commit();
896     break;
897   case 1: // show the mesh computation result dialog if there are some errors
898     if ( memoryLack || !noCompError || !noHypoError )
899       isShowResultDlg = true;
900     else
901     {
902       isShowResultDlg = false;
903       commit();
904     }
905     break;
906   default: // show the result dialog after each mesh computation
907     isShowResultDlg = true;
908   }
909
910   // SHOW RESULTS
911   if ( isShowResultDlg )
912     showComputeResult( memoryLack, noCompError,aCompErrors, noHypoError, aHypErrors );
913 }
914
915 void SMESHGUI_BaseComputeOp::showComputeResult( const bool theMemoryLack,
916                                                 const bool theNoCompError,
917                                                 SMESH::compute_error_array_var& theCompErrors,
918                                                 const bool theNoHypoError,
919                                                 const QString& theHypErrors )
920 {
921   bool hasShape = myMesh->HasShapeToMesh();
922   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
923   aCompDlg->myMemoryLackGroup->hide();
924
925   if ( theMemoryLack )
926   {
927     aCompDlg->myMemoryLackGroup->show();
928     aCompDlg->myFullInfo->hide();
929     aCompDlg->myBriefInfo->hide();
930     aCompDlg->myHypErrorGroup->hide();
931     aCompDlg->myCompErrorGroup->hide();
932   }
933   else if ( theNoCompError && theNoHypoError )
934   {
935     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
936     aCompDlg->myFullInfo->SetMeshInfo( aRes );
937     aCompDlg->myFullInfo->show();
938     aCompDlg->myBriefInfo->hide();
939     aCompDlg->myHypErrorGroup->hide();
940     aCompDlg->myCompErrorGroup->hide();
941   }
942   else
943   {
944     QTableWidget* tbl = aCompDlg->myTable;
945     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
946     aCompDlg->myBriefInfo->SetMeshInfo( aRes );
947     aCompDlg->myBriefInfo->show();
948     aCompDlg->myFullInfo->hide();
949
950     if ( theNoHypoError ) {
951       aCompDlg->myHypErrorGroup->hide();
952     }
953     else {
954       aCompDlg->myHypErrorGroup->show();
955       aCompDlg->myHypErrorLabel->setText( theHypErrors );
956     }
957
958     if ( theNoCompError ) {
959       aCompDlg->myCompErrorGroup->hide();
960     }
961     else {
962       aCompDlg->myCompErrorGroup->show();
963
964       if ( !hasShape ) {
965         aCompDlg->myPublishBtn->hide();
966         aCompDlg->myShowBtn->hide();
967       }
968       else {
969         aCompDlg->myPublishBtn->show();
970         aCompDlg->myShowBtn->show();
971       }
972
973       // fill table of errors
974       tbl->setRowCount( theCompErrors->length() );
975       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
976       else             tbl->showColumn( COL_SHAPE );
977       tbl->setColumnWidth( COL_ERROR, 200 );
978
979       bool hasBadMesh = false;
980       for ( int row = 0; row < theCompErrors->length(); ++row )
981       {
982         SMESH::ComputeError & err = theCompErrors[ row ];
983
984         QString text = err.algoName.in();
985         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
986         else tbl->item( row, COL_ALGO )->setText( text );
987
988         text = SMESH::errorText( err.code, err.comment.in() );
989         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
990         else tbl->item( row, COL_ERROR )->setText( text );
991
992         text = QString("%1").arg( err.subShapeID );
993         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
994         else tbl->item( row, COL_SHAPEID )->setText( text );
995
996         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
997         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
998         else tbl->item( row, COL_SHAPE )->setText( text );
999
1000         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
1001         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
1002         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
1003
1004         text = err.hasBadMesh ? "hasBadMesh" : "";
1005         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
1006         else tbl->item( row, COL_BAD_MESH )->setText( text );
1007         if ( err.hasBadMesh ) hasBadMesh = true;
1008
1009         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
1010         tbl->resizeRowToContents( row );
1011       }
1012       tbl->resizeColumnToContents( COL_ALGO );
1013       tbl->resizeColumnToContents( COL_SHAPE );
1014
1015       if ( hasBadMesh )
1016         aCompDlg->myBadMeshBtn->show();
1017       else
1018         aCompDlg->myBadMeshBtn->hide();
1019
1020       tbl->setCurrentCell(0,0);
1021       currentCellChanged(); // to update buttons
1022     }
1023   }
1024   // show dialog and wait, becase Compute can be invoked from Preview operation
1025   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
1026   aCompDlg->show();
1027 }
1028
1029 //================================================================================
1030 /*!
1031  * \brief Stops operation
1032  */
1033 //================================================================================
1034
1035 void SMESHGUI_BaseComputeOp::stopOperation()
1036 {
1037   SMESHGUI_Operation::stopOperation();
1038   if ( myTShapeDisplayer )
1039     myTShapeDisplayer->SetVisibility( false );
1040   if ( myBadMeshDisplayer ) {
1041     myBadMeshDisplayer->SetVisibility( false );
1042     // delete it in order not to have problems at its destruction when the viewer
1043     // where it worked is dead due to e.g. study closing
1044     delete myBadMeshDisplayer;
1045     myBadMeshDisplayer = 0;
1046   }
1047   myIObject.Nullify();
1048 }
1049
1050 //================================================================================
1051 /*!
1052  * \brief publish selected subshape
1053  */
1054 //================================================================================
1055
1056 void SMESHGUI_BaseComputeOp::onPublishShape()
1057 {
1058   GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1059   SALOMEDS::Study_var study = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
1060
1061   QList<int> rows;
1062   SMESH::getSelectedRows( table(), rows );
1063   int row;
1064   foreach ( row, rows )
1065   {
1066     int curSub = table()->item(row, COL_SHAPEID)->text().toInt();
1067     GEOM::GEOM_Object_var shape = SMESH::getSubShape( curSub, myMainShape );
1068     if ( !shape->_is_nil() && ! SMESH::getSubShapeSO( curSub, myMainShape ))
1069     {
1070       if ( !SMESH::getSubShapeSO( 1, myMainShape )) // the main shape not published
1071       {
1072         QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( myMainShape, "MAIN_SHAPE" ));
1073         SALOMEDS::SObject_var so =
1074           geomGen->AddInStudy( study, myMainShape, name.toLatin1().data(), GEOM::GEOM_Object::_nil());
1075         // look for myMainShape in the table
1076         for ( int r = 0, nr = table()->rowCount(); r < nr; ++r ) {
1077           if ( table()->item( r, COL_SHAPEID )->text() == "1" ) {
1078             if ( so->_is_nil() ) {
1079               table()->item( r, COL_SHAPE )->setText( so->GetName() );
1080               table()->item( r, COL_PUBLISHED )->setText( so->GetID() );
1081             }
1082             break;
1083           }
1084         }
1085         if ( curSub == 1 ) continue;
1086       }
1087       QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( shape, "ERROR_SHAPE" ));
1088       SALOMEDS::SObject_var so = geomGen->AddInStudy( study, shape, name.toLatin1().data(), myMainShape);
1089       if ( !so->_is_nil() ) {
1090         table()->item( row, COL_SHAPE )->setText( so->GetName() );
1091         table()->item( row, COL_PUBLISHED )->setText( so->GetID() );
1092       }
1093     }
1094   }
1095   getSMESHGUI()->getApp()->updateObjectBrowser();
1096   currentCellChanged(); // to update buttons
1097 }
1098
1099 //================================================================================
1100 /*!
1101  * \brief show mesh elements preventing computation of a submesh of current row
1102  */
1103 //================================================================================
1104
1105 void SMESHGUI_BaseComputeOp::onShowBadMesh()
1106 {
1107   myTShapeDisplayer->SetVisibility( false );
1108   QList<int> rows;
1109   if ( SMESH::getSelectedRows( table(), rows ) == 1 ) {
1110     bool hasBadMesh = ( !table()->item(rows.front(), COL_BAD_MESH)->text().isEmpty() );
1111     if ( hasBadMesh ) {
1112       int curSub = table()->item(rows.front(), COL_SHAPEID)->text().toInt();
1113       SMESHGUI* gui = getSMESHGUI();
1114       SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1115       SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1116       if ( myBadMeshDisplayer ) delete myBadMeshDisplayer;
1117       myBadMeshDisplayer = new SMESHGUI_MeshEditPreview( view );
1118       SMESH::MeshPreviewStruct_var aMeshData = gen->GetBadInputElements(myMesh,curSub);
1119       vtkFloatingPointType aPointSize = SMESH::GetFloat("SMESH:node_size",3);
1120       vtkFloatingPointType aLineWidth = SMESH::GetFloat("SMESH:element_width",1);
1121       // delete property !!!!!!!!!!
1122       vtkProperty* prop = vtkProperty::New();
1123       prop->SetLineWidth( aLineWidth * 3 );
1124       prop->SetPointSize( aPointSize * 3 );
1125       prop->SetColor( 250, 0, 250 );
1126       myBadMeshDisplayer->GetActor()->SetProperty( prop );
1127       myBadMeshDisplayer->SetData( aMeshData._retn() );
1128     }
1129   }
1130 }
1131
1132 //================================================================================
1133 /*!
1134  * \brief SLOT called when a selected cell in table() changed
1135  */
1136 //================================================================================
1137
1138 void SMESHGUI_BaseComputeOp::currentCellChanged()
1139 {
1140   myTShapeDisplayer->SetVisibility( false );
1141   if ( myBadMeshDisplayer )
1142     myBadMeshDisplayer->SetVisibility( false );
1143
1144   bool publishEnable = 0, showEnable = 0, showOnly = 1, hasBadMesh = 0;
1145   QList<int> rows;
1146   int nbSelected = SMESH::getSelectedRows( table(), rows );
1147   int row;
1148   foreach ( row, rows )
1149   {
1150     bool hasData     = ( !table()->item( row, COL_SHAPE )->text().isEmpty() );
1151     bool isPublished = ( !table()->item( row, COL_PUBLISHED )->text().isEmpty() );
1152     if ( hasData && !isPublished )
1153       publishEnable = true;
1154
1155     int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1156     bool prsReady = myTShapeDisplayer->HasReadyActorsFor( curSub, myMainShape );
1157     if ( prsReady ) {
1158       myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1159       showOnly = false;
1160     }
1161     else {
1162       showEnable = true;
1163     }
1164
1165     if ( !table()->item(row, COL_BAD_MESH)->text().isEmpty() )
1166       hasBadMesh = true;
1167   }
1168   myCompDlg->myPublishBtn->setEnabled( publishEnable );
1169   myCompDlg->myShowBtn   ->setEnabled( showEnable );
1170   myCompDlg->myBadMeshBtn->setEnabled( hasBadMesh && ( nbSelected == 1 ));
1171 }
1172
1173 //================================================================================
1174 /*!
1175  * \brief update preview
1176  */
1177 //================================================================================
1178
1179 void SMESHGUI_BaseComputeOp::onPreviewShape()
1180 {
1181   if ( myTShapeDisplayer )
1182   {
1183     SUIT_OverrideCursor aWaitCursor;
1184     QList<int> rows;
1185     SMESH::getSelectedRows( table(), rows );
1186
1187     bool showOnly = true;
1188     int row;
1189     foreach ( row, rows )
1190     {
1191       int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1192       if ( curSub > 0 ) {
1193         myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1194         showOnly = false;
1195       }
1196     }
1197     currentCellChanged(); // to update buttons
1198   }
1199 }
1200
1201 //================================================================================
1202 /*!
1203  * \brief Destructor
1204  */
1205 //================================================================================
1206
1207 SMESHGUI_BaseComputeOp::~SMESHGUI_BaseComputeOp()
1208 {
1209   delete myCompDlg;
1210   myCompDlg = 0;
1211   delete myTShapeDisplayer;
1212   if ( myBadMeshDisplayer )
1213     delete myBadMeshDisplayer;
1214 }
1215
1216 //================================================================================
1217 /*!
1218  * \brief Gets dialog of compute operation
1219  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
1220  */
1221 //================================================================================
1222
1223 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::computeDlg() const
1224 {
1225   if ( !myCompDlg )
1226   {
1227     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
1228     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), false );
1229     // connect signals and slots
1230     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
1231     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
1232     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
1233
1234     QTableWidget* aTable = me->table();
1235     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
1236     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
1237   }
1238   return myCompDlg;
1239 }
1240
1241 //================================================================================
1242 /*!
1243  * \brief returns from compute mesh result dialog
1244  */
1245 //================================================================================
1246
1247 bool SMESHGUI_BaseComputeOp::onApply()
1248 {
1249   return true;
1250 }
1251
1252 //================================================================================
1253 /*!
1254  * \brief Return a table
1255  */
1256 //================================================================================
1257
1258 QTableWidget* SMESHGUI_BaseComputeOp::table()
1259 {
1260   return myCompDlg->myTable;
1261 }
1262
1263
1264 //================================================================================
1265 /*!
1266  * \brief Constructor
1267 */
1268 //================================================================================
1269
1270 SMESHGUI_ComputeOp::SMESHGUI_ComputeOp()
1271  : SMESHGUI_BaseComputeOp()
1272 {
1273 }
1274
1275
1276 //================================================================================
1277 /*!
1278  * \brief Desctructor
1279 */
1280 //================================================================================
1281
1282 SMESHGUI_ComputeOp::~SMESHGUI_ComputeOp()
1283 {
1284 }
1285
1286 //================================================================================
1287 /*!
1288  * \brief perform it's intention action: compute mesh
1289  */
1290 //================================================================================
1291
1292 void SMESHGUI_ComputeOp::startOperation()
1293 {
1294   SMESHGUI_BaseComputeOp::startOperation();
1295   if (myMesh->_is_nil())
1296     return;
1297   computeMesh();
1298 }
1299
1300 //================================================================================
1301 /*!
1302  * \brief check the same operations on the same mesh
1303  */
1304 //================================================================================
1305
1306 bool SMESHGUI_BaseComputeOp::isValid(  SUIT_Operation* theOp  ) const
1307 {
1308   SMESHGUI_BaseComputeOp* baseOp = dynamic_cast<SMESHGUI_BaseComputeOp*>( theOp );
1309   bool ret = true;
1310   if ( !myMesh->_is_nil() && baseOp ) {
1311     SMESH::SMESH_Mesh_var aMesh = baseOp->getMesh();
1312     if ( !aMesh->_is_nil() && aMesh->GetId() == myMesh->GetId() ) ret = false;
1313   }
1314   return ret;
1315 }
1316
1317 //================================================================================
1318 /*!
1319  * \brief Gets dialog of this operation
1320  * \retval LightApp_Dialog* - pointer to dialog of this operation
1321  */
1322 //================================================================================
1323
1324 LightApp_Dialog* SMESHGUI_ComputeOp::dlg() const
1325 {
1326   return computeDlg();
1327 }
1328
1329 //================================================================================
1330 /*!
1331  * \brief Constructor
1332 */
1333 //================================================================================
1334
1335 SMESHGUI_PrecomputeOp::SMESHGUI_PrecomputeOp()
1336  : SMESHGUI_BaseComputeOp(),
1337  myDlg( 0 ),
1338  myOrderMgr( 0 ),
1339  myActiveDlg( 0 ),
1340  myPreviewDisplayer( 0 )
1341 {
1342   myHelpFileName = "constructing_meshes_page.html#preview_mesh_anchor";
1343 }
1344
1345 //================================================================================
1346 /*!
1347  * \brief Destructor
1348  */
1349 //================================================================================
1350
1351 SMESHGUI_PrecomputeOp::~SMESHGUI_PrecomputeOp()
1352 {
1353   delete myDlg;
1354   myDlg = 0;
1355   delete myOrderMgr;
1356   myOrderMgr = 0;
1357   myActiveDlg = 0;
1358   if ( myPreviewDisplayer )
1359     delete myPreviewDisplayer;
1360   myPreviewDisplayer = 0;
1361 }
1362
1363 //================================================================================
1364 /*!
1365  * \brief Gets current dialog of this operation
1366  * \retval LightApp_Dialog* - pointer to dialog of this operation
1367  */
1368 //================================================================================
1369
1370 LightApp_Dialog* SMESHGUI_PrecomputeOp::dlg() const
1371 {
1372   return myActiveDlg;
1373 }
1374
1375 //================================================================================
1376 /*!
1377  * \brief perform it's intention action: prepare data
1378  */
1379 //================================================================================
1380
1381 void SMESHGUI_PrecomputeOp::startOperation()
1382 {
1383   if ( !myDlg )
1384   {
1385     myDlg = new SMESHGUI_PrecomputeDlg( desktop() );
1386     
1387     // connect signals
1388     connect( myDlg, SIGNAL( preview() ), this, SLOT( onPreview() ) );
1389     connect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onCompute() ) );
1390     connect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onCompute() ) );
1391   }
1392   myActiveDlg = myDlg;
1393
1394   // connect signal to compute dialog. which will be shown after Compute mesh operation
1395   SMESHGUI_ComputeDlg* cmpDlg = computeDlg();
1396   if ( cmpDlg )
1397   {
1398     // disconnect signals
1399     disconnect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1400     disconnect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1401     disconnect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1402     disconnect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1403     disconnect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1404
1405     // connect signals
1406     if( cmpDlg->testButtonFlags( QtxDialog::OK ) )
1407       connect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1408     if( cmpDlg->testButtonFlags( QtxDialog::Apply ) )
1409       connect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1410     if( cmpDlg->testButtonFlags( QtxDialog::Help ) )
1411       connect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1412     if( cmpDlg->testButtonFlags( QtxDialog::Cancel ) )
1413       connect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1414     if( cmpDlg->testButtonFlags( QtxDialog::Close ) )
1415       connect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1416   }
1417
1418   SMESHGUI_BaseComputeOp::startOperation();
1419   if (myMesh->_is_nil())
1420     return;
1421
1422   if (myDlg->getPreviewMode() == -1)
1423   {
1424     // nothing to preview
1425     SUIT_MessageBox::warning(desktop(),
1426                              tr("SMESH_WRN_WARNING"),
1427                              tr("SMESH_WRN_NOTHING_PREVIEW"));
1428     onCancel();
1429     return;
1430   }
1431
1432   // disconnect slot from preview dialog to have Apply from results of compute operation only 
1433   disconnect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1434   disconnect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1435
1436   myDlg->show();
1437 }
1438
1439 //================================================================================
1440 /*!
1441  * \brief Stops operation
1442  */
1443 //================================================================================
1444
1445 void SMESHGUI_PrecomputeOp::stopOperation()
1446 {
1447   if ( myPreviewDisplayer )
1448   {
1449     myPreviewDisplayer->SetVisibility( false );
1450     delete myPreviewDisplayer;
1451     myPreviewDisplayer = 0;
1452   }
1453   myMapShapeId.clear();
1454   SMESHGUI_BaseComputeOp::stopOperation();
1455 }
1456
1457 //================================================================================
1458 /*!
1459  * \brief reinitialize dialog after operaiton become active again
1460  */
1461 //================================================================================
1462
1463 void SMESHGUI_PrecomputeOp::resumeOperation()
1464 {
1465   if ( myActiveDlg == myDlg )
1466     initDialog();
1467   SMESHGUI_BaseComputeOp::resumeOperation();
1468 }
1469
1470 //================================================================================
1471 /*!
1472  * \brief perform it's intention action: reinitialise dialog
1473  */
1474 //================================================================================
1475
1476 void SMESHGUI_PrecomputeOp::initDialog()
1477 {
1478   QList<int> modes;
1479
1480   QMap<int, int> modeMap;
1481   _PTR(SObject)  pMesh = studyDS()->FindObjectID( myIObject->getEntry() );
1482   getAssignedAlgos( pMesh, modeMap );
1483   if ( modeMap.contains( SMESH::DIM_3D ) )
1484   {
1485     if ( modeMap.contains( SMESH::DIM_2D ) )
1486       modes.append( SMESH::DIM_2D );
1487     if ( modeMap.contains( SMESH::DIM_1D ) )
1488       modes.append( SMESH::DIM_1D );
1489   }
1490   else if ( modeMap.contains( SMESH::DIM_2D ) )
1491   {
1492     if ( modeMap.contains( SMESH::DIM_1D ) )
1493       modes.append( SMESH::DIM_1D );
1494   }
1495
1496   myOrderMgr = new SMESHGUI_MeshOrderMgr( myDlg->getMeshOrderBox() );
1497   myOrderMgr->SetMesh( myMesh );
1498   bool isOrder = myOrderMgr->GetMeshOrder(myPrevOrder);
1499   myDlg->getMeshOrderBox()->setShown(isOrder);
1500   if ( !isOrder ) {
1501     delete myOrderMgr;
1502     myOrderMgr = 0;
1503   }
1504
1505   myDlg->setPreviewModes( modes );
1506 }
1507
1508 //================================================================================
1509 /*!
1510  * \brief detect asigned mesh algorithms
1511  */
1512 //================================================================================
1513
1514 void SMESHGUI_PrecomputeOp::getAssignedAlgos(_PTR(SObject) theMesh,
1515                                              QMap<int,int>& theModeMap)
1516 {
1517   _PTR(SObject)          aHypRoot;
1518   _PTR(GenericAttribute) anAttr;
1519   int aPart = SMESH::Tag_RefOnAppliedAlgorithms;
1520   if ( theMesh && theMesh->FindSubObject( aPart, aHypRoot ) )
1521   {
1522     _PTR(ChildIterator) anIter =
1523       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
1524     for ( ; anIter->More(); anIter->Next() )
1525     {
1526       _PTR(SObject) anObj = anIter->Value();
1527       _PTR(SObject) aRefObj;
1528       if ( anObj->ReferencedObject( aRefObj ) )
1529         anObj = aRefObj;
1530       else
1531         continue;
1532       
1533       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
1534       {
1535         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
1536         if ( CORBA::is_nil( aVar ) )
1537           continue;
1538         
1539         for( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1540         {
1541           SMESH::SMESH_Algo_var algo;
1542           switch(dim) {
1543           case SMESH::DIM_1D: algo = SMESH::SMESH_1D_Algo::_narrow( aVar ); break;
1544           case SMESH::DIM_2D: algo = SMESH::SMESH_2D_Algo::_narrow( aVar ); break;
1545           case SMESH::DIM_3D: algo = SMESH::SMESH_3D_Algo::_narrow( aVar ); break;
1546           default: break;
1547           }
1548           if ( !algo->_is_nil() )
1549             theModeMap[ dim ] = 0;
1550         }
1551       }
1552     }
1553   }
1554 }
1555
1556 //================================================================================
1557 /*!
1558  * \brief perform it's intention action: compute mesh
1559  */
1560 //================================================================================
1561
1562 void SMESHGUI_PrecomputeOp::onCompute()
1563 {
1564   myDlg->hide();
1565   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1566     myOrderMgr->SetMeshOrder();
1567   myMapShapeId.clear();
1568   myActiveDlg = computeDlg();
1569   computeMesh();
1570 }
1571
1572 //================================================================================
1573 /*!
1574  * \brief perform it's intention action: compute mesh
1575  */
1576 //================================================================================
1577
1578 void SMESHGUI_PrecomputeOp::onCancel()
1579 {
1580   QObject* curDlg = sender();
1581   if ( curDlg == computeDlg() && myActiveDlg == myDlg )
1582   {
1583     // return from error messages
1584     myDlg->show();
1585     return;
1586   }
1587
1588   bool isRestoreOrder = false;
1589   if ( myActiveDlg == myDlg  && !myMesh->_is_nil() && myMapShapeId.count() )
1590   {
1591     // ask to remove already computed mesh elements
1592     if ( SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1593                                     tr( "CLEAR_SUBMESH_QUESTION" ),
1594                                     tr( "SMESH_BUT_DELETE" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0 )
1595     {
1596       // remove all submeshes for collected shapes
1597       QMap<int,int>::const_iterator it = myMapShapeId.constBegin();
1598       for ( ; it != myMapShapeId.constEnd(); ++it )
1599         myMesh->ClearSubMesh( *it );
1600       isRestoreOrder = true;
1601     }
1602   }
1603
1604   // return previous mesh order
1605   if (myOrderMgr && myOrderMgr->IsOrderChanged()) {
1606     if (!isRestoreOrder)
1607       isRestoreOrder = 
1608         (SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1609                                     tr( "SMESH_REJECT_MESH_ORDER" ),
1610                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0);
1611     if (isRestoreOrder)
1612       myOrderMgr->SetMeshOrder(myPrevOrder);
1613   }
1614
1615   delete myOrderMgr;
1616   myOrderMgr = 0;
1617
1618   myMapShapeId.clear();
1619   SMESHGUI_BaseComputeOp::onCancel();
1620 }
1621
1622 //================================================================================
1623 /*!
1624  * \brief perform it's intention action: preview mesh
1625  */
1626 //================================================================================
1627
1628 void SMESHGUI_PrecomputeOp::onPreview()
1629 {
1630   if ( !myDlg || myMesh->_is_nil() || myMainShape->_is_nil() )
1631     return;
1632
1633   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1634   if ( !aMeshSObj )
1635     return;
1636
1637   // set modified submesh priority if any
1638   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1639     myOrderMgr->SetMeshOrder();
1640
1641   // Compute preview of mesh, 
1642   // i.e. compute mesh till indicated dimension
1643   int dim = myDlg->getPreviewMode();
1644   
1645   SMESH::MemoryReserve aMemoryReserve;
1646   
1647   SMESH::compute_error_array_var aCompErrors;
1648   QString                        aHypErrors;
1649
1650   bool computeFailed = true, memoryLack = false;
1651
1652   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
1653     aCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1654
1655   SMESHGUI* gui = getSMESHGUI();
1656   SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1657   SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1658   if ( errors->length() > 0 ) {
1659     aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1660   }
1661
1662   SUIT_OverrideCursor aWaitCursor;
1663
1664   SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1665   if ( myPreviewDisplayer ) delete myPreviewDisplayer;
1666   myPreviewDisplayer = new SMESHGUI_MeshEditPreview( view );
1667   
1668   SMESH::long_array_var aShapesId = new SMESH::long_array();
1669   try {
1670 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1671     OCC_CATCH_SIGNALS;
1672 #endif
1673       
1674     SMESH::MeshPreviewStruct_var previewData =
1675       gen->Precompute(myMesh, myMainShape, (SMESH::Dimension)dim, aShapesId);
1676
1677     SMESH::MeshPreviewStruct* previewRes = previewData._retn();
1678     if ( previewRes && previewRes->nodesXYZ.length() > 0 )
1679     {
1680       computeFailed = false;
1681       myPreviewDisplayer->SetData( previewRes );
1682       // append shape indeces with computed mesh entities
1683       for ( int i = 0, n = aShapesId->length(); i < n; i++ )
1684         myMapShapeId[ aShapesId[ i ] ] = 0;
1685     }
1686     else
1687       myPreviewDisplayer->SetVisibility(false);
1688   }
1689   catch(const SALOME::SALOME_Exception & S_ex){
1690     memoryLack = true;
1691     myPreviewDisplayer->SetVisibility(false);
1692   }
1693
1694   try {
1695 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1696     OCC_CATCH_SIGNALS;
1697 #endif
1698     aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1699     // check if there are memory problems
1700     for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
1701       memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
1702   }
1703   catch(const SALOME::SALOME_Exception & S_ex){
1704     memoryLack = true;
1705   }
1706
1707   if ( memoryLack )
1708     aMemoryReserve.release();
1709
1710   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1711   bool noHypoError = ( aHypErrors.isEmpty() );
1712
1713   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( gui );
1714   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
1715
1716   bool isShowError = true;
1717   switch( aNotifyMode ) {
1718   case 0: // show the mesh computation result dialog NEVER
1719     isShowError = false;
1720     break;
1721   case 1: // show the mesh computation result dialog if there are some errors
1722   default: // show the result dialog after each mesh computation
1723     if ( !computeFailed && !memoryLack && noCompError && noHypoError )
1724       isShowError = false;
1725     break;
1726   }
1727
1728   aWaitCursor.suspend();
1729   // SHOW ERRORS
1730   if ( isShowError )
1731   {
1732     myDlg->hide();
1733     aCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
1734     showComputeResult( memoryLack, noCompError, aCompErrors, noHypoError, aHypErrors );
1735   }
1736 }
1737
1738
1739 //================================================================================
1740 /*!
1741  * \brief Constructor
1742 */
1743 //================================================================================
1744
1745 SMESHGUI_PrecomputeDlg::SMESHGUI_PrecomputeDlg( QWidget* parent )
1746  : SMESHGUI_Dialog( parent, false, false, OK | Cancel | Help ),
1747    myOrderBox(0)
1748 {
1749   setWindowTitle( tr( "CAPTION" ) );
1750
1751   setButtonText( OK, tr( "COMPUTE" ) );
1752   QFrame* main = mainFrame();
1753
1754   QVBoxLayout* layout = new QVBoxLayout( main );
1755
1756   myOrderBox = new SMESHGUI_MeshOrderBox( main );
1757   layout->addWidget(myOrderBox);
1758
1759   QFrame* frame = new QFrame( main );
1760   layout->setMargin(0); layout->setSpacing(0);
1761   layout->addWidget( frame );
1762
1763   QHBoxLayout* frameLay = new QHBoxLayout( frame );
1764   frameLay->setMargin(0); frameLay->setSpacing(SPACING);
1765   
1766   myPreviewMode = new QtxComboBox( frame );
1767   frameLay->addWidget( myPreviewMode );
1768
1769   myPreviewBtn = new QPushButton( tr( "PREVIEW" ), frame );
1770   frameLay->addWidget( myPreviewBtn );
1771
1772   connect( myPreviewBtn, SIGNAL( clicked( bool ) ), this, SIGNAL( preview() ) );
1773 }
1774
1775 //================================================================================
1776 /*!
1777  * \brief Destructor
1778 */
1779 //================================================================================
1780
1781 SMESHGUI_PrecomputeDlg::~SMESHGUI_PrecomputeDlg()
1782 {
1783 }
1784
1785 //================================================================================
1786 /*!
1787  * \brief Sets available preview modes
1788 */
1789 //================================================================================
1790
1791 void SMESHGUI_PrecomputeDlg::setPreviewModes( const QList<int>& theModes )
1792 {
1793   myPreviewMode->clear();
1794   QList<int>::const_iterator it = theModes.constBegin();
1795   for ( int i = 0; it != theModes.constEnd(); ++it, i++ )
1796   {
1797     QString mode = QString( "PREVIEW_%1" ).arg( *it );
1798     myPreviewMode->addItem( tr( mode.toLatin1().data() ) );
1799     myPreviewMode->setId( i, *it );
1800   }
1801   myPreviewBtn->setEnabled( !theModes.isEmpty() );
1802 }
1803
1804 //================================================================================
1805 /*!
1806  * \brief Returns current preview mesh mode
1807 */
1808 //================================================================================
1809
1810 int SMESHGUI_PrecomputeDlg::getPreviewMode() const
1811 {
1812   return myPreviewMode->currentId();
1813 }
1814
1815 //================================================================================
1816 /*!
1817  * \brief Returns current preview mesh mode
1818 */
1819 //================================================================================
1820
1821 SMESHGUI_MeshOrderBox* SMESHGUI_PrecomputeDlg::getMeshOrderBox() const
1822 {
1823   return myOrderBox;
1824 }
1825
1826
1827 //================================================================================
1828 /*!
1829  * \brief Constructor
1830 */
1831 //================================================================================
1832
1833 SMESHGUI_EvaluateOp::SMESHGUI_EvaluateOp()
1834  : SMESHGUI_BaseComputeOp()
1835 {
1836 }
1837
1838
1839 //================================================================================
1840 /*!
1841  * \brief Desctructor
1842 */
1843 //================================================================================
1844
1845 SMESHGUI_EvaluateOp::~SMESHGUI_EvaluateOp()
1846 {
1847 }
1848
1849 //================================================================================
1850 /*!
1851  * \brief perform it's intention action: compute mesh
1852  */
1853 //================================================================================
1854
1855 void SMESHGUI_EvaluateOp::startOperation()
1856 {
1857   SMESHGUI_BaseComputeOp::evaluateDlg();
1858   SMESHGUI_BaseComputeOp::startOperation();
1859   if (myMesh->_is_nil())
1860     return;
1861   evaluateMesh();
1862 }
1863
1864 //================================================================================
1865 /*!
1866  * \brief Gets dialog of this operation
1867  * \retval LightApp_Dialog* - pointer to dialog of this operation
1868  */
1869 //================================================================================
1870
1871 LightApp_Dialog* SMESHGUI_EvaluateOp::dlg() const
1872 {
1873   return evaluateDlg();
1874 }
1875
1876 //================================================================================
1877 /*!
1878  * \brief evaluateMesh()
1879 */
1880 //================================================================================
1881
1882 void SMESHGUI_BaseComputeOp::evaluateMesh()
1883 {
1884   // EVALUATE MESH
1885
1886   SMESH::MemoryReserve aMemoryReserve;
1887
1888   SMESH::compute_error_array_var aCompErrors;
1889   QString                        aHypErrors;
1890
1891   bool evaluateFailed = true, memoryLack = false;
1892   SMESH::long_array_var aRes;
1893
1894   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1895   if ( !aMeshSObj ) //  IPAL21340
1896     return;
1897
1898   bool hasShape = myMesh->HasShapeToMesh();
1899   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
1900   if ( shapeOK )
1901   {
1902     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1903     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
1904     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1905     if ( errors->length() > 0 ) {
1906       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1907     }
1908     SUIT_OverrideCursor aWaitCursor;
1909     try {
1910 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1911       OCC_CATCH_SIGNALS;
1912 #endif
1913       aRes = gen->Evaluate(myMesh, myMainShape);
1914     }
1915     catch(const SALOME::SALOME_Exception & S_ex){
1916       memoryLack = true;
1917     }
1918
1919     try {
1920 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1921       OCC_CATCH_SIGNALS;
1922 #endif
1923       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1924     }
1925     catch(const SALOME::SALOME_Exception & S_ex){
1926       memoryLack = true;
1927     }
1928   }
1929
1930   if ( memoryLack )
1931     aMemoryReserve.release();
1932
1933   evaluateFailed =  ( aCompErrors->length() > 0 );
1934   myCompDlg->setWindowTitle(tr( evaluateFailed ? "SMESH_WRN_EVALUATE_FAILED" : "SMESH_EVALUATE_SUCCEED"));
1935
1936   // SHOW ERRORS
1937   
1938   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1939   bool noHypoError = ( aHypErrors.isEmpty() );
1940
1941   //SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
1942   //int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
1943
1944   bool isShowResultDlg = true;
1945   //if( noHypoError )
1946   //switch( aNotifyMode ) {
1947   //case 0: // show the mesh computation result dialog NEVER
1948   //isShowResultDlg = false;
1949   //commit();
1950   //break;
1951   //case 1: // show the mesh computation result dialog if there are some errors
1952   //if ( memoryLack || !noHypoError )
1953   //  isShowResultDlg = true;
1954   //else
1955   //{
1956   //  isShowResultDlg = false;
1957   //  commit();
1958   //}
1959   //break;
1960   //default: // show the result dialog after each mesh computation
1961   //isShowResultDlg = true;
1962   //}
1963
1964   // SHOW RESULTS
1965   if ( isShowResultDlg )
1966     showEvaluateResult( aRes, memoryLack, noCompError, aCompErrors,
1967                         noHypoError, aHypErrors);
1968 }
1969
1970
1971 void SMESHGUI_BaseComputeOp::showEvaluateResult(const SMESH::long_array& theRes,
1972                                                 const bool theMemoryLack,
1973                                                 const bool theNoCompError,
1974                                                 SMESH::compute_error_array_var& theCompErrors,
1975                                                 const bool theNoHypoError,
1976                                                 const QString& theHypErrors)
1977 {
1978   bool hasShape = myMesh->HasShapeToMesh();
1979   SMESHGUI_ComputeDlg* aCompDlg = evaluateDlg();
1980   aCompDlg->myMemoryLackGroup->hide();
1981
1982   if ( theMemoryLack )
1983   {
1984     aCompDlg->myMemoryLackGroup->show();
1985     aCompDlg->myFullInfo->hide();
1986     aCompDlg->myBriefInfo->hide();
1987     aCompDlg->myHypErrorGroup->hide();
1988     aCompDlg->myCompErrorGroup->hide();
1989   }
1990   else if ( theNoCompError && theNoHypoError )
1991   {
1992     aCompDlg->myFullInfo->SetMeshInfo( theRes );
1993     aCompDlg->myFullInfo->show();
1994     aCompDlg->myBriefInfo->hide();
1995     aCompDlg->myHypErrorGroup->hide();
1996     aCompDlg->myCompErrorGroup->hide();
1997   }
1998   else
1999   {
2000     QTableWidget* tbl = aCompDlg->myTable;
2001     aCompDlg->myBriefInfo->SetMeshInfo( theRes );
2002     aCompDlg->myBriefInfo->show();
2003     aCompDlg->myFullInfo->hide();
2004
2005     if ( theNoHypoError ) {
2006       aCompDlg->myHypErrorGroup->hide();
2007     }
2008     else {
2009       aCompDlg->myHypErrorGroup->show();
2010       aCompDlg->myHypErrorLabel->setText( theHypErrors );
2011     }
2012
2013     if ( theNoCompError ) {
2014       aCompDlg->myCompErrorGroup->hide();
2015     }
2016     else {
2017       aCompDlg->myCompErrorGroup->show();
2018
2019       aCompDlg->myPublishBtn->hide();
2020       aCompDlg->myShowBtn->hide();
2021
2022       // fill table of errors
2023       tbl->setRowCount( theCompErrors->length() );
2024       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
2025       else             tbl->showColumn( COL_SHAPE );
2026       tbl->setColumnWidth( COL_ERROR, 200 );
2027
2028       bool hasBadMesh = false;
2029       for ( int row = 0; row < theCompErrors->length(); ++row )
2030       {
2031         SMESH::ComputeError & err = theCompErrors[ row ];
2032
2033         QString text = err.algoName.in();
2034         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
2035         else tbl->item( row, COL_ALGO )->setText( text );
2036
2037         text = SMESH::errorText( err.code, err.comment.in() );
2038         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
2039         else tbl->item( row, COL_ERROR )->setText( text );
2040
2041         text = QString("%1").arg( err.subShapeID );
2042         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
2043         else tbl->item( row, COL_SHAPEID )->setText( text );
2044
2045         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
2046         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
2047         else tbl->item( row, COL_SHAPE )->setText( text );
2048
2049         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
2050         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
2051         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
2052
2053         text = err.hasBadMesh ? "hasBadMesh" : "";
2054         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
2055         else tbl->item( row, COL_BAD_MESH )->setText( text );
2056         if ( err.hasBadMesh ) hasBadMesh = true;
2057
2058         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
2059         tbl->resizeRowToContents( row );
2060       }
2061       tbl->resizeColumnToContents( COL_ALGO );
2062       tbl->resizeColumnToContents( COL_SHAPE );
2063
2064       if ( hasBadMesh )
2065         aCompDlg->myBadMeshBtn->show();
2066       else
2067         aCompDlg->myBadMeshBtn->hide();
2068
2069       tbl->setCurrentCell(0,0);
2070       currentCellChanged(); // to update buttons
2071     }
2072   }
2073   // show dialog and wait, becase Compute can be invoked from Preview operation
2074   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
2075   aCompDlg->show();
2076 }
2077
2078
2079 //================================================================================
2080 /*!
2081  * \brief Gets dialog of evaluate operation
2082  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
2083  */
2084 //================================================================================
2085
2086 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::evaluateDlg() const
2087 {
2088   if ( !myCompDlg )
2089   {
2090     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
2091     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), true );
2092     // connect signals and slots
2093     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
2094     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
2095     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
2096     QTableWidget* aTable = me->table();
2097     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
2098     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
2099   }
2100   return myCompDlg;
2101 }
2102