kexi
kexidataprovider.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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)
00067 continue;
00068 #else
00069
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
00083
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
00105
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
00151
00152 KexiDB::QueryColumnInfo::Vector fieldsExpanded;
00153 uint dataFieldsCount;
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
00170
00171
00172
00173 }
00174 }
00175 else {
00176 dataFieldsCount = m_dataItems.count();
00177 }
00178
00179
00180 foreach(QValueList<uint>::ConstIterator, it, invalidSources) {
00181
00182
00183
00184
00185
00186
00187
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
00194 }
00195
00196
00197
00198
00199
00200
00201 #if 0
00202
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
00219
00220
00221 QDict<char> tmpUsedDataSources(1013);
00222
00223 if (query)
00224 query->debug();
00225
00226
00227
00228
00229
00230
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
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 }
|