Salome HOME
- Deleted Study as an input parameter and class field.
[modules/kernel.git] / src / KernelHelpers / SALOME_StudyEditor.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
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 // Author: Guillaume Boulant (EDF/R&D) 
20
21 #include "SALOME_StudyEditor.hxx"
22 #include "SALOME_KernelServices.hxx"
23
24 /** Canonic constructor. The object can't be used without a setStudy() */
25 SALOME_StudyEditor::SALOME_StudyEditor() {
26   _sbuilder = KERNEL::getStudy()->NewBuilder();
27 }
28
29 SALOMEDS::SObject_ptr SALOME_StudyEditor::newObject(SALOMEDS::SObject_ptr parent) {
30   return _sbuilder->NewObject(parent);
31 }
32
33 SALOMEDS::SObject_ptr SALOME_StudyEditor::findObject(const char * entry) {
34   SALOMEDS::SObject_var sobject = KERNEL::getStudy()->FindObjectID(entry);
35   return sobject._retn();
36 }
37
38 SALOMEDS::SComponent_ptr SALOME_StudyEditor::newRoot(const char * moduleName) {
39   SALOMEDS::SComponent_var sroot = findRoot(moduleName);
40   if ( sroot->_is_nil() ) {
41     sroot = _sbuilder->NewComponent(moduleName);
42     _sbuilder->SetName(sroot,moduleName);
43   }
44   return sroot._retn();
45 }
46
47 bool SALOME_StudyEditor::bindEngine(SALOMEDS::SComponent_var studyRoot,
48                              Engines::EngineComponent_var engine)
49 {
50   // WARN: The engine should be associated to the SComponent IF
51   // AND ONLY IF it is a SALOMEDS::Driver. Otherwise, there is no
52   // need to do that, and it could even lead to exception
53   // raising (eh, you work on SALOME isn't it?)
54   SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(engine);
55   if ( driver->_is_nil() || studyRoot->_is_nil() ) return false;
56   _sbuilder->DefineComponentInstance(studyRoot, engine);
57   return true;
58 }
59
60 SALOMEDS::SComponent_ptr SALOME_StudyEditor::findRoot(const char * moduleName) {
61   return KERNEL::getStudy()->FindComponent(moduleName);
62 }
63
64 void SALOME_StudyEditor::setName(SALOMEDS::SObject_var sobject, const char * value) {
65   _sbuilder->SetName(sobject,value);  
66 }
67 const char * SALOME_StudyEditor::getName(SALOMEDS::SObject_var sobject) {
68   if (sobject->_is_nil()) return NULL;
69   return sobject->GetName();
70 }
71
72 /**
73  * This function specifies which resource file is to be used to
74  * associate an icon to the specified object. Note that even if the
75  * icon as no sens outside the GUI context, it can be defined here
76  * because only the resource name is provided. The effective rendering
77  * is done in the object browser of course, and use this string
78  * attribute. WARN: note that the resource name is supposed to be the
79  * base name of a file managed by the SALOME resource manager (i.e. a
80  * file located in the directory specified in the SalomeApp.xml).
81  */
82 void SALOME_StudyEditor::setIcon(SALOMEDS::SObject_var sobject, const char * resourcename) {
83   SALOMEDS::GenericAttribute_var anAttr;
84   SALOMEDS::AttributePixMap_var aPixmap;
85   anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributePixMap");
86   aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
87   aPixmap->SetPixMap(resourcename);
88 }
89
90 //
91 // MEM: AttributeParameter
92 // Note that the AttributeParameter is a multitype dictionnary whose
93 // keys are the names specified when setting the value
94 //
95
96 /**
97  * Add a parameter attribute of type integer or simply set its value
98  * if it already exists.
99  */
100 void SALOME_StudyEditor::setParameterInt(SALOMEDS::SObject_var sobject, const char * name, int value) { 
101   SALOMEDS::GenericAttribute_var anAttr;
102   SALOMEDS::AttributeParameter_var aParam;
103   anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributeParameter");
104   aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
105   aParam->SetInt(name,value);
106 }
107
108 int SALOME_StudyEditor::getParameterInt(SALOMEDS::SObject_var sobject, const char * name) {
109   if (sobject->_is_nil()) return UNDEFINED;
110
111   SALOMEDS::GenericAttribute_var anAttr;
112   SALOMEDS::AttributeParameter_var aParam;
113   if ( sobject->FindAttribute(anAttr,"AttributeParameter") ) {
114     aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
115     return aParam->GetInt(name);
116   }
117   return UNDEFINED;
118 }
119
120 /**
121  * Add a parameter attribute of type boolean or simply set its value
122  * if it already exists.
123  */
124 void SALOME_StudyEditor::setParameterBool(SALOMEDS::SObject_var sobject, const char * name, bool value) {
125   SALOMEDS::GenericAttribute_var anAttr;
126   SALOMEDS::AttributeParameter_var aParam;
127   anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributeParameter");
128   aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
129   aParam->SetBool(name,value);
130 }
131
132 bool SALOME_StudyEditor::getParameterBool(SALOMEDS::SObject_var sobject, const char * name) {
133   if (sobject->_is_nil()) return false;
134   // _GBO_ Q: Maybe it's better in this case to raise an exception?
135
136   SALOMEDS::GenericAttribute_var anAttr;
137   SALOMEDS::AttributeParameter_var aParam;
138   if ( sobject->FindAttribute(anAttr,"AttributeParameter") ) {
139     aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
140     return aParam->GetBool(name);
141   }
142   return false;
143
144 }