1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File : VISUConfig.cc
24 // Author : Alexey PETROV
27 #include "VISUConfig.hh"
32 static int MYDEBUG = 0;
34 static int MYDEBUG = 0;
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;
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()));
53 //===========================================================================
54 static int mySCnt = 0;
55 static int myQCnt = 0;
56 static int myIsBatchMode = 0;
58 Mutex::Mutex(QMutex* theMutex, QApplication* theQApp, int theDelay) :
59 myQApp(theQApp), isQAppLocked(theQApp->locked()), myDelay(theDelay),
60 myMutex(theMutex), isSessionLocked(theMutex->locked())
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) {
73 if(!isQAppLocked && !myQCnt) {
76 myQApp->processEvents(myDelay+3);
79 mySCnt--; if(!isSessionLocked && !mySCnt) { myMutex->unlock();}
80 if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
84 //===========================================================================
85 static Storable::TCallbackMap VisuStoreMap;
86 string Storable::ToString(){
88 Storable::DataToStream( strOut, "myComment", GetComment() );
91 if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
92 //apo - auto_ptr<char> aRet(strOut.str());
96 void Storable::Registry(const char* theComment, TStorableEngine theEngine)
97 throw(std::logic_error&)
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 !!!");
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;
113 aName = next.stripWhiteSpace();
117 aName = next.left( pos ).stripWhiteSpace();
118 aValue = next.mid( pos+1 );
119 if ( aValue.isNull() )
122 if ( !aName.isEmpty() )
123 theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
127 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
128 QString output = ( !theName.isNull() ? theName : QString("") )
130 + ( !theVal.isNull() ? theVal : QString("") );
131 theStr<<output.latin1()<<";";
134 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
135 QString output = ( !theName.isNull() ? theName : QString("") )
137 + QString::number( theVal );
138 theStr<<output.latin1()<<";";
141 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
142 QString output = ( !theName.isNull() ? theName : QString("") )
144 + QString::number( theVal );
145 theStr<<output.latin1()<<";";
147 Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
148 const string& thePrefix, const string& theLocalPersistentID)
149 throw (std::runtime_error&)
152 QString strIn( theLocalPersistentID.c_str() );
154 StrToMap(strIn,aMap);
155 //CORBA::String_var aResultID(GetResultSO(theSObject)->GetID());
156 //aMap.insert(TRestoringMap::value_type("ResultID", strdup(aResultID)));
158 QString aComment = VISU::Storable::FindValue(aMap,"myComment",&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);
165 }catch(std::logic_error& exc){
166 INFOS("Follow exception was accured :\n"<<exc.what());
168 INFOS("Unknown exception was accured!");
173 const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
174 throw(std::logic_error&)
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");
184 if(isFind != NULL) *isFind = true;
189 //===========================================================================
190 PortableServer::Servant GetServant(CORBA::Object_ptr theObject){
191 if(CORBA::is_nil(theObject)) return NULL;
193 PortableServer::POA_ptr aPOA = Base_i::GetPOA();
194 return aPOA->reference_to_servant(theObject);
196 INFOS("GetServant - Unknown exception was accured!");
202 //===========================================================================
203 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
204 SALOMEDS::GenericAttribute_var anAttr;
205 CORBA::Object_var anObj;
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);
215 INFOS("SObjectToObject - Unknown exception was accured!");
221 //===========================================================================
222 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
223 const char* theComment, int IsAllLevels)
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();
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)
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);
263 if(strcmp(theName,"") != 0){
264 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
265 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
266 aName->SetValue(theName);
268 if(strcmp(thePersistentRef,"") != 0){
269 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
270 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
271 aPRef->SetValue(thePersistentRef);
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);
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);
284 CORBA::String_var anEntry = newObj->GetID();
285 string aRet(anEntry);
286 if(0 && MYDEBUG) MESSAGE("CreateAttributes - anEntry = "<<aRet<<"; IOR = "<<theIOR);
292 QString VISU::GenerateName(const string& theFmt, int theId){
293 static QString aName;
295 aName.sprintf("%s:%d",theFmt.c_str(),theId);
297 aName.sprintf("%s",theFmt.c_str());