1 // Created on: 2017-06-16 2 // Created by: Natalia ERMOLAEVA 3 // Copyright (c) 2017 OPEN CASCADE SAS 4 // 5 // This file is part of Open CASCADE Technology software library. 6 // 7 // This library is free software; you can redistribute it and/or modify it under 8 // the terms of the GNU Lesser General Public License version 2.1 as published 9 // by the Free Software Foundation, with special exception defined in the file 10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 11 // distribution for complete text of the license and disclaimer of any warranty. 12 // 13 // Alternatively, this file may be used under the terms of Open CASCADE 14 // commercial license or contractual agreement. 15 16 #ifndef TreeModel_ModelBase_H 17 #define TreeModel_ModelBase_H 18 19 #include <Standard.hxx> 20 #include <inspector/TreeModel_ItemBase.hxx> 21 #include <inspector/TreeModel_HeaderSection.hxx> 22 23 #include <NCollection_List.hxx> 24 25 #include <Standard_WarningsDisable.hxx> 26 #include <QAbstractItemModel> 27 #include <QExplicitlySharedDataPointer> 28 #include <QMap> 29 #include <QModelIndex> 30 #include <QVariant> 31 #include <QVector> 32 #include <Standard_WarningsRestore.hxx> 33 34 class TreeModel_VisibilityState; 35 36 //! \class TreeModel_ModelBase 37 //! \brief Implementation of the tree item based model of QAbstractItemModel. 38 //! The TreeModel_ModelBase class defines the abstract model realization through the base item architecture. 39 //! By the model index creation, a base item is created and attached to the index. 40 //! Each item contains an iformation about the item parent, position in the parent and 41 //! the item's children. So, it is possible to get the model index relation from the item. 42 class TreeModel_ModelBase : public QAbstractItemModel 43 { 44 public: 45 46 //! Constructor 47 //! \param theParent the parent object 48 Standard_EXPORT TreeModel_ModelBase (QObject* theParent = 0); 49 50 //! Destructor ~TreeModel_ModelBase()51 virtual ~TreeModel_ModelBase() {} 52 53 //! Creates model columns and root items. 54 //! Default columns are: [0] - Name, [1] - Visibility, [2] - Row 55 Standard_EXPORT virtual void InitColumns(); 56 57 //! Returns the item shared pointer by the model index 58 //! if it is in the index internal pointer 59 //! @param theIndex a model index 60 Standard_EXPORT static TreeModel_ItemBasePtr GetItemByIndex (const QModelIndex& theIndex); 61 62 //! Resets the model items content. Calls the same method of the root item. 63 //! It leads to reset of all child/sub child items. 64 Standard_EXPORT virtual void Reset(); 65 66 //! Returns the model root item. 67 //! It is realized for OCAFBrowser RootItem(const int theColumn) const68 TreeModel_ItemBasePtr RootItem (const int theColumn) const { return myRootItems[theColumn]; } 69 70 //! Emits the layoutChanged signal from outside of this class 71 Standard_EXPORT void EmitLayoutChanged(); 72 73 //! Emits the dataChanged signal from outside of this class 74 Standard_EXPORT void EmitDataChanged (const QModelIndex& theTopLeft, const QModelIndex& theBottomRight, 75 const QVector<int>& theRoles = QVector<int>(), const bool isResetItem = true); 76 77 //! Sets state whether visibility column (0) is used in the model 78 //! \param theState state SetUseVisibilityColumn(const bool theState)79 void SetUseVisibilityColumn (const bool theState) { m_pUseVisibilityColumn = theState; } 80 81 //! Returns state whether visibility column (0) is used in the model 82 //! \param theState state IsUseVisibilityColumn() const83 bool IsUseVisibilityColumn() const { return m_pUseVisibilityColumn; } 84 85 //! Fills visibility state checker 86 //! \param theController the checker interface SetVisibilityState(TreeModel_VisibilityState * theController)87 void SetVisibilityState (TreeModel_VisibilityState* theController) { myVisibilityState = theController; } 88 89 //! Returns visibility state checker 90 //! \return the checker interface GetVisibilityState() const91 TreeModel_VisibilityState* GetVisibilityState () const { return myVisibilityState; } 92 93 //! Returns true if the tree view model contains highlighted items. This highlight is set manually. HasHighlighted()94 bool HasHighlighted() { return !myHighlightedIndices.isEmpty(); } 95 96 //! Sets items of the indices highlighted in the model. 97 //! \param theIndices a list of tree model indices SetHighlighted(const QModelIndexList & theIndices=QModelIndexList ())98 void SetHighlighted (const QModelIndexList& theIndices = QModelIndexList()) { myHighlightedIndices = theIndices; } 99 100 //! Returns the index of the item in the model specified by the given row, column and parent index. 101 //! Saves an internal pointer at the createIndex. This pointer is a shared pointer to the class, 102 //! that realizes a base item interface. If the parent is invalid, a root item is used, otherwise a new item 103 //! is created by the pointer item saved the parent model index 104 //! \param theRow the index row position 105 //! \param theColummn the index column position 106 //! \param theParent the parent index 107 //! \return the model index 108 Standard_EXPORT virtual QModelIndex index (int theRow, int theColumn, 109 const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE; 110 111 //! Returns the data stored under the given role for the item referred to by the index. 112 //! \param theIndex a model index 113 //! \param theRole an enumeration value of role for data obtaining 114 Standard_EXPORT virtual QVariant data (const QModelIndex& theIndex, int theRole = Qt::DisplayRole) const Standard_OVERRIDE; 115 116 //! Returns the parent index by the child index. Founds the item, saved in the index; 117 //! obtains the parent item by the item. Create a new index by the item and containing it. 118 //! \param theIndex a model index 119 Standard_EXPORT virtual QModelIndex parent (const QModelIndex& theIndex) const Standard_OVERRIDE; 120 121 //! Returns the item flags for the given index. The base class implementation returns a combination of flags that 122 //! enables the item (ItemIsEnabled) and allows it to be selected (ItemIsSelectable) 123 //! \param theIndex the model index 124 //! \return Qt flag combination 125 Standard_EXPORT virtual Qt::ItemFlags flags (const QModelIndex& theIndex) const Standard_OVERRIDE; 126 127 //! Returns the header data for the given role and section in the header with the specified orientation. 128 //! \param theSection the header section. For horizontal headers - column number, for vertical headers - row number. 129 //! \param theOrientation a header orientation 130 //! \param theRole a data role 131 //! \return the header data 132 Standard_EXPORT virtual QVariant headerData (int theSection, Qt::Orientation theOrientation, 133 int theRole = Qt::DisplayRole) const Standard_OVERRIDE; 134 135 //! Returns the number of rows under the given parent. When the parent is valid it means that rowCount is returning 136 //! the number of children of parent. 137 //! \param theParent a parent model index 138 //! \return the number of rows 139 Standard_EXPORT virtual int rowCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE; 140 141 //! Returns header item, that can be modified 142 //! \param theColumnId a column index 143 //! \return header section value ChangeHeaderItem(const int theColumnId)144 TreeModel_HeaderSection* ChangeHeaderItem (const int theColumnId) { return &myHeaderValues[theColumnId]; } 145 146 //! Returns count of columns in the model 147 //! \param theParent an index of the parent item 148 //! \return integer value columnCount(const QModelIndex & theParent=QModelIndex ()) const149 virtual int columnCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE 150 { (void)theParent; return myHeaderValues.size(); } 151 152 //! Returns default value of the visibility column 153 //! \return integer value ColumnVisibilityWidth()154 static int ColumnVisibilityWidth() { return 20; } 155 156 //! Returns selected items in the cell of given orientation. 157 //! \param theIndices a container of selected indices 158 //! \param theCellId column index if orientation is horizontal, row index otherwise 159 //! \param theOrientation an orientation to apply the cell index 160 //! \return model indices from the list 161 Standard_EXPORT static QModelIndexList Selected (const QModelIndexList& theIndices, const int theCellId, 162 const Qt::Orientation theOrientation = Qt::Horizontal); 163 164 //! Returns single selected item in the cell of given orientation. If the orientation is Horizontal, 165 //! in the cell id column, one row should be selected. 166 //! \param theIndices a container of selected indices 167 //! \param theCellId column index if orientation is horizontal, row index otherwise 168 //! \param theOrientation an orientation to apply the cell index 169 //! \return model index from the list 170 Standard_EXPORT static QModelIndex SingleSelected (const QModelIndexList& theIndices, const int theCellId, 171 const Qt::Orientation theOrientation = Qt::Horizontal); 172 173 //! Returns selected tree model items for indices. 174 //! \param theIndices a container of selected indices 175 //! \return model items from the list 176 Standard_EXPORT static QList<TreeModel_ItemBasePtr> SelectedItems (const QModelIndexList& theIndices); 177 178 //! Returns presentations of sub items 179 //! \param theIndices a container of selected indices 180 //! \thePresentations [out] container of presentations 181 Standard_EXPORT static void SubItemsPresentations (const QModelIndexList& theIndices, 182 NCollection_List<Handle(Standard_Transient)>& thePresentations); 183 184 protected: 185 //! Creates root item 186 //! \param theColumnId index of a column 187 virtual TreeModel_ItemBasePtr createRootItem (const int theColumnId) = 0; 188 189 //! Sets header properties item. 190 //! \param theColumnId a column index 191 //! \param theSection a section value 192 Standard_EXPORT void setHeaderItem (const int theColumnId, const TreeModel_HeaderSection& theSection); 193 194 //! Converts the item shared pointer to void* type 195 //! \param theItem 196 //! \return an item pointer 197 Standard_EXPORT static void* getIndexValue (const TreeModel_ItemBasePtr& theItem); 198 199 //! Returns presentations of sub items. Recursive method to get presentations of all children 200 //! \param theItem an item to get own presentations and presentations of children 201 //! \thePresentations [out] container of presentations found 202 static void subItemsPresentations (const TreeModel_ItemBasePtr& theItem, 203 NCollection_List<Handle(Standard_Transient)>& thePresentations); 204 205 private: 206 //! Creates root item 207 //! \param theColumnId index of a column 208 Standard_EXPORT void createRoot (const int theColumnId); 209 210 protected: 211 212 QMap<int, TreeModel_ItemBasePtr> myRootItems; //!< container of root items, for each column own root item 213 QMap<int, TreeModel_HeaderSection> myHeaderValues; //!< header values 214 //!< model subclass. The model is fulfilled by this item content 215 216 bool m_pUseVisibilityColumn; //!< the state whether column=0 is reserved for Visibility state 217 TreeModel_VisibilityState* myVisibilityState; //!< the interface of item visibility 218 QIcon myVisibleIcon; //!< icon of visible state 219 QIcon myInvisibleIcon; //!< icon of invisible state 220 221 QModelIndexList myHighlightedIndices; //!< tree model indices that should be visualized as highlighted 222 }; 223 224 #endif 225