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