File: DFBrowser_TreeModel.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 (300 lines) | stat: -rw-r--r-- 11,416 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
// 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/DFBrowser_TreeModel.hxx>

#include <inspector/DFBrowser_Item.hxx>
#include <inspector/DFBrowser_ItemApplication.hxx>
#include <inspector/DFBrowser_ItemDocument.hxx>
#include <inspector/DFBrowser_Module.hxx>
#include <inspector/DFBrowser_Window.hxx>
#include <inspector/DFBrowserPane_Tools.hxx>
#include <NCollection_List.hxx>

#include <TDocStd_Application.hxx>
#include <TDocStd_Document.hxx>
#include <TNaming_Builder.hxx>
#include <TNaming_NamedShape.hxx>

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

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
DFBrowser_TreeModel::DFBrowser_TreeModel(QObject* theParent)
    : TreeModel_ModelBase(theParent)
{
}

// =======================================================================
// function : InitColumns
// purpose :
// =======================================================================
void DFBrowser_TreeModel::InitColumns()
{
  setHeaderItem(0, TreeModel_HeaderSection("Name"));
}

// =======================================================================
// function : SetModule
// purpose :
// =======================================================================
void DFBrowser_TreeModel::SetModule(DFBrowser_Module* theModule)
{
  DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication>(RootItem(0));
  aRootItem->SetModule(theModule);
}

// =======================================================================
// function : createRootItem
// purpose :
// =======================================================================
TreeModel_ItemBasePtr DFBrowser_TreeModel::createRootItem(const int)
{
  return DFBrowser_ItemApplication::CreateItem(TreeModel_ItemBasePtr());
}

// =======================================================================
// function : Init
// purpose :
// =======================================================================
void DFBrowser_TreeModel::Init(const Handle(TDocStd_Application)& theApplication)
{
  DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication>(RootItem(0));
  Reset();
  aRootItem->SetApplication(theApplication);
  EmitLayoutChanged();
}

// =======================================================================
// function : GetTDocStdApplication
// purpose :
// =======================================================================
Handle(TDocStd_Application) DFBrowser_TreeModel::GetTDocStdApplication() const
{
  DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication>(RootItem(0));
  return aRootItem->GetApplication();
}

// =======================================================================
// function : FindIndex
// purpose :
// =======================================================================
QModelIndex DFBrowser_TreeModel::FindIndex(const TDF_Label& theLabel) const
{
  TDF_Label aRoot = theLabel.Root();

  NCollection_List<TDF_Label> aLabels;
  aLabels.Prepend(theLabel);
  TDF_Label aFather = theLabel.Father();
  if (!aFather.IsNull())
  {
    while (aFather != aRoot)
    {
      aLabels.Prepend(aFather);
      aFather = aFather.Father();
    }
  }
  bool                  aDocumentItemFound = false;
  QModelIndex           aParentIndex       = index(0, 0);
  TreeModel_ItemBasePtr aParentItem =
    TreeModel_ModelBase::GetItemByIndex(aParentIndex); // application item
  // find document, where label of document item is equal to Root label
  for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
  {
    QModelIndex               anIndex    = index(aChildId, 0, aParentIndex);
    TreeModel_ItemBasePtr     anItemBase = TreeModel_ModelBase::GetItemByIndex(anIndex);
    DFBrowser_ItemDocumentPtr anItem     = itemDynamicCast<DFBrowser_ItemDocument>(anItemBase);
    if (anItem->GetLabel() == aRoot)
    {
      aParentItem        = anItem;
      aParentIndex       = anIndex;
      aDocumentItemFound = true;
      break;
    }
  }
  if (!aDocumentItemFound) // document is not found
    return QModelIndex();

  for (NCollection_List<TDF_Label>::const_iterator aLabelIt = aLabels.begin();
       aLabelIt != aLabels.end() && aParentIndex.isValid();
       aLabelIt++)
  {
    const TDF_Label aLabel = *aLabelIt;
    for (int aParentChildId = 0, aCount = aParentItem->rowCount(); aParentChildId < aCount;
         aParentChildId++)
    {
      QModelIndex       anIndex = index(aParentChildId, 0, aParentIndex);
      DFBrowser_ItemPtr anItem =
        itemDynamicCast<DFBrowser_Item>(TreeModel_ModelBase::GetItemByIndex(anIndex));
      if (anItem->HasAttribute())
        continue;

      if (anItem->HasLabel() && anItem->GetLabel().IsEqual(aLabel))
      {
        aParentItem  = anItem;
        aParentIndex = anIndex;
        break;
      }
    }
  }
  return aParentIndex;
}

// =======================================================================
// function : FindIndexByPath
// purpose :
// =======================================================================
QModelIndex DFBrowser_TreeModel::FindIndexByPath(const QStringList& theLabelEntries,
                                                 const QString&     theValue) const
{
  QModelIndex aFoundIndex;

  QModelIndex           aRootIndex = index(0, 0);
  TreeModel_ItemBasePtr aRootItem =
    TreeModel_ModelBase::GetItemByIndex(aRootIndex); // application item
  // find document, where label of document item is equal to Root label
  for (int aDocItemId = 0, aNbDocItems = aRootItem->rowCount();
       aDocItemId < aNbDocItems && !aFoundIndex.isValid();
       aDocItemId++)
  {
    QModelIndex aParentIndex = index(aDocItemId, 0, aRootIndex);
    if (!aParentIndex.isValid()) // OCAF document for this document item is not found
      continue;
    if (theLabelEntries.size() == 0)
    {
      aFoundIndex = aParentIndex;
      break;
    }
    TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex(aParentIndex);
    for (int aPathId = 1, aPathCount = theLabelEntries.size(); aPathId < aPathCount + 1; aPathId++)
    {
      QString anEntry;
      if (aPathId < aPathCount)
        anEntry = theLabelEntries[aPathId];
      else
        anEntry = theValue;

      bool aFoundEntry = false;
      for (int aChildId = 0, aNbChildren = aParentItem->rowCount(); aChildId < aNbChildren;
           aChildId++)
      {
        QModelIndex           anIndex    = index(aChildId, 0, aParentIndex);
        TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex(anIndex);
        DFBrowser_ItemPtr     anItem     = itemDynamicCast<DFBrowser_Item>(anItemBase);

        if (aPathId == aPathCount && anItem->HasAttribute())
        {
          // processing attribute in theValue
          DFBrowser_ItemApplicationPtr aRootAppItem =
            itemDynamicCast<DFBrowser_ItemApplication>(RootItem(0));
          QString anAttributeInfo = DFBrowser_Module::GetAttributeInfo(anItem->GetAttribute(),
                                                                       aRootAppItem->GetModule(),
                                                                       Qt::DisplayRole,
                                                                       0)
                                      .toString();
          if (anAttributeInfo == anEntry)
          {
            aParentItem  = anItem;
            aParentIndex = anIndex;
            aFoundEntry  = true;
            break;
          }
        }
        else if (anItem->HasLabel()
                 && anEntry
                      == QString(DFBrowserPane_Tools::GetEntry(anItem->GetLabel()).ToCString()))
        {
          aParentItem  = anItem;
          aParentIndex = anIndex;
          aFoundEntry  = true;
          break;
        }
      }
      if (!aFoundEntry) // an entry is not found on some level tree, find it in other documents
        break;
      else
        aFoundIndex = aParentIndex;
    }
  }
  return aFoundIndex;
}

// =======================================================================
// function : FindIndexByAttribute
// purpose :
// =======================================================================
QModelIndex DFBrowser_TreeModel::FindIndexByAttribute(Handle(TDF_Attribute) theAttribute) const
{
  QModelIndex     aFoundIndex;
  const TDF_Label aLabel = theAttribute->Label();

  QModelIndex aParentIndex = FindIndex(aLabel);
  if (!aParentIndex.isValid())
    return aFoundIndex;

  TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex(aParentIndex);
  for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
  {
    QModelIndex           anIndex    = index(aChildId, 0, aParentIndex);
    TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex(anIndex);
    DFBrowser_ItemPtr     anItem     = itemDynamicCast<DFBrowser_Item>(anItemBase);
    if (anItem->GetAttribute() == theAttribute)
    {
      aFoundIndex = anIndex;
      break;
    }
  }
  return aFoundIndex;
}

// =======================================================================
// function : ConvertToIndices
// purpose :
// =======================================================================
void DFBrowser_TreeModel::ConvertToIndices(const NCollection_List<TDF_Label>& theReferences,
                                           QModelIndexList&                   theIndices)
{
  for (NCollection_List<TDF_Label>::Iterator aLabelItr(theReferences); aLabelItr.More();
       aLabelItr.Next())
    theIndices.append(FindIndex(aLabelItr.Value()));
}

// =======================================================================
// function : ConvertToIndices
// purpose :
// =======================================================================
void DFBrowser_TreeModel::ConvertToIndices(
  const NCollection_List<Handle(TDF_Attribute)>& theReferences,
  QModelIndexList&                               theIndices)
{
  for (NCollection_List<Handle(TDF_Attribute)>::Iterator anAttrItr(theReferences); anAttrItr.More();
       anAttrItr.Next())
    theIndices.append(FindIndexByAttribute(anAttrItr.Value()));
}

// =======================================================================
// function : data
// purpose :
// =======================================================================
QVariant DFBrowser_TreeModel::data(const QModelIndex& theIndex, int theRole) const
{
  if (theRole == Qt::BackgroundRole && myHighlightedIndices.contains(theIndex))
    return DFBrowserPane_Tools::LightHighlightColor();
  return TreeModel_ModelBase::data(theIndex, theRole);
}