0029559: Samples - wrong copyright statement in FuncDemo
[occt.git] / tools / DFBrowserPane / DFBrowserPane_AttributePane.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_AttributePane.hxx>
17 #include <inspector/DFBrowserPane_ItemRole.hxx>
18 #include <inspector/DFBrowserPane_TableView.hxx>
19 #include <inspector/DFBrowserPane_Tools.hxx>
20 #include <inspector/DFBrowserPane_AttributePaneModel.hxx>
21
22 #include <AIS_InteractiveObject.hxx>
23
24 #include <QGridLayout>
25 #include <QHeaderView>
26 #include <QItemSelectionModel>
27 #include <QTableView>
28 #include <QWidget>
29
30 // =======================================================================
31 // function : Constructor
32 // purpose :
33 // =======================================================================
34 DFBrowserPane_AttributePane::DFBrowserPane_AttributePane()
35 : DFBrowserPane_AttributePaneAPI(), myMainWidget (0), myTableView (0)
36 {
37   myPaneModel = new DFBrowserPane_AttributePaneModel();
38
39   getPaneModel()->SetColumnCount (getColumnCount());
40   getPaneModel()->SetHeaderValues (getHeaderValues(Qt::Horizontal), Qt::Horizontal);
41
42   mySelectionModels.push_back (new QItemSelectionModel (myPaneModel));
43 }
44
45 // =======================================================================
46 // function : GetWidget
47 // purpose :
48 // =======================================================================
49 QWidget* DFBrowserPane_AttributePane::GetWidget (QWidget* theParent, const bool isToCreate)
50 {
51   if (!myMainWidget && isToCreate)
52     myMainWidget = CreateWidget (theParent);
53   return myMainWidget;
54 }
55
56 // =======================================================================
57 // function : CreateWidget
58 // purpose :
59 // =======================================================================
60 QWidget* DFBrowserPane_AttributePane::CreateWidget (QWidget* theParent)
61 {
62   QWidget* aMainWidget = new QWidget (theParent);
63   aMainWidget->setVisible (false);
64
65   myTableView = new DFBrowserPane_TableView (aMainWidget, getTableColumnWidths());
66   myTableView->SetModel (myPaneModel);
67   QTableView* aTableView = myTableView->GetTableView();
68   DFBrowserPane_AttributePaneModel* aPaneModel = dynamic_cast<DFBrowserPane_AttributePaneModel*>(myPaneModel);
69   if (aPaneModel)
70   {
71     if (aPaneModel->GetOrientation() == Qt::Vertical)
72       aTableView->horizontalHeader()->setVisible (!aPaneModel->HeaderValues (Qt::Horizontal).isEmpty());
73     else
74       aTableView->verticalHeader()->setVisible (!aPaneModel->HeaderValues (Qt::Vertical).isEmpty());
75   }
76   aTableView->setSelectionModel (mySelectionModels.front());
77   aTableView->setSelectionBehavior (QAbstractItemView::SelectRows);
78
79   QGridLayout* aLay = new QGridLayout (aMainWidget);
80   aLay->setContentsMargins (0, 0, 0, 0);
81   aLay->addWidget (myTableView);
82
83   return aMainWidget;
84 }
85
86 // =======================================================================
87 // function : Init
88 // purpose :
89 // =======================================================================
90 void DFBrowserPane_AttributePane::Init (const Handle(TDF_Attribute)& theAttribute)
91 {
92   QList<QVariant> aValues;
93   GetValues (theAttribute, aValues);
94   getPaneModel()->Init (aValues);
95
96   if (myTableView)
97     myTableView->GetTableView()->resizeColumnToContents (0);
98 }
99
100 // =======================================================================
101 // function : GetAttributeInfo
102 // purpose :
103 // =======================================================================
104 QVariant DFBrowserPane_AttributePane::GetAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
105                                                         int theRole, int theColumnId)
106 {
107   switch (theRole)
108   {
109     case DFBrowserPane_ItemRole_ShortInfo:
110     {
111       QList<QVariant> aValues;
112       GetShortAttributeInfo (theAttribute, aValues);
113       QStringList anInfoList;
114       for (QList<QVariant>::const_iterator aValuesIt = aValues.begin(); aValuesIt != aValues.end(); aValuesIt++)
115         anInfoList.append (aValuesIt->toString());
116       return QVariant (anInfoList.join (", "));
117     }
118     default:
119       return DFBrowserPane_AttributePane::GetAttributeInfoByType (theAttribute.IsNull() ? ""
120                                             : theAttribute->DynamicType()->Name(), theRole, theColumnId);
121   }
122 }
123
124 // =======================================================================
125 // function : GetShortAttributeInfo
126 // purpose :
127 // =======================================================================
128 void DFBrowserPane_AttributePane::GetShortAttributeInfo (const Handle(TDF_Attribute)& theAttribute, QList<QVariant>& theValues)
129 {
130   QList<QVariant> aValues;
131   GetValues(theAttribute, aValues);
132
133   for (int aValuesEvenId  = 1; aValuesEvenId < aValues.size(); aValuesEvenId = aValuesEvenId + 2)
134     theValues.append (aValues[aValuesEvenId]);
135 }
136
137 // =======================================================================
138 // function : GetAttributeInfoByType
139 // purpose :
140 // =======================================================================
141 QVariant DFBrowserPane_AttributePane::GetAttributeInfoByType (const Standard_CString& theAttributeName,
142                                                               int theRole, int theColumnId)
143 {
144   if (theColumnId != 0)
145     return QVariant();
146
147   switch (theRole)
148   {
149     case Qt::DisplayRole:
150     case Qt::ToolTipRole:    return QVariant (theAttributeName);
151     case Qt::DecorationRole: return QIcon (":/icons/attribute.png");
152     case DFBrowserPane_ItemRole_Decoration_40x40: return QIcon (":/icons/attribute_40x40.png");
153     default: break;
154   }
155   return QVariant();
156 }
157
158 // =======================================================================
159 // function : getPaneModel
160 // purpose :
161 // =======================================================================
162 DFBrowserPane_AttributePaneModel* DFBrowserPane_AttributePane::getPaneModel() const
163 {
164   return dynamic_cast<DFBrowserPane_AttributePaneModel*> (myPaneModel);
165 }
166
167 // =======================================================================
168 // function : getTableColumnWidths
169 // purpose :
170 // =======================================================================
171 QMap<int, int> DFBrowserPane_AttributePane::getTableColumnWidths() const
172 {
173   QMap<int, int> aValues;
174   for (int aColumnId = 0, aCount = getPaneModel()->columnCount(); aColumnId < aCount; aColumnId++)
175     aValues.insert (aColumnId, DFBrowserPane_Tools::DefaultPanelColumnWidth (aColumnId));
176   return aValues;
177 }