Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/visu.git] / src / VISU_I / VISUConfig.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File   : VISUConfig.cc
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISUConfig.hh"
28
29 using namespace std;
30
31 #ifdef _DEBUG_
32 static int MYDEBUG = 0;
33 #else
34 static int MYDEBUG = 0;
35 #endif
36
37 namespace VISU{
38   //===========================================================================
39   QMutex* Base_i::myMutex = NULL; //apo - &VISUMutex;
40   CORBA::ORB_var Base_i::myOrb;
41   PortableServer::POA_var Base_i::myPoa;
42   SALOME_NamingService* Base_i::myNamingService;
43   SALOME_LifeCycleCORBA* Base_i::myEnginesLifeCycle;
44   VISU_Gen_i* Base_i::myVisuGenImpl;
45
46   Base_i::Base_i() {}
47
48   Base_i::~Base_i() {}
49
50   char* Base_i::GetID(){ 
51     if(myID == ""){
52       CORBA::Object_var anObject = _this();
53       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
54       myID = anIOR.in();
55     }
56     return CORBA::string_dup(myID.c_str());
57   }
58
59   //===========================================================================
60   static int mySCnt = 0;
61   static QMutex aMutex(TRUE);
62
63   Mutex::Mutex(QMutex* theMutex): myMutex(&aMutex){
64     if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!mySCnt);
65     if(!mySCnt++) 
66       myMutex->lock();
67   }
68
69   Mutex::~Mutex(){
70     if(!(--mySCnt))
71       myMutex->unlock();
72     if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!mySCnt);
73   }
74
75
76   //===========================================================================
77   static Storable::TCallbackMap VisuStoreMap;
78   string Storable::ToString(){
79     ostringstream strOut;
80     Storable::DataToStream( strOut, "myComment", GetComment() );
81     ToStream(strOut);
82     strOut<<ends;
83     if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
84     return strOut.str(); 
85   }
86
87   void Storable::Registry(const char* theComment, TStorableEngine theEngine)
88   {
89     if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
90       if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
91       throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
92     }
93   }
94   
95   void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
96     if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
97     QStringList strList = QStringList::split( ";", theStr, false );
98     for ( int i = 0; i < strList.count(); i++ ) {
99       QString next = strList[ i ];
100       int pos = next.find("=");
101       QString aName, aValue;
102       if ( pos < 0 ) {
103         aName = next.stripWhiteSpace();
104         aValue = "";
105       }
106       else {
107         aName = next.left( pos ).stripWhiteSpace();
108         aValue = next.mid( pos+1 );
109         if ( aValue.isNull() )
110           aValue = "";
111       }
112       if ( !aName.isEmpty() )
113         theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );   
114     }
115   }
116
117   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
118     QString output = ( !theName.isNull() ? theName : QString("") ) 
119                    + QString( "=" ) 
120                    + ( !theVal.isNull()  ? theVal  : QString("") );
121     theStr<<output.latin1()<<";";
122   }
123
124   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
125     QString output = ( !theName.isNull() ? theName : QString("") ) 
126                    + QString( "=" ) 
127                    + QString::number( theVal );
128     theStr<<output.latin1()<<";";
129   }
130
131   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
132     QString output = ( !theName.isNull() ? theName : QString("") ) 
133                    + QString( "=" ) 
134                    + QString::number( theVal );
135     theStr<<output.latin1()<<";";
136   }
137   Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
138                              const string& thePrefix, const string& theLocalPersistentID) 
139   {
140     try{
141       QString strIn( theLocalPersistentID.c_str() );
142       TRestoringMap aMap;         
143       StrToMap(strIn,aMap);
144       bool isExist;
145       QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
146       if ( isExist ) {
147         TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
148         if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
149         if(i == VisuStoreMap.end()) return NULL;
150         return (i->second)(theSObject,thePrefix,aMap);
151       }
152     }catch(std::logic_error& exc){
153       INFOS("Follow exception was occured :\n"<<exc.what());
154     }catch(...){
155       INFOS("Unknown exception was occured!");
156     }
157     return NULL;
158   }
159   
160   const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
161   {
162     TRestoringMap::const_iterator i = theMap.find(theArg);
163     if(i == theMap.end()) {
164       if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
165       if(isFind != NULL) *isFind = false;
166       //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
167       static QString BAD_VALUE("NULL");
168       return BAD_VALUE;
169     }
170     if(isFind != NULL) *isFind = true;
171     return i->second;
172   }
173   
174
175   //===========================================================================
176   PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
177     if(CORBA::is_nil(theObject))  return NULL;
178     try{
179       PortableServer::POA_ptr aPOA = Base_i::GetPOA();
180       PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
181       return aServant;
182     } catch (...) {
183       INFOS("GetServant - Unknown exception was occured!!!"); 
184       return NULL;
185     }
186   }
187
188
189   //===========================================================================
190   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
191     SALOMEDS::GenericAttribute_var anAttr;
192     CORBA::Object_var anObj;
193     try{
194       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
195         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
196         CORBA::String_var aValue = anIOR->Value();
197         CORBA::ORB_ptr anORB = Base_i::GetORB();
198         if(strcmp(aValue,"") != 0)
199           anObj = anORB->string_to_object(aValue);
200       }
201     }catch(...){
202       INFOS("SObjectToObject - Unknown exception was occured!!!");
203     }
204     return anObj;
205   }
206
207
208   //===========================================================================
209   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
210                               const char* theComment, int IsAllLevels)
211   {
212     SALOMEDS::ChildIterator_var anIter = 
213       theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
214     anIter->InitEx(IsAllLevels);
215     SALOMEDS::SObject_var aFieldSO;
216     for(;anIter->More();anIter->Next()) {
217       SALOMEDS::GenericAttribute_var anAttr;
218       if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
219         SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
220         CORBA::String_var aString = aCmnt->Value();
221         string aValue(aString);
222         if (aValue == theComment) {
223           aFieldSO = anIter->Value();
224           aString = aFieldSO->GetID();
225           aValue = aString;
226           return aValue;
227         }
228       }
229     }
230     return "";
231   }
232   //===========================================================================
233   string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument, 
234                           const char* theFatherEntry, const char* theRefFatherEntry,
235                           const char* theIOR, const char* theName, 
236                           const char* thePersistentRef, const char* theComment,
237                           CORBA::Boolean theCreateNew)
238   {
239     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
240     SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
241     SALOMEDS::SObject_var newObj;
242     if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
243     else newObj = aFather;
244     SALOMEDS::GenericAttribute_var anAttr;
245     if(strcmp(theIOR,"") != 0){
246       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
247       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
248       anIOR->SetValue(theIOR); 
249     }
250     if(strcmp(theName,"") != 0){
251       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
252       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
253       aName->SetValue(theName); 
254     }
255     if(strcmp(thePersistentRef,"") != 0){
256       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
257       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
258       aPRef->SetValue(thePersistentRef); 
259     }
260     if(strcmp(theComment,"") != 0){
261       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
262       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
263       aCmnt->SetValue(theComment);
264       if(MYDEBUG) INFOS("CreateAttributes - Comment = "<<theComment);
265     }
266     if(strcmp(theRefFatherEntry,"") != 0){
267       SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
268       SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
269       aStudyBuilder->Addreference(anObj,newObj);
270     }
271     CORBA::String_var anEntry = newObj->GetID();
272     string aRet(anEntry);
273     if(MYDEBUG) 
274       INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
275     return aRet;
276   }
277 }
278
279
280 QString VISU::GenerateName(const string& theFmt, int theId){
281   static QString aName;
282   if(theId > 0)
283     aName.sprintf("%s:%d",theFmt.c_str(),theId);
284   else
285     aName.sprintf("%s",theFmt.c_str());
286   return aName;
287 }