00001 #ifndef OnXSvc_Type_h
00002 #define OnXSvc_Type_h
00003
00004
00005 #include <Lib/Interfaces/IIterator.h>
00006 #include <OnX/Core/BaseType.h>
00007
00008 class IDataProviderSvc;
00009
00010 #include <OnXSvc/ISoConversionSvc.h>
00011 #include <OnXSvc/IUserInterfaceSvc.h>
00012
00013 #include <Lib/Interfaces/ISession.h>
00014 #include <Lib/Out.h>
00015 #include <Lib/smanip.h>
00016
00017 #include <GaudiKernel/IDataProviderSvc.h>
00018 #include <GaudiKernel/SmartDataPtr.h>
00019 #include <GaudiKernel/System.h>
00020 #include <GaudiKernel/ObjectVector.h>
00021
00022 namespace OnXSvc {
00023
00024 template <class Object>
00025 class Iterator : public Lib::IIterator {
00026 public:
00027 virtual Lib::Identifier object() {
00028 if(!fVector) return 0;
00029 if(fIterator==fVector->end()) return 0;
00030 return *fIterator;
00031 }
00032 virtual void next() {
00033 if(!fVector) return;
00034
00035 while(true) {
00036 ++fIterator;
00037 if(fIterator==fVector->end()) return;
00038 if(*fIterator) return;
00039 }
00040 }
00041 virtual void* tag() { return 0;}
00042 public:
00043 typedef ObjectVector<Object> Collection;
00044 Iterator(Collection* aVector = 0):fVector(aVector) {
00045 if(fVector) fIterator = fVector->begin();
00046 }
00047 private:
00048 Collection* fVector;
00049 typename Collection::iterator fIterator;
00050 };
00051
00052 template <class Object>
00053 class Type : public OnX::BaseType {
00054 private:
00055 typedef ObjectVector<Object> Collection;
00056 public:
00057 virtual void* cast(const std::string& aClass) const {
00058
00059
00060 if(aClass=="Slash::Data::IVisualizer") {
00061 return (void*)static_cast<const Slash::Data::IVisualizer*>(this);
00062 } else {
00063 return OnX::BaseType::cast(aClass);
00064 }
00065 }
00066 virtual std::string name() const { return fType;}
00067 virtual Lib::IIterator* iterator() {
00068 if(fIterator) return fIterator;
00069 if(!fDataProviderSvc) {
00070 Lib::Out out(printer());
00071 out << "OnXSvc::Type<>::iterator :"
00072 << " no data provider found."
00073 << Lib::endl;
00074 return new Iterator< Object >();
00075 }
00076 SmartDataPtr<DataObject> smartDataObject(fDataProviderSvc,fLocation);
00077 if(smartDataObject) {}
00078 DataObject* dataObject = 0;
00079 StatusCode sc = fDataProviderSvc->retrieveObject(fLocation, dataObject);
00080 if(!sc.isSuccess()) {
00081 Lib::Out out(printer());
00082 out << "OnXSvc::Type<>::iterator :"
00083 << " retreiveObject failed."
00084 << Lib::endl;
00085 return new Iterator< Object >();
00086 }
00087 Collection* collection = dynamic_cast<Collection*>(dataObject);
00088 if(!collection) {
00089 Lib::Out out(printer());
00090 out << "OnXSvc::Type<>::iterator :"
00091 << " dynamic_cast failed."
00092 << Lib::endl;
00093 return new Iterator< Object >();
00094 }
00095 return new Iterator< Object >(collection);
00096 }
00097 virtual void setIterator(Lib::IIterator* aIterator) { fIterator = aIterator;}
00098 public:
00099 virtual void beginVisualize() {
00100 if(!fUISvc) {
00101 fSoRegion = 0;
00102 return;
00103 }
00104 ISession* session = fUISvc->session();
00105 if(!session) {
00106 Lib::Out out(printer());
00107 out << "OnXSvc::Type<>::beginVisualize :"
00108 << " can't get a ISession."
00109 << Lib::endl;
00110 fSoRegion = 0;
00111 return;
00112 }
00113 fSoRegion = fUISvc->currentSoRegion();
00114 }
00115 virtual void endVisualize() {
00116 fSoRegion = 0;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 public:
00152 Type(const CLID& aCLID,
00153 const std::string& aType,
00154 const std::string& aLocation,
00155 IUserInterfaceSvc* aUISvc,
00156 ISoConversionSvc* aSoCnvSvc,
00157 IDataProviderSvc* aDataProviderSvc)
00158 :OnX::BaseType(aUISvc->printer())
00159 ,fCLID(aCLID)
00160 ,fType(aType)
00161 ,fLocation(aLocation)
00162 ,fIterator(0)
00163 ,fConverter(0)
00164
00165 ,fSoRegion(0)
00166 ,fUISvc(aUISvc)
00167 ,fSoCnvSvc(aSoCnvSvc)
00168 ,fDataProviderSvc(aDataProviderSvc) {
00169
00170 if(!fUISvc) {
00171 Lib::Out out(printer());
00172 out << "OnXSvc::Type<> :"
00173 << " can't get a IUserInterfaceSvc."
00174 << Lib::endl;
00175 }
00176
00177
00178 }
00179 virtual ~Type() {
00180
00181 }
00182 void setLocation(const std::string& aLocation) { fLocation = aLocation;}
00183 const std::string& location() const { return fLocation;}
00184 protected:
00185 bool setLocationFromSession(bool verbose = true) {
00186 if(!fUISvc) return false;
00187 ISession* session = fUISvc->session();
00188 if(!session) return false;
00189 std::string value;
00190 if(!session->parameterValue(fType+".location",value)) {
00191 if(verbose) {
00192 Lib::Out out(printer());
00193 out << "OnXSvc::Type<" << fType << ">::setLocationFromSession :"
00194 << " Session parameter " << fType << ".location not found."
00195 << Lib::endl;
00196 }
00197 return false;
00198 }
00199 if(value=="") {
00200 if(verbose) {
00201 Lib::Out out(printer());
00202 out << "OnXSvc::Type<" << fType << ">::setLocationFromSession :"
00203 << " Session parameter " << fType << ".location empty."
00204 << Lib::endl;
00205 }
00206 return false;
00207 }
00208 if(verbose) {
00209 Lib::Out out(printer());
00210 out << "OnXSvc::Type<" << fType << ">::setLocationFromSession :"
00211 << " set location \"" << value << "\""
00212 << Lib::endl;
00213 }
00214 setLocation(value);
00215 return true;
00216 }
00217 bool attribute(const std::string& aWhat,std::string& aValue) const {
00218 aValue = "";
00219 if(!fUISvc) return false;
00220 ISession* session = fUISvc->session();
00221 if(!session) return false;
00222 return session->parameterValue(aWhat,aValue);
00223 }
00224 bool attribute(const std::string& aWhat,float& aR,float& aG,float& aB) const {
00225 aR = 0.5F;
00226 aG = 0.5F;
00227 aB = 0.5F;
00228 if(!fUISvc) return false;
00229 ISession* session = fUISvc->session();
00230 if(!session) return false;
00231 std::string value;
00232 if(!session->parameterValue(aWhat,value)) return false;
00233 double r,g,b;
00234 if(!Lib::smanip::torgb(value,r,g,b)) return false;
00235 aR = (float)r;
00236 aG = (float)g;
00237 aB = (float)b;
00238 return true;
00239 }
00240 bool modelingSolid() const {
00241 if(!fUISvc) return true;
00242 ISession* session = fUISvc->session();
00243 if(!session) return true;
00244 std::string value;
00245 if(!session->parameterValue("modeling.modeling",value)) return true;
00246 return (value=="solid" ? true : false);
00247 }
00248 private:
00249 const CLID& fCLID;
00250 std::string fType;
00251 std::string fLocation;
00252 Lib::IIterator* fIterator;
00253 IConverter* fConverter;
00254
00255 protected:
00256 SoRegion* fSoRegion;
00257 IUserInterfaceSvc* fUISvc;
00258 ISoConversionSvc* fSoCnvSvc;
00259 IDataProviderSvc* fDataProviderSvc;
00260 };
00261
00262
00263 }
00264
00265 #endif