1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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 "SchemaNodeItem.hxx"
21 #include "SchemaInPortItem.hxx"
22 #include "SchemaOutPortItem.hxx"
23 #include "InputPort.hxx"
24 #include "OutputPort.hxx"
25 #include "ItemMimeData.hxx"
26 #include "QtGuiContext.hxx"
27 #include "GuiExecutor.hxx"
29 #include "Message.hxx"
39 #include "YacsTrace.hxx"
42 using namespace YACS::ENGINE;
43 using namespace YACS::HMI;
45 SchemaNodeItem::SchemaNodeItem(SchemaItem *parent, QString label, Subject* subject)
46 : SchemaItem(parent, label, subject)
48 DEBTRACE("SchemaNodeItem::SchemaNodeItem");
49 _itemDeco.replace(YLabel, QIcon("icons:node.png"));
50 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
51 if (!model->isEdition())
53 _itemCheckState.replace(YLabel, Qt::Unchecked);
54 setExecState(YACS::UNDEFINED);
59 SchemaNodeItem::~SchemaNodeItem()
61 DEBTRACE("SchemaNodeItem::~SchemaNodeItem");
64 void SchemaNodeItem::update(GuiEvent event, int type, Subject* son)
66 DEBTRACE("SchemaNodeItem::update "<<eventName(event)<<" "<<type<<" "<<son);
67 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
69 SubjectNode *snode = 0;
76 case YACS::HMI::INPUTPORT:
77 case YACS::HMI::INPUTDATASTREAMPORT:
79 int nbsons = childCount();
80 model->beginInsertRows(modelIndex(), nbsons, nbsons);
81 item = new SchemaInPortItem(this,
82 son->getName().c_str(),
84 model->endInsertRows();
87 case YACS::HMI::OUTPUTPORT:
88 case YACS::HMI::OUTPUTDATASTREAMPORT:
90 int nbsons = childCount();
91 model->beginInsertRows(modelIndex(), nbsons, nbsons);
92 item = new SchemaOutPortItem(this,
93 son->getName().c_str(),
95 model->endInsertRows();
99 // DEBTRACE("SchemaNodeItem::update(), ADD, type not handled: " << type);
102 case YACS::HMI::ORDER:
104 YASSERT(QtGuiContext::getQtCurrent()->_mapOfSchemaItem.count(son));
105 //bool isInput = dynamic_cast<SubjectInputPort*>(son);
107 snode = dynamic_cast<SubjectNode*>(_subject);
109 Node* node = snode->getNode();
110 ElementaryNode* father = dynamic_cast<ElementaryNode*>(node);
112 int nbChildren = childCount();
114 model->beginRemoveRows(modelIndex(), 0, nbChildren-1);
115 for (int i = nbChildren; i >= 0; i--)
116 removeChild(child(i));
117 model->endRemoveRows();
119 list<InputPort*> plisti = father->getSetOfInputPort();
120 int nbIn = plisti.size();
123 model->beginInsertRows(modelIndex(), 0, nbIn-1);
124 list<InputPort*>::iterator iti = plisti.begin();
125 for(; iti != plisti.end(); iti++)
127 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*iti)];
128 SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sub];
131 model->endInsertRows();
134 list<OutputPort*> plisto = father->getSetOfOutputPort();
135 int nbOut = plisto.size();
138 model->beginInsertRows(modelIndex(), nbIn, nbIn + nbOut -1);
139 list<OutputPort*>::iterator ito = plisto.begin();
140 for(; ito != plisto.end(); ito++)
142 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*ito)];
143 SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sub];
146 model->endInsertRows();
150 case YACS::HMI::UPDATE:
151 snode = dynamic_cast<SubjectNode*>(_subject);
153 node = snode->getNode();
155 switch (node->getState())
158 _itemForeground.replace(YLabel, QColor("red"));
159 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
162 _itemForeground.replace(YLabel, QColor("blue"));
163 model->setData(modelIndex(YLabel), 0);
169 case YACS::HMI::UPDATEPROGRESS:
171 model->setData(modelIndex(YState), 0);
174 //DEBTRACE("SchemaNodeItem::update(), event not handled: " << eventName(event));
175 SchemaItem::update(event, type, son);
179 void SchemaNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
181 ElementaryNodeMenu m;
182 m.popupMenu(caller, globalPos);
185 Qt::ItemFlags SchemaNodeItem::flags(const QModelIndex &index)
187 Qt::ItemFlags pflag = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled;
188 if ( !QtGuiContext::getQtCurrent() || !QtGuiContext::getQtCurrent()->isEdition())
191 if (QtGuiContext::getQtCurrent()->isEdition())
192 pflag = pflag | Qt::ItemIsDragEnabled;
193 Qt::ItemFlags flagEdit = 0;
194 int column = index.column();
198 flagEdit = Qt::ItemIsEditable; // --- item value editable in model view (for node case in switch)
202 return pflag | flagEdit;
206 * drag for nodes in tree are used for control link with Left Mouse Button
207 * and for reparent with Middle Mouse Button
209 QString SchemaNodeItem::getMimeFormat()
211 if (QApplication::mouseButtons() == Qt::MidButton)
212 return "yacs/subjectNode";
214 return "yacs/subjectOutGate";
217 void SchemaNodeItem::toggleState()
219 DEBTRACE("SchemaNodeItem::toggleState");
220 SchemaItem::toggleState();
221 GuiExecutor *guiExec = QtGuiContext::getQtCurrent()->getGuiExecutor();
223 SubjectNode *subjectNode = dynamic_cast<SubjectNode*>(getSubject());
224 YASSERT(subjectNode);
225 string nodeName = QtGuiContext::getQtCurrent()->getProc()->getChildName(subjectNode->getNode());
226 DEBTRACE("nodeName=" << nodeName);
228 if (_itemCheckState.value(YLabel) == Qt::Checked) // already toggled
229 guiExec->addBreakpoint(nodeName);
231 guiExec->removeBreakpoint(nodeName);
235 * drop in nodes are used for control link
237 bool SchemaNodeItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
239 DEBTRACE("SchemaNodeItem::dropMimeData");
240 if (!data) return false;
241 const ItemMimeData* myData = dynamic_cast<const ItemMimeData*>(data);
242 if (!myData) return false;
243 if(!myData->hasFormat("yacs/subjectOutGate")) return false;
245 Subject *subFrom = myData->getSubject();
246 if (!subFrom) return false;
247 SubjectNode* from = dynamic_cast<SubjectNode*>(subFrom);
249 SubjectNode *to = dynamic_cast<SubjectNode*>(getSubject());
250 if (!to) return false;
256 if (!SubjectNode::tryCreateLink(from, to))
262 void SchemaNodeItem::setCaseValue()
264 DEBTRACE("SchemaNodeItem::setCaseValue");
265 Subject *sub = _parentItem->getSubject();
266 SubjectSwitch *sSwitch = dynamic_cast<SubjectSwitch*>(sub);
267 if (!sSwitch) return;
269 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
270 Switch *aSwitch = dynamic_cast<Switch*>(sSwitch->getNode());
272 SubjectNode *sNode = dynamic_cast<SubjectNode*>(_subject);
274 int rank = aSwitch->getRankOfNode(sNode->getNode());
275 if (rank == Switch::ID_FOR_DEFAULT_NODE)
276 _itemData.replace(YValue, "default");
278 _itemData.replace(YValue, rank);
279 model->setData(modelIndex(YValue), 0);
282 QVariant SchemaNodeItem::editionWhatsThis(int column) const
284 return "<p>To edit the node properties, select the node and use the input panel. <a href=\"modification.html#property-page-for-node\">More...</a></p>";