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