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 "SchemaNodeItem.hxx"
20 #include "SchemaInPortItem.hxx"
21 #include "SchemaOutPortItem.hxx"
22 #include "InputPort.hxx"
23 #include "OutputPort.hxx"
24 #include "ItemMimeData.hxx"
25 #include "QtGuiContext.hxx"
26 #include "GuiExecutor.hxx"
28 #include "Message.hxx"
38 #include "YacsTrace.hxx"
41 using namespace YACS::ENGINE;
42 using namespace YACS::HMI;
44 SchemaNodeItem::SchemaNodeItem(SchemaItem *parent, QString label, Subject* subject)
45 : SchemaItem(parent, label, subject)
47 DEBTRACE("SchemaNodeItem::SchemaNodeItem");
48 _itemDeco.replace(YLabel, QIcon("icons:node.png"));
49 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
50 if (!model->isEdition())
52 _itemCheckState.replace(YLabel, Qt::Unchecked);
53 setExecState(YACS::UNDEFINED);
58 SchemaNodeItem::~SchemaNodeItem()
60 DEBTRACE("SchemaNodeItem::~SchemaNodeItem");
63 void SchemaNodeItem::update(GuiEvent event, int type, Subject* son)
65 DEBTRACE("SchemaNodeItem::update "<<eventName(event)<<" "<<type<<" "<<son);
66 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
68 SubjectNode *snode = 0;
75 case YACS::HMI::INPUTPORT:
76 case YACS::HMI::INPUTDATASTREAMPORT:
78 int nbsons = childCount();
79 model->beginInsertRows(modelIndex(), nbsons, nbsons);
80 item = new SchemaInPortItem(this,
81 son->getName().c_str(),
83 model->endInsertRows();
86 case YACS::HMI::OUTPUTPORT:
87 case YACS::HMI::OUTPUTDATASTREAMPORT:
89 int nbsons = childCount();
90 model->beginInsertRows(modelIndex(), nbsons, nbsons);
91 item = new SchemaOutPortItem(this,
92 son->getName().c_str(),
94 model->endInsertRows();
98 // DEBTRACE("SchemaNodeItem::update(), ADD, type not handled: " << type);
101 case YACS::HMI::ORDER:
103 assert(QtGuiContext::getQtCurrent()->_mapOfSchemaItem.count(son));
104 //bool isInput = dynamic_cast<SubjectInputPort*>(son);
106 snode = dynamic_cast<SubjectNode*>(_subject);
108 Node* node = snode->getNode();
109 ElementaryNode* father = dynamic_cast<ElementaryNode*>(node);
111 int nbChildren = childCount();
113 model->beginRemoveRows(modelIndex(), 0, nbChildren-1);
114 for (int i = nbChildren; i >= 0; i--)
115 removeChild(child(i));
116 model->endRemoveRows();
118 list<InputPort*> plisti = father->getSetOfInputPort();
119 int nbIn = plisti.size();
122 model->beginInsertRows(modelIndex(), 0, nbIn-1);
123 list<InputPort*>::iterator iti = plisti.begin();
124 for(; iti != plisti.end(); iti++)
126 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*iti)];
127 SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sub];
130 model->endInsertRows();
133 list<OutputPort*> plisto = father->getSetOfOutputPort();
134 int nbOut = plisto.size();
137 model->beginInsertRows(modelIndex(), nbIn, nbIn + nbOut -1);
138 list<OutputPort*>::iterator ito = plisto.begin();
139 for(; ito != plisto.end(); ito++)
141 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*ito)];
142 SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sub];
145 model->endInsertRows();
149 case YACS::HMI::UPDATE:
150 snode = dynamic_cast<SubjectNode*>(_subject);
152 node = snode->getNode();
154 switch (node->getState())
157 _itemForeground.replace(YLabel, QColor("red"));
158 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
161 _itemForeground.replace(YLabel, QColor("blue"));
162 model->setData(modelIndex(YLabel), 0);
168 case YACS::HMI::UPDATEPROGRESS:
170 model->setData(modelIndex(YState), 0);
173 //DEBTRACE("SchemaNodeItem::update(), event not handled: " << eventName(event));
174 SchemaItem::update(event, type, son);
178 void SchemaNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
180 ElementaryNodeMenu m;
181 m.popupMenu(caller, globalPos);
184 Qt::ItemFlags SchemaNodeItem::flags(const QModelIndex &index)
186 Qt::ItemFlags pflag = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled;
188 Qt::ItemFlags flagEdit = 0;
189 int column = index.column();
193 // flagEdit = Qt::ItemIsEditable; // --- port name editable in model view
196 flagEdit = Qt::ItemIsEditable; // --- port value editable in model view
200 return pflag | flagEdit;
204 * drag for nodes in tree are used for control link with Left Mouse Button
205 * and for reparent with Middle Mouse Button
207 QString SchemaNodeItem::getMimeFormat()
209 if (QApplication::mouseButtons() == Qt::MidButton)
210 return "yacs/subjectNode";
212 return "yacs/subjectOutGate";
215 void SchemaNodeItem::toggleState()
217 DEBTRACE("SchemaNodeItem::toggleState");
218 SchemaItem::toggleState();
219 GuiExecutor *guiExec = QtGuiContext::getQtCurrent()->getGuiExecutor();
221 SubjectNode *subjectNode = dynamic_cast<SubjectNode*>(getSubject());
223 string nodeName = QtGuiContext::getQtCurrent()->getProc()->getChildName(subjectNode->getNode());
224 DEBTRACE("nodeName=" << nodeName);
226 if (_itemCheckState.value(YLabel) == Qt::Checked) // already toggled
227 guiExec->addBreakpoint(nodeName);
229 guiExec->removeBreakpoint(nodeName);
233 * drop in nodes are used for control link
235 bool SchemaNodeItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
237 DEBTRACE("SchemaNodeItem::dropMimeData");
238 if (!data) return false;
239 const ItemMimeData* myData = dynamic_cast<const ItemMimeData*>(data);
240 if (!myData) return false;
241 if(!myData->hasFormat("yacs/subjectOutGate")) return false;
243 Subject *subFrom = myData->getSubject();
244 if (!subFrom) return false;
245 SubjectNode* from = dynamic_cast<SubjectNode*>(subFrom);
247 SubjectNode *to = dynamic_cast<SubjectNode*>(getSubject());
248 if (!to) return false;
254 if (!SubjectNode::tryCreateLink(from, to))
260 void SchemaNodeItem::setCaseValue()
262 Subject *sub = _parentItem->getSubject();
263 SubjectSwitch *sSwitch = dynamic_cast<SubjectSwitch*>(sub);
264 if (!sSwitch) return;
266 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
267 Switch *aSwitch = dynamic_cast<Switch*>(sSwitch->getNode());
269 SubjectNode *sNode = dynamic_cast<SubjectNode*>(_subject);
271 int rank = aSwitch->getRankOfNode(sNode->getNode());
272 if (rank == Switch::ID_FOR_DEFAULT_NODE)
273 _itemData.replace(YValue, "default");
275 _itemData.replace(YValue, rank);
276 model->setData(modelIndex(YValue), 0);
279 QVariant SchemaNodeItem::editionWhatsThis(int column) const
281 return "To edit the node properties, select the node and use the input panel.\n";