Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / genericgui / FormHPContainer.cxx
1 // Copyright (C) 2006-2015  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 "FormHPContainer.hxx"
21 #include "FormAdvParamContainer.hxx"
22 #include "HomogeneousPoolContainer.hxx"
23 #include "QtGuiContext.hxx"
24 #include "guiObservers.hxx"
25 //#define _DEVDEBUG_
26 #include "Resource.hxx"
27 #include "YacsTrace.hxx"
28
29 #include <QIntValidator>
30 #include <QLineEdit>
31
32 #if HAS_QSCI4>0
33 #include <qsciscintilla.h>
34 #include <qscilexerpython.h>
35 #endif
36
37 #include <sstream>
38 #include <limits>
39
40 using namespace std;
41
42 FormHPContainer::FormHPContainer(QWidget *parent):FormContainerBase(parent),_poolSz(new QLineEdit(this)),_initScriptModified(false)
43 {
44   QIntValidator *iv(new QIntValidator(_poolSz)); iv->setRange(1,INT_MAX);
45   _poolSz->setValidator(iv);
46   label_15->setText("Size of pool :");
47   gridLayout_2_2->addWidget(_poolSz);
48   FormHPContainer::FillPanel(0); // --- set widgets before signal connexion to avoid false modif detection
49   connect(_poolSz, SIGNAL(textChanged(const QString&)),this, SLOT(onModifySzOfPool(const QString&)));
50   ch_aoc->setEnabled(false);
51   ch_aoc->setCheckState(Qt::Checked);
52   //
53 #if HAS_QSCI4>0
54   _initScript=new QsciScintilla(_advancedParams->tw_advance);
55   QsciLexerPython *lex(new QsciLexerPython(_initScript));
56   lex->setFont(YACS::HMI::Resource::pythonfont);
57   _initScript->setLexer(lex);
58   _initScript->setBraceMatching(QsciScintilla::SloppyBraceMatch);
59   _initScript->setAutoIndent(1);
60   _initScript->setIndentationWidth(4);
61   _initScript->setIndentationGuides(1);
62   _initScript->setIndentationsUseTabs(0);
63   _initScript->setAutoCompletionThreshold(2);
64   _initScript->setMarginWidth(1,0);
65   _initScript->setFolding(QsciScintilla::PlainFoldStyle);
66 #else
67   _initScript=new QTextEdit(this);
68 #endif
69   connect(_initScript,SIGNAL(textChanged()),this,SLOT(initSciptChanged()));
70   QGridLayout *gridLayout(new QGridLayout(_initScript));
71   _advancedParams->tw_advance->addTab(_initScript,"Init Script");
72 }
73
74 FormHPContainer::~FormHPContainer()
75 {
76 }
77
78 void FormHPContainer::FillPanel(YACS::ENGINE::Container *container)
79 {
80   DEBTRACE("FormHPContainer::FillPanel");
81   FormContainerBase::FillPanel(container);
82   if(!container)
83     return ;
84   YACS::ENGINE::HomogeneousPoolContainer *hpc(dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(container));
85   if(!hpc)
86     throw YACS::Exception("FormHPContainer::FillPanel : not a HP Container !");
87   _poolSz->setText(QString("%1").arg(hpc->getSizeOfPool()));
88   std::string initScript;
89   if(_properties.count(YACS::ENGINE::HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY))
90     initScript=_properties[YACS::ENGINE::HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY];
91   std::string initScript2(BuildWithFinalEndLine(initScript));
92   _initScript->blockSignals(true);
93   _initScript->setText(initScript2.c_str());
94   _initScript->blockSignals(false);
95   if (!YACS::HMI::QtGuiContext::getQtCurrent()->isEdition())
96     {
97       //if the schema is in execution do not allow editing
98       _poolSz->setEnabled(false);
99       _initScript->setEnabled(false);
100     }
101 }
102
103 QString FormHPContainer::getTypeStr() const
104 {
105   return QString("Container (HP)");
106 }
107
108 void FormHPContainer::onModifySzOfPool(const QString& newSz)
109 {
110   if (!_container)
111     return;
112   map<string,string> properties(_container->getProperties());
113   uint sz;
114   bool isOK;
115   sz=newSz.toUInt(&isOK);
116   if(!isOK)
117     return ;
118   _properties[YACS::ENGINE::HomogeneousPoolContainer::SIZE_OF_POOL_KEY] = newSz.toStdString();
119   if(properties[YACS::ENGINE::HomogeneousPoolContainer::SIZE_OF_POOL_KEY] != newSz.toStdString())
120     onModified();
121 }
122
123 bool FormHPContainer::onApply()
124 {
125   YACS::HMI::SubjectContainerBase *scont(YACS::HMI::QtGuiContext::getQtCurrent()->_mapOfSubjectContainer[_container]);
126   YASSERT(scont);
127   bool ret(scont->setName(le_name->text().toStdString()));
128   std::map<std::string,std::string> properties(_properties);
129   if(_initScriptModified)
130     {
131       std::string text(_initScript->text().toStdString());
132       std::string text2(BuildWithFinalEndLine(text));
133       properties[YACS::ENGINE::HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY]=text2;
134     }
135   _initScriptModified=false;
136   DEBTRACE(ret);
137   if(ret)
138     ret = scont->setProperties(properties);
139   return ret;
140 }
141
142 void FormHPContainer::initSciptChanged()
143 {
144   _initScriptModified=true;
145   onModified();
146 }
147
148 std::string FormHPContainer::BuildWithFinalEndLine(const std::string& script)
149 {
150   if(script.empty())
151     return std::string("\n");
152   std::size_t sz(script.length());
153   if(script[sz-1]!='\n')
154     {
155       std::string ret(script);
156       ret+="\n";
157       return ret;
158     }
159   else
160     return script;
161 }