kexi

kexicelleditorfactory.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004-2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and,or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexicelleditorfactory.h"
21
22#include <tqptrdict.h>
23#include <tqintdict.h>
24#include <kstaticdeleter.h>
25
26#include <kexidb/indexschema.h>
27#include <kexidb/tableschema.h>
28#include "kexitableviewdata.h"
29#include "kexidatetableedit.h"
30#include "kexitimetableedit.h"
31#include "kexidatetimetableedit.h"
32#include "kexitableedit.h"
33#include "kexiinputtableedit.h"
34#include "kexicomboboxtableedit.h"
35#include "kexiblobtableedit.h"
36#include "kexibooltableedit.h"
37
38//============= KexiCellEditorFactoryItem ============
39
40KexiCellEditorFactoryItem::KexiCellEditorFactoryItem()
41{
42}
43
44KexiCellEditorFactoryItem::~KexiCellEditorFactoryItem()
45{
46}
47
48//============= KexiCellEditorFactoryPrivate ============
49
51class KexiCellEditorFactoryPrivate
52{
53 public:
54 KexiCellEditorFactoryPrivate()
55 : items(101)
56 , items_by_type(101, false)
57 {
58 items.setAutoDelete( true );
59 items_by_type.setAutoDelete( false );
60 }
61 ~KexiCellEditorFactoryPrivate() {}
62
63 TQString key(uint type, const TQString& subType) const
64 {
65 TQString key = TQString::number(type);
66 if (!subType.isEmpty())
67 key += (TQString(" ") + subType);
68 return key;
69 }
70
71 void registerItem( KexiCellEditorFactoryItem& item, uint type, const TQString& subType = TQString() )
72 {
73 if (!items[ &item ])
74 items.insert( &item, &item );
75
76 items_by_type.insert( key(type, subType), &item );
77 }
78
79 KexiCellEditorFactoryItem *findItem(uint type, const TQString& subType)
80 {
81 KexiCellEditorFactoryItem *item = items_by_type[ key(type, subType) ];
82 if (item)
83 return item;
84 item = items_by_type[ key(type, TQString()) ];
85 if (item)
86 return item;
87 return items_by_type[ key( KexiDB::Field::InvalidType, TQString() ) ];
88 }
89
90 TQPtrDict<KexiCellEditorFactoryItem> items;
91
92 TQDict<KexiCellEditorFactoryItem> items_by_type;
93};
94
95static KStaticDeleter<KexiCellEditorFactoryPrivate> KexiCellEditorFactory_deleter;
96static KexiCellEditorFactoryPrivate *KexiCellEditorFactory_static = 0;
97
98//============= KexiCellEditorFactory ============
99
100KexiCellEditorFactory::KexiCellEditorFactory()
101{
102}
103
104KexiCellEditorFactory::~KexiCellEditorFactory()
105{
106}
107
108
109// Initializes standard editor cell editor factories
110void KexiCellEditorFactory::init()
111{
112 if (KexiCellEditorFactory_static)
113 return;
114 KexiCellEditorFactory_deleter.setObject(KexiCellEditorFactory_static, new KexiCellEditorFactoryPrivate());
115
116 KexiCellEditorFactory_static->registerItem( *new KexiBlobEditorFactoryItem(), KexiDB::Field::BLOB );
117 KexiCellEditorFactory_static->registerItem( *new KexiDateEditorFactoryItem(), KexiDB::Field::Date );
118 KexiCellEditorFactory_static->registerItem( *new KexiTimeEditorFactoryItem(), KexiDB::Field::Time );
119 KexiCellEditorFactory_static->registerItem( *new KexiDateTimeEditorFactoryItem(), KexiDB::Field::DateTime );
120 KexiCellEditorFactory_static->registerItem( *new KexiComboBoxEditorFactoryItem(), KexiDB::Field::Enum );
121 KexiCellEditorFactory_static->registerItem( *new KexiBoolEditorFactoryItem(), KexiDB::Field::Boolean );
122 KexiCellEditorFactory_static->registerItem( *new KexiTDEIconTableEditorFactoryItem(), KexiDB::Field::Text, "TDEIcon" );
123 //default type
124 KexiCellEditorFactory_static->registerItem( *new KexiInputEditorFactoryItem(), KexiDB::Field::InvalidType );
125}
126
127void KexiCellEditorFactory::registerItem( KexiCellEditorFactoryItem& item, uint type, const TQString& subType )
128{
129 init();
130 KexiCellEditorFactory_static->registerItem( item, type, subType );
131}
132
133static bool hasEnumType( const KexiTableViewColumn &column )
134{
135 /*not db-aware case*/
136 if (column.relatedData())
137 return true;
138 /*db-aware case*/
139 if (!column.field() || !column.field()->table())
140 return false;
141 KexiDB::LookupFieldSchema *lookupFieldSchema = column.field()->table()->lookupFieldSchema( *column.field() );
142 if (!lookupFieldSchema)
143 return false;
144 if (lookupFieldSchema->rowSource().name().isEmpty())
145 return false;
146 return true;
147}
148
150{
151 init();
152 KexiDB::Field *realField;
153 if (column.visibleLookupColumnInfo) {
154 realField = column.visibleLookupColumnInfo->field;
155 }
156 else {
157 realField = column.field();
158 }
159
161
162 if (hasEnumType(column)) {
163 //--we need to create combo box because of relationship:
165 }
166 else {
167 item = KexiCellEditorFactory::item( realField->type(), realField->subType() );
168 }
169
170#if 0 //js: TODO LATER
171 //--check if we need to create combo box because of relationship:
172 //WARNING: it's assumed that indices are one-field long
173 KexiDB::TableSchema *table = f.table();
174 if (table) {
175 //find index that contain this field
176 KexiDB::IndexSchema::ListIterator it = table->indicesIterator();
177 for (;it.current();++it) {
178 KexiDB::IndexSchema *idx = it.current();
179 if (idx->fields()->findRef(&f)!=-1) {
180 //find details-side rel. for this index
181 KexiDB::Relationship *rel = idx->detailsRelationships()->first();
182 if (rel) {
183
184 }
185 }
186 }
187 }
188#endif
189
190 return item->createEditor(column, parent);
191}
192
193KexiCellEditorFactoryItem* KexiCellEditorFactory::item( uint type, const TQString& subType )
194{
195 init();
196 return KexiCellEditorFactory_static->findItem(type, subType);
197}
198
A base class for implementing cell editor factories.
static KexiTableEdit * createEditor(KexiTableViewColumn &column, TQWidget *parent=0)
static KexiCellEditorFactoryItem * item(uint type, const TQString &subType=TQString())
static void registerItem(KexiCellEditorFactoryItem &item, uint type, const TQString &subType=TQString())
Meta-data for a field.
Definition field.h:73
Type type() const
Definition field.cpp:134
@ InvalidType
Definition field.h:84
virtual TableSchema * table() const
Definition field.cpp:300
TQString subType() const
Definition field.h:318
Provides information about database index that can be created for a database table.
Definition indexschema.h:44
Relationship::List * detailsRelationships()
Definition indexschema.h:94
Provides information about lookup field's setup.
LookupFieldSchema * lookupFieldSchema(const Field &field) const
Abstract class for a cell editor. Handles cell painting and displaying the editor widget.
KexiDB::Field * field() const
KexiDB::QueryColumnInfo * visibleLookupColumnInfo
KexiTableViewData * relatedData() const