Salome HOME
Copyright update 2021
[modules/yacs.git] / src / genericgui / TablePortsEdition.cxx
1 // Copyright (C) 2006-2021  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, or (at your option) any later version.
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
20 #include "TablePortsEdition.hxx"
21 #include "SchemaItem.hxx"
22 #include "QtGuiContext.hxx"
23 #include "GuiEditor.hxx"
24 #include "guiObservers.hxx"
25 #include "Catalog.hxx"
26 #include "CatalogWidget.hxx"
27 #include "DataPort.hxx"
28 #include "InputPort.hxx"
29 #include "OutputPort.hxx"
30 #include "ElementaryNode.hxx"
31 #include "Scene.hxx"
32 #include "SceneItem.hxx"
33 #include "SceneComposedNodeItem.hxx"
34
35 #include <QItemSelectionModel>
36 #include <QDialog>
37 #include <QInputDialog>
38 #include <QHeaderView>
39
40 #include <cassert>
41
42 //#define _DEVDEBUG_
43 #include "YacsTrace.hxx"
44
45 using namespace std;
46 using namespace YACS::HMI;
47 using namespace YACS::ENGINE;
48
49 YComboBox::YComboBox(QWidget *parent)
50   : QComboBox(parent)
51 {
52   DEBTRACE("ComboBox::ComboBox");
53 }
54
55 YComboBox::~YComboBox()
56 {
57 }
58
59 void YComboBox::hidePopup()
60 {
61   DEBTRACE("YComboBox::hidePopup");
62   QComboBox::hidePopup();
63   emit popupHide();
64 }
65
66 void YComboBox::showPopup()
67 {
68   DEBTRACE("YComboBox::showPopup");
69   QComboBox::showPopup();
70   emit popupShow();
71 }
72
73 void YComboBox::keyPressEvent(QKeyEvent *e)
74 {
75   //accept all key events but do nothing to avoid creating ports with keys
76 }
77
78 #ifndef QT_NO_WHEELEVENT
79 void YComboBox::wheelEvent(QWheelEvent *e)
80 {
81   //idem
82 }
83 #endif
84
85
86 TablePortsEdition::TablePortsEdition(bool inPorts, QWidget *parent)
87 {
88   DEBTRACE("TablePortsEdition::TablePortsEdition");
89   setupUi(this);
90   _inPorts = inPorts;
91   _node = 0;
92   _nbUp = 0;
93   if (tv_ports->horizontalHeader())
94       tv_ports->horizontalHeader()->setStretchLastSection(true);
95
96   delete cb_insert;
97   cb_insert = new YComboBox(layoutWidget);
98   gridLayout1->addWidget(cb_insert, 0, 4, 1, 1);
99   cb_insert->setToolTip("port creation: select a port type");
100
101   connect(cb_insert, SIGNAL(popupHide()),
102           this, SLOT(oncb_insert_popupHide()));
103   connect(cb_insert, SIGNAL(popupShow()),
104           this, SLOT(oncb_insert_popupShow()));
105
106   connect(cb_insert, SIGNAL(activated(const QString&)),
107           this, SLOT(oncb_insert_activated(const QString&)));
108 }
109
110 TablePortsEdition::~TablePortsEdition()
111 {
112   DEBTRACE("TablePortsEdition::~TablePortsEdition");
113 }
114
115 void TablePortsEdition::setNode(SubjectElementaryNode* node)
116 {
117   _node = node;
118 }
119
120 void TablePortsEdition::on_pb_up_clicked()
121 {
122   DEBTRACE("TablePortsEdition::on_pb_up_clicked");
123   upOrDown(1);// --- n>0 means go up n steps
124   _nbUp = 0;
125 }
126
127 void TablePortsEdition::on_pb_down_clicked()
128 {
129   DEBTRACE("TablePortsEdition::on_pb_down_clicked");
130   upOrDown(0); // --- 0 means go down one step
131   _nbUp = 0;
132 }
133
134 /*! move up or down the port in the list of ports.
135  *  when isUp = 0, go down one step, when isUp = n>0,
136  *  go up n steps.
137  */
138 void TablePortsEdition::upOrDown(int isUp)
139 {
140   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
141   SubjectDataPort *spToMove = 0;
142
143   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
144   QModelIndex index;
145   if (!items.isEmpty()) index = items.first();
146   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
147
148   if (item)
149     {
150       Subject *sub = item->getSubject();
151       DEBTRACE(sub->getName());
152       spToMove = dynamic_cast<SubjectDataPort*>(sub);
153     }
154
155   if (spToMove)
156     {
157       Subject *sub = item->parent()->getSubject();
158       SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
159       YASSERT(sen);
160       sen->OrderDataPorts(spToMove,isUp);
161       if (Scene::_autoComputeLinks)
162         {
163           YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
164           SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
165           SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
166           proc->rebuildLinks();
167         }
168
169       QItemSelectionModel *selectionModel = tv_ports->selectionModel();
170       QModelIndex topLeft=item->modelIndex(0);
171       QItemSelection selection(topLeft, topLeft);
172       selectionModel->select(selection, QItemSelectionModel::Select);
173     }
174 }
175
176 void TablePortsEdition::on_pb_insert_clicked()
177 {
178   DEBTRACE("TablePortsEdition::on_pb_insert_clicked");
179   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
180   SubjectDataPort *spBefore = 0;
181   _nbUp = 0;
182
183   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
184   QModelIndex index;
185   if (!items.isEmpty()) index = items.first();
186   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
187   if (!item)
188     return;
189
190   Subject *sub = item->getSubject();
191   DEBTRACE(sub->getName());
192   spBefore = dynamic_cast<SubjectDataPort*>(sub);
193   DataPort *port = spBefore->getPort();
194
195   bool isInput = false;
196   if (dynamic_cast<SubjectInputPort*>(spBefore))
197     isInput = true;
198
199   sub = item->parent()->getSubject();
200   SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
201   YASSERT(sen);
202   ElementaryNode* father = dynamic_cast<ElementaryNode*>(sen->getNode());
203
204   if (isInput)
205     {
206       int rang = 0;
207       list<InputPort*> plist = father->getSetOfInputPort();
208       list<InputPort*>::iterator pos = plist.begin();
209       for (; (*pos) != port; pos++)
210         rang++;
211       _nbUp = plist.size() -rang;
212     }
213   else
214     {
215       int rang = 0;
216       list<OutputPort*> plist = father->getSetOfOutputPort();
217       list<OutputPort*>::iterator pos = plist.begin();
218       for (; (*pos) != port; pos++)
219         rang++;
220       _nbUp = plist.size() -rang;
221     }
222
223   DEBTRACE(_nbUp);
224   cb_insert->showPopup();  
225 }
226
227 void TablePortsEdition::oncb_insert_activated(const QString& text)
228 {
229   DEBTRACE("TablePortsEdition::oncb_insert_activated " << text.toStdString());
230   SubjectDataPort *spBefore = 0;
231   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
232   QModelIndex index;
233   if (!items.isEmpty()) index = items.first();
234   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
235   if (item)
236     {
237       Subject *sub = item->getSubject();
238       DEBTRACE(sub->getName());
239       spBefore = dynamic_cast<SubjectDataPort*>(sub);
240     }
241   string portType = text.toStdString();
242   QStringList defaultTypes;
243   defaultTypes << "Int" << "Double" << "String" << "Bool";
244   if (defaultTypes.contains(text))
245     portType = text.toLower().toStdString();
246   YACS::ENGINE::Catalog *catalog =
247     QtGuiContext::getQtCurrent()->getGMain()->getCatalogWidget()->getCatalogFromType(portType);
248   if (!catalog) catalog = QtGuiContext::getQtCurrent()->getCurrentCatalog();
249   YASSERT(catalog);
250   GuiEditor *editor = QtGuiContext::getQtCurrent()->getGMain()->_guiEditor;
251   SubjectDataPort * sdp = 0;
252   if (_inPorts)
253     sdp = editor->CreateInputPort(_node, "", catalog, portType, spBefore);
254   else
255     sdp = editor->CreateOutputPort(_node, "", catalog, portType, spBefore);
256   if (_nbUp)
257     {
258       DEBTRACE("move up created port " << _nbUp);
259       _node->OrderDataPorts(sdp, _nbUp);
260       _nbUp = 0;
261     }
262 }
263
264 void TablePortsEdition::on_pb_remove_clicked()
265 {
266   DEBTRACE("TablePortsEdition::on_pb_remove_clicked");
267   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
268   SubjectDataPort *spToRemove = 0;
269   QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
270   QModelIndex index;
271   if (!items.isEmpty()) index = items.first();
272   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
273   if (item)
274     {
275       Subject *sub = item->getSubject();
276       DEBTRACE(sub->getName());
277       spToRemove = dynamic_cast<SubjectDataPort*>(sub);
278     }
279   if (spToRemove)
280     {
281       QtGuiContext::getQtCurrent()->getGMain()->_guiEditor->DeleteSubject(spToRemove->getParent(),
282                                                                           spToRemove);
283     }
284 }
285
286 void TablePortsEdition::adjustColumns()
287 {
288   DEBTRACE("TablePortsEdition::adjustColumns");
289   if (!tv_ports->isColumnHidden(YLabel))
290     tv_ports->resizeColumnToContents(YLabel); 
291   if (!tv_ports->isColumnHidden(YType))
292     tv_ports->resizeColumnToContents(YType); 
293 }
294
295
296 void TablePortsEdition::setEditablePorts(bool isEditable)
297 {
298   DEBTRACE("TablePortsEdition::setEditablePorts " << isEditable);
299   if (QtGuiContext::getQtCurrent()->isEdition())
300     {
301       tv_ports->setColumnHidden(YType,  false);
302       tv_ports->setColumnHidden(YValue, false);
303     }
304   else
305     {
306       isEditable = false; // --- no edition in execution !
307       tv_ports->setColumnHidden(YType,  true);
308       tv_ports->setColumnHidden(YState, false);
309     }
310
311   if (tv_ports->verticalHeader())
312     tv_ports->verticalHeader()->setVisible(false);
313
314
315   pb_up->setEnabled(isEditable);
316   pb_down->setEnabled(isEditable);
317   pb_insert->setEnabled(isEditable);
318   pb_remove->setEnabled(isEditable);
319   cb_insert->setEnabled(isEditable);
320   if (isEditable)
321     {
322       pb_up->show();
323       pb_down->show();
324       pb_insert->show();
325       pb_remove->show();
326       cb_insert->show();
327     }
328   else
329     {
330       pb_up->hide();
331       pb_down->hide();
332       pb_insert->hide();
333       pb_remove->hide();
334       cb_insert->hide();
335     }
336 }
337
338 void TablePortsEdition::oncb_insert_popupHide()
339 {
340   DEBTRACE("TablePortsEdition::oncb_insert_popupHide");
341   if (cb_insert->currentIndex() < 0) 
342     _nbUp = 0; // --- no selection, no port creation, no move
343   DEBTRACE(_nbUp);
344 }
345
346 void TablePortsEdition::oncb_insert_popupShow()
347 {
348   DEBTRACE("TablePortsEdition::oncb_insert_popupShow");
349 }
350