0027398: Integrate Qt Browser Widget to Open CASCADE Technology
[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 <DFBrowserPane_AttributePane.hxx>
17 #include <DFBrowserPane_ItemRole.hxx>
18 #include <DFBrowserPane_TableView.hxx>
19 #include <DFBrowserPane_Tools.hxx>
20 #include <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 //#define REQUIRE_OCAF_REVIEW:3 : start
31
32 // =======================================================================
33 // function : Constructor
34 // purpose :
35 // =======================================================================
36 DFBrowserPane_AttributePane::DFBrowserPane_AttributePane()
37 : DFBrowserPane_AttributePaneAPI(), myMainWidget (0), myTableView (0), myPaneModel (0)
38 {
39   myPaneModel = new DFBrowserPane_AttributePaneModel();
40
41   QList<QVariant> aHeaderValues;
42   aHeaderValues << "Values";
43   getPaneModel()->SetHeaderValues (aHeaderValues, Qt::Horizontal);
44
45   mySelectionModels.push_back (new QItemSelectionModel (myPaneModel));
46 }
47
48 // =======================================================================
49 // function : GetWidget
50 // purpose :
51 // =======================================================================
52 QWidget* DFBrowserPane_AttributePane::GetWidget (QWidget* theParent, const bool isToCreate)
53 {
54   if (!myMainWidget && isToCreate)
55     myMainWidget = CreateWidget (theParent);
56   return myMainWidget;
57 }
58
59 // =======================================================================
60 // function : CreateWidget
61 // purpose :
62 // =======================================================================
63 QWidget* DFBrowserPane_AttributePane::CreateWidget (QWidget* theParent)
64 {
65   QWidget* aMainWidget = new QWidget (theParent);
66   aMainWidget->setVisible (false);
67
68   myTableView = new DFBrowserPane_TableView (aMainWidget, getTableColumnWidths());
69   myTableView->SetModel (myPaneModel);
70   QTableView* aTableView = myTableView->GetTableView();
71   aTableView->setSelectionModel (mySelectionModels.front());
72   aTableView->setSelectionBehavior (QAbstractItemView::SelectRows);
73
74   QGridLayout* aLay = new QGridLayout (aMainWidget);
75   aLay->setContentsMargins (0, 0, 0, 0);
76   aLay->addWidget (myTableView);
77
78   return aMainWidget;
79 }
80
81 // =======================================================================
82 // function : Init
83 // purpose :
84 // =======================================================================
85 void DFBrowserPane_AttributePane::Init (const Handle(TDF_Attribute)& theAttribute)
86 {
87   QList<QVariant> aValues;
88   GetValues (theAttribute, aValues);
89   getPaneModel()->Init (aValues);
90 }
91
92 // =======================================================================
93 // function : GetAttributeInfo
94 // purpose :
95 // =======================================================================
96 QVariant DFBrowserPane_AttributePane::GetAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
97                                                         int theRole, int theColumnId)
98 {
99   switch (theRole)
100   {
101     case DFBrowserPane_ItemRole_ShortInfo:
102     {
103       QList<QVariant> aValues;
104       GetShortAttributeInfo (theAttribute, aValues);
105       QStringList anInfoList;
106       for (QList<QVariant>::const_iterator aValuesIt = aValues.begin(); aValuesIt != aValues.end(); aValuesIt++)
107         anInfoList.append (aValuesIt->toString());
108       return QVariant (anInfoList.join (", "));
109     }
110     default:
111       return DFBrowserPane_AttributePane::GetAttributeInfoByType (theAttribute.IsNull() ? ""
112                                             : theAttribute->DynamicType()->Name(), theRole, theColumnId);
113   }
114 }
115
116 // =======================================================================
117 // function : GetAttributeInfoByType
118 // purpose :
119 // =======================================================================
120 QVariant DFBrowserPane_AttributePane::GetAttributeInfoByType (const Standard_CString& theAttributeName,
121                                                               int theRole, int theColumnId)
122 {
123   if (theColumnId != 0)
124     return QVariant();
125
126   switch (theRole)
127   {
128     case Qt::DisplayRole:
129     case Qt::ToolTipRole:    return QVariant (theAttributeName);
130     case Qt::DecorationRole: return QIcon (":/icons/attribute.png");
131     case DFBrowserPane_ItemRole_Decoration_40x40: return QIcon (":/icons/attribute_40x40.png");
132     default: break;
133   }
134   return QVariant();
135 }
136
137 // =======================================================================
138 // function : getPaneModel
139 // purpose :
140 // =======================================================================
141 DFBrowserPane_AttributePaneModel* DFBrowserPane_AttributePane::getPaneModel() const
142 {
143   return dynamic_cast<DFBrowserPane_AttributePaneModel*> (myPaneModel);
144 }
145
146 // =======================================================================
147 // function : getTableColumnWidths
148 // purpose :
149 // =======================================================================
150 QMap<int, int> DFBrowserPane_AttributePane::getTableColumnWidths() const
151 {
152   QMap<int, int> aValues;
153   for (int aColumnId = 0, aCount = getPaneModel()->columnCount(); aColumnId < aCount; aColumnId++)
154     aValues.insert (aColumnId, DFBrowserPane_Tools::DefaultPanelColumnWidth (aColumnId));
155   return aValues;
156 }
157 //#define REQUIRE_OCAF_REVIEW:2 : end