1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/TreeModel_ModelBase.hxx>
18 #include <inspector/TreeModel_ItemBase.hxx>
19 #include <inspector/TreeModel_Tools.hxx>
20 #include <inspector/TreeModel_VisibilityState.hxx>
22 #include <Standard_WarningsDisable.hxx>
24 #include <Standard_WarningsRestore.hxx>
26 // =======================================================================
27 // function : Constructor
29 // =======================================================================
30 TreeModel_ModelBase::TreeModel_ModelBase (QObject* theParent)
31 : QAbstractItemModel (theParent), m_pRootItem (0), m_pUseVisibilityColumn (false),
36 // =======================================================================
37 // function : GetItemByIndex
39 // =======================================================================
40 TreeModel_ItemBasePtr TreeModel_ModelBase::GetItemByIndex (const QModelIndex& theIndex)
42 TreeModel_ItemBase* anItem = (TreeModel_ItemBase*)theIndex.internalPointer();
43 return TreeModel_ItemBasePtr (anItem);
46 // =======================================================================
49 // =======================================================================
50 void TreeModel_ModelBase::Reset()
52 for (int aColId = 0, aNbColumns = columnCount(); aColId < aNbColumns; aColId++)
54 TreeModel_ItemBasePtr aRootItem = RootItem (aColId);
60 // =======================================================================
63 // =======================================================================
64 QModelIndex TreeModel_ModelBase::index (int theRow, int theColumn, const QModelIndex& theParent) const
66 if (!hasIndex (theRow, theColumn, theParent))
69 // to create index on the root item
70 if (!theParent.isValid())
71 return createIndex (theRow, theColumn, getIndexValue (RootItem (theColumn)));
73 TreeModel_ItemBasePtr aParentItem;
74 if (!theParent.isValid())
75 aParentItem = RootItem (theColumn);
77 aParentItem = GetItemByIndex (theParent);
79 TreeModel_ItemBasePtr aChildItem = aParentItem->Child (theRow, theColumn);
80 return aChildItem ? createIndex (theRow, theColumn, getIndexValue (aChildItem)) : QModelIndex();
83 // =======================================================================
86 // =======================================================================
87 QVariant TreeModel_ModelBase::data (const QModelIndex& theIndex, int theRole) const
89 if (!theIndex.isValid())
90 return QVariant ("undefined");
92 if (IsUseVisibilityColumn() && theIndex.column() == TreeModel_ColumnType_Visibility)
94 if (theRole != Qt::DecorationRole)
97 TreeModel_ItemBasePtr anItem = GetItemByIndex (theIndex);
98 if (!anItem->data (theIndex, theRole).isNull()) // value is already in cache
99 return anItem->data (theIndex, theRole);
101 if (!anItem->IsInitialized())
104 if (!myVisibilityState || !myVisibilityState->CanBeVisible (theIndex))
107 QVariant aValue = QIcon (myVisibilityState->IsVisible (theIndex) ? ":/icons/item_visible.png"
108 : ":/icons/item_invisible.png");
109 anItem->SetCustomData (aValue, theRole);
113 TreeModel_ItemBasePtr anItem = GetItemByIndex (theIndex);
114 return anItem->data (theIndex, theRole);
117 // =======================================================================
120 // =======================================================================
121 QModelIndex TreeModel_ModelBase::parent (const QModelIndex& theIndex) const
123 if (!theIndex.isValid())
124 return QModelIndex();
126 TreeModel_ItemBasePtr aChildItem = GetItemByIndex (theIndex);
127 TreeModel_ItemBasePtr aParentItem = aChildItem ? aChildItem->Parent() : TreeModel_ItemBasePtr();
130 return QModelIndex();
132 return createIndex (aParentItem->Row(), aParentItem->Column(), getIndexValue (aParentItem));
135 // =======================================================================
138 // =======================================================================
139 Qt::ItemFlags TreeModel_ModelBase::flags (const QModelIndex& theIndex) const
141 if (!theIndex.isValid())
143 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
146 // =======================================================================
147 // function : headerData
149 // =======================================================================
150 QVariant TreeModel_ModelBase::headerData (int theSection, Qt::Orientation theOrientation, int theRole) const
152 if (theOrientation != Qt::Horizontal || theRole != Qt::DisplayRole)
155 if (IsUseVisibilityColumn() && theSection == TreeModel_ColumnType_Visibility)
158 return GetHeaderItem (theSection).GetName();
161 // =======================================================================
162 // function : rowCount
164 // =======================================================================
165 int TreeModel_ModelBase::rowCount (const QModelIndex& theParent) const
167 // to create index on the root item
168 if (!theParent.isValid())
171 TreeModel_ItemBasePtr aParentItem;
172 if (!theParent.isValid())
173 aParentItem = RootItem (0);
175 aParentItem = GetItemByIndex (theParent);
177 return aParentItem ? aParentItem->rowCount() : 0;
180 // =======================================================================
181 // function : EmitLayoutChanged
183 // =======================================================================
184 void TreeModel_ModelBase::EmitLayoutChanged()
186 emit layoutChanged();
189 // =======================================================================
190 // function : EmitLayoutChanged
192 // =======================================================================
193 void TreeModel_ModelBase::EmitDataChanged (const QModelIndex& theTopLeft, const QModelIndex& theBottomRight,
194 const QVector<int>& theRoles,
195 const bool isResetItem)
197 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (theTopLeft);
198 if (anItemBase && isResetItem)
201 #if QT_VERSION < 0x050000
203 emit dataChanged (theTopLeft, theBottomRight);
205 emit dataChanged (theTopLeft, theBottomRight, theRoles);
209 // =======================================================================
210 // function : SingleSelected
212 // =======================================================================
213 QModelIndex TreeModel_ModelBase::SingleSelected (const QModelIndexList& theIndices, const int theCellId,
214 const Qt::Orientation theOrientation)
216 QModelIndexList aFirstColumnSelectedIndices;
217 for (QModelIndexList::const_iterator anIndicesIt = theIndices.begin(); anIndicesIt != theIndices.end(); anIndicesIt++)
219 QModelIndex anIndex = *anIndicesIt;
220 if ((theOrientation == Qt::Horizontal && anIndex.column() == theCellId) ||
221 (theOrientation == Qt::Vertical && anIndex.row() == theCellId))
222 aFirstColumnSelectedIndices.append (anIndex);
224 return aFirstColumnSelectedIndices.size() == 1 ? aFirstColumnSelectedIndices.first() : QModelIndex();
227 // =======================================================================
228 // function : getIndexValue
230 // =======================================================================
231 void* TreeModel_ModelBase::getIndexValue (const TreeModel_ItemBasePtr& theItem)
233 return theItem.data();