1 // Copyright (C) 2006-2008 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
19 #include "ValueDelegate.hxx"
20 #include "guiObservers.hxx"
21 #include "SchemaItem.hxx"
22 #include "DataPort.hxx"
23 #include "TypeCode.hxx"
27 #include "YacsTrace.hxx"
35 using namespace YACS::HMI;
38 // -----------------------------------------------------------------------------
40 GenericEditor::GenericEditor()
48 GenericEditor::~GenericEditor()
50 DEBTRACE("GenericEditor::~GenericEditor " << this);
54 void GenericEditor::setSubject(Subject* subject)
59 void GenericEditor::setColumn(int column)
64 void GenericEditor::setDelegate(const ValueDelegate* delegate)
69 QString GenericEditor::GetStrValue()
73 Subject* GenericEditor::getSubject()
78 int GenericEditor::getColumnInSubject()
83 void GenericEditor::setData(QVariant val)
87 bool GenericEditor::firstSetData()
94 // -----------------------------------------------------------------------------
96 GeneralEditor::GeneralEditor(Subject* subject,
97 const ValueDelegate* delegate,
100 : QLineEdit(parent), GenericEditor()
102 DEBTRACE("GeneralEditor::GeneralEditor");
103 setDelegate(delegate);
108 GeneralEditor::~GeneralEditor()
112 QString GeneralEditor::GetStrValue()
114 DEBTRACE("GeneralEditor::GetStrValue " << text().toStdString());
118 void GeneralEditor::setData(QVariant val)
120 DEBTRACE("GeneralEditor::setData " << this);
121 DEBTRACE(val.canConvert<QString>());
122 DEBTRACE(val.toString().toStdString());
123 setText(val.toString());
126 // -----------------------------------------------------------------------------
128 IntEditor::IntEditor(Subject* subject,
129 const ValueDelegate* delegate,
132 : QSpinBox(parent), GenericEditor()
137 setDelegate(delegate);
141 IntEditor::~IntEditor()
145 QString IntEditor::GetStrValue()
152 void IntEditor::setData(QVariant val)
154 DEBTRACE("IntEditor::setData");
155 DEBTRACE(val.canConvert<int>());
156 setValue(val.toInt());
159 // -----------------------------------------------------------------------------
161 CaseSwitchEditor::CaseSwitchEditor(Subject* subject,
162 const ValueDelegate* delegate,
165 : CaseSwitch(parent), GenericEditor()
167 sb_case->setMinimum(INT_MIN);
168 sb_case->setMaximum(INT_MAX);
170 setDelegate(delegate);
174 CaseSwitchEditor::~CaseSwitchEditor()
178 QString CaseSwitchEditor::GetStrValue()
181 int val = sb_case->value();
183 val = YACS::ENGINE::Switch::ID_FOR_DEFAULT_NODE;
189 void CaseSwitchEditor::setData(QVariant val)
191 DEBTRACE("CaseSwitchEditor::setData");
192 if (val == "default")
194 setDefaultChecked(true);
198 setDefaultChecked(false);
199 DEBTRACE(val.canConvert<int>() << " " << val.toInt());
200 sb_case->setValue(val.toInt());
205 // -----------------------------------------------------------------------------
207 ValueDelegate::ValueDelegate(QObject *parent)
208 : QItemDelegate(parent)
210 DEBTRACE("ValueDelegate::ValueDelegate");
214 ValueDelegate::~ValueDelegate()
216 DEBTRACE("ValueDelegate::~ValueDelegate");
219 QWidget *ValueDelegate::createEditor(QWidget *parent,
220 const QStyleOptionViewItem &option,
221 const QModelIndex &index) const
223 DEBTRACE("ValueDelegate::createEditor");
225 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
226 Subject *subject = item->getSubject();
227 int column = index.column();
228 DEBTRACE(subject->getName() << " " << column);
231 SubjectDataPort *sport = 0;
232 SubjectNode *snode = 0;
234 if (column == YValue)
236 sport = dynamic_cast<SubjectDataPort*>(subject);
238 snode= dynamic_cast<SubjectNode*>(subject);
242 YACS::ENGINE::DataPort *port = sport->getPort();
243 YACS::ENGINE::TypeCode *tc = port->edGetType();
244 YACS::ENGINE::DynType dt = tc->kind();
245 if (dt == YACS::ENGINE::Int)
246 editor = new IntEditor(subject, this, column, parent);
250 SubjectSwitch *sSwitch = dynamic_cast<SubjectSwitch*>(snode->getParent());
252 editor = new CaseSwitchEditor(subject, this, column, parent);
256 if (!editor) editor = new GeneralEditor(subject, this, column, parent);
261 * Initialise the editor with data from model, unless there is a
262 * previous error in edition of this item, and it is the first
263 * initialisation of the editor: in case of error, a new editor
264 * must be initialised with the wrong edited string, for correction.
266 void ValueDelegate::setEditorData(QWidget *editor,
267 const QModelIndex &index) const
269 DEBTRACE("ValueDelegate::setEditorData");
270 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
272 QString edited = gedit->GetStrValue();
273 DEBTRACE(edited.toStdString());
274 Subject *sub = gedit->getSubject();
275 if (! gedit->firstSetData()) // --- editor already initialized
277 else if (_errorMap.count(sub)) // --- initialize with previous error
279 string error = _errorMap[sub];
280 gedit->setData(QString(error.c_str()));
281 _errorMap.erase(gedit->getSubject());
282 DEBTRACE("--------erase-----------");
284 else // --- initialize with model
285 gedit->setData(index.model()->data(index, Qt::DisplayRole));
289 * Nothing done here: model->setData is done by UPDATE.
291 void ValueDelegate::setModelData(QWidget *editor,
292 QAbstractItemModel *model,
293 const QModelIndex &index) const
295 DEBTRACE("ValueDelegate::setModelData");
296 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
298 QString value = gedit->GetStrValue();
299 DEBTRACE(value.toStdString());
300 //model->setData(index, value, Qt::EditRole); // real set done by update
303 void ValueDelegate::updateEditorGeometry(QWidget *editor,
304 const QStyleOptionViewItem &option,
305 const QModelIndex &index) const
307 DEBTRACE("ValueDelegate::updateEditorGeometry");
308 editor->setGeometry(option.rect);
312 * If edition is in error, keep the wrong string value in a map,
313 * to initialise a next instance of editor with the wrong string,
314 * in order to help correction of the edition.
316 void ValueDelegate::setResultEditing(QWidget *editor, bool isOk)
318 DEBTRACE("ValueDelegate::setResultEditing " << isOk);
319 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
321 Subject *sub = gedit->getSubject();
322 string val = gedit->GetStrValue().toStdString();
323 DEBTRACE(sub->getName() << " " << val);
326 sub->update(EDIT, 1, sub);
327 _errorMap[sub] = val;
330 sub->update(EDIT, 0, sub);