Salome HOME
6e9bbca789fc319fc18c0638a7c4f75d40b25d36
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_EditHypothesesDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_EditHypothesesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_EditHypothesesDlg.h"
30
31 #include <qlistbox.h>
32 #include <qlayout.h>
33 #include <qgroupbox.h>
34 #include <qlabel.h>
35
36 #include <SALOMEconfig.h>
37 #include CORBA_SERVER_HEADER(SMESH_Gen)
38
39 //=================================================================================
40 // function : SMESHGUI_EditHypothesesDlg()
41 // purpose  : Constructs a SMESHGUI_EditHypothesesDlg which is a child of 'parent', with the
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 SMESHGUI_EditHypothesesDlg::SMESHGUI_EditHypothesesDlg()
47 : SMESHGUI_Dialog( false, true )
48 {
49     setName("SMESHGUI_EditHypothesesDlg");
50     setCaption(tr("SMESH_EDIT_HYPOTHESES"));
51     
52     QVBoxLayout* main = new QVBoxLayout( mainFrame(), 0, 5 );
53
54     /***************************************************************/
55     QGroupBox* GroupC1 = new QGroupBox(tr("SMESH_ARGUMENTS"), mainFrame(), "GroupC1");
56     GroupC1->setColumnLayout( 3, Qt::Horizontal );
57
58     createObject( tr("SMESH_OBJECT_MESHorSUBMESH"), GroupC1, MeshOrSubMesh );
59     createObject( tr("SMESH_OBJECT_GEOM"), GroupC1, GeomShape );
60
61     main->addWidget( GroupC1 );
62
63     /***************************************************************/
64     GroupHypotheses = new QGroupBox(tr("SMESH_HYPOTHESES"), mainFrame(), "GroupHypotheses");
65     GroupHypotheses->setColumnLayout(0, Qt::Vertical);
66     GroupHypotheses->layout()->setSpacing(0);
67     GroupHypotheses->layout()->setMargin(0);
68     QGridLayout* grid_3 = new QGridLayout(GroupHypotheses->layout());
69     grid_3->setAlignment(Qt::AlignTop);
70     grid_3->setSpacing(6);
71     grid_3->setMargin(11);
72
73     TextHypDefinition = new QLabel(tr("SMESH_AVAILABLE"), GroupHypotheses, "TextHypDefinition");
74     grid_3->addWidget(TextHypDefinition, 0, 0);
75
76     ListHypDefinition = new QListBox(GroupHypotheses, "ListHypDefinition");
77     ListHypDefinition->setMinimumSize(100, 100);
78     grid_3->addWidget(ListHypDefinition, 1, 0);
79
80     TextHypAssignation = new QLabel(tr("SMESH_EDIT_USED"), GroupHypotheses, "TextHypAssignation");
81     grid_3->addWidget(TextHypAssignation, 0, 1);
82
83     ListHypAssignation = new QListBox(GroupHypotheses, "ListHypAssignation");
84     ListHypAssignation->setMinimumSize(100, 100);
85     grid_3->addWidget(ListHypAssignation, 1, 1);
86
87     main->addWidget( GroupHypotheses );
88
89     /***************************************************************/
90     GroupAlgorithms = new QGroupBox(tr("SMESH_ADD_ALGORITHM"), mainFrame(), "GroupAlgorithms");
91     GroupAlgorithms->setColumnLayout(0, Qt::Vertical);
92     GroupAlgorithms->layout()->setSpacing(0);
93     GroupAlgorithms->layout()->setMargin(0);
94     QGridLayout* grid_4 = new QGridLayout(GroupAlgorithms->layout());
95     grid_4->setAlignment(Qt::AlignTop);
96     grid_4->setSpacing(6);
97     grid_4->setMargin(11);
98
99     TextAlgoDefinition = new QLabel(tr("SMESH_AVAILABLE"), GroupAlgorithms, "TextAlgoDefinition");
100     grid_4->addWidget(TextAlgoDefinition, 0, 0);
101
102     ListAlgoDefinition = new QListBox(GroupAlgorithms, "ListAlgoDefinition");
103     ListAlgoDefinition->setMinimumSize(100, 100);
104     grid_4->addWidget(ListAlgoDefinition, 1, 0);
105
106     TextAlgoAssignation = new QLabel(tr("SMESH_EDIT_USED"), GroupAlgorithms, "TextAlgoAssignation");
107     grid_4->addWidget(TextAlgoAssignation, 0, 1);
108
109     ListAlgoAssignation = new QListBox(GroupAlgorithms, "ListAlgoAssignation");
110     ListAlgoAssignation ->setMinimumSize(100, 100);
111     grid_4->addWidget(ListAlgoAssignation, 1, 1);
112
113     main->addWidget( GroupAlgorithms );
114
115     connect(ListHypAssignation,  SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(removeItem(QListBoxItem*)));
116     connect(ListAlgoAssignation, SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(removeItem(QListBoxItem*)));
117
118     connect(ListHypDefinition,  SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(addItem(QListBoxItem*)));
119     connect(ListAlgoDefinition, SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(addItem(QListBoxItem*)));
120 }
121
122 //=================================================================================
123 // function : ~SMESHGUI_EditHypothesesDlg()
124 // purpose  : Destroys the object and frees any allocated resources
125 //=================================================================================
126 SMESHGUI_EditHypothesesDlg::~SMESHGUI_EditHypothesesDlg()
127 {
128   // no need to delete child widgets, Qt does it all for us
129 }
130
131 //=======================================================================
132 // function : isOld()
133 // purpose  :
134 //=======================================================================
135 bool SMESHGUI_EditHypothesesDlg::isOld (QListBoxItem* hypItem)
136 {
137   if (hypItem->rtti() == ListBoxIOR::RTTI_IOR) {
138     ListBoxIOR* hyp = (ListBoxIOR*) hypItem;
139     return (myMapOldHypos.find(hyp->GetIOR()) != myMapOldHypos.end() ||
140             myMapOldAlgos.find(hyp->GetIOR()) != myMapOldAlgos.end());
141   }
142
143   return false;
144 }
145
146 //=================================================================================
147 // function : removeItem()
148 // purpose  :
149 //=================================================================================
150 void SMESHGUI_EditHypothesesDlg::removeItem (QListBoxItem* item)
151 {
152   const QObject* aSender = sender();
153
154   if (!item) return;
155
156   if (aSender == ListHypAssignation) {
157     myNbModification += isOld(item) ? 1 : -1;
158     ListHypAssignation->removeItem(ListHypAssignation->index(item));
159   }
160   else if (aSender == ListAlgoAssignation) {
161     myNbModification += isOld(item) ? 1 : -1;
162     ListAlgoAssignation->removeItem(ListAlgoAssignation->index(item));
163   }
164
165   emit( needToUpdate() );
166 }
167
168 //=================================================================================
169 // function : addItem()
170 // purpose  :
171 //=================================================================================
172 void SMESHGUI_EditHypothesesDlg::addItem (QListBoxItem* item)
173 {
174   const QObject* aSender = sender();
175
176   if (!item) return;
177
178   ListBoxIOR* i = 0;
179   if (item->rtti() == ListBoxIOR::RTTI_IOR)
180     i = (ListBoxIOR*)item;
181   if (!i) return;
182
183   bool isFound = false;
184
185   ListBoxIOR* anItem;
186   if (aSender == ListHypDefinition) {
187     for (int j = 0, n = ListHypAssignation->count(); !isFound && j < n; j++) {
188       if (ListHypAssignation->item(j)->rtti() == ListBoxIOR::RTTI_IOR) {
189         anItem = (ListBoxIOR*)ListHypAssignation->item(j);
190         isFound = !strcmp(anItem->GetIOR(), i->GetIOR());
191       }
192     }
193     if (!isFound)
194       anItem = new ListBoxIOR (ListHypAssignation,
195                                CORBA::string_dup(i->GetIOR()),
196                                CORBA::string_dup(i->text().latin1()));
197
198   } else if (aSender == ListAlgoDefinition) {
199     for (int j = 0, n = ListAlgoAssignation->count(); !isFound && j < n; j++) {
200       if (ListAlgoAssignation->item(j)->rtti() == ListBoxIOR::RTTI_IOR) {
201         anItem = (ListBoxIOR*)ListAlgoAssignation->item(j);
202         isFound = !strcmp(anItem->GetIOR(), i->GetIOR());
203       }
204     }
205     if (!isFound)
206       anItem = new ListBoxIOR (ListAlgoAssignation,
207                                CORBA::string_dup(i->GetIOR()),
208                                CORBA::string_dup(i->text().latin1()));
209   } else {
210   }
211
212   if (!isFound)
213     myNbModification += isOld(item) ? -1 : 1;
214
215   emit( needToUpdate() );
216 }
217
218 //=================================================================================
219 // function : setListsEnabled()
220 // purpose  :
221 //=================================================================================
222 void SMESHGUI_EditHypothesesDlg::setListsEnabled( const bool en )
223 {
224   ListHypDefinition  ->setEnabled( en );
225   ListHypAssignation ->setEnabled( en );
226   ListAlgoDefinition ->setEnabled( en );
227   ListAlgoAssignation->setEnabled( en );
228 }
229
230 //=================================================================================
231 // function : hypoCount()
232 // purpose  :
233 //=================================================================================
234 int SMESHGUI_EditHypothesesDlg::hypoCount() const
235 {
236   return ListHypAssignation->count();
237 }
238
239 //=================================================================================
240 // function : algoCount()
241 // purpose  :
242 //=================================================================================
243 int SMESHGUI_EditHypothesesDlg::algoCount() const
244 {
245   return ListAlgoAssignation->count();
246 }
247
248 //=================================================================================
249 // function : isModified()
250 // purpose  :
251 //=================================================================================
252 bool SMESHGUI_EditHypothesesDlg::isModified() const
253 {
254   return myNbModification!=0;
255 }
256
257 //=================================================================================
258 // function : findItem()
259 // purpose  :
260 //=================================================================================
261 int SMESHGUI_EditHypothesesDlg::findItem( QListBox* listBox, const QString& ior )
262 {
263   for (int i = 0; i < listBox->count(); i++)
264   {
265     if (listBox->item(i)->rtti() == ListBoxIOR::RTTI_IOR)
266     {
267       ListBoxIOR* anItem = (ListBoxIOR*)(listBox->item(i));
268       if( anItem && ior == anItem->GetIOR() )
269         return i;
270     }
271   }
272   return -1;
273 }