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())
120 QPixmap image (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_COPY_MESH")));
123 setAttribute(Qt::WA_DeleteOnClose, true);
124 setWindowTitle(tr("SMESH_COPY_MESH_TITLE"));
125 setSizeGripEnabled(true);
127 QVBoxLayout* SMESHGUI_CopyMeshDlgLayout = new QVBoxLayout(this);
128 SMESHGUI_CopyMeshDlgLayout->setSpacing(SPACING);
129 SMESHGUI_CopyMeshDlgLayout->setMargin(MARGIN);
131 /***************************************************************/
132 ConstructorsBox = new QGroupBox(tr("SMESH_COPY_MESH_TITLE"), this);
133 QButtonGroup* GroupConstructors = new QButtonGroup(this);
134 QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout(ConstructorsBox);
135 ConstructorsBoxLayout->setSpacing(SPACING);
136 ConstructorsBoxLayout->setMargin(MARGIN);
138 QRadioButton* RadioButton1= new QRadioButton(ConstructorsBox);
139 RadioButton1->setIcon(image);
140 GroupConstructors->addButton(RadioButton1, 0);
142 ConstructorsBoxLayout->addWidget(RadioButton1);
143 RadioButton1->setChecked(true);
144 GroupConstructors->addButton(RadioButton1, 0);
146 /***************************************************************/
147 GroupArguments = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
148 QGridLayout* GroupArgumentsLayout = new QGridLayout(GroupArguments);
149 GroupArgumentsLayout->setSpacing(SPACING);
150 GroupArgumentsLayout->setMargin(MARGIN);
152 myIdValidator = new SMESHGUI_IdValidator(this);
154 // Controls for idSource/elements selection
155 myTextLabelElements = new QLabel(tr("OBJECT_NAME"), GroupArguments);
156 myLineEditElements = new QLineEdit(GroupArguments);
157 myLineEditElements->setValidator(myIdValidator);
158 myLineEditElements->setMaxLength(-1);
159 myFilterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupArguments );
160 connect(myFilterBtn, SIGNAL(clicked()), this, SLOT(setFilters()));
162 // Control for the mesh objects selection
163 myIdSourceCheck = new QCheckBox(tr("SMESH_SELECT_WHOLE_MESH"), GroupArguments);
165 // Name of a mesh to create
166 QLabel* meshNameLabel = new QLabel(tr("NEW_NAME"), GroupArguments);
167 myMeshNameEdit = new QLineEdit(GroupArguments);
169 // CheckBox for copying groups
170 myCopyGroupsCheck = new QCheckBox(tr("SMESH_MAKE_GROUPS"), GroupArguments);
171 myCopyGroupsCheck->setChecked(false);
173 // CheckBox for keeping ids
174 myKeepIdsCheck = new QCheckBox(tr("SMESH_KEEP_IDS"), GroupArguments);
175 myKeepIdsCheck->setChecked(true);
178 GroupArgumentsLayout->addWidget(myTextLabelElements, 0, 0);
179 GroupArgumentsLayout->addWidget(myLineEditElements, 0, 1, 1, 5);
180 GroupArgumentsLayout->addWidget(myFilterBtn, 0, 6);
181 GroupArgumentsLayout->addWidget(myIdSourceCheck, 1, 0, 1, 6);
182 GroupArgumentsLayout->addWidget(meshNameLabel, 2, 0);
183 GroupArgumentsLayout->addWidget(myMeshNameEdit, 2, 1, 1, 5);
184 GroupArgumentsLayout->addWidget(myCopyGroupsCheck, 3, 0, 1, 6);
185 GroupArgumentsLayout->addWidget(myKeepIdsCheck, 4, 0, 1, 6);
187 /***************************************************************/
188 GroupButtons = new QGroupBox(this);
189 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
190 GroupButtonsLayout->setSpacing(SPACING);
191 GroupButtonsLayout->setMargin(MARGIN);
193 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
194 buttonOk->setAutoDefault(true);
195 buttonOk->setDefault(true);
196 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
197 buttonApply->setAutoDefault(true);
198 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
199 buttonCancel->setAutoDefault(true);
200 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
201 buttonHelp->setAutoDefault(true);
203 GroupButtonsLayout->addWidget(buttonOk);
204 GroupButtonsLayout->addSpacing(10);
205 GroupButtonsLayout->addWidget(buttonApply);
206 GroupButtonsLayout->addSpacing(10);
207 GroupButtonsLayout->addStretch();
208 GroupButtonsLayout->addWidget(buttonCancel);
209 GroupButtonsLayout->addWidget(buttonHelp);
211 /***************************************************************/
212 SMESHGUI_CopyMeshDlgLayout->addWidget(ConstructorsBox);
213 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupArguments);
214 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupButtons);
216 /* Initialisations */
217 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
219 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
222 myIdSourceFilter = new SMESH_TypeFilter( IDSOURCE );
224 myHelpFileName = "copy_mesh_page.html";
228 /* signals and slots connections */
229 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
230 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
231 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
232 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
234 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()),
235 this, SLOT (DeactivateActiveDialog()));
236 connect(mySelectionMgr, SIGNAL (currentSelectionChanged()),
237 this, SLOT (SelectionIntoArgument()));
238 connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),/* to close dialog if study change */
239 this, SLOT (ClickOnCancel()));
241 connect(myLineEditElements, SIGNAL(textChanged(const QString&)),
242 this, SLOT (onTextChange(const QString&)));
243 connect(myIdSourceCheck, SIGNAL(toggled(bool)),
244 this, SLOT (onSelectIdSource(bool)));
246 SelectionIntoArgument();
249 //=================================================================================
250 // function : ~SMESHGUI_CopyMeshDlg()
251 // purpose : Destroys the object and frees any allocated resources
252 //=================================================================================
254 SMESHGUI_CopyMeshDlg::~SMESHGUI_CopyMeshDlg()
258 myFilterDlg->setParent( 0 );
259 delete myFilterDlg; myFilterDlg = 0;
261 if ( myIdSourceFilter )
263 if ( mySelectionMgr )
264 mySelectionMgr->removeFilter( myIdSourceFilter );
265 delete myIdSourceFilter; myIdSourceFilter=0;
269 //=================================================================================
272 //=================================================================================
273 void SMESHGUI_CopyMeshDlg::Init (bool ResetControls)
277 myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
280 myLineEditElements->clear();
284 buttonOk->setEnabled(false);
285 buttonApply->setEnabled(false);
288 myMesh = SMESH::SMESH_Mesh::_nil();
290 myIdSourceCheck->setChecked(true);
291 myCopyGroupsCheck->setChecked(false);
292 myKeepIdsCheck->setChecked(false);
294 onSelectIdSource( myIdSourceCheck->isChecked() );
298 //=================================================================================
299 // function : ClickOnApply()
301 //=================================================================================
303 bool SMESHGUI_CopyMeshDlg::ClickOnApply()
305 if (mySMESHGUI->isActiveStudyLocked())
313 SUIT_OverrideCursor aWaitCursor;
314 SMESH::SMESH_IDSource_var aPartToCopy;
315 if ( myIdSourceCheck->isChecked())
317 aPartToCopy = mySelectedObject;
321 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
322 SMESH::long_array_var anElementsId = new SMESH::long_array;
323 anElementsId->length(aListElementsId.count());
324 for (int i = 0; i < aListElementsId.count(); i++)
325 anElementsId[i] = aListElementsId[i].toInt();
327 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
328 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
330 QByteArray meshName = myMeshNameEdit->text().toLatin1();
331 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
332 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
334 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
335 SMESH::SMESH_Mesh_var newMesh =
336 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
337 #ifdef WITHGENERICOBJ
338 // obj has been published in study. Its refcount has been incremented.
339 // It is safe to decrement its refcount
340 // so that it will be destroyed when the entry in study will be removed
341 newMesh->UnRegister();
346 mySMESHGUI->updateObjBrowser(true);
347 SMESHGUI::Modified();
350 mySelectedObject = SMESH::SMESH_IDSource::_nil();
351 SelectionIntoArgument();
356 //=================================================================================
357 // function : ClickOnOk()
359 //=================================================================================
360 void SMESHGUI_CopyMeshDlg::ClickOnOk()
366 //=================================================================================
367 // function : ClickOnCancel()
369 //=================================================================================
370 void SMESHGUI_CopyMeshDlg::ClickOnCancel()
372 disconnect(mySelectionMgr, 0, this, 0);
373 if ( mySelectionMgr )
374 mySelectionMgr->removeFilter( myIdSourceFilter );
375 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
376 aViewWindow->SetSelectionMode( ActorSelection );
377 mySMESHGUI->ResetState();
381 //=================================================================================
382 // function : ClickOnHelp()
384 //=================================================================================
385 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
387 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
389 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
393 platform = "winapplication";
395 platform = "application";
397 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
398 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
399 arg(app->resourceMgr()->stringValue("ExternalBrowser",
401 arg(myHelpFileName));
405 //=======================================================================
406 // function : onTextChange()
408 //=======================================================================
410 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
412 QLineEdit* send = (QLineEdit*)sender();
415 BusyLocker lock( myBusy );
417 //if (send == myLineEditElements)
420 buttonOk->setEnabled(false);
421 buttonApply->setEnabled(false);
423 // hilight entered elements
424 SMDS_Mesh* aMesh = 0;
426 aMesh = myActor->GetObject()->GetMesh();
428 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
429 if (myActor && aMesh)
431 TColStd_MapOfInteger newIndices;
432 if (send == myLineEditElements) {
433 for (int i = 0; i < aListId.count(); i++)
434 if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
436 newIndices.Add(e->GetID());
439 myNbOkElements = newIndices.Extent();
441 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
442 mySelector->AddOrRemoveIndex( anIO, newIndices, false );
443 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
444 aViewWindow->highlight( anIO, true, true );
448 myNbOkElements = aListId.count();
451 if (myNbOkElements) {
452 buttonOk->setEnabled(true);
453 buttonApply->setEnabled(true);
457 //=================================================================================
458 // function : SelectionIntoArgument()
459 // purpose : Called when selection as changed or other case
460 //=================================================================================
462 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
465 BusyLocker lock( myBusy );
469 QString aString = "";
471 myLineEditElements->setText(aString);
473 buttonOk->setEnabled(false);
474 buttonApply->setEnabled(false);
475 myFilterBtn->setEnabled(false);
479 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
480 int nbSel = aList.Extent();
484 Handle(SALOME_InteractiveObject) IO = aList.First();
485 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
486 if ( mySelectedObject->_is_nil() )
489 myMesh = SMESH::GetMeshByIO(IO);
490 if (myMesh->_is_nil())
493 myActor = SMESH::FindActorByEntry(IO->getEntry());
495 myActor = SMESH::FindActorByObject(myMesh);
497 if (myIdSourceCheck->isChecked())
499 SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
500 if ( aString.isEmpty() ) aString = " ";
504 SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
505 myNbOkElements = aString.size();
506 myFilterBtn->setEnabled(true);
508 myLineEditElements->setText( aString );
509 bool ok = !aString.isEmpty();
511 buttonOk->setEnabled(ok);
512 buttonApply->setEnabled(ok);
515 //=======================================================================
516 //function : onSelectIdSource
518 //=======================================================================
519 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
522 myTextLabelElements->setText(tr("OBJECT_NAME"));
524 myTextLabelElements->setText(tr("ELEM_IDS"));
527 myLineEditElements->clear();
530 mySelectionMgr->clearFilters();
531 mySelectionMgr->installFilter(myIdSourceFilter);
532 SMESH::SetPointRepresentation(false);
535 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
536 aViewWindow->SetSelectionMode( ActorSelection );
537 myLineEditElements->setReadOnly(true);
538 myLineEditElements->setValidator(0);
542 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
543 aViewWindow->SetSelectionMode( CellSelection );
544 myLineEditElements->setReadOnly(false);
545 myLineEditElements->setValidator(myIdValidator);
546 onTextChange(myLineEditElements->text());
549 SelectionIntoArgument();
552 //=================================================================================
553 // function : isValid
555 //=================================================================================
557 bool SMESHGUI_CopyMeshDlg::isValid()
559 if ( myIdSourceCheck->isChecked() )
560 return !mySelectedObject->_is_nil();
562 return myNbOkElements > 0;
565 //=================================================================================
566 // function : DeactivateActiveDialog()
568 //=================================================================================
569 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
571 if (ConstructorsBox->isEnabled()) {
572 ConstructorsBox->setEnabled(false);
573 GroupArguments->setEnabled(false);
574 GroupButtons->setEnabled(false);
575 mySMESHGUI->ResetState();
576 mySMESHGUI->SetActiveDialogBox(0);
577 if ( mySelectionMgr )
578 mySelectionMgr->removeFilter( myIdSourceFilter );
582 //=================================================================================
583 // function : ActivateThisDialog()
585 //=================================================================================
586 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
588 /* Emit a signal to deactivate the active dialog */
589 mySMESHGUI->EmitSignalDeactivateDialog();
590 ConstructorsBox->setEnabled(true);
591 GroupArguments->setEnabled(true);
592 GroupButtons->setEnabled(true);
594 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
596 onSelectIdSource( myIdSourceCheck->isChecked() );
598 SelectionIntoArgument();
601 //=================================================================================
602 // function : enterEvent()
604 //=================================================================================
605 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
607 if (!ConstructorsBox->isEnabled())
608 ActivateThisDialog();
611 //=================================================================================
612 // function : closeEvent()
614 //=================================================================================
615 void SMESHGUI_CopyMeshDlg::closeEvent (QCloseEvent*)
617 /* same than click on cancel button */
621 //=======================================================================
622 //function : hideEvent
623 //purpose : caused by ESC key
624 //=======================================================================
625 void SMESHGUI_CopyMeshDlg::hideEvent (QHideEvent*)
631 //=================================================================================
632 // function : keyPressEvent()
634 //=================================================================================
635 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
637 QDialog::keyPressEvent( e );
638 if ( e->isAccepted() )
641 if ( e->key() == Qt::Key_F1 ) {
647 //=================================================================================
648 // function : setFilters()
649 // purpose : SLOT. Called when "Filter" button pressed.
650 //=================================================================================
651 void SMESHGUI_CopyMeshDlg::setFilters()
653 if(myMesh->_is_nil()) {
654 SUIT_MessageBox::critical(this,
656 tr("NO_MESH_SELECTED"));
660 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
662 myFilterDlg->SetSelection();
663 myFilterDlg->SetMesh( myMesh );
664 myFilterDlg->SetSourceWg( myLineEditElements );