0031362: Inspectors - MessageView plugin for message alerts
[occt.git] / tools / DFBrowserPane / DFBrowserPane_TDataStdTreeNodeModel.cxx
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 #include <inspector/DFBrowserPane_TDataStdTreeNodeModel.hxx>
17 #include <inspector/DFBrowserPane_TDataStdTreeNodeItem.hxx>
18
19 #include <TDataStd_TreeNode.hxx>
20
21 #include <Standard_WarningsDisable.hxx>
22 #include <QAbstractItemModel>
23 #include <Standard_WarningsRestore.hxx>
24
25 // =======================================================================
26 // function : Constructor
27 // purpose :
28 // =======================================================================
29 DFBrowserPane_TDataStdTreeNodeModel::DFBrowserPane_TDataStdTreeNodeModel (QObject* theParent)
30 : TreeModel_ModelBase (theParent)
31 {
32 }
33
34 // =======================================================================
35 // function : InitColumns
36 // purpose :
37 // =======================================================================
38 void DFBrowserPane_TDataStdTreeNodeModel::InitColumns()
39 {
40   setHeaderItem (0, TreeModel_HeaderSection ("Name"));
41 }
42
43 // =======================================================================
44 // function : createRootItem
45 // purpose :
46 // =======================================================================
47 TreeModel_ItemBasePtr DFBrowserPane_TDataStdTreeNodeModel::createRootItem (const int theColumnId)
48 {
49   return DFBrowserPane_TDataStdTreeNodeItem::CreateItem (TreeModel_ItemBasePtr(), 0, theColumnId);
50 }
51
52 // =======================================================================
53 // function : SetAttribute
54 // purpose :
55 // =======================================================================
56 void DFBrowserPane_TDataStdTreeNodeModel::SetAttribute (const Handle(TDF_Attribute)& theAttribute)
57 {
58   DFBrowserPane_TDataStdTreeNodeItemPtr aRootItem = itemDynamicCast<DFBrowserPane_TDataStdTreeNodeItem>(RootItem (0));
59   Reset();
60   aRootItem->SetAttribute (theAttribute);
61   EmitLayoutChanged();
62 }
63
64 // =======================================================================
65 // function : FindIndex
66 // purpose :
67 // =======================================================================
68 QModelIndex DFBrowserPane_TDataStdTreeNodeModel::FindIndex (const Handle(TDF_Attribute)& theAttribute,
69                                                             const QModelIndex theParentIndex)
70 {
71   QModelIndex aParentIndex = theParentIndex;
72   
73   if (!aParentIndex.isValid())
74     aParentIndex = index (0, 0);
75
76   DFBrowserPane_TDataStdTreeNodeItemPtr aParentItem = itemDynamicCast<DFBrowserPane_TDataStdTreeNodeItem>
77     (TreeModel_ModelBase::GetItemByIndex (aParentIndex));
78
79   if (aParentItem->GetAttribute() == theAttribute)
80     return aParentIndex;
81
82   for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
83   {
84     QModelIndex anIndex = index (aChildId, 0, aParentIndex);
85     TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
86     DFBrowserPane_TDataStdTreeNodeItemPtr anItem = itemDynamicCast<DFBrowserPane_TDataStdTreeNodeItem>(anItemBase);
87
88     if (anItem->GetAttribute() == theAttribute)
89       return anIndex;
90
91     QModelIndex aSubIndex = FindIndex (theAttribute, anIndex);
92     if (aSubIndex.isValid())
93       return aSubIndex;
94   }
95   return QModelIndex();
96 }