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
102 //================================================================================
106 //================================================================================
108 SMESHGUI_CopyMeshDlg::SMESHGUI_CopyMeshDlg( SMESHGUI* theModule )
109 : QDialog( SMESH::GetDesktop( theModule ) ),
110 mySMESHGUI( theModule ),
111 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
113 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
170 myKeepIdsCheck = new QCheckBox(tr("SMESH_KEEP_IDS"), GroupArguments);
171 myKeepIdsCheck->setChecked(true);
174 GroupArgumentsLayout->addWidget(myTextLabelElements, 0, 0);
175 GroupArgumentsLayout->addWidget(myLineEditElements, 0, 1, 1, 5);
176 GroupArgumentsLayout->addWidget(myFilterBtn, 0, 6);
177 GroupArgumentsLayout->addWidget(myIdSourceCheck, 1, 0, 1, 6);
178 GroupArgumentsLayout->addWidget(meshNameLabel, 2, 0);
179 GroupArgumentsLayout->addWidget(myMeshNameEdit, 2, 1, 1, 5);
180 GroupArgumentsLayout->addWidget(myCopyGroupsCheck, 3, 0, 1, 6);
181 GroupArgumentsLayout->addWidget(myKeepIdsCheck, 4, 0, 1, 6);
183 /***************************************************************/
184 GroupButtons = new QGroupBox(this);
185 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
186 GroupButtonsLayout->setSpacing(SPACING);
187 GroupButtonsLayout->setMargin(MARGIN);
189 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
190 buttonOk->setAutoDefault(true);
191 buttonOk->setDefault(true);
192 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
193 buttonApply->setAutoDefault(true);
194 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
195 buttonCancel->setAutoDefault(true);
196 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
197 buttonHelp->setAutoDefault(true);
199 GroupButtonsLayout->addWidget(buttonOk);
200 GroupButtonsLayout->addSpacing(10);
201 GroupButtonsLayout->addWidget(buttonApply);
202 GroupButtonsLayout->addSpacing(10);
203 GroupButtonsLayout->addStretch();
204 GroupButtonsLayout->addWidget(buttonCancel);
205 GroupButtonsLayout->addWidget(buttonHelp);
207 /***************************************************************/
208 SMESHGUI_CopyMeshDlgLayout->addWidget(ConstructorsBox);
209 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupArguments);
210 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupButtons);
212 /* Initialisations */
213 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
215 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
218 myIdSourceFilter = new SMESH_TypeFilter( SMESH::IDSOURCE );
220 myHelpFileName = "copy_mesh_page.html";
224 /* signals and slots connections */
225 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
226 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
227 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
228 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
230 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()),
231 this, SLOT (DeactivateActiveDialog()));
232 connect(mySelectionMgr, SIGNAL (currentSelectionChanged()),
233 this, SLOT (SelectionIntoArgument()));
234 connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),/* to close dialog if study change */
235 this, SLOT (reject()));
237 connect(myLineEditElements, SIGNAL(textChanged(const QString&)),
238 this, SLOT (onTextChange(const QString&)));
239 connect(myIdSourceCheck, SIGNAL(toggled(bool)),
240 this, SLOT (onSelectIdSource(bool)));
242 SelectionIntoArgument();
245 //=================================================================================
246 // function : ~SMESHGUI_CopyMeshDlg()
247 // purpose : Destroys the object and frees any allocated resources
248 //=================================================================================
250 SMESHGUI_CopyMeshDlg::~SMESHGUI_CopyMeshDlg()
254 myFilterDlg->setParent( 0 );
255 delete myFilterDlg; myFilterDlg = 0;
257 if ( myIdSourceFilter )
259 if ( mySelectionMgr )
260 mySelectionMgr->removeFilter( myIdSourceFilter );
261 delete myIdSourceFilter; myIdSourceFilter=0;
265 //=================================================================================
268 //=================================================================================
269 void SMESHGUI_CopyMeshDlg::Init (bool ResetControls)
273 myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
276 myLineEditElements->clear();
280 buttonOk->setEnabled(false);
281 buttonApply->setEnabled(false);
284 myMesh = SMESH::SMESH_Mesh::_nil();
286 myIdSourceCheck->setChecked(true);
287 myCopyGroupsCheck->setChecked(false);
288 myKeepIdsCheck->setChecked(false);
290 onSelectIdSource( myIdSourceCheck->isChecked() );
294 //=================================================================================
295 // function : ClickOnApply()
297 //=================================================================================
299 bool SMESHGUI_CopyMeshDlg::ClickOnApply()
301 if (mySMESHGUI->isActiveStudyLocked())
307 QStringList anEntryList;
310 SUIT_OverrideCursor aWaitCursor;
311 SMESH::SMESH_IDSource_wrap aPartToCopy;
312 if ( myIdSourceCheck->isChecked())
314 aPartToCopy = mySelectedObject;
315 aPartToCopy->Register();
319 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
320 SMESH::long_array_var anElementsId = new SMESH::long_array;
321 anElementsId->length(aListElementsId.count());
322 for (int i = 0; i < aListElementsId.count(); i++)
323 anElementsId[i] = aListElementsId[i].toInt();
325 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
326 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
328 QByteArray meshName = myMeshNameEdit->text().toLatin1();
329 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
330 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
332 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
333 SMESH::SMESH_Mesh_var newMesh =
334 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
335 if( !newMesh->_is_nil() )
336 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
337 anEntryList.append( aSObject->GetID().c_str() );
342 mySMESHGUI->updateObjBrowser(true);
343 SMESHGUI::Modified();
345 if( LightApp_Application* anApp =
346 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
347 anApp->browseObjects( anEntryList, isApplyAndClose() );
350 mySelectedObject = SMESH::SMESH_IDSource::_nil();
351 SelectionIntoArgument();
356 //=================================================================================
357 // function : ClickOnOk()
359 //=================================================================================
360 void SMESHGUI_CopyMeshDlg::ClickOnOk()
362 setIsApplyAndClose( true );
367 //=================================================================================
368 // function : reject()
370 //=================================================================================
371 void SMESHGUI_CopyMeshDlg::reject()
373 disconnect(mySelectionMgr, 0, this, 0);
374 if ( mySelectionMgr )
375 mySelectionMgr->removeFilter( myIdSourceFilter );
376 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
377 aViewWindow->SetSelectionMode( ActorSelection );
378 mySMESHGUI->ResetState();
382 //=================================================================================
383 // function : ClickOnHelp()
385 //=================================================================================
386 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
388 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
390 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
394 platform = "winapplication";
396 platform = "application";
398 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
399 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
400 arg(app->resourceMgr()->stringValue("ExternalBrowser",
402 arg(myHelpFileName));
406 //=======================================================================
407 // function : onTextChange()
409 //=======================================================================
411 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
413 QLineEdit* send = (QLineEdit*)sender();
416 BusyLocker lock( myBusy );
418 //if (send == myLineEditElements)
421 buttonOk->setEnabled(false);
422 buttonApply->setEnabled(false);
424 // hilight entered elements
425 SMDS_Mesh* aMesh = 0;
427 aMesh = myActor->GetObject()->GetMesh();
429 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
430 if (myActor && aMesh)
432 TColStd_MapOfInteger newIndices;
433 if (send == myLineEditElements) {
434 for (int i = 0; i < aListId.count(); i++)
435 if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
437 newIndices.Add(e->GetID());
440 myNbOkElements = newIndices.Extent();
442 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
443 mySelector->AddOrRemoveIndex( anIO, newIndices, false );
444 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
445 aViewWindow->highlight( anIO, true, true );
449 myNbOkElements = aListId.count();
452 if (myNbOkElements) {
453 buttonOk->setEnabled(true);
454 buttonApply->setEnabled(true);
458 //=================================================================================
459 // function : SelectionIntoArgument()
460 // purpose : Called when selection as changed or other case
461 //=================================================================================
463 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
466 BusyLocker lock( myBusy );
470 QString aString = "";
472 myLineEditElements->setText(aString);
474 buttonOk->setEnabled(false);
475 buttonApply->setEnabled(false);
476 myFilterBtn->setEnabled(false);
480 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
481 int nbSel = aList.Extent();
485 Handle(SALOME_InteractiveObject) IO = aList.First();
486 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
487 if ( mySelectedObject->_is_nil() )
490 myMesh = SMESH::GetMeshByIO(IO);
491 if (myMesh->_is_nil())
494 myActor = SMESH::FindActorByEntry(IO->getEntry());
496 myActor = SMESH::FindActorByObject(myMesh);
498 if (myIdSourceCheck->isChecked())
500 SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
501 if ( aString.isEmpty() ) aString = " ";
502 else aString = aString.trimmed(); // issue 0021327
506 SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
507 myNbOkElements = aString.size();
508 myFilterBtn->setEnabled(true);
510 myLineEditElements->setText( aString );
511 bool ok = !aString.isEmpty();
513 buttonOk->setEnabled(ok);
514 buttonApply->setEnabled(ok);
517 //=======================================================================
518 //function : onSelectIdSource
520 //=======================================================================
521 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
524 myTextLabelElements->setText(tr("OBJECT_NAME"));
526 myTextLabelElements->setText(tr("ELEM_IDS"));
529 myLineEditElements->clear();
532 mySelectionMgr->clearFilters();
533 mySelectionMgr->installFilter(myIdSourceFilter);
534 SMESH::SetPointRepresentation(false);
537 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
538 aViewWindow->SetSelectionMode( ActorSelection );
539 myLineEditElements->setReadOnly(true);
540 myLineEditElements->setValidator(0);
544 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
545 aViewWindow->SetSelectionMode( CellSelection );
546 myLineEditElements->setReadOnly(false);
547 myLineEditElements->setValidator(myIdValidator);
548 onTextChange(myLineEditElements->text());
551 SelectionIntoArgument();
554 //=================================================================================
555 // function : isValid
557 //=================================================================================
559 bool SMESHGUI_CopyMeshDlg::isValid()
561 if ( myIdSourceCheck->isChecked() )
562 return !mySelectedObject->_is_nil();
564 return myNbOkElements > 0;
567 //=================================================================================
568 // function : DeactivateActiveDialog()
570 //=================================================================================
571 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
573 if (ConstructorsBox->isEnabled()) {
574 ConstructorsBox->setEnabled(false);
575 GroupArguments->setEnabled(false);
576 GroupButtons->setEnabled(false);
577 mySMESHGUI->ResetState();
578 mySMESHGUI->SetActiveDialogBox(0);
579 if ( mySelectionMgr )
580 mySelectionMgr->removeFilter( myIdSourceFilter );
584 //=================================================================================
585 // function : ActivateThisDialog()
587 //=================================================================================
588 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
590 /* Emit a signal to deactivate the active dialog */
591 mySMESHGUI->EmitSignalDeactivateDialog();
592 ConstructorsBox->setEnabled(true);
593 GroupArguments->setEnabled(true);
594 GroupButtons->setEnabled(true);
596 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
598 onSelectIdSource( myIdSourceCheck->isChecked() );
600 SelectionIntoArgument();
603 //=================================================================================
604 // function : enterEvent()
606 //=================================================================================
607 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
609 if (!ConstructorsBox->isEnabled())
610 ActivateThisDialog();
613 //=================================================================================
614 // function : keyPressEvent()
616 //=================================================================================
617 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
619 QDialog::keyPressEvent( e );
620 if ( e->isAccepted() )
623 if ( e->key() == Qt::Key_F1 ) {
629 //=================================================================================
630 // function : setFilters()
631 // purpose : SLOT. Called when "Filter" button pressed.
632 //=================================================================================
633 void SMESHGUI_CopyMeshDlg::setFilters()
635 if(myMesh->_is_nil()) {
636 SUIT_MessageBox::critical(this,
638 tr("NO_MESH_SELECTED"));
642 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
644 myFilterDlg->SetSelection();
645 myFilterDlg->SetMesh( myMesh );
646 myFilterDlg->SetSourceWg( myLineEditElements );
651 //================================================================
652 // function : setIsApplyAndClose
653 // Purpose : Set value of the flag indicating that the dialog is
654 // accepted by Apply & Close button
655 //================================================================
656 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
658 myIsApplyAndClose = theFlag;
661 //================================================================
662 // function : isApplyAndClose
663 // Purpose : Get value of the flag indicating that the dialog is
664 // accepted by Apply & Close button
665 //================================================================
666 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
668 return myIsApplyAndClose;