Salome HOME
updated copyright message
[modules/yacs.git] / src / genericgui / EditionSwitch.cxx
1 // Copyright (C) 2006-2023  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 "EditionSwitch.hxx"
21 #include "TableSwitch.hxx"
22 #include "SchemaModel.hxx"
23 #include "QtGuiContext.hxx"
24 #include "SchemaItem.hxx"
25 #include "ValueDelegate.hxx"
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 #include <cassert>
31 #include <sstream>
32
33 using namespace std;
34
35 using namespace YACS;
36 using namespace YACS::HMI;
37
38 EditionSwitch::EditionSwitch(Subject* subject,
39                              QWidget* parent,
40                              const char* name)
41   : EditionBloc(subject, parent, name)
42 {
43   _tvSwitch = new TableSwitch(this);
44   _wid->gridLayout1->addWidget(_tvSwitch);
45   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
46   _tvSwitch->tv_nodes->setModel(model);
47   _tvSwitch->sb_select->setMinimum(INT_MIN);
48   _tvSwitch->sb_select->setMaximum(INT_MAX);
49   connect(_tvSwitch->sb_select, SIGNAL(valueChanged(const QString &)),
50           this, SLOT(onModifySelect(const QString &)));
51
52   _valueDelegate = new ValueDelegate(parent);
53   _tvSwitch->tv_nodes->setItemDelegateForColumn(YValue, _valueDelegate);
54   _tvSwitch->setEditableCase(true);
55   if (!QtGuiContext::getQtCurrent()->isEdition())
56     _tvSwitch->setEnabled (false);
57
58   connect(_valueDelegate, SIGNAL(commitData(QWidget*)),
59           this, SLOT(onCommitData(QWidget*)));
60 }
61
62 EditionSwitch::~EditionSwitch()
63 {
64 }
65
66 void EditionSwitch::synchronize()
67 {
68   DEBTRACE("EditionSwitch::synchronize");
69   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
70   SchemaItem* schemaItem = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject];
71   if (schemaItem)
72     {
73       QModelIndex parentIndex = schemaItem->modelIndex();
74       _tvSwitch->tv_nodes->setRootIndex(parentIndex);
75       int numRows = model->rowCount(parentIndex);
76       for (int row = 0; row < numRows; ++row)
77         {
78           QModelIndex index = model->index(row, 0, parentIndex);
79           SchemaItem *childItem = static_cast<SchemaItem*>(index.internalPointer());
80           bool hidden = true;
81           if (Subject *sub = childItem->getSubject())
82             if (dynamic_cast<SubjectNode*>(sub))
83               hidden = false;
84           _tvSwitch->tv_nodes->setRowHidden(row, hidden);
85         }
86     }
87   _tvSwitch->adjustColumns();
88   _subject->update(SETSELECT, 0, _subject);
89 }
90
91 void EditionSwitch::onModifySelect(const QString &text)
92 {
93   DEBTRACE("EditionSwitch::onModifySelect " << text.toStdString());
94   SubjectSwitch *sswitch = dynamic_cast<SubjectSwitch*>(_subject);
95   YASSERT(sswitch);
96   sswitch->setSelect(text.toStdString());
97 }
98
99 void EditionSwitch::update(GuiEvent event, int type, Subject* son)
100 {
101   DEBTRACE("EditionSwitch::update " <<eventName(event) << " " << type);
102   switch (event)
103     {
104
105     case SETSELECT:
106       {
107         SubjectComposedNode *scn = dynamic_cast<SubjectComposedNode*>(_subject);
108         string val = scn->getValue();
109         istringstream ss(val);
110         DEBTRACE(val);
111         int i = 0;
112         ss >> i;
113         DEBTRACE(i);
114         _tvSwitch->sb_select->setValue(i);
115       }
116       break;
117     default:
118       EditionBloc::update(event, type, son);
119     }
120 }
121
122 void EditionSwitch::onCommitData(QWidget *editor)
123 {
124   DEBTRACE("EditionSwitch::onCommitData " << editor);
125   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
126   YASSERT(gedit);
127   QString val = gedit->GetStrValue();
128   DEBTRACE(val.toStdString());
129   Subject *sub = gedit->getSubject();
130   YASSERT(sub);
131   SubjectNode *snode = dynamic_cast<SubjectNode*>(sub);
132   YASSERT(snode);
133   sub = snode->getParent();
134   SubjectSwitch *sswitch = dynamic_cast<SubjectSwitch*>(sub);
135   YASSERT(sswitch);
136   bool isOk = sswitch->setCase(val.toStdString(), snode);
137   if (_valueDelegate)
138     _valueDelegate->setResultEditing(editor, isOk);
139
140 }