]> SALOME platform Git repositories - modules/yacs.git/blob - src/genericgui/TablePortsEdition.cxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / genericgui / TablePortsEdition.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "TablePortsEdition.hxx"
20 #include "SchemaItem.hxx"
21 #include "QtGuiContext.hxx"
22 #include "GuiEditor.hxx"
23 #include "guiObservers.hxx"
24 #include "Catalog.hxx"
25 #include "CatalogWidget.hxx"
26 #include "DataPort.hxx"
27 #include "InputPort.hxx"
28 #include "OutputPort.hxx"
29 #include "ElementaryNode.hxx"
30
31 #include <QItemSelectionModel>
32 #include <QDialog>
33 #include <QInputDialog>
34 #include <QHeaderView>
35
36 #include <cassert>
37
38 //#define _DEVDEBUG_
39 #include "YacsTrace.hxx"
40
41 using namespace std;
42 using namespace YACS::HMI;
43 using namespace YACS::ENGINE;
44
45 YComboBox::YComboBox(QWidget *parent)
46   : QComboBox(parent)
47 {
48   DEBTRACE("ComboBox::ComboBox");
49 }
50
51 YComboBox::~YComboBox()
52 {
53 }
54
55 void YComboBox::hidePopup()
56 {
57   DEBTRACE("YComboBox::hidePopup");
58   QComboBox::hidePopup();
59   emit popupHide();
60 }
61
62 void YComboBox::showPopup()
63 {
64   DEBTRACE("YComboBox::showPopup");
65   QComboBox::showPopup();
66   emit popupShow();
67 }
68
69
70 TablePortsEdition::TablePortsEdition(bool inPorts, QWidget *parent)
71 {
72   DEBTRACE("TablePortsEdition::TablePortsEdition");
73   setupUi(this);
74   _inPorts = inPorts;
75   _node = 0;
76   _nbUp = 0;
77   if (tv_ports->horizontalHeader())
78       tv_ports->horizontalHeader()->setStretchLastSection(true);
79
80   delete cb_insert;
81   cb_insert = new YComboBox(layoutWidget);
82   gridLayout1->addWidget(cb_insert, 0, 4, 1, 1);
83   cb_insert->setToolTip("port creation: select a port type");
84
85   connect(cb_insert, SIGNAL(popupHide()),
86           this, SLOT(on_cb_insert_popupHide()));
87   connect(cb_insert, SIGNAL(popupShow()),
88           this, SLOT(on_cb_insert_popupShow()));
89
90   connect(cb_insert, SIGNAL(activated(const QString&)),
91           this, SLOT(on_cb_insert_activated(const QString&)));
92 }
93
94 TablePortsEdition::~TablePortsEdition()
95 {
96   DEBTRACE("TablePortsEdition::~TablePortsEdition");
97 }
98
99 void TablePortsEdition::setNode(SubjectElementaryNode* node)
100 {
101   _node = node;
102 }
103
104 void TablePortsEdition::on_pb_up_clicked()
105 {
106   DEBTRACE("TablePortsEdition::on_pb_up_clicked");
107   upOrDown(1);// --- n>0 means go up n steps
108   _nbUp = 0;
109 }
110
111 void TablePortsEdition::on_pb_down_clicked()
112 {
113   DEBTRACE("TablePortsEdition::on_pb_down_clicked");
114   upOrDown(0); // --- 0 means go down one step
115   _nbUp = 0;
116 }
117
118 /*! move up or down the port in the list of ports.
119  *  when isUp = 0, go down one step, when isUp = n>0,
120  *  go up n steps.
121  */
122 void TablePortsEdition::upOrDown(int isUp)
123 {
124   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
125   SubjectDataPort *spToMove = 0;
126
127   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
128   QModelIndex index;
129   if (!items.isEmpty()) index = items.first();
130   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
131
132   if (item)
133     {
134       Subject *sub = item->getSubject();
135       DEBTRACE(sub->getName());
136       spToMove = dynamic_cast<SubjectDataPort*>(sub);
137     }
138
139   if (spToMove)
140     {
141       Subject *sub = item->parent()->getSubject();
142       SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
143       assert(sen);
144       sen->OrderDataPorts(spToMove,isUp);
145     }
146 }
147
148 void TablePortsEdition::on_pb_insert_clicked()
149 {
150   DEBTRACE("TablePortsEdition::on_pb_insert_clicked");
151   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
152   SubjectDataPort *spBefore = 0;
153   _nbUp = 0;
154
155   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
156   QModelIndex index;
157   if (!items.isEmpty()) index = items.first();
158   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
159   if (!item)
160     return;
161
162   Subject *sub = item->getSubject();
163   DEBTRACE(sub->getName());
164   spBefore = dynamic_cast<SubjectDataPort*>(sub);
165   DataPort *port = spBefore->getPort();
166
167   bool isInput = false;
168   if (dynamic_cast<SubjectInputPort*>(spBefore))
169     isInput = true;
170
171   sub = item->parent()->getSubject();
172   SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
173   assert(sen);
174   ElementaryNode* father = dynamic_cast<ElementaryNode*>(sen->getNode());
175
176   if (isInput)
177     {
178       int rang = 0;
179       list<InputPort*> plist = father->getSetOfInputPort();
180       list<InputPort*>::iterator pos = plist.begin();
181       for (; (*pos) != port; pos++)
182         rang++;
183       _nbUp = plist.size() -rang;
184     }
185   else
186     {
187       int rang = 0;
188       list<OutputPort*> plist = father->getSetOfOutputPort();
189       list<OutputPort*>::iterator pos = plist.begin();
190       for (; (*pos) != port; pos++)
191         rang++;
192       _nbUp = plist.size() -rang;
193     }
194
195   DEBTRACE(_nbUp);
196   cb_insert->showPopup();  
197 }
198
199 void TablePortsEdition::on_cb_insert_activated(const QString& text)
200 {
201   DEBTRACE("TablePortsEdition::on_cb_insert_currentIndexChanged " << text.toStdString());
202   SubjectDataPort *spBefore = 0;
203   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
204   QModelIndex index;
205   if (!items.isEmpty()) index = items.first();
206   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
207   if (item)
208     {
209       Subject *sub = item->getSubject();
210       DEBTRACE(sub->getName());
211       spBefore = dynamic_cast<SubjectDataPort*>(sub);
212     }
213   string portType = text.toStdString();
214   QStringList defaultTypes;
215   defaultTypes << "Int" << "Double" << "String" << "Bool";
216   if (defaultTypes.contains(text))
217     portType = text.toLower().toStdString();
218   YACS::ENGINE::Catalog *catalog =
219     QtGuiContext::getQtCurrent()->getGMain()->getCatalogWidget()->getCatalogFromType(portType);
220   if (!catalog) catalog = QtGuiContext::getQtCurrent()->getCurrentCatalog();
221   assert(catalog);
222   GuiEditor *editor = QtGuiContext::getQtCurrent()->getGMain()->_guiEditor;
223   SubjectDataPort * sdp = 0;
224   if (_inPorts)
225     sdp = editor->CreateInputPort(_node, "", catalog, portType, spBefore);
226   else
227     sdp = editor->CreateOutputPort(_node, "", catalog, portType, spBefore);
228   if (_nbUp)
229     {
230       DEBTRACE("move up created port " << _nbUp);
231       _node->OrderDataPorts(sdp, _nbUp);
232       _nbUp = 0;
233     }
234 }
235
236 void TablePortsEdition::on_pb_remove_clicked()
237 {
238   DEBTRACE("TablePortsEdition::on_pb_remove_clicked");
239   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
240   SubjectDataPort *spToRemove = 0;
241   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
242   QModelIndex index;
243   if (!items.isEmpty()) index = items.first();
244   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
245   if (item)
246     {
247       Subject *sub = item->getSubject();
248       DEBTRACE(sub->getName());
249       spToRemove = dynamic_cast<SubjectDataPort*>(sub);
250     }
251   if (spToRemove)
252     {
253       QtGuiContext::getQtCurrent()->getGMain()->_guiEditor->DeleteSubject(spToRemove->getParent(),
254                                                                           spToRemove);
255     }
256 }
257
258 void TablePortsEdition::adjustColumns()
259 {
260   DEBTRACE("TablePortsEdition::adjustColumns");
261   if (!tv_ports->isColumnHidden(YLabel))
262     tv_ports->resizeColumnToContents(YLabel); 
263   if (!tv_ports->isColumnHidden(YType))
264     tv_ports->resizeColumnToContents(YType); 
265 }
266
267
268 void TablePortsEdition::setEditablePorts(bool isEditable)
269 {
270   DEBTRACE("TablePortsEdition::setEditablePorts " << isEditable);
271   if (QtGuiContext::getQtCurrent()->isEdition())
272     {
273       tv_ports->setColumnHidden(YType,  false);
274       tv_ports->setColumnHidden(YValue, false);
275     }
276   else
277     {
278       isEditable = false; // --- no edition in execution !
279       tv_ports->setColumnHidden(YType,  true);
280       tv_ports->setColumnHidden(YState, false);
281     }
282
283   if (tv_ports->verticalHeader())
284     tv_ports->verticalHeader()->setVisible(false);
285
286
287   pb_up->setEnabled(isEditable);
288   pb_down->setEnabled(isEditable);
289   pb_insert->setEnabled(isEditable);
290   pb_remove->setEnabled(isEditable);
291   cb_insert->setEnabled(isEditable);
292   if (isEditable)
293     {
294       pb_up->show();
295       pb_down->show();
296       pb_insert->show();
297       pb_remove->show();
298       cb_insert->show();
299     }
300   else
301     {
302       pb_up->hide();
303       pb_down->hide();
304       pb_insert->hide();
305       pb_remove->hide();
306       cb_insert->hide();
307     }
308 }
309
310 void TablePortsEdition::on_cb_insert_popupHide()
311 {
312   DEBTRACE("TablePortsEdition::on_cb_insert_popupHide");
313   if (cb_insert->currentIndex() < 0) 
314     _nbUp = 0; // --- no selection, no port creation, no move
315   DEBTRACE(_nbUp);
316 }
317
318 void TablePortsEdition::on_cb_insert_popupShow()
319 {
320   DEBTRACE("TablePortsEdition::on_cb_insert_popupShow");
321 }
322