DigestGuiCore/categoryitemmodel.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  categoryitemmodel.h
00004  *  Digest
00005  * 
00006  *  Created by Aidan Lane on Wed Jul 20 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 CATEGORYITEMMODEL_H
00026 #define CATEGORYITEMMODEL_H
00027 
00028 
00029 #include <QAbstractItemModel>
00030 #include "MvcDigestDb/abstractdigestdbview.h"
00031 
00032 #include <QHash>
00033 
00034 
00035 class CategoryItemModel : public QAbstractItemModel, public AbstractDigestDbView {
00036 
00037   Q_OBJECT
00038 
00039 public:
00040   struct CategoryItem;
00041 
00042   // Note: This isn't called "ItemType", as it's designed for external use.
00043   enum IndexType {
00044     RootIndex,
00045     LibraryIndex,
00046     ClassIndex,
00047     CollectionIndex
00048   };
00049 
00050   enum ModelFlag {
00051     ItemsAreSelectable  = 0x1, 
00052     ItemsAreEditable    = 0x2, 
00053     ItemsAreCheckable   = 0x4, 
00054     ItemsAreDropEnabled = 0x8  
00055   };
00056   Q_DECLARE_FLAGS( ModelFlags, ModelFlag ); // typesafe flags
00057 
00058 
00059   CategoryItemModel( QObject* parent = 0 );
00060   CategoryItemModel( DigestDbController* controller, QObject* parent = 0 );
00061   virtual ~CategoryItemModel();
00062 
00063   inline ModelFlags modelFlags();
00064   void setModelFlags( ModelFlags flags );
00065 
00066   CategoryItemModel::IndexType indexType( const QModelIndex& index ) const;
00067   int indexDatabaseId( const QModelIndex& index ) const;
00068 
00069   QModelIndex libraryIndex() const { return c_libraryIndex; }
00070 
00071   virtual Qt::ItemFlags flags( const QModelIndex& index ) const;
00072   virtual int rowCount( const QModelIndex& parent = QModelIndex() ) const;
00073   virtual int columnCount( const QModelIndex& parent = QModelIndex() ) const;
00074 
00075   virtual QVariant headerData( int section, Qt::Orientation orientation,
00076                                int role = Qt::DisplayRole ) const;
00077 
00078   virtual QModelIndex parent( const QModelIndex& index ) const;
00079 
00080   virtual QModelIndex index( int row, int column,
00081                              const QModelIndex& parent = QModelIndex() ) const;
00082 
00083   virtual QVariant data( const QModelIndex& index,
00084                          int role = Qt::DisplayRole ) const;
00085 
00086   virtual bool setData( const QModelIndex& index, const QVariant& value,
00087                         int role = Qt::EditRole );
00088 
00089   Qt::DropActions supportedDropActions() const;
00090   virtual QStringList mimeTypes() const;
00091   virtual bool dropMimeData( const QMimeData* data, Qt::DropAction action,
00092                              int row, int column, const QModelIndex& index );
00093 
00094   QModelIndexList checkedIndexes() const;
00095   QString gestureQueryForIndexes( const QModelIndexList& indexes ) const;
00096   QString gestureQueryForCheckedIndexes() const;
00097 
00098   bool isReady() const { return m_ready; } // hacky
00099 
00100 
00101 protected:
00102   DEFINE_CUSTOM_VIEWEVENT_HANDLER
00103   virtual void resetEvent( VEvent* );
00104   virtual void classesEvent( VClassesEvent* );
00105   virtual void collectionsEvent( VCollectionsEvent* );
00106 
00107   void resetModel();
00108   QModelIndex index( CategoryItem* item ) const;
00109 
00110   QModelIndexList checkedIndexes( CategoryItem* parent ) const;
00111 
00112 
00113 private:
00114   void init();
00115 
00116   // Temporary state:
00117   CategoryItem* m_rootItem;
00118   ModelFlags    m_modelFlags;
00119   bool          m_ready;
00120 
00121   // Note: we must keep these two separate as their IDs will overlap
00122   QHash<int, CategoryItem*> m_classIdToItem;
00123   QHash<int, CategoryItem*> m_collectionIdToItem;
00124 
00125   // Caches:
00126   CategoryItem* c_libraryItem;
00127   QModelIndex   c_libraryIndex; // ok to cache - never leaves row 0, column 0
00128 };
00129 
00130 Q_DECLARE_OPERATORS_FOR_FLAGS( CategoryItemModel::ModelFlags );
00131 
00132 
00141 CategoryItemModel::ModelFlags CategoryItemModel::modelFlags() {
00142   return m_modelFlags;
00143 }
00144 
00145 
00146 #endif  // ! CATEGORYITEMMODEL_H

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