Salome HOME
Merge branch V7_3_1_BR
[modules/yacs.git] / src / genericgui / EditionSwitch.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 "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
56   connect(_valueDelegate, SIGNAL(commitData(QWidget*)),
57           this, SLOT(onCommitData(QWidget*)));
58 }
59
60 EditionSwitch::~EditionSwitch()
61 {
62 }
63
64 void EditionSwitch::synchronize()
65 {
66   DEBTRACE("EditionSwitch::synchronize");
67   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
68   SchemaItem* schemaItem = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject];
69   if (schemaItem)
70     {
71       QModelIndex parentIndex = schemaItem->modelIndex();
72       _tvSwitch->tv_nodes->setRootIndex(parentIndex);
73       int numRows = model->rowCount(parentIndex);
74       for (int row = 0; row < numRows; ++row)
75         {
76           QModelIndex index = model->index(row, 0, parentIndex);
77           SchemaItem *childItem = static_cast<SchemaItem*>(index.internalPointer());
78           bool hidden = true;
79           if (Subject *sub = childItem->getSubject())
80             if (dynamic_cast<SubjectNode*>(sub))
81               hidden = false;
82           _tvSwitch->tv_nodes->setRowHidden(row, hidden);
83         }
84     }
85   _tvSwitch->adjustColumns();
86   _subject->update(SETSELECT, 0, _subject);
87 }
88
89 void EditionSwitch::onModifySelect(const QString &text)
90 {
91   DEBTRACE("EditionSwitch::onModifySelect " << text.toStdString());
92   SubjectSwitch *sswitch = dynamic_cast<SubjectSwitch*>(_subject);
93   YASSERT(sswitch);
94   sswitch->setSelect(text.toStdString());
95 }
96
97 void EditionSwitch::update(GuiEvent event, int type, Subject* son)
98 {
99   DEBTRACE("EditionSwitch::update " <<eventName(event) << " " << type);
100   switch (event)
101     {
102
103     case SETSELECT:
104       {
105         SubjectComposedNode *scn = dynamic_cast<SubjectComposedNode*>(_subject);
106         string val = scn->getValue();
107         istringstream ss(val);
108         DEBTRACE(val);
109         int i = 0;
110         ss >> i;
111         DEBTRACE(i);
112         _tvSwitch->sb_select->setValue(i);
113       }
114       break;
115     default:
116       EditionBloc::update(event, type, son);
117     }
118 }
119
120 void EditionSwitch::onCommitData(QWidget *editor)
121 {
122   DEBTRACE("EditionSwitch::onCommitData " << editor);
123   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
124   YASSERT(gedit);
125   QString val = gedit->GetStrValue();
126   DEBTRACE(val.toStdString());
127   Subject *sub = gedit->getSubject();
128   YASSERT(sub);
129   SubjectNode *snode = dynamic_cast<SubjectNode*>(sub);
130   YASSERT(snode);
131   sub = snode->getParent();
132   SubjectSwitch *sswitch = dynamic_cast<SubjectSwitch*>(sub);
133   YASSERT(sswitch);
134   bool isOk = sswitch->setCase(val.toStdString(), snode);
135   if (_valueDelegate)
136     _valueDelegate->setResultEditing(editor, isOk);
137
138 }