Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / genericgui / SchemaNodeItem.cxx
1 //  Copyright (C) 2006-2008  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.
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 #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"
27 #include "Menus.hxx"
28 #include "Message.hxx"
29
30 #include "Node.hxx"
31 #include "Switch.hxx"
32
33 #include <QIcon>
34
35 #include <cassert>
36
37 //#define _DEVDEBUG_
38 #include "YacsTrace.hxx"
39
40 using namespace std;
41 using namespace YACS::ENGINE;
42 using namespace YACS::HMI;
43
44 SchemaNodeItem::SchemaNodeItem(SchemaItem *parent, QString label, Subject* subject)
45   : SchemaItem(parent, label, subject)
46 {
47   DEBTRACE("SchemaNodeItem::SchemaNodeItem");
48   _itemDeco.replace(YLabel, QIcon("icons:node.png"));
49   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
50   if (!model->isEdition())
51     {
52       _itemCheckState.replace(YLabel, Qt::Unchecked);
53       setExecState(YACS::UNDEFINED);
54     }
55   setCaseValue();
56 }
57
58 SchemaNodeItem::~SchemaNodeItem()
59 {
60   DEBTRACE("SchemaNodeItem::~SchemaNodeItem");
61 }
62
63 void SchemaNodeItem::update(GuiEvent event, int type, Subject* son)
64 {
65   DEBTRACE("SchemaNodeItem::update "<<eventName(event)<<" "<<type<<" "<<son);
66   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
67   SchemaItem *item = 0;
68   SubjectNode *snode = 0;
69   Node* node = 0;
70   switch (event)
71     {
72     case YACS::HMI::ADD:
73       switch (type)
74         {
75         case YACS::HMI::INPUTPORT:
76         case YACS::HMI::INPUTDATASTREAMPORT:
77           {
78             int nbsons = childCount();
79             model->beginInsertRows(modelIndex(), nbsons, nbsons);
80             item =  new SchemaInPortItem(this,
81                                          son->getName().c_str(),
82                                          son);
83             model->endInsertRows();
84           }
85           break;
86         case YACS::HMI::OUTPUTPORT:
87         case YACS::HMI::OUTPUTDATASTREAMPORT:
88           {
89             int nbsons = childCount();
90             model->beginInsertRows(modelIndex(), nbsons, nbsons);
91             item =  new SchemaOutPortItem(this,
92                                           son->getName().c_str(),
93                                           son);
94             model->endInsertRows();
95           }
96           break;
97 //         default:
98 //           DEBTRACE("SchemaNodeItem::update(), ADD, type not handled: " << type);
99         }
100       break;
101     case YACS::HMI::ORDER:
102       {
103         assert(QtGuiContext::getQtCurrent()->_mapOfSchemaItem.count(son));
104         //bool isInput = dynamic_cast<SubjectInputPort*>(son);
105
106         snode = dynamic_cast<SubjectNode*>(_subject);
107         assert(snode);
108         Node* node = snode->getNode();
109         ElementaryNode* father = dynamic_cast<ElementaryNode*>(node);
110         assert(father);
111         int nbChildren = childCount();
112
113         model->beginRemoveRows(modelIndex(), 0, nbChildren-1);
114         for (int i = nbChildren; i >= 0; i--)
115           removeChild(child(i));
116         model->endRemoveRows();
117
118         list<InputPort*> plisti = father->getSetOfInputPort();
119         int nbIn = plisti.size();
120         if (nbIn)
121           {
122             model->beginInsertRows(modelIndex(), 0, nbIn-1);
123             list<InputPort*>::iterator iti = plisti.begin();
124             for(; iti != plisti.end(); iti++)
125               {
126                 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*iti)];
127                 SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sub];
128                 appendChild(item);
129               }
130             model->endInsertRows();
131           }
132
133         list<OutputPort*> plisto = father->getSetOfOutputPort();
134         int nbOut = plisto.size();
135         if (nbOut)
136           {
137             model->beginInsertRows(modelIndex(), nbIn, nbIn + nbOut -1);
138             list<OutputPort*>::iterator ito = plisto.begin();
139             for(; ito != plisto.end(); ito++)
140               {
141                 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*ito)];
142                 SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sub];
143                 appendChild(item);
144               }
145             model->endInsertRows();
146           }
147       }
148       break;
149     case YACS::HMI::UPDATE:
150       snode = dynamic_cast<SubjectNode*>(_subject);
151       assert(snode);
152       node = snode->getNode();
153       assert(node);
154       switch (node->getState())
155         {
156         case YACS::INVALID:
157           _itemForeground.replace(YLabel, QColor("red"));
158           model->setData(modelIndex(YLabel), 0);  // --- to emit dataChanged signal
159           break;
160         case YACS::READY:
161           _itemForeground.replace(YLabel, QColor("blue"));
162           model->setData(modelIndex(YLabel), 0);
163           break;
164         default:
165           break;
166         }
167       break;
168     case YACS::HMI::UPDATEPROGRESS:
169       setExecState(type);
170       model->setData(modelIndex(YState), 0);
171       break;
172     default:
173       //DEBTRACE("SchemaNodeItem::update(), event not handled: " << eventName(event));
174       SchemaItem::update(event, type, son);
175     }
176 }
177
178 void SchemaNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
179 {
180   ElementaryNodeMenu m;
181   m.popupMenu(caller, globalPos);
182 }
183
184 Qt::ItemFlags SchemaNodeItem::flags(const QModelIndex &index)
185 {
186   Qt::ItemFlags pflag = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled;
187
188   Qt::ItemFlags flagEdit = 0;
189   int column = index.column();
190   switch (column)
191     {
192 //     case 0:
193 //       flagEdit = Qt::ItemIsEditable; // --- port name editable in model view
194 //       break;
195     case YValue:
196       flagEdit = Qt::ItemIsEditable; // --- port value editable in model view
197       break;     
198     }
199
200   return pflag | flagEdit;
201 }
202
203 /*!
204  *  drag for nodes in tree are used for control link with Left Mouse Button
205  *  and for reparent with Middle Mouse Button
206  */
207 QString SchemaNodeItem::getMimeFormat()
208 {
209   if (QApplication::mouseButtons() == Qt::MidButton)
210     return "yacs/subjectNode";
211   else
212     return "yacs/subjectOutGate";
213 }
214
215 void SchemaNodeItem::toggleState()
216 {
217   DEBTRACE("SchemaNodeItem::toggleState");
218   SchemaItem::toggleState();
219   GuiExecutor *guiExec = QtGuiContext::getQtCurrent()->getGuiExecutor();
220   assert(guiExec);
221   SubjectNode *subjectNode = dynamic_cast<SubjectNode*>(getSubject());
222   assert(subjectNode);
223   string nodeName = QtGuiContext::getQtCurrent()->getProc()->getChildName(subjectNode->getNode());
224   DEBTRACE("nodeName=" << nodeName);
225
226   if (_itemCheckState.value(YLabel) == Qt::Checked) // already toggled
227     guiExec->addBreakpoint(nodeName);
228   else
229     guiExec->removeBreakpoint(nodeName);
230 }
231
232 /*!
233  *  drop in nodes are used for control link
234  */
235 bool SchemaNodeItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
236 {
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;
242
243   Subject *subFrom = myData->getSubject();
244   if (!subFrom) return false;
245   SubjectNode* from = dynamic_cast<SubjectNode*>(subFrom);
246
247   SubjectNode *to = dynamic_cast<SubjectNode*>(getSubject());
248   if (!to) return false;
249
250   bool ret =false;
251   if (from && to)
252     {
253        ret =true;
254        if (!SubjectNode::tryCreateLink(from, to))
255          Message mess;
256     }
257   return ret;
258 }
259
260 void SchemaNodeItem::setCaseValue()
261 {
262   Subject *sub = _parentItem->getSubject();
263   SubjectSwitch *sSwitch = dynamic_cast<SubjectSwitch*>(sub);
264   if (!sSwitch) return;
265
266   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
267   Switch *aSwitch = dynamic_cast<Switch*>(sSwitch->getNode());
268   assert(aSwitch);
269   SubjectNode *sNode = dynamic_cast<SubjectNode*>(_subject);
270   assert(sNode);
271   int rank = aSwitch->getRankOfNode(sNode->getNode());
272   if (rank == Switch::ID_FOR_DEFAULT_NODE)
273     _itemData.replace(YValue, "default");
274   else
275     _itemData.replace(YValue, rank);
276   model->setData(modelIndex(YValue), 0);
277 }
278
279 QVariant SchemaNodeItem::editionWhatsThis(int column) const
280 {
281   return "To edit the node properties, select the node and use the input panel.\n";
282 }