Salome HOME
Revert "Synchronize adm files"
[modules/yacs.git] / src / genericgui / ValueDelegate.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 "ValueDelegate.hxx"
21 #include "guiObservers.hxx"
22 #include "SchemaItem.hxx"
23 #include "DataPort.hxx"
24 #include "StudyPorts.hxx"
25 #include "TypeCode.hxx"
26 #include "Switch.hxx"
27 #include "ItemEdition.hxx"
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 #include <QSize>
33 #include <QSpinBox>
34 #include <cassert>
35 #include <climits>
36
37 using namespace std;
38 using namespace YACS::HMI;
39
40
41 // -----------------------------------------------------------------------------
42
43 GenericEditor::GenericEditor()
44 {
45   _subject = 0;
46   _column = 0;
47   _delegate = 0;
48   _first = true;
49 }
50
51 GenericEditor::~GenericEditor()
52 {
53   DEBTRACE("GenericEditor::~GenericEditor " << this);
54   
55 }
56
57 void GenericEditor::setSubject(Subject* subject)
58 {
59   _subject = subject;
60 }
61
62 void GenericEditor::setColumn(int column)
63 {
64   _column= column;
65 }
66
67 void GenericEditor::setDelegate(const ValueDelegate* delegate)
68 {
69   _delegate = delegate;
70 }
71
72 QString GenericEditor::GetStrValue()
73 {
74   return "";
75 }
76
77 Subject* GenericEditor::getSubject()
78 {
79   return _subject;
80 }
81
82 int GenericEditor::getColumnInSubject()
83 {
84   return _column;
85 }
86
87 void GenericEditor::setData(QVariant val)
88 {
89 }
90
91 bool GenericEditor::firstSetData()
92 {
93   bool ret = _first;
94   _first = false;
95   return ret; 
96 }
97
98 // -----------------------------------------------------------------------------
99
100 GeneralEditor::GeneralEditor(Subject* subject,
101                              const ValueDelegate* delegate,
102                              int column,
103                              QWidget * parent)
104   : QLineEdit(parent), GenericEditor()
105 {
106   DEBTRACE("GeneralEditor::GeneralEditor");
107   setDelegate(delegate);
108   setSubject(subject);
109   setColumn(column);
110 }
111
112 GeneralEditor::~GeneralEditor()
113 {
114 }
115
116 QString GeneralEditor::GetStrValue()
117 {
118   DEBTRACE("GeneralEditor::GetStrValue " << text().toStdString());
119   return text();
120 }
121
122 void GeneralEditor::setData(QVariant val)
123 {
124   DEBTRACE("GeneralEditor::setData " << this);
125   DEBTRACE(val.canConvert<QString>());
126   DEBTRACE(val.toString().toStdString());
127   setText(val.toString());
128 }
129
130 // -----------------------------------------------------------------------------
131
132 NameEditor::NameEditor(Subject* subject,
133                        const ValueDelegate* delegate,
134                        int column,
135                        QWidget * parent)
136   : QLineEdit(parent), GenericEditor()
137 {
138   DEBTRACE("NameEditor::NameEditor");
139   setDelegate(delegate);
140   setSubject(subject);
141   setColumn(column);
142 }
143
144 NameEditor::~NameEditor()
145 {
146 }
147
148 QString NameEditor::GetStrValue()
149 {
150   DEBTRACE("Name::GetStrValue " << text().toStdString());
151   string filtered = ItemEdition::filterName(text().toStdString());
152   return filtered.c_str();
153 }
154
155 void NameEditor::setData(QVariant val)
156 {
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());
162 }
163
164 // -----------------------------------------------------------------------------
165
166 IntEditor::IntEditor(Subject* subject,
167                      const ValueDelegate* delegate,
168                      int column,
169                      QWidget * parent)
170   : QSpinBox(parent), GenericEditor()
171 {
172   setMinimum(INT_MIN);
173   setMaximum(INT_MAX);
174   setSubject(subject);
175   setDelegate(delegate);
176   setColumn(column);
177 }
178
179 IntEditor::~IntEditor()
180 {
181 }
182
183 QString IntEditor::GetStrValue()
184 {
185   QString str;
186   str.setNum(value());
187   return str;
188 }
189
190 void IntEditor::setData(QVariant val)
191 {
192   DEBTRACE("IntEditor::setData");
193   DEBTRACE(val.canConvert<int>());
194   setValue(val.toInt());
195 }
196
197 // -----------------------------------------------------------------------------
198
199 CaseSwitchEditor::CaseSwitchEditor(Subject* subject,
200                        const ValueDelegate* delegate,
201                        int column,
202                        QWidget* parent)
203   : CaseSwitch(parent), GenericEditor()
204 {
205   sb_case->setMinimum(INT_MIN);
206   sb_case->setMaximum(INT_MAX);
207   setSubject(subject);
208   setDelegate(delegate);
209   setColumn(column);
210 }
211
212 CaseSwitchEditor::~CaseSwitchEditor()
213 {
214 }
215
216 QString CaseSwitchEditor::GetStrValue()
217 {
218   QString str;
219   int val = sb_case->value();
220   if (_isDefault)
221     val = YACS::ENGINE::Switch::ID_FOR_DEFAULT_NODE;
222   str.setNum(val);
223   DEBTRACE(val);
224   return str;
225 }
226
227 void CaseSwitchEditor::setData(QVariant val)
228 {
229   DEBTRACE("CaseSwitchEditor::setData");
230   if (val == "default")
231     {
232       setDefaultChecked(true);
233     }
234   else
235     {
236       setDefaultChecked(false);
237       DEBTRACE(val.canConvert<int>() << " " << val.toInt());
238       sb_case->setValue(val.toInt());
239     }
240 }
241
242
243 // -----------------------------------------------------------------------------
244
245 ValueDelegate::ValueDelegate(QObject *parent)
246   : QItemDelegate(parent)
247 {
248   DEBTRACE("ValueDelegate::ValueDelegate");
249   _errorMap.clear();
250 }
251
252 ValueDelegate::~ValueDelegate()
253 {
254   DEBTRACE("ValueDelegate::~ValueDelegate");
255 }
256       
257 QWidget *ValueDelegate::createEditor(QWidget *parent,
258                                      const QStyleOptionViewItem &option,
259                                      const QModelIndex &index) const
260 {
261   DEBTRACE("ValueDelegate::createEditor");
262
263   SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
264   Subject *subject = item->getSubject();
265   int column = index.column();
266   DEBTRACE(subject->getName() << " " << column);
267   
268   QWidget *editor = 0;
269   SubjectDataPort *sport = 0;
270   SubjectNode *snode = 0;
271
272   if (column == YValue)
273     {
274       sport = dynamic_cast<SubjectDataPort*>(subject);
275       if (!sport)
276         snode= dynamic_cast<SubjectNode*>(subject);
277       
278       if (sport)
279         {
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);
287         }
288       else if (snode)
289         {
290           SubjectSwitch *sSwitch = dynamic_cast<SubjectSwitch*>(snode->getParent());
291           if (sSwitch)
292             editor = new CaseSwitchEditor(subject, this, column, parent);
293         }
294     }
295
296   if (column == YLabel)
297     {
298       sport = dynamic_cast<SubjectDataPort*>(subject);      
299       if (sport)
300         editor = new NameEditor(subject, this, column, parent);
301     }
302
303   if (!editor) editor = new GeneralEditor(subject, this, column, parent);
304   return editor;
305 }
306
307 /*!
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.
312  */     
313 void ValueDelegate::setEditorData(QWidget *editor,
314                                   const QModelIndex &index) const
315 {
316   DEBTRACE("ValueDelegate::setEditorData");
317   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
318   YASSERT(gedit);
319   QString edited = gedit->GetStrValue();
320   DEBTRACE(edited.toStdString());
321   Subject *sub = gedit->getSubject();
322   if (! gedit->firstSetData())    // --- editor already initialized
323     return;
324   else if (_errorMap.count(sub))  // --- initialize with previous error
325     {
326       string error = _errorMap[sub];
327       gedit->setData(QString(error.c_str()));
328       _errorMap.erase(gedit->getSubject());
329       DEBTRACE("--------erase-----------");
330     }
331   else                            // --- initialize with model
332     gedit->setData(index.model()->data(index, Qt::DisplayRole));
333 }
334
335 /*!
336  * Nothing done here: model->setData is done by UPDATE.
337  */
338 void ValueDelegate::setModelData(QWidget *editor,
339                                  QAbstractItemModel *model,
340                                  const QModelIndex &index) const
341 {
342   DEBTRACE("ValueDelegate::setModelData");
343   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
344   YASSERT(gedit);
345   QString value = gedit->GetStrValue();
346   DEBTRACE(value.toStdString());
347   //model->setData(index, value, Qt::EditRole); // real set done by update
348 }
349       
350 void ValueDelegate::updateEditorGeometry(QWidget *editor,
351                                          const QStyleOptionViewItem &option,
352                                          const QModelIndex &index) const
353 {
354   DEBTRACE("ValueDelegate::updateEditorGeometry");
355   editor->setGeometry(option.rect);
356 }
357
358 /*!
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.
362  */
363 void ValueDelegate::setResultEditing(QWidget *editor, bool isOk)
364 {
365   DEBTRACE("ValueDelegate::setResultEditing " << isOk);
366   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
367   YASSERT(gedit);
368   Subject *sub = gedit->getSubject();
369   string val = gedit->GetStrValue().toStdString();
370   DEBTRACE(sub->getName() << " " << val);
371   if (!isOk)
372     {
373       sub->update(EDIT, 1, sub);
374       _errorMap[sub] = val;
375     }
376   else
377       sub->update(EDIT, 0, sub);
378 }