factory.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "factory.h"
22#include "property.h"
23#include "customproperty.h"
24
25#include "booledit.h"
26#include "combobox.h"
27#include "coloredit.h"
28#include "cursoredit.h"
29#include "dateedit.h"
30#include "datetimeedit.h"
31#include "dummywidget.h"
32#include "fontedit.h"
33#include "linestyleedit.h"
34#include "pixmapedit.h"
35#include "pointedit.h"
36#include "rectedit.h"
37#include "sizeedit.h"
38#include "sizepolicyedit.h"
39#include "spinbox.h"
40#include "stringlistedit.h"
41#include "stringedit.h"
42#include "symbolcombo.h"
43#include "timeedit.h"
44#include "urledit.h"
45
46#include <tqvaluelist.h>
47#include <tqintdict.h>
48
49#include <kdebug.h>
50
51static KStaticDeleter<KoProperty::FactoryManager> m_managerDeleter;
52static KoProperty::FactoryManager* m_manager = 0;
53
54namespace KoProperty {
55
56CustomPropertyFactory::CustomPropertyFactory(TQObject *parent)
57 : TQObject(parent)
58{
59}
60
61CustomPropertyFactory::~CustomPropertyFactory()
62{
63}
64
65
67class FactoryManagerPrivate
68{
69 public:
70 FactoryManagerPrivate() {}
71 ~FactoryManagerPrivate() {}
72
73 //registered widgets for property types
74 TQIntDict<CustomPropertyFactory> registeredWidgets;
75 TQIntDict<CustomPropertyFactory> registeredCustomProperties;
76};
77}
78
79using namespace KoProperty;
80
81FactoryManager::FactoryManager()
82: TQObject(0, "KoProperty::FactoryManager")
83{
84 d = new FactoryManagerPrivate();
85}
86
87FactoryManager::~FactoryManager()
88{
89 delete d;
90}
91
94{
95 if(!m_manager)
96 m_managerDeleter.setObject( m_manager, new FactoryManager() );
97 return m_manager;
98}
99
101
102void
103FactoryManager::registerFactoryForEditor(int editorType, CustomPropertyFactory *widgetFactory)
104{
105 if(!widgetFactory)
106 return;
107 if(d->registeredWidgets.find(editorType))
108 kopropertywarn << "FactoryManager::registerFactoryForEditor(): "
109 "Overriding already registered custom widget type \"" << editorType << "\"" << endl;
110 d->registeredWidgets.replace(editorType, widgetFactory);
111}
112
113void
114FactoryManager::registerFactoryForEditors(const TQValueList<int> &editorTypes, CustomPropertyFactory *factory)
115{
116 TQValueList<int>::ConstIterator endIt = editorTypes.constEnd();
117 for(TQValueList<int>::ConstIterator it = editorTypes.constBegin(); it != endIt; ++it)
118 registerFactoryForEditor(*it, factory);
119}
120
121CustomPropertyFactory *
123{
124 return d->registeredWidgets.find(type);
125}
126
127Widget*
129{
130 if(!property)
131 return 0;
132
133 const int type = property->type();
134
135 CustomPropertyFactory *factory = d->registeredWidgets.find(type);
136 if (factory)
137 return factory->createCustomWidget(property);
138
139 //handle combobox-based widgets:
140 if (type==Cursor)
141 return new CursorEdit(property);
142
143 if (property->listData()) {
144 return new ComboBox(property);
145 }
146
147 //handle other widget types:
148 switch(type)
149 {
150 // Default TQVariant types
151 case String:
152 case CString:
153 return new StringEdit(property);
154 case Rect_X:
155 case Rect_Y:
156 case Rect_Width:
157 case Rect_Height:
158 case Point_X:
159 case Point_Y:
160 case Size_Width:
161 case Size_Height:
162 case SizePolicy_HorStretch:
163 case SizePolicy_VerStretch:
164 case Integer:
165 return new IntEdit(property);
166 case Double:
167 return new DoubleEdit(property);
168 case Boolean: {
169 //boolean editors can optionally accept 3rd state:
170 TQVariant thirdState = property ? property->option("3rdState") : TQVariant();
171 if (thirdState.toString().isEmpty())
172 return new BoolEdit(property);
173 else
174 return new ThreeStateBoolEdit(property);
175 }
176 case Date:
177 return new DateEdit(property);
178 case Time:
179 return new TimeEdit(property);
180 case DateTime:
181 return new DateTimeEdit(property);
182 case StringList:
183 return new StringListEdit(property);
184 case Color:
185 return new ColorButton(property);
186 case Font:
187 return new FontEdit(property);
188 case Pixmap:
189 return new PixmapEdit(property);
190
191 // Other default types
192 case Symbol:
193 return new SymbolCombo(property);
194 //case FontName:
195 // return new FontCombo(property);
196 case FileURL:
197 case DirectoryURL:
198 return new URLEdit(property);
199 case LineStyle:
200 return new LineStyleEdit(property);
201
202 // Composed types
203 case Size:
204 return new SizeEdit(property);
205 case Point:
206 return new PointEdit(property);
207 case Rect:
208 return new RectEdit(property);
209 case SizePolicy:
210 return new SizePolicyEdit(property);
211
212 case List:
213 case Map:
214 default:
215 kopropertywarn << "No editor for property " << property->name() << " of type " << property->type() << endl;
216 return new DummyWidget(property);
217 }
218}
219
221
222void
223FactoryManager::registerFactoryForProperty(int propertyType, CustomPropertyFactory *factory)
224{
225 if(!factory)
226 return;
227 if(d->registeredCustomProperties.find(propertyType))
228 kopropertywarn << "FactoryManager::registerFactoryForProperty(): "
229 "Overriding already registered custom property type \"" << propertyType << "\"" << endl;
230
231 d->registeredCustomProperties.replace(propertyType, factory);
232}
233
234void
235FactoryManager::registerFactoryForProperties(const TQValueList<int> &propertyTypes,
236 CustomPropertyFactory *factory)
237{
238 TQValueList<int>::ConstIterator endIt = propertyTypes.constEnd();
239 for(TQValueList<int>::ConstIterator it = propertyTypes.constBegin(); it != endIt; ++it)
240 registerFactoryForProperty(*it, factory);
241}
242
245{
246 const int type = parent->type();
247 CustomPropertyFactory *factory = d->registeredWidgets.find(type);
248 if (factory)
249 return factory->createCustomProperty(parent);
250
251 switch(type) {
252 case Size: case Size_Width: case Size_Height:
253 return new SizeCustomProperty(parent);
254 case Point: case Point_X: case Point_Y:
255 return new PointCustomProperty(parent);
256 case Rect: case Rect_X: case Rect_Y: case Rect_Width: case Rect_Height:
257 return new RectCustomProperty(parent);
258 case SizePolicy: case SizePolicy_HorStretch: case SizePolicy_VerStretch:
259 case SizePolicy_HorData: case SizePolicy_VerData:
260 return new SizePolicyCustomProperty(parent);
261 default:
262 return 0;
263 }
264}
265
Base class for custom properties.
Manages factories providing custom editors and properties.
Definition factory.h:114
void registerFactoryForProperties(const TQValueList< int > &propertyTypes, CustomPropertyFactory *factory)
Definition factory.cpp:235
Widget * createWidgetForProperty(Property *property)
Definition factory.cpp:128
static FactoryManager * self()
Definition factory.cpp:93
void registerFactoryForEditors(const TQValueList< int > &editorTypes, CustomPropertyFactory *factory)
Definition factory.cpp:114
void registerFactoryForEditor(int editorType, CustomPropertyFactory *factory)
Definition factory.cpp:103
CustomProperty * createCustomProperty(Property *parent)
Definition factory.cpp:244
CustomPropertyFactory * factoryForEditorType(int type)
Definition factory.cpp:122
void registerFactoryForProperty(int propertyType, CustomPropertyFactory *factory)
Definition factory.cpp:223
Custom property implementation for TQPoint type.
The base class representing a single property.
Definition property.h:177
ListData * listData() const
Definition property.cpp:428
Custom property implementation for TQRect type.
Custom property implementation for TQSize type.
Custom property implementation for TQSizePolicy type.
The base class for all item editors used in Editor.
Definition widget.h:37
Namespace for a set of classes implementing generic properties framework.
@ DateTime
date and time
Definition property.h:96
@ Point
point (x,y)
Definition property.h:82
@ String
string
Definition property.h:71
@ Boolean
boolean
Definition property.h:86
@ Time
time
Definition property.h:95
@ List
TQValueList<TQVariant>
Definition property.h:70
@ LineStyle
line style
Definition property.h:112
@ Pixmap
pixmap
Definition property.h:74
@ Symbol
unicode symbol code
Definition property.h:107
@ Rect
rectangle (x,y, width, height)
Definition property.h:76
@ DirectoryURL
url of a directory
Definition property.h:111
@ Size
size (width, height)
Definition property.h:77
@ Font
font
Definition property.h:73
@ FileURL
url of a file
Definition property.h:109
@ Color
color
Definition property.h:78
@ Integer
integer
Definition property.h:84
@ StringList
string list
Definition property.h:72
@ Date
date
Definition property.h:94
@ SizePolicy
size policy (horizontal, vertical)
Definition property.h:93
@ Cursor
cursor
Definition property.h:92
@ Map
TQStringVariantMap.
Definition property.h:69
@ Double
double
Definition property.h:87