1 // Copyright (C) 2007-2008 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 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_GroupOpDlg.cxx
24 // Author : Sergey LITONIN
27 #include "SMESHGUI_GroupOpDlg.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESH_TypeFilter.hxx"
35 #include "SUIT_ResourceMgr.h"
36 #include "SUIT_Desktop.h"
37 #include "SUIT_Session.h"
38 #include "SUIT_MessageBox.h"
40 #include "LightApp_Application.h"
42 #include "LightApp_SelectionMgr.h"
43 #include "SVTK_Selection.h"
44 #include "SVTK_ViewWindow.h"
45 #include "SVTK_Selector.h"
46 #include "SALOME_ListIO.hxx"
51 #include <qpushbutton.h>
52 #include <qgroupbox.h>
55 #include <qlineedit.h>
56 #include <qmessagebox.h>
62 * Class : SMESHGUI_GroupOpDlg
63 * Description : Perform boolean operations on groups
66 //=======================================================================
67 // name : SMESHGUI_GroupOpDlg::SMESHGUI_GroupOpDlg
68 // Purpose : Constructor
69 //=======================================================================
70 SMESHGUI_GroupOpDlg::SMESHGUI_GroupOpDlg( SMESHGUI* theModule, const int theMode )
71 : QDialog( SMESH::GetDesktop( theModule ), "SMESHGUI_GroupOpDlg", false,
72 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
73 mySMESHGUI( theModule ),
74 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
78 if (myMode == UNION) {
79 setCaption(tr("UNION_OF_TWO_GROUPS"));
80 myHelpFileName = "using_operations_on_groups_page.html#union_anchor";
82 else if (myMode == INTERSECT) {
83 setCaption(tr("INTERSECTION_OF_TWO_GROUPS"));
84 myHelpFileName = "using_operations_on_groups_page.html#intersection_anchor";
87 setCaption(tr("CUT_OF_TWO_GROUPS"));
88 myHelpFileName = "using_operations_on_groups_page.html#cut_anchor";
91 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
93 QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
95 QFrame* aMainFrame = createMainFrame (this);
96 QFrame* aBtnFrame = createButtonFrame(this);
98 aDlgLay->addWidget(aMainFrame);
99 aDlgLay->addWidget(aBtnFrame);
101 aDlgLay->setStretchFactor(aMainFrame, 1);
106 //=======================================================================
107 // name : SMESHGUI_GroupOpDlg::createMainFrame
108 // Purpose : Create frame containing dialog's input fields
109 //=======================================================================
110 QFrame* SMESHGUI_GroupOpDlg::createMainFrame (QWidget* theParent)
112 QGroupBox* aMainGrp = new QGroupBox(1, Qt::Horizontal, theParent);
113 aMainGrp->setFrameStyle(QFrame::NoFrame);
114 aMainGrp->setInsideMargin(0);
116 QGroupBox* aNameGrp = new QGroupBox(1, Qt::Vertical, tr("NAME"), aMainGrp);
117 new QLabel(tr("RESULT_NAME"), aNameGrp);
118 myNameEdit = new QLineEdit(aNameGrp);
120 QGroupBox* anArgGrp = new QGroupBox(3, Qt::Horizontal, tr("ARGUMENTS"), aMainGrp);
122 new QLabel(myMode == CUT ? tr("MAIN_OBJECT") :tr("OBJECT_1"), anArgGrp);
123 myBtn1 = new QPushButton(anArgGrp);
124 myEdit1 = new QLineEdit(anArgGrp);
125 myEdit1->setAlignment( Qt::AlignLeft );
127 new QLabel(myMode == CUT ? tr("TOOL_OBJECT") :tr("OBJECT_2"), anArgGrp);
128 myBtn2 = new QPushButton(anArgGrp);
129 myEdit2 = new QLineEdit(anArgGrp);
130 myEdit2->setAlignment( Qt::AlignLeft );
132 myEdit1->setReadOnly(true);
133 myEdit2->setReadOnly(true);
135 QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
136 myBtn1->setPixmap(aPix);
137 myBtn2->setPixmap(aPix);
142 //=======================================================================
143 // name : SMESHGUI_GroupOpDlg::createButtonFrame
144 // Purpose : Create frame containing buttons
145 //=======================================================================
146 QFrame* SMESHGUI_GroupOpDlg::createButtonFrame (QWidget* theParent)
148 QFrame* aFrame = new QFrame(theParent);
149 aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
151 myOkBtn = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
152 myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
153 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
154 myHelpBtn = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
156 QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
158 QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
160 aLay->addWidget(myOkBtn);
161 aLay->addWidget(myApplyBtn);
162 aLay->addItem(aSpacer);
163 aLay->addWidget(myCloseBtn);
164 aLay->addWidget(myHelpBtn);
166 // connect signals and slots
167 connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
168 connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
169 connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
170 connect(myHelpBtn, SIGNAL(clicked()), SLOT(onHelp()));
175 //=======================================================================
176 // name : SMESHGUI_GroupOpDlg::~SMESHGUI_GroupOpDlg
177 // Purpose : Destructor
178 //=======================================================================
179 SMESHGUI_GroupOpDlg::~SMESHGUI_GroupOpDlg()
183 //=======================================================================
184 // name : SMESHGUI_GroupOpDlg::Init
185 // Purpose : Init dialog fields, connect signals and slots, show dialog
186 //=======================================================================
187 void SMESHGUI_GroupOpDlg::Init()
189 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
192 myGroup1 = SMESH::SMESH_GroupBase::_nil();
193 myGroup2 = SMESH::SMESH_GroupBase::_nil();
195 // selection and SMESHGUI
196 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
197 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
198 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(ClickOnClose()));
200 connect(myBtn1, SIGNAL(clicked()), this, SLOT(onFocusChanged()));
201 connect(myBtn2, SIGNAL(clicked()), this, SLOT(onFocusChanged()));
205 // set selection mode
206 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
207 aViewWindow->SetSelectionMode(ActorSelection);
208 mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
213 //=======================================================================
214 // name : SMESHGUI_GroupOpDlg::isValid
215 // Purpose : Verify validity of input data
216 //=======================================================================
217 bool SMESHGUI_GroupOpDlg::isValid()
219 // Verify validity of group name
220 if (myNameEdit->text() == "") {
221 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
222 tr("EMPTY_NAME"), QMessageBox::Ok);
226 // Verufy wheter arguments speciffiyed
227 if (myGroup1->_is_nil() || myGroup2->_is_nil()) {
228 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
229 tr("INCORRECT_ARGUMENTS"), QMessageBox::Ok);
233 // Verify whether arguments belongs to same mesh
234 SMESH::SMESH_Mesh_ptr aMesh1 = myGroup1->GetMesh();
235 SMESH::SMESH_Mesh_ptr aMesh2 = myGroup2->GetMesh();
237 int aMeshId1 = !aMesh1->_is_nil() ? aMesh1->GetId() : -1;
238 int aMeshId2 = !aMesh2->_is_nil() ? aMesh2->GetId() : -1;
240 if (aMeshId1 != aMeshId2 || aMeshId1 == -1) {
241 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
242 tr("DIFF_MESHES"), QMessageBox::Ok);
246 // Verify whether groups have same types of entities
247 if (myGroup1->GetType() != myGroup2->GetType()) {
248 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
249 tr("DIFF_TYPES"), QMessageBox::Ok);
256 //=======================================================================
257 // name : SMESHGUI_GroupOpDlg::onApply
258 // Purpose : SLOT called when "Apply" button pressed.
259 //=======================================================================
260 bool SMESHGUI_GroupOpDlg::onApply()
262 if (!isValid() || mySMESHGUI->isActiveStudyLocked())
265 SMESH::SMESH_Mesh_ptr aMesh = myGroup1->GetMesh();
266 QString aName = myNameEdit->text();
267 SMESH::SMESH_Group_ptr aNewGrp = SMESH::SMESH_Group::_nil();
269 if (myMode == UNION) aNewGrp = aMesh->UnionGroups(myGroup1, myGroup2, aName.latin1());
270 else if (myMode == INTERSECT) aNewGrp = aMesh->IntersectGroups(myGroup1, myGroup2, aName.latin1());
271 else aNewGrp = aMesh->CutGroups(myGroup1, myGroup2, aName.latin1());
273 if (!aNewGrp->_is_nil()) {
274 mySMESHGUI->updateObjBrowser(true);
278 QMessageBox::critical(SMESHGUI::desktop(), tr("SMESH_ERROR"),
279 tr("SMESH_OPERATION_FAILED"), "OK");
284 //=======================================================================
285 // name : SMESHGUI_GroupOpDlg::onOk
286 // Purpose : SLOT called when "Ok" button pressed.
287 //=======================================================================
288 void SMESHGUI_GroupOpDlg::onOk()
294 //=======================================================================
295 // name : SMESHGUI_GroupOpDlg::onClose
296 // Purpose : SLOT called when "Close" button pressed. Close dialog
297 //=======================================================================
298 void SMESHGUI_GroupOpDlg::onClose()
300 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
301 aViewWindow->SetSelectionMode(ActorSelection);
302 disconnect(mySelectionMgr, 0, this, 0);
303 disconnect(mySMESHGUI, 0, this, 0);
304 mySMESHGUI->ResetState();
305 mySelectionMgr->clearFilters();
309 //=================================================================================
310 // function : onHelp()
312 //=================================================================================
313 void SMESHGUI_GroupOpDlg::onHelp()
315 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
317 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
321 platform = "winapplication";
323 platform = "application";
325 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
326 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
327 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
328 QObject::tr("BUT_OK"));
332 //=======================================================================
333 // name : SMESHGUI_GroupOpDlg::onSelectionDone
334 // Purpose : SLOT called when selection changed
335 //=======================================================================
336 void SMESHGUI_GroupOpDlg::onSelectionDone()
338 if (myFocusWg == myEdit1)
339 myGroup1 = SMESH::SMESH_GroupBase::_nil();
341 myGroup2 = SMESH::SMESH_GroupBase::_nil();
343 myFocusWg->setText("");
346 mySelectionMgr->selectedObjects(aList);
348 if (aList.Extent() == 1) {
349 SMESH::SMESH_GroupBase_var aGroup =
350 SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First());
352 if (!aGroup->_is_nil())
354 myFocusWg->setText(aGroup->GetName());
355 myFocusWg->setCursorPosition( 0 );
357 if (myFocusWg == myEdit1)
365 //=======================================================================
366 // name : SMESHGUI_GroupOpDlg::onDeactivate
367 // Purpose : SLOT called when dialog must be deativated
368 //=======================================================================
369 void SMESHGUI_GroupOpDlg::onDeactivate()
372 mySelectionMgr->clearFilters();
375 //=======================================================================
376 // name : SMESHGUI_GroupOpDlg::enterEvent
377 // Purpose : Event filter
378 //=======================================================================
379 void SMESHGUI_GroupOpDlg::enterEvent (QEvent*)
381 mySMESHGUI->EmitSignalDeactivateDialog();
383 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
384 aViewWindow->SetSelectionMode(ActorSelection);
385 mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
388 //=======================================================================
389 // name : SMESHGUI_GroupOpDlg::closeEvent
391 //=======================================================================
392 void SMESHGUI_GroupOpDlg::closeEvent (QCloseEvent*)
397 //=======================================================================
398 // name : SMESHGUI_GroupOpDlg::onFocusChanged
399 // Purpose : SLOT. Called when "Select" button pressed.
400 //=======================================================================
401 void SMESHGUI_GroupOpDlg::onFocusChanged()
403 const QObject* aSender = sender();
404 myFocusWg = aSender == myBtn1 ? myEdit1 : myEdit2;
408 //=======================================================================
409 // name : SMESHGUI_GroupOpDlg::reset
410 // Purpose : Rest state of dialog
411 //=======================================================================
412 void SMESHGUI_GroupOpDlg::reset()
414 myNameEdit->setText("");
415 myEdit1->setText("");
416 myEdit2->setText("");
418 myNameEdit->setFocus();
421 //=================================================================================
422 // function : keyPressEvent()
424 //=================================================================================
425 void SMESHGUI_GroupOpDlg::keyPressEvent( QKeyEvent* e )
427 QDialog::keyPressEvent( e );
428 if ( e->isAccepted() )
431 if ( e->key() == Key_F1 )