-
Notifications
You must be signed in to change notification settings - Fork 30
/
NamedThingsMdl.cpp
295 lines (263 loc) · 8.89 KB
/
NamedThingsMdl.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
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
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Oberon Viewer application.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "NamedThingsMdl.h"
#include <QBrush>
#include <QPixmap>
#include <QtDebug>
#include <QTreeView>
using namespace Ob;
NamedThingsMdl::NamedThingsMdl(QTreeView* parent) :
QAbstractItemModel(parent)
{
}
QTreeView*NamedThingsMdl::getParent() const
{
return static_cast<QTreeView*>(QObject::parent());
}
void NamedThingsMdl::setSyntax( CodeModel* syn )
{
beginResetModel();
d_root = Slot();
d_syn = syn;
fillTop();
endResetModel();
}
const CodeModel::NamedThing* NamedThingsMdl::getSymbol(const QModelIndex& index) const
{
if( !index.isValid() || d_syn == 0 )
return 0;
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
return s->d_sym;
}
QModelIndex NamedThingsMdl::findSymbol(const CodeModel::NamedThing* nt)
{
return findSymbol( &d_root, nt );
}
QVariant NamedThingsMdl::data(const QModelIndex& index, int role) const
{
if( !index.isValid() || d_syn == 0 )
return QVariant();
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
switch( role )
{
case Qt::DisplayRole:
switch( s->d_kind )
{
case Slot::Symbol:
default:
if( const CodeModel::Element* v = dynamic_cast<const CodeModel::Element*>(s->d_sym) )
{
if( v->isStub() )
return s->d_sym->d_name + " (" + CodeModel::Element::s_kindName[v->d_kind] + ")";
}else if( const CodeModel::Procedure* v = dynamic_cast<const CodeModel::Procedure*>(s->d_sym) )
{
return s->d_sym->d_name + ( s->d_sym->d_public ? "*" : "" ) + "(" +
( v->d_vals.isEmpty() ? "" : "," ) + ")";
}
// else
return s->d_sym->d_name + ( s->d_sym->d_public ? "*" : "" );
case Slot::Consts:
return "CONST";
case Slot::Vars:
return "VAR";
case Slot::Procs:
return "PROC";
case Slot::Types:
return "TYPE";
case Slot::Params:
return "PARAM";
case Slot::Stubs:
return "STUB";
}
break;
case Qt::FontRole:
if( s->d_kind != Slot::Symbol )
{
QFont f = getParent()->font();
f.setItalic(true);
return f;
}
break;
case Qt::ForegroundRole:
if( s->d_sym == 0 || s->d_sym->d_id != 0 )
return QBrush( Qt::black );
else
return QBrush( Qt::gray );
break;
}
return QVariant();
}
QModelIndex NamedThingsMdl::parent ( const QModelIndex & index ) const
{
if( index.isValid() )
{
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
if( s->d_parent == &d_root )
return QModelIndex();
// else
Q_ASSERT( s->d_parent != 0 );
Q_ASSERT( s->d_parent->d_parent != 0 );
return createIndex( s->d_parent->d_parent->d_children.indexOf( s->d_parent ), 0, s->d_parent );
}else
return QModelIndex();
}
int NamedThingsMdl::rowCount ( const QModelIndex & parent ) const
{
if( parent.isValid() )
{
Slot* s = static_cast<Slot*>( parent.internalPointer() );
Q_ASSERT( s != 0 );
return s->d_children.size();
}else
return d_root.d_children.size();
}
QModelIndex NamedThingsMdl::index ( int row, int column, const QModelIndex & parent ) const
{
const Slot* s = &d_root;
if( parent.isValid() )
{
s = static_cast<Slot*>( parent.internalPointer() );
Q_ASSERT( s != 0 );
}
if( row < s->d_children.size() && column < columnCount( parent ) )
return createIndex( row, column, s->d_children[row] );
else
return QModelIndex();
}
Qt::ItemFlags NamedThingsMdl::flags( const QModelIndex & index ) const
{
Q_UNUSED(index)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable; // | Qt::ItemIsDragEnabled;
}
void NamedThingsMdl::fillTop()
{
if( d_syn == 0 )
return;
typedef QMap<QByteArray,const CodeModel::Module*> Sorter;
Sorter sorter;
foreach( const CodeModel::Module* m, d_syn->getGlobalScope().d_mods )
sorter.insert( m->d_name, m );
for( Sorter::const_iterator j = sorter.begin(); j != sorter.end(); ++j )
{
Slot* super = new Slot();
super->d_parent = &d_root;
super->d_sym = j.value();
d_root.d_children.append( super );
fill( super, j.value() );
}
}
QModelIndex NamedThingsMdl::findSymbol(NamedThingsMdl::Slot* slot, const CodeModel::NamedThing* nt) const
{
for( int i = 0; i < slot->d_children.size(); i++ )
{
Slot* s = slot->d_children[i];
if( s->d_sym == nt )
return createIndex( i, 0, s );
QModelIndex index = findSymbol( s, nt );
if( index.isValid() )
return index;
}
return QModelIndex();
}
void NamedThingsMdl::fillSection( Slot* super, const Sorter& sorter, quint8 kind )
{
if( sorter.isEmpty() )
return;
if( kind != Slot::Symbol)
super = new Slot(super,kind);
for( Sorter::const_iterator j = sorter.begin(); j != sorter.end(); ++j )
{
Slot* s = new Slot();
s->d_parent = super;
s->d_sym = j.value();
super->d_children.append( s );
if( const CodeModel::Procedure* p = dynamic_cast<const CodeModel::Procedure*>(j.value()) )
fill(s, p );
else if( const CodeModel::Type* r = dynamic_cast<const CodeModel::Type*>(j.value()) )
{
if( r->d_kind == CodeModel::Type::Pointer )
r = r->d_type;
if( r->d_kind != CodeModel::Type::Record )
continue;
Sorter s2;
CodeModel::Type::Vals::const_iterator i;
for( i = r->d_vals.begin(); i != r->d_vals.end(); ++i )
s2.insert( i.value()->d_name.toLower(), i.value() );
#ifdef OB_OBN2
CodeModel::Type::Procs::const_iterator j;
for( j = r->d_procs.begin(); j != r->d_procs.end(); ++j )
s2.insert( j.value()->d_name.toLower(), j.value() );
#endif
fillSection( s, s2, Slot::Symbol );
}else if( const CodeModel::Element* t = dynamic_cast<const CodeModel::Element*>(j.value() ) )
{
if( t->isStub() )
{
Sorter s2;
foreach( const CodeModel::Element* v, t->d_vals )
s2.insert( v->d_name.toLower(), v );
fillSection( s, s2, Slot::Symbol );
}
}
}
}
void NamedThingsMdl::fill(Slot* super, const CodeModel::Unit* ds )
{
if( ds == 0 )
return;
Sorter sorter;
if( const CodeModel::Procedure* p = dynamic_cast<const CodeModel::Procedure*>(ds) )
{
foreach( const CodeModel::Element* x, p->d_vals )
if( !x->d_name.isEmpty() )
sorter.insert( x->d_name.toLower(), x );
fillSection( super, sorter, Slot::Params );
}
sorter.clear();
foreach( const CodeModel::Element* x, ds->getConsts() )
if( !x->d_name.isEmpty() )
sorter.insert( x->d_name.toLower(), x );
fillSection( super, sorter, Slot::Consts );
sorter.clear();
foreach( const CodeModel::Type* x, ds->d_types )
if( !x->d_name.isEmpty() )
sorter.insert( x->d_name.toLower(), x );
fillSection( super, sorter, Slot::Types );
sorter.clear();
foreach( const CodeModel::Element* x, ds->getVars() )
if( !x->d_name.isEmpty() )
sorter.insert( x->d_name.toLower(), x );
fillSection( super, sorter, Slot::Vars );
sorter.clear();
foreach( const CodeModel::Element* x, ds->getUnknowns() )
if( !x->d_name.isEmpty() )
sorter.insert( x->d_name.toLower(), x );
foreach( const CodeModel::Element* x, ds->getStubProcs() )
if( !x->d_name.isEmpty() )
sorter.insert( x->d_name.toLower(), x );
fillSection( super, sorter, Slot::Stubs );
sorter.clear();
foreach( const CodeModel::Procedure* x, ds->d_procs )
if( !x->d_name.isEmpty() && x->d_receiver == 0 )
sorter.insert( x->d_name.toLower(), x );
fillSection( super, sorter, Slot::Procs );
}