1 // Copyright (C) 2006-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
32 #include "SceneItem.hxx"
33 #include "SceneComposedNodeItem.hxx"
35 #include <QItemSelectionModel>
37 #include <QInputDialog>
38 #include <QHeaderView>
43 #include "YacsTrace.hxx"
46 using namespace YACS::HMI;
47 using namespace YACS::ENGINE;
49 YComboBox::YComboBox(QWidget *parent)
52 DEBTRACE("ComboBox::ComboBox");
55 YComboBox::~YComboBox()
59 void YComboBox::hidePopup()
61 DEBTRACE("YComboBox::hidePopup");
62 QComboBox::hidePopup();
66 void YComboBox::showPopup()
68 DEBTRACE("YComboBox::showPopup");
69 QComboBox::showPopup();
73 void YComboBox::keyPressEvent(QKeyEvent *e)
75 //accept all key events but do nothing to avoid creating ports with keys
78 #ifndef QT_NO_WHEELEVENT
79 void YComboBox::wheelEvent(QWheelEvent *e)
86 TablePortsEdition::TablePortsEdition(bool inPorts, QWidget *parent)
88 DEBTRACE("TablePortsEdition::TablePortsEdition");
93 if (tv_ports->horizontalHeader())
94 tv_ports->horizontalHeader()->setStretchLastSection(true);
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");
101 connect(cb_insert, SIGNAL(popupHide()),
102 this, SLOT(oncb_insert_popupHide()));
103 connect(cb_insert, SIGNAL(popupShow()),
104 this, SLOT(oncb_insert_popupShow()));
106 connect(cb_insert, SIGNAL(activated(const QString&)),
107 this, SLOT(oncb_insert_activated(const QString&)));
110 TablePortsEdition::~TablePortsEdition()
112 DEBTRACE("TablePortsEdition::~TablePortsEdition");
115 void TablePortsEdition::setNode(SubjectElementaryNode* node)
120 void TablePortsEdition::on_pb_up_clicked()
122 DEBTRACE("TablePortsEdition::on_pb_up_clicked");
123 upOrDown(1);// --- n>0 means go up n steps
127 void TablePortsEdition::on_pb_down_clicked()
129 DEBTRACE("TablePortsEdition::on_pb_down_clicked");
130 upOrDown(0); // --- 0 means go down one step
134 /*! move up or down the port in the list of ports.
135 * when isUp = 0, go down one step, when isUp = n>0,
138 void TablePortsEdition::upOrDown(int isUp)
140 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
141 SubjectDataPort *spToMove = 0;
143 QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
145 if (!items.isEmpty()) index = items.first();
146 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
150 Subject *sub = item->getSubject();
151 DEBTRACE(sub->getName());
152 spToMove = dynamic_cast<SubjectDataPort*>(sub);
157 Subject *sub = item->parent()->getSubject();
158 SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
160 sen->OrderDataPorts(spToMove,isUp);
161 if (Scene::_autoComputeLinks)
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();
169 QItemSelectionModel *selectionModel = tv_ports->selectionModel();
170 QModelIndex topLeft=item->modelIndex(0);
171 QItemSelection selection(topLeft, topLeft);
172 selectionModel->select(selection, QItemSelectionModel::Select);
176 void TablePortsEdition::on_pb_insert_clicked()
178 DEBTRACE("TablePortsEdition::on_pb_insert_clicked");
179 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
180 SubjectDataPort *spBefore = 0;
183 QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
185 if (!items.isEmpty()) index = items.first();
186 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
190 Subject *sub = item->getSubject();
191 DEBTRACE(sub->getName());
192 spBefore = dynamic_cast<SubjectDataPort*>(sub);
193 DataPort *port = spBefore->getPort();
195 bool isInput = false;
196 if (dynamic_cast<SubjectInputPort*>(spBefore))
199 sub = item->parent()->getSubject();
200 SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
202 ElementaryNode* father = dynamic_cast<ElementaryNode*>(sen->getNode());
207 list<InputPort*> plist = father->getSetOfInputPort();
208 list<InputPort*>::iterator pos = plist.begin();
209 for (; (*pos) != port; pos++)
211 _nbUp = plist.size() -rang;
216 list<OutputPort*> plist = father->getSetOfOutputPort();
217 list<OutputPort*>::iterator pos = plist.begin();
218 for (; (*pos) != port; pos++)
220 _nbUp = plist.size() -rang;
224 cb_insert->showPopup();
227 void TablePortsEdition::oncb_insert_activated(const QString& text)
229 DEBTRACE("TablePortsEdition::oncb_insert_activated " << text.toStdString());
230 SubjectDataPort *spBefore = 0;
231 QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
233 if (!items.isEmpty()) index = items.first();
234 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
237 Subject *sub = item->getSubject();
238 DEBTRACE(sub->getName());
239 spBefore = dynamic_cast<SubjectDataPort*>(sub);
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();
250 GuiEditor *editor = QtGuiContext::getQtCurrent()->getGMain()->_guiEditor;
251 SubjectDataPort * sdp = 0;
253 sdp = editor->CreateInputPort(_node, "", catalog, portType, spBefore);
255 sdp = editor->CreateOutputPort(_node, "", catalog, portType, spBefore);
258 DEBTRACE("move up created port " << _nbUp);
259 _node->OrderDataPorts(sdp, _nbUp);
264 void TablePortsEdition::on_pb_remove_clicked()
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();
271 if (!items.isEmpty()) index = items.first();
272 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
275 Subject *sub = item->getSubject();
276 DEBTRACE(sub->getName());
277 spToRemove = dynamic_cast<SubjectDataPort*>(sub);
281 QtGuiContext::getQtCurrent()->getGMain()->_guiEditor->DeleteSubject(spToRemove->getParent(),
286 void TablePortsEdition::adjustColumns()
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);
296 void TablePortsEdition::setEditablePorts(bool isEditable)
298 DEBTRACE("TablePortsEdition::setEditablePorts " << isEditable);
299 if (QtGuiContext::getQtCurrent()->isEdition())
301 tv_ports->setColumnHidden(YType, false);
302 tv_ports->setColumnHidden(YValue, false);
306 isEditable = false; // --- no edition in execution !
307 tv_ports->setColumnHidden(YType, true);
308 tv_ports->setColumnHidden(YState, false);
311 if (tv_ports->verticalHeader())
312 tv_ports->verticalHeader()->setVisible(false);
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);
338 void TablePortsEdition::oncb_insert_popupHide()
340 DEBTRACE("TablePortsEdition::oncb_insert_popupHide");
341 if (cb_insert->currentIndex() < 0)
342 _nbUp = 0; // --- no selection, no port creation, no move
346 void TablePortsEdition::oncb_insert_popupShow()
348 DEBTRACE("TablePortsEdition::oncb_insert_popupShow");