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 #ifdef QT_ONLY
00050 #else
00051 #include <kdebug.h>
00052 #endif
00053 
00054 static KStaticDeleter<KoProperty::FactoryManager> m_managerDeleter;
00055 static KoProperty::FactoryManager* m_manager = 0;
00056 
00057 namespace KoProperty {
00058 
00059 CustomPropertyFactory::CustomPropertyFactory(QObject *parent)
00060  : QObject(parent)
00061 {
00062 }
00063 
00064 CustomPropertyFactory::~CustomPropertyFactory()
00065 {
00066 }
00067 
00068 
00070 class FactoryManagerPrivate
00071 {
00072     public:
00073         FactoryManagerPrivate() {}
00074         ~FactoryManagerPrivate() {}
00075 
00076         //registered widgets for property types
00077         QIntDict<CustomPropertyFactory> registeredWidgets;
00078         QIntDict<CustomPropertyFactory> registeredCustomProperties;
00079 };
00080 }
00081 
00082 using namespace KoProperty;
00083 
00084 FactoryManager::FactoryManager()
00085 : QObject(0, "KoProperty::FactoryManager")
00086 {
00087     d = new FactoryManagerPrivate();
00088 }
00089 
00090 FactoryManager::~FactoryManager()
00091 {
00092     delete d;
00093 }
00094 
00095 FactoryManager*
00096 FactoryManager::self()
00097 {
00098     if(!m_manager)
00099         m_managerDeleter.setObject( m_manager, new FactoryManager() );
00100     return m_manager;
00101 }
00102 
00104 
00105 void
00106 FactoryManager::registerFactoryForEditor(int editorType, CustomPropertyFactory *widgetFactory)
00107 {
00108     if(!widgetFactory)
00109         return;
00110     if(d->registeredWidgets.find(editorType))
00111         kopropertywarn << "FactoryManager::registerFactoryForEditor(): "
00112         "Overriding already registered custom widget type \"" << editorType << "\"" << endl;
00113     d->registeredWidgets.replace(editorType, widgetFactory);
00114 }
00115 
00116 void
00117 FactoryManager::registerFactoryForEditors(const QValueList<int> &editorTypes, CustomPropertyFactory *factory)
00118 {
00119     QValueList<int>::ConstIterator endIt = editorTypes.constEnd();
00120     for(QValueList<int>::ConstIterator it = editorTypes.constBegin(); it != endIt; ++it)
00121         registerFactoryForEditor(*it, factory);
00122 }
00123 
00124 CustomPropertyFactory *
00125 FactoryManager::factoryForEditorType(int type)
00126 {
00127     return d->registeredWidgets.find(type);
00128 }
00129 
00130 Widget*
00131 FactoryManager::createWidgetForProperty(Property *property)
00132 {
00133     if(!property)
00134         return 0;
00135 
00136     const int type = property->type();
00137 
00138     CustomPropertyFactory *factory = d->registeredWidgets.find(type);
00139     if (factory)
00140         return factory->createCustomWidget(property);
00141 
00142     //handle combobox-based widgets:
00143     if (type==Cursor)
00144         return new CursorEdit(property);
00145 
00146     if (property->listData()) {
00147         return new ComboBox(property);
00148     }
00149 
00150     //handle other widget types:
00151     switch(type)
00152     {
00153         // Default QVariant types
00154         case String:
00155         case CString:
00156             return new StringEdit(property);
00157         case Rect_X:
00158         case Rect_Y:
00159         case Rect_Width:
00160         case Rect_Height:
00161         case Point_X:
00162         case Point_Y:
00163         case Size_Width:
00164         case Size_Height:
00165         case SizePolicy_HorStretch:
00166         case SizePolicy_VerStretch:
00167         case Integer:
00168             return new IntEdit(property);
00169         case Double:
00170             return new DoubleEdit(property);
00171         case Boolean:
00172             return new BoolEdit(property);
00173         case Date:
00174             return new DateEdit(property);
00175         case Time:
00176             return new TimeEdit(property);
00177         case DateTime:
00178             return new DateTimeEdit(property);
00179         case StringList:
00180             return new StringListEdit(property);
00181         case Color:
00182             return new ColorButton(property);
00183         case Font:
00184             return new FontEdit(property);
00185         case Pixmap:
00186             return new PixmapEdit(property);
00187 
00188         // Other default types
00189         case Symbol:
00190             return new SymbolCombo(property);
00191         //case FontName:
00192         //  return new FontCombo(property);
00193         case FileURL:
00194         case DirectoryURL:
00195             return new URLEdit(property);
00196         case LineStyle:
00197             return new LineStyleEdit(property);
00198 
00199         // Composed types
00200         case Size:
00201             return new SizeEdit(property);
00202         case Point:
00203             return new PointEdit(property);
00204         case Rect:
00205             return new RectEdit(property);
00206         case SizePolicy:
00207             return new SizePolicyEdit(property);
00208 
00209         case List:
00210         case Map:
00211         default:
00212             kopropertywarn << "No editor for property " << property->name() << " of type " << property->type() << endl;
00213             return new DummyWidget(property);
00214     }
00215 }
00216 
00218 
00219 void
00220 FactoryManager::registerFactoryForProperty(int propertyType, CustomPropertyFactory *factory)
00221 {
00222     if(!factory)
00223         return;
00224     if(d->registeredCustomProperties.find(propertyType))
00225         kopropertywarn << "FactoryManager::registerFactoryForProperty(): "
00226         "Overriding already registered custom property type \"" << propertyType << "\"" << endl;
00227 
00228     d->registeredCustomProperties.replace(propertyType, factory);
00229 }
00230 
00231 void
00232 FactoryManager::registerFactoryForProperties(const QValueList<int> &propertyTypes, 
00233     CustomPropertyFactory *factory)
00234 {
00235     QValueList<int>::ConstIterator endIt = propertyTypes.constEnd();
00236     for(QValueList<int>::ConstIterator it = propertyTypes.constBegin(); it != endIt; ++it)
00237         registerFactoryForProperty(*it, factory);
00238 }
00239 
00240 CustomProperty*
00241 FactoryManager::createCustomProperty(Property *parent)
00242 {
00243     const int type = parent->type();
00244     CustomPropertyFactory *factory = d->registeredWidgets.find(type);
00245     if (factory)
00246         return factory->createCustomProperty(parent);
00247 
00248     switch(type) {
00249         case Size: case Size_Width: case Size_Height:
00250             return new SizeCustomProperty(parent);
00251         case Point: case Point_X: case Point_Y:
00252             return new PointCustomProperty(parent);
00253         case Rect: case Rect_X: case Rect_Y: case Rect_Width: case Rect_Height:
00254             return new RectCustomProperty(parent);
00255         case SizePolicy: case SizePolicy_HorStretch: case SizePolicy_VerStretch:
00256         case SizePolicy_HorData: case SizePolicy_VerData:
00257             return new SizePolicyCustomProperty(parent);
00258         default:
00259             return 0;
00260     }
00261 }
00262 
KDE Home | KDE Accessibility Home | Description of Access Keys