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