File: TreeModel_ItemProperties.cxx

package info (click to toggle)
opencascade 7.9.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 301,932 kB
  • sloc: cpp: 1,523,264; tcl: 10,159; cs: 5,173; java: 1,554; sh: 1,342; ansic: 827; xml: 699; makefile: 31; javascript: 22
file content (344 lines) | stat: -rw-r--r-- 11,784 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
// Created on: 2020-01-25
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2020 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_ItemProperties.hxx>
#include <inspector/TreeModel_ItemStream.hxx>
#include <inspector/Convert_Tools.hxx>
#include <inspector/Convert_TransientShape.hxx>

#include <BRepBuilderAPI_MakeVertex.hxx>
#include <gp_XYZ.hxx>
#include <Quantity_ColorRGBA.hxx>
#include <Standard_Dump.hxx>

#include <Standard_WarningsDisable.hxx>
#include <QApplication>
#include <QFont>
#include <Standard_WarningsRestore.hxx>

IMPLEMENT_STANDARD_RTTIEXT(TreeModel_ItemProperties, Standard_Transient)

// =======================================================================
// function : Init
// purpose :
// =======================================================================
void TreeModel_ItemProperties::Init()
{
  myRowValues.Clear();

  InitByStream(Item()->Stream());
}

// =======================================================================
// function : InitByStream
// purpose :
// =======================================================================
void TreeModel_ItemProperties::InitByStream(const Standard_SStream& aStream)
{
  NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue> aValues;
  TCollection_AsciiString aStreamText = Standard_Dump::Text(aStream);
  Standard_Dump::SplitJson(aStreamText, aValues);

  TreeModel_ItemStreamPtr aStreamParent = itemDynamicCast<TreeModel_ItemStream>(Item());
  TCollection_AsciiString aKey;
  Standard_DumpValue      aKeyValue;
  if (!aStreamParent)
  {
    if (!Item() || Item()->Object().IsNull())
    {
      aKey = "Dump";
    }
    else
    {
      const Handle(Standard_Transient)& anItemObject = Item()->Object();
      aKey = anItemObject.IsNull() ? "Dump" : anItemObject->DynamicType()->Name();
    }
    aKeyValue = Standard_DumpValue(aStreamText, 1);

    myKey         = aKey;
    myStreamValue = aKeyValue;
  }
  else
  {
    TCollection_AsciiString aValue;
    if (Item()->Parent())
    {
      const Handle(TreeModel_ItemProperties)& aParentProperties = Item()->Parent()->Properties();
      if (aParentProperties)
        aParentProperties->ChildStream(Item()->Row(), aKey, aKeyValue);
    }
    myKey         = aKey;
    myStreamValue = aKeyValue;

    aValues.Clear();
    Standard_Dump::SplitJson(myStreamValue.myValue, aValues);
  }

  for (Standard_Integer anIndex = 1; anIndex <= aValues.Size(); anIndex++)
  {
    Standard_DumpValue aValue = aValues.FindFromIndex(anIndex);
    if (Standard_Dump::HasChildKey(aValue.myValue))
      myChildren.Add(aValues.FindKey(anIndex), aValue);
    else
    {
      TreeModel_RowValue aRowValue(aValue.myStartPosition,
                                   aValues.FindKey(anIndex).ToCString(),
                                   aValue.myValue.ToCString());
      myRowValues.Add(myRowValues.Size() + 1, aRowValue);
    }
  }
  if (myRowValues.Size() == 1)
  {
    Quantity_Color aColor;
    if (Convert_Tools::ConvertStreamToColor(aStream, aColor))
    {
      Standard_Real aRed, aGreen, aBlue;
      aColor.Values(aRed, aGreen, aBlue, Quantity_TOC_sRGB);

      int aDelta = 255;
      myRowValues.ChangeFromIndex(1).CustomValues.insert(
        (int)Qt::BackgroundRole,
        QColor((int)(aRed * aDelta), (int)(aGreen * aDelta), (int)(aBlue * aDelta)));
    }
  }
  // in case if the stream alert has empty key avalue, use as the key the first row value
  if ((myKey.IsEmpty() || myKey.IsEqual("Dump")) && myRowValues.Size() > 0)
  {
    myKey = myRowValues.FindFromIndex(1).Value.toString().toStdString().c_str();
  }
}

// =======================================================================
// function :  Reset
// purpose :
// =======================================================================
void TreeModel_ItemProperties::Reset()
{
  myKey         = "";
  myStreamValue = Standard_DumpValue();

  myChildren.Clear();
  myRowValues.Clear();
}

// =======================================================================
// function : RowCount
// purpose :
// =======================================================================
int TreeModel_ItemProperties::RowCount() const
{
  return RowValues().Size();
}

// =======================================================================
// function : Data
// purpose :
// =======================================================================
QVariant TreeModel_ItemProperties::Data(const int theRow, const int theColumn, int theRole) const
{
  if (theColumn == 1 && theRole == Qt::BackgroundRole)
  {
    const QMap<int, QVariant>& aCachedValues = RowValues().FindFromIndex(theRow + 1).CustomValues;
    if (aCachedValues.contains((int)theRole))
      return aCachedValues[(int)theRole];
  }

  if (theRole == Qt::FontRole) // method name is in italic
  {
    if (Data(theRow, 0, Qt::DisplayRole).toString().contains("className"))
    {
      QFont aFont = qApp->font();
      aFont.setItalic(true);
      return aFont;
    }
  }
  if (theRole == Qt::ForegroundRole)
  {
    if (Data(theRow, 0, Qt::DisplayRole).toString().contains("className"))
      return QColor(Qt::darkGray).darker(150);
  }

  if (theRole == Qt::DisplayRole || theRole == Qt::ToolTipRole)
  {
    if (theColumn == 0)
      return RowValues().FindFromIndex(theRow + 1).Key;
    else if (theColumn == 1)
      return RowValues().FindFromIndex(theRow + 1).Value;
  }

  return QVariant();
}

// =======================================================================
// function : EditType
// purpose :
// =======================================================================
ViewControl_EditType TreeModel_ItemProperties::EditType(const int, const int theColumn) const
{
  if (theColumn == 0)
    return ViewControl_EditType_None;

  Quantity_Color aColor;
  if (Convert_Tools::ConvertStreamToColor(Item()->Stream(), aColor))
  {
    return ViewControl_EditType_Color;
  }
  return ViewControl_EditType_Line;
}

// =======================================================================
// function : ReplaceValue
// purpose :
// =======================================================================
Standard_Boolean ReplaceValue(const TCollection_AsciiString& theFromValue,
                              const TCollection_AsciiString& theToValue,
                              Standard_DumpValue&            theStreamValue)
{
  TCollection_AsciiString aStreamValue = theStreamValue.myValue;

  int aPosition = aStreamValue.FirstLocationInSet(theFromValue, 1, aStreamValue.Length());
  if (aPosition < 1)
    return Standard_False;

  aPosition +=
    2; // due to 'FirstLocationInSet' returns position taking into account '"\' as 1 position

  TCollection_AsciiString aPartStart = aStreamValue.SubString(1, aPosition - 1);
  TCollection_AsciiString aPartFinal =
    aStreamValue.SubString(aPosition + theFromValue.Length(), aStreamValue.Length());
  theStreamValue.myValue = aPartStart + theToValue + aPartFinal;

  return Standard_True;
}

// =======================================================================
// function : SetData
// purpose :
// =======================================================================
bool TreeModel_ItemProperties::SetData(const int       theRow,
                                       const int       theColumn,
                                       const QVariant& theValue,
                                       int             theRole)
{
  if (theColumn == 0)
    return false;

  if (theRole != Qt::DisplayRole && theRole != Qt::EditRole)
    return false;

  if (myRowValues.Size() == 1 && theColumn == 1)
  {
    TCollection_AsciiString aStreamValue(theValue.toString().toStdString().c_str());
    NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue> aKeyToValues;
    if (Standard_Dump::SplitJson(aStreamValue, aKeyToValues))
    {
      Standard_SStream aStream;
      aStream << aStreamValue.ToCString();

      int                aStartPos = 1;
      Quantity_ColorRGBA aColor;
      if (aColor.InitFromJson(aStream, aStartPos))
      {
        Standard_Real aRed, aGreen, aBlue;
        aColor.GetRGB().Values(aRed, aGreen, aBlue, Quantity_TOC_sRGB);
        int aDelta = 255;
        myRowValues.ChangeFromIndex(1).CustomValues.insert(
          (int)Qt::BackgroundRole,
          QColor((int)(aRed * aDelta), (int)(aGreen * aDelta), (int)(aBlue * aDelta)));
      }
      Standard_DumpValue aValue            = aKeyToValues.FindFromIndex(1);
      myStreamValue.myValue                = aValue.myValue.ToCString();
      myRowValues.ChangeFromIndex(1).Value = aValue.myValue.ToCString();

      Item()->StoreItemProperties(theRow, theColumn, theValue);
      return true;
    }
    TCollection_AsciiString aFromValue =
      myRowValues.ChangeFromIndex(1).Value.toString().toStdString().c_str();
    if (ReplaceValue(aFromValue, aStreamValue, myStreamValue))
    {
      aStreamValue = myStreamValue.myValue;
      if (Standard_Dump::SplitJson(aStreamValue, aKeyToValues))
      {
        Standard_DumpValue aValue            = aKeyToValues.FindFromIndex(1);
        myRowValues.ChangeFromIndex(1).Value = aValue.myValue.ToCString();

        Item()->StoreItemProperties(theRow, theColumn, aStreamValue.ToCString());
        return true;
      }
    }
  }

  myRowValues.ChangeFromIndex(theRow + 1).Value = theValue;
  Item()->StoreItemProperties(theRow, theColumn, theValue);
  return true;
}

// =======================================================================
// function : Presentations
// purpose :
// =======================================================================
void TreeModel_ItemProperties::Presentations(
  NCollection_List<Handle(Standard_Transient)>& thePresentations)
{
  if (!Item())
  {
    return;
  }
  const Standard_SStream& aStream = Item()->Stream();
  Convert_Tools::ConvertStreamToPresentations(aStream, 1, -1, thePresentations);
}

// =======================================================================
// function : TableFlags
// purpose :
// =======================================================================
Qt::ItemFlags TreeModel_ItemProperties::TableFlags(const int, const int theColumn) const
{
  Qt::ItemFlags aFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

  if (theColumn == 1)
    aFlags = aFlags | Qt::ItemIsEditable;

  return aFlags;
}

// =======================================================================
// function : ChildStream
// purpose :
// =======================================================================
void TreeModel_ItemProperties::ChildStream(const int                theRowId,
                                           TCollection_AsciiString& theKey,
                                           Standard_DumpValue&      theValue) const
{
  if (myChildren.Size() <= theRowId)
    return;

  theKey   = myChildren.FindKey(theRowId + 1);
  theValue = myChildren.FindFromIndex(theRowId + 1);
}

// =======================================================================
// function : initItem
// purpose :
// =======================================================================
void TreeModel_ItemProperties::initItem() const
{
  if (!Item())
    return;
  if (Item()->IsInitialized())
    return;
  Item()->Init();
}