-
Notifications
You must be signed in to change notification settings - Fork 12
/
SchemaTree.cpp
128 lines (110 loc) · 4.22 KB
/
SchemaTree.cpp
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
/* This file is part of SCView, a STEP-Express viewer
Copyright (C) 2012 Laurent Bauer lahoraent@hotmail.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; version 2
of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "SchemaTree.h"
#include <QStringListModel>
#include <ExpDict.h>
#include <Registry.h>
#include <iostream>
using namespace std;
enum itemType {
EntityDescriptorItemType
, TypeDescriptorItemType
};
SchemaTree::SchemaTree(Registry *registry, QWidget *parent)
: QTreeWidget(parent)
, m_Registry( registry )
, m_EntityIcon(":/Entity")
, m_TypeIcon(":/Type")
{
// L&F
setAlternatingRowColors(true);
setHeaderHidden(true);
// Root : The Schema
m_Registry->ResetSchemas();
const Schema * schema = m_Registry->NextSchema();
QTreeWidgetItem * rootItem = new QTreeWidgetItem (this);
rootItem->setText(0, schema->Name());
rootItem->setIcon(0, QIcon(":/Schema"));
//Fill Entities
// Root for Entities
QTreeWidgetItem * entitiesItem = new QTreeWidgetItem (rootItem);
entitiesItem->setText(0,tr("Entities") );
entitiesItem->setIcon(0, m_EntityIcon);
// all entities
const EntityDescriptor * entityDescriptor;
while ( (entityDescriptor= m_Registry->NextEntity() ) )
{
QTreeWidgetItem * entityItem = new QTreeWidgetItem(entitiesItem, EntityDescriptorItemType);
entityItem->setText(0,entityDescriptor->Name());
entityItem->setIcon(0,m_EntityIcon);
void* voidEntityDescriptor=(void*) entityDescriptor;
entityItem->setData(0,Qt::UserRole,QVariant::fromValue(voidEntityDescriptor) );
m_DescriptorToItem.insert(voidEntityDescriptor, entityItem);
}
//Fill Types
// Root for types
QTreeWidgetItem * typesItem = new QTreeWidgetItem (rootItem);
typesItem->setText(0,tr("Types") );
typesItem->setIcon(0, m_TypeIcon);
// all types
const TypeDescriptor * typeDescriptor;
while ( (typeDescriptor= m_Registry->NextType() ) )
{
QTreeWidgetItem * typeItem = new QTreeWidgetItem(typesItem, TypeDescriptorItemType);
typeItem->setText(0,typeDescriptor->Name());
typeItem->setIcon (0,m_TypeIcon);
void* voidTypeDescriptor=(void*) typeDescriptor;
typeItem->setData(0,Qt::UserRole,QVariant::fromValue(voidTypeDescriptor) );
m_DescriptorToItem.insert(voidTypeDescriptor, typeItem);
}
sortItems(0,Qt::AscendingOrder);
connect (this, SIGNAL(itemSelectionChanged()), this, SLOT(findSelection()));
}
void SchemaTree::fillStringListModel(QStringListModel *model)
{
QHash <const void*, QTreeWidgetItem*>::const_iterator it ;
QStringList list;
for (it = m_DescriptorToItem.constBegin(); it != m_DescriptorToItem.constEnd(); ++it)
list << it.value()->text(0);
model->setStringList(list);
}
void SchemaTree::findSelection()
{
if (selectedItems().count()>0)
{
QTreeWidgetItem * item = selectedItems().first();
int itemType = item->type();
if ((itemType==EntityDescriptorItemType) || (itemType==TypeDescriptorItemType) )
{
TypeDescriptor * desc = (TypeDescriptor *) item->data(0,Qt::UserRole).value<void*>();
if (desc)
emit selectedDescriptorChanged(desc);
}
}
}
void SchemaTree::select(const TypeDescriptor *td)
{
blockSignals(true);
if (m_DescriptorToItem.contains(td))
{
clearSelection();
QTreeWidgetItem * item = m_DescriptorToItem.value(td);
item->setSelected(true);
if (selectedItems().size() >0)
scrollToItem(selectedItems().at(0));
}
blockSignals(false);
}