Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / genericgui / EditionElementaryNode.cxx
1 // Copyright (C) 2006-2015  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 "EditionElementaryNode.hxx"
21 #include "ValueDelegate.hxx"
22 #include "QtGuiContext.hxx"
23
24 #include "Proc.hxx"
25 #include "Port.hxx"
26 #include "DataPort.hxx"
27 #include "TypeCode.hxx"
28
29 #include "TablePortsEdition.hxx"
30
31 //#define _DEVDEBUG_
32 #include "YacsTrace.hxx"
33
34 #include <cassert>
35
36 using namespace std;
37
38 using namespace YACS;
39 using namespace YACS::HMI;
40
41 EditionElementaryNode::EditionElementaryNode(Subject* subject,
42                                              QWidget* parent,
43                                              const char* name)
44   : EditionNode(subject, parent, name)
45 {
46   _subElemNode = 0;
47   _twPorts = 0;
48   _tvInPorts = 0;
49   _tvOutPorts = 0;
50   _valueDelegate = 0;
51
52   _subElemNode = dynamic_cast<SubjectElementaryNode*>(_subject);
53   YASSERT(_subElemNode);
54   _valueDelegate = new ValueDelegate(parent);
55
56   connect(_valueDelegate, SIGNAL(commitData(QWidget*)),
57           this, SLOT(onCommitData(QWidget*)));
58
59 }
60
61 EditionElementaryNode::~EditionElementaryNode()
62 {
63 }
64
65 void EditionElementaryNode::onApply()
66 {
67   ItemEdition::onApply();
68 }
69
70 void EditionElementaryNode::onCancel()
71 {
72   ItemEdition::onCancel();
73 }
74
75 void EditionElementaryNode::onPortIndexChanged(int index)
76 {
77   DEBTRACE("EditionElementaryNode::onPortIndexChanged " << index);
78   synchronize();
79 }
80
81 void EditionElementaryNode::synchronize()
82 {
83   //DEBTRACE("EditionElementaryNode::synchronize");
84   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
85   SchemaItem* schemaItem = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject];
86   if (schemaItem)
87     {
88       QModelIndex parentIndex = schemaItem->modelIndex();
89       int numRows = model->rowCount(parentIndex);
90       if (_tvInPorts)
91         {
92           _tvInPorts->cb_insert->setCurrentIndex(0);
93           _tvInPorts->setNode(_subElemNode);
94           _tvInPorts->tv_ports->setRootIndex(parentIndex);
95           for (int row = 0; row < numRows; ++row)
96             {
97               QModelIndex index = model->index(row, 0, parentIndex);
98               SchemaItem *childItem = static_cast<SchemaItem*>(index.internalPointer());
99               bool hidden = true;
100               if (Subject *sub = childItem->getSubject())
101                 if (dynamic_cast<SubjectInputPort*>(sub)
102                     || dynamic_cast<SubjectInputDataStreamPort*>(sub))
103                   hidden = false;
104               _tvInPorts->tv_ports->setRowHidden(row, hidden);
105             }
106           _tvInPorts->adjustColumns();
107         }
108       if (_tvOutPorts)
109         {
110           _tvOutPorts->cb_insert->setCurrentIndex(0);
111           _tvOutPorts->setNode(_subElemNode);
112           _tvOutPorts->tv_ports->setRootIndex(parentIndex);
113           for (int row = 0; row < numRows; ++row)
114             {
115               QModelIndex index = model->index(row, 0, parentIndex);
116               SchemaItem *childItem = static_cast<SchemaItem*>(index.internalPointer());
117               bool hidden = true;
118               if (Subject *sub = childItem->getSubject())
119                 if (dynamic_cast<SubjectOutputPort*>(sub)
120                     || dynamic_cast<SubjectOutputDataStreamPort*>(sub))
121                   hidden = false;
122               _tvOutPorts->tv_ports->setRowHidden(row, hidden);
123             } 
124           _tvOutPorts->adjustColumns();
125         }
126     }
127 }
128
129 void EditionElementaryNode::setEditablePorts(bool isEditable)
130 {
131   if (_tvInPorts) _tvInPorts->setEditablePorts(isEditable);
132   if (_tvOutPorts) _tvOutPorts->setEditablePorts(isEditable);
133 }
134
135 bool EditionElementaryNode::hasInputPorts()
136 {
137   return true;
138 }
139
140 bool EditionElementaryNode::hasOutputPorts()
141 {
142   return true;
143 }
144
145 void EditionElementaryNode::createTablePorts(QLayout* layout)
146 {
147   _twPorts = new QTabWidget(this);
148   layout->addWidget(_twPorts);
149   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
150
151   QModelIndex schemIndex = model->index(0, 0, QModelIndex());
152   QModelIndex TypesDirIndex = model->index(0, 0, schemIndex);
153   
154   if (hasInputPorts())
155     {
156       _tvInPorts =new TablePortsEdition(true, _twPorts);
157       _tvInPorts->tv_ports->setModel(model);
158       _tvInPorts->tv_ports->setItemDelegateForColumn(YLabel, _valueDelegate); // --- port label
159       _tvInPorts->tv_ports->setItemDelegateForColumn(YValue, _valueDelegate); // --- port value
160       _tvInPorts->cb_insert->setModel(model);
161       _tvInPorts->cb_insert->setRootModelIndex(TypesDirIndex);
162       _twPorts->addTab(_tvInPorts, "input Ports");
163     }
164
165   if (hasOutputPorts())
166     {
167       _tvOutPorts =new TablePortsEdition(false, _twPorts);
168       _tvOutPorts->tv_ports->setModel(model);
169       _tvOutPorts->tv_ports->setItemDelegateForColumn(YLabel, _valueDelegate); // --- port label
170       _tvOutPorts->tv_ports->setItemDelegateForColumn(YValue, _valueDelegate); // --- port value
171       _tvOutPorts->cb_insert->setModel(model);
172       _tvOutPorts->cb_insert->setRootModelIndex(TypesDirIndex);
173       _twPorts->addTab(_tvOutPorts, "output Ports");
174     }
175
176   connect(_twPorts, SIGNAL(currentChanged(int)),
177           this, SLOT(onPortIndexChanged(int)));
178
179   setEditablePorts(false);
180 }
181
182 /*!
183  *  After edition with a specific editor created by ValueDelegate
184  *  for a cell of TablePortsEdition, the resulting string is tested
185  *  for setValue on the corresponding subject. Result of the setValue
186  *  (succes or failure) is transmitted to ValueDelegate for further
187  *  action in case of failure.
188  */
189 void EditionElementaryNode::onCommitData(QWidget *editor)
190 {
191   DEBTRACE("EditionElementaryNode::onCommitData " << editor);
192   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
193   YASSERT(gedit);
194   QString val = gedit->GetStrValue();
195   DEBTRACE(val.toStdString());
196   Subject *sub = gedit->getSubject();
197   YASSERT(sub);
198   SubjectDataPort *sdp = dynamic_cast<SubjectDataPort*>(sub);
199   YASSERT(sdp);
200   string strval = val.toStdString();
201   bool isOk = false;
202
203   if (gedit->getColumnInSubject() == YValue)
204     {
205       DEBTRACE(strval);
206        isOk = sdp->setValue(strval);
207
208       GuiExecutor* executor = QtGuiContext::getQtCurrent()->getGuiExecutor();
209       if (executor) executor->setInPortValue(sdp->getPort(), strval);
210     }
211
212   else // --- YLabel
213     {
214       isOk = sdp->setName(strval);
215     }
216
217   if (_valueDelegate)
218     _valueDelegate->setResultEditing(editor, isOk);
219  }