MvcDigestDb/models/digestdbmodel.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  digestdbmodel.h
00004  *  Digest
00005  * 
00006  *  Created by Aidan Lane on Fri Aug 12 2005.
00007  *  Copyright (c) 2005-2006 Optimisation and Constraint Solving Group,
00008  *  Monash University. All rights reserved.
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00023  */
00024 
00025 #ifndef DIGESTDBMODEL_H
00026 #define DIGESTDBMODEL_H
00027 
00028 
00029 #include "MvcCore/abstractmodel.h"
00030 
00031 #include <QCache>
00032 #include <QList>
00033 #include <QPointer>
00034 #include <QSqlDatabase>
00035 #include <QSqlQuery>
00036 
00037 #include "digestdbmodelevents.h"
00038 #include "sharedmacros.h"
00039 
00040 
00041 class DigestDbController;
00042 
00043 
00044 // TODO: re-write the following for DigestDbModel!!
00072 class DigestDbModel : public QObject, public AbstractModel {
00073 
00074   Q_OBJECT
00075 
00076 public:
00077   DigestDbModel( const QSqlDatabase& database, QObject* parent = 0 );
00078 
00079   DigestDbController* digestDbController() const;
00080 
00081   DECLARE_CLASS_KEY( "digestdbmodel" );
00082 
00084   inline static MvcModuleId_t classModuleId() { return MvcDigestDb::id(); }
00086   virtual MvcModuleId_t moduleId() const { return classModuleId(); }
00087 
00088   // TODO: add warning about modifying DB withing sending notification!
00089   //       -> please read only!!!!
00090   const QSqlDatabase& database() const { return m_db; }
00091 
00092   // TODO: add setDatabase() ??? - would need a full update of all views!
00093 
00094   static bool createTables( const QSqlDatabase& db );
00095 
00096 
00097   QHash<int, QString> fetchClasses() const;
00098   QString fetchClassLabel( int id ) const;
00099   bool classLabelExists( const QString& label ) const;
00100   const IdSet& lastInsertedClassIdSet() const {
00101     return m_lastInsertedClassIdSet;
00102   }
00103 
00104   QHash<int, QString> fetchCollections() const;
00105   QString fetchCollectionLabel( int id ) const;
00106   bool collectionLabelExists( const QString& label ) const;
00107   const IdSet& lastInsertedCollectionIdSet() const {
00108     return m_lastInsertedCollectionIdSet;
00109   }
00110 
00111   QHash<int, QString> fetchExperimentsBasic() const;
00112   QString fetchExperimentLabel( int id ) const;
00113   DExperimentRecord fetchExperiment( int id, bool* ok = 0 ) const;
00114   const IdSet& lastInsertedExperimentIdSet() const {
00115     return m_lastInsertedExperimentIdSet;
00116   }
00117 
00118   int gestureCount() const;
00119   DGestureRecord fetchGesture( int id, bool* ok = 0 ) const;
00120   const IdSet& lastInsertedGestureIdSet() const {
00121     return m_lastInsertedGestureIdSet;
00122   }
00123 
00124   QHash<int, QString> fetchTrainedRecogsBasic() const;
00125   QString fetchTrainedRecogLabel( int id ) const;
00126   DTrainedRecogRecord fetchTrainedRecog( int id, bool* ok = 0 ) const;
00127   const IdSet& lastInsertedTrainedRecogIdSet() const {
00128     return m_lastInsertedTrainedRecogIdSet;
00129   }
00130 
00131   // Note: The default separator is ", " (i.e. with a space after the comma),
00132   //       as this is designed for display / printing.
00133   QString classesToString( const QSet<int>& classes,
00134                            const QString& joinSep = QString(", ") ) const;
00135   QString collectionsToString( const QSet<int>& collections,
00136                                const QString& joinSep = QString(", ") ) const;
00137 
00138   static QString idSetToString( const IdSet& idSet );
00139 
00140 
00141 protected:
00142   virtual void prepareQueries( const QSqlDatabase& db );
00143 
00144   inline virtual void customEvent( QEvent* e ) {
00145     MEvent* me = dynamic_cast<MEvent*>(e);
00146     if ( me != 0 ) dispatchEvent(me);
00147   }
00148   virtual void dispatchEvent( MEvent* );
00149 
00150   virtual void changeControllerEvent( MChangeControllerEvent* event );
00151 
00152   virtual void classAddEvent( MClassAddEvent* );
00153   virtual void classUpdateEvent( MClassUpdateEvent* );
00154   virtual void classesRemoveEvent( MClassesRemoveEvent* );
00155 
00156   virtual void collectionAddEvent( MCollectionAddEvent* );
00157   virtual void collectionUpdateEvent( MCollectionUpdateEvent* );
00158   virtual void collectionsRemoveEvent( MCollectionsRemoveEvent* );
00159 
00160   virtual void experimentAddEvent( MExperimentAddEvent* );
00161   virtual void experimentUpdateEvent( MExperimentUpdateEvent* );
00162   virtual void experimentsRemoveEvent( MExperimentsRemoveEvent* );
00163 
00164   virtual void gestureAddEvent( MGestureAddEvent* );
00165   virtual void gestureUpdateEvent( MGestureUpdateEvent* );
00166   virtual void gesturesChangeClassesEvent( MGesturesChangeClassesEvent* );
00167   virtual void gesturesChangeCollectionsEvent( MGesturesChangeCollectionsEvent* );
00168   virtual void gesturesRemoveEvent( MGesturesRemoveEvent* );
00169 
00170   virtual void trainedRecogAddEvent( MTrainedRecogAddEvent* );
00171   virtual void trainedRecogUpdateEvent( MTrainedRecogUpdateEvent* );
00172   virtual void trainedRecogsRemoveEvent( MTrainedRecogsRemoveEvent* );
00173 
00174   virtual bool gesturesChangeCategories( const QString& table,
00175                                          const QString& categoryColumn,
00176                                          const IdSet& idSet,
00177                                          const IdSet& addSet,
00178                                          const IdSet& removeSet );
00179   virtual bool removeFromTable( const QString& table,
00180                                 const QString& idColumn, const IdSet& idSet );
00181 
00182   void handleDbError();
00183 
00184 
00185 private:
00186   // Temporary state:
00187   mutable QSqlDatabase m_db;
00188 
00189   mutable QSqlQuery  m_classFetchAllQuery;
00190   mutable QSqlQuery  m_classFetchLabelQuery;
00191   IdSet              m_lastInsertedClassIdSet;
00192 
00193   QSqlQuery m_classMapInsertQuery;
00194   QSqlQuery m_classMapDeleteQuery;
00195 
00196   mutable QSqlQuery m_collectFetchAllQuery;
00197   mutable QSqlQuery m_collectFetchLabelQuery;
00198   IdSet   m_lastInsertedCollectionIdSet;
00199 
00200   QSqlQuery m_collectMapInsertQuery;
00201   QSqlQuery m_collectMapDeleteQuery;
00202 
00203   QSqlQuery m_experimentInsertQuery;
00204   QSqlQuery m_experimentUpdateQuery;
00205   QSqlQuery m_experimentDeleteQuery;
00206   mutable QSqlQuery m_experimentFetchAllBasicQuery;
00207   mutable QSqlQuery m_experimentFetchLabelQuery;
00208   mutable QSqlQuery m_experimentFetchQuery;
00209   IdSet m_lastInsertedExperimentIdSet;
00210 
00211   QSqlQuery m_gestInsertQuery;
00212   QSqlQuery m_gestUpdateQuery;
00213   QSqlQuery m_gestDeleteQuery;
00214   mutable QSqlQuery m_gestFetchQuery;
00215   IdSet m_lastInsertedGestureIdSet;
00216 
00217   QSqlQuery m_recogInsertQuery;
00218   QSqlQuery m_recogUpdateQuery;
00219   QSqlQuery m_recogDeleteQuery;
00220   mutable QSqlQuery m_recogFetchAllBasicQuery;
00221   mutable QSqlQuery m_recogFetchLabelQuery;
00222   mutable QSqlQuery m_recogFetchQuery;
00223   IdSet m_lastInsertedTrainedRecogIdSet;
00224 
00225   // Temporary caches:
00226   QPointer<DigestDbController> c_digestDbController;
00227 };
00228 
00229 
00230 #endif  // ! DIGESTDBMODEL_H

Generated on Mon Jul 30 09:46:50 2007 for Digest by  doxygen 1.5.2