1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESHGUI_CopyMeshDlg.cxx
24 #include "SMESHGUI_CopyMeshDlg.h"
27 #include "SMESHGUI_SpinBox.h"
28 #include "SMESHGUI_Utils.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_MeshUtils.h"
31 #include "SMESHGUI_IdValidator.h"
32 #include "SMESHGUI_FilterDlg.h"
34 #include <SMESH_Actor.h>
35 #include <SMESH_TypeFilter.hxx>
36 #include <SMDS_Mesh.hxx>
38 // SALOME GUI includes
39 #include <SUIT_Desktop.h>
40 #include <SUIT_ResourceMgr.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_OverrideCursor.h>
45 #include <LightApp_Application.h>
46 #include <LightApp_SelectionMgr.h>
48 #include <SVTK_ViewModel.h>
49 #include <SVTK_ViewWindow.h>
50 #include <SALOME_ListIO.hxx>
52 // SALOME KERNEL includes
53 #include <SALOMEDSClient_SObject.hxx>
56 #include <TColStd_MapOfInteger.hxx>
59 #include <QApplication>
60 #include <QButtonGroup>
64 #include <QPushButton>
65 #include <QRadioButton>
67 #include <QHBoxLayout>
68 #include <QVBoxLayout>
69 #include <QGridLayout>
74 #include <SALOMEconfig.h>
75 #include CORBA_SERVER_HEADER(SMESH_Group)
76 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
80 \brief Simple 'busy state' flag locker.
89 //! Constructor. Sets passed boolean flag to \c true.
90 BusyLocker( bool& busy ) : myBusy( busy ) { myBusy = true; }
91 //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
92 ~BusyLocker() { myBusy = false; }
94 bool& myBusy; //! External 'busy state' boolean flag
102 //================================================================================
106 //================================================================================
108 SMESHGUI_CopyMeshDlg::SMESHGUI_CopyMeshDlg( SMESHGUI* theModule )
109 : QDialog( SMESH::GetDesktop( theModule ) ),
110 mySMESHGUI( theModule ),
111 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
112 mySelectedObject(SMESH::SMESH_IDSource::_nil()),
114 myIsApplyAndClose( false )
116 QPixmap image (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_COPY_MESH")));
119 setAttribute(Qt::WA_DeleteOnClose, true);
120 setWindowTitle(tr("SMESH_COPY_MESH_TITLE"));
121 setSizeGripEnabled(true);
123 QVBoxLayout* SMESHGUI_CopyMeshDlgLayout = new QVBoxLayout(this);
124 SMESHGUI_CopyMeshDlgLayout->setSpacing(SPACING);
125 SMESHGUI_CopyMeshDlgLayout->setMargin(MARGIN);
127 /***************************************************************/
128 ConstructorsBox = new QGroupBox(tr("SMESH_COPY_MESH_TITLE"), this);
129 QButtonGroup* GroupConstructors = new QButtonGroup(this);
130 QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout(ConstructorsBox);
131 ConstructorsBoxLayout->setSpacing(SPACING);
132 ConstructorsBoxLayout->setMargin(MARGIN);
134 QRadioButton* RadioButton1= new QRadioButton(ConstructorsBox);
135 RadioButton1->setIcon(image);
136 GroupConstructors->addButton(RadioButton1, 0);
138 ConstructorsBoxLayout->addWidget(RadioButton1);
139 RadioButton1->setChecked(true);
140 GroupConstructors->addButton(RadioButton1, 0);
142 /***************************************************************/
143 GroupArguments = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
144 QGridLayout* GroupArgumentsLayout = new QGridLayout(GroupArguments);
145 GroupArgumentsLayout->setSpacing(SPACING);
146 GroupArgumentsLayout->setMargin(MARGIN);
148 myIdValidator = new SMESHGUI_IdValidator(this);
150 // Controls for idSource/elements selection
151 myTextLabelElements = new QLabel(tr("OBJECT_NAME"), GroupArguments);
152 myLineEditElements = new QLineEdit(GroupArguments);
153 myLineEditElements->setValidator(myIdValidator);
154 myLineEditElements->setMaxLength(-1);
155 myFilterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupArguments );
156 connect(myFilterBtn, SIGNAL(clicked()), this, SLOT(setFilters()));
158 // Control for the mesh objects selection
159 myIdSourceCheck = new QCheckBox(tr("SMESH_SELECT_WHOLE_MESH"), GroupArguments);
161 // Name of a mesh to create
162 QLabel* meshNameLabel = new QLabel(tr("NEW_NAME"), GroupArguments);
163 myMeshNameEdit = new QLineEdit(GroupArguments);
165 // CheckBox for copying groups
166 myCopyGroupsCheck = new QCheckBox(tr("SMESH_MAKE_GROUPS"), GroupArguments);
167 myCopyGroupsCheck->setChecked(false);
169 // CheckBox for keeping ids ( OBSOLETE )
170 myKeepIdsCheck = new QCheckBox(tr("SMESH_KEEP_IDS"), GroupArguments);
171 myKeepIdsCheck->setChecked(true);
172 myKeepIdsCheck->hide();
175 GroupArgumentsLayout->addWidget(myTextLabelElements, 0, 0);
176 GroupArgumentsLayout->addWidget(myLineEditElements, 0, 1, 1, 5);
177 GroupArgumentsLayout->addWidget(myFilterBtn, 0, 6);
178 GroupArgumentsLayout->addWidget(myIdSourceCheck, 1, 0, 1, 6);
179 GroupArgumentsLayout->addWidget(meshNameLabel, 2, 0);
180 GroupArgumentsLayout->addWidget(myMeshNameEdit, 2, 1, 1, 5);
181 GroupArgumentsLayout->addWidget(myCopyGroupsCheck, 3, 0, 1, 6);
182 // GroupArgumentsLayout->addWidget(myKeepIdsCheck, 4, 0, 1, 6);
184 /***************************************************************/
185 GroupButtons = new QGroupBox(this);
186 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
187 GroupButtonsLayout->setSpacing(SPACING);
188 GroupButtonsLayout->setMargin(MARGIN);
190 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
191 buttonOk->setAutoDefault(true);
192 buttonOk->setDefault(true);
193 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
194 buttonApply->setAutoDefault(true);
195 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
196 buttonCancel->setAutoDefault(true);
197 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
198 buttonHelp->setAutoDefault(true);
200 GroupButtonsLayout->addWidget(buttonOk);
201 GroupButtonsLayout->addSpacing(10);
202 GroupButtonsLayout->addWidget(buttonApply);
203 GroupButtonsLayout->addSpacing(10);
204 GroupButtonsLayout->addStretch();
205 GroupButtonsLayout->addWidget(buttonCancel);
206 GroupButtonsLayout->addWidget(buttonHelp);
208 /***************************************************************/
209 SMESHGUI_CopyMeshDlgLayout->addWidget(ConstructorsBox);
210 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupArguments);
211 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupButtons);
213 /* Initialisations */
214 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
216 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
219 myIdSourceFilter = new SMESH_TypeFilter( SMESH::IDSOURCE );
221 myHelpFileName = "copy_mesh_page.html";
225 /* signals and slots connections */
226 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
227 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
228 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
229 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
231 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()),
232 this, SLOT (DeactivateActiveDialog()));
233 connect(mySelectionMgr, SIGNAL (currentSelectionChanged()),
234 this, SLOT (SelectionIntoArgument()));
235 connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),/* to close dialog if study change */
236 this, SLOT (reject()));
237 connect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()),
238 this, SLOT (onOpenView()));
239 connect(mySMESHGUI, SIGNAL (SignalCloseView()),
240 this, SLOT (onCloseView()));
242 connect(myLineEditElements, SIGNAL(textChanged(const QString&)),
243 this, SLOT (onTextChange(const QString&)));
244 connect(myIdSourceCheck, SIGNAL(toggled(bool)),
245 this, SLOT (onSelectIdSource(bool)));
247 SelectionIntoArgument();
250 //=================================================================================
251 // function : ~SMESHGUI_CopyMeshDlg()
252 // purpose : Destroys the object and frees any allocated resources
253 //=================================================================================
255 SMESHGUI_CopyMeshDlg::~SMESHGUI_CopyMeshDlg()
259 myFilterDlg->setParent( 0 );
260 delete myFilterDlg; myFilterDlg = 0;
262 if ( myIdSourceFilter )
264 if ( mySelectionMgr )
265 mySelectionMgr->removeFilter( myIdSourceFilter );
266 delete myIdSourceFilter; myIdSourceFilter=0;
270 //=================================================================================
273 //=================================================================================
274 void SMESHGUI_CopyMeshDlg::Init (bool ResetControls)
278 myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
281 myLineEditElements->clear();
285 buttonOk->setEnabled(false);
286 buttonApply->setEnabled(false);
289 myMesh = SMESH::SMESH_Mesh::_nil();
291 myIdSourceCheck->setChecked(true);
292 myCopyGroupsCheck->setChecked(false);
293 myKeepIdsCheck->setChecked(false);
295 onSelectIdSource( myIdSourceCheck->isChecked() );
299 //=================================================================================
300 // function : ClickOnApply()
302 //=================================================================================
304 bool SMESHGUI_CopyMeshDlg::ClickOnApply()
306 if (mySMESHGUI->isActiveStudyLocked())
312 QStringList anEntryList;
315 SUIT_OverrideCursor aWaitCursor;
317 SMESH::IDSource_wrap aPartToCopy;
318 if ( myIdSourceCheck->isChecked())
320 aPartToCopy = mySelectedObject;
321 aPartToCopy->Register();
325 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
326 SMESH::long_array_var anElementsId = new SMESH::long_array;
327 anElementsId->length(aListElementsId.count());
328 for (int i = 0; i < aListElementsId.count(); i++)
329 anElementsId[i] = aListElementsId[i].toInt();
331 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
332 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
334 QByteArray meshName = myMeshNameEdit->text().toLatin1();
335 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
336 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
338 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
339 SMESH::SMESH_Mesh_var newMesh =
340 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
341 if( !newMesh->_is_nil() )
342 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
343 anEntryList.append( aSObject->GetID().c_str() );
348 mySMESHGUI->updateObjBrowser(true);
349 SMESHGUI::Modified();
351 if( LightApp_Application* anApp =
352 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
353 anApp->browseObjects( anEntryList, isApplyAndClose() );
356 mySelectedObject = SMESH::SMESH_IDSource::_nil();
357 SelectionIntoArgument();
362 //=================================================================================
363 // function : ClickOnOk()
365 //=================================================================================
366 void SMESHGUI_CopyMeshDlg::ClickOnOk()
368 setIsApplyAndClose( true );
373 //=================================================================================
374 // function : reject()
376 //=================================================================================
377 void SMESHGUI_CopyMeshDlg::reject()
379 disconnect(mySelectionMgr, 0, this, 0);
380 if ( mySelectionMgr )
381 mySelectionMgr->removeFilter( myIdSourceFilter );
382 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
383 aViewWindow->SetSelectionMode( ActorSelection );
384 mySMESHGUI->ResetState();
388 //=================================================================================
389 // function : onOpenView()
391 //=================================================================================
392 void SMESHGUI_CopyMeshDlg::onOpenView()
395 SMESH::SetPointRepresentation(false);
398 mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
399 ActivateThisDialog();
403 //=================================================================================
404 // function : onCloseView()
406 //=================================================================================
407 void SMESHGUI_CopyMeshDlg::onCloseView()
409 DeactivateActiveDialog();
413 //=================================================================================
414 // function : ClickOnHelp()
416 //=================================================================================
417 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
419 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
421 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
425 platform = "winapplication";
427 platform = "application";
429 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
430 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
431 arg(app->resourceMgr()->stringValue("ExternalBrowser",
433 arg(myHelpFileName));
437 //=======================================================================
438 // function : onTextChange()
440 //=======================================================================
442 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
444 QLineEdit* send = (QLineEdit*)sender();
447 BusyLocker lock( myBusy );
449 //if (send == myLineEditElements)
452 buttonOk->setEnabled(false);
453 buttonApply->setEnabled(false);
455 // hilight entered elements
456 SMDS_Mesh* aMesh = 0;
458 aMesh = myActor->GetObject()->GetMesh();
460 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
461 if (myActor && aMesh)
463 TColStd_MapOfInteger newIndices;
464 if (send == myLineEditElements) {
465 for (int i = 0; i < aListId.count(); i++)
466 if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
468 newIndices.Add(e->GetID());
471 myNbOkElements = newIndices.Extent();
473 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
474 mySelector->AddOrRemoveIndex( anIO, newIndices, false );
475 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
476 aViewWindow->highlight( anIO, true, true );
480 myNbOkElements = aListId.count();
483 if (myNbOkElements) {
484 buttonOk->setEnabled(true);
485 buttonApply->setEnabled(true);
489 //=================================================================================
490 // function : SelectionIntoArgument()
491 // purpose : Called when selection as changed or other case
492 //=================================================================================
494 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
497 if (myFilterDlg && myFilterDlg->isVisible()) return; // filter dlg active
498 if (!GroupButtons->isEnabled()) return; // inactive
500 BusyLocker lock( myBusy );
504 QString aString = "";
506 myLineEditElements->setText(aString);
508 buttonOk->setEnabled(false);
509 buttonApply->setEnabled(false);
510 myFilterBtn->setEnabled(false);
514 mySelectionMgr->selectedObjects(aList);
515 int nbSel = aList.Extent();
519 Handle(SALOME_InteractiveObject) IO = aList.First();
520 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
521 if ( mySelectedObject->_is_nil() )
524 myMesh = SMESH::GetMeshByIO(IO);
525 if (myMesh->_is_nil())
528 myActor = SMESH::FindActorByEntry(IO->getEntry());
530 myActor = SMESH::FindActorByObject(myMesh);
532 if (myIdSourceCheck->isChecked())
534 SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
535 if ( aString.isEmpty() ) aString = " ";
536 else aString = aString.trimmed(); // issue 0021327
540 SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
541 myNbOkElements = aString.size();
542 myFilterBtn->setEnabled(true);
544 myLineEditElements->setText( aString );
545 bool ok = !aString.isEmpty();
547 buttonOk->setEnabled(ok);
548 buttonApply->setEnabled(ok);
551 //=======================================================================
552 //function : onSelectIdSource
554 //=======================================================================
555 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
558 myTextLabelElements->setText(tr("OBJECT_NAME"));
560 myTextLabelElements->setText(tr("ELEM_IDS"));
563 myLineEditElements->clear();
566 mySelectionMgr->clearFilters();
567 mySelectionMgr->installFilter(myIdSourceFilter);
568 SMESH::SetPointRepresentation(false);
571 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
572 aViewWindow->SetSelectionMode( ActorSelection );
573 myLineEditElements->setReadOnly(true);
574 myLineEditElements->setValidator(0);
578 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
579 aViewWindow->SetSelectionMode( CellSelection );
580 myLineEditElements->setReadOnly(false);
581 myLineEditElements->setValidator(myIdValidator);
582 onTextChange(myLineEditElements->text());
585 SelectionIntoArgument();
588 //=================================================================================
589 // function : isValid
591 //=================================================================================
593 bool SMESHGUI_CopyMeshDlg::isValid()
595 if ( myIdSourceCheck->isChecked() )
596 return !mySelectedObject->_is_nil();
598 return myNbOkElements > 0;
601 //=================================================================================
602 // function : DeactivateActiveDialog()
604 //=================================================================================
605 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
607 if (ConstructorsBox->isEnabled()) {
608 ConstructorsBox->setEnabled(false);
609 GroupArguments->setEnabled(false);
610 GroupButtons->setEnabled(false);
611 mySMESHGUI->ResetState();
612 mySMESHGUI->SetActiveDialogBox(0);
613 if ( mySelectionMgr )
614 mySelectionMgr->removeFilter( myIdSourceFilter );
618 //=================================================================================
619 // function : ActivateThisDialog()
621 //=================================================================================
622 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
624 /* Emit a signal to deactivate the active dialog */
625 mySMESHGUI->EmitSignalDeactivateDialog();
626 ConstructorsBox->setEnabled(true);
627 GroupArguments->setEnabled(true);
628 GroupButtons->setEnabled(true);
630 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
632 onSelectIdSource( myIdSourceCheck->isChecked() );
634 SelectionIntoArgument();
638 //=================================================================================
639 // function : enterEvent()
641 //=================================================================================
642 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
644 if ( !ConstructorsBox->isEnabled() ) {
645 SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
646 if ( aViewWindow && !mySelector ) {
647 mySelector = aViewWindow->GetSelector();
649 ActivateThisDialog();
652 //=================================================================================
653 // function : keyPressEvent()
655 //=================================================================================
656 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
658 QDialog::keyPressEvent( e );
659 if ( e->isAccepted() )
662 if ( e->key() == Qt::Key_F1 ) {
668 //=================================================================================
669 // function : setFilters()
670 // purpose : SLOT. Called when "Filter" button pressed.
671 //=================================================================================
672 void SMESHGUI_CopyMeshDlg::setFilters()
674 if(myMesh->_is_nil()) {
675 SUIT_MessageBox::critical(this,
677 tr("NO_MESH_SELECTED"));
681 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
684 if ( myMesh->NbEdges() ) types << SMESH::EDGE;
685 if ( myMesh->NbFaces() ) types << SMESH::FACE;
686 if ( myMesh->NbVolumes() ) types << SMESH::VOLUME;
687 if ( myMesh->NbBalls() ) types << SMESH::BALL;
688 if ( myMesh->Nb0DElements()) types << SMESH::ELEM0D;
689 if ( types.count() > 1 ) types << SMESH::ALL;
691 myFilterDlg->Init( types );
692 myFilterDlg->SetSelection();
693 myFilterDlg->SetMesh( myMesh );
694 myFilterDlg->SetSourceWg( myLineEditElements );
699 //================================================================
700 // function : setIsApplyAndClose
701 // Purpose : Set value of the flag indicating that the dialog is
702 // accepted by Apply & Close button
703 //================================================================
704 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
706 myIsApplyAndClose = theFlag;
709 //================================================================
710 // function : isApplyAndClose
711 // Purpose : Get value of the flag indicating that the dialog is
712 // accepted by Apply & Close button
713 //================================================================
714 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
716 return myIsApplyAndClose;