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_var aPartToCopy;
312 if ( myIdSourceCheck->isChecked())
314 aPartToCopy = mySelectedObject;
318 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
319 SMESH::long_array_var anElementsId = new SMESH::long_array;
320 anElementsId->length(aListElementsId.count());
321 for (int i = 0; i < aListElementsId.count(); i++)
322 anElementsId[i] = aListElementsId[i].toInt();
324 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
325 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
327 QByteArray meshName = myMeshNameEdit->text().toLatin1();
328 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
329 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
331 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
332 SMESH::SMESH_Mesh_var newMesh =
333 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
334 if( !newMesh->_is_nil() )
335 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
336 anEntryList.append( aSObject->GetID().c_str() );
341 mySMESHGUI->updateObjBrowser(true);
342 SMESHGUI::Modified();
344 if( LightApp_Application* anApp =
345 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
346 anApp->browseObjects( anEntryList, isApplyAndClose() );
349 mySelectedObject = SMESH::SMESH_IDSource::_nil();
350 SelectionIntoArgument();
355 //=================================================================================
356 // function : ClickOnOk()
358 //=================================================================================
359 void SMESHGUI_CopyMeshDlg::ClickOnOk()
361 setIsApplyAndClose( true );
366 //=================================================================================
367 // function : reject()
369 //=================================================================================
370 void SMESHGUI_CopyMeshDlg::reject()
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 = " ";
501 else aString = aString.trimmed(); // issue 0021327
505 SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
506 myNbOkElements = aString.size();
507 myFilterBtn->setEnabled(true);
509 myLineEditElements->setText( aString );
510 bool ok = !aString.isEmpty();
512 buttonOk->setEnabled(ok);
513 buttonApply->setEnabled(ok);
516 //=======================================================================
517 //function : onSelectIdSource
519 //=======================================================================
520 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
523 myTextLabelElements->setText(tr("OBJECT_NAME"));
525 myTextLabelElements->setText(tr("ELEM_IDS"));
528 myLineEditElements->clear();
531 mySelectionMgr->clearFilters();
532 mySelectionMgr->installFilter(myIdSourceFilter);
533 SMESH::SetPointRepresentation(false);
536 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
537 aViewWindow->SetSelectionMode( ActorSelection );
538 myLineEditElements->setReadOnly(true);
539 myLineEditElements->setValidator(0);
543 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
544 aViewWindow->SetSelectionMode( CellSelection );
545 myLineEditElements->setReadOnly(false);
546 myLineEditElements->setValidator(myIdValidator);
547 onTextChange(myLineEditElements->text());
550 SelectionIntoArgument();
553 //=================================================================================
554 // function : isValid
556 //=================================================================================
558 bool SMESHGUI_CopyMeshDlg::isValid()
560 if ( myIdSourceCheck->isChecked() )
561 return !mySelectedObject->_is_nil();
563 return myNbOkElements > 0;
566 //=================================================================================
567 // function : DeactivateActiveDialog()
569 //=================================================================================
570 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
572 if (ConstructorsBox->isEnabled()) {
573 ConstructorsBox->setEnabled(false);
574 GroupArguments->setEnabled(false);
575 GroupButtons->setEnabled(false);
576 mySMESHGUI->ResetState();
577 mySMESHGUI->SetActiveDialogBox(0);
578 if ( mySelectionMgr )
579 mySelectionMgr->removeFilter( myIdSourceFilter );
583 //=================================================================================
584 // function : ActivateThisDialog()
586 //=================================================================================
587 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
589 /* Emit a signal to deactivate the active dialog */
590 mySMESHGUI->EmitSignalDeactivateDialog();
591 ConstructorsBox->setEnabled(true);
592 GroupArguments->setEnabled(true);
593 GroupButtons->setEnabled(true);
595 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
597 onSelectIdSource( myIdSourceCheck->isChecked() );
599 SelectionIntoArgument();
602 //=================================================================================
603 // function : enterEvent()
605 //=================================================================================
606 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
608 if (!ConstructorsBox->isEnabled())
609 ActivateThisDialog();
612 //=================================================================================
613 // function : keyPressEvent()
615 //=================================================================================
616 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
618 QDialog::keyPressEvent( e );
619 if ( e->isAccepted() )
622 if ( e->key() == Qt::Key_F1 ) {
628 //=================================================================================
629 // function : setFilters()
630 // purpose : SLOT. Called when "Filter" button pressed.
631 //=================================================================================
632 void SMESHGUI_CopyMeshDlg::setFilters()
634 if(myMesh->_is_nil()) {
635 SUIT_MessageBox::critical(this,
637 tr("NO_MESH_SELECTED"));
641 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
643 myFilterDlg->SetSelection();
644 myFilterDlg->SetMesh( myMesh );
645 myFilterDlg->SetSourceWg( myLineEditElements );
650 //================================================================
651 // function : setIsApplyAndClose
652 // Purpose : Set value of the flag indicating that the dialog is
653 // accepted by Apply & Close button
654 //================================================================
655 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
657 myIsApplyAndClose = theFlag;
660 //================================================================
661 // function : isApplyAndClose
662 // Purpose : Get value of the flag indicating that the dialog is
663 // accepted by Apply & Close button
664 //================================================================
665 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
667 return myIsApplyAndClose;