Version: 8.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SUIT_TreeModel.h
Go to the documentation of this file.
1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 
20 // File: SUIT_TreeModel.h
21 // Author: Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22 
23 #ifndef SUIT_TREEMODEL_H
24 #define SUIT_TREEMODEL_H
25 
26 #include "SUIT.h"
27 
28 #include <Qtx.h>
29 
30 #include <QAbstractItemModel>
31 #include <QSortFilterProxyModel>
32 #include <QModelIndex>
33 #include <QItemDelegate>
34 #include <QVariant>
35 #include <QMap>
36 
37 #ifdef WIN32
38 #pragma warning( disable:4251 )
39 #endif
40 
41 class SUIT_DataObject;
42 class SUIT_TreeModel;
43 class QMimeData;
44 
46 {
47 public:
48  virtual SUIT_DataObject* findObject( const QString& ) const = 0;
49 };
50 
52 {
54 
55 public:
57 
58  operator const QAbstractItemModel*() const;
59  operator QAbstractItemModel*();
60  operator const QObject*() const;
61 
62  virtual SUIT_DataObject* root() const = 0;
63  virtual void setRoot( SUIT_DataObject* ) = 0;
64  virtual SUIT_DataObject* object( const QModelIndex& = QModelIndex() ) const = 0;
65  virtual QModelIndex index( const SUIT_DataObject*, int = 0 ) const = 0;
66  virtual bool autoDeleteTree() const = 0;
67  virtual void setAutoDeleteTree( const bool ) = 0;
68  virtual bool autoUpdate() const = 0;
69  virtual void setAutoUpdate( const bool ) = 0;
70  virtual bool updateModified() const = 0;
71  virtual void setUpdateModified( const bool ) = 0;
72  virtual QAbstractItemDelegate* delegate() const = 0;
73  virtual bool customSorting( const int ) const = 0;
74  virtual bool lessThan( const QModelIndex& left, const QModelIndex& right ) const = 0;
75  virtual void forgetObject( const SUIT_DataObject* ) = 0;
76 
77  virtual void updateTree( const QModelIndex& ) = 0;
78  virtual void updateTree( SUIT_DataObject* = 0 ) = 0;
79 
80  virtual void registerColumn( const int group_id, const QString& name, const int custom_id ) = 0;
81  virtual void unregisterColumn( const int group_id, const QString& name ) = 0;
82  virtual void setColumnIcon( const QString& name, const QPixmap& icon ) = 0;
83  virtual QPixmap columnIcon( const QString& name ) const = 0;
84  virtual void setAppropriate( const QString& name, const Qtx::Appropriate appr ) = 0;
85  virtual Qtx::Appropriate appropriate( const QString& name ) const = 0;
86  virtual void setVisibilityState(const QString& id, Qtx::VisibilityState state, bool emitChanged = true) = 0;
87  virtual void setVisibilityStateForAll(Qtx::VisibilityState state) = 0;
88  virtual Qtx::VisibilityState visibilityState(const QString& id) const = 0;
89  virtual void setHeaderFlags( const QString& name, const Qtx::HeaderViewFlags flags ) = 0;
90  virtual Qtx::HeaderViewFlags headerFlags( const QString& name ) const = 0;
91  virtual void emitClicked( SUIT_DataObject* obj, const QModelIndex& index) = 0;
92 
93  virtual SUIT_DataSearcher* searcher() const;
94  virtual void setSearcher( SUIT_DataSearcher* );
95 };
96 
97 
99 {
100  Q_OBJECT
101 
102 private:
103  class TreeItem;
104  class TreeSync;
105  typedef SUIT_DataObject* ObjPtr;
107 
108 public:
110  typedef enum {
111  DisplayRole = Qt::DisplayRole,
112  DecorationRole = Qt::DecorationRole,
113  EditRole = Qt::EditRole,
114  ToolTipRole = Qt::ToolTipRole,
115  StatusTipRole = Qt::StatusTipRole,
116  WhatsThisRole = Qt::WhatsThisRole,
117  FontRole = Qt::FontRole,
118  TextAlignmentRole = Qt::TextAlignmentRole,
119  BackgroundRole = Qt::BackgroundRole,
120  ForegroundRole = Qt::ForegroundRole,
121  CheckStateRole = Qt::CheckStateRole,
122  SizeHintRole = Qt::SizeHintRole,
123  BaseColorRole = Qt::UserRole,
127  AppropriateRole = Qtx::AppropriateRole
128  } Role;
129 
130  SUIT_TreeModel( QObject* = 0 );
132  ~SUIT_TreeModel();
133 
134  SUIT_DataObject* root() const;
135  void setRoot( SUIT_DataObject* );
136 
137  virtual QVariant data( const QModelIndex&, int = DisplayRole ) const;
138  virtual bool setData( const QModelIndex&, const QVariant&, int = EditRole );
139  virtual Qt::ItemFlags flags( const QModelIndex& ) const;
140  virtual QVariant headerData( int, Qt::Orientation, int = Qt::DisplayRole ) const;
141 
142  virtual Qt::DropActions supportedDropActions() const;
143 
144  virtual QModelIndex index( int, int, const QModelIndex& = QModelIndex() ) const;
145  virtual QModelIndex parent( const QModelIndex& ) const;
146 
147  virtual int columnCount( const QModelIndex& = QModelIndex() ) const;
148  virtual int rowCount( const QModelIndex& = QModelIndex() ) const;
149  virtual void registerColumn( const int group_id, const QString& name, const int custom_id );
150  virtual void unregisterColumn( const int group_id, const QString& name );
151  virtual void setColumnIcon( const QString& name, const QPixmap& icon );
152  virtual QPixmap columnIcon( const QString& name ) const;
153  virtual void setAppropriate( const QString& name, const Qtx::Appropriate appr );
154  virtual Qtx::Appropriate appropriate( const QString& name ) const;
155  virtual void setVisibilityState(const QString& id, Qtx::VisibilityState state, bool emitChanged = true);
157  virtual Qtx::VisibilityState visibilityState(const QString& id) const;
158  virtual void setHeaderFlags( const QString& name, const Qtx::HeaderViewFlags flags );
159  virtual Qtx::HeaderViewFlags headerFlags( const QString& name ) const;
160  virtual void emitClicked( SUIT_DataObject* obj, const QModelIndex& index);
161 
162 
163  SUIT_DataObject* object( const QModelIndex& = QModelIndex() ) const;
164  QModelIndex index( const SUIT_DataObject*, int = 0 ) const;
165 
166  bool autoDeleteTree() const;
167  void setAutoDeleteTree( const bool );
168 
169  bool autoUpdate() const;
170  void setAutoUpdate( const bool );
171 
172  bool updateModified() const;
173  void setUpdateModified( const bool );
174 
175  virtual bool customSorting( const int ) const;
176  virtual bool lessThan( const QModelIndex& left, const QModelIndex& right ) const;
177  virtual void forgetObject( const SUIT_DataObject* );
178 
179  QAbstractItemDelegate* delegate() const;
180 
181 
182  virtual void updateTreeModel(SUIT_DataObject*,TreeItem*);
183 
184  virtual QStringList mimeTypes() const;
185  virtual QMimeData* mimeData (const QModelIndexList& indexes) const;
186  virtual bool dropMimeData (const QMimeData *data, Qt::DropAction action,
187  int row, int column, const QModelIndex &parent);
188 
189 public slots:
190  virtual void updateTree( const QModelIndex& );
191  virtual void updateTree( SUIT_DataObject* = 0 );
192 
193 signals:
194  void modelUpdated();
195  void clicked( SUIT_DataObject*, int );
196  void dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction );
197  void renamed( SUIT_DataObject* );
198 
199 private:
200  void initialize();
201 
202  TreeItem* rootItem() const;
203  TreeItem* treeItem( const QModelIndex& ) const;
204  TreeItem* treeItem( const SUIT_DataObject* ) const;
205  SUIT_DataObject* object( const TreeItem* ) const;
206  QString objectId( const QModelIndex& = QModelIndex() ) const;
207 
208  TreeItem* createItem( SUIT_DataObject*, TreeItem* = 0, TreeItem* = 0 );
209  TreeItem* createItemAtPos( SUIT_DataObject*, TreeItem* = 0, int pos=0 );
210  void updateItem( TreeItem*, bool emitLayoutChanged );
211  void removeItem( TreeItem* );
212 
213 private slots:
214  void onInserted( SUIT_DataObject*, SUIT_DataObject* );
215  void onRemoved( SUIT_DataObject*, SUIT_DataObject* );
216  void onModified( SUIT_DataObject* );
217 
218 private:
219  typedef QMap<SUIT_DataObject*, TreeItem*> ItemMap;
220  typedef struct
221  {
222  QString myName;
223  QMap<int,int> myIds;
224  QPixmap myIcon;
227 
228  } ColumnInfo;
229 
230  typedef QMap<QString,Qtx::VisibilityState> VisibilityMap;
231 
239  QVector<ColumnInfo> myColumns;
240 
242 };
243 
245 {
246  Q_OBJECT
247 
248 public:
249  SUIT_ProxyModel( QObject* = 0 );
252  ~SUIT_ProxyModel();
253 
254  SUIT_DataObject* root() const;
255  void setRoot( SUIT_DataObject* );
256 
257  SUIT_DataObject* object( const QModelIndex& = QModelIndex() ) const;
258  QModelIndex index( const SUIT_DataObject*, int = 0 ) const;
259 
260  bool autoDeleteTree() const;
261  void setAutoDeleteTree( const bool );
262 
263  bool autoUpdate() const;
264  void setAutoUpdate( const bool );
265 
266  bool updateModified() const;
267  void setUpdateModified( const bool );
268 
269  bool isSortingEnabled() const;
270  bool customSorting( const int ) const;
271 
272  virtual void forgetObject( const SUIT_DataObject* );
273  virtual bool lessThan( const QModelIndex&, const QModelIndex& ) const;
274  virtual void registerColumn( const int group_id, const QString& name, const int custom_id );
275  virtual void unregisterColumn( const int group_id, const QString& name );
276  virtual void setColumnIcon( const QString& name, const QPixmap& icon );
277  virtual QPixmap columnIcon( const QString& name ) const;
278  virtual void setAppropriate( const QString& name, const Qtx::Appropriate appr );
279  virtual Qtx::Appropriate appropriate( const QString& name ) const;
280  virtual void setVisibilityState(const QString& id, Qtx::VisibilityState state, bool emitChanged = true);
282  virtual Qtx::VisibilityState visibilityState(const QString& id) const;
283  virtual void setHeaderFlags( const QString& name, const Qtx::HeaderViewFlags flags );
284  virtual Qtx::HeaderViewFlags headerFlags( const QString& name ) const;
285  virtual void emitClicked( SUIT_DataObject* obj, const QModelIndex& index);
286 
287  virtual SUIT_DataSearcher* searcher() const;
288  virtual void setSearcher( SUIT_DataSearcher* );
289 
290  QAbstractItemDelegate* delegate() const;
291 
292 public slots:
293  virtual void updateTree( const QModelIndex& );
294  virtual void updateTree( SUIT_DataObject* = 0 );
295  void setSortingEnabled( bool );
296 
297 signals:
298  void modelUpdated();
299  void clicked( SUIT_DataObject*, int );
300  void dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction );
301  void renamed( SUIT_DataObject* );
302 
303 protected:
304  SUIT_AbstractModel* treeModel() const;
305  virtual bool filterAcceptsRow( int, const QModelIndex& ) const;
306 
307 private:
309 };
310 
312 {
313  Q_OBJECT
314 
315 public:
316  SUIT_ItemDelegate( QObject* = 0 );
317 
318  virtual void paint( QPainter*, const QStyleOptionViewItem&,
319  const QModelIndex& ) const;
320 
321  virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
322 };
323 
324 #ifdef WIN32
325 #pragma warning( default:4251 )
326 #endif
327 
328 #endif // SUIT_TREEMODEL_H