kexi
kexicelleditorfactory.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kexicelleditorfactory.h"
00021
00022 #include <qptrdict.h>
00023 #include <qintdict.h>
00024 #include <kstaticdeleter.h>
00025
00026 #include <kexidb/indexschema.h>
00027 #include <kexidb/tableschema.h>
00028 #include "kexitableviewdata.h"
00029 #include "kexidatetableedit.h"
00030 #include "kexitimetableedit.h"
00031 #include "kexidatetimetableedit.h"
00032 #include "kexitableedit.h"
00033 #include "kexiinputtableedit.h"
00034 #include "kexicomboboxtableedit.h"
00035 #include "kexiblobtableedit.h"
00036 #include "kexibooltableedit.h"
00037
00038
00039
00040 KexiCellEditorFactoryItem::KexiCellEditorFactoryItem()
00041 {
00042 }
00043
00044 KexiCellEditorFactoryItem::~KexiCellEditorFactoryItem()
00045 {
00046 }
00047
00048
00049
00051 class KexiCellEditorFactoryPrivate
00052 {
00053 public:
00054 KexiCellEditorFactoryPrivate()
00055 : items(101)
00056 , items_by_type(101, false)
00057 {
00058 items.setAutoDelete( true );
00059 items_by_type.setAutoDelete( false );
00060 }
00061 ~KexiCellEditorFactoryPrivate() {}
00062
00063 QString key(uint type, const QString& subType) const
00064 {
00065 QString key = QString::number(type);
00066 if (!subType.isEmpty())
00067 key += (QString(" ") + subType);
00068 return key;
00069 }
00070
00071 void registerItem( KexiCellEditorFactoryItem& item, uint type, const QString& subType = QString::null )
00072 {
00073 if (!items[ &item ])
00074 items.insert( &item, &item );
00075
00076 items_by_type.insert( key(type, subType), &item );
00077 }
00078
00079 KexiCellEditorFactoryItem *findItem(uint type, const QString& subType)
00080 {
00081 KexiCellEditorFactoryItem *item = items_by_type[ key(type, subType) ];
00082 if (item)
00083 return item;
00084 item = items_by_type[ key(type, QString::null) ];
00085 if (item)
00086 return item;
00087 return items_by_type[ key( KexiDB::Field::InvalidType, QString::null ) ];
00088 }
00089
00090 QPtrDict<KexiCellEditorFactoryItem> items;
00091
00092 QDict<KexiCellEditorFactoryItem> items_by_type;
00093 };
00094
00095 static KStaticDeleter<KexiCellEditorFactoryPrivate> KexiCellEditorFactory_deleter;
00096 static KexiCellEditorFactoryPrivate *KexiCellEditorFactory_static = 0;
00097
00098
00099
00100 KexiCellEditorFactory::KexiCellEditorFactory()
00101 {
00102 }
00103
00104 KexiCellEditorFactory::~KexiCellEditorFactory()
00105 {
00106 }
00107
00108
00109
00110 void KexiCellEditorFactory::init()
00111 {
00112 if (KexiCellEditorFactory_static)
00113 return;
00114 KexiCellEditorFactory_deleter.setObject(KexiCellEditorFactory_static, new KexiCellEditorFactoryPrivate());
00115
00116 KexiCellEditorFactory_static->registerItem( *new KexiBlobEditorFactoryItem(), KexiDB::Field::BLOB );
00117 KexiCellEditorFactory_static->registerItem( *new KexiDateEditorFactoryItem(), KexiDB::Field::Date );
00118 KexiCellEditorFactory_static->registerItem( *new KexiTimeEditorFactoryItem(), KexiDB::Field::Time );
00119 KexiCellEditorFactory_static->registerItem( *new KexiDateTimeEditorFactoryItem(), KexiDB::Field::DateTime );
00120 KexiCellEditorFactory_static->registerItem( *new KexiComboBoxEditorFactoryItem(), KexiDB::Field::Enum );
00121 KexiCellEditorFactory_static->registerItem( *new KexiBoolEditorFactoryItem(), KexiDB::Field::Boolean );
00122 KexiCellEditorFactory_static->registerItem( *new KexiKIconTableEditorFactoryItem(), KexiDB::Field::Text, "KIcon" );
00123
00124 KexiCellEditorFactory_static->registerItem( *new KexiInputEditorFactoryItem(), KexiDB::Field::InvalidType );
00125 }
00126
00127 void KexiCellEditorFactory::registerItem( KexiCellEditorFactoryItem& item, uint type, const QString& subType )
00128 {
00129 init();
00130 KexiCellEditorFactory_static->registerItem( item, type, subType );
00131 }
00132
00133 static bool hasEnumType( const KexiTableViewColumn &column )
00134 {
00135
00136 if (column.relatedData())
00137 return true;
00138
00139 if (!column.field() || !column.field()->table())
00140 return false;
00141 KexiDB::LookupFieldSchema *lookupFieldSchema = column.field()->table()->lookupFieldSchema( *column.field() );
00142 if (!lookupFieldSchema)
00143 return false;
00144 if (lookupFieldSchema->rowSource().name().isEmpty())
00145 return false;
00146 return true;
00147 }
00148
00149 KexiTableEdit* KexiCellEditorFactory::createEditor(KexiTableViewColumn &column, QWidget* parent)
00150 {
00151 init();
00152 KexiDB::Field *realField;
00153 if (column.visibleLookupColumnInfo) {
00154 realField = column.visibleLookupColumnInfo->field;
00155 }
00156 else {
00157 realField = column.field();
00158 }
00159
00160 KexiCellEditorFactoryItem *item = 0;
00161
00162 if (hasEnumType(column)) {
00163
00164 item = KexiCellEditorFactory::item( KexiDB::Field::Enum );
00165 }
00166 else {
00167 item = KexiCellEditorFactory::item( realField->type(), realField->subType() );
00168 }
00169
00170 #if 0 //js: TODO LATER
00171
00172
00173 KexiDB::TableSchema *table = f.table();
00174 if (table) {
00175
00176 KexiDB::IndexSchema::ListIterator it = table->indicesIterator();
00177 for (;it.current();++it) {
00178 KexiDB::IndexSchema *idx = it.current();
00179 if (idx->fields()->findRef(&f)!=-1) {
00180
00181 KexiDB::Relationship *rel = idx->detailsRelationships()->first();
00182 if (rel) {
00183
00184 }
00185 }
00186 }
00187 }
00188 #endif
00189
00190 return item->createEditor(column, parent);
00191 }
00192
00193 KexiCellEditorFactoryItem* KexiCellEditorFactory::item( uint type, const QString& subType )
00194 {
00195 init();
00196 return KexiCellEditorFactory_static->findItem(type, subType);
00197 }
00198
|