1 // Copyright (C) 2007-2010 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.
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 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_CopyMeshDlg.cxx
25 #include "SMESHGUI_CopyMeshDlg.h"
28 #include "SMESHGUI_SpinBox.h"
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_MeshUtils.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_FilterDlg.h"
35 #include <SMESH_Actor.h>
36 #include <SMESH_TypeFilter.hxx>
37 #include <SMDS_Mesh.hxx>
39 // SALOME GUI includes
40 #include <SUIT_Desktop.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_OverrideCursor.h>
46 #include <LightApp_Application.h>
47 #include <LightApp_SelectionMgr.h>
49 #include <SVTK_ViewModel.h>
50 #include <SVTK_ViewWindow.h>
51 #include <SALOME_ListIO.hxx>
53 // SALOME KERNEL includes
54 #include <SALOMEDSClient_SObject.hxx>
57 #include <TColStd_MapOfInteger.hxx>
60 #include <QApplication>
61 #include <QButtonGroup>
65 #include <QPushButton>
66 #include <QRadioButton>
68 #include <QHBoxLayout>
69 #include <QVBoxLayout>
70 #include <QGridLayout>
75 #include <SALOMEconfig.h>
76 #include CORBA_SERVER_HEADER(SMESH_Group)
77 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
81 \brief Simple 'busy state' flag locker.
90 //! Constructor. Sets passed boolean flag to \c true.
91 BusyLocker( bool& busy ) : myBusy( busy ) { myBusy = true; }
92 //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
93 ~BusyLocker() { myBusy = false; }
95 bool& myBusy; //! External 'busy state' boolean flag
102 //To disable automatic genericobj management, the following line should be commented.
103 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
104 #define WITHGENERICOBJ
107 //================================================================================
111 //================================================================================
113 SMESHGUI_CopyMeshDlg::SMESHGUI_CopyMeshDlg( SMESHGUI* theModule )
114 : QDialog( SMESH::GetDesktop( theModule ) ),
115 mySMESHGUI( theModule ),
116 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
118 mySelectedObject(SMESH::SMESH_IDSource::_nil()),
119 myIsApplyAndClose( false )
121 QPixmap image (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_COPY_MESH")));
124 setAttribute(Qt::WA_DeleteOnClose, true);
125 setWindowTitle(tr("SMESH_COPY_MESH_TITLE"));
126 setSizeGripEnabled(true);
128 QVBoxLayout* SMESHGUI_CopyMeshDlgLayout = new QVBoxLayout(this);
129 SMESHGUI_CopyMeshDlgLayout->setSpacing(SPACING);
130 SMESHGUI_CopyMeshDlgLayout->setMargin(MARGIN);
132 /***************************************************************/
133 ConstructorsBox = new QGroupBox(tr("SMESH_COPY_MESH_TITLE"), this);
134 QButtonGroup* GroupConstructors = new QButtonGroup(this);
135 QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout(ConstructorsBox);
136 ConstructorsBoxLayout->setSpacing(SPACING);
137 ConstructorsBoxLayout->setMargin(MARGIN);
139 QRadioButton* RadioButton1= new QRadioButton(ConstructorsBox);
140 RadioButton1->setIcon(image);
141 GroupConstructors->addButton(RadioButton1, 0);
143 ConstructorsBoxLayout->addWidget(RadioButton1);
144 RadioButton1->setChecked(true);
145 GroupConstructors->addButton(RadioButton1, 0);
147 /***************************************************************/
148 GroupArguments = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
149 QGridLayout* GroupArgumentsLayout = new QGridLayout(GroupArguments);
150 GroupArgumentsLayout->setSpacing(SPACING);
151 GroupArgumentsLayout->setMargin(MARGIN);
153 myIdValidator = new SMESHGUI_IdValidator(this);
155 // Controls for idSource/elements selection
156 myTextLabelElements = new QLabel(tr("OBJECT_NAME"), GroupArguments);
157 myLineEditElements = new QLineEdit(GroupArguments);
158 myLineEditElements->setValidator(myIdValidator);
159 myLineEditElements->setMaxLength(-1);
160 myFilterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupArguments );
161 connect(myFilterBtn, SIGNAL(clicked()), this, SLOT(setFilters()));
163 // Control for the mesh objects selection
164 myIdSourceCheck = new QCheckBox(tr("SMESH_SELECT_WHOLE_MESH"), GroupArguments);
166 // Name of a mesh to create
167 QLabel* meshNameLabel = new QLabel(tr("NEW_NAME"), GroupArguments);
168 myMeshNameEdit = new QLineEdit(GroupArguments);
170 // CheckBox for copying groups
171 myCopyGroupsCheck = new QCheckBox(tr("SMESH_MAKE_GROUPS"), GroupArguments);
172 myCopyGroupsCheck->setChecked(false);
174 // CheckBox for keeping ids
175 myKeepIdsCheck = new QCheckBox(tr("SMESH_KEEP_IDS"), GroupArguments);
176 myKeepIdsCheck->setChecked(true);
179 GroupArgumentsLayout->addWidget(myTextLabelElements, 0, 0);
180 GroupArgumentsLayout->addWidget(myLineEditElements, 0, 1, 1, 5);
181 GroupArgumentsLayout->addWidget(myFilterBtn, 0, 6);
182 GroupArgumentsLayout->addWidget(myIdSourceCheck, 1, 0, 1, 6);
183 GroupArgumentsLayout->addWidget(meshNameLabel, 2, 0);
184 GroupArgumentsLayout->addWidget(myMeshNameEdit, 2, 1, 1, 5);
185 GroupArgumentsLayout->addWidget(myCopyGroupsCheck, 3, 0, 1, 6);
186 GroupArgumentsLayout->addWidget(myKeepIdsCheck, 4, 0, 1, 6);
188 /***************************************************************/
189 GroupButtons = new QGroupBox(this);
190 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
191 GroupButtonsLayout->setSpacing(SPACING);
192 GroupButtonsLayout->setMargin(MARGIN);
194 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
195 buttonOk->setAutoDefault(true);
196 buttonOk->setDefault(true);
197 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
198 buttonApply->setAutoDefault(true);
199 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
200 buttonCancel->setAutoDefault(true);
201 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
202 buttonHelp->setAutoDefault(true);
204 GroupButtonsLayout->addWidget(buttonOk);
205 GroupButtonsLayout->addSpacing(10);
206 GroupButtonsLayout->addWidget(buttonApply);
207 GroupButtonsLayout->addSpacing(10);
208 GroupButtonsLayout->addStretch();
209 GroupButtonsLayout->addWidget(buttonCancel);
210 GroupButtonsLayout->addWidget(buttonHelp);
212 /***************************************************************/
213 SMESHGUI_CopyMeshDlgLayout->addWidget(ConstructorsBox);
214 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupArguments);
215 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupButtons);
217 /* Initialisations */
218 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
220 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
223 myIdSourceFilter = new SMESH_TypeFilter( IDSOURCE );
225 myHelpFileName = "copy_mesh_page.html";
229 /* signals and slots connections */
230 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
231 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
232 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
233 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
235 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()),
236 this, SLOT (DeactivateActiveDialog()));
237 connect(mySelectionMgr, SIGNAL (currentSelectionChanged()),
238 this, SLOT (SelectionIntoArgument()));
239 connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),/* to close dialog if study change */
240 this, SLOT (ClickOnCancel()));
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;
316 SMESH::SMESH_IDSource_var aPartToCopy;
317 if ( myIdSourceCheck->isChecked())
319 aPartToCopy = mySelectedObject;
323 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
324 SMESH::long_array_var anElementsId = new SMESH::long_array;
325 anElementsId->length(aListElementsId.count());
326 for (int i = 0; i < aListElementsId.count(); i++)
327 anElementsId[i] = aListElementsId[i].toInt();
329 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
330 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
332 QByteArray meshName = myMeshNameEdit->text().toLatin1();
333 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
334 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
336 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
337 SMESH::SMESH_Mesh_var newMesh =
338 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
339 if( !newMesh->_is_nil() )
340 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
341 anEntryList.append( aSObject->GetID().c_str() );
342 #ifdef WITHGENERICOBJ
343 // obj has been published in study. Its refcount has been incremented.
344 // It is safe to decrement its refcount
345 // so that it will be destroyed when the entry in study will be removed
346 newMesh->UnRegister();
351 mySMESHGUI->updateObjBrowser(true);
352 SMESHGUI::Modified();
354 if( LightApp_Application* anApp =
355 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
356 anApp->browseObjects( anEntryList, isApplyAndClose() );
359 mySelectedObject = SMESH::SMESH_IDSource::_nil();
360 SelectionIntoArgument();
365 //=================================================================================
366 // function : ClickOnOk()
368 //=================================================================================
369 void SMESHGUI_CopyMeshDlg::ClickOnOk()
371 setIsApplyAndClose( true );
376 //=================================================================================
377 // function : ClickOnCancel()
379 //=================================================================================
380 void SMESHGUI_CopyMeshDlg::ClickOnCancel()
382 disconnect(mySelectionMgr, 0, this, 0);
383 if ( mySelectionMgr )
384 mySelectionMgr->removeFilter( myIdSourceFilter );
385 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
386 aViewWindow->SetSelectionMode( ActorSelection );
387 mySMESHGUI->ResetState();
391 //=================================================================================
392 // function : ClickOnHelp()
394 //=================================================================================
395 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
397 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
399 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
403 platform = "winapplication";
405 platform = "application";
407 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
408 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
409 arg(app->resourceMgr()->stringValue("ExternalBrowser",
411 arg(myHelpFileName));
415 //=======================================================================
416 // function : onTextChange()
418 //=======================================================================
420 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
422 QLineEdit* send = (QLineEdit*)sender();
425 BusyLocker lock( myBusy );
427 //if (send == myLineEditElements)
430 buttonOk->setEnabled(false);
431 buttonApply->setEnabled(false);
433 // hilight entered elements
434 SMDS_Mesh* aMesh = 0;
436 aMesh = myActor->GetObject()->GetMesh();
438 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
439 if (myActor && aMesh)
441 TColStd_MapOfInteger newIndices;
442 if (send == myLineEditElements) {
443 for (int i = 0; i < aListId.count(); i++)
444 if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
446 newIndices.Add(e->GetID());
449 myNbOkElements = newIndices.Extent();
451 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
452 mySelector->AddOrRemoveIndex( anIO, newIndices, false );
453 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
454 aViewWindow->highlight( anIO, true, true );
458 myNbOkElements = aListId.count();
461 if (myNbOkElements) {
462 buttonOk->setEnabled(true);
463 buttonApply->setEnabled(true);
467 //=================================================================================
468 // function : SelectionIntoArgument()
469 // purpose : Called when selection as changed or other case
470 //=================================================================================
472 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
475 BusyLocker lock( myBusy );
479 QString aString = "";
481 myLineEditElements->setText(aString);
483 buttonOk->setEnabled(false);
484 buttonApply->setEnabled(false);
485 myFilterBtn->setEnabled(false);
489 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
490 int nbSel = aList.Extent();
494 Handle(SALOME_InteractiveObject) IO = aList.First();
495 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
496 if ( mySelectedObject->_is_nil() )
499 myMesh = SMESH::GetMeshByIO(IO);
500 if (myMesh->_is_nil())
503 myActor = SMESH::FindActorByEntry(IO->getEntry());
505 myActor = SMESH::FindActorByObject(myMesh);
507 if (myIdSourceCheck->isChecked())
509 SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
510 if ( aString.isEmpty() ) aString = " ";
514 SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
515 myNbOkElements = aString.size();
516 myFilterBtn->setEnabled(true);
518 myLineEditElements->setText( aString );
519 bool ok = !aString.isEmpty();
521 buttonOk->setEnabled(ok);
522 buttonApply->setEnabled(ok);
525 //=======================================================================
526 //function : onSelectIdSource
528 //=======================================================================
529 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
532 myTextLabelElements->setText(tr("OBJECT_NAME"));
534 myTextLabelElements->setText(tr("ELEM_IDS"));
537 myLineEditElements->clear();
540 mySelectionMgr->clearFilters();
541 mySelectionMgr->installFilter(myIdSourceFilter);
542 SMESH::SetPointRepresentation(false);
545 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
546 aViewWindow->SetSelectionMode( ActorSelection );
547 myLineEditElements->setReadOnly(true);
548 myLineEditElements->setValidator(0);
552 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
553 aViewWindow->SetSelectionMode( CellSelection );
554 myLineEditElements->setReadOnly(false);
555 myLineEditElements->setValidator(myIdValidator);
556 onTextChange(myLineEditElements->text());
559 SelectionIntoArgument();
562 //=================================================================================
563 // function : isValid
565 //=================================================================================
567 bool SMESHGUI_CopyMeshDlg::isValid()
569 if ( myIdSourceCheck->isChecked() )
570 return !mySelectedObject->_is_nil();
572 return myNbOkElements > 0;
575 //=================================================================================
576 // function : DeactivateActiveDialog()
578 //=================================================================================
579 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
581 if (ConstructorsBox->isEnabled()) {
582 ConstructorsBox->setEnabled(false);
583 GroupArguments->setEnabled(false);
584 GroupButtons->setEnabled(false);
585 mySMESHGUI->ResetState();
586 mySMESHGUI->SetActiveDialogBox(0);
587 if ( mySelectionMgr )
588 mySelectionMgr->removeFilter( myIdSourceFilter );
592 //=================================================================================
593 // function : ActivateThisDialog()
595 //=================================================================================
596 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
598 /* Emit a signal to deactivate the active dialog */
599 mySMESHGUI->EmitSignalDeactivateDialog();
600 ConstructorsBox->setEnabled(true);
601 GroupArguments->setEnabled(true);
602 GroupButtons->setEnabled(true);
604 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
606 onSelectIdSource( myIdSourceCheck->isChecked() );
608 SelectionIntoArgument();
611 //=================================================================================
612 // function : enterEvent()
614 //=================================================================================
615 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
617 if (!ConstructorsBox->isEnabled())
618 ActivateThisDialog();
621 //=================================================================================
622 // function : closeEvent()
624 //=================================================================================
625 void SMESHGUI_CopyMeshDlg::closeEvent (QCloseEvent*)
627 /* same than click on cancel button */
631 //=======================================================================
632 //function : hideEvent
633 //purpose : caused by ESC key
634 //=======================================================================
635 void SMESHGUI_CopyMeshDlg::hideEvent (QHideEvent*)
641 //=================================================================================
642 // function : keyPressEvent()
644 //=================================================================================
645 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
647 QDialog::keyPressEvent( e );
648 if ( e->isAccepted() )
651 if ( e->key() == Qt::Key_F1 ) {
657 //=================================================================================
658 // function : setFilters()
659 // purpose : SLOT. Called when "Filter" button pressed.
660 //=================================================================================
661 void SMESHGUI_CopyMeshDlg::setFilters()
663 if(myMesh->_is_nil()) {
664 SUIT_MessageBox::critical(this,
666 tr("NO_MESH_SELECTED"));
670 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
672 myFilterDlg->SetSelection();
673 myFilterDlg->SetMesh( myMesh );
674 myFilterDlg->SetSourceWg( myLineEditElements );
679 //================================================================
680 // function : setIsApplyAndClose
681 // Purpose : Set value of the flag indicating that the dialog is
682 // accepted by Apply & Close button
683 //================================================================
684 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
686 myIsApplyAndClose = theFlag;
689 //================================================================
690 // function : isApplyAndClose
691 // Purpose : Get value of the flag indicating that the dialog is
692 // accepted by Apply & Close button
693 //================================================================
694 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
696 return myIsApplyAndClose;