00001 /*************************************************************************** 00002 * file klffactory.cpp 00003 * This file is part of the KLatexFormula Project. 00004 * Copyright (C) 2010 by Philippe Faist 00005 * philippe.faist at bluewin.ch 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 ***************************************************************************/ 00022 /* $Id: klffactory.cpp 442 2010-08-13 17:34:38Z philippe $ */ 00023 00024 #include <QDebug> 00025 #include <QStringList> 00026 00027 #include "klffactory.h" 00028 00029 00030 KLFFactoryBase::KLFFactoryBase(KLFFactoryManager *factoryManager) 00031 : pFactoryManager(factoryManager) 00032 { 00033 pFactoryManager->registerFactory(this); 00034 } 00035 KLFFactoryBase::~KLFFactoryBase() 00036 { 00037 pFactoryManager->unRegisterFactory(this); 00038 } 00039 00040 KLFFactoryManager::KLFFactoryManager() 00041 { 00042 } 00043 KLFFactoryManager::~KLFFactoryManager() 00044 { 00045 } 00046 00047 KLFFactoryBase * KLFFactoryManager::findFactoryFor(const QString& objType) 00048 { 00049 int k; 00050 for (k = 0; k < pRegisteredFactories.size(); ++k) { 00051 KLFFactoryBase * factory = pRegisteredFactories[k]; 00052 if (factory->supportedTypes().contains(objType)) { 00053 return factory; 00054 } 00055 } 00056 qWarning() 00057 <<"KLFFactoryManager::findFactoryFor(object type="<<objType<<"): No factory found!"; 00058 00059 return NULL; 00060 } 00061 00062 QStringList KLFFactoryManager::allSupportedTypes() 00063 { 00064 QStringList objtypes; 00065 int k; 00066 for (k = 0; k < pRegisteredFactories.size(); ++k) { 00067 objtypes << pRegisteredFactories[k]->supportedTypes(); 00068 } 00069 return objtypes; 00070 } 00071 00072 void KLFFactoryManager::registerFactory(KLFFactoryBase *factory) 00073 { 00074 if (pRegisteredFactories.indexOf(factory) != -1) { 00075 qWarning()<<"KLFFactory<>::registerFactory(): Factory " << factory << " is already registered!"; 00076 return; 00077 } 00078 pRegisteredFactories.prepend(factory); 00079 } 00080 00081 void KLFFactoryManager::unRegisterFactory(KLFFactoryBase *factory) 00082 { 00083 if (pRegisteredFactories.indexOf(factory) == -1) { 00084 qWarning()<<"KLFFactory<>::unRegisterFactory(): Factory "<<factory<<" is not registered!"; 00085 return; 00086 } 00087 pRegisteredFactories.removeAll(factory); 00088 } 00089 00090