1 // Copyright (C) 2006-2012 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
20 #include "ValueDelegate.hxx"
21 #include "guiObservers.hxx"
22 #include "SchemaItem.hxx"
23 #include "DataPort.hxx"
24 #include "StudyPorts.hxx"
25 #include "TypeCode.hxx"
27 #include "ItemEdition.hxx"
30 #include "YacsTrace.hxx"
38 using namespace YACS::HMI;
41 // -----------------------------------------------------------------------------
43 GenericEditor::GenericEditor()
51 GenericEditor::~GenericEditor()
53 DEBTRACE("GenericEditor::~GenericEditor " << this);
57 void GenericEditor::setSubject(Subject* subject)
62 void GenericEditor::setColumn(int column)
67 void GenericEditor::setDelegate(const ValueDelegate* delegate)
72 QString GenericEditor::GetStrValue()
77 Subject* GenericEditor::getSubject()
82 int GenericEditor::getColumnInSubject()
87 void GenericEditor::setData(QVariant val)
91 bool GenericEditor::firstSetData()
98 // -----------------------------------------------------------------------------
100 GeneralEditor::GeneralEditor(Subject* subject,
101 const ValueDelegate* delegate,
104 : QLineEdit(parent), GenericEditor()
106 DEBTRACE("GeneralEditor::GeneralEditor");
107 setDelegate(delegate);
112 GeneralEditor::~GeneralEditor()
116 QString GeneralEditor::GetStrValue()
118 DEBTRACE("GeneralEditor::GetStrValue " << text().toStdString());
122 void GeneralEditor::setData(QVariant val)
124 DEBTRACE("GeneralEditor::setData " << this);
125 DEBTRACE(val.canConvert<QString>());
126 DEBTRACE(val.toString().toStdString());
127 setText(val.toString());
130 // -----------------------------------------------------------------------------
132 NameEditor::NameEditor(Subject* subject,
133 const ValueDelegate* delegate,
136 : QLineEdit(parent), GenericEditor()
138 DEBTRACE("NameEditor::NameEditor");
139 setDelegate(delegate);
144 NameEditor::~NameEditor()
148 QString NameEditor::GetStrValue()
150 DEBTRACE("Name::GetStrValue " << text().toStdString());
151 string filtered = ItemEdition::filterName(text().toStdString());
152 return filtered.c_str();
155 void NameEditor::setData(QVariant val)
157 DEBTRACE("NameEditor::setData " << this);
158 DEBTRACE(val.canConvert<QString>());
159 DEBTRACE(val.toString().toStdString());
160 string filtered = ItemEdition::filterName(val.toString().toStdString());
161 setText(filtered.c_str());
164 // -----------------------------------------------------------------------------
166 IntEditor::IntEditor(Subject* subject,
167 const ValueDelegate* delegate,
170 : QSpinBox(parent), GenericEditor()
175 setDelegate(delegate);
179 IntEditor::~IntEditor()
183 QString IntEditor::GetStrValue()
190 void IntEditor::setData(QVariant val)
192 DEBTRACE("IntEditor::setData");
193 DEBTRACE(val.canConvert<int>());
194 setValue(val.toInt());
197 // -----------------------------------------------------------------------------
199 CaseSwitchEditor::CaseSwitchEditor(Subject* subject,
200 const ValueDelegate* delegate,
203 : CaseSwitch(parent), GenericEditor()
205 sb_case->setMinimum(INT_MIN);
206 sb_case->setMaximum(INT_MAX);
208 setDelegate(delegate);
212 CaseSwitchEditor::~CaseSwitchEditor()
216 QString CaseSwitchEditor::GetStrValue()
219 int val = sb_case->value();
221 val = YACS::ENGINE::Switch::ID_FOR_DEFAULT_NODE;
227 void CaseSwitchEditor::setData(QVariant val)
229 DEBTRACE("CaseSwitchEditor::setData");
230 if (val == "default")
232 setDefaultChecked(true);
236 setDefaultChecked(false);
237 DEBTRACE(val.canConvert<int>() << " " << val.toInt());
238 sb_case->setValue(val.toInt());
243 // -----------------------------------------------------------------------------
245 ValueDelegate::ValueDelegate(QObject *parent)
246 : QItemDelegate(parent)
248 DEBTRACE("ValueDelegate::ValueDelegate");
252 ValueDelegate::~ValueDelegate()
254 DEBTRACE("ValueDelegate::~ValueDelegate");
257 QWidget *ValueDelegate::createEditor(QWidget *parent,
258 const QStyleOptionViewItem &option,
259 const QModelIndex &index) const
261 DEBTRACE("ValueDelegate::createEditor");
263 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
264 Subject *subject = item->getSubject();
265 int column = index.column();
266 DEBTRACE(subject->getName() << " " << column);
269 SubjectDataPort *sport = 0;
270 SubjectNode *snode = 0;
272 if (column == YValue)
274 sport = dynamic_cast<SubjectDataPort*>(subject);
276 snode= dynamic_cast<SubjectNode*>(subject);
280 YACS::ENGINE::DataPort *port = sport->getPort();
281 YACS::ENGINE::InputStudyPort* istport=dynamic_cast<YACS::ENGINE::InputStudyPort*>(port);
282 YACS::ENGINE::OutputStudyPort* ostport=dynamic_cast<YACS::ENGINE::OutputStudyPort*>(port);
283 YACS::ENGINE::TypeCode *tc = port->edGetType();
284 YACS::ENGINE::DynType dt = tc->kind();
285 if (!istport && !ostport && dt == YACS::ENGINE::Int)
286 editor = new IntEditor(subject, this, column, parent);
290 SubjectSwitch *sSwitch = dynamic_cast<SubjectSwitch*>(snode->getParent());
292 editor = new CaseSwitchEditor(subject, this, column, parent);
296 if (column == YLabel)
298 sport = dynamic_cast<SubjectDataPort*>(subject);
300 editor = new NameEditor(subject, this, column, parent);
303 if (!editor) editor = new GeneralEditor(subject, this, column, parent);
308 * Initialise the editor with data from model, unless there is a
309 * previous error in edition of this item, and it is the first
310 * initialisation of the editor: in case of error, a new editor
311 * must be initialised with the wrong edited string, for correction.
313 void ValueDelegate::setEditorData(QWidget *editor,
314 const QModelIndex &index) const
316 DEBTRACE("ValueDelegate::setEditorData");
317 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
319 QString edited = gedit->GetStrValue();
320 DEBTRACE(edited.toStdString());
321 Subject *sub = gedit->getSubject();
322 if (! gedit->firstSetData()) // --- editor already initialized
324 else if (_errorMap.count(sub)) // --- initialize with previous error
326 string error = _errorMap[sub];
327 gedit->setData(QString(error.c_str()));
328 _errorMap.erase(gedit->getSubject());
329 DEBTRACE("--------erase-----------");
331 else // --- initialize with model
332 gedit->setData(index.model()->data(index, Qt::DisplayRole));
336 * Nothing done here: model->setData is done by UPDATE.
338 void ValueDelegate::setModelData(QWidget *editor,
339 QAbstractItemModel *model,
340 const QModelIndex &index) const
342 DEBTRACE("ValueDelegate::setModelData");
343 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
345 QString value = gedit->GetStrValue();
346 DEBTRACE(value.toStdString());
347 //model->setData(index, value, Qt::EditRole); // real set done by update
350 void ValueDelegate::updateEditorGeometry(QWidget *editor,
351 const QStyleOptionViewItem &option,
352 const QModelIndex &index) const
354 DEBTRACE("ValueDelegate::updateEditorGeometry");
355 editor->setGeometry(option.rect);
359 * If edition is in error, keep the wrong string value in a map,
360 * to initialise a next instance of editor with the wrong string,
361 * in order to help correction of the edition.
363 void ValueDelegate::setResultEditing(QWidget *editor, bool isOk)
365 DEBTRACE("ValueDelegate::setResultEditing " << isOk);
366 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
368 Subject *sub = gedit->getSubject();
369 string val = gedit->GetStrValue().toStdString();
370 DEBTRACE(sub->getName() << " " << val);
373 sub->update(EDIT, 1, sub);
374 _errorMap[sub] = val;
377 sub->update(EDIT, 0, sub);