]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_GroupOpDlg.cxx
Salome HOME
Bug 0016617: Regression in V5.1.1rc3: the groups are not generated.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupOpDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_GroupOpDlg.cxx
24 //  Author : Sergey LITONIN
25 //  Module : SMESH
26 //
27 #include "SMESHGUI_GroupOpDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32
33 #include "SMESH_TypeFilter.hxx"
34
35 #include "SUIT_ResourceMgr.h"
36 #include "SUIT_Desktop.h"
37 #include "SUIT_Session.h"
38 #include "SUIT_MessageBox.h"
39
40 #include "LightApp_Application.h"
41
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"
47
48 // QT Includes
49 #include <qframe.h>
50 #include <qlayout.h>
51 #include <qpushbutton.h>
52 #include <qgroupbox.h>
53 #include <qlabel.h>
54 #include <qlistbox.h>
55 #include <qlineedit.h>
56 #include <qmessagebox.h>
57
58 #define SPACING 5
59 #define MARGIN  10
60
61 /*!
62  *  Class       : SMESHGUI_GroupOpDlg
63  *  Description : Perform boolean operations on groups
64  */
65
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 ) )
75 {
76   myMode = theMode;
77
78   if (myMode == UNION) {
79     setCaption(tr("UNION_OF_TWO_GROUPS"));
80     myHelpFileName = "using_operations_on_groups_page.html#union_anchor";
81   }
82   else if (myMode == INTERSECT) {
83     setCaption(tr("INTERSECTION_OF_TWO_GROUPS"));
84     myHelpFileName = "using_operations_on_groups_page.html#intersection_anchor";
85   }
86   else {
87     setCaption(tr("CUT_OF_TWO_GROUPS"));
88     myHelpFileName = "using_operations_on_groups_page.html#cut_anchor";
89   }
90
91   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
92
93   QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
94
95   QFrame* aMainFrame = createMainFrame  (this);
96   QFrame* aBtnFrame  = createButtonFrame(this);
97
98   aDlgLay->addWidget(aMainFrame);
99   aDlgLay->addWidget(aBtnFrame);
100
101   aDlgLay->setStretchFactor(aMainFrame, 1);
102
103   Init();
104 }
105
106 //=======================================================================
107 // name    : SMESHGUI_GroupOpDlg::createMainFrame
108 // Purpose : Create frame containing dialog's input fields
109 //=======================================================================
110 QFrame* SMESHGUI_GroupOpDlg::createMainFrame (QWidget* theParent)
111 {
112   QGroupBox* aMainGrp = new QGroupBox(1, Qt::Horizontal, theParent);
113   aMainGrp->setFrameStyle(QFrame::NoFrame);
114   aMainGrp->setInsideMargin(0);
115
116   QGroupBox* aNameGrp = new QGroupBox(1, Qt::Vertical, tr("NAME"), aMainGrp);
117   new QLabel(tr("RESULT_NAME"), aNameGrp);
118   myNameEdit = new QLineEdit(aNameGrp);
119
120   QGroupBox* anArgGrp = new QGroupBox(3, Qt::Horizontal, tr("ARGUMENTS"), aMainGrp);
121
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 );
126
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 );
131
132   myEdit1->setReadOnly(true);
133   myEdit2->setReadOnly(true);
134
135   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
136   myBtn1->setPixmap(aPix);
137   myBtn2->setPixmap(aPix);
138
139   return aMainGrp;
140 }
141
142 //=======================================================================
143 // name    : SMESHGUI_GroupOpDlg::createButtonFrame
144 // Purpose : Create frame containing buttons
145 //=======================================================================
146 QFrame* SMESHGUI_GroupOpDlg::createButtonFrame (QWidget* theParent)
147 {
148   QFrame* aFrame = new QFrame(theParent);
149   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
150
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);
155
156   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
157
158   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
159
160   aLay->addWidget(myOkBtn);
161   aLay->addWidget(myApplyBtn);
162   aLay->addItem(aSpacer);
163   aLay->addWidget(myCloseBtn);
164   aLay->addWidget(myHelpBtn);
165
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()));
171
172   return aFrame;
173 }
174
175 //=======================================================================
176 // name    : SMESHGUI_GroupOpDlg::~SMESHGUI_GroupOpDlg
177 // Purpose : Destructor
178 //=======================================================================
179 SMESHGUI_GroupOpDlg::~SMESHGUI_GroupOpDlg()
180 {
181 }
182
183 //=======================================================================
184 // name    : SMESHGUI_GroupOpDlg::Init
185 // Purpose : Init dialog fields, connect signals and slots, show dialog
186 //=======================================================================
187 void SMESHGUI_GroupOpDlg::Init()
188 {
189   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
190   myFocusWg = myEdit1;
191
192   myGroup1 = SMESH::SMESH_GroupBase::_nil();
193   myGroup2 = SMESH::SMESH_GroupBase::_nil();
194
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()));
199
200   connect(myBtn1, SIGNAL(clicked()), this, SLOT(onFocusChanged()));
201   connect(myBtn2, SIGNAL(clicked()), this, SLOT(onFocusChanged()));
202
203   this->show();
204
205   // set selection mode
206   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
207     aViewWindow->SetSelectionMode(ActorSelection);
208   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
209
210   return;
211 }
212
213 //=======================================================================
214 // name    : SMESHGUI_GroupOpDlg::isValid
215 // Purpose : Verify validity of input data
216 //=======================================================================
217 bool SMESHGUI_GroupOpDlg::isValid()
218 {
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);
223     return false;
224   }
225
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);
230     return false;
231   }
232
233   // Verify whether arguments belongs to same mesh
234   SMESH::SMESH_Mesh_ptr aMesh1 = myGroup1->GetMesh();
235   SMESH::SMESH_Mesh_ptr aMesh2 = myGroup2->GetMesh();
236
237   int aMeshId1 = !aMesh1->_is_nil() ? aMesh1->GetId() : -1;
238   int aMeshId2 = !aMesh2->_is_nil() ? aMesh2->GetId() : -1;
239
240   if (aMeshId1 != aMeshId2 || aMeshId1 == -1) {
241     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
242                              tr("DIFF_MESHES"), QMessageBox::Ok);
243     return false;
244   }
245
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);
250     return false;
251   }
252
253   return true;
254 }
255
256 //=======================================================================
257 // name    : SMESHGUI_GroupOpDlg::onApply
258 // Purpose : SLOT called when "Apply" button pressed.
259 //=======================================================================
260 bool SMESHGUI_GroupOpDlg::onApply()
261 {
262   if (!isValid() || mySMESHGUI->isActiveStudyLocked())
263     return false;
264
265   SMESH::SMESH_Mesh_ptr aMesh = myGroup1->GetMesh();
266   QString aName = myNameEdit->text();
267   SMESH::SMESH_Group_ptr aNewGrp = SMESH::SMESH_Group::_nil();
268
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());
272
273   if (!aNewGrp->_is_nil()) {
274     mySMESHGUI->updateObjBrowser(true);
275     reset();
276     return true;
277   } else {
278     QMessageBox::critical(SMESHGUI::desktop(), tr("SMESH_ERROR"),
279                           tr("SMESH_OPERATION_FAILED"), "OK");
280     return false;
281   }
282 }
283
284 //=======================================================================
285 // name    : SMESHGUI_GroupOpDlg::onOk
286 // Purpose : SLOT called when "Ok" button pressed.
287 //=======================================================================
288 void SMESHGUI_GroupOpDlg::onOk()
289 {
290   if (onApply())
291     onClose();
292 }
293
294 //=======================================================================
295 // name    : SMESHGUI_GroupOpDlg::onClose
296 // Purpose : SLOT called when "Close" button pressed. Close dialog
297 //=======================================================================
298 void SMESHGUI_GroupOpDlg::onClose()
299 {
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();
306   reject();
307 }
308
309 //=================================================================================
310 // function : onHelp()
311 // purpose  :
312 //=================================================================================
313 void SMESHGUI_GroupOpDlg::onHelp()
314 {
315   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
316   if (app) 
317     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
318   else {
319                 QString platform;
320 #ifdef WIN32
321                 platform = "winapplication";
322 #else
323                 platform = "application";
324 #endif
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"));
329   }
330 }
331
332 //=======================================================================
333 // name    : SMESHGUI_GroupOpDlg::onSelectionDone
334 // Purpose : SLOT called when selection changed
335 //=======================================================================
336 void SMESHGUI_GroupOpDlg::onSelectionDone()
337 {
338   if (myFocusWg == myEdit1)
339     myGroup1 = SMESH::SMESH_GroupBase::_nil();
340   else
341     myGroup2 = SMESH::SMESH_GroupBase::_nil();
342
343   myFocusWg->setText("");
344
345   SALOME_ListIO aList;
346   mySelectionMgr->selectedObjects(aList);
347
348   if (aList.Extent() == 1) {
349     SMESH::SMESH_GroupBase_var aGroup =
350       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First());
351
352     if (!aGroup->_is_nil())
353     {
354       myFocusWg->setText(aGroup->GetName());
355       myFocusWg->setCursorPosition( 0 );
356
357       if (myFocusWg == myEdit1)
358         myGroup1 = aGroup;
359       else
360         myGroup2 = aGroup;
361     }
362   }
363 }
364
365 //=======================================================================
366 // name    : SMESHGUI_GroupOpDlg::onDeactivate
367 // Purpose : SLOT called when dialog must be deativated
368 //=======================================================================
369 void SMESHGUI_GroupOpDlg::onDeactivate()
370 {
371   setEnabled(false);
372   mySelectionMgr->clearFilters();
373 }
374
375 //=======================================================================
376 // name    : SMESHGUI_GroupOpDlg::enterEvent
377 // Purpose : Event filter
378 //=======================================================================
379 void SMESHGUI_GroupOpDlg::enterEvent (QEvent*)
380 {
381   mySMESHGUI->EmitSignalDeactivateDialog();
382   setEnabled(true);
383   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
384     aViewWindow->SetSelectionMode(ActorSelection);
385   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
386 }
387
388 //=======================================================================
389 // name    : SMESHGUI_GroupOpDlg::closeEvent
390 // purpose :
391 //=======================================================================
392 void SMESHGUI_GroupOpDlg::closeEvent (QCloseEvent*)
393 {
394   onClose();
395 }
396
397 //=======================================================================
398 // name    : SMESHGUI_GroupOpDlg::onFocusChanged
399 // Purpose : SLOT. Called when "Select" button pressed.
400 //=======================================================================
401 void SMESHGUI_GroupOpDlg::onFocusChanged()
402 {
403   const QObject* aSender = sender();
404   myFocusWg = aSender == myBtn1 ? myEdit1 : myEdit2;
405   onSelectionDone();
406 }
407
408 //=======================================================================
409 // name    : SMESHGUI_GroupOpDlg::reset
410 // Purpose : Rest state of dialog
411 //=======================================================================
412 void SMESHGUI_GroupOpDlg::reset()
413 {
414   myNameEdit->setText("");
415   myEdit1->setText("");
416   myEdit2->setText("");
417   myFocusWg = myEdit1;
418   myNameEdit->setFocus();
419 }
420
421 //=================================================================================
422 // function : keyPressEvent()
423 // purpose  :
424 //=================================================================================
425 void SMESHGUI_GroupOpDlg::keyPressEvent( QKeyEvent* e )
426 {
427   QDialog::keyPressEvent( e );
428   if ( e->isAccepted() )
429     return;
430
431   if ( e->key() == Key_F1 )
432     {
433       e->accept();
434       onHelp();
435     }
436 }