Salome HOME
Re-wrote PARAVIS CORBA engine
[modules/paravis.git] / src / PVGUI / deprecated / PV_Tools.cxx
1 // Copyright (C) 2010-2014  CEA/DEN, EDF R&D
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 //  File   : PV_Tools.cxx
20 //  Author : Alexey PETROV
21 //  Module : VISU
22 //
23 #include "PV_Tools.h"  
24 #include "PARAVIS_Gen_i.hh"
25
26 #include <omnithread.h>
27 #include CORBA_SERVER_HEADER(SALOME_Session)
28 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
29
30 #include <SALOME_NamingService.hxx>
31
32 #include <QString>
33
34 namespace PARAVIS
35 {
36 //----------------------------------------------------------------------------
37   SALOMEDS::ListOfFileNames* GetListOfFileNames(const QStringList& theFileNames)
38   {
39     SALOMEDS::ListOfFileNames_var aListOfFileNames = new SALOMEDS::ListOfFileNames;
40     if(!theFileNames.isEmpty()){
41       aListOfFileNames->length(theFileNames.size());
42       int i = 0;
43       foreach (QString aName, theFileNames) {
44         aListOfFileNames[i] = qPrintable(aName);
45         i++;
46       }
47     }
48     return aListOfFileNames._retn();
49   }
50
51   //----------------------------------------------------------------------------
52   SALOMEDS::SComponent_var FindOrCreateParaVisComponent(SALOMEDS::Study_ptr theStudyDocument)
53   {
54     SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("PARAVIS");
55     if (aSComponent->_is_nil()) {
56       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
57       aStudyBuilder->NewCommand();
58       int aLocked = theStudyDocument->GetProperties()->IsLocked();
59       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
60       aSComponent = aStudyBuilder->NewComponent("PARAVIS");
61       SALOMEDS::GenericAttribute_var anAttr = 
62         aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
63       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
64       
65       CORBA::ORB_var anORB = PARAVIS_Gen_i::GetORB();
66       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
67       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
68       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = 
69         SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
70       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "PARAVIS" );
71       if ( !CORBA::is_nil(Comp) ) {
72         aName->SetValue( Comp->componentusername() );
73       }
74       
75       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
76       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
77       aPixmap->SetPixMap( "pqAppIcon16.png" );
78
79       // Create Attribute parameters for future using
80       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeParameter");
81       
82       PARAVIS_Gen_var aParaVisGen = PARAVIS_Gen_i::GetParavisGenImpl()->_this();
83       aStudyBuilder->DefineComponentInstance(aSComponent, aParaVisGen);
84       if (aLocked) 
85         theStudyDocument->GetProperties()->SetLocked(true);
86       aStudyBuilder->CommitCommand();
87     }
88     return aSComponent;
89   }
90
91   PARAVIS_Base_i* GET_SERVANT(CORBA::Object_ptr aObj) {
92     //dynamic_cast<PARAVIS_Base_i*>(PARAVIS_Gen_i::GetPOA()->reference_to_servant(OBJ).in())
93     if (CORBA::is_nil(aObj))
94       return NULL;
95     try {
96       PortableServer::POA_ptr aPOA = PARAVIS_Gen_i::GetPOA();
97       PortableServer::ServantBase_var aServant = aPOA->reference_to_servant(aObj);
98       return dynamic_cast<PARAVIS_Base_i*>(aServant.in());
99     } catch (...) {
100       MESSAGE("GET_SERVANT: Unknown exception!!!");
101     }
102     return NULL;
103   }
104
105   void SetRestoreParam(SALOMEDS::SComponent_ptr theComponent, bool theRestore)
106   {
107     SALOMEDS::GenericAttribute_var anAttr;
108     if (theComponent->FindAttribute(anAttr, "AttributeParameter")) {
109       SALOMEDS::AttributeParameter_var aParams = SALOMEDS::AttributeParameter::_narrow(anAttr);
110       aParams->SetBool("RestorePath", theRestore);
111     }
112   }
113
114   bool GetRestoreParam(SALOMEDS::SComponent_ptr theComponent)
115   {
116     SALOMEDS::GenericAttribute_var anAttr;
117     if (theComponent->FindAttribute(anAttr, "AttributeParameter")) {
118       SALOMEDS::AttributeParameter_var aParams = SALOMEDS::AttributeParameter::_narrow(anAttr);
119       if (aParams->IsSet("RestorePath", 2)) {
120         return aParams->GetBool("RestorePath");
121       }
122     }
123     return true;
124   }
125 };