3b306b4224ba700f0212549e5e3e476f7258c3be
[occt.git] / tools / DFBrowserPane / DFBrowserPane_AttributePaneModel.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_AttributePaneModel.hxx>
17
18 #include <QApplication>
19 #include <QFont>
20 #include <QColor>
21
22 // =======================================================================
23 // function : Constructor
24 // purpose :
25 // =======================================================================
26 DFBrowserPane_AttributePaneModel::DFBrowserPane_AttributePaneModel (QObject* theParent)
27 : QAbstractTableModel (theParent), myOrientation (Qt::Vertical), myColumnCount (1)
28 {
29   myItalicColumns.append (0);
30 }
31
32 // =======================================================================
33 // function : Init
34 // purpose :
35 // =======================================================================
36 void DFBrowserPane_AttributePaneModel::Init (const QList<QVariant>& theValues)
37 {
38   myValuesMap.clear();
39
40   if (myOrientation == Qt::Vertical)
41   {
42     int aRows = theValues.size() / myColumnCount;
43     QList<QVariant> aRowValues;
44     int aValuesIndex = 0;
45     for (int aRowId = 0; aRowId < aRows; aRowId++)
46     {
47       aRowValues.clear();
48       for (int aColumnId = 0; aColumnId < myColumnCount; aColumnId++)
49       {
50         aRowValues.append (theValues[aValuesIndex]);
51         aValuesIndex++;
52       }
53       myValuesMap[aRowId] = aRowValues;
54     }
55   }
56   else {
57     int aCols = theValues.size() / myColumnCount;
58     QList<QVariant> aColValues;
59     int aValuesIndex = 0;
60     for (int aColumnId = 0; aColumnId < aCols; aColumnId++)
61     {
62       aColValues.clear();
63       for (int aRowId = 0; aRowId < myColumnCount; aRowId++)
64       {
65         aColValues.append (theValues[aValuesIndex]);
66         aValuesIndex++;
67       }
68       myValuesMap[aColumnId] = aColValues;
69     }
70   }
71   emit layoutChanged();
72 }
73
74 // =======================================================================
75 // function : SetHeaderValues
76 // purpose :
77 // =======================================================================
78 void DFBrowserPane_AttributePaneModel::SetHeaderValues (const QList<QVariant>& theValues,
79                                                         Qt::Orientation theOrientation)
80 {
81   if (theOrientation == Qt::Horizontal)
82     myHorizontalHeaderValues = theValues;
83   else
84     myVerticalHeaderValues = theValues;
85 }
86
87 // =======================================================================
88 // function : columnCount
89 // purpose :
90 // =======================================================================
91 int DFBrowserPane_AttributePaneModel::columnCount (const QModelIndex&/* theParent*/) const
92 {
93   return myOrientation == Qt::Vertical ? myColumnCount : myValuesMap.size();
94 }
95
96 // =======================================================================
97 // function : rowCount
98 // purpose :
99 // =======================================================================
100 int DFBrowserPane_AttributePaneModel::rowCount (const QModelIndex&/* theParent*/) const
101 {
102   return myOrientation == Qt::Vertical ? myValuesMap.size() : myColumnCount;
103 }
104
105 // =======================================================================
106 // function : data
107 // purpose :
108 // =======================================================================
109 QVariant DFBrowserPane_AttributePaneModel::data (const QModelIndex& theIndex, int theRole) const
110 {
111   QVariant aValue;
112
113   if (theRole == Qt::DisplayRole)
114   {
115     if (myOrientation == Qt::Vertical)
116     {
117       int aRowId = theIndex.row();
118       QList<QVariant> aRowValues = myValuesMap[aRowId];
119       aValue = aRowValues.at (theIndex.column());
120     }
121     else
122     {
123       int aColId = theIndex.column();
124       QList<QVariant> aColValues = myValuesMap[aColId];
125       aValue = aColValues.at (theIndex.row());
126     }
127   }
128   if (myItalicColumns.contains (theIndex.column()) && theRole == Qt::FontRole)
129   {
130     QFont aFont = qApp->font();
131     aFont.setItalic (true);
132     return aFont;
133   }
134   if (myItalicColumns.contains (theIndex.column()) && theRole == Qt::ForegroundRole)
135     return QColor (Qt::darkGray).darker(150);
136
137   return aValue;
138 }
139
140 // =======================================================================
141 // function : headerData
142 // purpose :
143 // =======================================================================
144 QVariant DFBrowserPane_AttributePaneModel::headerData (int theSection, Qt::Orientation theOrientation,
145                                                        int theRole) const
146 {
147   QVariant aValue = QAbstractTableModel::headerData (theSection, theOrientation, theRole);
148   if (theRole == Qt::DisplayRole)
149   {
150     if (theOrientation == Qt::Horizontal)
151     {
152       if (!myHorizontalHeaderValues.empty() && theSection < myHorizontalHeaderValues.size())
153         aValue = myHorizontalHeaderValues[theSection];
154     }
155     else
156     { // vertical
157       if (!myVerticalHeaderValues.empty() && theSection < myVerticalHeaderValues.size())
158         aValue = myVerticalHeaderValues[theSection];
159     }
160   }
161   return aValue;
162 }