Salome HOME
996f0c9b56e6be01bbd1dc1c14d8b9e858740362
[modules/yacs.git] / src / genericgui / FormContainerDecorator.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 "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   if(!cont)
37     throw YACS::Exception("FormContainerDecorator constrctor : container is empty !");
38   setupUi(this);
39   _icon.addFile("icons:icon_down.png");
40   _icon.addFile("icons:icon_up.png",QSize(), QIcon::Normal, QIcon::On);
41   tb_container->setIcon(_icon);
42   connect(this,SIGNAL(typeOfContainerIsKnown(const QString&)),label,SLOT(setText(const QString &)));
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   checkOK();
64   checkAndRepareTypeIfNecessary(container);
65   _typ->FillPanel(container);
66 }
67
68 QWidget *FormContainerDecorator::getWidget()
69 {
70   checkOK();
71   return _typ;
72 }
73
74 bool FormContainerDecorator::onApply()
75 {
76   checkOK();
77   return _typ->onApply();
78 }
79
80 void FormContainerDecorator::onCancel()
81 {
82   checkOK();
83   _typ->onCancel();
84 }
85
86 void FormContainerDecorator::show()
87 {
88   QWidget::show();
89   checkOK();
90   _typ->show();
91   tb_container->setChecked(CHECKED);
92   on_tb_container_toggled(CHECKED);
93 }
94
95 void FormContainerDecorator::hide()
96 {
97   QWidget::hide();
98   checkOK();
99   _typ->hide();
100 }
101
102 void FormContainerDecorator::on_tb_container_toggled(bool checked)
103 {
104   DEBTRACE("FormContainer::on_tb_container_toggled " << checked);
105   CHECKED = checked;
106   if (CHECKED)
107     {
108       getWidget()->show();
109     }
110   else
111     {
112       getWidget()->hide();
113     }
114 }
115
116 void FormContainerDecorator::synchronizeCheckContainer()
117 {
118   checkOK();
119   tb_container->setChecked(CHECKED);
120 }
121
122 std::string FormContainerDecorator::getHostName(int index) const
123 {
124   checkOK();
125   return _typ->cb_resource->itemText(index).toStdString();
126 }
127
128 void FormContainerDecorator::setName(const std::string& name)
129 {
130   checkOK();
131   _typ->le_name->setText(name.c_str());
132 }
133
134 void FormContainerDecorator::onResMousePressed()
135 {
136   emit(resourceMousePressed());
137 }
138
139 void FormContainerDecorator::onResActivated(int v)
140 {
141   emit(resourceActivated(v));
142 }
143
144 void FormContainerDecorator::onContToggled(bool v)
145 {
146   emit(containerToggled(v));
147 }
148
149 void FormContainerDecorator::checkOK() const
150 {
151   if(!_typ)
152     throw YACS::Exception("Null Widget Container !!!!");
153 }
154
155 void FormContainerDecorator::checkAndRepareTypeIfNecessary(YACS::ENGINE::Container *container)
156 {
157   checkOK();
158   if(!container)
159     throw YACS::Exception("Null Container !!!!");
160   YACS::ENGINE::HomogeneousPoolContainer *cont1(dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(container));
161   bool isTyp1(dynamic_cast<FormHPContainer *>(_typ)!=0);
162   if((!cont1 && !isTyp1) || (cont1 && isTyp1))
163     return ;
164   QWidget *parent(_typ->parentWidget());
165   delete _typ; _typ=0;
166   if(!cont1)
167     _typ=new FormContainer(parent);
168   else
169     _typ=new FormHPContainer(parent);
170   gridLayout_1->addWidget(_typ);
171   emit typeOfContainerIsKnown(_typ->getTypeStr());
172   connectForTyp();
173   _typ->FillPanel(container);
174 }
175
176 void FormContainerDecorator::connectForTyp()
177 {
178   connect(_typ->cb_resource,SIGNAL(mousePressed()),this,SLOT(onResMousePressed()));
179   connect(_typ->cb_resource,SIGNAL(activated(int)),this,SLOT(onResActivated));
180   connect(tb_container,SIGNAL(toggled(bool)),this,SLOT(onContToggled()));
181 }