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