File: TreeModel_ModelBase.cxx

package info (click to toggle)
opencascade 7.3.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 278,376 kB
  • sloc: cpp: 1,136,010; ansic: 81,569; tcl: 14,864; cs: 5,173; java: 1,522; xml: 468; sh: 375; perl: 37; makefile: 25
file content (234 lines) | stat: -rw-r--r-- 8,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Created on: 2017-06-16
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2017 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement. 

#include <inspector/TreeModel_ModelBase.hxx>

#include <inspector/TreeModel_ItemBase.hxx>
#include <inspector/TreeModel_Tools.hxx>
#include <inspector/TreeModel_VisibilityState.hxx>

#include <Standard_WarningsDisable.hxx>
#include <QIcon>
#include <Standard_WarningsRestore.hxx>

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
TreeModel_ModelBase::TreeModel_ModelBase (QObject* theParent)
: QAbstractItemModel (theParent), m_pRootItem (0), m_pUseVisibilityColumn (false),
  myVisibilityState (0)
{
}

// =======================================================================
// function :  GetItemByIndex
// purpose :
// =======================================================================
TreeModel_ItemBasePtr TreeModel_ModelBase::GetItemByIndex (const QModelIndex& theIndex)
{
  TreeModel_ItemBase* anItem = (TreeModel_ItemBase*)theIndex.internalPointer();
  return TreeModel_ItemBasePtr (anItem);
}

// =======================================================================
// function :  reset
// purpose :
// =======================================================================
void TreeModel_ModelBase::Reset()
{
  for (int aColId = 0, aNbColumns = columnCount(); aColId < aNbColumns; aColId++)
  {
    TreeModel_ItemBasePtr aRootItem = RootItem (aColId);
    if (aRootItem)
      aRootItem->Reset();
  }
}

// =======================================================================
// function :  index
// purpose :
// =======================================================================
QModelIndex TreeModel_ModelBase::index (int theRow, int theColumn, const QModelIndex& theParent) const
{
  if (!hasIndex (theRow, theColumn, theParent))
    return QModelIndex();

  // to create index on the root item
  if (!theParent.isValid())
    return createIndex (theRow, theColumn, getIndexValue (RootItem (theColumn)));

  TreeModel_ItemBasePtr aParentItem;
  if (!theParent.isValid())
    aParentItem = RootItem (theColumn);
  else
    aParentItem = GetItemByIndex (theParent);

  TreeModel_ItemBasePtr aChildItem = aParentItem->Child (theRow, theColumn);
  return aChildItem ? createIndex (theRow, theColumn, getIndexValue (aChildItem)) : QModelIndex();
}

// =======================================================================
// function :  data
// purpose :
// =======================================================================
QVariant TreeModel_ModelBase::data (const QModelIndex& theIndex, int theRole) const
{
  if (!theIndex.isValid())
    return QVariant ("undefined");

  if (IsUseVisibilityColumn() && theIndex.column() == TreeModel_ColumnType_Visibility)
  {
    if (theRole != Qt::DecorationRole)
      return QVariant();

    TreeModel_ItemBasePtr anItem = GetItemByIndex (theIndex);
    if (!anItem->data (theIndex, theRole).isNull()) // value is already in cache
      return anItem->data (theIndex, theRole);

    if (!anItem->IsInitialized())
      anItem->Init();

    if (!myVisibilityState || !myVisibilityState->CanBeVisible (theIndex))
      return QVariant();

    QVariant aValue = QIcon (myVisibilityState->IsVisible (theIndex) ? ":/icons/item_visible.png"
                                                                     : ":/icons/item_invisible.png");
    anItem->SetCustomData (aValue, theRole);
    return aValue;
  }

  TreeModel_ItemBasePtr anItem = GetItemByIndex (theIndex);
  return anItem->data (theIndex, theRole);
}

// =======================================================================
// function :  parent
// purpose :
// =======================================================================
QModelIndex TreeModel_ModelBase::parent (const QModelIndex& theIndex) const
{
  if (!theIndex.isValid())
    return QModelIndex();

  TreeModel_ItemBasePtr aChildItem = GetItemByIndex (theIndex);
  TreeModel_ItemBasePtr aParentItem = aChildItem ? aChildItem->Parent() : TreeModel_ItemBasePtr();

  if (!aParentItem)
    return QModelIndex();

  return createIndex (aParentItem->Row(), aParentItem->Column(), getIndexValue (aParentItem));
}

// =======================================================================
// function :  flags
// purpose :
// =======================================================================
Qt::ItemFlags TreeModel_ModelBase::flags (const QModelIndex& theIndex) const
{
  if (!theIndex.isValid())
    return 0;
  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

// =======================================================================
// function : headerData
// purpose :
// =======================================================================
QVariant TreeModel_ModelBase::headerData (int theSection, Qt::Orientation theOrientation, int theRole) const
{
  if (theOrientation != Qt::Horizontal || theRole != Qt::DisplayRole)
    return QVariant();

  if (IsUseVisibilityColumn() && theSection == TreeModel_ColumnType_Visibility)
    return QVariant();

  return GetHeaderItem (theSection).GetName();
}

// =======================================================================
// function :  rowCount
// purpose :
// =======================================================================
int TreeModel_ModelBase::rowCount (const QModelIndex& theParent) const
{
  // to create index on the root item
  if (!theParent.isValid())
    return 1;

  TreeModel_ItemBasePtr aParentItem;
  if (!theParent.isValid())
    aParentItem = RootItem (0);
  else
    aParentItem = GetItemByIndex (theParent);

  return aParentItem ? aParentItem->rowCount() : 0;
}

// =======================================================================
// function : EmitLayoutChanged
// purpose :
// =======================================================================
void TreeModel_ModelBase::EmitLayoutChanged()
{
  emit layoutChanged();
}

// =======================================================================
// function : EmitLayoutChanged
// purpose :
// =======================================================================
void TreeModel_ModelBase::EmitDataChanged (const QModelIndex& theTopLeft, const QModelIndex& theBottomRight,
                                           const QVector<int>& theRoles,
                                           const bool isResetItem)
{
  TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (theTopLeft);
  if (anItemBase && isResetItem)
    anItemBase->Reset();

#if QT_VERSION < 0x050000
  (void)theRoles;
  emit dataChanged (theTopLeft, theBottomRight);
#else
  emit dataChanged (theTopLeft, theBottomRight, theRoles);
#endif
}

// =======================================================================
// function : SingleSelected
// purpose :
// =======================================================================
QModelIndex TreeModel_ModelBase::SingleSelected (const QModelIndexList& theIndices, const int theCellId,
                                                 const Qt::Orientation theOrientation)
{
  QModelIndexList aFirstColumnSelectedIndices;
  for (QModelIndexList::const_iterator anIndicesIt = theIndices.begin(); anIndicesIt != theIndices.end(); anIndicesIt++)
  {
    QModelIndex anIndex = *anIndicesIt;
    if ((theOrientation == Qt::Horizontal && anIndex.column() == theCellId) ||
        (theOrientation == Qt::Vertical && anIndex.row() == theCellId))
      aFirstColumnSelectedIndices.append (anIndex);
  }
  return aFirstColumnSelectedIndices.size() == 1 ? aFirstColumnSelectedIndices.first() : QModelIndex();
}

// =======================================================================
// function :  getIndexValue
// purpose :
// =======================================================================
void* TreeModel_ModelBase::getIndexValue (const TreeModel_ItemBasePtr& theItem)
{
  return theItem.data();
}