kexi

kexicelleditorfactory.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 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 
00025 #include <kexidb/indexschema.h>
00026 #include <kexidb/tableschema.h>
00027 #include "kexitableviewdata.h"
00028 
00029 //============= KexiCellEditorFactoryItem ============
00030 
00031 KexiCellEditorFactoryItem::KexiCellEditorFactoryItem()
00032 {
00033 }
00034 
00035 KexiCellEditorFactoryItem::~KexiCellEditorFactoryItem()
00036 {
00037 }
00038 
00039 //============= KexiCellEditorFactoryPrivate ============
00040 
00042 class KexiCellEditorFactoryPrivate
00043 {
00044     public:
00045         KexiCellEditorFactoryPrivate()
00046          : items(101)
00047          , items_by_type(101, false)
00048         {
00049             items.setAutoDelete( true );
00050             items_by_type.setAutoDelete( false );
00051         }
00052         ~KexiCellEditorFactoryPrivate() {}
00053 
00054         QString key(uint type, const QString& subType) const
00055         {
00056             QString key = QString::number(type);
00057             if (!subType.isEmpty())
00058                 key += (QString(" ") + subType);
00059             return key;
00060         }
00061 
00062         void registerItem( KexiCellEditorFactoryItem& item, uint type, const QString& subType )
00063         {
00064             if (!items[ &item ])
00065                 items.insert( &item, &item );
00066 
00067             items_by_type.insert( key(type, subType), &item );
00068         }
00069         
00070         KexiCellEditorFactoryItem *findItem(uint type, const QString& subType)
00071         {
00072             KexiCellEditorFactoryItem *item = items_by_type[ key(type, subType) ];
00073             if (item)
00074                 return item;
00075             item = items_by_type[ key(type, QString::null) ];
00076             if (item)
00077                 return item;
00078             return items_by_type[ key( KexiDB::Field::InvalidType, QString::null ) ];
00079         }
00080 
00081         QPtrDict<KexiCellEditorFactoryItem> items; 
00082 
00083         QDict<KexiCellEditorFactoryItem> items_by_type; 
00084 };
00085 
00086 KexiCellEditorFactoryPrivate static_KexiCellEditorFactory;
00087 
00088 
00089 //============= KexiCellEditorFactory ============
00090 
00091 KexiCellEditorFactory::KexiCellEditorFactory()
00092 {
00093 }
00094 
00095 KexiCellEditorFactory::~KexiCellEditorFactory()
00096 {
00097 }
00098 
00099 void KexiCellEditorFactory::registerItem( KexiCellEditorFactoryItem& item, uint type, const QString& subType )
00100 {
00101     static_KexiCellEditorFactory.registerItem( item, type, subType );
00102 }
00103 
00104 KexiTableEdit* KexiCellEditorFactory::createEditor(KexiTableViewColumn &column, QScrollView* parent)
00105 {
00106     KexiTableViewData *rel_data = column.relatedData();
00107     KexiCellEditorFactoryItem *item = 0;
00108     if (rel_data) {
00109         //--we need to create combo box because of relationship:
00110         item = KexiCellEditorFactory::item( KexiDB::Field::Enum );
00111     }
00112     else {
00113         item = KexiCellEditorFactory::item( column.field()->type(), column.field()->subType() );
00114     }
00115     
00116 #if 0 //js: TODO LATER
00117     //--check if we need to create combo box because of relationship:
00118     //WARNING: it's assumed that indices are one-field long
00119     KexiDB::TableSchema *table = f.table();
00120     if (table) {
00121         //find index that contain this field
00122         KexiDB::IndexSchema::ListIterator it = table->indicesIterator();
00123         for (;it.current();++it) {
00124             KexiDB::IndexSchema *idx = it.current();
00125             if (idx->fields()->findRef(&f)!=-1) {
00126                 //find details-side rel. for this index
00127                 KexiDB::Relationship *rel = idx->detailsRelationships()->first();
00128                 if (rel) {
00129                     
00130                 }
00131             }
00132         }
00133     }
00134 #endif
00135 
00136     return item->createEditor(column, parent);
00137 }
00138 
00139 KexiCellEditorFactoryItem* KexiCellEditorFactory::item( uint type, const QString& subType )
00140 {
00141     return static_KexiCellEditorFactory.findItem(type, subType);
00142 }
00143 
KDE Home | KDE Accessibility Home | Description of Access Keys