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