KTutorial
0.5.1
|
00001 /*************************************************************************** 00002 * Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com> * 00003 * Copyright (C) 2011 by Daniel Calviño Sánchez <danxuliu@gmail.com> * 00004 * Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com> * 00005 * * 00006 * This program is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU General Public License as published by * 00008 * the Free Software Foundation; either version 2 of the License, or * 00009 * (at your option) any later version. * 00010 * * 00011 * This program is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00014 * GNU General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU General Public License * 00017 * along with this program; If not, see <http://www.gnu.org/licenses/>. * 00018 ***************************************************************************/ 00019 00020 #include "ObjectRegister.h" 00021 00022 namespace ktutorial { 00023 namespace editorsupport { 00024 00025 //public: 00026 00027 ObjectRegister::ObjectRegister(QObject* parent /*= 0*/): QObject(parent), 00028 mNextId(1) { 00029 } 00030 00031 int ObjectRegister::idForObject(QObject* object) { 00032 if (!object) { 00033 return 0; 00034 } 00035 00036 int id = mRegisteredIds.value(object); 00037 00038 if (!id) { 00039 id = mNextId; 00040 mRegisteredObjects.insert(id, object); 00041 mRegisteredIds.insert(object, id); 00042 connect(object, SIGNAL(destroyed(QObject*)), 00043 this, SLOT(deregister(QObject*))); 00044 mNextId++; 00045 00046 registerMetaObject(object->metaObject()); 00047 } 00048 00049 return id; 00050 } 00051 00052 QObject* ObjectRegister::objectForId(int objectId) { 00053 return mRegisteredObjects.value(objectId); 00054 } 00055 00056 const QMetaObject* ObjectRegister::metaObjectForClassName( 00057 const QString& className) const { 00058 return mRegisteredMetaObjects.value(className); 00059 } 00060 00061 void ObjectRegister::clear() { 00062 mRegisteredIds.clear(); 00063 mRegisteredObjects.clear(); 00064 mRegisteredMetaObjects.clear(); 00065 } 00066 00067 void ObjectRegister::registerMetaObject(const QMetaObject* metaObject) { 00068 if (mRegisteredMetaObjects.contains(metaObject->className())) { 00069 return; 00070 } 00071 00072 mRegisteredMetaObjects.insert(metaObject->className(), metaObject); 00073 00074 if (metaObject->superClass()) { 00075 registerMetaObject(metaObject->superClass()); 00076 } 00077 } 00078 00079 //private slots: 00080 00081 void ObjectRegister::deregister(QObject* object) { 00082 int id = mRegisteredIds.value(object); 00083 mRegisteredIds.remove(object); 00084 mRegisteredObjects.remove(id); 00085 } 00086 00087 } 00088 }