Salome HOME
23586: [EDF] HYDRO: Copy mesh to new geometry
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CopyMeshDlg.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SMESHGUI_CopyMeshDlg.cxx
23
24 #include "SMESHGUI_CopyMeshDlg.h"
25
26 #include "SMESHGUI.h"
27 #include "SMESHGUI_FilterDlg.h"
28 #include "SMESHGUI_GEOMGenUtils.h"
29 #include "SMESHGUI_IdValidator.h"
30 #include "SMESHGUI_MeshUtils.h"
31 #include "SMESHGUI_Selection.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35
36 #include <SMESH_Actor.h>
37 #include <SMESH_TypeFilter.hxx>
38 #include <SMDS_Mesh.hxx>
39
40 // SALOME GUI includes
41 #include <LightApp_Application.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <SALOME_ListIO.hxx>
44 #include <SUIT_Desktop.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_OverrideCursor.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_Session.h>
49 #include <SVTK_ViewModel.h>
50 #include <SVTK_ViewWindow.h>
51 #include <SalomeApp_Tools.h>
52
53 // SALOME KERNEL includes
54 #include <SALOMEDSClient_Study.hxx>
55 #include <SALOMEDSClient_SObject.hxx>
56
57 // OCCT includes
58 #include <TColStd_MapOfInteger.hxx>
59
60 // Qt includes
61 #include <QApplication>
62 #include <QButtonGroup>
63 #include <QGroupBox>
64 #include <QLabel>
65 #include <QLineEdit>
66 #include <QPushButton>
67 #include <QRadioButton>
68 #include <QCheckBox>
69 #include <QHBoxLayout>
70 #include <QVBoxLayout>
71 #include <QGridLayout>
72 #include <QSpinBox>
73 #include <QKeyEvent>
74
75 // IDL includes
76 #include <SALOMEconfig.h>
77 #include CORBA_SERVER_HEADER(SMESH_Group)
78 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
79
80 /*!
81   \class BusyLocker
82   \brief Simple 'busy state' flag locker.
83   \internal
84 */
85
86 namespace
87 {
88   class BusyLocker
89   {
90   public:
91     //! Constructor. Sets passed boolean flag to \c true.
92     BusyLocker( bool& busy ) : myBusy( busy ) { myBusy = true; }
93     //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
94     ~BusyLocker() { myBusy = false; }
95   private:
96     bool& myBusy; //! External 'busy state' boolean flag
97   };
98 }
99
100 #define SPACING 6
101 #define MARGIN  11
102
103
104 //================================================================================
105 /*!
106  * \brief Constructor
107  */
108 //================================================================================
109
110 SMESHGUI_CopyMeshDlg::SMESHGUI_CopyMeshDlg( SMESHGUI* theModule )
111   : QDialog( SMESH::GetDesktop( theModule ) ),
112     mySMESHGUI( theModule ),
113     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
114     mySelectedObject(SMESH::SMESH_IDSource::_nil()),
115     myFilterDlg(0),
116     myIsApplyAndClose( false )
117 {
118   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_COPY_MESH")));
119   QPixmap image2 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_COPY_MESH_WG")));
120
121   setModal(false);
122   setAttribute(Qt::WA_DeleteOnClose, true);
123   setWindowTitle(tr("SMESH_COPY_MESH_TITLE"));
124   setSizeGripEnabled(true);
125
126   QVBoxLayout* SMESHGUI_CopyMeshDlgLayout = new QVBoxLayout(this);
127   SMESHGUI_CopyMeshDlgLayout->setSpacing(SPACING);
128   SMESHGUI_CopyMeshDlgLayout->setMargin(MARGIN);
129
130   /***************************************************************/
131   ConstructorsBox = new QGroupBox(tr("SMESH_COPY_MESH_TITLE"), this);
132   GroupConstructors = new QButtonGroup(this);
133   QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout(ConstructorsBox);
134   ConstructorsBoxLayout->setSpacing(SPACING);
135   ConstructorsBoxLayout->setMargin(MARGIN);
136
137   QRadioButton* RadioButton1= new QRadioButton(ConstructorsBox);
138   RadioButton1->setIcon(image1);
139   GroupConstructors->addButton(RadioButton1, 0);
140   QRadioButton* RadioButton2= new QRadioButton(ConstructorsBox);
141   RadioButton2->setIcon(image2);
142   GroupConstructors->addButton(RadioButton2, 1);
143
144   ConstructorsBoxLayout->addWidget(RadioButton1);
145   ConstructorsBoxLayout->addWidget(RadioButton2);
146   RadioButton1->setChecked(true);
147
148   /***************************************************************/
149   GroupArguments = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
150   QGridLayout* GroupArgumentsLayout = new QGridLayout(GroupArguments);
151   GroupArgumentsLayout->setSpacing(SPACING);
152   GroupArgumentsLayout->setMargin(MARGIN);
153
154   myIdValidator = new SMESHGUI_IdValidator(this);
155
156   // Controls for idSource/elements selection
157   myTextLabelElements = new QLabel(tr("OBJECT_NAME"), GroupArguments);
158   myLineEditElements = new QLineEdit(GroupArguments);
159   myLineEditElements->setValidator(myIdValidator);
160   myLineEditElements->setMaxLength(-1);
161   myFilterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupArguments );
162   connect(myFilterBtn,   SIGNAL(clicked()), this, SLOT(setFilters()));
163
164   // Control for the mesh objects selection
165   myIdSourceCheck = new QCheckBox(tr("SMESH_SELECT_WHOLE_MESH"), GroupArguments);
166
167   // Name of a mesh to create
168   QLabel* meshNameLabel = new QLabel(tr("NEW_NAME"), GroupArguments);
169   myMeshNameEdit = new QLineEdit(GroupArguments);
170
171   // CheckBox for copying groups
172   myCopyGroupsCheck = new QCheckBox(tr("SMESH_MAKE_GROUPS"), GroupArguments);
173   myCopyGroupsCheck->setChecked(true);
174
175   // CheckBox for keeping ids ( OBSOLETE )
176   myKeepIdsCheck = new QCheckBox(tr("SMESH_KEEP_IDS"), GroupArguments);
177   myKeepIdsCheck->setChecked(true);
178   myKeepIdsCheck->hide();
179
180   // New geometry
181   myGeomLabel = new QLabel( tr("NEW_GEOM"), GroupArguments );
182   myGeomNameEdit = new QLineEdit( GroupArguments );
183   myGeomNameEdit->setReadOnly(true);
184
185   // CheckBox to reuse hypotheses
186   myReuseHypCheck = new QCheckBox(tr("REUSE_HYPOTHESES"), GroupArguments);
187   myReuseHypCheck->setChecked(true);
188
189   // CheckBox to copy mesh elements
190   myCopyElementsCheck = new QCheckBox(tr("COPY_ELEMENTS"), GroupArguments);
191   myCopyElementsCheck->setChecked(true);
192
193   // layout
194   GroupArgumentsLayout->addWidget(myTextLabelElements,  0, 0);
195   GroupArgumentsLayout->addWidget(myLineEditElements,   0, 1, 1, 5);
196   GroupArgumentsLayout->addWidget(myFilterBtn,          0, 6);
197   GroupArgumentsLayout->addWidget(myIdSourceCheck,      1, 0, 1, 6);
198   GroupArgumentsLayout->addWidget(meshNameLabel,        2, 0);
199   GroupArgumentsLayout->addWidget(myMeshNameEdit,       2, 1, 1, 5);
200   GroupArgumentsLayout->addWidget(myGeomLabel,          3, 0);
201   GroupArgumentsLayout->addWidget(myGeomNameEdit,       3, 1, 1, 5);
202   GroupArgumentsLayout->addWidget(myCopyGroupsCheck,    4, 0, 1, 6);
203   GroupArgumentsLayout->addWidget(myReuseHypCheck,      5, 0, 1, 6);
204   GroupArgumentsLayout->addWidget(myCopyElementsCheck,  6, 0, 1, 6);
205   // GroupArgumentsLayout->addWidget(myKeepIdsCheck,       7, 0, 1, 6);
206   GroupArgumentsLayout->setRowStretch( 6, 1 );
207
208   /***************************************************************/
209   GroupButtons = new QGroupBox(this);
210   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
211   GroupButtonsLayout->setSpacing(SPACING);
212   GroupButtonsLayout->setMargin(MARGIN);
213
214   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
215   buttonOk->setAutoDefault(true);
216   buttonOk->setDefault(true);
217   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
218   buttonApply->setAutoDefault(true);
219   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
220   buttonCancel->setAutoDefault(true);
221   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
222   buttonHelp->setAutoDefault(true);
223
224   GroupButtonsLayout->addWidget(buttonOk);
225   GroupButtonsLayout->addSpacing(10);
226   GroupButtonsLayout->addWidget(buttonApply);
227   GroupButtonsLayout->addSpacing(10);
228   GroupButtonsLayout->addStretch();
229   GroupButtonsLayout->addWidget(buttonCancel);
230   GroupButtonsLayout->addWidget(buttonHelp);
231
232   /***************************************************************/
233   SMESHGUI_CopyMeshDlgLayout->addWidget(ConstructorsBox);
234   SMESHGUI_CopyMeshDlgLayout->addWidget(GroupArguments);
235   SMESHGUI_CopyMeshDlgLayout->addWidget(GroupButtons);
236
237   /* Initialisations */
238   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
239
240   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
241
242   // Selection filter
243   myIdSourceFilter = new SMESH_TypeFilter( SMESH::IDSOURCE );
244
245   myHelpFileName = "copy_mesh.html";
246
247   Init();
248
249   /* signals and slots connections */
250   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
251   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
252   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
253   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
254
255   connect(mySMESHGUI,     SIGNAL (SignalDeactivateActiveDialog()),
256           this,           SLOT   (DeactivateActiveDialog()));
257   connect(mySelectionMgr, SIGNAL (currentSelectionChanged()),
258           this,           SLOT   (SelectionIntoArgument()));
259   connect(mySMESHGUI,     SIGNAL (SignalCloseAllDialogs()),/* to close dialog if study change */
260           this,           SLOT   (reject()));
261   connect(mySMESHGUI,     SIGNAL (SignalActivatedViewManager()),
262           this,           SLOT   (onOpenView()));
263   connect(mySMESHGUI,     SIGNAL (SignalCloseView()),
264           this,           SLOT   (onCloseView()));
265
266   connect(myLineEditElements, SIGNAL(textChanged(const QString&)),
267           this,               SLOT  (onTextChange(const QString&)));
268   connect(myIdSourceCheck,    SIGNAL(toggled(bool)),
269           this,               SLOT  (onSelectIdSource(bool)));
270   connect(GroupConstructors,  SIGNAL(buttonClicked(int)),
271           this,               SLOT  (onConstructor(int)));
272
273
274   SelectionIntoArgument();
275 }
276
277 //=================================================================================
278 // function : ~SMESHGUI_CopyMeshDlg()
279 // purpose  : Destroys the object and frees any allocated resources
280 //=================================================================================
281
282 SMESHGUI_CopyMeshDlg::~SMESHGUI_CopyMeshDlg()
283 {
284   if ( myFilterDlg )
285   {
286     myFilterDlg->setParent( 0 );
287     delete myFilterDlg; myFilterDlg = 0;
288   }
289   if ( myIdSourceFilter )
290   {
291     if ( mySelectionMgr )
292       mySelectionMgr->removeFilter( myIdSourceFilter );
293     delete myIdSourceFilter; myIdSourceFilter=0;
294   }
295 }
296
297 //=================================================================================
298 // function : Init()
299 // purpose  :
300 //=================================================================================
301 void SMESHGUI_CopyMeshDlg::Init (bool ResetControls)
302 {
303   myBusy = false;
304
305   if ( !isWithGeomMode() )
306     myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
307
308   if ( ResetControls )
309   {
310     myLineEditElements->clear();
311     myNbOkElements = 0;
312
313     buttonOk->setEnabled(false);
314     buttonApply->setEnabled(false);
315
316     myActor = 0;
317     myMesh = SMESH::SMESH_Mesh::_nil();
318
319     myIdSourceCheck->setChecked(true);
320
321     onConstructor( 0 );
322   }
323 }
324
325 //=======================================================================
326 //function : onConstructor
327 //purpose  : switch operation mode
328 //=======================================================================
329
330 void SMESHGUI_CopyMeshDlg::onConstructor( int withGeom )
331 {
332   myGeomLabel        ->setVisible( withGeom );
333   myGeomNameEdit     ->setVisible( withGeom );
334   myReuseHypCheck    ->setVisible( withGeom );
335   myCopyElementsCheck->setVisible( withGeom );
336   myFilterBtn        ->setVisible( !withGeom );
337   myIdSourceCheck    ->setVisible( !withGeom );
338
339   if ( !withGeom )
340     myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
341
342   onSelectIdSource( /*toSelectMesh=*/ myIdSourceCheck->isChecked() || withGeom );
343 }
344
345 //=======================================================================
346 //function : getErrorMsg
347 //purpose  : Return an error message and entries of invalid smesh object
348 //=======================================================================
349
350 QString SMESHGUI_CopyMeshDlg::getErrorMsg( SMESH::string_array_var theInvalidEntries,
351                                            QStringList &           theEntriesToBrowse )
352 {
353   if ( theInvalidEntries->length() == 0 )
354     return tr("SMESH_OPERATION_FAILED");
355
356   // theInvalidEntries - SObject's that hold geometry objects whose
357   // counterparts are not found in the newGeometry, followed by SObject's
358   // holding mesh sub-objects that are invalid because they depend on a not found
359   // preceeding sub-shape
360
361   QString msg = tr("SUBSHAPES_NOT_FOUND_MSG") + "\n";
362
363   QString objString;
364   for ( CORBA::ULong i = 0; i < theInvalidEntries->length(); ++i )
365   {
366     _PTR(Study) study = SMESH::GetActiveStudyDocument();
367     _PTR(SObject) so = study->FindObjectID( theInvalidEntries[i].in() );
368
369     int objType = SMESHGUI_Selection::type( theInvalidEntries[i].in(), study );
370     if ( objType < 0 ) // geom object
371     {
372       objString += "\n";
373       if ( so )
374         objString += so->GetName().c_str();
375       else
376         objString += theInvalidEntries[i].in(); // it's something like "FACE #2"
377     }
378     else // smesh object
379     {
380       theEntriesToBrowse.push_back( theInvalidEntries[i].in() );
381
382       objString += "\n   ";
383       switch ( objType ) {
384       case SMESH::MESH:
385         objString += tr("SMESH_MESH"); break;
386       case SMESH::HYPOTHESIS:
387         objString += tr("SMESH_HYPOTHESIS"); break;
388       case SMESH::ALGORITHM:
389         objString += tr("SMESH_ALGORITHM"); break;
390       case SMESH::SUBMESH_VERTEX:
391       case SMESH::SUBMESH_EDGE:
392       case SMESH::SUBMESH_FACE:
393       case SMESH::SUBMESH_SOLID:
394       case SMESH::SUBMESH_COMPOUND:
395       case SMESH::SUBMESH:
396         objString += tr("SMESH_SUBMESH"); break;
397       case SMESH::GROUP:
398         objString += tr("SMESH_GROUP"); break;
399       default:;
400       }
401       objString += " \"";
402       if ( so )
403         objString += so->GetName().c_str();
404       objString += "\" (";
405       objString += theInvalidEntries[i].in();
406       objString += ")";
407     }
408   }
409   if ( !objString.isEmpty() )
410     msg += objString;
411
412   return msg;
413 }
414
415 //=================================================================================
416 // function : ClickOnApply()
417 // purpose  :
418 //=================================================================================
419
420 bool SMESHGUI_CopyMeshDlg::ClickOnApply()
421 {
422   if (mySMESHGUI->isActiveStudyLocked())
423     return false;
424
425   if( !isValid() )
426     return false;
427
428   QStringList anEntryList;
429   bool toShowObjects = isApplyAndClose();
430   try
431   {
432     SUIT_OverrideCursor aWaitCursor;
433
434     SMESH::IDSource_wrap aPartToCopy;
435     if ( myIdSourceCheck->isChecked())
436     {
437       aPartToCopy = mySelectedObject;
438       aPartToCopy->Register();
439     }
440     else
441     {
442       QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
443       SMESH::long_array_var anElementsId = new SMESH::long_array;
444       anElementsId->length(aListElementsId.count());
445       for (int i = 0; i < aListElementsId.count(); i++)
446         anElementsId[i] = aListElementsId[i].toInt();
447
448       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
449       aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
450     }
451     QByteArray meshName = myMeshNameEdit->text().toLatin1();
452     bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
453     bool toReuseHyps  = ( myReuseHypCheck->isChecked() );
454     bool toCopyElems  = ( myCopyElementsCheck->isChecked() );
455     bool toKeepIDs    = ( myKeepIdsCheck->isChecked() );
456
457     SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
458     SMESH::SMESH_Mesh_var newMesh;
459     if ( isWithGeomMode() )
460     {
461       SMESH::SMESH_Mesh_var       srcMesh = mySelectedObject->GetMesh();
462       SMESH::ListOfGroups_var     newGroups;
463       SMESH::submesh_array_var    newSubmeshes;
464       SMESH::ListOfHypothesis_var newHypotheses;
465       SMESH::string_array_var     invalidEntries;
466       CORBA::Boolean ok = gen->CopyMeshWithGeom( srcMesh, myNewGeometry,
467                                                  meshName.constData(),
468                                                  toCopyGroups, toReuseHyps, toCopyElems,
469                                                  newMesh.out(),
470                                                  newGroups.out(),
471                                                  newSubmeshes.out(),
472                                                  newHypotheses.out(),
473                                                  invalidEntries.out() );
474       if ( !ok )
475       {
476         if ( invalidEntries->length() > 0 )
477           toShowObjects = true;
478         SUIT_MessageBox::warning( this,
479                                   tr("SMESH_WRN_WARNING"),
480                                   getErrorMsg( invalidEntries, anEntryList ));
481       }
482     }
483     else
484     {
485       newMesh = gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
486     }
487     if ( !newMesh->_is_nil() )
488       if ( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
489       {
490         anEntryList.append( aSObject->GetID().c_str() );
491
492         if ( isWithGeomMode() )
493           SMESH::SetName( aSObject, meshName );
494       }
495   }
496   catch(const SALOME::SALOME_Exception & S_ex)
497   {
498     SalomeApp_Tools::QtCatchCorbaException(S_ex);
499   }
500   catch (...)
501   {
502   }
503
504   mySMESHGUI->updateObjBrowser(true);
505   SMESHGUI::Modified();
506
507   if( LightApp_Application* anApp =
508       dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
509     anApp->browseObjects( anEntryList, toShowObjects );
510
511   Init(false);
512   mySelectedObject = SMESH::SMESH_IDSource::_nil();
513   SelectionIntoArgument();
514
515   return true;
516 }
517
518 //=================================================================================
519 // function : ClickOnOk()
520 // purpose  :
521 //=================================================================================
522 void SMESHGUI_CopyMeshDlg::ClickOnOk()
523 {
524   setIsApplyAndClose( true );
525   if( ClickOnApply() )
526     reject();
527 }
528
529 //=================================================================================
530 // function : reject()
531 // purpose  :
532 //=================================================================================
533 void SMESHGUI_CopyMeshDlg::reject()
534 {
535   disconnect(mySelectionMgr, 0, this, 0);
536   if ( mySelectionMgr )
537     mySelectionMgr->removeFilter( myIdSourceFilter );
538   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
539     aViewWindow->SetSelectionMode( ActorSelection );
540   mySMESHGUI->ResetState();
541   QDialog::reject();
542 }
543
544 //=================================================================================
545 // function : onOpenView()
546 // purpose  :
547 //=================================================================================
548 void SMESHGUI_CopyMeshDlg::onOpenView()
549 {
550   if ( mySelector ) {
551     SMESH::SetPointRepresentation(false);
552   }
553   else {
554     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
555     ActivateThisDialog();
556   }
557 }
558
559 //=================================================================================
560 // function : onCloseView()
561 // purpose  :
562 //=================================================================================
563 void SMESHGUI_CopyMeshDlg::onCloseView()
564 {
565   DeactivateActiveDialog();
566   mySelector = 0;
567 }
568
569 //=================================================================================
570 // function : ClickOnHelp()
571 // purpose  :
572 //=================================================================================
573 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
574 {
575   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
576   if (app)
577     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
578   else {
579     QString platform;
580 #ifdef WIN32
581     platform = "winapplication";
582 #else
583     platform = "application";
584 #endif
585     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
586                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
587                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
588                                                                  platform)).
589                              arg(myHelpFileName));
590   }
591 }
592
593 //=======================================================================
594 // function : onTextChange()
595 // purpose  :
596 //=======================================================================
597
598 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
599 {
600   QLineEdit* send = (QLineEdit*)sender();
601
602   if (myBusy) return;
603   BusyLocker lock( myBusy );
604
605   //if (send == myLineEditElements)
606   myNbOkElements = 0;
607
608   buttonOk->setEnabled(false);
609   buttonApply->setEnabled(false);
610
611   // highlight entered elements
612   SMDS_Mesh* aMesh = 0;
613   if (myActor)
614     aMesh = myActor->GetObject()->GetMesh();
615
616   QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
617   if (myActor && aMesh)
618   {
619     TColStd_MapOfInteger newIndices;
620     if (send == myLineEditElements) {
621       for (int i = 0; i < aListId.count(); i++)
622         if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
623         {
624           newIndices.Add(e->GetID());
625         }
626     }
627     myNbOkElements = newIndices.Extent();
628
629     Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
630     mySelector->AddOrRemoveIndex( anIO, newIndices, false );
631     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
632       aViewWindow->highlight( anIO, true, true );
633   }
634   else
635   {
636     myNbOkElements = aListId.count();
637   }
638
639   if (myNbOkElements) {
640     buttonOk->setEnabled(true);
641     buttonApply->setEnabled(true);
642   }
643 }
644
645 //=================================================================================
646 // function : SelectionIntoArgument()
647 // purpose  : Called when selection as changed or other case
648 //=================================================================================
649
650 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
651 {
652   if (myBusy) return;
653   if (myFilterDlg && myFilterDlg->isVisible()) return; // filter dlg active
654   if (!GroupButtons->isEnabled()) return;              // inactive
655
656   BusyLocker lock( myBusy );
657
658   // clear
659   myActor = 0;
660   QString aString = "";
661
662   myNbOkElements = 0;
663   buttonOk->setEnabled(false);
664   buttonApply->setEnabled(false);
665   myFilterBtn->setEnabled(false);
666
667   // get selected mesh or geometry
668   SALOME_ListIO aList;
669   mySelectionMgr->selectedObjects(aList);
670   int nbSel = aList.Extent();
671   if (nbSel != 1)
672     return;
673
674   Handle(SALOME_InteractiveObject) IO = aList.First();
675
676   SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(IO);
677   GEOM::GEOM_Object_var geom = SMESH::GetGeom(IO);
678
679   if ( !mesh->_is_nil() )
680   {
681     myMesh  = mesh;
682     myActor = SMESH::FindActorByEntry(IO->getEntry());
683     if (!myActor)
684       myActor = SMESH::FindActorByObject(myMesh);
685
686     mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
687     if ( mySelectedObject->_is_nil() )
688       return;
689   }
690   else if ( !geom->_is_nil() )
691   {
692     myNewGeometry = geom;
693   }
694   else
695     return;
696
697   if (myIdSourceCheck->isChecked() || isWithGeomMode() )
698   {
699     SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
700     if ( aString.isEmpty() ) aString = " ";
701     else                     aString = aString.trimmed(); // issue 0021327
702   }
703   else
704   {
705     SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
706     myNbOkElements = aString.size();
707     myFilterBtn->setEnabled(true);
708   }
709
710   bool ok = !aString.isEmpty();
711   if ( !mesh->_is_nil() )
712   {
713     myLineEditElements->setText( aString );
714     if ( isWithGeomMode() )
715       myMeshNameEdit->setText( aString );
716   }
717   else if ( !geom->_is_nil() )
718   {
719     myGeomNameEdit->setText( aString );
720     ok = ok && !myLineEditElements->text().isEmpty();
721   }
722
723   if ( ok && isWithGeomMode() && !myMesh->_is_nil() )
724     ok = myMesh->HasShapeToMesh();
725
726
727   buttonOk->setEnabled(ok);
728   buttonApply->setEnabled(ok);
729 }
730
731 //=======================================================================
732 //function : onSelectIdSource
733 //purpose  :
734 //=======================================================================
735 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
736 {
737   if ( isWithGeomMode() )
738     myTextLabelElements->setText(tr("SMESH_MESH"));
739   else if ( toSelectMesh )
740     myTextLabelElements->setText(tr("OBJECT_NAME"));
741   else
742     myTextLabelElements->setText(tr("ELEM_IDS"));
743
744   if (toSelectMesh) {
745     myLineEditElements->clear();
746   }
747
748   mySelectionMgr->clearFilters();
749   if ( !isWithGeomMode() )
750     mySelectionMgr->installFilter(myIdSourceFilter);
751   SMESH::SetPointRepresentation(false);
752
753   if (toSelectMesh) {
754     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
755       aViewWindow->SetSelectionMode( ActorSelection );
756     myLineEditElements->setReadOnly(true);
757     myLineEditElements->setValidator(0);
758   }
759   else
760   {
761     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
762       aViewWindow->SetSelectionMode( CellSelection );
763     myLineEditElements->setReadOnly(false);
764     myLineEditElements->setValidator(myIdValidator);
765     onTextChange(myLineEditElements->text());
766   }
767
768   SelectionIntoArgument();
769 }
770
771 //=================================================================================
772 // function : isValid
773 // purpose  :
774 //=================================================================================
775
776 bool SMESHGUI_CopyMeshDlg::isValid()
777 {
778   bool ok = false;
779   if ( myIdSourceCheck->isChecked() || isWithGeomMode() )
780   {
781     ok = ( !mySelectedObject->_is_nil() );
782     if ( isWithGeomMode() )
783       ok = ok && ( !myNewGeometry->_is_nil() );
784   }
785   else
786   {
787     ok = ( myNbOkElements > 0 );
788   }
789
790   return ok;
791 }
792
793 //=======================================================================
794 //function : isWithGeomMode
795 //purpose  : Return true if the mode is "with geometry"
796 //=======================================================================
797
798 bool SMESHGUI_CopyMeshDlg::isWithGeomMode()
799 {
800   return ( GroupConstructors->checkedId() == 1 );
801 }
802
803 //=================================================================================
804 // function : DeactivateActiveDialog()
805 // purpose  :
806 //=================================================================================
807 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
808 {
809   if (ConstructorsBox->isEnabled()) {
810     ConstructorsBox->setEnabled(false);
811     GroupArguments->setEnabled(false);
812     GroupButtons->setEnabled(false);
813     mySMESHGUI->ResetState();
814     mySMESHGUI->SetActiveDialogBox(0);
815     if ( mySelectionMgr )
816       mySelectionMgr->removeFilter( myIdSourceFilter );
817   }
818 }
819
820 //=================================================================================
821 // function : ActivateThisDialog()
822 // purpose  :
823 //=================================================================================
824 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
825 {
826   /* Emit a signal to deactivate the active dialog */
827   mySMESHGUI->EmitSignalDeactivateDialog();
828   ConstructorsBox->setEnabled(true);
829   GroupArguments->setEnabled(true);
830   GroupButtons->setEnabled(true);
831
832   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
833
834   onSelectIdSource( myIdSourceCheck->isChecked() );
835
836   SelectionIntoArgument();
837 }
838
839
840 //=================================================================================
841 // function : enterEvent()
842 // purpose  :
843 //=================================================================================
844 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
845 {
846   if ( !ConstructorsBox->isEnabled() ) {
847     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
848     if ( aViewWindow && !mySelector ) {
849       mySelector = aViewWindow->GetSelector();
850     }
851     ActivateThisDialog();
852   }
853 }
854 //=================================================================================
855 // function : keyPressEvent()
856 // purpose  :
857 //=================================================================================
858 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
859 {
860   QDialog::keyPressEvent( e );
861   if ( e->isAccepted() )
862     return;
863
864   if ( e->key() == Qt::Key_F1 ) {
865     e->accept();
866     ClickOnHelp();
867   }
868 }
869
870 //=================================================================================
871 // function : setFilters()
872 // purpose  : SLOT. Called when "Filter" button pressed.
873 //=================================================================================
874 void SMESHGUI_CopyMeshDlg::setFilters()
875 {
876   if(myMesh->_is_nil()) {
877     SUIT_MessageBox::critical(this,
878                               tr("SMESH_ERROR"),
879                               tr("NO_MESH_SELECTED"));
880    return;
881   }
882   if ( !myFilterDlg )
883     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
884
885   QList<int> types;
886   if ( myMesh->NbEdges()     ) types << SMESH::EDGE;
887   if ( myMesh->NbFaces()     ) types << SMESH::FACE;
888   if ( myMesh->NbVolumes()   ) types << SMESH::VOLUME;
889   if ( myMesh->NbBalls()     ) types << SMESH::BALL;
890   if ( myMesh->Nb0DElements()) types << SMESH::ELEM0D;
891   if ( types.count() > 1 )     types << SMESH::ALL;
892
893   myFilterDlg->Init( types );
894   myFilterDlg->SetSelection();
895   myFilterDlg->SetMesh( myMesh );
896   myFilterDlg->SetSourceWg( myLineEditElements );
897
898   myFilterDlg->show();
899 }
900
901 //================================================================
902 // function : setIsApplyAndClose
903 // Purpose  : Set value of the flag indicating that the dialog is
904 //            accepted by Apply & Close button
905 //================================================================
906 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
907 {
908   myIsApplyAndClose = theFlag;
909 }
910
911 //================================================================
912 // function : isApplyAndClose
913 // Purpose  : Get value of the flag indicating that the dialog is
914 //            accepted by Apply & Close button
915 //================================================================
916 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
917 {
918   return myIsApplyAndClose;
919 }