kexi

kexitableviewdata.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
00003    Copyright (C) 2003   Daniel Molkentin <molkentin@kde.org>
00004    Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  
00021    Original Author:  Till Busch <till@bux.at>
00022    Original Project: buX (www.bux.at)
00023 */
00024 
00025 #ifndef KEXITABLEVIEWDATA_H
00026 #define KEXITABLEVIEWDATA_H
00027 
00028 #include <qptrlist.h>
00029 #include <qvariant.h>
00030 #include <qvaluevector.h>
00031 #include <qstring.h>
00032 #include <qobject.h>
00033 
00034 #include "kexitableitem.h"
00035 
00036 #include <kexidb/error.h>
00037 
00038 namespace KexiDB {
00039 class Field;
00040 class QuerySchema;
00041 class RowEditBuffer;
00042 class Cursor;
00043 }
00044 
00045 namespace KexiUtils {
00046 class Validator;
00047 }
00048 class KexiTableViewData;
00049 
00050 
00052 class KEXIDATATABLE_EXPORT KexiTableViewColumn {
00053     public:
00054         typedef QPtrList<KexiTableViewColumn> List;
00055         typedef QPtrListIterator<KexiTableViewColumn> ListIterator;
00056 
00059         KexiTableViewColumn(KexiDB::Field& f, bool owner = false);
00060 
00065         KexiTableViewColumn(const QString& name, KexiDB::Field::Type ctype,
00066             uint cconst = KexiDB::Field::NoConstraints,
00067             uint options = KexiDB::Field::NoOptions,
00068             uint length=0, uint precision=0,
00069             QVariant defaultValue=QVariant(),
00070             const QString& caption = QString::null,
00071             const QString& description = QString::null,
00072             uint width = 0);
00073 
00075         KexiTableViewColumn(const QString& name, KexiDB::Field::Type ctype, const QString& caption,
00076             const QString& description = QString::null);
00077 
00079         KexiTableViewColumn(const KexiDB::QuerySchema &query, KexiDB::QueryColumnInfo& fi);
00080 
00081         virtual ~KexiTableViewColumn();
00082 
00083         virtual bool acceptsFirstChar(const QChar& ch) const;
00084 
00088         inline bool isReadOnly() const { return m_readOnly; }
00089 
00090 //TODO: syncroize this with table view:
00092         inline void setReadOnly(bool ro) { m_readOnly=ro; }
00093 
00095         inline bool visible() const { return fieldinfo ? fieldinfo->visible : m_visible; }
00096 
00098         inline void setVisible(bool v) { if (fieldinfo) fieldinfo->visible=v;
00099             m_visible=v; }
00100 
00105         inline QString captionAliasOrName() const { return m_captionAliasOrName; }
00106 
00110         void setValidator( KexiUtils::Validator* v );
00111 
00113         inline KexiUtils::Validator* validator() const { return m_validator; }
00114 
00122         void setRelatedData(KexiTableViewData *data);
00123 
00127         inline KexiTableViewData *relatedData() const { return m_relatedData; }
00128 
00131         inline KexiDB::Field* field() const { return m_field; }
00132 
00136         void setRelatedDataEditable(bool set);
00137 
00141         inline bool relatedDataEditable() const { return m_relatedDataEditable; }
00142 
00145         KexiDB::QueryColumnInfo* fieldinfo;
00146 
00147         bool isDBAware : 1; 
00148 
00149 /*      QString caption;
00150         int type; //!< one of KexiDB::Field::Type
00151         uint width;
00152 */
00153 //      bool isNull() const;
00154         
00155 /*      virtual QString caption() const;
00156         virtual void setCaption(const QString& c);
00157     */  
00158     protected:
00160         KexiTableViewColumn(bool);
00161 
00162         void init();
00163 
00164         QString m_captionAliasOrName;
00165 
00166         KexiUtils::Validator* m_validator;
00167 
00169         KexiTableViewData* m_data;
00170 
00171         KexiTableViewData* m_relatedData;
00172         uint m_relatedDataPKeyID;
00173 
00174         KexiDB::Field* m_field;
00175 
00176         bool m_readOnly : 1;
00177         bool m_fieldOwned : 1;
00178         bool m_visible : 1;
00179         bool m_relatedDataEditable : 1;
00180         
00181     friend class KexiTableViewData;
00182 };
00183 
00184 
00186 //typedef QValueVector<KexiTableViewColumn> KexiTableViewColumnList;
00187 
00188 typedef QPtrList<KexiTableItem> KexiTableViewDataBase;
00189 
00194 class KEXIDATATABLE_EXPORT KexiTableViewData : public QObject, protected KexiTableViewDataBase
00195 {
00196     Q_OBJECT
00197 
00198 public: 
00199     typedef QPtrListIterator<KexiTableItem> Iterator;
00200 
00202     KexiTableViewData();
00203 
00205     KexiTableViewData(KexiDB::Cursor *c);
00206 
00207 //TODO: make this more generic: allow to add more columns!
00216     KexiTableViewData(
00217         const QValueList<QVariant> &keys, const QValueList<QVariant> &values,
00218         KexiDB::Field::Type keyType = KexiDB::Field::Text, 
00219         KexiDB::Field::Type valueType = KexiDB::Field::Text);
00220 
00225     KexiTableViewData(KexiDB::Field::Type keyType, KexiDB::Field::Type valueType);
00226 
00227     virtual ~KexiTableViewData();
00228 //js    void setSorting(int key, bool order=true, short type=1);
00229 
00231 
00232     void preloadAllRows();
00233 
00235     void setSorting(int column, bool ascending=true);
00236 
00240     inline int sortedColumn() const { return m_key; }
00241 
00245     inline int sortingOrder() const { return m_order; }
00246 
00249     void addColumn( KexiTableViewColumn* col );
00250 
00251     inline int globalColumnID(int visibleID) { return m_globalColumnsIDs.at( visibleID ); }
00252     inline int visibleColumnID(int globalID) { return m_visibleColumnsIDs.at( globalID ); }
00253 
00254     /*virtual?*/
00256     inline bool isDBAware() { return m_cursor; }
00257 
00260     QString dbTableName() const;
00261 
00262     inline KexiDB::Cursor* cursor() const { return m_cursor; }
00263 
00264     inline uint columnsCount() const { return columns.count(); }
00265 
00266     inline KexiTableViewColumn* column(uint c) { return columns.at(c); }
00267 
00269     KexiTableViewColumn::List columns;
00270 
00274     virtual bool isReadOnly() const;
00275 
00279     virtual void setReadOnly(bool set);
00280 
00282     virtual bool isInsertingEnabled() const { return m_insertingEnabled; }
00283 
00287     virtual void setInsertingEnabled(bool set);
00288 
00295     void clearRowEditBuffer();
00296 
00305     bool updateRowEditBufferRef(KexiTableItem *item, 
00306         int colnum, KexiTableViewColumn* col, QVariant& newval, bool allowSignals = true);
00307 
00309     inline bool updateRowEditBuffer(KexiTableItem *item, int colnum, KexiTableViewColumn* col, 
00310         QVariant newval, bool allowSignals = true)
00311     {
00312         QVariant newv(newval);
00313         return updateRowEditBufferRef(item, colnum, col, newv, allowSignals);
00314     }
00315 
00318     inline bool updateRowEditBuffer(KexiTableItem *item, int colnum, 
00319         QVariant newval, bool allowSignals = true)
00320     {
00321         KexiTableViewColumn* col = columns.at(colnum);
00322         return col ? updateRowEditBufferRef(item, colnum, col, newval, allowSignals) : false;
00323     }
00324 
00325     inline KexiDB::RowEditBuffer* rowEditBuffer() const { return m_pRowEditBuffer; }
00326 
00328     inline KexiDB::ResultInfo* result() { return &m_result; }
00329 
00330     bool saveRowChanges(KexiTableItem& item, bool repaint = false);
00331 
00332     bool saveNewRow(KexiTableItem& item, bool repaint = false);
00333 
00334     bool deleteRow(KexiTableItem& item, bool repaint = false);
00335 
00338     void deleteRows( const QValueList<int> &rowsToDelete, bool repaint = false );
00339 
00350     virtual bool deleteAllRows(bool repaint = false);
00351 
00356     virtual void clearInternal();
00357 
00361     void insertRow(KexiTableItem& item, uint index, bool repaint = false);
00362 
00363 /*TODO: add this as well? 
00364     void insertRow(KexiTableItem& item, KexiTableItem& aboveItem); */
00365 
00369     int autoIncrementedColumn();
00370 
00372     void reload() { emit reloadRequested(); }
00373 
00374     inline KexiTableItem* at( uint index ) { return KexiTableViewDataBase::at(index); }
00375     inline virtual uint count() const { return KexiTableViewDataBase::count(); }
00376     inline bool isEmpty () const { return KexiTableViewDataBase::isEmpty(); }
00377     inline KexiTableItem* first() { return KexiTableViewDataBase::first(); }
00378     inline KexiTableItem* last() { return KexiTableViewDataBase::last(); }
00379     inline int findRef( const KexiTableItem* item ) { return KexiTableViewDataBase::findRef(item); }
00380     inline void sort() { KexiTableViewDataBase::sort(); }
00381     inline bool removeFirst() { return KexiTableViewDataBase::removeFirst(); }
00382     inline bool removeLast() { return KexiTableViewDataBase::removeLast(); }
00383     inline void append( const KexiTableItem* item ) { KexiTableViewDataBase::append(item); }
00384     inline void prepend( const KexiTableItem* item ) { KexiTableViewDataBase::prepend(item); }
00385     inline Iterator iterator() { return Iterator(*this); }
00386     inline Iterator* createIterator() { return new Iterator(*this); }
00387 
00394     inline bool containsROWIDInfo() const { return m_containsROWIDInfo; }
00395 
00396     inline KexiTableItem* createItem() const
00397     { return new KexiTableItem(columns.count()+(m_containsROWIDInfo?1:0)); }
00398 
00399 signals:
00400     void destroying();
00401 
00406     void aboutToChangeCell(KexiTableItem *item, int colnum, QVariant& newValue,
00407         KexiDB::ResultInfo* result);
00408 
00412     void aboutToInsertRow(KexiTableItem *item, KexiDB::ResultInfo* result, bool repaint);
00413 
00417     void aboutToUpdateRow(KexiTableItem *item, KexiDB::RowEditBuffer* buffer,
00418         KexiDB::ResultInfo* result);
00419 
00420     void rowUpdated(KexiTableItem*); 
00421 
00422     void rowInserted(KexiTableItem*, bool repaint); 
00423 
00425     void rowInserted(KexiTableItem*, uint index, bool repaint);
00426 
00430     void aboutToDeleteRow(KexiTableItem& item, KexiDB::ResultInfo* result, bool repaint);
00431 
00433     void rowDeleted(); 
00434 
00436     void rowsDeleted( const QValueList<int> &rowsToDelete );
00437 
00439     void reloadRequested();
00440 
00441     void rowRepaintRequested(KexiTableItem&);
00442 
00443 protected:
00444     void init();
00445     void init(
00446         const QValueList<QVariant> &keys, const QValueList<QVariant> &values,
00447         KexiDB::Field::Type keyType, KexiDB::Field::Type valueType);
00448 
00449     virtual int compareItems(Item item1, Item item2);
00450     int cmpStr(Item item1, Item item2);
00451     int cmpInt(Item item1, Item item2);
00452     int cmpUInt(Item item1, Item item2);
00453     int cmpLongLong(Item item1, Item item2);
00454     int cmpULongLong(Item item1, Item item2);
00455     int cmpDouble(Item item1, Item item2);
00456     int cmpDate(Item item1, Item item2);
00457     int cmpDateTime(Item item1, Item item2);
00458     int cmpTime(Item item1, Item item2);
00459 
00461     bool saveRow(KexiTableItem& item, bool insert, bool repaint);
00462 
00463     int m_key;
00464     short m_order;
00465     short m_type;
00466     static unsigned short charTable[];
00467     KexiDB::RowEditBuffer *m_pRowEditBuffer;
00468     QGuardedPtr<KexiDB::Cursor> m_cursor;
00469 
00471 //  QDict<KexiTableViewColumn> *m_simpleColumnsByName;
00472 
00473     KexiDB::ResultInfo m_result;
00474 
00475     uint m_visibleColumnsCount;
00476     QValueVector<int> m_visibleColumnsIDs, m_globalColumnsIDs;
00477 
00478     bool m_readOnly : 1;
00479     bool m_insertingEnabled : 1;
00480 
00483     bool m_inside_acceptEditor : 1;
00484 
00486     bool m_containsROWIDInfo : 1;
00487 
00488     int m_autoIncrementedColumn;
00489 
00490     int (KexiTableViewData::*cmpFunc)(void *, void *);
00491 
00494     QVariant m_leftTmp, m_rightTmp;
00495 };
00496 
00497 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys