File: TreeModel_ModelBase.cxx

package info (click to toggle)
opencascade 7.9.2%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 301,924 kB
  • sloc: cpp: 1,523,264; tcl: 10,159; cs: 5,173; java: 1,554; sh: 1,342; ansic: 827; xml: 699; makefile: 30; javascript: 22
file content (371 lines) | stat: -rw-r--r-- 13,258 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// 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_ItemProperties.hxx>
#include <inspector/TreeModel_Tools.hxx>
#include <inspector/TreeModel_VisibilityState.hxx>

#include <Standard_Transient.hxx>

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

const int COLUMN_NAME_WIDTH = 260;
const int COLUMN_SIZE_WIDTH = 30;

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
TreeModel_ModelBase::TreeModel_ModelBase(QObject* theParent)
    : QAbstractItemModel(theParent),
      m_pUseVisibilityColumn(false),
      myVisibilityState(0)
{
  myVisibleIcon   = QIcon(":/icons/item_visible.png");
  myInvisibleIcon = QIcon(":/icons/item_invisible.png");
}

// =======================================================================
// function :  InitColumns
// purpose :
// =======================================================================
void TreeModel_ModelBase::InitColumns()
{
  setHeaderItem(TreeModel_ColumnType_Name, TreeModel_HeaderSection("Name", COLUMN_NAME_WIDTH));
  setHeaderItem(
    TreeModel_ColumnType_Visibility,
    TreeModel_HeaderSection("Visibility", TreeModel_ModelBase::ColumnVisibilityWidth()));
  setHeaderItem(TreeModel_ColumnType_Row, TreeModel_HeaderSection("Row", COLUMN_SIZE_WIDTH));
}

// =======================================================================
// 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 = myVisibilityState->IsVisible(theIndex) ? myVisibleIcon : myInvisibleIcon;
    anItem->SetCustomData(aValue, theRole);
    return aValue;
  }

  TreeModel_ItemBasePtr anItem     = GetItemByIndex(theIndex);
  QVariant              anItemData = anItem->data(theIndex, theRole);

  if (anItemData.isNull() && theRole == Qt::BackgroundRole
      && myHighlightedIndices.contains(theIndex))
    anItemData = TreeModel_Tools::LightHighlightColor();

  return anItemData;
}

// =======================================================================
// 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 Qt::ItemFlags();
  }
  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 myHeaderValues[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);

  if (!aParentItem)
    return 0;

  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 : Selected
// purpose :
// =======================================================================
QModelIndexList TreeModel_ModelBase::Selected(const QModelIndexList& theIndices,
                                              const int              theCellId,
                                              const Qt::Orientation  theOrientation)
{
  QModelIndexList aSelected;
  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))
      aSelected.append(anIndex);
  }
  return aSelected;
}

// =======================================================================
// function : SingleSelected
// purpose :
// =======================================================================
QModelIndex TreeModel_ModelBase::SingleSelected(const QModelIndexList& theIndices,
                                                const int              theCellId,
                                                const Qt::Orientation  theOrientation)
{
  QModelIndexList aSelected = Selected(theIndices, theCellId, theOrientation);
  return aSelected.size() == 1 ? aSelected.first() : QModelIndex();
}

// =======================================================================
// function :  SelectedItems
// purpose :
// =======================================================================
QList<TreeModel_ItemBasePtr> TreeModel_ModelBase::SelectedItems(const QModelIndexList& theIndices)
{
  QList<TreeModel_ItemBasePtr> anItems;

  for (QModelIndexList::const_iterator anIndicesIt = theIndices.begin();
       anIndicesIt != theIndices.end();
       anIndicesIt++)
  {
    TreeModel_ItemBasePtr anItem = TreeModel_ModelBase::GetItemByIndex(*anIndicesIt);
    if (!anItem || anItems.contains(anItem))
      continue;
    anItems.append(anItem);
  }
  return anItems;
}

// =======================================================================
// function : SubItemsPresentations
// purpose :
// =======================================================================
void TreeModel_ModelBase::SubItemsPresentations(
  const QModelIndexList&                        theIndices,
  NCollection_List<Handle(Standard_Transient)>& thePresentations)
{
  QList<TreeModel_ItemBasePtr> anItems;

  for (QModelIndexList::const_iterator anIndicesIt = theIndices.begin();
       anIndicesIt != theIndices.end();
       anIndicesIt++)
  {
    TreeModel_ItemBasePtr anItem = TreeModel_ModelBase::GetItemByIndex(*anIndicesIt);
    if (!anItem || anItems.contains(anItem))
      continue;
    subItemsPresentations(anItem, thePresentations);
  }
}

// =======================================================================
// function : subItemPresentations
// purpose :
// =======================================================================
void TreeModel_ModelBase::subItemsPresentations(
  const TreeModel_ItemBasePtr&                  theItem,
  NCollection_List<Handle(Standard_Transient)>& thePresentations)
{
  theItem->Presentations(thePresentations);

  QList<TreeModel_ItemBasePtr> anItems;
  for (int aRowId = 0; aRowId < theItem->rowCount(); aRowId++)
  {
    subItemsPresentations(theItem->Child(aRowId, theItem->Column()), thePresentations);
  }
}

// =======================================================================
// function : createRoot
// purpose :
// =======================================================================
void TreeModel_ModelBase::createRoot(const int theColumnId)
{
  myRootItems.insert(theColumnId, createRootItem(theColumnId));
}

// =======================================================================
// function : setHeaderItem
// purpose :
// =======================================================================
void TreeModel_ModelBase::setHeaderItem(const int                      theColumnId,
                                        const TreeModel_HeaderSection& theSection)
{
  if (theSection.IsEmpty())
  {
    // remove section
    myHeaderValues.remove(theColumnId);
    myRootItems.remove(theColumnId);
  }

  myHeaderValues[theColumnId] = theSection;
  createRoot(theColumnId);
}

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