Salome HOME
2fde2c311823bff5044e6e8a6c0b995411360e5d
[modules/yacs.git] / src / genericgui / SchemaInPortItem.cxx
1 // Copyright (C) 2006-2014  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, or (at your option) any later version.
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
20 #include "SchemaInPortItem.hxx"
21 #include "ItemMimeData.hxx"
22 #include "QtGuiContext.hxx"
23 #include "Menus.hxx"
24 #include "Message.hxx"
25
26 #include "commandsProc.hxx"
27 #include "DataPort.hxx"
28 #include "TypeCode.hxx"
29 #include "InputPort.hxx"
30 #include "OutputPort.hxx"
31 #include "DataNode.hxx"
32 #include "InlineNode.hxx"
33
34 #include <QIcon>
35
36 //#define _DEVDEBUG_
37 #include "YacsTrace.hxx"
38
39 using namespace std;
40 using namespace YACS::ENGINE;
41 using namespace YACS::HMI;
42
43 /*! column 1 = name, set in SchemaItem,
44  *  column 2 = data type, never editable,
45  *  column 3 = value
46  */
47 SchemaInPortItem::SchemaInPortItem(SchemaItem *parent, QString label, Subject* subject)
48   : SchemaItem(parent, label, subject)
49 {
50   SubjectDataPort *subPort = dynamic_cast<SubjectDataPort*>(subject);
51   _isDataStream = false;
52   if (subPort)
53     {
54       DataPort *dport = subPort->getPort();
55       TypeOfElem typort = ProcInvoc::getTypeOfPort(dport);
56       TypeOfElem typnode = ProcInvoc::getTypeOfNode(dport->getNode());
57 //       _itemData.replace(YType, dport->edGetType()->getKindRepr());
58       _itemData.replace(YType, dport->edGetType()->name());
59       _itemForeground.replace(YType, QColor("black"));
60       InputPort *inport = 0;
61
62       if(typnode==YACS::HMI::STUDYOUTNODE)
63         {
64           //It's a study out node
65           if(typort==YACS::HMI::INPUTPORT)
66             {
67               _itemDeco.replace(YLabel, QIcon("icons:in_port.png"));
68               inport = dynamic_cast<InputPort*>(dport);
69               std::string val=inport->getAsString();
70               if(val != "")
71                 {
72                   _itemData.replace(YValue, val.c_str());
73                   if (inport->edGetNumberOfLinks())
74                     _itemForeground.replace(YValue, QColor("green"));
75                   else
76                     _itemForeground.replace(YValue, QColor("red"));
77                 }
78               else
79                 {
80                   _itemData.replace(YValue, "not initialized");
81                   _itemForeground.replace(YValue, QColor("red"));
82                 }
83             }
84           return;
85         }
86
87       switch (typort)
88         {
89         case YACS::HMI::INPUTPORT:
90           _isDataStream = false;
91           _itemDeco.replace(YLabel, QIcon("icons:in_port.png"));
92           inport = dynamic_cast<InputPort*>(dport);
93           if (inport->edIsManuallyInitialized())
94             {
95               _itemData.replace(YValue, inport->getAsString().c_str());
96               _itemForeground.replace(YValue, QColor("green"));
97             }
98           else if (inport->edGetNumberOfLinks())
99             {
100               _itemData.replace(YValue, "linked");
101               _itemForeground.replace(YValue, QColor("blue"));
102             }
103           else
104             {
105               _itemData.replace(YValue, "not initialized");
106               _itemForeground.replace(YValue, QColor("red"));
107             }
108           break;
109         case YACS::HMI::INPUTDATASTREAMPORT:
110           _isDataStream = true;
111           _itemDeco.replace(YLabel, QIcon("icons:in_port.png"));
112           _itemData.replace(YValue, "stream");
113           _itemForeground.replace(YValue, QColor("grey"));
114           break;
115         }
116     }
117 }
118
119 void SchemaInPortItem::update(GuiEvent event, int type, Subject* son)
120 {
121   DEBTRACE("SchemaInPortItem::update");
122   SchemaItem::update(event, type, son);
123   QModelIndex index = QModelIndex();
124   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
125   switch (event)
126     {
127     case SETVALUE:
128       {
129         SubjectInputPort *sip = dynamic_cast<SubjectInputPort*>(son);
130         if (sip)
131           {
132             InputPort* port = dynamic_cast<InputPort*>(sip->getPort());
133             TypeOfElem typnode = ProcInvoc::getTypeOfNode(port->getNode());
134             DEBTRACE(port->getAsString());
135             if(typnode==YACS::HMI::STUDYOUTNODE)
136               {
137                 if(port->getAsString().empty())
138                   {
139                     _itemData.replace(YValue, "not initialized");
140                     if (port->edGetNumberOfLinks())
141                       _itemForeground.replace(YValue, QColor("blue"));
142                     else
143                       _itemForeground.replace(YValue, QColor("red"));
144                   }
145                 else
146                   {
147                     _itemData.replace(YValue, port->getAsString().c_str());
148                     if (port->edGetNumberOfLinks())
149                       _itemForeground.replace(YValue, QColor("green"));
150                     else
151                       _itemForeground.replace(YValue, QColor("red"));
152                   }
153               }
154             else
155               {
156                 _itemData.replace(YValue, port->getAsString().c_str());
157                 _itemForeground.replace(YValue, QColor("green"));
158               }
159             model->setData(modelIndex(YValue), 0); // --- to emit dataChanged signal
160           }
161       }
162       break;
163     case UPDATE:
164       {
165         SubjectInputPort *sip = dynamic_cast<SubjectInputPort*>(_subject);
166         if (sip)
167           {
168             InputPort* port = dynamic_cast<InputPort*>(sip->getPort());
169             TypeOfElem typnode = ProcInvoc::getTypeOfNode(port->getNode());
170             TypeOfElem typort = ProcInvoc::getTypeOfPort(port);
171             _itemData.replace(YType, port->edGetType()->name());
172             _itemForeground.replace(YType, QColor("black"));
173             if(typnode==YACS::HMI::STUDYOUTNODE)
174               {
175                 if(port->getAsString().empty())
176                   {
177                     _itemData.replace(YValue, "not initialized");
178                     if (port->edGetNumberOfLinks())
179                       _itemForeground.replace(YValue, QColor("blue"));
180                     else
181                       _itemForeground.replace(YValue, QColor("red"));
182                   }
183                 else
184                   {
185                     _itemData.replace(YValue, port->getAsString().c_str());
186                     if (port->edGetNumberOfLinks())
187                       _itemForeground.replace(YValue, QColor("green"));
188                     else
189                       _itemForeground.replace(YValue, QColor("red"));
190                   }
191               }
192             else
193               {
194                 if (port->edGetNumberOfLinks())
195                   {
196                     _itemData.replace(YValue, "linked");
197                     _itemForeground.replace(YValue, QColor("blue"));
198                   }
199                 else if (port->edIsManuallyInitialized())
200                   {
201                     _itemData.replace(YValue, port->getAsString().c_str());
202                     _itemForeground.replace(YValue, QColor("green"));
203                   }
204                 else
205                   {
206                     _itemData.replace(YValue, "not initialized");
207                     _itemForeground.replace(YValue, QColor("red"));
208                   }
209               }
210             model->setData(modelIndex(YValue), 0); // --- to emit dataChanged signal
211           }
212       }
213       break;
214     case UPDATEPROGRESS:
215       {
216         SubjectInputPort *sip = dynamic_cast<SubjectInputPort*>(son);
217         if (sip)
218           {
219             _itemData.replace(YValue, sip->getExecValue().c_str());
220             _itemForeground.replace(YValue, QColor("darkCyan"));
221             model->setData(modelIndex(YValue), 0); // --- to emit dataChanged signal
222           }
223       }
224       break;      
225     }
226 }
227
228 Qt::ItemFlags SchemaInPortItem::flags(const QModelIndex &index)
229 {
230   //DEBTRACE("SchemaInPortItem::flags");
231   Qt::ItemFlags pflag = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
232
233   Qt::ItemFlags flagEdit = 0;
234   int column = index.column();
235   switch (column)
236     {
237     case 0:
238       {
239         SubjectDataPort *sdp = dynamic_cast<SubjectDataPort*>(_subject);
240         Node *parent = sdp->getPort()->getNode();
241         if (parent)
242           if ( (dynamic_cast<DataNode*>(parent) || dynamic_cast<InlineNode*>(parent)) &&
243                QtGuiContext::getQtCurrent()->isEdition() )
244             flagEdit = Qt::ItemIsEditable; // --- port name editable
245       }
246       break;
247     case 2:
248       if (!_isDataStream)
249         flagEdit = Qt::ItemIsEditable; // --- port value editable      
250     }
251   return pflag | flagEdit;
252 }
253
254 bool SchemaInPortItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
255 {
256   DEBTRACE("SchemaInPortItem::dropMimeData");
257   if (!data) return false;
258   const ItemMimeData* myData = dynamic_cast<const ItemMimeData*>(data);
259   if (!myData) return false;
260   if(!myData->hasFormat("yacs/subjectOutPort")) return false;
261   SubjectDataPort *to = dynamic_cast<SubjectDataPort*>(getSubject());
262   if (!to) return false;
263   InPort *toport = dynamic_cast<InPort*>(to->getPort());
264   if (!toport) return false;
265
266   bool ret =false;
267   TypeOfElem typort = ProcInvoc::getTypeOfPort(toport);
268   switch (typort)
269     {
270     case YACS::HMI::INPUTPORT:
271     case YACS::HMI::INPUTDATASTREAMPORT:
272       {
273         ret =true;
274         Subject *sub = myData->getSubject();
275         if (!sub) break;
276         SubjectDataPort* from = dynamic_cast<SubjectDataPort*>(sub);
277         if (from && to)
278           if (!SubjectDataPort::tryCreateLink(from, to,myData->getControl()))
279             Message mess;
280         break;
281       }
282     default:
283       break;
284     }
285   return ret;
286 }
287
288 void SchemaInPortItem::popupMenu(QWidget *caller, const QPoint &globalPos)
289 {
290   InPortMenu m;
291   m.popupMenu(caller, globalPos);
292 }
293
294 QVariant SchemaInPortItem::editionWhatsThis(int column) const
295 {
296   QString answer;
297   if (_isDataStream)
298     answer = "Edition, DataStream input port ";
299   else
300     answer = "Edition, DataFlow input port ";
301   switch (column)
302     {
303     case YLabel:
304       answer += "name=";
305       answer += _itemData.value(column).toString();
306       answer += "\nInput port is edited in the input panel of the node.\n" \
307         "Select the node to get access to it's input panel.";
308       break;
309     case YType:
310       answer += "type=";
311       answer += _itemData.value(column).toString();
312       answer += "\nInput port is edited in the input panel of the node.\n" \
313         "Select the node to get access to it's input panel.\n" \
314         "Type is not modifiable, you must delete and recreate the port. Existing links will be lost.";
315       break;
316     case YValue:
317       answer += "value=";
318       answer += _itemData.value(column).toString();
319       answer += "\nInput port is edited in the input panel of the node.\n" \
320         "Select the node to get access to it's input panel.\n";
321       if (_isDataStream)
322         answer += "DataStream ports have no editable value, they must be linked to an output DataStream port.";
323       else
324         {
325           answer += "DataFlow ports must be either manually initialized or linked to an output DataFlow port. "; 
326           answer += "If the port is manually initialized and linked, the value from the link will prevail ";
327           answer += "if it is available when the node is executed.";
328         }
329       break;
330     }
331   return answer;
332 }
333
334