]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_TreeModel.h
Salome HOME
e35570342eed3397db4127c4841497bde9e5922b
[modules/gui.git] / src / SUIT / SUIT_TreeModel.h
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File:   SUIT_TreeModel.h
23 // Author: Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #ifndef SUIT_TREEMODEL_H
26 #define SUIT_TREEMODEL_H
27
28 #include "SUIT.h"
29
30 #include <Qtx.h>
31
32 #include <QAbstractItemModel>
33 #include <QSortFilterProxyModel>
34 #include <QModelIndex>
35 #include <QItemDelegate>
36 #include <QVariant>
37
38 #ifdef WIN32
39 #pragma warning( disable:4251 )
40 #endif
41
42 class SUIT_DataObject;
43 class SUIT_TreeModel;
44
45 class SUIT_EXPORT SUIT_AbstractModel
46 {
47 public:
48   SUIT_AbstractModel();
49
50   operator const QAbstractItemModel*() const;
51   operator QAbstractItemModel*();
52   operator const QObject*() const;
53
54   virtual SUIT_DataObject* root() const = 0;
55   virtual void             setRoot( SUIT_DataObject* ) = 0;
56   virtual SUIT_DataObject* object( const QModelIndex& = QModelIndex() ) const = 0;
57   virtual QModelIndex      index( const SUIT_DataObject*, int = 0 ) const = 0;
58   virtual bool             autoDeleteTree() const = 0;
59   virtual void             setAutoDeleteTree( const bool ) = 0;
60   virtual bool             autoUpdate() const = 0;
61   virtual void             setAutoUpdate( const bool ) = 0;
62   virtual QAbstractItemDelegate* delegate() const = 0;
63   virtual bool             customSorting( const int ) const = 0;
64   virtual bool             lessThan( const QModelIndex& left, const QModelIndex& right ) const = 0;
65
66   virtual void             updateTree( const QModelIndex& ) = 0;
67   virtual void             updateTree( SUIT_DataObject* = 0 ) = 0;
68
69   virtual void             registerColumn( const int group_id, const QString& name, const int custom_id ) = 0;
70   virtual void             unregisterColumn( const int group_id, const QString& name ) = 0;
71   virtual void             setColumnIcon( const QString& name, const QPixmap& icon ) = 0;
72   virtual QPixmap          columnIcon( const QString& name ) const = 0;
73   virtual void             setAppropriate( const QString& name, const Qtx::Appropriate appr ) = 0;
74   virtual Qtx::Appropriate appropriate( const QString& name ) const = 0;
75 };
76
77
78 class SUIT_EXPORT SUIT_TreeModel : public QAbstractItemModel, public SUIT_AbstractModel
79 {
80   Q_OBJECT
81
82 private:
83   class TreeItem;
84   class TreeSync;
85   typedef SUIT_DataObject*          ObjPtr;
86   typedef SUIT_TreeModel::TreeItem* ItemPtr;
87  
88 public:
89   //! Data role
90   typedef enum {
91     DisplayRole         = Qt::DisplayRole,       //!< text label
92     DecorationRole      = Qt::DecorationRole,    //!< icon
93     EditRole            = Qt::EditRole,          //!< edit mode
94     ToolTipRole         = Qt::ToolTipRole,       //!< tooltip
95     StatusTipRole       = Qt::StatusTipRole,     //!< status tip
96     WhatsThisRole       = Qt::WhatsThisRole,     //!< what's this info
97     FontRole            = Qt::FontRole,          //!< font
98     TextAlignmentRole   = Qt::TextAlignmentRole, //!< text alignment
99     BackgroundRole      = Qt::BackgroundRole,    //!< background color
100     ForegroundRole      = Qt::ForegroundRole,    //!< text color
101     CheckStateRole      = Qt::CheckStateRole,    //!< check state
102     SizeHintRole        = Qt::SizeHintRole,      //!< size hint
103     BaseColorRole       = Qt::UserRole,          //!< (editor) background color
104     TextColorRole,                               //!< (editor) text color    (Qt::UserRole + 1)
105     HighlightRole,                               //!< highlight color        (Qt::UserRole + 2)
106     HighlightedTextRole,                         //!< highlighted text color (Qt::UserRole + 3)
107     AppropriateRole     = Qtx::AppropriateRole   //!< appropriate flag       (Qt::UserRole + 100)
108   } Role;
109
110   SUIT_TreeModel( QObject* = 0 );
111   SUIT_TreeModel( SUIT_DataObject*, QObject* = 0 );
112   ~SUIT_TreeModel();
113
114   SUIT_DataObject*       root() const;
115   void                   setRoot( SUIT_DataObject* );
116
117   virtual QVariant       data( const QModelIndex&, int = DisplayRole ) const;
118   virtual bool           setData( const QModelIndex&, const QVariant&, int = EditRole );
119   virtual Qt::ItemFlags  flags( const QModelIndex& ) const;
120   virtual QVariant       headerData( int, Qt::Orientation, int = Qt::DisplayRole ) const;
121
122   virtual QModelIndex    index( int, int, const QModelIndex& = QModelIndex() ) const;
123   virtual QModelIndex    parent( const QModelIndex& ) const;
124
125   virtual int              columnCount( const QModelIndex& = QModelIndex() ) const;
126   virtual int              rowCount( const QModelIndex& = QModelIndex() ) const;
127   virtual void             registerColumn( const int group_id, const QString& name, const int custom_id );
128   virtual void             unregisterColumn( const int group_id, const QString& name );
129   virtual void             setColumnIcon( const QString& name, const QPixmap& icon );
130   virtual QPixmap          columnIcon( const QString& name ) const;
131   virtual void             setAppropriate( const QString& name, const Qtx::Appropriate appr );
132   virtual Qtx::Appropriate appropriate( const QString& name ) const;
133
134   SUIT_DataObject*       object( const QModelIndex& = QModelIndex() ) const;
135   QModelIndex            index( const SUIT_DataObject*, int = 0 ) const;
136
137   bool                   autoDeleteTree() const;
138   void                   setAutoDeleteTree( const bool );
139
140   bool                   autoUpdate() const;
141   void                   setAutoUpdate( const bool );
142
143   virtual bool           customSorting( const int ) const;
144   virtual bool           lessThan( const QModelIndex& left, const QModelIndex& right ) const;
145
146   QAbstractItemDelegate* delegate() const;
147
148 public slots:
149   virtual void           updateTree( const QModelIndex& );
150   virtual void           updateTree( SUIT_DataObject* = 0 );
151
152 signals:
153   void modelUpdated();
154
155 private:
156   void                   initialize();
157
158   TreeItem*              rootItem() const;
159   TreeItem*              treeItem( const QModelIndex& ) const;
160   TreeItem*              treeItem( const SUIT_DataObject* ) const;
161   SUIT_DataObject*       object( const TreeItem* ) const;
162
163   TreeItem*              createItem( SUIT_DataObject*, TreeItem* = 0, TreeItem* = 0 );
164   void                   updateItem( TreeItem* );
165   void                   removeItem( TreeItem* );
166
167 private slots:
168   void                   onInserted( SUIT_DataObject*, SUIT_DataObject* );
169   void                   onRemoved( SUIT_DataObject*, SUIT_DataObject* );
170
171 private:
172   typedef QMap<SUIT_DataObject*, TreeItem*> ItemMap;
173   typedef struct
174   {
175     QString myName;
176         QMap<int,int> myIds;
177         QPixmap myIcon;
178         Qtx::Appropriate myAppropriate;
179
180   } ColumnInfo;
181   
182   SUIT_DataObject*    myRoot;
183   TreeItem*           myRootItem;
184   ItemMap             myItems;
185   bool                myAutoDeleteTree;
186   bool                myAutoUpdate;
187   QVector<ColumnInfo> myColumns;
188
189   friend class SUIT_TreeModel::TreeSync;
190 };
191
192 class SUIT_EXPORT SUIT_ProxyModel : public QSortFilterProxyModel, public SUIT_AbstractModel
193 {
194   Q_OBJECT
195
196 public:
197   SUIT_ProxyModel( QObject* = 0 );
198   SUIT_ProxyModel( SUIT_DataObject*, QObject* = 0 );
199   SUIT_ProxyModel( SUIT_AbstractModel*, QObject* = 0 );
200   ~SUIT_ProxyModel();
201
202   SUIT_DataObject*       root() const;
203   void                   setRoot( SUIT_DataObject* );
204
205   SUIT_DataObject*       object( const QModelIndex& = QModelIndex() ) const;
206   QModelIndex            index( const SUIT_DataObject*, int = 0 ) const;
207
208   bool                   autoDeleteTree() const;
209   void                   setAutoDeleteTree( const bool );
210
211   bool                   autoUpdate() const;
212   void                   setAutoUpdate( const bool );
213  
214   bool                   isSortingEnabled() const;
215   bool                   customSorting( const int ) const;
216
217   virtual bool             lessThan( const QModelIndex&, const QModelIndex& ) const;
218   virtual void             registerColumn( const int group_id, const QString& name, const int custom_id );
219   virtual void             unregisterColumn( const int group_id, const QString& name );
220   virtual void             setColumnIcon( const QString& name, const QPixmap& icon );
221   virtual QPixmap          columnIcon( const QString& name ) const;
222   virtual void             setAppropriate( const QString& name, const Qtx::Appropriate appr );
223   virtual Qtx::Appropriate appropriate( const QString& name ) const;
224
225   QAbstractItemDelegate* delegate() const;
226
227 public slots:
228   virtual void           updateTree( const QModelIndex& );
229   virtual void           updateTree( SUIT_DataObject* = 0 );
230   void                   setSortingEnabled( bool );
231
232 signals:
233   void modelUpdated();
234
235 protected:
236   SUIT_AbstractModel*    treeModel() const;
237
238 private:
239   bool                   mySortingEnabled;
240 };
241
242 class SUIT_EXPORT SUIT_ItemDelegate : public QItemDelegate
243 {
244   Q_OBJECT
245
246 public:
247   SUIT_ItemDelegate( QObject* = 0 );
248   
249   virtual void paint( QPainter*, const QStyleOptionViewItem&,
250                       const QModelIndex& ) const;
251 };
252
253 #ifdef WIN32
254 #pragma warning( default:4251 )
255 #endif
256
257 #endif // SUIT_TREEMODEL_H