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