Salome HOME
1. Implementation of new pattern mapping algorthm (mesh refinement)
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreateHypothesesDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  CEA
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 //  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org
20 //
21 //
22 //
23 //  File   : SMESHGUI_CreateHypothesesDlg.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : SMESH
26 //  $Header$
27
28 // QT Includes
29 #include <qbuttongroup.h>
30 #include <qgroupbox.h>
31 #include <qpushbutton.h>
32 #include <qlayout.h>
33 #include <qlistview.h>
34 #include <qheader.h>
35
36 #include "QAD_Application.h"
37 #include "QAD_Desktop.h"
38
39 #include "SALOME_ListIteratorOfListIO.hxx"
40
41 #include "SMESHGUI_CreateHypothesesDlg.h"
42 #include "SMESHGUI_HypothesesUtils.h"
43 #include "SMESHGUI_Hypotheses.h"
44 #include "SMESHGUI_Utils.h"
45 #include "SMESHGUI.h"
46
47 #include "utilities.h"
48
49 using namespace std;
50
51 //=================================================================================
52 // class    : SMESHGUI_CreateHypothesesDlg()
53 // purpose  : Constructs a SMESHGUI_CreateHypothesesDlg which is a child of 'parent', with the 
54 //            name 'name' and widget flags set to 'f'.
55 //            The dialog will by default be modeless, unless you set 'modal' to
56 //            TRUE to construct a modal dialog.
57 //=================================================================================
58 SMESHGUI_CreateHypothesesDlg::SMESHGUI_CreateHypothesesDlg (QWidget* parent,
59                                                             const char* name,
60                                                             bool modal,
61                                                             bool isAlgo)
62      : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
63        myIsAlgo( isAlgo )
64 {
65   MESSAGE("SMESHGUI_CreateHypothesesDlg");
66
67   if ( !name )
68     setName( "SMESHGUI_CreateHypothesesDlg" );
69   setCaption( isAlgo ? tr( "SMESH_CREATE_ALGORITHMS"  ) : tr( "SMESH_CREATE_HYPOTHESES"  ) );
70   setSizeGripEnabled( TRUE );
71
72   QGridLayout* SMESHGUI_CreateHypothesesDlgLayout = new QGridLayout( this ); 
73   SMESHGUI_CreateHypothesesDlgLayout->setSpacing( 6 );
74   SMESHGUI_CreateHypothesesDlgLayout->setMargin( 11 );
75
76   /***************************************************************/
77   GroupAlgorithms = new QGroupBox( this, "GroupAlgorithms" );
78   GroupAlgorithms->setTitle( isAlgo ? tr("SMESH_AVAILABLE_ALGORITHMS") : tr("SMESH_AVAILABLE_HYPOTHESES") );
79   GroupAlgorithms->setColumnLayout(0, Qt::Vertical );
80   GroupAlgorithms->layout()->setSpacing( 0 );
81   GroupAlgorithms->layout()->setMargin( 0 );
82
83   QGridLayout* hypLayout = new QGridLayout( GroupAlgorithms->layout() );
84   hypLayout->setGeometry( QRect( 12, 18, 139, 250 ) ); 
85   hypLayout->setAlignment( Qt::AlignTop );
86   hypLayout->setSpacing( 6 );
87   hypLayout->setMargin( 11 );
88
89   ListAlgoDefinition = new QListView( GroupAlgorithms, "ListAlgoDefinition" );
90   ListAlgoDefinition->setMinimumSize( 400, 200 );
91   ListAlgoDefinition->addColumn("");
92   ListAlgoDefinition->header()->hide();
93   ListAlgoDefinition->setSelectionMode(QListView::Single);
94   ListAlgoDefinition->setResizeMode(QListView::AllColumns);
95   ListAlgoDefinition->setRootIsDecorated( true );
96
97   hypLayout->addWidget( ListAlgoDefinition, 0, 0 );
98   SMESHGUI_CreateHypothesesDlgLayout->addWidget( GroupAlgorithms, 0, 0 );
99
100   /***************************************************************/
101   GroupButtons = new QGroupBox( this, "GroupButtons" );
102   GroupButtons->setColumnLayout( 0, Qt::Vertical );
103   GroupButtons->layout()->setSpacing( 0 );
104   GroupButtons->layout()->setMargin( 0 );
105   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
106   GroupButtonsLayout->setAlignment( Qt::AlignTop );
107   GroupButtonsLayout->setSpacing( 6 );
108   GroupButtonsLayout->setMargin( 11 );
109
110   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
111   buttonApply->setText( tr( "SMESH_BUT_CREATE"  ) );
112   buttonApply->setAutoDefault( TRUE );
113   buttonApply->setDefault( FALSE );
114   buttonApply->setEnabled( FALSE ) ;
115   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
116
117   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
118   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
119
120   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
121   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
122   buttonCancel->setAutoDefault( TRUE );
123   buttonCancel->setDefault( TRUE );
124   buttonCancel->setEnabled( TRUE ) ;
125   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
126
127   SMESHGUI_CreateHypothesesDlgLayout->addWidget( GroupButtons, 1, 0 );
128   /***************************************************************/
129
130   Init();
131 }
132
133 //=================================================================================
134 // function : ~SMESHGUI_CreateHypothesesDlg()
135 // purpose  : Destroys the object and frees any allocated resources
136 //=================================================================================
137 SMESHGUI_CreateHypothesesDlg::~SMESHGUI_CreateHypothesesDlg()
138 {
139     // no need to delete child widgets, Qt does it all for us
140 }
141
142 //=================================================================================
143 // function : Init()
144 // purpose  :
145 //=================================================================================
146 void SMESHGUI_CreateHypothesesDlg::Init()
147 {
148   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
149   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
150
151   InitAlgoDefinition();
152
153   /* signals and slots connections */
154   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
155   connect( buttonApply , SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) ) ;
156
157 //  connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
158   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
159  
160   connect( ListAlgoDefinition, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
161   connect( ListAlgoDefinition, SIGNAL( doubleClicked(QListViewItem*) ),    this, SLOT( onDoubleClicked(QListViewItem*) ) );
162
163   int x, y ;
164   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
165   this->move( x, y ) ;
166   this->show() ; 
167 }
168
169 //=================================================================================
170 // function : ClickOnCancel()
171 // purpose  :
172 //=================================================================================
173 void SMESHGUI_CreateHypothesesDlg::ClickOnCancel()
174 {
175   close() ;
176 }
177
178 //=================================================================================
179 // function : ClickOnApply()
180 // purpose  :
181 //=================================================================================
182 void SMESHGUI_CreateHypothesesDlg::ClickOnApply()
183 {
184   if (mySMESHGUI->ActiveStudyLocked())
185     return;
186   QListViewItem* item = ListAlgoDefinition->selectedItem();
187   if ( !item )
188     return;
189   QString aHypType = item->text( 1 );
190   MESSAGE("Apply " << aHypType);
191   char* sHypType = (char*)aHypType.latin1();
192
193   HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
194   if ( !aHypData ) 
195     return;
196   QString aClientLibName = aHypData->ClientLibName;
197   MESSAGE("Client lib name = " << aClientLibName);
198
199   if (aClientLibName == "")
200   {
201     // Call hypothesis creation server method (without GUI)
202     QString aHypName = aHypData->Label;
203     SMESH::CreateHypothesis(sHypType, aHypName, myIsAlgo);
204   }
205   else
206   {
207     // Get hypotheses creator client (GUI)
208     SMESHGUI_GenericHypothesisCreator* aCreator =
209       SMESH::GetHypothesisCreator(sHypType);
210
211     // Create hypothesis/algorithm
212     aCreator->CreateHypothesis(myIsAlgo, this);
213   }
214
215 //  buttonApply->setEnabled(FALSE);
216   return;
217 }
218
219 //=================================================================================
220 // function : ActivateThisDialog()
221 // purpose  :
222 //=================================================================================
223 void SMESHGUI_CreateHypothesesDlg::ActivateThisDialog()
224 {
225   mySMESHGUI->EmitSignalDeactivateDialog() ;   
226   GroupButtons->setEnabled(true) ;
227   return ;
228 }
229
230 //=================================================================================
231 // function : enterEvent()
232 // purpose  :
233 //=================================================================================
234 void SMESHGUI_CreateHypothesesDlg::enterEvent(QEvent* e)
235 {
236   ActivateThisDialog() ;
237   return ;
238 }
239
240 //=================================================================================
241 // function : closeEvent()
242 // purpose  :
243 //=================================================================================
244 void SMESHGUI_CreateHypothesesDlg::closeEvent( QCloseEvent* e )
245 {
246   mySMESHGUI->ResetState();
247   QDialog::closeEvent( e );
248 }
249
250 //=================================================================================
251 // function : onSelectionChanged()
252 // purpose  :
253 //=================================================================================
254 void SMESHGUI_CreateHypothesesDlg::onSelectionChanged()
255 {
256   QListViewItem* item = ListAlgoDefinition->selectedItem();
257   buttonApply->setEnabled( item && item->depth() > 0 ) ;
258 }
259
260 //=================================================================================
261 // function : onDoubleClicked()
262 // purpose  :
263 //=================================================================================
264 void SMESHGUI_CreateHypothesesDlg::onDoubleClicked(QListViewItem* i)
265 {
266   if ( i && i->depth() > 0 )
267     this->ClickOnApply();
268 }
269
270 //=================================================================================
271 // function : InitAlgoDefinition()
272 // purpose  :
273 //=================================================================================
274 void SMESHGUI_CreateHypothesesDlg::InitAlgoDefinition()
275 {
276   ListAlgoDefinition->clear();
277   QStringList HypList = SMESH::GetAvailableHypotheses(myIsAlgo);
278   for ( int i = 0; i < HypList.count(); ++i ) {
279     HypothesisData* aHypData = SMESH::GetHypothesisData( HypList[i] );
280     QListViewItem* parentItem = 0;
281     QListViewItem* childItem = ListAlgoDefinition->firstChild();
282     while ( childItem ) {
283       if ( childItem->text(0) == aHypData->PluginName ) {
284         parentItem = childItem;
285         break;
286       }
287       childItem = childItem->nextSibling();
288     }
289     if ( !parentItem ) 
290       parentItem = new QListViewItem( ListAlgoDefinition, aHypData->PluginName );
291     parentItem->setOpen( true );
292     QListViewItem* aItem = new QListViewItem( parentItem, aHypData->Label, HypList[i] );
293     QPixmap aPixMap (QAD_Desktop::getResourceManager()->loadPixmap
294                      ("SMESH", tr(aHypData->IconId)));
295     if ( !aPixMap.isNull() )
296       aItem->setPixmap( 0, aPixMap );
297   }
298 }