F:/KPlato/koffice/libs/koproperty/factory.cpp

Aller à la documentation de ce fichier.
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 #include "booledit.h"
00025 #include "combobox.h"
00026 #include "coloredit.h"
00027 #include "cursoredit.h"
00028 #include "dateedit.h"
00029 #include "datetimeedit.h"
00030 #include "dummywidget.h"
00031 #include "fontedit.h"
00032 #include "linestyleedit.h"
00033 #include "pixmapedit.h"
00034 #include "pointedit.h"
00035 #include "rectedit.h"
00036 #include "sizeedit.h"
00037 #include "sizepolicyedit.h"
00038 #include "spinbox.h"
00039 #include "stringlistedit.h"
00040 #include "stringedit.h"
00041 #include "symbolcombo.h"
00042 #include "timeedit.h"
00043 #include "urledit.h"
00044 
00045 #include <kdebug.h>
00046 
00047 #include <QHash>
00048 
00049 static KStaticDeleter<KoProperty::FactoryManager> m_managerDeleter;
00050 static KoProperty::FactoryManager* m_manager = 0;
00051 
00052 namespace KoProperty {
00053 
00054 CustomPropertyFactory::CustomPropertyFactory(QObject *parent)
00055  : QObject(parent)
00056 {
00057 }
00058 
00059 CustomPropertyFactory::~CustomPropertyFactory()
00060 {
00061 }
00062 
00063 
00065 class FactoryManagerPrivate
00066 {
00067         public:
00068                 FactoryManagerPrivate() {}
00069                 ~FactoryManagerPrivate() {}
00070 
00071                 //registered widgets for property types
00072                 QHash<int, CustomPropertyFactory*> registeredWidgets;
00073                 QHash<int, CustomPropertyFactory*> registeredCustomProperties;
00074 };
00075 }
00076 
00077 using namespace KoProperty;
00078 
00079 FactoryManager::FactoryManager()
00080 : QObject(0)
00081 {
00082         setObjectName("KoProperty::FactoryManager");
00083         d = new FactoryManagerPrivate();
00084 }
00085 
00086 FactoryManager::~FactoryManager()
00087 {
00088         delete d;
00089 }
00090 
00091 FactoryManager*
00092 FactoryManager::self()
00093 {
00094         if(!m_manager)
00095                 m_managerDeleter.setObject( m_manager, new FactoryManager() );
00096         return m_manager;
00097 }
00098 
00100 
00101 void
00102 FactoryManager::registerFactoryForEditor(int editorType, CustomPropertyFactory *widgetFactory)
00103 {
00104         if(!widgetFactory)
00105                 return;
00106         if(d->registeredWidgets.contains(editorType))
00107                 kopropertywarn << "FactoryManager::registerFactoryForEditor(): "
00108                 "Overriding already registered custom widget type \"" << editorType << "\"" << endl;
00109         d->registeredWidgets.insert(editorType, widgetFactory);
00110 }
00111 
00112 void
00113 FactoryManager::registerFactoryForEditors(const QList<int> &editorTypes, CustomPropertyFactory *factory)
00114 {
00115         QList<int>::ConstIterator endIt = editorTypes.constEnd();
00116         for(QList<int>::ConstIterator it = editorTypes.constBegin(); it != endIt; ++it)
00117                 registerFactoryForEditor(*it, factory);
00118 }
00119 
00120 CustomPropertyFactory *
00121 FactoryManager::factoryForEditorType(int type)
00122 {
00123         return d->registeredWidgets[type];
00124 }
00125 
00126 Widget*
00127 FactoryManager::createWidgetForProperty(Property *property)
00128 {
00129         if(!property)
00130                 return 0;
00131 
00132         const int type = property->type();
00133 
00134         CustomPropertyFactory *factory = d->registeredWidgets[type];
00135         if (factory)
00136                 return factory->createCustomWidget(property);
00137 
00138         //handle combobox-based widgets:
00139         if (type==Cursor)
00140                 return new CursorEdit(property);
00141 
00142         if (property->listData()) {
00143                 return new ComboBox(property);
00144         }
00145 
00146         //handle other widget types:
00147         switch(type)
00148         {
00149                 // Default QVariant types
00150                 case String:
00151                 case CString:
00152                         return new StringEdit(property);
00153                 case Rect_X:
00154                 case Rect_Y:
00155                 case Rect_Width:
00156                 case Rect_Height:
00157                 case Point_X:
00158                 case Point_Y:
00159                 case Size_Width:
00160                 case Size_Height:
00161                 case SizePolicy_HorStretch:
00162                 case SizePolicy_VerStretch:
00163                 case Integer:
00164                         return new IntEdit(property);
00165                 case Double:
00166                         return new DoubleEdit(property);
00167                 case Boolean: {
00168                         //boolean editors can optionally accept 3rd state:
00169                         QVariant thirdState = property->option("3rdState");
00170                         if (thirdState.toString().isEmpty())
00171                                 return new BoolEdit(property);
00172                         else
00173                                 return new ThreeStateBoolEdit(property);
00174                 }
00175                 case Date:
00176                         return new DateEdit(property);
00177                 case Time:
00178                         return new TimeEdit(property);
00179                 case DateTime:
00180                         return new DateTimeEdit(property);
00181                 case StringList:
00182                         return new StringListEdit(property);
00183                 case Color:
00184                         return new ColorButton(property);
00185                 case Font:
00186                         return new FontEdit(property);
00187                 case Pixmap:
00188                         return new PixmapEdit(property);
00189 
00190                 // Other default types
00191                 case Symbol:
00192                         return new SymbolCombo(property);
00193                 //case FontName:
00194                 //      return new FontCombo(property);
00195                 case FileURL:
00196                 case DirectoryURL:
00197                         return new URLEdit(property);
00198                 case LineStyle:
00199                         return new LineStyleEdit(property);
00200 
00201                 // Composed types
00202                 case Size:
00203                         return new SizeEdit(property);
00204                 case Point:
00205                         return new PointEdit(property);
00206                 case Rect:
00207                         return new RectEdit(property);
00208                 case SizePolicy:
00209                         return new SizePolicyEdit(property);
00210 
00211                 case List:
00212                 case Map:
00213                 default:
00214                         kopropertywarn << "No editor for property " << property->name() << " of type " << property->type() << endl;
00215                         return new DummyWidget(property);
00216         }
00217 }
00218 
00220 
00221 void
00222 FactoryManager::registerFactoryForProperty(int propertyType, CustomPropertyFactory *factory)
00223 {
00224         if(!factory)
00225                 return;
00226         if(d->registeredCustomProperties.contains(propertyType))
00227                 kopropertywarn << "FactoryManager::registerFactoryForProperty(): "
00228                 "Overriding already registered custom property type \"" << propertyType << "\"" << endl;
00229         
00230         delete d->registeredCustomProperties[ propertyType ];
00231         d->registeredCustomProperties.insert(propertyType, factory);
00232 }
00233 
00234 void
00235 FactoryManager::registerFactoryForProperties(const QList<int> &propertyTypes, 
00236         CustomPropertyFactory *factory)
00237 {
00238         QList<int>::ConstIterator endIt = propertyTypes.constEnd();
00239         for(QList<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[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 

Généré le Wed Nov 22 23:41:06 2006 pour KPlato par  doxygen 1.5.1-p1