1 // Copyright (C) 2007-2013 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 // 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
101 //To disable automatic genericobj management, the following line should be commented.
102 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
103 #define WITHGENERICOBJ
106 //================================================================================
110 //================================================================================
112 SMESHGUI_CopyMeshDlg::SMESHGUI_CopyMeshDlg( SMESHGUI* theModule )
113 : QDialog( SMESH::GetDesktop( theModule ) ),
114 mySMESHGUI( theModule ),
115 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
117 mySelectedObject(SMESH::SMESH_IDSource::_nil()),
118 myIsApplyAndClose( false )
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( SMESH::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(reject()));
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 (reject()));
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())
311 QStringList anEntryList;
314 SUIT_OverrideCursor aWaitCursor;
315 SMESH::SMESH_IDSource_var aPartToCopy;
316 if ( myIdSourceCheck->isChecked())
318 aPartToCopy = mySelectedObject;
322 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
323 SMESH::long_array_var anElementsId = new SMESH::long_array;
324 anElementsId->length(aListElementsId.count());
325 for (int i = 0; i < aListElementsId.count(); i++)
326 anElementsId[i] = aListElementsId[i].toInt();
328 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
329 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
331 QByteArray meshName = myMeshNameEdit->text().toLatin1();
332 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
333 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
335 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
336 SMESH::SMESH_Mesh_var newMesh =
337 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
338 if( !newMesh->_is_nil() )
339 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
340 anEntryList.append( aSObject->GetID().c_str() );
341 #ifdef WITHGENERICOBJ
342 // obj has been published in study. Its refcount has been incremented.
343 // It is safe to decrement its refcount
344 // so that it will be destroyed when the entry in study will be removed
345 newMesh->UnRegister();
350 mySMESHGUI->updateObjBrowser(true);
351 SMESHGUI::Modified();
353 if( LightApp_Application* anApp =
354 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
355 anApp->browseObjects( anEntryList, isApplyAndClose() );
358 mySelectedObject = SMESH::SMESH_IDSource::_nil();
359 SelectionIntoArgument();
364 //=================================================================================
365 // function : ClickOnOk()
367 //=================================================================================
368 void SMESHGUI_CopyMeshDlg::ClickOnOk()
370 setIsApplyAndClose( true );
375 //=================================================================================
376 // function : reject()
378 //=================================================================================
379 void SMESHGUI_CopyMeshDlg::reject()
381 disconnect(mySelectionMgr, 0, this, 0);
382 if ( mySelectionMgr )
383 mySelectionMgr->removeFilter( myIdSourceFilter );
384 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
385 aViewWindow->SetSelectionMode( ActorSelection );
386 mySMESHGUI->ResetState();
390 //=================================================================================
391 // function : ClickOnHelp()
393 //=================================================================================
394 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
396 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
398 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
402 platform = "winapplication";
404 platform = "application";
406 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
407 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
408 arg(app->resourceMgr()->stringValue("ExternalBrowser",
410 arg(myHelpFileName));
414 //=======================================================================
415 // function : onTextChange()
417 //=======================================================================
419 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
421 QLineEdit* send = (QLineEdit*)sender();
424 BusyLocker lock( myBusy );
426 //if (send == myLineEditElements)
429 buttonOk->setEnabled(false);
430 buttonApply->setEnabled(false);
432 // hilight entered elements
433 SMDS_Mesh* aMesh = 0;
435 aMesh = myActor->GetObject()->GetMesh();
437 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
438 if (myActor && aMesh)
440 TColStd_MapOfInteger newIndices;
441 if (send == myLineEditElements) {
442 for (int i = 0; i < aListId.count(); i++)
443 if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
445 newIndices.Add(e->GetID());
448 myNbOkElements = newIndices.Extent();
450 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
451 mySelector->AddOrRemoveIndex( anIO, newIndices, false );
452 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
453 aViewWindow->highlight( anIO, true, true );
457 myNbOkElements = aListId.count();
460 if (myNbOkElements) {
461 buttonOk->setEnabled(true);
462 buttonApply->setEnabled(true);
466 //=================================================================================
467 // function : SelectionIntoArgument()
468 // purpose : Called when selection as changed or other case
469 //=================================================================================
471 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
474 BusyLocker lock( myBusy );
478 QString aString = "";
480 myLineEditElements->setText(aString);
482 buttonOk->setEnabled(false);
483 buttonApply->setEnabled(false);
484 myFilterBtn->setEnabled(false);
488 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
489 int nbSel = aList.Extent();
493 Handle(SALOME_InteractiveObject) IO = aList.First();
494 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
495 if ( mySelectedObject->_is_nil() )
498 myMesh = SMESH::GetMeshByIO(IO);
499 if (myMesh->_is_nil())
502 myActor = SMESH::FindActorByEntry(IO->getEntry());
504 myActor = SMESH::FindActorByObject(myMesh);
506 if (myIdSourceCheck->isChecked())
508 SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
509 if ( aString.isEmpty() ) aString = " ";
510 else aString = aString.trimmed(); // issue 0021327
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 : keyPressEvent()
624 //=================================================================================
625 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
627 QDialog::keyPressEvent( e );
628 if ( e->isAccepted() )
631 if ( e->key() == Qt::Key_F1 ) {
637 //=================================================================================
638 // function : setFilters()
639 // purpose : SLOT. Called when "Filter" button pressed.
640 //=================================================================================
641 void SMESHGUI_CopyMeshDlg::setFilters()
643 if(myMesh->_is_nil()) {
644 SUIT_MessageBox::critical(this,
646 tr("NO_MESH_SELECTED"));
650 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
652 myFilterDlg->SetSelection();
653 myFilterDlg->SetMesh( myMesh );
654 myFilterDlg->SetSourceWg( myLineEditElements );
659 //================================================================
660 // function : setIsApplyAndClose
661 // Purpose : Set value of the flag indicating that the dialog is
662 // accepted by Apply & Close button
663 //================================================================
664 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
666 myIsApplyAndClose = theFlag;
669 //================================================================
670 // function : isApplyAndClose
671 // Purpose : Get value of the flag indicating that the dialog is
672 // accepted by Apply & Close button
673 //================================================================
674 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
676 return myIsApplyAndClose;