kexi

kexicelleditorfactory.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2007 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and,or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 //============= KexiCellEditorFactoryItem ============
00039 
00040 KexiCellEditorFactoryItem::KexiCellEditorFactoryItem()
00041 {
00042 }
00043 
00044 KexiCellEditorFactoryItem::~KexiCellEditorFactoryItem()
00045 {
00046 }
00047 
00048 //============= KexiCellEditorFactoryPrivate ============
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 //============= KexiCellEditorFactory ============
00099 
00100 KexiCellEditorFactory::KexiCellEditorFactory()
00101 {
00102 }
00103 
00104 KexiCellEditorFactory::~KexiCellEditorFactory()
00105 {
00106 }
00107 
00108 
00109 // Initializes standard editor cell editor factories
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     //default type
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     /*not db-aware case*/
00136     if (column.relatedData())
00137         return true;
00138     /*db-aware case*/
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         //--we need to create combo box because of relationship:
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     //--check if we need to create combo box because of relationship:
00172     //WARNING: it's assumed that indices are one-field long
00173     KexiDB::TableSchema *table = f.table();
00174     if (table) {
00175         //find index that contain this field
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                 //find details-side rel. for this index
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 
KDE Home | KDE Accessibility Home | Description of Access Keys