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