Back to index

salome-gui  6.5.0
SUIT_TreeModel.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:   SUIT_TreeModel.h
00021 // Author: Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 
00023 #ifndef SUIT_TREEMODEL_H
00024 #define SUIT_TREEMODEL_H
00025 
00026 #include "SUIT.h"
00027 
00028 #include <Qtx.h>
00029 
00030 #include <QAbstractItemModel>
00031 #include <QSortFilterProxyModel>
00032 #include <QModelIndex>
00033 #include <QItemDelegate>
00034 #include <QVariant>
00035 #include <QMap>
00036 
00037 #ifdef WIN32
00038 #pragma warning( disable:4251 )
00039 #endif
00040 
00041 class SUIT_DataObject;
00042 class SUIT_TreeModel;
00043 class QMimeData;
00044 
00045 class SUIT_EXPORT SUIT_DataSearcher
00046 {
00047 public:
00048   virtual SUIT_DataObject* findObject( const QString& ) const = 0;
00049 };
00050 
00051 class SUIT_EXPORT SUIT_AbstractModel
00052 {
00053   SUIT_DataSearcher* mySearcher;
00054 
00055 public:
00056   SUIT_AbstractModel();
00057 
00058   operator const QAbstractItemModel*() const;
00059   operator QAbstractItemModel*();
00060   operator const QObject*() const;
00061 
00062   virtual SUIT_DataObject*      root() const = 0;
00063   virtual void                  setRoot( SUIT_DataObject* ) = 0;
00064   virtual SUIT_DataObject*      object( const QModelIndex& = QModelIndex() ) const = 0;
00065   virtual QModelIndex           index( const SUIT_DataObject*, int = 0 ) const = 0;
00066   virtual bool                  autoDeleteTree() const = 0;
00067   virtual void                  setAutoDeleteTree( const bool ) = 0;
00068   virtual bool                  autoUpdate() const = 0;
00069   virtual void                  setAutoUpdate( const bool ) = 0;
00070   virtual bool                  updateModified() const = 0;
00071   virtual void                  setUpdateModified( const bool ) = 0;
00072   virtual QAbstractItemDelegate* delegate() const = 0;
00073   virtual bool                  customSorting( const int ) const = 0;
00074   virtual bool                  lessThan( const QModelIndex& left, const QModelIndex& right ) const = 0;
00075   virtual void                  forgetObject( const SUIT_DataObject* ) = 0;
00076 
00077   virtual void                  updateTree( const QModelIndex& ) = 0;
00078   virtual void                  updateTree( SUIT_DataObject* = 0 ) = 0;
00079 
00080   virtual void                  registerColumn( const int group_id, const QString& name, const int custom_id ) = 0;
00081   virtual void                  unregisterColumn( const int group_id, const QString& name ) = 0;
00082   virtual void                  setColumnIcon( const QString& name, const QPixmap& icon ) = 0;
00083   virtual QPixmap               columnIcon( const QString& name ) const = 0;
00084   virtual void                  setAppropriate( const QString& name, const Qtx::Appropriate appr ) = 0;
00085   virtual Qtx::Appropriate      appropriate( const QString& name ) const = 0;
00086   virtual void                  setVisibilityState(const QString& id, Qtx::VisibilityState state) = 0;
00087   virtual void                  setVisibilityStateForAll(Qtx::VisibilityState state) = 0;
00088   virtual Qtx::VisibilityState  visibilityState(const QString& id) const = 0;
00089   virtual void                  setHeaderFlags( const QString& name, const Qtx::HeaderViewFlags flags ) = 0;
00090   virtual Qtx::HeaderViewFlags  headerFlags( const QString& name ) const = 0;
00091   virtual void                  emitClicked( SUIT_DataObject* obj, const QModelIndex& index) = 0;
00092 
00093   virtual SUIT_DataSearcher*    searcher() const;
00094   virtual void                  setSearcher( SUIT_DataSearcher* );
00095 };
00096 
00097 
00098 class SUIT_EXPORT SUIT_TreeModel : public QAbstractItemModel, public SUIT_AbstractModel
00099 {
00100   Q_OBJECT
00101 
00102 private:
00103   class TreeItem;
00104   class TreeSync;
00105   typedef SUIT_DataObject*          ObjPtr;
00106   typedef SUIT_TreeModel::TreeItem* ItemPtr;
00107  
00108 public:
00110   typedef enum {
00111     DisplayRole         = Qt::DisplayRole,       
00112     DecorationRole      = Qt::DecorationRole,    
00113     EditRole            = Qt::EditRole,          
00114     ToolTipRole         = Qt::ToolTipRole,       
00115     StatusTipRole       = Qt::StatusTipRole,     
00116     WhatsThisRole       = Qt::WhatsThisRole,     
00117     FontRole            = Qt::FontRole,          
00118     TextAlignmentRole   = Qt::TextAlignmentRole, 
00119     BackgroundRole      = Qt::BackgroundRole,    
00120     ForegroundRole      = Qt::ForegroundRole,    
00121     CheckStateRole      = Qt::CheckStateRole,    
00122     SizeHintRole        = Qt::SizeHintRole,      
00123     BaseColorRole       = Qt::UserRole,          
00124     TextColorRole,                               
00125     HighlightRole,                               
00126     HighlightedTextRole,                         
00127     AppropriateRole     = Qtx::AppropriateRole   
00128   } Role;
00129 
00130   SUIT_TreeModel( QObject* = 0 );
00131   SUIT_TreeModel( SUIT_DataObject*, QObject* = 0 );
00132   ~SUIT_TreeModel();
00133 
00134   SUIT_DataObject*       root() const;
00135   void                   setRoot( SUIT_DataObject* );
00136 
00137   virtual QVariant       data( const QModelIndex&, int = DisplayRole ) const;
00138   virtual bool           setData( const QModelIndex&, const QVariant&, int = EditRole );
00139   virtual Qt::ItemFlags  flags( const QModelIndex& ) const;
00140   virtual QVariant       headerData( int, Qt::Orientation, int = Qt::DisplayRole ) const;
00141 
00142   virtual Qt::DropActions supportedDropActions() const;
00143 
00144   virtual QModelIndex    index( int, int, const QModelIndex& = QModelIndex() ) const;
00145   virtual QModelIndex    parent( const QModelIndex& ) const;
00146 
00147   virtual int              columnCount( const QModelIndex& = QModelIndex() ) const;
00148   virtual int              rowCount( const QModelIndex& = QModelIndex() ) const;
00149   virtual void             registerColumn( const int group_id, const QString& name, const int custom_id );
00150   virtual void             unregisterColumn( const int group_id, const QString& name );
00151   virtual void             setColumnIcon( const QString& name, const QPixmap& icon );
00152   virtual QPixmap          columnIcon( const QString& name ) const;
00153   virtual void             setAppropriate( const QString& name, const Qtx::Appropriate appr );
00154   virtual Qtx::Appropriate appropriate( const QString& name ) const;
00155   virtual void                  setVisibilityState(const QString& id, Qtx::VisibilityState state);
00156   virtual void                  setVisibilityStateForAll(Qtx::VisibilityState state);
00157   virtual Qtx::VisibilityState  visibilityState(const QString& id) const;
00158   virtual void                  setHeaderFlags( const QString& name, const Qtx::HeaderViewFlags flags );
00159   virtual Qtx::HeaderViewFlags  headerFlags( const QString& name ) const;
00160   virtual void           emitClicked( SUIT_DataObject* obj, const QModelIndex& index);
00161 
00162 
00163   SUIT_DataObject*       object( const QModelIndex& = QModelIndex() ) const;
00164   QModelIndex            index( const SUIT_DataObject*, int = 0 ) const;
00165 
00166   bool                   autoDeleteTree() const;
00167   void                   setAutoDeleteTree( const bool );
00168 
00169   bool                   autoUpdate() const;
00170   void                   setAutoUpdate( const bool );
00171 
00172   bool                   updateModified() const;
00173   void                   setUpdateModified( const bool );
00174 
00175   virtual bool           customSorting( const int ) const;
00176   virtual bool           lessThan( const QModelIndex& left, const QModelIndex& right ) const;
00177   virtual void           forgetObject( const SUIT_DataObject* );
00178 
00179   QAbstractItemDelegate* delegate() const;
00180 
00181 
00182   virtual void           updateTreeModel(SUIT_DataObject*,TreeItem*);
00183 
00184   virtual QStringList    mimeTypes() const;
00185   virtual QMimeData*     mimeData (const QModelIndexList& indexes) const;
00186   virtual bool           dropMimeData (const QMimeData *data, Qt::DropAction action,
00187                                        int row, int column, const QModelIndex &parent);
00188 
00189 public slots:
00190   virtual void           updateTree( const QModelIndex& );
00191   virtual void           updateTree( SUIT_DataObject* = 0 );
00192 
00193 signals:
00194   void modelUpdated();
00195   void clicked( SUIT_DataObject*, int );
00196   void dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction );
00197 
00198 private:
00199   void                   initialize();
00200 
00201   TreeItem*              rootItem() const;
00202   TreeItem*              treeItem( const QModelIndex& ) const;
00203   TreeItem*              treeItem( const SUIT_DataObject* ) const;
00204   SUIT_DataObject*       object( const TreeItem* ) const;
00205   QString                objectId( const QModelIndex& = QModelIndex() ) const;
00206 
00207   TreeItem*              createItem( SUIT_DataObject*, TreeItem* = 0, TreeItem* = 0 );
00208   TreeItem*              createItemAtPos( SUIT_DataObject*, TreeItem* = 0, int pos=0 );
00209   void                   updateItem( TreeItem*, bool emitLayoutChanged );
00210   void                   removeItem( TreeItem* );
00211 
00212 private slots:
00213   void                   onInserted( SUIT_DataObject*, SUIT_DataObject* );
00214   void                   onRemoved( SUIT_DataObject*, SUIT_DataObject* );
00215 
00216 private:
00217   typedef QMap<SUIT_DataObject*, TreeItem*> ItemMap;
00218   typedef struct
00219   {
00220     QString myName;
00221         QMap<int,int> myIds;
00222         QPixmap myIcon;
00223         Qtx::HeaderViewFlags myHeaderFlags;
00224         Qtx::Appropriate myAppropriate;
00225 
00226   } ColumnInfo;
00227 
00228   typedef QMap<QString,Qtx::VisibilityState> VisibilityMap;
00229   
00230   SUIT_DataObject*    myRoot;
00231   TreeItem*           myRootItem;
00232   ItemMap             myItems;
00233   VisibilityMap       myVisibilityMap;
00234   bool                myAutoDeleteTree;
00235   bool                myAutoUpdate;
00236   bool                myUpdateModified;
00237   QVector<ColumnInfo> myColumns;
00238 
00239   friend class SUIT_TreeModel::TreeSync;
00240 };
00241 
00242 class SUIT_EXPORT SUIT_ProxyModel : public QSortFilterProxyModel, public SUIT_AbstractModel
00243 {
00244   Q_OBJECT
00245 
00246 public:
00247   SUIT_ProxyModel( QObject* = 0 );
00248   SUIT_ProxyModel( SUIT_DataObject*, QObject* = 0 );
00249   SUIT_ProxyModel( SUIT_AbstractModel*, QObject* = 0 );
00250   ~SUIT_ProxyModel();
00251 
00252   SUIT_DataObject*       root() const;
00253   void                   setRoot( SUIT_DataObject* );
00254 
00255   SUIT_DataObject*       object( const QModelIndex& = QModelIndex() ) const;
00256   QModelIndex            index( const SUIT_DataObject*, int = 0 ) const;
00257 
00258   bool                   autoDeleteTree() const;
00259   void                   setAutoDeleteTree( const bool );
00260 
00261   bool                   autoUpdate() const;
00262   void                   setAutoUpdate( const bool );
00263 
00264   bool                   updateModified() const;
00265   void                   setUpdateModified( const bool );
00266 
00267   bool                   isSortingEnabled() const;
00268   bool                   customSorting( const int ) const;
00269 
00270   virtual void             forgetObject( const SUIT_DataObject* );
00271   virtual bool             lessThan( const QModelIndex&, const QModelIndex& ) const;
00272   virtual void             registerColumn( const int group_id, const QString& name, const int custom_id );
00273   virtual void             unregisterColumn( const int group_id, const QString& name );
00274   virtual void             setColumnIcon( const QString& name, const QPixmap& icon );
00275   virtual QPixmap          columnIcon( const QString& name ) const;
00276   virtual void             setAppropriate( const QString& name, const Qtx::Appropriate appr );
00277   virtual Qtx::Appropriate appropriate( const QString& name ) const;
00278   virtual void                  setVisibilityState(const QString& id, Qtx::VisibilityState state);
00279   virtual void                  setVisibilityStateForAll(Qtx::VisibilityState state);
00280   virtual Qtx::VisibilityState  visibilityState(const QString& id) const;
00281   virtual void                  setHeaderFlags( const QString& name, const Qtx::HeaderViewFlags flags );
00282   virtual Qtx::HeaderViewFlags  headerFlags( const QString& name ) const;
00283   virtual void             emitClicked( SUIT_DataObject* obj, const QModelIndex& index);
00284 
00285   virtual SUIT_DataSearcher*    searcher() const;
00286   virtual void                  setSearcher( SUIT_DataSearcher* );
00287 
00288   QAbstractItemDelegate* delegate() const;
00289 
00290 public slots:
00291   virtual void           updateTree( const QModelIndex& );
00292   virtual void           updateTree( SUIT_DataObject* = 0 );
00293   void                   setSortingEnabled( bool );
00294 
00295 signals:
00296   void modelUpdated();
00297   void clicked( SUIT_DataObject*, int );
00298   void dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction );
00299 
00300 protected:
00301   SUIT_AbstractModel*    treeModel() const;
00302   virtual bool           filterAcceptsRow( int, const QModelIndex& ) const;
00303 
00304 private:
00305   bool                   mySortingEnabled;
00306 };
00307 
00308 class SUIT_EXPORT SUIT_ItemDelegate : public QItemDelegate
00309 {
00310   Q_OBJECT
00311 
00312 public:
00313   SUIT_ItemDelegate( QObject* = 0 );
00314   
00315   virtual void paint( QPainter*, const QStyleOptionViewItem&,
00316                       const QModelIndex& ) const;
00317                       
00318   virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
00319 };
00320 
00321 #ifdef WIN32
00322 #pragma warning( default:4251 )
00323 #endif
00324 
00325 #endif // SUIT_TREEMODEL_H