Salome HOME
5e995f9af01361fe09d74e3aaf64c29c97cd0a96
[modules/yacs.git] / src / genericgui / FormContainerDecorator.cxx
1 // Copyright (C) 2006-2021  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 "FormContainerDecorator.hxx"
21 #include "FormContainer.hxx"
22 #include "FormHPContainer.hxx"
23
24 #include "HomogeneousPoolContainer.hxx"
25 #include "Exception.hxx"
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 using namespace YACS::ENGINE;
31
32 bool FormContainerDecorator::CHECKED = false;
33
34 FormContainerDecorator::FormContainerDecorator(YACS::ENGINE::Container *cont, QWidget *parent):QWidget(parent),_typ(0)
35 {
36   setupUi(this);
37   _icon.addFile("icons:icon_down.png");
38   _icon.addFile("icons:icon_up.png",QSize(), QIcon::Normal, QIcon::On);
39   tb_container->setIcon(_icon);
40   connect(this,SIGNAL(typeOfContainerIsKnown(const QString&)),label,SLOT(setText(const QString &)));
41   if(!cont)
42     return ;
43   HomogeneousPoolContainer *hpc(dynamic_cast<HomogeneousPoolContainer *>(cont));
44   if(!hpc)
45     _typ=new FormContainer(this);
46   else
47     _typ=new FormHPContainer(this);
48   emit typeOfContainerIsKnown(_typ->getTypeStr());
49   _typ->FillPanel(cont);
50   gridLayout_1->addWidget(_typ);
51   connectForTyp();
52   tb_container->setChecked(CHECKED);
53   on_tb_container_toggled(CHECKED);
54 }
55
56 FormContainerDecorator::~FormContainerDecorator()
57 {
58   delete _typ;
59 }
60
61 void FormContainerDecorator::FillPanel(YACS::ENGINE::Container *container)
62 {
63   checkAndRepareTypeIfNecessary(container);
64   _typ->FillPanel(container);
65 }
66
67 QWidget *FormContainerDecorator::getWidget()
68 {
69   return _typ;
70 }
71
72 bool FormContainerDecorator::onApply()
73 {
74   if(!checkOK())
75     return false;
76   return _typ->onApply();
77 }
78
79 void FormContainerDecorator::onCancel()
80 {
81   if(!checkOK())
82     return ;
83   _typ->onCancel();
84 }
85
86 void FormContainerDecorator::show()
87 {
88   QWidget::show();
89   if(!checkOK())
90     return ;
91   _typ->show();
92   tb_container->setChecked(CHECKED);
93   on_tb_container_toggled(CHECKED);
94 }
95
96 void FormContainerDecorator::hide()
97 {
98   QWidget::hide();
99   if(!checkOK())
100     return ;
101   _typ->hide();
102 }
103
104 void FormContainerDecorator::on_tb_container_toggled(bool checked)
105 {
106   DEBTRACE("FormContainer::on_tb_container_toggled " << checked);
107   CHECKED = checked;
108   if (CHECKED)
109     {
110       getWidget()->show();
111     }
112   else
113     {
114       getWidget()->hide();
115     }
116 }
117
118 void FormContainerDecorator::synchronizeCheckContainer()
119 {
120   tb_container->setChecked(CHECKED);
121 }
122
123 std::string FormContainerDecorator::getHostName(int index) const
124 {
125   if(!checkOK())
126     return std::string();
127   return _typ->cb_resource->itemText(index).toStdString();
128 }
129
130 void FormContainerDecorator::setName(const std::string& name)
131 {
132   if(!checkOK())
133     return ;
134   _typ->le_name->setText(name.c_str());
135 }
136
137 void FormContainerDecorator::onResMousePressed()
138 {
139   emit(resourceMousePressed());
140 }
141
142 void FormContainerDecorator::onResActivated(int v)
143 {
144   emit(resourceActivated(v));
145 }
146
147 void FormContainerDecorator::onContToggled(bool v)
148 {
149   emit(containerToggled(v));
150 }
151
152 bool FormContainerDecorator::checkOK() const
153 {
154   return _typ;
155 }
156
157 void FormContainerDecorator::checkAndRepareTypeIfNecessary(YACS::ENGINE::Container *container)
158 {
159   if(!container)
160     return ;
161   YACS::ENGINE::HomogeneousPoolContainer *cont1(dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(container));
162   if(_typ)
163     {
164       bool isTyp1(dynamic_cast<FormHPContainer *>(_typ)!=0);
165       if((!cont1 && !isTyp1) || (cont1 && isTyp1))
166         return ;
167       delete _typ; _typ=0;
168     }
169   if(!cont1)
170     _typ=new FormContainer(this);
171   else
172     _typ=new FormHPContainer(this);
173   gridLayout_1->addWidget(_typ);
174   emit typeOfContainerIsKnown(_typ->getTypeStr());
175   connectForTyp();
176   _typ->FillPanel(container);
177 }
178
179 void FormContainerDecorator::connectForTyp()
180 {
181   connect(_typ->cb_resource,SIGNAL(mousePressed()),this,SLOT(onResMousePressed()));
182   connect(_typ->cb_resource,SIGNAL(activated(int)),this,SLOT(onResActivated(int)));
183   connect(tb_container,SIGNAL(toggled(bool)),this,SLOT(onContToggled(bool)));
184 }