-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocafdelegatemodel.h
146 lines (129 loc) · 3.84 KB
/
ocafdelegatemodel.h
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
#ifndef OCAFDELEGATEMODEL_H
#define OCAFDELEGATEMODEL_H
#include <QAbstractItemModel>
#include <BinDrivers.hxx>
#include <TDataStd_Integer.hxx>
#include <TDF_Label.hxx>
#include <TDocStd_Application.hxx>
#include <TDocStd_Document.hxx>
#include <TDataStd_Name.hxx>
#include <TNaming_Builder.hxx>
#include <TNaming_NamedShape.hxx>
#include <TDataStd_IntPackedMap.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDataXtd_Triangulation.hxx>
#include <TDF_Reference.hxx>
#include <TDF_ChildIDIterator.hxx>
#include <QTreeView>
class LabelIndex
{
friend class LabelIndexFactory;
public:
~LabelIndex()
{
for(int i = 0;i < children.size(); i++)
{
delete children[i];
}
}
int getTag() const {return tag;}
int getIndex() const {return index;}
int getChildCount() const {return children.size();}
LabelIndex* getChild(int index) const {
if(index < children.size())
return children[index];
else
return nullptr;
}
LabelIndex* getParent() const
{
return parent;
}
LabelIndex* addChild(int tag)
{
LabelIndex* child = new LabelIndex(tag,this);
children.push_back(child);
child->index = children.size() - 1;
return child;
}
void removeChild(int index)
{
delete children[index];
children.erase(children.begin()+index);
}
private:
LabelIndex(int tag,LabelIndex* parent = nullptr)
{
if(parent != nullptr)
{
this->parent = parent;
this->tag = tag;
this->index = 0;
}
else
{
this->parent = nullptr;
this->tag=tag;
this->index = 0;
}
}
LabelIndex* parent;
std::vector<LabelIndex*> children;
int tag;
int index;
};
class LabelIndexFactory
{
public:
LabelIndexFactory(opencascade::handle<TDocStd_Document> document)
{
TDF_Label rootLabel = document->GetData()->Root();
root = new LabelIndex(rootLabel.Tag(),nullptr);
for (TDF_ChildIterator it(rootLabel); it.More(); it.Next()) {
setupLevel(root->addChild(it.Value().Tag()),it.Value());
}
}
~LabelIndexFactory()
{
delete root;
}
LabelIndex* getRootLabel() const
{
return root;
}
private:
LabelIndex* root;
void setupLevel(LabelIndex* parent,TDF_Label parentLabel)
{
for (TDF_ChildIterator it(parentLabel); it.More(); it.Next()) {
setupLevel(parent->addChild(it.Value().Tag()),it.Value());
}
}
};
class OcafDelegateModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit OcafDelegateModel(opencascade::handle<TDocStd_Document> doc, QObject *parent = nullptr): QAbstractItemModel(parent),document(doc),indexHelper(doc)
{
}
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
// Basic functionality:
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
QModelIndex sibling(int row, int column, const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
// Fetch data dynamically:
/*bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;*/
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
private:
void setupModelData();
opencascade::handle<TDocStd_Document> document;
LabelIndexFactory indexHelper;
};
#endif // OCAFDELEGATEMODEL_H