0029025: TInspector include files are not installed to inc directory by CMake
[occt.git] / tools / DFBrowserPane / DFBrowserPane_TDataStdTreeNode.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_TDataStdTreeNode.hxx>
17
18 #include <inspector/DFBrowserPane_AttributePaneModel.hxx>
19 #include <inspector/DFBrowserPane_TableView.hxx>
20 #include <inspector/DFBrowserPane_TDataStdTreeNodeItem.hxx>
21 #include <inspector/DFBrowserPane_TDataStdTreeNodeModel.hxx>
22 #include <inspector/DFBrowserPane_Tools.hxx>
23
24 #include <TDataStd_TreeNode.hxx>
25
26 #include <QVariant>
27 #include <QTableView>
28 #include <QTreeView>
29 #include <QVBoxLayout>
30 #include <QWidget>
31 #include <QItemSelectionModel>
32
33 // =======================================================================
34 // function : Constructor
35 // purpose :
36 // =======================================================================
37 DFBrowserPane_TDataStdTreeNode::DFBrowserPane_TDataStdTreeNode()
38 : DFBrowserPane_AttributePane()
39 {
40   myModel = new DFBrowserPane_TDataStdTreeNodeModel (0);
41   mySelectionModels.clear(); // do not use selection model of parent pane
42   mySelectionModels.push_back (new QItemSelectionModel (myModel));
43 }
44
45 // =======================================================================
46 // function : CreateWidget
47 // purpose :
48 // =======================================================================
49 QWidget* DFBrowserPane_TDataStdTreeNode::CreateWidget (QWidget* theParent)
50 {
51   QWidget* aMainWidget = new QWidget (theParent);
52   aMainWidget->setVisible (false);
53
54   myTableView = new DFBrowserPane_TableView (aMainWidget, getTableColumnWidths());
55   DFBrowserPane_TableView::SetFixedRowCount (1, myTableView->GetTableView());
56   myTableView->SetModel (myPaneModel);
57
58   QVBoxLayout* aLay = new QVBoxLayout (aMainWidget);
59   aLay->setContentsMargins (0, 0, 0, 0);
60   aLay->addWidget (myTableView);
61
62   myTreeNodeView = new QTreeView (theParent);
63   myTreeNodeView->setModel (myModel);
64   myTreeNodeView->setSelectionModel (mySelectionModels.front());
65   myTreeNodeView->setSelectionBehavior (QAbstractItemView::SelectRows);
66
67   aLay->addWidget (myTreeNodeView);
68   return aMainWidget;
69 }
70
71 // =======================================================================
72 // function : Init
73 // purpose :
74 // =======================================================================
75 void DFBrowserPane_TDataStdTreeNode::Init (const Handle(TDF_Attribute)& theAttribute)
76 {
77   Handle(TDataStd_TreeNode) aTreeNode = Handle(TDataStd_TreeNode)::DownCast (theAttribute);
78
79   bool aDefaultGUID = aTreeNode->ID() != aTreeNode->GetDefaultTreeID();
80   myTableView->setVisible (!aDefaultGUID);
81   if (!aDefaultGUID) {
82     QList<QVariant> aValues;
83     char aStr[256];
84     aTreeNode->ID().ToCString (aStr);
85     TCollection_AsciiString aString(aStr);
86     aValues.append (DFBrowserPane_Tools::ToString(aString));
87     getPaneModel()->Init (aValues);
88   }
89
90
91   DFBrowserPane_TDataStdTreeNodeModel* aModel = dynamic_cast<DFBrowserPane_TDataStdTreeNodeModel*> (myModel);
92   aModel->Reset();
93
94   if (!aTreeNode.IsNull())
95   {
96     Handle(TDataStd_TreeNode) aRootItem = aTreeNode->Root();
97     aModel->SetAttribute (aRootItem);
98
99     QModelIndex anIndex = aModel->FindIndex (theAttribute);
100     if (anIndex.isValid())
101     {
102       myTreeNodeView->setExpanded (anIndex.parent(), true);
103       myTreeNodeView->scrollTo (anIndex);
104
105       TreeModel_ItemBasePtr anAttributeItem = TreeModel_ModelBase::GetItemByIndex (anIndex);
106       DFBrowserPane_TDataStdTreeNodeItemPtr anAttributeNodeItem =
107               itemDynamicCast<DFBrowserPane_TDataStdTreeNodeItem>(anAttributeItem);
108       anAttributeNodeItem->setCurrentAttribute (true);
109     }
110   }
111   aModel->EmitLayoutChanged();
112 }
113
114 // =======================================================================
115 // function : GetShortAttributeInfo
116 // purpose :
117 // =======================================================================
118 void DFBrowserPane_TDataStdTreeNode::GetShortAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
119                                                             QList<QVariant>& theValues)
120 {
121   Handle(TDataStd_TreeNode) aTNAttribute = Handle(TDataStd_TreeNode)::DownCast (theAttribute);
122   bool aDefaultGUID = aTNAttribute->ID() != aTNAttribute->GetDefaultTreeID();
123   QString aGUIDPrefix;
124   if (!aDefaultGUID) {
125     QList<QVariant> aValues;
126     char aStr[256];
127     aTNAttribute->ID().ToCString (aStr);
128     TCollection_AsciiString aString(aStr);
129     aGUIDPrefix = DFBrowserPane_Tools::ToString(aString);
130   }
131
132   if (aTNAttribute->HasFather())
133   {
134     TDF_Label aLabel = aTNAttribute->Father()->Label();
135     theValues.append (QString ("%1 ==> %2").arg(aGUIDPrefix).arg (DFBrowserPane_Tools::GetEntry (aLabel).ToCString()));
136   }
137   else 
138   {
139     Handle(TDataStd_TreeNode) aFirstChild = aTNAttribute->First();
140     QStringList aRefs;
141     while (! aFirstChild.IsNull() ) 
142     {
143       TDF_Label aLabel = aFirstChild->Label();
144       aRefs.append (DFBrowserPane_Tools::GetEntry (aLabel).ToCString());
145       aFirstChild = aFirstChild->Next();
146     }
147     theValues.append (QString ("%1 <== (%2)").arg(aGUIDPrefix).arg (aRefs.join (", ")));
148   }
149 }
150
151 // =======================================================================
152 // function : GetReferences
153 // purpose :
154 // =======================================================================
155 void DFBrowserPane_TDataStdTreeNode::GetReferences (const Handle(TDF_Attribute)& theAttribute,
156                                                     NCollection_List<TDF_Label>& theRefLabels,
157                                                     Handle(Standard_Transient)&)
158 {
159   Handle(TDataStd_TreeNode) anAttribute = Handle(TDataStd_TreeNode)::DownCast (theAttribute);
160   if (anAttribute.IsNull())
161     return;
162
163   theRefLabels.Append (anAttribute->Label());
164 }