Salome HOME
Update version number: 3.1.0a2
[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   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->isActiveStudyLocked())
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     // Call hypothesis creation server method (without GUI)
201     QString aHypName = aHypData->Label;
202     SMESH::CreateHypothesis(sHypType, aHypName, myIsAlgo);
203   } else {
204     // Get hypotheses creator client (GUI)
205     SMESHGUI_GenericHypothesisCreator* aCreator =
206       SMESH::GetHypothesisCreator(sHypType);
207
208     // Create hypothesis/algorithm
209     aCreator->CreateHypothesis(myIsAlgo, this);
210   }
211
212 //  buttonApply->setEnabled(FALSE);
213   return;
214 }
215
216 //=================================================================================
217 // function : ActivateThisDialog()
218 // purpose  :
219 //=================================================================================
220 void SMESHGUI_CreateHypothesesDlg::ActivateThisDialog()
221 {
222   mySMESHGUI->EmitSignalDeactivateDialog();
223   GroupButtons->setEnabled(true);
224   return;
225 }
226
227 //=================================================================================
228 // function : enterEvent()
229 // purpose  :
230 //=================================================================================
231 void SMESHGUI_CreateHypothesesDlg::enterEvent (QEvent*)
232 {
233   ActivateThisDialog();
234   return;
235 }
236
237 //=================================================================================
238 // function : closeEvent()
239 // purpose  :
240 //=================================================================================
241 void SMESHGUI_CreateHypothesesDlg::closeEvent (QCloseEvent* e)
242 {
243   mySMESHGUI->ResetState();
244   QDialog::closeEvent(e);
245 }
246
247 //=================================================================================
248 // function : onSelectionChanged()
249 // purpose  :
250 //=================================================================================
251 void SMESHGUI_CreateHypothesesDlg::onSelectionChanged()
252 {
253   QListViewItem* item = ListAlgoDefinition->selectedItem();
254   buttonApply->setEnabled(item && item->depth() > 0);
255 }
256
257 //=================================================================================
258 // function : onDoubleClicked()
259 // purpose  :
260 //=================================================================================
261 void SMESHGUI_CreateHypothesesDlg::onDoubleClicked (QListViewItem* i)
262 {
263   if (i && i->depth() > 0)
264     this->ClickOnApply();
265 }
266
267 //=================================================================================
268 // function : InitAlgoDefinition()
269 // purpose  :
270 //=================================================================================
271 void SMESHGUI_CreateHypothesesDlg::InitAlgoDefinition()
272 {
273   ListAlgoDefinition->clear();
274   QStringList HypList = SMESH::GetAvailableHypotheses(myIsAlgo);
275   for (int i = 0; i < HypList.count(); ++i) {
276     HypothesisData* aHypData = SMESH::GetHypothesisData(HypList[i]);
277     QListViewItem* parentItem = 0;
278     QListViewItem* childItem = ListAlgoDefinition->firstChild();
279     while (childItem) {
280       if (childItem->text(0) == aHypData->PluginName) {
281         parentItem = childItem;
282         break;
283       }
284       childItem = childItem->nextSibling();
285     }
286     if (!parentItem)
287       parentItem = new QListViewItem(ListAlgoDefinition, aHypData->PluginName);
288     parentItem->setOpen(true);
289     QListViewItem* aItem = new QListViewItem(parentItem, aHypData->Label, HypList[i]);
290     QPixmap aPixMap (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr(aHypData->IconId)));
291     if (!aPixMap.isNull())
292       aItem->setPixmap(0, aPixMap);
293   }
294 }