1 // Copyright (C) 2006-2008 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
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"
31 #include <QItemSelectionModel>
33 #include <QInputDialog>
34 #include <QHeaderView>
39 #include "YacsTrace.hxx"
42 using namespace YACS::HMI;
43 using namespace YACS::ENGINE;
45 YComboBox::YComboBox(QWidget *parent)
48 DEBTRACE("ComboBox::ComboBox");
51 YComboBox::~YComboBox()
55 void YComboBox::hidePopup()
57 DEBTRACE("YComboBox::hidePopup");
58 QComboBox::hidePopup();
62 void YComboBox::showPopup()
64 DEBTRACE("YComboBox::showPopup");
65 QComboBox::showPopup();
70 TablePortsEdition::TablePortsEdition(bool inPorts, QWidget *parent)
72 DEBTRACE("TablePortsEdition::TablePortsEdition");
77 if (tv_ports->horizontalHeader())
78 tv_ports->horizontalHeader()->setStretchLastSection(true);
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");
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()));
90 connect(cb_insert, SIGNAL(activated(const QString&)),
91 this, SLOT(on_cb_insert_activated(const QString&)));
94 TablePortsEdition::~TablePortsEdition()
96 DEBTRACE("TablePortsEdition::~TablePortsEdition");
99 void TablePortsEdition::setNode(SubjectElementaryNode* node)
104 void TablePortsEdition::on_pb_up_clicked()
106 DEBTRACE("TablePortsEdition::on_pb_up_clicked");
107 upOrDown(1);// --- n>0 means go up n steps
111 void TablePortsEdition::on_pb_down_clicked()
113 DEBTRACE("TablePortsEdition::on_pb_down_clicked");
114 upOrDown(0); // --- 0 means go down one step
118 /*! move up or down the port in the list of ports.
119 * when isUp = 0, go down one step, when isUp = n>0,
122 void TablePortsEdition::upOrDown(int isUp)
124 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
125 SubjectDataPort *spToMove = 0;
127 QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
129 if (!items.isEmpty()) index = items.first();
130 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
134 Subject *sub = item->getSubject();
135 DEBTRACE(sub->getName());
136 spToMove = dynamic_cast<SubjectDataPort*>(sub);
141 Subject *sub = item->parent()->getSubject();
142 SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
144 sen->OrderDataPorts(spToMove,isUp);
148 void TablePortsEdition::on_pb_insert_clicked()
150 DEBTRACE("TablePortsEdition::on_pb_insert_clicked");
151 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
152 SubjectDataPort *spBefore = 0;
155 QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
157 if (!items.isEmpty()) index = items.first();
158 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
162 Subject *sub = item->getSubject();
163 DEBTRACE(sub->getName());
164 spBefore = dynamic_cast<SubjectDataPort*>(sub);
165 DataPort *port = spBefore->getPort();
167 bool isInput = false;
168 if (dynamic_cast<SubjectInputPort*>(spBefore))
171 sub = item->parent()->getSubject();
172 SubjectElementaryNode* sen = dynamic_cast<SubjectElementaryNode*>(sub);
174 ElementaryNode* father = dynamic_cast<ElementaryNode*>(sen->getNode());
179 list<InputPort*> plist = father->getSetOfInputPort();
180 list<InputPort*>::iterator pos = plist.begin();
181 for (; (*pos) != port; pos++)
183 _nbUp = plist.size() -rang;
188 list<OutputPort*> plist = father->getSetOfOutputPort();
189 list<OutputPort*>::iterator pos = plist.begin();
190 for (; (*pos) != port; pos++)
192 _nbUp = plist.size() -rang;
196 cb_insert->showPopup();
199 void TablePortsEdition::on_cb_insert_activated(const QString& text)
201 DEBTRACE("TablePortsEdition::on_cb_insert_currentIndexChanged " << text.toStdString());
202 SubjectDataPort *spBefore = 0;
203 QModelIndexList items = tv_ports->selectionModel()->selection().indexes();
205 if (!items.isEmpty()) index = items.first();
206 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
209 Subject *sub = item->getSubject();
210 DEBTRACE(sub->getName());
211 spBefore = dynamic_cast<SubjectDataPort*>(sub);
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();
222 GuiEditor *editor = QtGuiContext::getQtCurrent()->getGMain()->_guiEditor;
223 SubjectDataPort * sdp = 0;
225 sdp = editor->CreateInputPort(_node, "", catalog, portType, spBefore);
227 sdp = editor->CreateOutputPort(_node, "", catalog, portType, spBefore);
230 DEBTRACE("move up created port " << _nbUp);
231 _node->OrderDataPorts(sdp, _nbUp);
236 void TablePortsEdition::on_pb_remove_clicked()
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();
243 if (!items.isEmpty()) index = items.first();
244 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
247 Subject *sub = item->getSubject();
248 DEBTRACE(sub->getName());
249 spToRemove = dynamic_cast<SubjectDataPort*>(sub);
253 QtGuiContext::getQtCurrent()->getGMain()->_guiEditor->DeleteSubject(spToRemove->getParent(),
258 void TablePortsEdition::adjustColumns()
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);
268 void TablePortsEdition::setEditablePorts(bool isEditable)
270 DEBTRACE("TablePortsEdition::setEditablePorts " << isEditable);
271 if (QtGuiContext::getQtCurrent()->isEdition())
273 tv_ports->setColumnHidden(YType, false);
274 tv_ports->setColumnHidden(YValue, false);
278 isEditable = false; // --- no edition in execution !
279 tv_ports->setColumnHidden(YType, true);
280 tv_ports->setColumnHidden(YState, false);
283 if (tv_ports->verticalHeader())
284 tv_ports->verticalHeader()->setVisible(false);
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);
310 void TablePortsEdition::on_cb_insert_popupHide()
312 DEBTRACE("TablePortsEdition::on_cb_insert_popupHide");
313 if (cb_insert->currentIndex() < 0)
314 _nbUp = 0; // --- no selection, no port creation, no move
318 void TablePortsEdition::on_cb_insert_popupShow()
320 DEBTRACE("TablePortsEdition::on_cb_insert_popupShow");