lib

factory.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "factory.h"
00022 #include "property.h"
00023 #include "customproperty.h"
00024 
00025 #include "booledit.h"
00026 #include "combobox.h"
00027 #include "coloredit.h"
00028 #include "cursoredit.h"
00029 #include "dateedit.h"
00030 #include "datetimeedit.h"
00031 #include "dummywidget.h"
00032 #include "fontedit.h"
00033 #include "linestyleedit.h"
00034 #include "pixmapedit.h"
00035 #include "pointedit.h"
00036 #include "rectedit.h"
00037 #include "sizeedit.h"
00038 #include "sizepolicyedit.h"
00039 #include "spinbox.h"
00040 #include "stringlistedit.h"
00041 #include "stringedit.h"
00042 #include "symbolcombo.h"
00043 #include "timeedit.h"
00044 #include "urledit.h"
00045 
00046 #include <qvaluelist.h>
00047 #include <qintdict.h>
00048 
00049 #include <kdebug.h>
00050 
00051 static KStaticDeleter<KoProperty::FactoryManager> m_managerDeleter;
00052 static KoProperty::FactoryManager* m_manager = 0;
00053 
00054 namespace KoProperty {
00055 
00056 CustomPropertyFactory::CustomPropertyFactory(QObject *parent)
00057  : QObject(parent)
00058 {
00059 }
00060 
00061 CustomPropertyFactory::~CustomPropertyFactory()
00062 {
00063 }
00064 
00065 
00067 class FactoryManagerPrivate
00068 {
00069     public:
00070         FactoryManagerPrivate() {}
00071         ~FactoryManagerPrivate() {}
00072 
00073         //registered widgets for property types
00074         QIntDict<CustomPropertyFactory> registeredWidgets;
00075         QIntDict<CustomPropertyFactory> registeredCustomProperties;
00076 };
00077 }
00078 
00079 using namespace KoProperty;
00080 
00081 FactoryManager::FactoryManager()
00082 : QObject(0, "KoProperty::FactoryManager")
00083 {
00084     d = new FactoryManagerPrivate();
00085 }
00086 
00087 FactoryManager::~FactoryManager()
00088 {
00089     delete d;
00090 }
00091 
00092 FactoryManager*
00093 FactoryManager::self()
00094 {
00095     if(!m_manager)
00096         m_managerDeleter.setObject( m_manager, new FactoryManager() );
00097     return m_manager;
00098 }
00099 
00101 
00102 void
00103 FactoryManager::registerFactoryForEditor(int editorType, CustomPropertyFactory *widgetFactory)
00104 {
00105     if(!widgetFactory)
00106         return;
00107     if(d->registeredWidgets.find(editorType))
00108         kopropertywarn << "FactoryManager::registerFactoryForEditor(): "
00109         "Overriding already registered custom widget type \"" << editorType << "\"" << endl;
00110     d->registeredWidgets.replace(editorType, widgetFactory);
00111 }
00112 
00113 void
00114 FactoryManager::registerFactoryForEditors(const QValueList<int> &editorTypes, CustomPropertyFactory *factory)
00115 {
00116     QValueList<int>::ConstIterator endIt = editorTypes.constEnd();
00117     for(QValueList<int>::ConstIterator it = editorTypes.constBegin(); it != endIt; ++it)
00118         registerFactoryForEditor(*it, factory);
00119 }
00120 
00121 CustomPropertyFactory *
00122 FactoryManager::factoryForEditorType(int type)
00123 {
00124     return d->registeredWidgets.find(type);
00125 }
00126 
00127 Widget*
00128 FactoryManager::createWidgetForProperty(Property *property)
00129 {
00130     if(!property)
00131         return 0;
00132 
00133     const int type = property->type();
00134 
00135     CustomPropertyFactory *factory = d->registeredWidgets.find(type);
00136     if (factory)
00137         return factory->createCustomWidget(property);
00138 
00139     //handle combobox-based widgets:
00140     if (type==Cursor)
00141         return new CursorEdit(property);
00142 
00143     if (property->listData()) {
00144         return new ComboBox(property);
00145     }
00146 
00147     //handle other widget types:
00148     switch(type)
00149     {
00150         // Default QVariant types
00151         case String:
00152         case CString:
00153             return new StringEdit(property);
00154         case Rect_X:
00155         case Rect_Y:
00156         case Rect_Width:
00157         case Rect_Height:
00158         case Point_X:
00159         case Point_Y:
00160         case Size_Width:
00161         case Size_Height:
00162         case SizePolicy_HorStretch:
00163         case SizePolicy_VerStretch:
00164         case Integer:
00165             return new IntEdit(property);
00166         case Double:
00167             return new DoubleEdit(property);
00168         case Boolean: {
00169             //boolean editors can optionally accept 3rd state:
00170             QVariant thirdState = property ? property->option("3rdState") : QVariant();
00171             if (thirdState.toString().isEmpty())
00172                 return new BoolEdit(property);
00173             else
00174                 return new ThreeStateBoolEdit(property);
00175         }
00176         case Date:
00177             return new DateEdit(property);
00178         case Time:
00179             return new TimeEdit(property);
00180         case DateTime:
00181             return new DateTimeEdit(property);
00182         case StringList:
00183             return new StringListEdit(property);
00184         case Color:
00185             return new ColorButton(property);
00186         case Font:
00187             return new FontEdit(property);
00188         case Pixmap:
00189             return new PixmapEdit(property);
00190 
00191         // Other default types
00192         case Symbol:
00193             return new SymbolCombo(property);
00194         //case FontName:
00195         //  return new FontCombo(property);
00196         case FileURL:
00197         case DirectoryURL:
00198             return new URLEdit(property);
00199         case LineStyle:
00200             return new LineStyleEdit(property);
00201 
00202         // Composed types
00203         case Size:
00204             return new SizeEdit(property);
00205         case Point:
00206             return new PointEdit(property);
00207         case Rect:
00208             return new RectEdit(property);
00209         case SizePolicy:
00210             return new SizePolicyEdit(property);
00211 
00212         case List:
00213         case Map:
00214         default:
00215             kopropertywarn << "No editor for property " << property->name() << " of type " << property->type() << endl;
00216             return new DummyWidget(property);
00217     }
00218 }
00219 
00221 
00222 void
00223 FactoryManager::registerFactoryForProperty(int propertyType, CustomPropertyFactory *factory)
00224 {
00225     if(!factory)
00226         return;
00227     if(d->registeredCustomProperties.find(propertyType))
00228         kopropertywarn << "FactoryManager::registerFactoryForProperty(): "
00229         "Overriding already registered custom property type \"" << propertyType << "\"" << endl;
00230 
00231     d->registeredCustomProperties.replace(propertyType, factory);
00232 }
00233 
00234 void
00235 FactoryManager::registerFactoryForProperties(const QValueList<int> &propertyTypes, 
00236     CustomPropertyFactory *factory)
00237 {
00238     QValueList<int>::ConstIterator endIt = propertyTypes.constEnd();
00239     for(QValueList<int>::ConstIterator it = propertyTypes.constBegin(); it != endIt; ++it)
00240         registerFactoryForProperty(*it, factory);
00241 }
00242 
00243 CustomProperty*
00244 FactoryManager::createCustomProperty(Property *parent)
00245 {
00246     const int type = parent->type();
00247     CustomPropertyFactory *factory = d->registeredWidgets.find(type);
00248     if (factory)
00249         return factory->createCustomProperty(parent);
00250 
00251     switch(type) {
00252         case Size: case Size_Width: case Size_Height:
00253             return new SizeCustomProperty(parent);
00254         case Point: case Point_X: case Point_Y:
00255             return new PointCustomProperty(parent);
00256         case Rect: case Rect_X: case Rect_Y: case Rect_Width: case Rect_Height:
00257             return new RectCustomProperty(parent);
00258         case SizePolicy: case SizePolicy_HorStretch: case SizePolicy_VerStretch:
00259         case SizePolicy_HorData: case SizePolicy_VerData:
00260             return new SizePolicyCustomProperty(parent);
00261         default:
00262             return 0;
00263     }
00264 }
00265 
KDE Home | KDE Accessibility Home | Description of Access Keys