summaryrefslogtreecommitdiffstats
path: root/kexi/widget/tableview/kexicelleditorfactory.cpp
blob: 9cfcfe27381b5c437d432533e3d0411793192894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* This file is part of the KDE project
   Copyright (C) 2004-2007 Jaroslaw Staniek <[email protected]>

   This program is free software; you can redistribute it and,or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexicelleditorfactory.h"

#include <tqptrdict.h>
#include <tqintdict.h>
#include <kstaticdeleter.h>

#include <kexidb/indexschema.h>
#include <kexidb/tableschema.h>
#include "kexitableviewdata.h"
#include "kexidatetableedit.h"
#include "kexitimetableedit.h"
#include "kexidatetimetableedit.h"
#include "kexitableedit.h"
#include "kexiinputtableedit.h"
#include "kexicomboboxtableedit.h"
#include "kexiblobtableedit.h"
#include "kexibooltableedit.h"

//============= KexiCellEditorFactoryItem ============

KexiCellEditorFactoryItem::KexiCellEditorFactoryItem()
{
}

KexiCellEditorFactoryItem::~KexiCellEditorFactoryItem()
{
}

//============= KexiCellEditorFactoryPrivate ============

//! @internal
class KexiCellEditorFactoryPrivate
{
	public:
		KexiCellEditorFactoryPrivate()
		 : items(101)
		 , items_by_type(101, false)
		{
			items.setAutoDelete( true );
			items_by_type.setAutoDelete( false );
		}
		~KexiCellEditorFactoryPrivate() {}

		TQString key(uint type, const TQString& subType) const
		{
			TQString key = TQString::number(type);
			if (!subType.isEmpty())
				key += (TQString(" ") + subType);
			return key;
		}

		void registerItem( KexiCellEditorFactoryItem& item, uint type, const TQString& subType = TQString() )
		{
			if (!items[ &item ])
				items.insert( &item, &item );

			items_by_type.insert( key(type, subType), &item );
		}
		
		KexiCellEditorFactoryItem *findItem(uint type, const TQString& subType)
		{
			KexiCellEditorFactoryItem *item = items_by_type[ key(type, subType) ];
			if (item)
				return item;
			item = items_by_type[ key(type, TQString()) ];
			if (item)
				return item;
			return items_by_type[ key( KexiDB::Field::InvalidType, TQString() ) ];
		}

		TQPtrDict<KexiCellEditorFactoryItem> items; //!< list of editor factory items (for later destroy)

		TQDict<KexiCellEditorFactoryItem> items_by_type; //!< editor factory items accessed by a key
};

static KStaticDeleter<KexiCellEditorFactoryPrivate> KexiCellEditorFactory_deleter;
static KexiCellEditorFactoryPrivate *KexiCellEditorFactory_static = 0;

//============= KexiCellEditorFactory ============

KexiCellEditorFactory::KexiCellEditorFactory()
{
}

KexiCellEditorFactory::~KexiCellEditorFactory()
{
}


// Initializes standard editor cell editor factories
void KexiCellEditorFactory::init()
{
	if (KexiCellEditorFactory_static)
		return;
	KexiCellEditorFactory_deleter.setObject(KexiCellEditorFactory_static, new KexiCellEditorFactoryPrivate());

	KexiCellEditorFactory_static->registerItem( *new KexiBlobEditorFactoryItem(), KexiDB::Field::BLOB );
	KexiCellEditorFactory_static->registerItem( *new KexiDateEditorFactoryItem(), KexiDB::Field::Date );
	KexiCellEditorFactory_static->registerItem( *new KexiTimeEditorFactoryItem(), KexiDB::Field::Time );
	KexiCellEditorFactory_static->registerItem( *new KexiDateTimeEditorFactoryItem(), KexiDB::Field::DateTime );
	KexiCellEditorFactory_static->registerItem( *new KexiComboBoxEditorFactoryItem(), KexiDB::Field::Enum );
	KexiCellEditorFactory_static->registerItem( *new KexiBoolEditorFactoryItem(), KexiDB::Field::Boolean );
	KexiCellEditorFactory_static->registerItem( *new KexiKIconTableEditorFactoryItem(), KexiDB::Field::Text, "KIcon" );
	//default type
	KexiCellEditorFactory_static->registerItem( *new KexiInputEditorFactoryItem(), KexiDB::Field::InvalidType );
}

void KexiCellEditorFactory::registerItem( KexiCellEditorFactoryItem& item, uint type, const TQString& subType )
{
	init();
	KexiCellEditorFactory_static->registerItem( item, type, subType );
}

static bool hasEnumType( const KexiTableViewColumn &column )
{
	/*not db-aware case*/
	if (column.relatedData())
		return true;
	/*db-aware case*/
	if (!column.field() || !column.field()->table())
		return false;
	KexiDB::LookupFieldSchema *lookupFieldSchema = column.field()->table()->lookupFieldSchema( *column.field() );
	if (!lookupFieldSchema)
		return false;
	if (lookupFieldSchema->rowSource().name().isEmpty())
		return false;
	return true;
}

KexiTableEdit* KexiCellEditorFactory::createEditor(KexiTableViewColumn &column, TQWidget* parent)
{
	init();
	KexiDB::Field *realField;
	if (column.visibleLookupColumnInfo) {
		realField = column.visibleLookupColumnInfo->field;
	}
	else {
		realField = column.field();
	}

	KexiCellEditorFactoryItem *item = 0;

	if (hasEnumType(column)) {
		//--we need to create combo box because of relationship:
		item = KexiCellEditorFactory::item( KexiDB::Field::Enum );
	}
	else {
		item = KexiCellEditorFactory::item( realField->type(), realField->subType() );
	}
	
#if 0 //js: TODO LATER
	//--check if we need to create combo box because of relationship:
	//WARNING: it's assumed that indices are one-field long
	KexiDB::TableSchema *table = f.table();
	if (table) {
		//find index that contain this field
		KexiDB::IndexSchema::ListIterator it = table->indicesIterator();
		for (;it.current();++it) {
			KexiDB::IndexSchema *idx = it.current();
			if (idx->fields()->findRef(&f)!=-1) {
				//find details-side rel. for this index
				KexiDB::Relationship *rel = idx->detailsRelationships()->first();
				if (rel) {
					
				}
			}
		}
	}
#endif

	return item->createEditor(column, parent);
}

KexiCellEditorFactoryItem* KexiCellEditorFactory::item( uint type, const TQString& subType )
{
	init();
	return KexiCellEditorFactory_static->findItem(type, subType);
}