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