File: kjs_object_model.cpp

package info (click to toggle)
kjsembed 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,300 kB
  • sloc: cpp: 9,163; javascript: 701; sh: 21; makefile: 3
file content (181 lines) | stat: -rw-r--r-- 5,338 bytes parent folder | download | duplicates (2)
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
/* This file is part of the KDE libraries
    Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
    Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
    Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
    Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>

    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; either
    version 2 of the License, or (at your option) any later version.

    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 "kjs_object_model.h"

#include <QPixmap>
#include <QDebug>

#include <kjs/object.h>
#include <kjs/interpreter.h>
#include <kjs/PropertyNameArray.h>

struct Node {
    QByteArray name;
    KJS::JSObject *instance;
    Node *parent;
};

KJSObjectModel::KJSObjectModel(KJS::Interpreter *js, QObject *parent):
    QAbstractItemModel(parent), m_js(js)
{
}

void KJSObjectModel::updateModel(KJS::JSObject *root)
{
    m_root = root;
    reset();
}

KJSObjectModel::~KJSObjectModel()
{
}

Qt::ItemFlags KJSObjectModel::flags(const QModelIndex &index) const
{
    if (!index.isValid()) {
        return Qt::ItemIsEnabled;
    }
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

int KJSObjectModel::rowCount(const QModelIndex &parent) const
{
    KJS::ExecState *exec = m_js->globalExec();
    KJS::PropertyNameArray props;
    if (!parent.isValid()) {
        m_root->getPropertyNames(exec, props);
    } else {
        Node *item = static_cast<Node *>(parent.internalPointer());
        item->instance->getPropertyNames(exec, props);
    }
    return props.size();
}

int KJSObjectModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

QVariant KJSObjectModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        if (section == 0) {
            return "Object Name";
        } else {
            return "Value";
        }
    }
    return QVariant();
}

QModelIndex KJSObjectModel::index(int row, int column, const QModelIndex &parent) const
{
    KJS::JSObject *parentInstance = nullptr;
    Node *childItem = nullptr;
    KJS::ExecState *exec = m_js->globalExec();

    if (!parent.isValid()) {
        if (m_root) {
            parentInstance = m_root;
        } else {
            return QModelIndex();
        }
    } else {
        parentInstance = static_cast<Node *>(parent.internalPointer())->instance;
    }
    int idx = 0;
    KJS::PropertyNameArray props;
    parentInstance->getPropertyNames(exec, props);
    for (KJS::PropertyNameArrayIterator ref = props.begin(); ref != props.end(); ref++) {
        if (idx == row) {
            childItem = new Node;
            childItem->name = ref->ascii(); //### M.O.: this is wrong, can be unicode.
            childItem->instance = parentInstance->get(exec,
                                  childItem->name.constData())->toObject(exec);
            childItem->parent = static_cast<Node *>(parent.internalPointer());
            break;
        }
        ++idx;
    }
    if (childItem) {
        return createIndex(row, column, childItem);
    }

    return QModelIndex();
}

QModelIndex KJSObjectModel::parent(const QModelIndex &index) const
{
    if (!index.isValid()) {
        Node *node = new Node;
        node->instance = m_root;
        node->name = "Objects";
        node->parent = nullptr;
        return createIndex(0, index.column(), node);
    }

    Node *parentItem = static_cast<Node *>(index.internalPointer())->parent;
    if (parentItem) {
        Node *node = new Node;
        node->instance = parentItem->instance;
        node->name = parentItem->name;
        node->parent = parentItem->parent;
        return createIndex(0, index.column(), node);
    } else {
        return QModelIndex();
    }
}

QVariant KJSObjectModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    Node *item = static_cast<Node *>(index.internalPointer());
    KJS::JSObject *instance = item->instance;

    if (role == Qt::DecorationRole) {
        if (instance->implementsConstruct()) {
            return QPixmap(":/images/class.png");
        } else if (instance->implementsCall()) {
            return QPixmap(":/images/method.png");
        } else {
            return QPixmap(":/images/property.png");
        }
    }
    if (role == Qt::ForegroundRole) {
        if (instance->implementsConstruct()) {
            return QColor("blue");
        } else if (instance->implementsCall()) {
            return QColor("green");
        } else {
            return QColor("black");
        }
    }
    if (role == Qt::DisplayRole) {
        return item->name;
    }
    return QVariant();
}