浏览代码

ENH: Added virtual function objectNameToKey() to ctkAbstractQObjectFactory

This virtual function can be overloaded in sub-class to return an identifier
different from (or based on) the QObject name.
Jean-Christophe Fillion-Robin 15 年之前
父节点
当前提交
0f1026d79b
共有 2 个文件被更改,包括 17 次插入4 次删除
  1. 9 3
      Libs/Core/ctkAbstractQObjectFactory.h
  2. 8 1
      Libs/Core/ctkAbstractQObjectFactory.tpp

+ 9 - 3
Libs/Core/ctkAbstractQObjectFactory.h

@@ -38,16 +38,22 @@ public:
   virtual ~ctkAbstractQObjectFactory();
 
   /// 
-  /// Create an instance of the object
+  /// Create an instance of the object identified by \a itemKey
   virtual BaseClassType * instantiate(const QString& itemKey);
 
   /// 
-  /// Uninstanciate the object
+  /// Uninstanciate the object identified by \a itemKey
   virtual void uninstantiate(const QString& itemKey);
 
+  ///
+  /// Return a name allowing to uniquely identify the QObject
+  /// By default, it return \a objectName obtained using staticMetaObject.className()
+  virtual QString objectNameToKey(const QString& objectName);
+  
   /// 
   /// Register an object in the factory
-  /// Note: The parameter 'key' passed by reference will be updated with the associated object name
+  /// The parameter \a key passed by reference will be updated with the
+  /// associated object name obtained using ::objectNameToKey()
   template<typename ClassType>
   bool registerQObject(QString& key);
 

+ 8 - 1
Libs/Core/ctkAbstractQObjectFactory.tpp

@@ -55,10 +55,17 @@ void ctkAbstractQObjectFactory<BaseClassType>::uninstantiate(const QString& item
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
+QString ctkAbstractQObjectFactory<BaseClassType>::objectNameToKey(const QString& objectName)
+{
+  return objectName; 
+}
+
+//----------------------------------------------------------------------------
+template<typename BaseClassType>
 template<typename ClassType>
 bool ctkAbstractQObjectFactory<BaseClassType>::registerQObject(QString& key)
 {
-  key = QString::fromLatin1(ClassType::staticMetaObject.className());
+  key = this->objectNameToKey(QString::fromLatin1(ClassType::staticMetaObject.className()));
   return this->ctkAbstractObjectFactory<BaseClassType>::template registerObject<ClassType>(key);
 }