Salome HOME
0021806: [CEA 637] Wrap error message on Mesh Computation Failed Dialog
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ComputeDlg.cxx
1 // Copyright (C) 2007-2012  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       CASE2TEXT( COMPERR_CANCELED      );
349     case SMESH::COMPERR_ALGO_FAILED:
350       if ( strlen(comment) == 0 )
351         text = QObject::tr("COMPERR_ALGO_FAILED");
352       break;
353     case SMESH::COMPERR_WARNING:
354       return comment ? QString(comment) : QObject::tr("COMPERR_UNKNOWN");
355     default:
356       text = QString("#%1").arg( -errCode );
357     }
358     if ( text.length() > 0 ) text += ". ";
359     return text + comment;
360   }
361   // -----------------------------------------------------------------------
362   /*!
363    * \brief Return SO of a sub-shape
364    */
365   _PTR(SObject) getSubShapeSO( int subShapeID, GEOM::GEOM_Object_var aMainShape)
366   {
367     _PTR(SObject) so = SMESH::FindSObject(aMainShape);
368     if ( subShapeID == 1 || !so )
369       return so;
370     _PTR(ChildIterator) it;
371     if (_PTR(Study) study = SMESH::GetActiveStudyDocument())
372       it =  study->NewChildIterator(so);
373     _PTR(SObject) subSO;
374     if ( it ) {
375       for ( it->InitEx(true); !subSO && it->More(); it->Next() ) {
376         GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( it->Value() );
377         if ( !geom->_is_nil() ) {
378           GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
379           if ( list->length() == 1 && list[0] == subShapeID )
380             subSO = it->Value();
381         }
382       }
383     }
384     return subSO;
385   }
386   // -----------------------------------------------------------------------
387   /*!
388    * \brief Return sub-shape by ID
389    */
390   GEOM::GEOM_Object_ptr getSubShape( int subShapeID, GEOM::GEOM_Object_var aMainShape)
391   {
392     GEOM::GEOM_Object_var aSubShape;
393     if ( subShapeID == 1 )
394       aSubShape = aMainShape;
395     else if ( _PTR(SObject) so = getSubShapeSO( subShapeID, aMainShape ))
396       aSubShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>( so );
397     else
398       aSubShape = SMESH::GetSubShape( aMainShape, subShapeID );
399     return aSubShape._retn();
400   }
401   // -----------------------------------------------------------------------
402   /*!
403    * \brief Return shape type name
404    */
405 #define CASE2NAME(enum) case GEOM::enum: name = QObject::tr( "GEOM_" #enum ); break;
406   QString shapeTypeName(GEOM::GEOM_Object_var aShape, const char* dflt = "" )
407   {
408     QString name = dflt;
409     if ( !aShape->_is_nil() ) {
410       switch ( aShape->GetShapeType() ) {
411       CASE2NAME( VERTEX    );
412       CASE2NAME( EDGE      );
413       CASE2NAME( WIRE      );
414       CASE2NAME( FACE      );
415       CASE2NAME( SHELL     );
416       CASE2NAME( SOLID     );
417       CASE2NAME( COMPSOLID );
418       CASE2NAME( COMPOUND  );
419       default:;
420       }
421     }
422     return name;
423   }
424   // -----------------------------------------------------------------------
425   /*!
426    * \brief Return text describing a sub-shape
427    */
428   QString shapeText(int subShapeID, GEOM::GEOM_Object_var aMainShape )
429   {
430     QString text;
431     if ( _PTR(SObject) aSO = getSubShapeSO( subShapeID, aMainShape ))
432       text = aSO->GetName().c_str();
433     else {
434       text = QString("#%1").arg( subShapeID );
435       QString typeName = shapeTypeName( getSubShape( subShapeID, aMainShape ));
436       if ( typeName.length() )
437         text += QString(" (%1)").arg(typeName);
438     }
439     return text;
440   }
441   // -----------------------------------------------------------------------
442   /*!
443    * \brief Return a list of selected rows
444    */
445   int getSelectedRows(QTableWidget* table, QList<int>& rows)
446   {
447     rows.clear();
448     QList<QTableWidgetSelectionRange> selRanges = table->selectedRanges();
449     QTableWidgetSelectionRange range;
450     foreach( range, selRanges )
451     {
452       for ( int row = range.topRow(); row <= range.bottomRow(); ++row )
453         rows.append( row );
454     }
455     if ( rows.isEmpty() && table->currentRow() > -1 )
456       rows.append( table->currentRow() );
457
458     return rows.count();
459   }
460
461 } // namespace SMESH
462
463
464 // =========================================================================================
465 /*!
466  * \brief Dialog to compute a mesh and show computation errors
467  */
468 //=======================================================================
469
470 SMESHGUI_ComputeDlg::SMESHGUI_ComputeDlg( QWidget* parent, bool ForEval )
471  : SMESHGUI_Dialog( parent, false, true, Close/* | Help*/ )
472 {
473   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
474   aDlgLay->setMargin( 0 );
475   aDlgLay->setSpacing( SPACING );
476
477   QFrame* aMainFrame = createMainFrame(mainFrame(),ForEval);
478
479   aDlgLay->addWidget(aMainFrame);
480
481   aDlgLay->setStretchFactor(aMainFrame, 1);
482 }
483
484 // =========================================================================================
485 /*!
486  * \brief Destructor
487  */
488 //=======================================================================
489
490 SMESHGUI_ComputeDlg::~SMESHGUI_ComputeDlg()
491 {
492 }
493
494 //=======================================================================
495 // function : createMainFrame()
496 // purpose  : Create frame containing dialog's fields
497 //=======================================================================
498
499 QFrame* SMESHGUI_ComputeDlg::createMainFrame (QWidget* theParent, bool ForEval)
500 {
501   QFrame* aFrame = new QFrame(theParent);
502
503   SUIT_ResourceMgr* rm = resourceMgr();
504   QPixmap iconCompute (rm->loadPixmap("SMESH", tr("ICON_COMPUTE")));
505
506   // constructor
507
508   QGroupBox* aPixGrp;
509   if(ForEval) {
510     aPixGrp = new QGroupBox(tr("EVAL_DLG"), aFrame);
511   }
512   else {
513     aPixGrp = new QGroupBox(tr("CONSTRUCTOR"), aFrame);
514   }
515   QButtonGroup* aBtnGrp = new QButtonGroup(this);
516   QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
517   aPixGrpLayout->setMargin(MARGIN); aPixGrpLayout->setSpacing(SPACING);
518
519   QRadioButton* aRBut = new QRadioButton(aPixGrp);
520   aRBut->setIcon(iconCompute);
521   aRBut->setChecked(true);
522   aPixGrpLayout->addWidget(aRBut);
523   aBtnGrp->addButton(aRBut, 0);
524
525   // Mesh name
526
527   QGroupBox* nameBox = new QGroupBox(tr("SMESH_MESHINFO_NAME"), aFrame );
528   QHBoxLayout* nameBoxLayout = new QHBoxLayout(nameBox);
529   nameBoxLayout->setMargin(MARGIN); nameBoxLayout->setSpacing(SPACING);
530   myMeshName = new QLabel(nameBox);
531   nameBoxLayout->addWidget(myMeshName);
532
533   // Mesh Info
534
535   myBriefInfo = new SMESHGUI_MeshInfosBox(false, aFrame);
536   myFullInfo  = new SMESHGUI_MeshInfosBox(true,  aFrame);
537
538   // Computation errors
539
540   myCompErrorGroup = new QGroupBox(tr("ERRORS"), aFrame);
541   myWarningLabel = new QLabel(QString("<b>%1</b>").arg(tr("COMPUTE_WARNING")), myCompErrorGroup);
542   myTable        = new QTableWidget( 1, NB_COLUMNS, myCompErrorGroup);
543   myShowBtn      = new QPushButton(tr("SHOW_SHAPE"), myCompErrorGroup);
544   myPublishBtn   = new QPushButton(tr("PUBLISH_SHAPE"), myCompErrorGroup);
545   myBadMeshBtn   = new QPushButton(tr("SHOW_BAD_MESH"), myCompErrorGroup);
546
547   //myTable->setReadOnly( true ); // VSR: check
548   myTable->setEditTriggers( QAbstractItemView::NoEditTriggers );
549   myTable->hideColumn( COL_PUBLISHED );
550   myTable->hideColumn( COL_SHAPEID );
551   myTable->hideColumn( COL_BAD_MESH );
552   myTable->horizontalHeader()->setResizeMode( COL_ERROR, QHeaderView::Interactive );
553   myTable->setWordWrap( true );
554   myTable->horizontalHeader()->setStretchLastSection( true );
555   myTable->setMinimumWidth( 500 );
556
557   QStringList headers;
558   headers << tr( "COL_ALGO_HEADER" );
559   headers << tr( "COL_SHAPE_HEADER" );
560   headers << tr( "COL_ERROR_HEADER" );
561   headers << tr( "COL_SHAPEID_HEADER" );
562   headers << tr( "COL_PUBLISHED_HEADER" );
563
564   myTable->setHorizontalHeaderLabels( headers );
565
566   // layouting
567   QGridLayout* grpLayout = new QGridLayout(myCompErrorGroup);
568   grpLayout->setSpacing(SPACING);
569   grpLayout->setMargin(MARGIN);
570   grpLayout->addWidget( myWarningLabel, 0, 0, 1, 4 );
571   grpLayout->addWidget( myTable,        1, 0, 1, 4 );
572   grpLayout->addWidget( myShowBtn,      2, 0 );
573   grpLayout->addWidget( myPublishBtn,   2, 1 );
574   grpLayout->addWidget( myBadMeshBtn,   2, 2 );
575   grpLayout->setColumnStretch( 3, 1 );
576
577   // Hypothesis definition errors
578
579   myHypErrorGroup = new QGroupBox(tr("SMESH_WRN_MISSING_PARAMETERS"), aFrame);
580   QHBoxLayout* myHypErrorGroupLayout = new QHBoxLayout(myHypErrorGroup);
581   myHypErrorGroupLayout->setMargin(MARGIN);
582   myHypErrorGroupLayout->setSpacing(SPACING);
583   myHypErrorLabel = new QLabel(myHypErrorGroup);
584   myHypErrorGroupLayout->addWidget(myHypErrorLabel);
585
586   // Memory Lack Label
587
588   myMemoryLackGroup = new QGroupBox(tr("ERRORS"), aFrame);
589   QVBoxLayout* myMemoryLackGroupLayout = new QVBoxLayout(myMemoryLackGroup);
590   myMemoryLackGroupLayout->setMargin(MARGIN);
591   myMemoryLackGroupLayout->setSpacing(SPACING);
592   QLabel* memLackLabel = new QLabel(tr("MEMORY_LACK"), myMemoryLackGroup);
593   QFont bold = memLackLabel->font(); bold.setBold(true);
594   memLackLabel->setFont( bold );
595   memLackLabel->setMinimumWidth(300);
596   myMemoryLackGroupLayout->addWidget(memLackLabel);
597
598   // add all widgets to aFrame
599   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
600   aLay->setMargin( 0 );
601   aLay->setSpacing( 0 );
602   aLay->addWidget( aPixGrp );
603   aLay->addWidget( nameBox );
604   aLay->addWidget( myBriefInfo );
605   aLay->addWidget( myFullInfo );
606   aLay->addWidget( myHypErrorGroup );
607   aLay->addWidget( myCompErrorGroup );
608   aLay->addWidget( myMemoryLackGroup );
609   aLay->setStretchFactor( myCompErrorGroup, 1 );
610
611   ((QPushButton*) button( OK ))->setDefault( true );
612
613   return aFrame;
614 }
615
616 //================================================================================
617 /*!
618  * \brief Constructor
619 */
620 //================================================================================
621
622 SMESHGUI_BaseComputeOp::SMESHGUI_BaseComputeOp()
623   : SMESHGUI_Operation(), myCompDlg( 0 )
624 {
625   myTShapeDisplayer = new SMESH::TShapeDisplayer();
626   myBadMeshDisplayer = 0;
627
628   //myHelpFileName = "/files/about_meshes.htm"; // V3
629   myHelpFileName = "about_meshes_page.html"; // V4
630 }
631
632 SMESH::SMESH_Mesh_ptr SMESHGUI_BaseComputeOp::getMesh()
633 {
634   LightApp_SelectionMgr* Sel = selectionMgr();
635   SALOME_ListIO selected; Sel->selectedObjects( selected );
636   Handle(SALOME_InteractiveObject) anIO = selected.First();
637   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(anIO);
638   return myMesh->_is_nil() ? aMesh._retn() : SMESH::SMESH_Mesh::_duplicate( myMesh );
639 }
640
641 //================================================================================
642 /*!
643  * \brief Start operation
644  * \purpose Init dialog fields, connect signals and slots, show dialog
645  */
646 //================================================================================
647
648 void SMESHGUI_BaseComputeOp::startOperation()
649 {
650   // create compute dialog if not created before
651   computeDlg();
652
653   myMesh      = SMESH::SMESH_Mesh::_nil();
654   myMainShape = GEOM::GEOM_Object::_nil();
655
656   // check selection
657   LightApp_SelectionMgr *Sel = selectionMgr();
658   SALOME_ListIO selected; Sel->selectedObjects( selected );
659
660   int nbSel = selected.Extent();
661   if (nbSel != 1) {
662     SUIT_MessageBox::warning(desktop(),
663                              tr("SMESH_WRN_WARNING"),
664                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
665     onCancel();
666     return;
667   }
668
669   myIObject = selected.First();
670   myMesh = SMESH::GetMeshByIO(myIObject);
671   if (myMesh->_is_nil()) {
672     SUIT_MessageBox::warning(desktop(),
673                              tr("SMESH_WRN_WARNING"),
674                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
675     onCancel();
676     return;
677   }
678   myMainShape = myMesh->GetShapeToMesh();
679
680   SMESHGUI_Operation::startOperation();
681 }
682
683 //================================================================================
684 //================================================================================
685
686 SMESHGUI_ComputeDlg_QThread::SMESHGUI_ComputeDlg_QThread(SMESH::SMESH_Gen_var gen,
687                                                          SMESH::SMESH_Mesh_var mesh,
688                                                          GEOM::GEOM_Object_var mainShape)
689 {
690   myResult = false;
691   myGen = gen;
692   myMesh = mesh;
693   myMainShape = mainShape;
694 }
695
696 void SMESHGUI_ComputeDlg_QThread::run()
697 {
698   myResult = myGen->Compute(myMesh, myMainShape);
699 }
700
701 bool SMESHGUI_ComputeDlg_QThread::result()
702 {
703   return myResult;
704 }
705
706 void SMESHGUI_ComputeDlg_QThread::cancel()
707 {
708   myGen->CancelCompute(myMesh, myMainShape);
709 }
710
711 //================================================================================
712 //================================================================================
713
714 SMESHGUI_ComputeDlg_QThreadQDialog::SMESHGUI_ComputeDlg_QThreadQDialog(QWidget *parent,
715                                                                        SMESH::SMESH_Gen_var gen,
716                                                                        SMESH::SMESH_Mesh_var mesh,
717                                                                        GEOM::GEOM_Object_var mainShape)
718   : QDialog(parent),
719     qthread(gen, mesh, mainShape)
720 {
721   // --
722   setWindowTitle(tr("Compute"));
723   cancelButton = new QPushButton(tr("Cancel"));
724   cancelButton->setDefault(true);
725   connect(cancelButton, SIGNAL(clicked()), this, SLOT(onCancel()));
726   QHBoxLayout *layout = new QHBoxLayout;
727   layout->addWidget(cancelButton);
728   setLayout(layout);
729   resize(200, 50);
730   // --
731   startTimer(30); // 30 millisecs
732   qthread.start();
733 }
734
735 bool SMESHGUI_ComputeDlg_QThreadQDialog::result()
736 {
737   return qthread.result();
738 }
739
740 void SMESHGUI_ComputeDlg_QThreadQDialog::onCancel()
741 {
742   qthread.cancel();
743 }  
744
745 void SMESHGUI_ComputeDlg_QThreadQDialog::timerEvent(QTimerEvent *event)
746 {
747   if(qthread.isFinished())
748     {
749       close();
750     }
751   event->accept();
752 }
753
754 void SMESHGUI_ComputeDlg_QThreadQDialog::closeEvent(QCloseEvent *event)
755 {
756   if(qthread.isRunning())
757     {
758       event->ignore();
759       return;
760     }
761   event->accept();
762 }
763
764 //================================================================================
765 /*!
766  * \brief computeMesh()
767 */
768 //================================================================================
769
770 void SMESHGUI_BaseComputeOp::computeMesh()
771 {
772   // COMPUTE MESH
773
774   SMESH::MemoryReserve aMemoryReserve;
775
776   SMESH::compute_error_array_var aCompErrors;
777   QString                        aHypErrors;
778
779   bool computeFailed = true, memoryLack = false;
780
781   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
782   if ( !aMeshSObj ) // IPAL 21340
783     return;
784   bool hasShape = myMesh->HasShapeToMesh();
785   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
786   if ( shapeOK )
787   {
788     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
789     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
790     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
791     if ( errors->length() > 0 ) {
792       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
793     }
794     if ( myMesh->HasModificationsToDiscard() && // issue 0020693
795          SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
796                                     tr( "FULL_RECOMPUTE_QUESTION" ),
797                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 1, 0 ) == 0 )
798       myMesh->Clear();
799     SUIT_OverrideCursor aWaitCursor;
800     try {
801 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
802       OCC_CATCH_SIGNALS;
803 #endif
804       //SMESH::UpdateNulData(myIObject, true);
805       bool res;
806 #ifdef WITH_SMESH_CANCEL_COMPUTE
807       SMESHGUI_ComputeDlg_QThreadQDialog qthreaddialog(desktop(), gen, myMesh, myMainShape);
808       qthreaddialog.exec();
809       res = qthreaddialog.result();
810 #else
811       res = gen->Compute(myMesh, myMainShape);
812 #endif
813       if (res)
814         computeFailed = false;
815     }
816     catch(const SALOME::SALOME_Exception & S_ex){
817       memoryLack = true;
818     }
819     try {
820 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
821       OCC_CATCH_SIGNALS;
822 #endif
823       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
824       // check if there are memory problems
825       for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
826         memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
827     }
828     catch(const SALOME::SALOME_Exception & S_ex){
829       memoryLack = true;
830     }
831
832     if ( !memoryLack && !SMDS_Mesh::CheckMemory(true) ) { // has memory to show dialog boxes?
833       memoryLack = true;
834     }
835
836     // NPAL16631: if ( !memoryLack )
837     {
838       SMESH::ModifiedMesh(aMeshSObj, !computeFailed, myMesh->NbNodes() == 0);
839       update( UF_ObjBrowser | UF_Model );
840
841       // SHOW MESH
842       // NPAL16631: if ( getSMESHGUI()->automaticUpdate() )
843       SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
844       long newSize = myMesh->NbElements();
845       bool limitExceeded;
846       if ( !memoryLack )
847       {
848         if ( getSMESHGUI()->automaticUpdate( newSize, &limitExceeded ) )
849         {
850           try {
851 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
852             OCC_CATCH_SIGNALS;
853 #endif
854             SMESH::Update(myIObject, true);
855           }
856           catch (...) {
857 #ifdef _DEBUG_
858             MESSAGE ( "Exception thrown during mesh visualization" );
859 #endif
860             if ( SMDS_Mesh::CheckMemory(true) ) { // has memory to show warning?
861               SMESH::OnVisuException();
862             }
863             else {
864               memoryLack = true;
865             }
866           }
867         }
868         else if ( limitExceeded )
869         {
870           long limitSize = resMgr->integerValue( "SMESH", "update_limit", 500000 );
871           SUIT_MessageBox::warning( desktop(),
872                                     tr( "SMESH_WRN_WARNING" ),
873                                     tr( "SMESH_WRN_SIZE_LIMIT_EXCEEDED" ).arg( newSize ).arg( limitSize ) );
874         }
875       }
876       LightApp_SelectionMgr *Sel = selectionMgr();
877       if ( Sel )
878       {
879         SALOME_ListIO selected;
880         selected.Append( myIObject );
881         Sel->setSelectedObjects( selected );
882       }
883     }
884   }
885
886   if ( memoryLack )
887     aMemoryReserve.release();
888
889   myCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
890
891   // SHOW ERRORS
892   
893   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
894   bool noHypoError = ( aHypErrors.isEmpty() );
895
896   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
897   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
898
899   bool isShowResultDlg = true;
900   switch( aNotifyMode ) {
901   case 0: // show the mesh computation result dialog NEVER
902     isShowResultDlg = false;
903     commit();
904     break;
905   case 1: // show the mesh computation result dialog if there are some errors
906     if ( memoryLack || !noCompError || !noHypoError )
907       isShowResultDlg = true;
908     else
909     {
910       isShowResultDlg = false;
911       commit();
912     }
913     break;
914   default: // show the result dialog after each mesh computation
915     isShowResultDlg = true;
916   }
917
918   // SHOW RESULTS
919   if ( isShowResultDlg )
920     showComputeResult( memoryLack, noCompError,aCompErrors, noHypoError, aHypErrors );
921 }
922
923 void SMESHGUI_BaseComputeOp::showComputeResult( const bool theMemoryLack,
924                                                 const bool theNoCompError,
925                                                 SMESH::compute_error_array_var& theCompErrors,
926                                                 const bool theNoHypoError,
927                                                 const QString& theHypErrors )
928 {
929   bool hasShape = myMesh->HasShapeToMesh();
930   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
931   aCompDlg->myMemoryLackGroup->hide();
932
933   if ( theMemoryLack )
934   {
935     aCompDlg->myMemoryLackGroup->show();
936     aCompDlg->myFullInfo->hide();
937     aCompDlg->myBriefInfo->hide();
938     aCompDlg->myHypErrorGroup->hide();
939     aCompDlg->myCompErrorGroup->hide();
940   }
941   else if ( theNoCompError && theNoHypoError )
942   {
943     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
944     aCompDlg->myFullInfo->SetMeshInfo( aRes );
945     aCompDlg->myFullInfo->show();
946     aCompDlg->myBriefInfo->hide();
947     aCompDlg->myHypErrorGroup->hide();
948     aCompDlg->myCompErrorGroup->hide();
949   }
950   else
951   {
952     bool onlyWarnings = !theNoCompError; // == valid mesh computed but there are errors reported
953     for ( int i = 0; i < theCompErrors->length() && onlyWarnings; ++i )
954       onlyWarnings = ( theCompErrors[ i ].code == SMESH::COMPERR_WARNING );
955
956     // full or brief mesh info
957     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
958     if ( onlyWarnings ) {
959       aCompDlg->myFullInfo->SetMeshInfo( aRes );
960       aCompDlg->myFullInfo->show();
961       aCompDlg->myBriefInfo->hide();
962     } else {
963       aCompDlg->myBriefInfo->SetMeshInfo( aRes );
964       aCompDlg->myBriefInfo->show();
965       aCompDlg->myFullInfo->hide();
966     }
967
968     // pbs of hypo dfinitions
969     if ( theNoHypoError ) {
970       aCompDlg->myHypErrorGroup->hide();
971     } else {
972       aCompDlg->myHypErrorGroup->show();
973       aCompDlg->myHypErrorLabel->setText( theHypErrors );
974     }
975
976     // table of errors
977     if ( theNoCompError )
978     {
979       aCompDlg->myCompErrorGroup->hide();
980     }
981     else
982     {
983       aCompDlg->myCompErrorGroup->show();
984
985       if ( onlyWarnings )
986         aCompDlg->myWarningLabel->show();
987       else
988         aCompDlg->myWarningLabel->hide();
989
990       if ( !hasShape ) {
991         aCompDlg->myPublishBtn->hide();
992         aCompDlg->myShowBtn->hide();
993       }
994       else {
995         aCompDlg->myPublishBtn->show();
996         aCompDlg->myShowBtn->show();
997       }
998
999       // fill table of errors
1000       QTableWidget* tbl = aCompDlg->myTable;
1001       tbl->setRowCount( theCompErrors->length() );
1002       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
1003       else             tbl->showColumn( COL_SHAPE );
1004       tbl->setColumnWidth( COL_ERROR, 200 );
1005
1006       bool hasBadMesh = false;
1007       for ( int row = 0; row < theCompErrors->length(); ++row )
1008       {
1009         SMESH::ComputeError & err = theCompErrors[ row ];
1010
1011         QString text = err.algoName.in();
1012         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
1013         else tbl->item( row, COL_ALGO )->setText( text );
1014
1015         text = SMESH::errorText( err.code, err.comment.in() );
1016         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
1017         else tbl->item( row, COL_ERROR )->setText( text );
1018
1019         text = QString("%1").arg( err.subShapeID );
1020         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
1021         else tbl->item( row, COL_SHAPEID )->setText( text );
1022
1023         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
1024         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
1025         else tbl->item( row, COL_SHAPE )->setText( text );
1026
1027         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
1028         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
1029         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
1030
1031         text = err.hasBadMesh ? "hasBadMesh" : "";
1032         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
1033         else tbl->item( row, COL_BAD_MESH )->setText( text );
1034         if ( err.hasBadMesh ) hasBadMesh = true;
1035
1036         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
1037         tbl->resizeRowToContents( row );
1038       }
1039       tbl->resizeColumnToContents( COL_ALGO );
1040       tbl->resizeColumnToContents( COL_SHAPE );
1041       tbl->setWordWrap( true );
1042
1043       if ( hasBadMesh )
1044         aCompDlg->myBadMeshBtn->show();
1045       else
1046         aCompDlg->myBadMeshBtn->hide();
1047
1048       tbl->setCurrentCell(0,0);
1049       currentCellChanged(); // to update buttons
1050     }
1051   }
1052   // show dialog and wait, becase Compute can be invoked from Preview operation
1053   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
1054   aCompDlg->show();
1055 }
1056
1057 //================================================================================
1058 /*!
1059  * \brief Stops operation
1060  */
1061 //================================================================================
1062
1063 void SMESHGUI_BaseComputeOp::stopOperation()
1064 {
1065   SMESHGUI_Operation::stopOperation();
1066   if ( myTShapeDisplayer )
1067     myTShapeDisplayer->SetVisibility( false );
1068   if ( myBadMeshDisplayer ) {
1069     myBadMeshDisplayer->SetVisibility( false );
1070     // delete it in order not to have problems at its destruction when the viewer
1071     // where it worked is dead due to e.g. study closing
1072     delete myBadMeshDisplayer;
1073     myBadMeshDisplayer = 0;
1074   }
1075   myIObject.Nullify();
1076 }
1077
1078 //================================================================================
1079 /*!
1080  * \brief publish selected sub-shape
1081  */
1082 //================================================================================
1083
1084 void SMESHGUI_BaseComputeOp::onPublishShape()
1085 {
1086   GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1087   SALOMEDS::Study_var study = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
1088
1089   QList<int> rows;
1090   SMESH::getSelectedRows( table(), rows );
1091   int row;
1092   foreach ( row, rows )
1093   {
1094     int curSub = table()->item(row, COL_SHAPEID)->text().toInt();
1095     GEOM::GEOM_Object_var shape = SMESH::getSubShape( curSub, myMainShape );
1096     if ( !shape->_is_nil() && ! SMESH::getSubShapeSO( curSub, myMainShape ))
1097     {
1098       if ( !SMESH::getSubShapeSO( 1, myMainShape )) // the main shape not published
1099       {
1100         QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( myMainShape, "MAIN_SHAPE" ));
1101         SALOMEDS::SObject_var so =
1102           geomGen->AddInStudy( study, myMainShape, name.toLatin1().data(), GEOM::GEOM_Object::_nil());
1103         // look for myMainShape in the table
1104         for ( int r = 0, nr = table()->rowCount(); r < nr; ++r ) {
1105           if ( table()->item( r, COL_SHAPEID )->text() == "1" ) {
1106             if ( so->_is_nil() ) {
1107               table()->item( r, COL_SHAPE )->setText( so->GetName() );
1108               table()->item( r, COL_PUBLISHED )->setText( so->GetID() );
1109             }
1110             break;
1111           }
1112         }
1113         if ( curSub == 1 ) continue;
1114       }
1115       QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( shape, "ERROR_SHAPE" ));
1116       SALOMEDS::SObject_var so = geomGen->AddInStudy( study, shape, name.toLatin1().data(), myMainShape);
1117       if ( !so->_is_nil() ) {
1118         table()->item( row, COL_SHAPE )->setText( so->GetName() );
1119         table()->item( row, COL_PUBLISHED )->setText( so->GetID() );
1120       }
1121     }
1122   }
1123   getSMESHGUI()->getApp()->updateObjectBrowser();
1124   currentCellChanged(); // to update buttons
1125 }
1126
1127 //================================================================================
1128 /*!
1129  * \brief show mesh elements preventing computation of a submesh of current row
1130  */
1131 //================================================================================
1132
1133 void SMESHGUI_BaseComputeOp::onShowBadMesh()
1134 {
1135   myTShapeDisplayer->SetVisibility( false );
1136   QList<int> rows;
1137   if ( SMESH::getSelectedRows( table(), rows ) == 1 ) {
1138     bool hasBadMesh = ( !table()->item(rows.front(), COL_BAD_MESH)->text().isEmpty() );
1139     if ( hasBadMesh ) {
1140       int curSub = table()->item(rows.front(), COL_SHAPEID)->text().toInt();
1141       SMESHGUI* gui = getSMESHGUI();
1142       SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1143       SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1144       if ( myBadMeshDisplayer ) delete myBadMeshDisplayer;
1145       myBadMeshDisplayer = new SMESHGUI_MeshEditPreview( view );
1146       SMESH::MeshPreviewStruct_var aMeshData = gen->GetBadInputElements(myMesh,curSub);
1147       vtkFloatingPointType aPointSize = SMESH::GetFloat("SMESH:node_size",3);
1148       vtkFloatingPointType aLineWidth = SMESH::GetFloat("SMESH:element_width",1);
1149       // delete property !!!!!!!!!!
1150       vtkProperty* prop = vtkProperty::New();
1151       prop->SetLineWidth( aLineWidth * 3 );
1152       prop->SetPointSize( aPointSize * 3 );
1153       prop->SetColor( 250, 0, 250 );
1154       myBadMeshDisplayer->GetActor()->SetProperty( prop );
1155       myBadMeshDisplayer->SetData( aMeshData._retn() );
1156     }
1157   }
1158 }
1159
1160 //================================================================================
1161 /*!
1162  * \brief SLOT called when a selected cell in table() changed
1163  */
1164 //================================================================================
1165
1166 void SMESHGUI_BaseComputeOp::currentCellChanged()
1167 {
1168   myTShapeDisplayer->SetVisibility( false );
1169   if ( myBadMeshDisplayer )
1170     myBadMeshDisplayer->SetVisibility( false );
1171
1172   bool publishEnable = 0, showEnable = 0, showOnly = 1, hasBadMesh = 0;
1173   QList<int> rows;
1174   int nbSelected = SMESH::getSelectedRows( table(), rows );
1175   int row;
1176   foreach ( row, rows )
1177   {
1178     bool hasData     = ( !table()->item( row, COL_SHAPE )->text().isEmpty() );
1179     bool isPublished = ( !table()->item( row, COL_PUBLISHED )->text().isEmpty() );
1180     if ( hasData && !isPublished )
1181       publishEnable = true;
1182
1183     int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1184     bool prsReady = myTShapeDisplayer->HasReadyActorsFor( curSub, myMainShape );
1185     if ( prsReady ) {
1186       myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1187       showOnly = false;
1188     }
1189     else {
1190       showEnable = true;
1191     }
1192
1193     if ( !table()->item(row, COL_BAD_MESH)->text().isEmpty() )
1194       hasBadMesh = true;
1195   }
1196   myCompDlg->myPublishBtn->setEnabled( publishEnable );
1197   myCompDlg->myShowBtn   ->setEnabled( showEnable );
1198   myCompDlg->myBadMeshBtn->setEnabled( hasBadMesh && ( nbSelected == 1 ));
1199 }
1200
1201 //================================================================================
1202 /*!
1203  * \brief update preview
1204  */
1205 //================================================================================
1206
1207 void SMESHGUI_BaseComputeOp::onPreviewShape()
1208 {
1209   if ( myTShapeDisplayer )
1210   {
1211     SUIT_OverrideCursor aWaitCursor;
1212     QList<int> rows;
1213     SMESH::getSelectedRows( table(), rows );
1214
1215     bool showOnly = true;
1216     int row;
1217     foreach ( row, rows )
1218     {
1219       int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1220       if ( curSub > 0 ) {
1221         myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1222         showOnly = false;
1223       }
1224     }
1225     currentCellChanged(); // to update buttons
1226   }
1227 }
1228
1229 //================================================================================
1230 /*!
1231  * \brief Destructor
1232  */
1233 //================================================================================
1234
1235 SMESHGUI_BaseComputeOp::~SMESHGUI_BaseComputeOp()
1236 {
1237   delete myCompDlg;
1238   myCompDlg = 0;
1239   delete myTShapeDisplayer;
1240   if ( myBadMeshDisplayer )
1241     delete myBadMeshDisplayer;
1242 }
1243
1244 //================================================================================
1245 /*!
1246  * \brief Gets dialog of compute operation
1247  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
1248  */
1249 //================================================================================
1250
1251 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::computeDlg() const
1252 {
1253   if ( !myCompDlg )
1254   {
1255     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
1256     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), false );
1257     // connect signals and slots
1258     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
1259     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
1260     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
1261
1262     QTableWidget* aTable = me->table();
1263     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
1264     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
1265   }
1266   return myCompDlg;
1267 }
1268
1269 //================================================================================
1270 /*!
1271  * \brief returns from compute mesh result dialog
1272  */
1273 //================================================================================
1274
1275 bool SMESHGUI_BaseComputeOp::onApply()
1276 {
1277   return true;
1278 }
1279
1280 //================================================================================
1281 /*!
1282  * \brief Return a table
1283  */
1284 //================================================================================
1285
1286 QTableWidget* SMESHGUI_BaseComputeOp::table()
1287 {
1288   return myCompDlg->myTable;
1289 }
1290
1291
1292 //================================================================================
1293 /*!
1294  * \brief Constructor
1295 */
1296 //================================================================================
1297
1298 SMESHGUI_ComputeOp::SMESHGUI_ComputeOp()
1299  : SMESHGUI_BaseComputeOp()
1300 {
1301 }
1302
1303
1304 //================================================================================
1305 /*!
1306  * \brief Desctructor
1307 */
1308 //================================================================================
1309
1310 SMESHGUI_ComputeOp::~SMESHGUI_ComputeOp()
1311 {
1312 }
1313
1314 //================================================================================
1315 /*!
1316  * \brief perform it's intention action: compute mesh
1317  */
1318 //================================================================================
1319
1320 void SMESHGUI_ComputeOp::startOperation()
1321 {
1322   SMESHGUI_BaseComputeOp::startOperation();
1323   if (myMesh->_is_nil())
1324     return;
1325   computeMesh();
1326 }
1327
1328 //================================================================================
1329 /*!
1330  * \brief check the same operations on the same mesh
1331  */
1332 //================================================================================
1333
1334 bool SMESHGUI_BaseComputeOp::isValid(  SUIT_Operation* theOp  ) const
1335 {
1336   SMESHGUI_BaseComputeOp* baseOp = dynamic_cast<SMESHGUI_BaseComputeOp*>( theOp );
1337   bool ret = true;
1338   if ( !myMesh->_is_nil() && baseOp ) {
1339     SMESH::SMESH_Mesh_var aMesh = baseOp->getMesh();
1340     if ( !aMesh->_is_nil() && aMesh->GetId() == myMesh->GetId() ) ret = false;
1341   }
1342   return ret;
1343 }
1344
1345 //================================================================================
1346 /*!
1347  * \brief Gets dialog of this operation
1348  * \retval LightApp_Dialog* - pointer to dialog of this operation
1349  */
1350 //================================================================================
1351
1352 LightApp_Dialog* SMESHGUI_ComputeOp::dlg() const
1353 {
1354   return computeDlg();
1355 }
1356
1357 //================================================================================
1358 /*!
1359  * \brief Constructor
1360 */
1361 //================================================================================
1362
1363 SMESHGUI_PrecomputeOp::SMESHGUI_PrecomputeOp()
1364  : SMESHGUI_BaseComputeOp(),
1365  myDlg( 0 ),
1366  myOrderMgr( 0 ),
1367  myActiveDlg( 0 ),
1368  myPreviewDisplayer( 0 )
1369 {
1370   myHelpFileName = "constructing_meshes_page.html#preview_mesh_anchor";
1371 }
1372
1373 //================================================================================
1374 /*!
1375  * \brief Destructor
1376  */
1377 //================================================================================
1378
1379 SMESHGUI_PrecomputeOp::~SMESHGUI_PrecomputeOp()
1380 {
1381   delete myDlg;
1382   myDlg = 0;
1383   delete myOrderMgr;
1384   myOrderMgr = 0;
1385   myActiveDlg = 0;
1386   if ( myPreviewDisplayer )
1387     delete myPreviewDisplayer;
1388   myPreviewDisplayer = 0;
1389 }
1390
1391 //================================================================================
1392 /*!
1393  * \brief Gets current dialog of this operation
1394  * \retval LightApp_Dialog* - pointer to dialog of this operation
1395  */
1396 //================================================================================
1397
1398 LightApp_Dialog* SMESHGUI_PrecomputeOp::dlg() const
1399 {
1400   return myActiveDlg;
1401 }
1402
1403 //================================================================================
1404 /*!
1405  * \brief perform it's intention action: prepare data
1406  */
1407 //================================================================================
1408
1409 void SMESHGUI_PrecomputeOp::startOperation()
1410 {
1411   if ( !myDlg )
1412   {
1413     myDlg = new SMESHGUI_PrecomputeDlg( desktop() );
1414     
1415     // connect signals
1416     connect( myDlg, SIGNAL( preview() ), this, SLOT( onPreview() ) );
1417     connect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onCompute() ) );
1418     connect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onCompute() ) );
1419   }
1420   myActiveDlg = myDlg;
1421
1422   // connect signal to compute dialog. which will be shown after Compute mesh operation
1423   SMESHGUI_ComputeDlg* cmpDlg = computeDlg();
1424   if ( cmpDlg )
1425   {
1426     // disconnect signals
1427     disconnect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1428     disconnect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1429     disconnect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1430     disconnect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1431     disconnect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1432
1433     // connect signals
1434     if( cmpDlg->testButtonFlags( QtxDialog::OK ) )
1435       connect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1436     if( cmpDlg->testButtonFlags( QtxDialog::Apply ) )
1437       connect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1438     if( cmpDlg->testButtonFlags( QtxDialog::Help ) )
1439       connect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1440     if( cmpDlg->testButtonFlags( QtxDialog::Cancel ) )
1441       connect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1442     if( cmpDlg->testButtonFlags( QtxDialog::Close ) )
1443       connect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1444   }
1445
1446   SMESHGUI_BaseComputeOp::startOperation();
1447   if (myMesh->_is_nil())
1448     return;
1449
1450   if (myDlg->getPreviewMode() == -1)
1451   {
1452     // nothing to preview
1453     SUIT_MessageBox::warning(desktop(),
1454                              tr("SMESH_WRN_WARNING"),
1455                              tr("SMESH_WRN_NOTHING_PREVIEW"));
1456     onCancel();
1457     return;
1458   }
1459
1460   // disconnect slot from preview dialog to have Apply from results of compute operation only 
1461   disconnect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1462   disconnect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1463
1464   myDlg->show();
1465 }
1466
1467 //================================================================================
1468 /*!
1469  * \brief Stops operation
1470  */
1471 //================================================================================
1472
1473 void SMESHGUI_PrecomputeOp::stopOperation()
1474 {
1475   if ( myPreviewDisplayer )
1476   {
1477     myPreviewDisplayer->SetVisibility( false );
1478     delete myPreviewDisplayer;
1479     myPreviewDisplayer = 0;
1480   }
1481   myMapShapeId.clear();
1482   SMESHGUI_BaseComputeOp::stopOperation();
1483 }
1484
1485 //================================================================================
1486 /*!
1487  * \brief reinitialize dialog after operaiton become active again
1488  */
1489 //================================================================================
1490
1491 void SMESHGUI_PrecomputeOp::resumeOperation()
1492 {
1493   if ( myActiveDlg == myDlg )
1494     initDialog();
1495   SMESHGUI_BaseComputeOp::resumeOperation();
1496 }
1497
1498 //================================================================================
1499 /*!
1500  * \brief perform it's intention action: reinitialise dialog
1501  */
1502 //================================================================================
1503
1504 void SMESHGUI_PrecomputeOp::initDialog()
1505 {
1506   QList<int> modes;
1507
1508   QMap<int, int> modeMap;
1509   _PTR(SObject)  pMesh = studyDS()->FindObjectID( myIObject->getEntry() );
1510   getAssignedAlgos( pMesh, modeMap );
1511   if ( modeMap.contains( SMESH::DIM_3D ) )
1512   {
1513     if ( modeMap.contains( SMESH::DIM_2D ) )
1514       modes.append( SMESH::DIM_2D );
1515     if ( modeMap.contains( SMESH::DIM_1D ) )
1516       modes.append( SMESH::DIM_1D );
1517   }
1518   else if ( modeMap.contains( SMESH::DIM_2D ) )
1519   {
1520     if ( modeMap.contains( SMESH::DIM_1D ) )
1521       modes.append( SMESH::DIM_1D );
1522   }
1523
1524   myOrderMgr = new SMESHGUI_MeshOrderMgr( myDlg->getMeshOrderBox() );
1525   myOrderMgr->SetMesh( myMesh );
1526   bool isOrder = myOrderMgr->GetMeshOrder(myPrevOrder);
1527   myDlg->getMeshOrderBox()->setShown(isOrder);
1528   if ( !isOrder ) {
1529     delete myOrderMgr;
1530     myOrderMgr = 0;
1531   }
1532
1533   myDlg->setPreviewModes( modes );
1534 }
1535
1536 //================================================================================
1537 /*!
1538  * \brief detect asigned mesh algorithms
1539  */
1540 //================================================================================
1541
1542 void SMESHGUI_PrecomputeOp::getAssignedAlgos(_PTR(SObject) theMesh,
1543                                              QMap<int,int>& theModeMap)
1544 {
1545   _PTR(SObject)          aHypRoot;
1546   _PTR(GenericAttribute) anAttr;
1547   int aPart = SMESH::Tag_RefOnAppliedAlgorithms;
1548   if ( theMesh && theMesh->FindSubObject( aPart, aHypRoot ) )
1549   {
1550     _PTR(ChildIterator) anIter =
1551       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
1552     for ( ; anIter->More(); anIter->Next() )
1553     {
1554       _PTR(SObject) anObj = anIter->Value();
1555       _PTR(SObject) aRefObj;
1556       if ( anObj->ReferencedObject( aRefObj ) )
1557         anObj = aRefObj;
1558       else
1559         continue;
1560       
1561       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
1562       {
1563         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
1564         if ( CORBA::is_nil( aVar ) )
1565           continue;
1566         
1567         for( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1568         {
1569           SMESH::SMESH_Algo_var algo;
1570           switch(dim) {
1571           case SMESH::DIM_1D: algo = SMESH::SMESH_1D_Algo::_narrow( aVar ); break;
1572           case SMESH::DIM_2D: algo = SMESH::SMESH_2D_Algo::_narrow( aVar ); break;
1573           case SMESH::DIM_3D: algo = SMESH::SMESH_3D_Algo::_narrow( aVar ); break;
1574           default: break;
1575           }
1576           if ( !algo->_is_nil() )
1577             theModeMap[ dim ] = 0;
1578         }
1579       }
1580     }
1581   }
1582 }
1583
1584 //================================================================================
1585 /*!
1586  * \brief perform it's intention action: compute mesh
1587  */
1588 //================================================================================
1589
1590 void SMESHGUI_PrecomputeOp::onCompute()
1591 {
1592   myDlg->hide();
1593   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1594     myOrderMgr->SetMeshOrder();
1595   myMapShapeId.clear();
1596   myActiveDlg = computeDlg();
1597   computeMesh();
1598 }
1599
1600 //================================================================================
1601 /*!
1602  * \brief perform it's intention action: compute mesh
1603  */
1604 //================================================================================
1605
1606 void SMESHGUI_PrecomputeOp::onCancel()
1607 {
1608   QObject* curDlg = sender();
1609   if ( curDlg == computeDlg() && myActiveDlg == myDlg )
1610   {
1611     // return from error messages
1612     myDlg->show();
1613     return;
1614   }
1615
1616   bool isRestoreOrder = false;
1617   if ( myActiveDlg == myDlg  && !myMesh->_is_nil() && myMapShapeId.count() )
1618   {
1619     // ask to remove already computed mesh elements
1620     if ( SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1621                                     tr( "CLEAR_SUBMESH_QUESTION" ),
1622                                     tr( "SMESH_BUT_DELETE" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0 )
1623     {
1624       // remove all submeshes for collected shapes
1625       QMap<int,int>::const_iterator it = myMapShapeId.constBegin();
1626       for ( ; it != myMapShapeId.constEnd(); ++it )
1627         myMesh->ClearSubMesh( *it );
1628       isRestoreOrder = true;
1629     }
1630   }
1631
1632   // return previous mesh order
1633   if (myOrderMgr && myOrderMgr->IsOrderChanged()) {
1634     if (!isRestoreOrder)
1635       isRestoreOrder = 
1636         (SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1637                                     tr( "SMESH_REJECT_MESH_ORDER" ),
1638                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0);
1639     if (isRestoreOrder)
1640       myOrderMgr->SetMeshOrder(myPrevOrder);
1641   }
1642
1643   delete myOrderMgr;
1644   myOrderMgr = 0;
1645
1646   myMapShapeId.clear();
1647   SMESHGUI_BaseComputeOp::onCancel();
1648 }
1649
1650 //================================================================================
1651 /*!
1652  * \brief perform it's intention action: preview mesh
1653  */
1654 //================================================================================
1655
1656 void SMESHGUI_PrecomputeOp::onPreview()
1657 {
1658   if ( !myDlg || myMesh->_is_nil() || myMainShape->_is_nil() )
1659     return;
1660
1661   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1662   if ( !aMeshSObj )
1663     return;
1664
1665   // set modified submesh priority if any
1666   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1667     myOrderMgr->SetMeshOrder();
1668
1669   // Compute preview of mesh, 
1670   // i.e. compute mesh till indicated dimension
1671   int dim = myDlg->getPreviewMode();
1672   
1673   SMESH::MemoryReserve aMemoryReserve;
1674   
1675   SMESH::compute_error_array_var aCompErrors;
1676   QString                        aHypErrors;
1677
1678   bool computeFailed = true, memoryLack = false;
1679
1680   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
1681     aCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1682
1683   SMESHGUI* gui = getSMESHGUI();
1684   SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1685   SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1686   if ( errors->length() > 0 ) {
1687     aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1688   }
1689
1690   SUIT_OverrideCursor aWaitCursor;
1691
1692   SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1693   if ( myPreviewDisplayer ) delete myPreviewDisplayer;
1694   myPreviewDisplayer = new SMESHGUI_MeshEditPreview( view );
1695   
1696   SMESH::long_array_var aShapesId = new SMESH::long_array();
1697   try {
1698 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1699     OCC_CATCH_SIGNALS;
1700 #endif
1701       
1702     SMESH::MeshPreviewStruct_var previewData =
1703       gen->Precompute(myMesh, myMainShape, (SMESH::Dimension)dim, aShapesId);
1704
1705     SMESH::MeshPreviewStruct* previewRes = previewData._retn();
1706     if ( previewRes && previewRes->nodesXYZ.length() > 0 )
1707     {
1708       computeFailed = false;
1709       myPreviewDisplayer->SetData( previewRes );
1710       // append shape indeces with computed mesh entities
1711       for ( int i = 0, n = aShapesId->length(); i < n; i++ )
1712         myMapShapeId[ aShapesId[ i ] ] = 0;
1713     }
1714     else
1715       myPreviewDisplayer->SetVisibility(false);
1716   }
1717   catch(const SALOME::SALOME_Exception & S_ex){
1718     memoryLack = true;
1719     myPreviewDisplayer->SetVisibility(false);
1720   }
1721
1722   try {
1723 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1724     OCC_CATCH_SIGNALS;
1725 #endif
1726     aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1727     // check if there are memory problems
1728     for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
1729       memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
1730   }
1731   catch(const SALOME::SALOME_Exception & S_ex){
1732     memoryLack = true;
1733   }
1734
1735   if ( memoryLack )
1736     aMemoryReserve.release();
1737
1738   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1739   bool noHypoError = ( aHypErrors.isEmpty() );
1740
1741   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( gui );
1742   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
1743
1744   bool isShowError = true;
1745   switch( aNotifyMode ) {
1746   case 0: // show the mesh computation result dialog NEVER
1747     isShowError = false;
1748     break;
1749   case 1: // show the mesh computation result dialog if there are some errors
1750   default: // show the result dialog after each mesh computation
1751     if ( !computeFailed && !memoryLack && noCompError && noHypoError )
1752       isShowError = false;
1753     break;
1754   }
1755
1756   aWaitCursor.suspend();
1757   // SHOW ERRORS
1758   if ( isShowError )
1759   {
1760     myDlg->hide();
1761     aCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
1762     showComputeResult( memoryLack, noCompError, aCompErrors, noHypoError, aHypErrors );
1763   }
1764 }
1765
1766
1767 //================================================================================
1768 /*!
1769  * \brief Constructor
1770 */
1771 //================================================================================
1772
1773 SMESHGUI_PrecomputeDlg::SMESHGUI_PrecomputeDlg( QWidget* parent )
1774  : SMESHGUI_Dialog( parent, false, false, OK | Cancel | Help ),
1775    myOrderBox(0)
1776 {
1777   setWindowTitle( tr( "CAPTION" ) );
1778
1779   setButtonText( OK, tr( "COMPUTE" ) );
1780   QFrame* main = mainFrame();
1781
1782   QVBoxLayout* layout = new QVBoxLayout( main );
1783
1784   myOrderBox = new SMESHGUI_MeshOrderBox( main );
1785   layout->addWidget(myOrderBox);
1786
1787   QFrame* frame = new QFrame( main );
1788   layout->setMargin(0); layout->setSpacing(0);
1789   layout->addWidget( frame );
1790
1791   QHBoxLayout* frameLay = new QHBoxLayout( frame );
1792   frameLay->setMargin(0); frameLay->setSpacing(SPACING);
1793   
1794   myPreviewMode = new QtxComboBox( frame );
1795   frameLay->addWidget( myPreviewMode );
1796
1797   myPreviewBtn = new QPushButton( tr( "PREVIEW" ), frame );
1798   frameLay->addWidget( myPreviewBtn );
1799
1800   connect( myPreviewBtn, SIGNAL( clicked( bool ) ), this, SIGNAL( preview() ) );
1801 }
1802
1803 //================================================================================
1804 /*!
1805  * \brief Destructor
1806 */
1807 //================================================================================
1808
1809 SMESHGUI_PrecomputeDlg::~SMESHGUI_PrecomputeDlg()
1810 {
1811 }
1812
1813 //================================================================================
1814 /*!
1815  * \brief Sets available preview modes
1816 */
1817 //================================================================================
1818
1819 void SMESHGUI_PrecomputeDlg::setPreviewModes( const QList<int>& theModes )
1820 {
1821   myPreviewMode->clear();
1822   QList<int>::const_iterator it = theModes.constBegin();
1823   for ( int i = 0; it != theModes.constEnd(); ++it, i++ )
1824   {
1825     QString mode = QString( "PREVIEW_%1" ).arg( *it );
1826     myPreviewMode->addItem( tr( mode.toLatin1().data() ) );
1827     myPreviewMode->setId( i, *it );
1828   }
1829   myPreviewBtn->setEnabled( !theModes.isEmpty() );
1830 }
1831
1832 //================================================================================
1833 /*!
1834  * \brief Returns current preview mesh mode
1835 */
1836 //================================================================================
1837
1838 int SMESHGUI_PrecomputeDlg::getPreviewMode() const
1839 {
1840   return myPreviewMode->currentId();
1841 }
1842
1843 //================================================================================
1844 /*!
1845  * \brief Returns current preview mesh mode
1846 */
1847 //================================================================================
1848
1849 SMESHGUI_MeshOrderBox* SMESHGUI_PrecomputeDlg::getMeshOrderBox() const
1850 {
1851   return myOrderBox;
1852 }
1853
1854
1855 //================================================================================
1856 /*!
1857  * \brief Constructor
1858 */
1859 //================================================================================
1860
1861 SMESHGUI_EvaluateOp::SMESHGUI_EvaluateOp()
1862  : SMESHGUI_BaseComputeOp()
1863 {
1864 }
1865
1866
1867 //================================================================================
1868 /*!
1869  * \brief Desctructor
1870 */
1871 //================================================================================
1872
1873 SMESHGUI_EvaluateOp::~SMESHGUI_EvaluateOp()
1874 {
1875 }
1876
1877 //================================================================================
1878 /*!
1879  * \brief perform it's intention action: compute mesh
1880  */
1881 //================================================================================
1882
1883 void SMESHGUI_EvaluateOp::startOperation()
1884 {
1885   SMESHGUI_BaseComputeOp::evaluateDlg();
1886   SMESHGUI_BaseComputeOp::startOperation();
1887   if (myMesh->_is_nil())
1888     return;
1889   evaluateMesh();
1890 }
1891
1892 //================================================================================
1893 /*!
1894  * \brief Gets dialog of this operation
1895  * \retval LightApp_Dialog* - pointer to dialog of this operation
1896  */
1897 //================================================================================
1898
1899 LightApp_Dialog* SMESHGUI_EvaluateOp::dlg() const
1900 {
1901   return evaluateDlg();
1902 }
1903
1904 //================================================================================
1905 /*!
1906  * \brief evaluateMesh()
1907 */
1908 //================================================================================
1909
1910 void SMESHGUI_BaseComputeOp::evaluateMesh()
1911 {
1912   // EVALUATE MESH
1913
1914   SMESH::MemoryReserve aMemoryReserve;
1915
1916   SMESH::compute_error_array_var aCompErrors;
1917   QString                        aHypErrors;
1918
1919   bool evaluateFailed = true, memoryLack = false;
1920   SMESH::long_array_var aRes;
1921
1922   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1923   if ( !aMeshSObj ) //  IPAL21340
1924     return;
1925
1926   bool hasShape = myMesh->HasShapeToMesh();
1927   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
1928   if ( shapeOK )
1929   {
1930     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1931     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
1932     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1933     if ( errors->length() > 0 ) {
1934       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1935     }
1936     SUIT_OverrideCursor aWaitCursor;
1937     try {
1938 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1939       OCC_CATCH_SIGNALS;
1940 #endif
1941       aRes = gen->Evaluate(myMesh, myMainShape);
1942     }
1943     catch(const SALOME::SALOME_Exception & S_ex){
1944       memoryLack = true;
1945     }
1946
1947     try {
1948 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1949       OCC_CATCH_SIGNALS;
1950 #endif
1951       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1952     }
1953     catch(const SALOME::SALOME_Exception & S_ex){
1954       memoryLack = true;
1955     }
1956   }
1957
1958   if ( memoryLack )
1959     aMemoryReserve.release();
1960
1961   evaluateFailed =  ( aCompErrors->length() > 0 );
1962   myCompDlg->setWindowTitle(tr( evaluateFailed ? "SMESH_WRN_EVALUATE_FAILED" : "SMESH_EVALUATE_SUCCEED"));
1963
1964   // SHOW ERRORS
1965   
1966   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1967   bool noHypoError = ( aHypErrors.isEmpty() );
1968
1969   //SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
1970   //int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
1971
1972   bool isShowResultDlg = true;
1973   //if( noHypoError )
1974   //switch( aNotifyMode ) {
1975   //case 0: // show the mesh computation result dialog NEVER
1976   //isShowResultDlg = false;
1977   //commit();
1978   //break;
1979   //case 1: // show the mesh computation result dialog if there are some errors
1980   //if ( memoryLack || !noHypoError )
1981   //  isShowResultDlg = true;
1982   //else
1983   //{
1984   //  isShowResultDlg = false;
1985   //  commit();
1986   //}
1987   //break;
1988   //default: // show the result dialog after each mesh computation
1989   //isShowResultDlg = true;
1990   //}
1991
1992   // SHOW RESULTS
1993   if ( isShowResultDlg )
1994     showEvaluateResult( aRes, memoryLack, noCompError, aCompErrors,
1995                         noHypoError, aHypErrors);
1996 }
1997
1998
1999 void SMESHGUI_BaseComputeOp::showEvaluateResult(const SMESH::long_array& theRes,
2000                                                 const bool theMemoryLack,
2001                                                 const bool theNoCompError,
2002                                                 SMESH::compute_error_array_var& theCompErrors,
2003                                                 const bool theNoHypoError,
2004                                                 const QString& theHypErrors)
2005 {
2006   bool hasShape = myMesh->HasShapeToMesh();
2007   SMESHGUI_ComputeDlg* aCompDlg = evaluateDlg();
2008   aCompDlg->myMemoryLackGroup->hide();
2009
2010   if ( theMemoryLack )
2011   {
2012     aCompDlg->myMemoryLackGroup->show();
2013     aCompDlg->myFullInfo->hide();
2014     aCompDlg->myBriefInfo->hide();
2015     aCompDlg->myHypErrorGroup->hide();
2016     aCompDlg->myCompErrorGroup->hide();
2017   }
2018   else if ( theNoCompError && theNoHypoError )
2019   {
2020     aCompDlg->myFullInfo->SetMeshInfo( theRes );
2021     aCompDlg->myFullInfo->show();
2022     aCompDlg->myBriefInfo->hide();
2023     aCompDlg->myHypErrorGroup->hide();
2024     aCompDlg->myCompErrorGroup->hide();
2025   }
2026   else
2027   {
2028     QTableWidget* tbl = aCompDlg->myTable;
2029     aCompDlg->myBriefInfo->SetMeshInfo( theRes );
2030     aCompDlg->myBriefInfo->show();
2031     aCompDlg->myFullInfo->hide();
2032
2033     if ( theNoHypoError ) {
2034       aCompDlg->myHypErrorGroup->hide();
2035     }
2036     else {
2037       aCompDlg->myHypErrorGroup->show();
2038       aCompDlg->myHypErrorLabel->setText( theHypErrors );
2039     }
2040
2041     if ( theNoCompError ) {
2042       aCompDlg->myCompErrorGroup->hide();
2043     }
2044     else {
2045       aCompDlg->myCompErrorGroup->show();
2046
2047       aCompDlg->myPublishBtn->hide();
2048       aCompDlg->myShowBtn->hide();
2049
2050       // fill table of errors
2051       tbl->setRowCount( theCompErrors->length() );
2052       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
2053       else             tbl->showColumn( COL_SHAPE );
2054       tbl->setColumnWidth( COL_ERROR, 200 );
2055
2056       bool hasBadMesh = false;
2057       for ( int row = 0; row < theCompErrors->length(); ++row )
2058       {
2059         SMESH::ComputeError & err = theCompErrors[ row ];
2060
2061         QString text = err.algoName.in();
2062         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
2063         else tbl->item( row, COL_ALGO )->setText( text );
2064
2065         text = SMESH::errorText( err.code, err.comment.in() );
2066         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
2067         else tbl->item( row, COL_ERROR )->setText( text );
2068
2069         text = QString("%1").arg( err.subShapeID );
2070         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
2071         else tbl->item( row, COL_SHAPEID )->setText( text );
2072
2073         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
2074         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
2075         else tbl->item( row, COL_SHAPE )->setText( text );
2076
2077         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
2078         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
2079         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
2080
2081         text = err.hasBadMesh ? "hasBadMesh" : "";
2082         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
2083         else tbl->item( row, COL_BAD_MESH )->setText( text );
2084         if ( err.hasBadMesh ) hasBadMesh = true;
2085
2086         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
2087         tbl->resizeRowToContents( row );
2088       }
2089       tbl->resizeColumnToContents( COL_ALGO );
2090       tbl->resizeColumnToContents( COL_SHAPE );
2091       tbl->setWordWrap( true );
2092
2093       if ( hasBadMesh )
2094         aCompDlg->myBadMeshBtn->show();
2095       else
2096         aCompDlg->myBadMeshBtn->hide();
2097
2098       tbl->setCurrentCell(0,0);
2099       currentCellChanged(); // to update buttons
2100     }
2101   }
2102   // show dialog and wait, becase Compute can be invoked from Preview operation
2103   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
2104   aCompDlg->show();
2105 }
2106
2107
2108 //================================================================================
2109 /*!
2110  * \brief Gets dialog of evaluate operation
2111  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
2112  */
2113 //================================================================================
2114
2115 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::evaluateDlg() const
2116 {
2117   if ( !myCompDlg )
2118   {
2119     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
2120     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), true );
2121     // connect signals and slots
2122     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
2123     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
2124     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
2125     QTableWidget* aTable = me->table();
2126     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
2127     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
2128   }
2129   return myCompDlg;
2130 }
2131