1 // Copyright (C) 2007-2011 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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_DuplicateNodesDlg.cxx
25 // Author : Michael ZORIN, Open CASCADE S.A.S.
28 #include "SMESHGUI_DuplicateNodesDlg.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
34 #include <SMESH_TypeFilter.hxx>
36 // SALOME GUI includes
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_OverrideCursor.h>
43 #include <LightApp_Application.h>
44 #include <LightApp_SelectionMgr.h>
46 #include <SalomeApp_Tools.h>
48 #include <SVTK_ViewWindow.h>
49 #include <SALOME_ListIO.hxx>
50 #include <SALOME_ListIteratorOfListIO.hxx>
53 #include <QApplication>
54 #include <QButtonGroup>
58 #include <QPushButton>
59 #include <QRadioButton>
61 #include <QHBoxLayout>
62 #include <QVBoxLayout>
65 #include <utilities.h>
68 #include <SALOMEconfig.h>
69 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
76 \brief Simple 'busy state' flag locker.
83 //! Constructor. Sets passed boolean flag to \c true.
84 BusyLocker( bool& busy ) : myBusy( busy ) { myBusy = true; }
85 //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
86 ~BusyLocker() { myBusy = false; }
88 bool& myBusy; //! External 'busy state' boolean flag
93 \param theModule Mesh module instance
95 SMESHGUI_DuplicateNodesDlg::SMESHGUI_DuplicateNodesDlg( SMESHGUI* theModule )
96 : QDialog( SMESH::GetDesktop( theModule ) ),
97 mySMESHGUI( theModule ),
98 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
102 setAttribute(Qt::WA_DeleteOnClose, true);
103 setWindowTitle(tr("SMESH_DUPLICATE_TITLE"));
104 setSizeGripEnabled(true);
106 // Icons for the dialog operation modes and selection button
107 SUIT_ResourceMgr* aResMgr = SMESH::GetResourceMgr( mySMESHGUI );
108 QPixmap iconWithoutElem (aResMgr->loadPixmap("SMESH", tr("ICON_SMESH_DUPLICATE_NODES")));
109 QPixmap iconWithElem (aResMgr->loadPixmap("SMESH", tr("ICON_SMESH_DUPLICATE_NODES_WITH_ELEM")));
110 QPixmap iconSelect (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
113 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
114 aMainLayout->setSpacing(SPACING);
115 aMainLayout->setMargin(MARGIN);
117 // Operation modes selector
118 QGroupBox* aConstructorsBox = new QGroupBox(tr("DUPLICATION_MODE"), this);
119 myGroupConstructors = new QButtonGroup(this);
120 QHBoxLayout* aConstructorsBoxLayout = new QHBoxLayout(aConstructorsBox);
121 aConstructorsBoxLayout->setSpacing(SPACING);
122 aConstructorsBoxLayout->setMargin(MARGIN);
124 QRadioButton* aRadioButton1 = new QRadioButton(aConstructorsBox);
125 aRadioButton1->setIcon(iconWithoutElem);
126 QRadioButton* aRadioButton2 = new QRadioButton(aConstructorsBox);
127 aRadioButton2->setIcon(iconWithElem);
129 aConstructorsBoxLayout->addWidget(aRadioButton1);
130 aConstructorsBoxLayout->addWidget(aRadioButton2);
131 myGroupConstructors->addButton(aRadioButton1, 0);
132 myGroupConstructors->addButton(aRadioButton2, 1);
135 myGroupArguments = new QGroupBox(this);
136 QGridLayout* aGroupArgumentsLayout = new QGridLayout(myGroupArguments);
137 aGroupArgumentsLayout->setSpacing(SPACING);
138 aGroupArgumentsLayout->setMargin(MARGIN);
140 myTextLabel1 = new QLabel(myGroupArguments);
141 mySelectButton1 = new QPushButton(myGroupArguments);
142 mySelectButton1->setIcon(iconSelect);
143 myLineEdit1 = new QLineEdit(myGroupArguments);
144 myLineEdit1->setReadOnly(true);
146 myTextLabel2 = new QLabel(myGroupArguments);
147 mySelectButton2 = new QPushButton(myGroupArguments);
148 mySelectButton2->setIcon(iconSelect);
149 myLineEdit2 = new QLineEdit(myGroupArguments);
150 myLineEdit2->setReadOnly(true);
152 myTextLabel3 = new QLabel(myGroupArguments);
153 mySelectButton3 = new QPushButton(myGroupArguments);
154 mySelectButton3->setIcon(iconSelect);
155 myLineEdit3 = new QLineEdit(myGroupArguments);
156 myLineEdit3->setReadOnly(true);
158 myCheckBoxNewGroup = new QCheckBox(tr("CONSTRUCT_NEW_GROUP_NODES"), myGroupArguments);
160 aGroupArgumentsLayout->addWidget(myTextLabel1, 0, 0);
161 aGroupArgumentsLayout->addWidget(mySelectButton1, 0, 1);
162 aGroupArgumentsLayout->addWidget(myLineEdit1, 0, 2);
163 aGroupArgumentsLayout->addWidget(myTextLabel2, 1, 0);
164 aGroupArgumentsLayout->addWidget(mySelectButton2, 1, 1);
165 aGroupArgumentsLayout->addWidget(myLineEdit2, 1, 2);
166 aGroupArgumentsLayout->addWidget(myTextLabel3, 2, 0);
167 aGroupArgumentsLayout->addWidget(mySelectButton3, 2, 1);
168 aGroupArgumentsLayout->addWidget(myLineEdit3, 2, 2);
169 aGroupArgumentsLayout->addWidget(myCheckBoxNewGroup, 3, 0);
170 aGroupArgumentsLayout->setRowStretch(4, 1);
173 QGroupBox* aGroupButtons = new QGroupBox(this);
174 QHBoxLayout* aGroupButtonsLayout = new QHBoxLayout(aGroupButtons);
175 aGroupButtonsLayout->setSpacing(SPACING);
176 aGroupButtonsLayout->setMargin(MARGIN);
178 myButtonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGroupButtons);
179 myButtonOk->setAutoDefault(true);
180 myButtonOk->setDefault(true);
181 myButtonApply = new QPushButton(tr("SMESH_BUT_APPLY"), aGroupButtons);
182 myButtonApply->setAutoDefault(true);
183 myButtonClose = new QPushButton(tr("SMESH_BUT_CLOSE"), aGroupButtons);
184 myButtonClose->setAutoDefault(true);
185 myButtonHelp = new QPushButton(tr("SMESH_BUT_HELP"), aGroupButtons);
186 myButtonHelp->setAutoDefault(true);
188 aGroupButtonsLayout->addWidget(myButtonOk);
189 aGroupButtonsLayout->addSpacing(10);
190 aGroupButtonsLayout->addWidget(myButtonApply);
191 aGroupButtonsLayout->addSpacing(10);
192 aGroupButtonsLayout->addStretch();
193 aGroupButtonsLayout->addWidget(myButtonClose);
194 aGroupButtonsLayout->addWidget(myButtonHelp);
196 // Add mode selector, arguments and buttons to the main layout
197 aMainLayout->addWidget(aConstructorsBox);
198 aMainLayout->addWidget(myGroupArguments);
199 aMainLayout->addWidget(aGroupButtons);
201 // Initialize the dialog
205 myHelpFileName = "double_nodes_page.html";
207 // Signals and slots connections
208 connect(myGroupConstructors, SIGNAL(buttonClicked(int)), SLOT(onConstructorsClicked(int)));
210 connect(mySelectButton1, SIGNAL (clicked()), this, SLOT(onEditCurrentArgument()));
211 connect(mySelectButton2, SIGNAL (clicked()), this, SLOT(onEditCurrentArgument()));
212 connect(mySelectButton3, SIGNAL (clicked()), this, SLOT(onEditCurrentArgument()));
214 connect(myButtonOk, SIGNAL(clicked()), this, SLOT(onOk()));
215 connect(myButtonClose, SIGNAL(clicked()), this, SLOT(onClose()));
216 connect(myButtonApply, SIGNAL(clicked()), this, SLOT(onApply()));
217 connect(myButtonHelp, SIGNAL(clicked()), this, SLOT(onHelp()));
219 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionChanged()));
221 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(onDeactivate()));
222 connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(onClose()));
228 SMESHGUI_DuplicateNodesDlg::~SMESHGUI_DuplicateNodesDlg()
235 void SMESHGUI_DuplicateNodesDlg::Init()
237 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
238 myCheckBoxNewGroup->setChecked(true);
240 // Set initial parameters
242 myCurrentLineEdit = myLineEdit1;
248 // Set selection mode
249 mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
250 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
251 aViewWindow->SetSelectionMode(ActorSelection);
253 // Set construction mode
254 int operationMode = myGroupConstructors->checkedId();
255 if (operationMode < 0) {
256 // The dialog has been just displayed
258 myGroupConstructors->button(0)->setChecked(true);
260 onConstructorsClicked(operationMode);
264 \brief SLOT called to change the dialog operation mode.
265 \param constructorId id of the radio button in mode selector button group
267 void SMESHGUI_DuplicateNodesDlg::onConstructorsClicked (int constructorId)
270 myLineEdit1->clear();
271 myLineEdit2->clear();
272 myLineEdit3->clear();
278 // Set the first field as current
279 myCurrentLineEdit = myLineEdit1;
280 myCurrentLineEdit->setFocus();
282 switch (constructorId) {
285 // Set text to the group of arguments and to the first two labels
286 myGroupArguments->setTitle(tr("DUPLICATION_WITHOUT_ELEMS"));
287 myTextLabel1->setText(tr("GROUP_NODES_TO_DUPLICATE"));
288 myTextLabel2->setText(tr("GROUP_NODES_TO_REPLACE"));
290 // Set checkbox title
291 myCheckBoxNewGroup->setText(tr("CONSTRUCT_NEW_GROUP_NODES"));
293 // Hide the third field
294 myTextLabel3->hide();
295 mySelectButton3->hide();
302 // Set text to the group of arguments and to all the labels
303 myGroupArguments->setTitle(tr("DUPLICATION_WITH_ELEMS"));
304 myTextLabel1->setText(tr("GROUP_ELEMS_TO_DUPLICATE"));
305 myTextLabel2->setText(tr("GROUP_NODES_NOT_DUPLICATE"));
306 myTextLabel3->setText(tr("GROUP_ELEMS_TO_REPLACE"));
308 // Set checkbox title
309 myCheckBoxNewGroup->setText(tr("CONSTRUCT_NEW_GROUP_ELEMENTS"));
311 // Show the third field
312 myTextLabel3->show();
313 mySelectButton3->show();
321 onSelectionChanged();
325 \brief SLOT called to apply changes.
327 bool SMESHGUI_DuplicateNodesDlg::onApply()
329 if ( mySMESHGUI->isActiveStudyLocked() || !isValid() )
332 BusyLocker lock( myBusy );
334 bool toCreateGroup = myCheckBoxNewGroup->isChecked();
335 int operationMode = myGroupConstructors->checkedId();
339 SUIT_OverrideCursor aWaitCursor;
342 SMESH::SMESH_Mesh_var aMesh = myGroups1[0]->GetMesh();
343 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
345 if ( operationMode == 0 ) {
346 SMESH::ListOfGroups_var g1 = new SMESH::ListOfGroups();
347 g1->length( myGroups1.count() );
348 for ( int i = 0; i < myGroups1.count(); i++ )
349 g1[i] = myGroups1[i];
350 SMESH::ListOfGroups_var g2 = new SMESH::ListOfGroups();
351 g2->length( myGroups2.count() );
352 for ( int i = 0; i < myGroups2.count(); i++ )
353 g2[i] = myGroups2[i];
355 if ( toCreateGroup ) {
356 SMESH::SMESH_GroupBase_var aNewGroup =
357 aMeshEditor->DoubleNodeGroupsNew( g1.in(), g2.in() );
358 result = !CORBA::is_nil( aNewGroup );
361 result = aMeshEditor->DoubleNodeGroups( g1.in(), g2.in() );
365 SMESH::ListOfGroups_var g1 = new SMESH::ListOfGroups();
366 g1->length( myGroups1.count() );
367 for ( int i = 0; i < myGroups1.count(); i++ )
368 g1[i] = myGroups1[i];
369 SMESH::ListOfGroups_var g2 = new SMESH::ListOfGroups();
370 g2->length( myGroups2.count() );
371 for ( int i = 0; i < myGroups2.count(); i++ )
372 g2[i] = myGroups2[i];
373 SMESH::ListOfGroups_var g3 = new SMESH::ListOfGroups();
374 g3->length( myGroups3.count() );
376 for ( int i = 0; i < myGroups3.count(); i++ )
377 g3[i] = myGroups3[i];
378 if ( toCreateGroup ) {
379 SMESH::SMESH_GroupBase_ptr aNewGroup =
380 aMeshEditor->DoubleNodeElemGroupsNew( g1.in(), g2.in(), g3.in() );
381 result = !CORBA::is_nil( aNewGroup );
384 result = aMeshEditor->DoubleNodeElemGroups( g1.in(), g2.in(), g3.in() );
388 catch (const SALOME::SALOME_Exception& S_ex) {
389 SalomeApp_Tools::QtCatchCorbaException(S_ex);
391 catch ( const std::exception& exc ) {
392 INFOS( "Follow exception was cought:\n\t" << exc.what() );
395 INFOS( "Unknown exception was cought !!!" );
399 SUIT_MessageBox::warning(this,
400 tr("SMESH_WRN_WARNING"),
401 tr("SMESH_OPERATION_FAILED"));
406 mySelectionMgr->clearSelected();
408 SMESHGUI::Modified();
409 mySMESHGUI->updateObjBrowser(true);
411 // Reinitialize the dialog
418 \brief SLOT called to apply changes and close the dialog.
420 void SMESHGUI_DuplicateNodesDlg::onOk()
427 \brief SLOT called to close the dialog.
429 void SMESHGUI_DuplicateNodesDlg::onClose()
431 disconnect(mySelectionMgr, 0, this, 0);
432 disconnect(mySMESHGUI, 0, this, 0);
433 mySMESHGUI->ResetState();
434 mySelectionMgr->clearFilters();
439 \brief SLOT called when selection changed.
441 void SMESHGUI_DuplicateNodesDlg::onSelectionChanged()
443 if ( myBusy || !isEnabled() ) return;
445 int operationMode = myGroupConstructors->checkedId();
448 mySelectionMgr->selectedObjects( aList );
449 //int aNbSel = aList.Extent();
451 QList<SMESH::SMESH_GroupBase_var> aGroups;
453 SALOME_ListIteratorOfListIO anIter ( aList );
455 for ( ; anIter.More() && ok; anIter.Next()) {
456 SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>( anIter.Value() );
457 // check group is selected
458 ok = !CORBA::is_nil( aGroup );
459 // check groups of the same mesh are selected
461 SMESH::SMESH_Mesh_var aMesh1;
462 if ( !aGroups.isEmpty() ) aMesh1 = aGroups[0]->GetMesh();
463 SMESH::SMESH_Mesh_var aMesh2 = aGroup->GetMesh();
464 ok = CORBA::is_nil( aMesh1 ) || aMesh1->_is_equivalent( aMesh2 );
466 // check group of proper type is selected
468 SMESH::ElementType aGroupType = aGroup->GetType();
469 if ( operationMode == 0 ) {
470 ok = ( myCurrentLineEdit == myLineEdit1 && aGroupType == SMESH::NODE ) ||
471 ( myCurrentLineEdit == myLineEdit2 && aGroupType != SMESH::NODE );
474 ok = ( myCurrentLineEdit == myLineEdit1 && ( aGroupType == SMESH::EDGE ||
475 aGroupType == SMESH::FACE ) ) ||
476 ( myCurrentLineEdit == myLineEdit2 && aGroupType == SMESH::NODE ) ||
477 ( myCurrentLineEdit == myLineEdit3 && aGroupType != SMESH::NODE );
480 if ( ok ) aGroups << aGroup;
483 // Clear current field
484 myCurrentLineEdit->clear();
486 if ( ok && !aGroups.isEmpty() ) {
487 if ( myCurrentLineEdit == myLineEdit1 ) myGroups1 = aGroups;
488 else if ( myCurrentLineEdit == myLineEdit2 ) myGroups2 = aGroups;
489 else if ( myCurrentLineEdit == myLineEdit3 ) myGroups3 = aGroups;
490 CORBA::String_var name = aGroups[0]->GetName();
491 myCurrentLineEdit->setText( aGroups.count() == 1 ? QString(name).trimmed() :
492 QObject::tr( "SMESH_OBJECTS_SELECTED" ).arg( aGroups.count() ) );
495 if ( myCurrentLineEdit == myLineEdit1 ) myGroups1.clear();
496 else if ( myCurrentLineEdit == myLineEdit2 ) myGroups2.clear();
497 else if ( myCurrentLineEdit == myLineEdit3 ) myGroups3.clear();
498 myCurrentLineEdit->clear();
501 // Enable/disable "Apply and Close" and "Apply" buttons
502 bool isDataValid = isValid();
503 myButtonOk->setEnabled( isDataValid );
504 myButtonApply->setEnabled( isDataValid );
508 \brief SLOT called when selection button clicked.
510 void SMESHGUI_DuplicateNodesDlg::onEditCurrentArgument()
512 QPushButton* send = (QPushButton*)sender();
514 // Set current field for edition
515 if (send == mySelectButton1) {
516 myCurrentLineEdit = myLineEdit1;
518 else if (send == mySelectButton2) {
519 myCurrentLineEdit = myLineEdit2;
521 else if (send == mySelectButton3) {
522 myCurrentLineEdit = myLineEdit3;
525 myCurrentLineEdit->setFocus();
526 onSelectionChanged();
530 \brief Check if the input data is valid.
531 \return \c true id the data is valid
533 bool SMESHGUI_DuplicateNodesDlg::isValid()
535 return myGroupConstructors->checkedId() == 1 ?
536 ( !myGroups1.isEmpty() && !myGroups3.isEmpty() ) :
537 ( !myGroups1.isEmpty() );
542 \brief SLOT called when dialog shoud be deativated.
544 void SMESHGUI_DuplicateNodesDlg::onDeactivate()
547 mySelectionMgr->clearFilters();
549 mySMESHGUI->ResetState();
550 mySMESHGUI->SetActiveDialogBox(0);
555 \brief Receive dialog enter events.
556 Activates the dialog when the mouse cursor enters.
557 Reimplemented from QWidget class.
559 void SMESHGUI_DuplicateNodesDlg::enterEvent (QEvent*)
561 if ( !isEnabled() ) {
562 mySMESHGUI->EmitSignalDeactivateDialog();
564 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
566 // Set selection mode
567 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
568 aViewWindow->SetSelectionMode(ActorSelection);
569 mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
574 \brief Receive close events.
575 Reimplemented from QWidget class.
577 void SMESHGUI_DuplicateNodesDlg::closeEvent (QCloseEvent*)
583 \brief Receive key press events.
584 Reimplemented from QWidget class.
586 void SMESHGUI_DuplicateNodesDlg::keyPressEvent( QKeyEvent* e )
588 QDialog::keyPressEvent( e );
589 if ( e->isAccepted() )
592 if ( e->key() == Qt::Key_F1 ) {
599 \brief Show the dialog help page.
601 void SMESHGUI_DuplicateNodesDlg::onHelp()
603 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
605 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
609 platform = "winapplication";
611 platform = "application";
613 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
614 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
615 arg(app->resourceMgr()->stringValue("ExternalBrowser",
617 arg(myHelpFileName));