1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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 ( OBSOLETE )
170 myKeepIdsCheck = new QCheckBox(tr("SMESH_KEEP_IDS"), GroupArguments);
171 myKeepIdsCheck->setChecked(true);
172 myKeepIdsCheck->hide();
175 GroupArgumentsLayout->addWidget(myTextLabelElements, 0, 0);
176 GroupArgumentsLayout->addWidget(myLineEditElements, 0, 1, 1, 5);
177 GroupArgumentsLayout->addWidget(myFilterBtn, 0, 6);
178 GroupArgumentsLayout->addWidget(myIdSourceCheck, 1, 0, 1, 6);
179 GroupArgumentsLayout->addWidget(meshNameLabel, 2, 0);
180 GroupArgumentsLayout->addWidget(myMeshNameEdit, 2, 1, 1, 5);
181 GroupArgumentsLayout->addWidget(myCopyGroupsCheck, 3, 0, 1, 6);
182 // GroupArgumentsLayout->addWidget(myKeepIdsCheck, 4, 0, 1, 6);
184 /***************************************************************/
185 GroupButtons = new QGroupBox(this);
186 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
187 GroupButtonsLayout->setSpacing(SPACING);
188 GroupButtonsLayout->setMargin(MARGIN);
190 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
191 buttonOk->setAutoDefault(true);
192 buttonOk->setDefault(true);
193 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
194 buttonApply->setAutoDefault(true);
195 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
196 buttonCancel->setAutoDefault(true);
197 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
198 buttonHelp->setAutoDefault(true);
200 GroupButtonsLayout->addWidget(buttonOk);
201 GroupButtonsLayout->addSpacing(10);
202 GroupButtonsLayout->addWidget(buttonApply);
203 GroupButtonsLayout->addSpacing(10);
204 GroupButtonsLayout->addStretch();
205 GroupButtonsLayout->addWidget(buttonCancel);
206 GroupButtonsLayout->addWidget(buttonHelp);
208 /***************************************************************/
209 SMESHGUI_CopyMeshDlgLayout->addWidget(ConstructorsBox);
210 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupArguments);
211 SMESHGUI_CopyMeshDlgLayout->addWidget(GroupButtons);
213 /* Initialisations */
214 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
216 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
219 myIdSourceFilter = new SMESH_TypeFilter( SMESH::IDSOURCE );
221 myHelpFileName = "copy_mesh_page.html";
225 /* signals and slots connections */
226 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
227 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
228 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
229 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
231 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()),
232 this, SLOT (DeactivateActiveDialog()));
233 connect(mySelectionMgr, SIGNAL (currentSelectionChanged()),
234 this, SLOT (SelectionIntoArgument()));
235 connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),/* to close dialog if study change */
236 this, SLOT (reject()));
238 connect(myLineEditElements, SIGNAL(textChanged(const QString&)),
239 this, SLOT (onTextChange(const QString&)));
240 connect(myIdSourceCheck, SIGNAL(toggled(bool)),
241 this, SLOT (onSelectIdSource(bool)));
243 SelectionIntoArgument();
246 //=================================================================================
247 // function : ~SMESHGUI_CopyMeshDlg()
248 // purpose : Destroys the object and frees any allocated resources
249 //=================================================================================
251 SMESHGUI_CopyMeshDlg::~SMESHGUI_CopyMeshDlg()
255 myFilterDlg->setParent( 0 );
256 delete myFilterDlg; myFilterDlg = 0;
258 if ( myIdSourceFilter )
260 if ( mySelectionMgr )
261 mySelectionMgr->removeFilter( myIdSourceFilter );
262 delete myIdSourceFilter; myIdSourceFilter=0;
266 //=================================================================================
269 //=================================================================================
270 void SMESHGUI_CopyMeshDlg::Init (bool ResetControls)
274 myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
277 myLineEditElements->clear();
281 buttonOk->setEnabled(false);
282 buttonApply->setEnabled(false);
285 myMesh = SMESH::SMESH_Mesh::_nil();
287 myIdSourceCheck->setChecked(true);
288 myCopyGroupsCheck->setChecked(false);
289 myKeepIdsCheck->setChecked(false);
291 onSelectIdSource( myIdSourceCheck->isChecked() );
295 //=================================================================================
296 // function : ClickOnApply()
298 //=================================================================================
300 bool SMESHGUI_CopyMeshDlg::ClickOnApply()
302 if (mySMESHGUI->isActiveStudyLocked())
308 QStringList anEntryList;
311 SUIT_OverrideCursor aWaitCursor;
312 SMESH::SMESH_IDSource_wrap aPartToCopy;
313 if ( myIdSourceCheck->isChecked())
315 aPartToCopy = mySelectedObject;
316 aPartToCopy->Register();
320 QStringList aListElementsId = myLineEditElements->text().split(" ", QString::SkipEmptyParts);
321 SMESH::long_array_var anElementsId = new SMESH::long_array;
322 anElementsId->length(aListElementsId.count());
323 for (int i = 0; i < aListElementsId.count(); i++)
324 anElementsId[i] = aListElementsId[i].toInt();
326 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
327 aPartToCopy = aMeshEditor->MakeIDSource( anElementsId, SMESH::ALL );
329 QByteArray meshName = myMeshNameEdit->text().toLatin1();
330 bool toCopyGroups = ( myCopyGroupsCheck->isChecked() );
331 bool toKeepIDs = ( myKeepIdsCheck->isChecked() );
333 SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
334 SMESH::SMESH_Mesh_var newMesh =
335 gen->CopyMesh(aPartToCopy, meshName.constData(), toCopyGroups, toKeepIDs);
336 if( !newMesh->_is_nil() )
337 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
338 anEntryList.append( aSObject->GetID().c_str() );
343 mySMESHGUI->updateObjBrowser(true);
344 SMESHGUI::Modified();
346 if( LightApp_Application* anApp =
347 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
348 anApp->browseObjects( anEntryList, isApplyAndClose() );
351 mySelectedObject = SMESH::SMESH_IDSource::_nil();
352 SelectionIntoArgument();
357 //=================================================================================
358 // function : ClickOnOk()
360 //=================================================================================
361 void SMESHGUI_CopyMeshDlg::ClickOnOk()
363 setIsApplyAndClose( true );
368 //=================================================================================
369 // function : reject()
371 //=================================================================================
372 void SMESHGUI_CopyMeshDlg::reject()
374 disconnect(mySelectionMgr, 0, this, 0);
375 if ( mySelectionMgr )
376 mySelectionMgr->removeFilter( myIdSourceFilter );
377 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
378 aViewWindow->SetSelectionMode( ActorSelection );
379 mySMESHGUI->ResetState();
383 //=================================================================================
384 // function : ClickOnHelp()
386 //=================================================================================
387 void SMESHGUI_CopyMeshDlg::ClickOnHelp()
389 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
391 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
395 platform = "winapplication";
397 platform = "application";
399 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
400 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
401 arg(app->resourceMgr()->stringValue("ExternalBrowser",
403 arg(myHelpFileName));
407 //=======================================================================
408 // function : onTextChange()
410 //=======================================================================
412 void SMESHGUI_CopyMeshDlg::onTextChange (const QString& theNewText)
414 QLineEdit* send = (QLineEdit*)sender();
417 BusyLocker lock( myBusy );
419 //if (send == myLineEditElements)
422 buttonOk->setEnabled(false);
423 buttonApply->setEnabled(false);
425 // hilight entered elements
426 SMDS_Mesh* aMesh = 0;
428 aMesh = myActor->GetObject()->GetMesh();
430 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
431 if (myActor && aMesh)
433 TColStd_MapOfInteger newIndices;
434 if (send == myLineEditElements) {
435 for (int i = 0; i < aListId.count(); i++)
436 if ( const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt()))
438 newIndices.Add(e->GetID());
441 myNbOkElements = newIndices.Extent();
443 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
444 mySelector->AddOrRemoveIndex( anIO, newIndices, false );
445 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
446 aViewWindow->highlight( anIO, true, true );
450 myNbOkElements = aListId.count();
453 if (myNbOkElements) {
454 buttonOk->setEnabled(true);
455 buttonApply->setEnabled(true);
459 //=================================================================================
460 // function : SelectionIntoArgument()
461 // purpose : Called when selection as changed or other case
462 //=================================================================================
464 void SMESHGUI_CopyMeshDlg::SelectionIntoArgument()
467 BusyLocker lock( myBusy );
471 QString aString = "";
473 myLineEditElements->setText(aString);
475 buttonOk->setEnabled(false);
476 buttonApply->setEnabled(false);
477 myFilterBtn->setEnabled(false);
481 mySelectionMgr->selectedObjects(aList);
482 int nbSel = aList.Extent();
486 Handle(SALOME_InteractiveObject) IO = aList.First();
487 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
488 if ( mySelectedObject->_is_nil() )
491 myMesh = SMESH::GetMeshByIO(IO);
492 if (myMesh->_is_nil())
495 myActor = SMESH::FindActorByEntry(IO->getEntry());
497 myActor = SMESH::FindActorByObject(myMesh);
499 if (myIdSourceCheck->isChecked())
501 SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aString );
502 if ( aString.isEmpty() ) aString = " ";
503 else aString = aString.trimmed(); // issue 0021327
507 SMESH::GetNameOfSelectedElements( mySelector, IO, aString );
508 myNbOkElements = aString.size();
509 myFilterBtn->setEnabled(true);
511 myLineEditElements->setText( aString );
512 bool ok = !aString.isEmpty();
514 buttonOk->setEnabled(ok);
515 buttonApply->setEnabled(ok);
518 //=======================================================================
519 //function : onSelectIdSource
521 //=======================================================================
522 void SMESHGUI_CopyMeshDlg::onSelectIdSource (bool toSelectMesh)
525 myTextLabelElements->setText(tr("OBJECT_NAME"));
527 myTextLabelElements->setText(tr("ELEM_IDS"));
530 myLineEditElements->clear();
533 mySelectionMgr->clearFilters();
534 mySelectionMgr->installFilter(myIdSourceFilter);
535 SMESH::SetPointRepresentation(false);
538 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
539 aViewWindow->SetSelectionMode( ActorSelection );
540 myLineEditElements->setReadOnly(true);
541 myLineEditElements->setValidator(0);
545 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
546 aViewWindow->SetSelectionMode( CellSelection );
547 myLineEditElements->setReadOnly(false);
548 myLineEditElements->setValidator(myIdValidator);
549 onTextChange(myLineEditElements->text());
552 SelectionIntoArgument();
555 //=================================================================================
556 // function : isValid
558 //=================================================================================
560 bool SMESHGUI_CopyMeshDlg::isValid()
562 if ( myIdSourceCheck->isChecked() )
563 return !mySelectedObject->_is_nil();
565 return myNbOkElements > 0;
568 //=================================================================================
569 // function : DeactivateActiveDialog()
571 //=================================================================================
572 void SMESHGUI_CopyMeshDlg::DeactivateActiveDialog()
574 if (ConstructorsBox->isEnabled()) {
575 ConstructorsBox->setEnabled(false);
576 GroupArguments->setEnabled(false);
577 GroupButtons->setEnabled(false);
578 mySMESHGUI->ResetState();
579 mySMESHGUI->SetActiveDialogBox(0);
580 if ( mySelectionMgr )
581 mySelectionMgr->removeFilter( myIdSourceFilter );
585 //=================================================================================
586 // function : ActivateThisDialog()
588 //=================================================================================
589 void SMESHGUI_CopyMeshDlg::ActivateThisDialog()
591 /* Emit a signal to deactivate the active dialog */
592 mySMESHGUI->EmitSignalDeactivateDialog();
593 ConstructorsBox->setEnabled(true);
594 GroupArguments->setEnabled(true);
595 GroupButtons->setEnabled(true);
597 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
599 onSelectIdSource( myIdSourceCheck->isChecked() );
601 SelectionIntoArgument();
604 //=================================================================================
605 // function : enterEvent()
607 //=================================================================================
608 void SMESHGUI_CopyMeshDlg::enterEvent (QEvent*)
610 if (!ConstructorsBox->isEnabled())
611 ActivateThisDialog();
614 //=================================================================================
615 // function : keyPressEvent()
617 //=================================================================================
618 void SMESHGUI_CopyMeshDlg::keyPressEvent( QKeyEvent* e )
620 QDialog::keyPressEvent( e );
621 if ( e->isAccepted() )
624 if ( e->key() == Qt::Key_F1 ) {
630 //=================================================================================
631 // function : setFilters()
632 // purpose : SLOT. Called when "Filter" button pressed.
633 //=================================================================================
634 void SMESHGUI_CopyMeshDlg::setFilters()
636 if(myMesh->_is_nil()) {
637 SUIT_MessageBox::critical(this,
639 tr("NO_MESH_SELECTED"));
643 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
645 myFilterDlg->SetSelection();
646 myFilterDlg->SetMesh( myMesh );
647 myFilterDlg->SetSourceWg( myLineEditElements );
652 //================================================================
653 // function : setIsApplyAndClose
654 // Purpose : Set value of the flag indicating that the dialog is
655 // accepted by Apply & Close button
656 //================================================================
657 void SMESHGUI_CopyMeshDlg::setIsApplyAndClose( const bool theFlag )
659 myIsApplyAndClose = theFlag;
662 //================================================================
663 // function : isApplyAndClose
664 // Purpose : Get value of the flag indicating that the dialog is
665 // accepted by Apply & Close button
666 //================================================================
667 bool SMESHGUI_CopyMeshDlg::isApplyAndClose() const
669 return myIsApplyAndClose;