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