0029018: Documentation - Provide user guide for Qt browser
[occt.git] / tools / DFBrowserPane / DFBrowserPane_TNamingNaming.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_TNamingNaming.hxx>
17
18 #include <AIS_Shape.hxx>
19 #include <BRep_Builder.hxx>
20
21 #include <inspector/DFBrowserPane_AttributePaneModel.hxx>
22 #include <inspector/DFBrowserPane_TableView.hxx>
23 #include <inspector/DFBrowserPane_Tools.hxx>
24
25 #include <TDF_Label.hxx>
26 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
27 #include <TNaming_Name.hxx>
28 #include <TNaming_Naming.hxx>
29 #include <TNaming_NamedShape.hxx>
30
31 #include <TopoDS_Compound.hxx>
32
33 #include <QGridLayout>
34 #include <QHeaderView>
35 #include <QTableView>
36 #include <QVariant>
37 #include <QWidget>
38
39 // =======================================================================
40 // function : Constructor
41 // purpose :
42 // =======================================================================
43 DFBrowserPane_TNamingNaming::DFBrowserPane_TNamingNaming()
44 : DFBrowserPane_AttributePane(), myNamingView (0)
45 {
46   myNamingModel = new DFBrowserPane_AttributePaneModel();
47   myNamingModel->SetColumnCount (2);
48 }
49
50 // =======================================================================
51 // function : CreateWidget
52 // purpose :
53 // =======================================================================
54 QWidget* DFBrowserPane_TNamingNaming::CreateWidget (QWidget* theParent)
55 {
56   QWidget* aMainWidget = new QWidget (theParent);
57
58   myNamingView = new DFBrowserPane_TableView (aMainWidget);
59   myNamingView->GetTableView()->verticalHeader()->setVisible (false);
60   myNamingView->GetTableView()->horizontalHeader()->setVisible (false);
61   myNamingView->SetModel (myNamingModel);
62
63   myTableView = new DFBrowserPane_TableView (aMainWidget);
64   myTableView->SetModel (getPaneModel());
65   myTableView->GetTableView()->setSelectionModel (mySelectionModels.front());
66
67   QGridLayout* aLay = new QGridLayout (aMainWidget);
68   aLay->setContentsMargins (0, 0, 0, 0);
69   aLay->addWidget (myNamingView);
70   aLay->addWidget (myTableView);
71   aLay->setRowStretch (1, 1);
72
73   return aMainWidget;
74 }
75
76 // =======================================================================
77 // function : Init
78 // purpose :
79 // =======================================================================
80 void DFBrowserPane_TNamingNaming::Init (const Handle(TDF_Attribute)& theAttribute)
81 {
82   QList<QVariant> aValues;
83   GetValues (theAttribute, aValues);
84
85   QList<QVariant> aNamingValues;
86   for (int anValueId = 0; anValueId < 14; anValueId++)
87     aNamingValues.append (aValues[anValueId]);
88   myNamingModel->Init (aNamingValues);
89   if (myNamingView)
90     myNamingView->GetTableView()->resizeColumnToContents (0);
91
92   QList<QVariant> aNamedShapesValues;
93   for (int anValueId = 14, aCount = aValues.size(); anValueId < aCount; anValueId++)
94     aNamedShapesValues.append (aValues[anValueId]);
95
96   getPaneModel()->Init (aNamedShapesValues);
97   if (myTableView)
98     myTableView->GetTableView()->resizeColumnToContents (0);
99 }
100
101 // =======================================================================
102 // function : GetValues
103 // purpose :
104 // =======================================================================
105 void DFBrowserPane_TNamingNaming::GetValues (const Handle(TDF_Attribute)& theAttribute, QList<QVariant>& theValues)
106 {
107   Handle(TNaming_Naming) anAttribute = Handle(TNaming_Naming)::DownCast (theAttribute);
108   if (anAttribute.IsNull())
109     return;
110
111   TNaming_Name aNamingName = anAttribute->GetName();
112
113   // values from 0-13
114   theValues.append ("Type");
115   theValues.append (DFBrowserPane_Tools::ToName (DB_NAMING_TYPE, aNamingName.Type()).ToCString());
116   theValues.append ("ShapeType");
117   theValues.append (DFBrowserPane_Tools::ToName (DB_SHAPE_TYPE, aNamingName.ShapeType()).ToCString());
118   Handle(TNaming_NamedShape) aStopShape = aNamingName.StopNamedShape();
119   theValues.append ("StopNamedShape");
120   theValues.append (!aStopShape.IsNull() ? DFBrowserPane_Tools::GetEntry (aStopShape->Label()).ToCString() : "");
121   theValues.append ("Index");
122   theValues.append (QString::number (aNamingName.Index()));
123   TopoDS_Shape aShape = aNamingName.Shape();
124   theValues.append ("Shape(TShape)");
125   theValues.append (!aShape.IsNull() ? DFBrowserPane_Tools::GetPointerInfo (aShape.TShape()->This()).ToCString() : "");
126   TDF_Label aContextLabel = aNamingName.ContextLabel();
127   theValues.append ("ContextLabel");
128   theValues.append (!aContextLabel.IsNull() ? DFBrowserPane_Tools::GetEntry (aContextLabel).ToCString() : "");
129   theValues.append ("Orientation");
130   theValues.append (DFBrowserPane_Tools::ToName (DB_ORIENTATION_TYPE, aNamingName.Orientation()).ToCString());
131
132   // values from 14 till count of arguments
133   int anArgIndex = 1;
134   for (TNaming_ListIteratorOfListOfNamedShape anArgIt(aNamingName.Arguments()); anArgIt.More(); anArgIt.Next(), anArgIndex++)
135   {
136     theValues << "Argument";
137     theValues.append (DFBrowserPane_Tools::GetEntry (anArgIt.Value()->Label()).ToCString());
138   }
139 }
140
141 // =======================================================================
142 // function : GetPresentation
143 // purpose :
144 // =======================================================================
145 Handle(Standard_Transient) DFBrowserPane_TNamingNaming::GetPresentation (const Handle (TDF_Attribute)& theAttribute)
146 {
147   Handle(Standard_Transient) aPresentation;
148   Handle(TNaming_Naming) anAttribute = Handle(TNaming_Naming)::DownCast (theAttribute);
149   if (anAttribute.IsNull())
150     return aPresentation;
151
152   DFBrowserPane_TableView* aTableView = getTableView();
153   if (!aTableView) // the pane is not visualized yet
154     return aPresentation;
155
156   QStringList aSelectedEntries = DFBrowserPane_TableView::GetSelectedColumnValues (aTableView->GetTableView(), 1);
157   TNaming_Name aNamingName = anAttribute->GetName();
158
159   BRep_Builder aBuilder;
160   TopoDS_Compound aComp;
161   aBuilder.MakeCompound (aComp);
162   bool aHasShapes = false;
163   for (TNaming_ListIteratorOfListOfNamedShape aNamingIt(aNamingName.Arguments()); aNamingIt.More(); aNamingIt.Next())
164   {
165     Handle(TNaming_NamedShape) aShapeAttr = aNamingIt.Value();
166     if (aShapeAttr.IsNull())
167       continue;
168     TDF_Label aLabel = aShapeAttr->Label();
169     if (!aSelectedEntries.contains (DFBrowserPane_Tools::GetEntry (aLabel).ToCString()))
170       continue;
171     aBuilder.Add (aComp, aShapeAttr->Get());
172     aHasShapes = true;
173   }
174   TopoDS_Shape aShape = aComp;
175   if (!aShape.IsNull() && aHasShapes)
176     aPresentation = new AIS_Shape (aShape);
177
178   return aPresentation;
179 }
180
181 // =======================================================================
182 // function : GetReferences
183 // purpose :
184 // =======================================================================
185 void DFBrowserPane_TNamingNaming::GetReferences (const Handle(TDF_Attribute)& theAttribute,
186                                                  NCollection_List<TDF_Label>& theRefLabels,
187                                                  Handle(Standard_Transient)&)
188 {
189   Handle(TNaming_Naming) anAttribute = Handle(TNaming_Naming)::DownCast (theAttribute);
190   if (anAttribute.IsNull())
191     return;
192
193   QStringList aSelectedEntries = DFBrowserPane_TableView::GetSelectedColumnValues (getTableView()->GetTableView(), 1);
194   for (TNaming_ListIteratorOfListOfNamedShape aNamingIt(anAttribute->GetName().Arguments()); aNamingIt.More(); aNamingIt.Next())
195   {
196     Handle(TNaming_NamedShape) aShapeAttr = aNamingIt.Value();
197     if (aShapeAttr.IsNull())
198       continue;
199     TDF_Label aLabel = aShapeAttr->Label();
200     if (aSelectedEntries.contains (DFBrowserPane_Tools::GetEntry (aLabel).ToCString()))
201       theRefLabels.Append (aLabel);
202   }
203 }