]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISUConfig.cc
Salome HOME
MPV: Merge V1_2d
[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 int myQCnt = 0;
62   static int myIsBatchMode = 0;
63
64   Mutex::Mutex(QMutex* theMutex, QApplication* theQApp, int theDelay) :
65     myQApp(theQApp), isQAppLocked(theQApp->locked()), myDelay(theDelay),
66     myMutex(theMutex), isSessionLocked(theMutex->locked())
67   {
68     if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
69     if(!myIsBatchMode && isQAppLocked) myIsBatchMode++;
70     if(!isSessionLocked && !mySCnt) { myMutex->lock();}; mySCnt++;
71     if(!isQAppLocked && !myQCnt) { 
72       myQApp->lock(); 
73       myQApp->syncX();
74     };
75     myQCnt++;
76   }
77   Mutex::~Mutex(){
78     myQCnt--; 
79     if(!isQAppLocked && !myQCnt) { 
80       myQApp->flushX(); 
81       //if(myDelay > 0)
82         myQApp->processEvents(myDelay+3);
83       myQApp->unlock();
84     }
85     mySCnt--; if(!isSessionLocked && !mySCnt) { myMutex->unlock();}
86     if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
87   }
88
89
90   //===========================================================================
91   static Storable::TCallbackMap VisuStoreMap;
92   string Storable::ToString(){
93     ostringstream strOut;
94     Storable::DataToStream( strOut, "myComment", GetComment() );
95     ToStream(strOut);
96     strOut<<ends;
97     if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
98     return strOut.str(); 
99   }
100
101   void Storable::Registry(const char* theComment, TStorableEngine theEngine)
102     throw(std::logic_error&)
103   {
104     if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
105       if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
106       throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
107     }
108   }
109   
110   void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
111     if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
112     QStringList strList = QStringList::split( ";", theStr, false );
113     for ( int i = 0; i < strList.count(); i++ ) {
114       QString next = strList[ i ];
115       int pos = next.find("=");
116       QString aName, aValue;
117       if ( pos < 0 ) {
118         aName = next.stripWhiteSpace();
119         aValue = "";
120       }
121       else {
122         aName = next.left( pos ).stripWhiteSpace();
123         aValue = next.mid( pos+1 );
124         if ( aValue.isNull() )
125           aValue = "";
126       }
127       if ( !aName.isEmpty() )
128         theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );   
129     }
130   }
131
132   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
133     QString output = ( !theName.isNull() ? theName : QString("") ) 
134                    + QString( "=" ) 
135                    + ( !theVal.isNull()  ? theVal  : QString("") );
136     theStr<<output.latin1()<<";";
137   }
138
139   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
140     QString output = ( !theName.isNull() ? theName : QString("") ) 
141                    + QString( "=" ) 
142                    + QString::number( theVal );
143     theStr<<output.latin1()<<";";
144   }
145
146   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
147     QString output = ( !theName.isNull() ? theName : QString("") ) 
148                    + QString( "=" ) 
149                    + QString::number( theVal );
150     theStr<<output.latin1()<<";";
151   }
152   Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
153                              const string& thePrefix, const string& theLocalPersistentID) 
154     throw (std::runtime_error&)
155   {
156     try{
157       QString strIn( theLocalPersistentID.c_str() );
158       TRestoringMap aMap;         
159       StrToMap(strIn,aMap);
160       bool isExist;
161       QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
162       if ( isExist ) {
163         TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
164         if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
165         if(i == VisuStoreMap.end()) return NULL;
166         return (i->second)(theSObject,thePrefix,aMap);
167       }
168     }catch(std::logic_error& exc){
169       INFOS("Follow exception was accured :\n"<<exc.what());
170     }catch(...){
171       INFOS("Unknown exception was accured!");
172     }
173     return NULL;
174   }
175   
176   const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
177     throw(std::logic_error&)
178   {
179     TRestoringMap::const_iterator i = theMap.find(theArg);
180     if(i == theMap.end()) {
181       if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
182       if(isFind != NULL) *isFind = false;
183       //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
184       static QString BAD_VALUE("NULL");
185       return BAD_VALUE;
186     }
187     if(isFind != NULL) *isFind = true;
188     return i->second;
189   }
190   
191
192   //===========================================================================
193   PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
194     if(CORBA::is_nil(theObject))  return NULL;
195     try{
196       PortableServer::POA_ptr aPOA = Base_i::GetPOA();
197       PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
198       return aServant;
199     } catch (...) {
200       INFOS("GetServant - Unknown exception was accured!!!"); 
201       return NULL;
202     }
203   }
204
205
206   //===========================================================================
207   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
208     SALOMEDS::GenericAttribute_var anAttr;
209     CORBA::Object_var anObj;
210     try{
211       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
212         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
213         CORBA::String_var aValue = anIOR->Value();
214         CORBA::ORB_ptr anORB = Base_i::GetORB();
215         if(strcmp(aValue,"") != 0)
216           anObj = anORB->string_to_object(aValue);
217       }
218     }catch(...){
219       INFOS("SObjectToObject - Unknown exception was accured!!!");
220     }
221     return anObj;
222   }
223
224
225   //===========================================================================
226   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
227                               const char* theComment, int IsAllLevels)
228   {
229     SALOMEDS::ChildIterator_var anIter = 
230       theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
231     anIter->InitEx(IsAllLevels);
232     SALOMEDS::SObject_var aFieldSO;
233     for(;anIter->More();anIter->Next()) {
234       SALOMEDS::GenericAttribute_var anAttr;
235       if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
236         SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
237         CORBA::String_var aString = aCmnt->Value();
238         string aValue(aString);
239         if (aValue == theComment) {
240           aFieldSO = anIter->Value();
241           aString = aFieldSO->GetID();
242           aValue = aString;
243           return aValue;
244         }
245       }
246     }
247     return "";
248   }
249   //===========================================================================
250   string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument, 
251                           const char* theFatherEntry, const char* theRefFatherEntry,
252                           const char* theIOR, const char* theName, 
253                           const char* thePersistentRef, const char* theComment,
254                           CORBA::Boolean theCreateNew)
255   {
256     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
257     SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
258     SALOMEDS::SObject_var newObj;
259     if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
260     else newObj = aFather;
261     SALOMEDS::GenericAttribute_var anAttr;
262     if(strcmp(theIOR,"") != 0){
263       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
264       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
265       anIOR->SetValue(theIOR); 
266     }
267     if(strcmp(theName,"") != 0){
268       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
269       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
270       aName->SetValue(theName); 
271     }
272     if(strcmp(thePersistentRef,"") != 0){
273       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
274       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
275       aPRef->SetValue(thePersistentRef); 
276     }
277     if(strcmp(theComment,"") != 0){
278       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
279       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
280       aCmnt->SetValue(theComment);
281       INFOS("CreateAttributes - Comment = "<<theComment);
282     }
283     if(strcmp(theRefFatherEntry,"") != 0){
284       SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
285       SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
286       aStudyBuilder->Addreference(anObj,newObj);
287     }
288     CORBA::String_var anEntry = newObj->GetID();
289     string aRet(anEntry);
290     if(MYDEBUG) 
291       INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
292     return aRet;
293   }
294 }
295
296
297 QString VISU::GenerateName(const string& theFmt, int theId){
298   static QString aName;
299   if(theId > 0)
300     aName.sprintf("%s:%d",theFmt.c_str(),theId);
301   else
302     aName.sprintf("%s",theFmt.c_str());
303   return aName;
304 }