kexi

kexidataprovider.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library 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 library 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 library; see the file COPYING.LIB.  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 "kexidataprovider.h"
00021 
00022 #include <qwidget.h>
00023 #include <qobjectlist.h>
00024 
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 
00028 #include <tableview/kexitableitem.h>
00029 #include <tableview/kexitableviewdata.h>
00030 #include <kexidb/queryschema.h>
00031 #include <kexiutils/utils.h>
00032 
00033 #include "widgets/kexidbform.h"
00034 
00035 KexiFormDataProvider::KexiFormDataProvider()
00036  : KexiDataItemChangesListener()
00037  , m_mainWidget(0)
00038  , m_duplicatedItems(0)
00039 {
00040 }
00041 
00042 KexiFormDataProvider::~KexiFormDataProvider()
00043 {
00044     delete m_duplicatedItems;
00045 }
00046 
00047 void KexiFormDataProvider::setMainDataSourceWidget(QWidget* mainWidget)
00048 {
00049     m_mainWidget = mainWidget;
00050     m_dataItems.clear();
00051     m_usedDataSources.clear();
00052     m_fieldNumbersForDataItems.clear();
00053     if (!m_mainWidget)
00054         return;
00055 
00056     //find widgets whose will work as data items
00057     QObjectList *l = m_mainWidget->queryList( "QWidget" );
00058     QObjectListIt it( *l );
00059     QObject *obj;
00060     QDict<char> tmpSources;
00061     for ( ; (obj = it.current()) != 0; ++it ) {
00062         if (!dynamic_cast<KexiFormDataItemInterface*>(obj))
00063             continue;
00064 #if 0 
00065         KexiDBForm *dbForm = KexiUtils::findParent<KexiDBForm>(obj, "KexiDBForm"); //form's surface...
00066         if (dbForm!=m_mainWidget) //only set data for this form's data items
00067             continue;
00068 #else
00069         //tmp: reject widgets within subforms
00070         if (KexiUtils::findParent<KexiDBForm>(obj, "KexiDBSubForm"))
00071             continue;
00072 #endif
00073         QString dataSource( dynamic_cast<KexiFormDataItemInterface*>(obj)->dataSource().lower() );
00074         if (dataSource.isEmpty())
00075             continue;
00076         kexipluginsdbg << obj->name() << endl;
00077         m_dataItems.append( dynamic_cast<KexiFormDataItemInterface*>(obj) );
00078         dynamic_cast<KexiFormDataItemInterface*>(obj)->installListener( this );
00079         tmpSources.replace( dataSource, (char*)1 );
00080     }
00081     delete l;
00082     //now we've got a set (unique list) of field names in tmpSources
00083     //remember it in m_usedDataSources
00084     for (QDictIterator<char> it(tmpSources); it.current(); ++it) {
00085         m_usedDataSources += it.currentKey();
00086     }
00087 }
00088 
00089 void KexiFormDataProvider::fillDataItems(KexiTableItem& row)
00090 {
00091     kexidbg << "KexiFormDataProvider::fillDataItems() cnt=" << row.count() << endl;
00092     for (KexiFormDataItemInterfaceToIntMap::ConstIterator it = m_fieldNumbersForDataItems.constBegin(); 
00093         it!=m_fieldNumbersForDataItems.constEnd(); ++it)
00094     {
00095         kexipluginsdbg << "fill data of '" << it.key()->dataSource() <<  "' at idx=" << it.data() << endl;
00096         it.key()->setValue( row.at(it.data()) );
00097     }
00098 }
00099 
00100 void KexiFormDataProvider::fillDuplicatedDataItems(
00101     KexiFormDataItemInterface* item, const QVariant& value)
00102 {
00103     if (!m_duplicatedItems) {
00104         //build (once) a set of duplicated data items (having the same fields assigned)
00105         //so we can later check if an item is duplicated with a cost of o(1)
00106         QMap<KexiDB::Field*,int> tmpDuplicatedItems;
00107         QMapIterator<KexiDB::Field*,int> it_dup;
00108         for (QPtrListIterator<KexiFormDataItemInterface> it(m_dataItems); it.current(); ++it) {
00109             it_dup = tmpDuplicatedItems.find( it.current()->columnInfo()->field );
00110             uint count;
00111             if (it_dup==tmpDuplicatedItems.end())
00112                 count = 0;
00113             else
00114                 count = it_dup.data();
00115             tmpDuplicatedItems.insert( it.current()->columnInfo()->field, ++count );
00116         }
00117         m_duplicatedItems = new QPtrDict<char>(101);
00118         for (it_dup = tmpDuplicatedItems.begin(); it_dup!=tmpDuplicatedItems.end(); ++it_dup) {
00119             if (it_dup.data() > 1) {
00120                 m_duplicatedItems->insert( it_dup.key(), (char*)1 );
00121                 kexipluginsdbg << "duplicated item: " << static_cast<KexiDB::Field*>(it_dup.key())->name() 
00122                     << " (" << it_dup.data() << " times)" << endl;
00123             }
00124         }
00125     }
00126     if (m_duplicatedItems->find( item->columnInfo()->field )) {
00127         for (QPtrListIterator<KexiFormDataItemInterface> it(m_dataItems); it.current(); ++it) {
00128             if (it.current()!=item && item->columnInfo()->field == it.current()->columnInfo()->field) {
00129                 kexipluginsdbg << "- setting value for item '" 
00130                     << dynamic_cast<QObject*>(it.current())->name() << " == " << value.toString() << endl;
00131                 it.current()->setValue( value );
00132             }
00133         }
00134     }
00135 }
00136 
00137 void KexiFormDataProvider::valueChanged(KexiDataItemInterface* item)
00138 {
00139     Q_UNUSED( item );
00140 }
00141 
00142 bool KexiFormDataProvider::cursorAtNewRow()
00143 {
00144     return false;
00145 }
00146 
00147 void KexiFormDataProvider::invalidateDataSources( const QValueList<uint>& invalidSources,
00148  KexiDB::QuerySchema* query)
00149 {
00150     //fill m_fieldNumbersForDataItems mapping from data item to field number
00151     //(needed for fillDataItems)
00152     KexiDB::QueryColumnInfo::Vector fieldsExpanded;
00153     uint dataFieldsCount; // == fieldsExpanded.count() if query is available or else == m_dataItems.count()
00154     if (query) {
00155         fieldsExpanded = query->fieldsExpanded();
00156         dataFieldsCount = fieldsExpanded.count();
00157         QMap<KexiDB::QueryColumnInfo*,int> fieldsOrder( query->fieldsOrder() );
00158         for (QMapConstIterator<KexiDB::QueryColumnInfo*,int> it = fieldsOrder.constBegin(); it!=fieldsOrder.constEnd(); ++it) {
00159             kexipluginsdbg << "query->fieldsOrder()[ " << it.key()->field->name() << " ] = " << it.data() << endl;
00160         }
00161         for (QPtrListIterator<KexiFormDataItemInterface> it(m_dataItems); it.current(); ++it) {
00162             KexiFormDataItemInterface *item = it.current();
00163             KexiDB::QueryColumnInfo* ci = query->columnInfo( it.current()->dataSource() );
00164             int index = ci ? query->fieldsOrder()[ ci ] : -1;
00165             kexipluginsdbg << "query->fieldsOrder()[ " << (ci ? ci->field->name() : "") << " ] = " << index 
00166                 << " (dataSource: " << item->dataSource() << ", name=" << dynamic_cast<QObject*>(item)->name() << ")" << endl;
00167             if (index!=-1)
00168                 m_fieldNumbersForDataItems.insert( item, index );
00169     //todo
00170     //WRONG: not only used data sources can be fetched!
00171     //          m_fieldNumbersForDataItems.insert( it.current(), 
00172     //              m_usedDataSources.findIndex(it.current()->dataSource().lower()) );
00173         }
00174     }
00175     else {
00176         dataFieldsCount = m_dataItems.count();
00177     }
00178 
00179     //in 'newIndices' let's collect new indices for every data source
00180     foreach(QValueList<uint>::ConstIterator, it, invalidSources) {
00181         //all previous indices have corresponding data source
00182 //      for (; i < (*it); i++) {
00183 //          newIndices[i] = number++;
00184             //kexidbg << "invalidateDataSources(): " << i << " -> " << number-1 << endl;
00185 //      }
00186         //this index have no corresponding data source
00187 //      newIndices[i]=-1;
00188         KexiFormDataItemInterface *item = m_dataItems.at( *it );
00189         if (item)
00190             item->setInvalidState( QString::fromLatin1("#") + i18n("NAME") + QString::fromLatin1("?") );
00191         m_dataItems.remove(*it);
00192         kexidbg << "invalidateDataSources(): " << (*it) << " -> " << -1 << endl;
00193 //      i++;
00194     }
00195     //fill remaining part of the vector
00196 //  for (; i < dataFieldsCount; i++) { //m_dataItems.count(); i++) {
00197         //newIndices[i] = number++;
00198         //kexidbg << "invalidateDataSources(): " << i << " -> " << number-1 << endl;
00199     //}
00200 
00201 #if 0
00202     //recreate m_fieldNumbersForDataItems and mark widgets with invalid data sources
00203     KexiFormDataItemInterfaceToIntMap newFieldNumbersForDataItems;
00204     foreach(KexiFormDataItemInterfaceToIntMap::ConstIterator, it, m_fieldNumbersForDataItems) {
00205         bool ok;
00206         const int newIndex = newIndices.at( it.data(), &ok );
00207         if (ok && newIndex!=-1) {
00208             kexidbg << "invalidateDataSources(): " << it.key()->dataSource() << ": " << it.data() << " -> " << newIndex << endl;
00209             newFieldNumbersForDataItems.replace(it.key(), newIndex);
00210         }
00211         else {
00212             kexidbg << "invalidateDataSources(): removing " << it.key()->dataSource() << endl;
00213             m_dataItems.remove(it.key());
00214             it.key()->setInvalidState( QString::fromLatin1("#") + i18n("NAME") + QString::fromLatin1("?") );
00215         }
00216     }
00217 #endif
00218 //  m_fieldNumbersForDataItems = newFieldNumbersForDataItems;
00219 
00220     //update data sources set (some of them may be removed)
00221     QDict<char> tmpUsedDataSources(1013);
00222 
00223     if (query)
00224         query->debug();
00225 
00226     //if (query && m_dataItems.count()!=query->fieldCount()) {
00227     //  kdWarning() << "KexiFormDataProvider::invalidateDataSources(): m_dataItems.count()!=query->fieldCount() ("
00228     //   << m_dataItems.count() << "," << query->fieldCount() << ")" << endl;
00229     //}
00230     //i = 0;
00231     foreach_list(QPtrListIterator<KexiFormDataItemInterface>, it, m_dataItems) {
00232         KexiFormDataItemInterface * item = it.current();
00233         uint fieldNumber = m_fieldNumbersForDataItems[ item ];
00234         if (query) {
00235             KexiDB::QueryColumnInfo *ci = fieldsExpanded[fieldNumber];
00236 //          KexiDB::Field *f = ci->field;
00237             it.current()->setColumnInfo(ci);
00238             kexipluginsdbg << "- item=" << dynamic_cast<QObject*>(it.current())->name() 
00239                 << " dataSource=" << it.current()->dataSource()
00240                 << " field=" << ci->field->name() << endl;
00241         }
00242         tmpUsedDataSources.replace( it.current()->dataSource().lower(), (char*)1 );
00243     }
00244     m_usedDataSources.clear();
00245     foreach_list(QDictIterator<char>, it, tmpUsedDataSources) {
00246         m_usedDataSources += it.currentKey();
00247     }
00248 }
KDE Home | KDE Accessibility Home | Description of Access Keys