Salome HOME
indentation
[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       bool res;
836       SMESHGUI_ComputeDlg_QThreadQDialog qthreaddialog(desktop(), gen, myMesh, myMainShape);
837       qthreaddialog.exec();
838       res = qthreaddialog.result();
839       res = gen->Compute(myMesh, myMainShape);
840       if (res)
841         computeFailed = false;
842     }
843     catch(const SALOME::SALOME_Exception & S_ex) {
844       memoryLack = true;
845     }
846     try {
847 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
848       OCC_CATCH_SIGNALS;
849 #endif
850       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
851       // check if there are memory problems
852       for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
853         memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
854     }
855     catch(const SALOME::SALOME_Exception & S_ex) {
856       memoryLack = true;
857     }
858
859     if ( !memoryLack && !SMDS_Mesh::CheckMemory(true) ) { // has memory to show dialog boxes?
860       memoryLack = true;
861     }
862
863     // NPAL16631: if ( !memoryLack )
864     {
865       SMESH::ModifiedMesh(aMeshSObj, !computeFailed, myMesh->NbNodes() == 0);
866       update( UF_ObjBrowser | UF_Model );
867
868       // SHOW MESH
869       // NPAL16631: if ( getSMESHGUI()->automaticUpdate() )
870       SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
871       long newSize = myMesh->NbElements();
872       bool limitExceeded;
873       if ( !memoryLack )
874       {
875         if ( getSMESHGUI()->automaticUpdate( newSize, &limitExceeded ) )
876         {
877           try {
878 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
879             OCC_CATCH_SIGNALS;
880 #endif
881             SMESH::Update(myIObject, true);
882           }
883           catch (...) {
884 #ifdef _DEBUG_
885             MESSAGE ( "Exception thrown during mesh visualization" );
886 #endif
887             if ( SMDS_Mesh::CheckMemory(true) ) { // has memory to show warning?
888               SMESH::OnVisuException();
889             }
890             else {
891               memoryLack = true;
892             }
893           }
894         }
895         else if ( limitExceeded )
896         {
897           long limitSize = resMgr->integerValue( "SMESH", "update_limit", 500000 );
898           SUIT_MessageBox::warning( desktop(),
899                                     tr( "SMESH_WRN_WARNING" ),
900                                     tr( "SMESH_WRN_SIZE_LIMIT_EXCEEDED" ).arg( newSize ).arg( limitSize ) );
901         }
902       }
903       LightApp_SelectionMgr *Sel = selectionMgr();
904       if ( Sel )
905       {
906         SALOME_ListIO selected;
907         selected.Append( myIObject );
908         Sel->setSelectedObjects( selected );
909       }
910     }
911   }
912
913   if ( memoryLack )
914     aMemoryReserve.release();
915
916   myCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
917
918   // SHOW ERRORS
919   
920   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
921   bool noHypoError = ( aHypErrors.isEmpty() );
922
923   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
924   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
925
926   bool isShowResultDlg = true;
927   switch( aNotifyMode ) {
928   case 0: // show the mesh computation result dialog NEVER
929     isShowResultDlg = false;
930     commit();
931     break;
932   case 1: // show the mesh computation result dialog if there are some errors
933     if ( memoryLack || !noCompError || !noHypoError )
934       isShowResultDlg = true;
935     else
936     {
937       isShowResultDlg = false;
938       commit();
939     }
940     break;
941   default: // show the result dialog after each mesh computation
942     isShowResultDlg = true;
943   }
944
945   // SHOW RESULTS
946   if ( isShowResultDlg )
947     showComputeResult( memoryLack, noCompError,aCompErrors, noHypoError, aHypErrors );
948 }
949
950 void SMESHGUI_BaseComputeOp::showComputeResult( const bool theMemoryLack,
951                                                 const bool theNoCompError,
952                                                 SMESH::compute_error_array_var& theCompErrors,
953                                                 const bool theNoHypoError,
954                                                 const QString& theHypErrors )
955 {
956   bool hasShape = myMesh->HasShapeToMesh();
957   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
958   aCompDlg->myMemoryLackGroup->hide();
959
960   if ( theMemoryLack )
961   {
962     aCompDlg->myMemoryLackGroup->show();
963     aCompDlg->myFullInfo->hide();
964     aCompDlg->myBriefInfo->hide();
965     aCompDlg->myHypErrorGroup->hide();
966     aCompDlg->myCompErrorGroup->hide();
967   }
968   else if ( theNoCompError && theNoHypoError )
969   {
970     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
971     aCompDlg->myFullInfo->SetMeshInfo( aRes );
972     aCompDlg->myFullInfo->show();
973     aCompDlg->myBriefInfo->hide();
974     aCompDlg->myHypErrorGroup->hide();
975     aCompDlg->myCompErrorGroup->hide();
976   }
977   else
978   {
979     bool onlyWarnings = !theNoCompError; // == valid mesh computed but there are errors reported
980     for ( int i = 0; i < theCompErrors->length() && onlyWarnings; ++i )
981       onlyWarnings = ( theCompErrors[ i ].code == SMESH::COMPERR_WARNING ||
982                        theCompErrors[ i ].code == SMESH::COMPERR_NO_MESH_ON_SHAPE );
983
984     // full or brief mesh info
985     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
986     if ( onlyWarnings ) {
987       aCompDlg->myFullInfo->SetMeshInfo( aRes );
988       aCompDlg->myFullInfo->show();
989       aCompDlg->myBriefInfo->hide();
990     } else {
991       aCompDlg->myBriefInfo->SetMeshInfo( aRes );
992       aCompDlg->myBriefInfo->show();
993       aCompDlg->myFullInfo->hide();
994     }
995
996     // pbs of hypo dfinitions
997     if ( theNoHypoError ) {
998       aCompDlg->myHypErrorGroup->hide();
999     } else {
1000       aCompDlg->myHypErrorGroup->show();
1001       aCompDlg->myHypErrorLabel->setText( theHypErrors );
1002     }
1003
1004     // table of errors
1005     if ( theNoCompError )
1006     {
1007       aCompDlg->myCompErrorGroup->hide();
1008     }
1009     else
1010     {
1011       aCompDlg->myCompErrorGroup->show();
1012
1013       if ( onlyWarnings )
1014         aCompDlg->myWarningLabel->show();
1015       else
1016         aCompDlg->myWarningLabel->hide();
1017
1018       if ( !hasShape ) {
1019         aCompDlg->myPublishBtn->hide();
1020         aCompDlg->myShowBtn->hide();
1021       }
1022       else {
1023         aCompDlg->myPublishBtn->show();
1024         aCompDlg->myShowBtn->show();
1025       }
1026
1027       // fill table of errors
1028       QTableWidget* tbl = aCompDlg->myTable;
1029       tbl->setRowCount( theCompErrors->length() );
1030       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
1031       else             tbl->showColumn( COL_SHAPE );
1032       tbl->setColumnWidth( COL_ERROR, 200 );
1033
1034       bool hasBadMesh = false;
1035       for ( int row = 0; row < theCompErrors->length(); ++row )
1036       {
1037         SMESH::ComputeError & err = theCompErrors[ row ];
1038
1039         QString text = err.algoName.in();
1040         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
1041         else tbl->item( row, COL_ALGO )->setText( text );
1042
1043         text = SMESH::errorText( err.code, err.comment.in() );
1044         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
1045         else tbl->item( row, COL_ERROR )->setText( text );
1046
1047         text = QString("%1").arg( err.subShapeID );
1048         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
1049         else tbl->item( row, COL_SHAPEID )->setText( text );
1050
1051         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
1052         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
1053         else tbl->item( row, COL_SHAPE )->setText( text );
1054
1055         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
1056         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
1057         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
1058
1059         text = err.hasBadMesh ? "hasBadMesh" : "";
1060         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
1061         else tbl->item( row, COL_BAD_MESH )->setText( text );
1062         if ( err.hasBadMesh ) hasBadMesh = true;
1063
1064         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
1065         tbl->resizeRowToContents( row );
1066       }
1067       tbl->resizeColumnToContents( COL_ALGO );
1068       tbl->resizeColumnToContents( COL_SHAPE );
1069       tbl->setWordWrap( true );
1070
1071       if ( hasBadMesh )
1072         aCompDlg->myBadMeshBtn->show();
1073       else
1074         aCompDlg->myBadMeshBtn->hide();
1075
1076       tbl->setCurrentCell(0,0);
1077       currentCellChanged(); // to update buttons
1078     }
1079   }
1080   // show dialog and wait, becase Compute can be invoked from Preview operation
1081   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
1082   aCompDlg->show();
1083 }
1084
1085 //================================================================================
1086 /*!
1087  * \brief Stops operation
1088  */
1089 //================================================================================
1090
1091 void SMESHGUI_BaseComputeOp::stopOperation()
1092 {
1093   SMESHGUI_Operation::stopOperation();
1094   if ( myTShapeDisplayer )
1095     myTShapeDisplayer->SetVisibility( false );
1096   if ( myBadMeshDisplayer ) {
1097     myBadMeshDisplayer->SetVisibility( false );
1098     // delete it in order not to have problems at its destruction when the viewer
1099     // where it worked is dead due to e.g. study closing
1100     delete myBadMeshDisplayer;
1101     myBadMeshDisplayer = 0;
1102   }
1103   myIObject.Nullify();
1104 }
1105
1106 //================================================================================
1107 /*!
1108  * \brief publish selected sub-shape
1109  */
1110 //================================================================================
1111
1112 void SMESHGUI_BaseComputeOp::onPublishShape()
1113 {
1114   GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1115   SALOMEDS::Study_var study = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
1116
1117   QList<int> rows;
1118   SMESH::getSelectedRows( table(), rows );
1119   int row;
1120   foreach ( row, rows )
1121   {
1122     int curSub = table()->item(row, COL_SHAPEID)->text().toInt();
1123     GEOM::GEOM_Object_wrap shape = SMESH::getSubShape( curSub, myMainShape );
1124     if ( !shape->_is_nil() && ! SMESH::getSubShapeSO( curSub, myMainShape ))
1125     {
1126       if ( !SMESH::getSubShapeSO( 1, myMainShape )) // the main shape not published
1127       {
1128         QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( myMainShape, "MAIN_SHAPE" ));
1129         SALOMEDS::SObject_wrap so =
1130           geomGen->AddInStudy( study, myMainShape, name.toLatin1().data(), GEOM::GEOM_Object::_nil());
1131         // look for myMainShape in the table
1132         for ( int r = 0, nr = table()->rowCount(); r < nr; ++r ) {
1133           if ( table()->item( r, COL_SHAPEID )->text() == "1" ) {
1134             if ( so->_is_nil() ) {
1135               CORBA::String_var name  = so->GetName();
1136               CORBA::String_var entry = so->GetID();
1137               table()->item( r, COL_SHAPE     )->setText( name.in() );
1138               table()->item( r, COL_PUBLISHED )->setText( entry.in() );
1139             }
1140             break;
1141           }
1142         }
1143         if ( curSub == 1 ) continue;
1144       }
1145       QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( shape, "ERROR_SHAPE" ));
1146       SALOMEDS::SObject_wrap so = geomGen->AddInStudy( study, shape, name.toLatin1().data(), myMainShape);
1147       if ( !so->_is_nil() ) {
1148         CORBA::String_var name  = so->GetName();
1149         CORBA::String_var entry = so->GetID();
1150         table()->item( row, COL_SHAPE     )->setText( name.in() );
1151         table()->item( row, COL_PUBLISHED )->setText( entry.in() );
1152       }
1153     }
1154   }
1155   getSMESHGUI()->getApp()->updateObjectBrowser();
1156   currentCellChanged(); // to update buttons
1157 }
1158
1159 //================================================================================
1160 /*!
1161  * \brief show mesh elements preventing computation of a submesh of current row
1162  */
1163 //================================================================================
1164
1165 void SMESHGUI_BaseComputeOp::onShowBadMesh()
1166 {
1167   myTShapeDisplayer->SetVisibility( false );
1168   QList<int> rows;
1169   if ( SMESH::getSelectedRows( table(), rows ) == 1 ) {
1170     bool hasBadMesh = ( !table()->item(rows.front(), COL_BAD_MESH)->text().isEmpty() );
1171     if ( hasBadMesh ) {
1172       int curSub = table()->item(rows.front(), COL_SHAPEID)->text().toInt();
1173       SMESHGUI* gui = getSMESHGUI();
1174       SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1175       SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1176       if ( myBadMeshDisplayer ) delete myBadMeshDisplayer;
1177       myBadMeshDisplayer = new SMESHGUI_MeshEditPreview( view );
1178       SMESH::MeshPreviewStruct_var aMeshData = gen->GetBadInputElements(myMesh,curSub);
1179       double aPointSize = SMESH::GetFloat("SMESH:node_size",3);
1180       double aLineWidth = SMESH::GetFloat("SMESH:element_width",1);
1181       vtkProperty* prop = vtkProperty::New();
1182       prop->SetLineWidth( aLineWidth * 3 );
1183       prop->SetPointSize( aPointSize * 3 );
1184       prop->SetColor( 250, 0, 250 );
1185       myBadMeshDisplayer->GetActor()->SetProperty( prop );
1186       myBadMeshDisplayer->SetData( aMeshData._retn() );
1187       prop->Delete();
1188     }
1189   }
1190 }
1191
1192 //================================================================================
1193 /*!
1194  * \brief SLOT called when a selected cell in table() changed
1195  */
1196 //================================================================================
1197
1198 void SMESHGUI_BaseComputeOp::currentCellChanged()
1199 {
1200   myTShapeDisplayer->SetVisibility( false );
1201   if ( myBadMeshDisplayer )
1202     myBadMeshDisplayer->SetVisibility( false );
1203
1204   bool publishEnable = 0, showEnable = 0, showOnly = 1, hasBadMesh = 0;
1205   QList<int> rows;
1206   int nbSelected = SMESH::getSelectedRows( table(), rows );
1207   int row;
1208   foreach ( row, rows )
1209   {
1210     bool hasData     = ( !table()->item( row, COL_SHAPE )->text().isEmpty() );
1211     bool isPublished = ( !table()->item( row, COL_PUBLISHED )->text().isEmpty() );
1212     if ( hasData && !isPublished )
1213       publishEnable = true;
1214
1215     int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1216     bool prsReady = myTShapeDisplayer->HasReadyActorsFor( curSub, myMainShape );
1217     if ( prsReady ) {
1218       myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1219       showOnly = false;
1220     }
1221     else {
1222       showEnable = true;
1223     }
1224
1225     if ( !table()->item(row, COL_BAD_MESH)->text().isEmpty() )
1226       hasBadMesh = true;
1227   }
1228   myCompDlg->myPublishBtn->setEnabled( publishEnable );
1229   myCompDlg->myShowBtn   ->setEnabled( showEnable );
1230   myCompDlg->myBadMeshBtn->setEnabled( hasBadMesh && ( nbSelected == 1 ));
1231 }
1232
1233 //================================================================================
1234 /*!
1235  * \brief update preview
1236  */
1237 //================================================================================
1238
1239 void SMESHGUI_BaseComputeOp::onPreviewShape()
1240 {
1241   if ( myTShapeDisplayer )
1242   {
1243     SUIT_OverrideCursor aWaitCursor;
1244     QList<int> rows;
1245     SMESH::getSelectedRows( table(), rows );
1246
1247     bool showOnly = true;
1248     int row;
1249     foreach ( row, rows )
1250     {
1251       int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1252       if ( curSub > 0 ) {
1253         myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1254         showOnly = false;
1255       }
1256     }
1257     currentCellChanged(); // to update buttons
1258   }
1259 }
1260
1261 //================================================================================
1262 /*!
1263  * \brief Destructor
1264  */
1265 //================================================================================
1266
1267 SMESHGUI_BaseComputeOp::~SMESHGUI_BaseComputeOp()
1268 {
1269   delete myCompDlg;
1270   myCompDlg = 0;
1271   delete myTShapeDisplayer;
1272   if ( myBadMeshDisplayer )
1273     delete myBadMeshDisplayer;
1274 }
1275
1276 //================================================================================
1277 /*!
1278  * \brief Gets dialog of compute operation
1279  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
1280  */
1281 //================================================================================
1282
1283 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::computeDlg() const
1284 {
1285   if ( !myCompDlg )
1286   {
1287     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
1288     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), false );
1289     // connect signals and slots
1290     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
1291     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
1292     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
1293
1294     QTableWidget* aTable = me->table();
1295     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
1296     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
1297   }
1298   return myCompDlg;
1299 }
1300
1301 //================================================================================
1302 /*!
1303  * \brief returns from compute mesh result dialog
1304  */
1305 //================================================================================
1306
1307 bool SMESHGUI_BaseComputeOp::onApply()
1308 {
1309   return true;
1310 }
1311
1312 //================================================================================
1313 /*!
1314  * \brief Return a table
1315  */
1316 //================================================================================
1317
1318 QTableWidget* SMESHGUI_BaseComputeOp::table()
1319 {
1320   return myCompDlg->myTable;
1321 }
1322
1323
1324 //================================================================================
1325 /*!
1326  * \brief Constructor
1327 */
1328 //================================================================================
1329
1330 SMESHGUI_ComputeOp::SMESHGUI_ComputeOp()
1331  : SMESHGUI_BaseComputeOp()
1332 {
1333 }
1334
1335
1336 //================================================================================
1337 /*!
1338  * \brief Desctructor
1339 */
1340 //================================================================================
1341
1342 SMESHGUI_ComputeOp::~SMESHGUI_ComputeOp()
1343 {
1344 }
1345
1346 //================================================================================
1347 /*!
1348  * \brief perform it's intention action: compute mesh
1349  */
1350 //================================================================================
1351
1352 void SMESHGUI_ComputeOp::startOperation()
1353 {
1354   SMESHGUI_BaseComputeOp::startOperation();
1355   if (myMesh->_is_nil())
1356     return;
1357   computeMesh();
1358 }
1359
1360 //================================================================================
1361 /*!
1362  * \brief check the same operations on the same mesh
1363  */
1364 //================================================================================
1365
1366 bool SMESHGUI_BaseComputeOp::isValid(  SUIT_Operation* theOp  ) const
1367 {
1368   SMESHGUI_BaseComputeOp* baseOp = dynamic_cast<SMESHGUI_BaseComputeOp*>( theOp );
1369   bool ret = true;
1370   if ( !myMesh->_is_nil() && baseOp ) {
1371     SMESH::SMESH_Mesh_var aMesh = baseOp->getMesh();
1372     if ( !aMesh->_is_nil() && aMesh->GetId() == myMesh->GetId() ) ret = false;
1373   }
1374   return ret;
1375 }
1376
1377 //================================================================================
1378 /*!
1379  * \brief Gets dialog of this operation
1380  * \retval LightApp_Dialog* - pointer to dialog of this operation
1381  */
1382 //================================================================================
1383
1384 LightApp_Dialog* SMESHGUI_ComputeOp::dlg() const
1385 {
1386   return computeDlg();
1387 }
1388
1389 //================================================================================
1390 /*!
1391  * \brief Constructor
1392 */
1393 //================================================================================
1394
1395 SMESHGUI_PrecomputeOp::SMESHGUI_PrecomputeOp()
1396  : SMESHGUI_BaseComputeOp(),
1397  myDlg( 0 ),
1398  myOrderMgr( 0 ),
1399  myActiveDlg( 0 ),
1400  myPreviewDisplayer( 0 )
1401 {
1402   myHelpFileName = "constructing_meshes_page.html#preview_mesh_anchor";
1403 }
1404
1405 //================================================================================
1406 /*!
1407  * \brief Destructor
1408  */
1409 //================================================================================
1410
1411 SMESHGUI_PrecomputeOp::~SMESHGUI_PrecomputeOp()
1412 {
1413   delete myDlg;
1414   myDlg = 0;
1415   delete myOrderMgr;
1416   myOrderMgr = 0;
1417   myActiveDlg = 0;
1418   if ( myPreviewDisplayer )
1419     delete myPreviewDisplayer;
1420   myPreviewDisplayer = 0;
1421 }
1422
1423 //================================================================================
1424 /*!
1425  * \brief Gets current dialog of this operation
1426  * \retval LightApp_Dialog* - pointer to dialog of this operation
1427  */
1428 //================================================================================
1429
1430 LightApp_Dialog* SMESHGUI_PrecomputeOp::dlg() const
1431 {
1432   return myActiveDlg;
1433 }
1434
1435 //================================================================================
1436 /*!
1437  * \brief perform it's intention action: prepare data
1438  */
1439 //================================================================================
1440
1441 void SMESHGUI_PrecomputeOp::startOperation()
1442 {
1443   if ( !myDlg )
1444   {
1445     myDlg = new SMESHGUI_PrecomputeDlg( desktop() );
1446     
1447     // connect signals
1448     connect( myDlg, SIGNAL( preview() ), this, SLOT( onPreview() ) );
1449     connect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onCompute() ) );
1450     connect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onCompute() ) );
1451   }
1452   myActiveDlg = myDlg;
1453
1454   // connect signal to compute dialog. which will be shown after Compute mesh operation
1455   SMESHGUI_ComputeDlg* cmpDlg = computeDlg();
1456   if ( cmpDlg )
1457   {
1458     // disconnect signals
1459     disconnect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1460     disconnect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1461     disconnect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1462     disconnect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1463     disconnect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1464
1465     // connect signals
1466     if( cmpDlg->testButtonFlags( QtxDialog::OK ) )
1467       connect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1468     if( cmpDlg->testButtonFlags( QtxDialog::Apply ) )
1469       connect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1470     if( cmpDlg->testButtonFlags( QtxDialog::Help ) )
1471       connect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1472     if( cmpDlg->testButtonFlags( QtxDialog::Cancel ) )
1473       connect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1474     if( cmpDlg->testButtonFlags( QtxDialog::Close ) )
1475       connect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1476   }
1477
1478   SMESHGUI_BaseComputeOp::startOperation();
1479   if (myMesh->_is_nil())
1480     return;
1481
1482   if (myDlg->getPreviewMode() == -1)
1483   {
1484     // nothing to preview
1485     SUIT_MessageBox::warning(desktop(),
1486                              tr("SMESH_WRN_WARNING"),
1487                              tr("SMESH_WRN_NOTHING_PREVIEW"));
1488     onCancel();
1489     return;
1490   }
1491
1492   // disconnect slot from preview dialog to have Apply from results of compute operation only 
1493   disconnect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1494   disconnect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1495
1496   myDlg->show();
1497 }
1498
1499 //================================================================================
1500 /*!
1501  * \brief Stops operation
1502  */
1503 //================================================================================
1504
1505 void SMESHGUI_PrecomputeOp::stopOperation()
1506 {
1507   if ( myPreviewDisplayer )
1508   {
1509     myPreviewDisplayer->SetVisibility( false );
1510     delete myPreviewDisplayer;
1511     myPreviewDisplayer = 0;
1512   }
1513   myMapShapeId.clear();
1514   SMESHGUI_BaseComputeOp::stopOperation();
1515 }
1516
1517 //================================================================================
1518 /*!
1519  * \brief reinitialize dialog after operaiton become active again
1520  */
1521 //================================================================================
1522
1523 void SMESHGUI_PrecomputeOp::resumeOperation()
1524 {
1525   if ( myActiveDlg == myDlg )
1526     initDialog();
1527   SMESHGUI_BaseComputeOp::resumeOperation();
1528 }
1529
1530 //================================================================================
1531 /*!
1532  * \brief perform it's intention action: reinitialise dialog
1533  */
1534 //================================================================================
1535
1536 void SMESHGUI_PrecomputeOp::initDialog()
1537 {
1538   QList<int> modes;
1539
1540   QMap<int, int> modeMap;
1541   _PTR(SObject)  pMesh = studyDS()->FindObjectID( myIObject->getEntry() );
1542   getAssignedAlgos( pMesh, modeMap );
1543   if ( modeMap.contains( SMESH::DIM_3D ) )
1544   {
1545     if ( modeMap.contains( SMESH::DIM_2D ) )
1546       modes.append( SMESH::DIM_2D );
1547     if ( modeMap.contains( SMESH::DIM_1D ) )
1548       modes.append( SMESH::DIM_1D );
1549   }
1550   else if ( modeMap.contains( SMESH::DIM_2D ) )
1551   {
1552     if ( modeMap.contains( SMESH::DIM_1D ) )
1553       modes.append( SMESH::DIM_1D );
1554   }
1555
1556   myOrderMgr = new SMESHGUI_MeshOrderMgr( myDlg->getMeshOrderBox() );
1557   myOrderMgr->SetMesh( myMesh );
1558   bool isOrder = myOrderMgr->GetMeshOrder(myPrevOrder);
1559   myDlg->getMeshOrderBox()->setShown(isOrder);
1560   if ( !isOrder ) {
1561     delete myOrderMgr;
1562     myOrderMgr = 0;
1563   }
1564
1565   myDlg->setPreviewModes( modes );
1566 }
1567
1568 //================================================================================
1569 /*!
1570  * \brief detect asigned mesh algorithms
1571  */
1572 //================================================================================
1573
1574 void SMESHGUI_PrecomputeOp::getAssignedAlgos(_PTR(SObject) theMesh,
1575                                              QMap<int,int>& theModeMap)
1576 {
1577   _PTR(SObject)          aHypRoot;
1578   _PTR(GenericAttribute) anAttr;
1579   int aPart = SMESH::Tag_RefOnAppliedAlgorithms;
1580   if ( theMesh && theMesh->FindSubObject( aPart, aHypRoot ) )
1581   {
1582     _PTR(ChildIterator) anIter =
1583       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
1584     for ( ; anIter->More(); anIter->Next() )
1585     {
1586       _PTR(SObject) anObj = anIter->Value();
1587       _PTR(SObject) aRefObj;
1588       if ( anObj->ReferencedObject( aRefObj ) )
1589         anObj = aRefObj;
1590       else
1591         continue;
1592       
1593       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
1594       {
1595         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
1596         if ( CORBA::is_nil( aVar ) )
1597           continue;
1598         
1599         for( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1600         {
1601           SMESH::SMESH_Algo_var algo;
1602           switch(dim) {
1603           case SMESH::DIM_1D: algo = SMESH::SMESH_1D_Algo::_narrow( aVar ); break;
1604           case SMESH::DIM_2D: algo = SMESH::SMESH_2D_Algo::_narrow( aVar ); break;
1605           case SMESH::DIM_3D: algo = SMESH::SMESH_3D_Algo::_narrow( aVar ); break;
1606           default: break;
1607           }
1608           if ( !algo->_is_nil() )
1609             theModeMap[ dim ] = 0;
1610         }
1611       }
1612     }
1613   }
1614 }
1615
1616 //================================================================================
1617 /*!
1618  * \brief perform it's intention action: compute mesh
1619  */
1620 //================================================================================
1621
1622 void SMESHGUI_PrecomputeOp::onCompute()
1623 {
1624   myDlg->hide();
1625   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1626     myOrderMgr->SetMeshOrder();
1627   myMapShapeId.clear();
1628   myActiveDlg = computeDlg();
1629   computeMesh();
1630 }
1631
1632 //================================================================================
1633 /*!
1634  * \brief perform it's intention action: compute mesh
1635  */
1636 //================================================================================
1637
1638 void SMESHGUI_PrecomputeOp::onCancel()
1639 {
1640   QObject* curDlg = sender();
1641   if ( curDlg == computeDlg() && myActiveDlg == myDlg )
1642   {
1643     // return from error messages
1644     myDlg->show();
1645     return;
1646   }
1647
1648   bool isRestoreOrder = false;
1649   if ( myActiveDlg == myDlg  && !myMesh->_is_nil() && myMapShapeId.count() )
1650   {
1651     // ask to remove already computed mesh elements
1652     if ( SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1653                                     tr( "CLEAR_SUBMESH_QUESTION" ),
1654                                     tr( "SMESH_BUT_DELETE" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0 )
1655     {
1656       // remove all submeshes for collected shapes
1657       QMap<int,int>::const_iterator it = myMapShapeId.constBegin();
1658       for ( ; it != myMapShapeId.constEnd(); ++it )
1659         myMesh->ClearSubMesh( *it );
1660       isRestoreOrder = true;
1661     }
1662   }
1663
1664   // return previous mesh order
1665   if (myOrderMgr && myOrderMgr->IsOrderChanged()) {
1666     if (!isRestoreOrder)
1667       isRestoreOrder = 
1668         (SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1669                                     tr( "SMESH_REJECT_MESH_ORDER" ),
1670                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0);
1671     if (isRestoreOrder)
1672       myOrderMgr->SetMeshOrder(myPrevOrder);
1673   }
1674
1675   delete myOrderMgr;
1676   myOrderMgr = 0;
1677
1678   myMapShapeId.clear();
1679   SMESHGUI_BaseComputeOp::onCancel();
1680 }
1681
1682 //================================================================================
1683 /*!
1684  * \brief perform it's intention action: preview mesh
1685  */
1686 //================================================================================
1687
1688 void SMESHGUI_PrecomputeOp::onPreview()
1689 {
1690   if ( !myDlg || myMesh->_is_nil() || myMainShape->_is_nil() )
1691     return;
1692
1693   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1694   if ( !aMeshSObj )
1695     return;
1696
1697   // set modified submesh priority if any
1698   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1699     myOrderMgr->SetMeshOrder();
1700
1701   // Compute preview of mesh, 
1702   // i.e. compute mesh till indicated dimension
1703   int dim = myDlg->getPreviewMode();
1704   
1705   SMESH::MemoryReserve aMemoryReserve;
1706   
1707   SMESH::compute_error_array_var aCompErrors;
1708   QString                        aHypErrors;
1709
1710   bool computeFailed = true, memoryLack = false;
1711
1712   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
1713     aCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1714
1715   SMESHGUI* gui = getSMESHGUI();
1716   SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1717   SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1718   if ( errors->length() > 0 ) {
1719     aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1720   }
1721
1722   SUIT_OverrideCursor aWaitCursor;
1723
1724   SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1725   if ( myPreviewDisplayer ) delete myPreviewDisplayer;
1726   myPreviewDisplayer = new SMESHGUI_MeshEditPreview( view );
1727   
1728   SMESH::long_array_var aShapesId = new SMESH::long_array();
1729   try {
1730 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1731     OCC_CATCH_SIGNALS;
1732 #endif
1733       
1734     SMESH::MeshPreviewStruct_var previewData =
1735       gen->Precompute(myMesh, myMainShape, (SMESH::Dimension)dim, aShapesId);
1736
1737     SMESH::MeshPreviewStruct* previewRes = previewData._retn();
1738     if ( previewRes && previewRes->nodesXYZ.length() > 0 )
1739     {
1740       computeFailed = false;
1741       myPreviewDisplayer->SetData( previewRes );
1742       // append shape indeces with computed mesh entities
1743       for ( int i = 0, n = aShapesId->length(); i < n; i++ )
1744         myMapShapeId[ aShapesId[ i ] ] = 0;
1745     }
1746     else
1747       myPreviewDisplayer->SetVisibility(false);
1748   }
1749   catch(const SALOME::SALOME_Exception & S_ex){
1750     memoryLack = true;
1751     myPreviewDisplayer->SetVisibility(false);
1752   }
1753
1754   try {
1755 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1756     OCC_CATCH_SIGNALS;
1757 #endif
1758     aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1759     // check if there are memory problems
1760     for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
1761       memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
1762   }
1763   catch(const SALOME::SALOME_Exception & S_ex){
1764     memoryLack = true;
1765   }
1766
1767   if ( memoryLack )
1768     aMemoryReserve.release();
1769
1770   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1771   bool noHypoError = ( aHypErrors.isEmpty() );
1772
1773   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( gui );
1774   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
1775
1776   bool isShowError = true;
1777   switch( aNotifyMode ) {
1778   case 0: // show the mesh computation result dialog NEVER
1779     isShowError = false;
1780     break;
1781   case 1: // show the mesh computation result dialog if there are some errors
1782   default: // show the result dialog after each mesh computation
1783     if ( !computeFailed && !memoryLack && noCompError && noHypoError )
1784       isShowError = false;
1785     break;
1786   }
1787
1788   aWaitCursor.suspend();
1789   // SHOW ERRORS
1790   if ( isShowError )
1791   {
1792     myDlg->hide();
1793     aCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
1794     showComputeResult( memoryLack, noCompError, aCompErrors, noHypoError, aHypErrors );
1795   }
1796 }
1797
1798
1799 //================================================================================
1800 /*!
1801  * \brief Constructor
1802 */
1803 //================================================================================
1804
1805 SMESHGUI_PrecomputeDlg::SMESHGUI_PrecomputeDlg( QWidget* parent )
1806  : SMESHGUI_Dialog( parent, false, false, OK | Cancel | Help ),
1807    myOrderBox(0)
1808 {
1809   setWindowTitle( tr( "CAPTION" ) );
1810
1811   setButtonText( OK, tr( "COMPUTE" ) );
1812   QFrame* main = mainFrame();
1813
1814   QVBoxLayout* layout = new QVBoxLayout( main );
1815
1816   myOrderBox = new SMESHGUI_MeshOrderBox( main );
1817   layout->addWidget(myOrderBox);
1818
1819   QFrame* frame = new QFrame( main );
1820   layout->setMargin(0); layout->setSpacing(0);
1821   layout->addWidget( frame );
1822
1823   QHBoxLayout* frameLay = new QHBoxLayout( frame );
1824   frameLay->setMargin(0); frameLay->setSpacing(SPACING);
1825   
1826   myPreviewMode = new QtxComboBox( frame );
1827   frameLay->addWidget( myPreviewMode );
1828
1829   myPreviewBtn = new QPushButton( tr( "PREVIEW" ), frame );
1830   frameLay->addWidget( myPreviewBtn );
1831
1832   connect( myPreviewBtn, SIGNAL( clicked( bool ) ), this, SIGNAL( preview() ) );
1833 }
1834
1835 //================================================================================
1836 /*!
1837  * \brief Destructor
1838 */
1839 //================================================================================
1840
1841 SMESHGUI_PrecomputeDlg::~SMESHGUI_PrecomputeDlg()
1842 {
1843 }
1844
1845 //================================================================================
1846 /*!
1847  * \brief Sets available preview modes
1848 */
1849 //================================================================================
1850
1851 void SMESHGUI_PrecomputeDlg::setPreviewModes( const QList<int>& theModes )
1852 {
1853   myPreviewMode->clear();
1854   QList<int>::const_iterator it = theModes.constBegin();
1855   for ( int i = 0; it != theModes.constEnd(); ++it, i++ )
1856   {
1857     QString mode = QString( "PREVIEW_%1" ).arg( *it );
1858     myPreviewMode->addItem( tr( mode.toLatin1().data() ) );
1859     myPreviewMode->setId( i, *it );
1860   }
1861   myPreviewBtn->setEnabled( !theModes.isEmpty() );
1862 }
1863
1864 //================================================================================
1865 /*!
1866  * \brief Returns current preview mesh mode
1867 */
1868 //================================================================================
1869
1870 int SMESHGUI_PrecomputeDlg::getPreviewMode() const
1871 {
1872   return myPreviewMode->currentId();
1873 }
1874
1875 //================================================================================
1876 /*!
1877  * \brief Returns current preview mesh mode
1878 */
1879 //================================================================================
1880
1881 SMESHGUI_MeshOrderBox* SMESHGUI_PrecomputeDlg::getMeshOrderBox() const
1882 {
1883   return myOrderBox;
1884 }
1885
1886
1887 //================================================================================
1888 /*!
1889  * \brief Constructor
1890 */
1891 //================================================================================
1892
1893 SMESHGUI_EvaluateOp::SMESHGUI_EvaluateOp()
1894  : SMESHGUI_BaseComputeOp()
1895 {
1896 }
1897
1898
1899 //================================================================================
1900 /*!
1901  * \brief Desctructor
1902 */
1903 //================================================================================
1904
1905 SMESHGUI_EvaluateOp::~SMESHGUI_EvaluateOp()
1906 {
1907 }
1908
1909 //================================================================================
1910 /*!
1911  * \brief perform it's intention action: compute mesh
1912  */
1913 //================================================================================
1914
1915 void SMESHGUI_EvaluateOp::startOperation()
1916 {
1917   SMESHGUI_BaseComputeOp::evaluateDlg();
1918   SMESHGUI_BaseComputeOp::startOperation();
1919   if (myMesh->_is_nil())
1920     return;
1921   evaluateMesh();
1922 }
1923
1924 //================================================================================
1925 /*!
1926  * \brief Gets dialog of this operation
1927  * \retval LightApp_Dialog* - pointer to dialog of this operation
1928  */
1929 //================================================================================
1930
1931 LightApp_Dialog* SMESHGUI_EvaluateOp::dlg() const
1932 {
1933   return evaluateDlg();
1934 }
1935
1936 //================================================================================
1937 /*!
1938  * \brief evaluateMesh()
1939 */
1940 //================================================================================
1941
1942 void SMESHGUI_BaseComputeOp::evaluateMesh()
1943 {
1944   // EVALUATE MESH
1945
1946   SMESH::MemoryReserve aMemoryReserve;
1947
1948   SMESH::compute_error_array_var aCompErrors;
1949   QString                        aHypErrors;
1950
1951   bool evaluateFailed = true, memoryLack = false;
1952   SMESH::long_array_var aRes;
1953
1954   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1955   if ( !aMeshSObj ) //  IPAL21340
1956     return;
1957
1958   bool hasShape = myMesh->HasShapeToMesh();
1959   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
1960   if ( shapeOK )
1961   {
1962     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1963     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
1964     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1965     if ( errors->length() > 0 ) {
1966       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1967     }
1968     SUIT_OverrideCursor aWaitCursor;
1969     try {
1970 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1971       OCC_CATCH_SIGNALS;
1972 #endif
1973       aRes = gen->Evaluate(myMesh, myMainShape);
1974     }
1975     catch(const SALOME::SALOME_Exception & S_ex){
1976       memoryLack = true;
1977     }
1978
1979     try {
1980 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1981       OCC_CATCH_SIGNALS;
1982 #endif
1983       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1984     }
1985     catch(const SALOME::SALOME_Exception & S_ex){
1986       memoryLack = true;
1987     }
1988   }
1989
1990   if ( memoryLack )
1991     aMemoryReserve.release();
1992
1993   evaluateFailed =  ( aCompErrors->length() > 0 );
1994   myCompDlg->setWindowTitle(tr( evaluateFailed ? "SMESH_WRN_EVALUATE_FAILED" : "SMESH_EVALUATE_SUCCEED"));
1995
1996   // SHOW ERRORS
1997   
1998   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1999   bool noHypoError = ( aHypErrors.isEmpty() );
2000
2001   //SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
2002   //int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
2003
2004   bool isShowResultDlg = true;
2005   //if( noHypoError )
2006   //switch( aNotifyMode ) {
2007   //case 0: // show the mesh computation result dialog NEVER
2008   //isShowResultDlg = false;
2009   //commit();
2010   //break;
2011   //case 1: // show the mesh computation result dialog if there are some errors
2012   //if ( memoryLack || !noHypoError )
2013   //  isShowResultDlg = true;
2014   //else
2015   //{
2016   //  isShowResultDlg = false;
2017   //  commit();
2018   //}
2019   //break;
2020   //default: // show the result dialog after each mesh computation
2021   //isShowResultDlg = true;
2022   //}
2023
2024   // SHOW RESULTS
2025   if ( isShowResultDlg )
2026     showEvaluateResult( aRes, memoryLack, noCompError, aCompErrors,
2027                         noHypoError, aHypErrors);
2028 }
2029
2030
2031 void SMESHGUI_BaseComputeOp::showEvaluateResult(const SMESH::long_array& theRes,
2032                                                 const bool theMemoryLack,
2033                                                 const bool theNoCompError,
2034                                                 SMESH::compute_error_array_var& theCompErrors,
2035                                                 const bool theNoHypoError,
2036                                                 const QString& theHypErrors)
2037 {
2038   bool hasShape = myMesh->HasShapeToMesh();
2039   SMESHGUI_ComputeDlg* aCompDlg = evaluateDlg();
2040   aCompDlg->myMemoryLackGroup->hide();
2041
2042   if ( theMemoryLack )
2043   {
2044     aCompDlg->myMemoryLackGroup->show();
2045     aCompDlg->myFullInfo->hide();
2046     aCompDlg->myBriefInfo->hide();
2047     aCompDlg->myHypErrorGroup->hide();
2048     aCompDlg->myCompErrorGroup->hide();
2049   }
2050   else if ( theNoCompError && theNoHypoError )
2051   {
2052     aCompDlg->myFullInfo->SetMeshInfo( theRes );
2053     aCompDlg->myFullInfo->show();
2054     aCompDlg->myBriefInfo->hide();
2055     aCompDlg->myHypErrorGroup->hide();
2056     aCompDlg->myCompErrorGroup->hide();
2057   }
2058   else
2059   {
2060     QTableWidget* tbl = aCompDlg->myTable;
2061     aCompDlg->myBriefInfo->SetMeshInfo( theRes );
2062     aCompDlg->myBriefInfo->show();
2063     aCompDlg->myFullInfo->hide();
2064
2065     if ( theNoHypoError ) {
2066       aCompDlg->myHypErrorGroup->hide();
2067     }
2068     else {
2069       aCompDlg->myHypErrorGroup->show();
2070       aCompDlg->myHypErrorLabel->setText( theHypErrors );
2071     }
2072
2073     if ( theNoCompError ) {
2074       aCompDlg->myCompErrorGroup->hide();
2075     }
2076     else {
2077       aCompDlg->myCompErrorGroup->show();
2078
2079       aCompDlg->myPublishBtn->hide();
2080       aCompDlg->myShowBtn->hide();
2081
2082       // fill table of errors
2083       tbl->setRowCount( theCompErrors->length() );
2084       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
2085       else             tbl->showColumn( COL_SHAPE );
2086       tbl->setColumnWidth( COL_ERROR, 200 );
2087
2088       bool hasBadMesh = false;
2089       for ( int row = 0; row < theCompErrors->length(); ++row )
2090       {
2091         SMESH::ComputeError & err = theCompErrors[ row ];
2092
2093         QString text = err.algoName.in();
2094         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
2095         else tbl->item( row, COL_ALGO )->setText( text );
2096
2097         text = SMESH::errorText( err.code, err.comment.in() );
2098         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
2099         else tbl->item( row, COL_ERROR )->setText( text );
2100
2101         text = QString("%1").arg( err.subShapeID );
2102         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
2103         else tbl->item( row, COL_SHAPEID )->setText( text );
2104
2105         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
2106         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
2107         else tbl->item( row, COL_SHAPE )->setText( text );
2108
2109         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
2110         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
2111         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
2112
2113         text = err.hasBadMesh ? "hasBadMesh" : "";
2114         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
2115         else tbl->item( row, COL_BAD_MESH )->setText( text );
2116         if ( err.hasBadMesh ) hasBadMesh = true;
2117
2118         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
2119         tbl->resizeRowToContents( row );
2120       }
2121       tbl->resizeColumnToContents( COL_ALGO );
2122       tbl->resizeColumnToContents( COL_SHAPE );
2123       tbl->setWordWrap( true );
2124
2125       if ( hasBadMesh )
2126         aCompDlg->myBadMeshBtn->show();
2127       else
2128         aCompDlg->myBadMeshBtn->hide();
2129
2130       tbl->setCurrentCell(0,0);
2131       currentCellChanged(); // to update buttons
2132     }
2133   }
2134   // show dialog and wait, becase Compute can be invoked from Preview operation
2135   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
2136   aCompDlg->show();
2137 }
2138
2139
2140 //================================================================================
2141 /*!
2142  * \brief Gets dialog of evaluate operation
2143  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
2144  */
2145 //================================================================================
2146
2147 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::evaluateDlg() const
2148 {
2149   if ( !myCompDlg )
2150   {
2151     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
2152     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), true );
2153     // connect signals and slots
2154     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
2155     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
2156     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
2157     QTableWidget* aTable = me->table();
2158     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
2159     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
2160   }
2161   return myCompDlg;
2162 }
2163