• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

kjsembed

  • kjsembed
  • examples
  • kjsconsole
kjs_object_model.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22#include "kjs_object_model.h"
23
24#include <QtGui/QPixmap>
25#include <QtCore/QDebug>
26
27#include <kjs/object.h>
28#include <kjs/interpreter.h>
29#include <kjs/PropertyNameArray.h>
30
31struct Node
32{
33 QByteArray name;
34 KJS::JSObject *instance;
35 Node *parent;
36};
37
38KJSObjectModel::KJSObjectModel(KJS::Interpreter *js, QObject *parent ):
39 QAbstractItemModel(parent), m_js(js)
40{
41}
42
43void KJSObjectModel::updateModel( KJS::JSObject *root)
44{
45 m_root = root;
46 reset();
47}
48
49KJSObjectModel::~KJSObjectModel()
50{
51}
52
53Qt::ItemFlags KJSObjectModel::flags(const QModelIndex &index) const
54{
55 if (!index.isValid())
56 return Qt::ItemIsEnabled;
57 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
58}
59
60
61int KJSObjectModel::rowCount(const QModelIndex &parent ) const
62{
63 KJS::ExecState *exec = m_js->globalExec();
64 KJS::PropertyNameArray props;
65 if (!parent.isValid())
66 m_root->getPropertyNames(exec, props);
67 else
68 {
69 Node *item = static_cast<Node*>(parent.internalPointer());
70 item->instance->getPropertyNames(exec, props);
71 }
72 return props.size();
73}
74
75int KJSObjectModel::columnCount(const QModelIndex &parent ) const
76{
77 Q_UNUSED(parent);
78 return 1;
79}
80
81QVariant KJSObjectModel::headerData(int section, Qt::Orientation orientation, int role) const
82{
83 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
84 {
85 if( section == 0)
86 return "Object Name";
87 else
88 return "Value";
89 }
90 return QVariant();
91}
92
93QModelIndex KJSObjectModel::index(int row, int column, const QModelIndex &parent ) const
94{
95 KJS::JSObject *parentInstance = 0;
96 Node *childItem = 0;
97 KJS::ExecState *exec = m_js->globalExec();
98
99 if (!parent.isValid())
100 {
101 if (m_root)
102 parentInstance = m_root;
103 else
104 return QModelIndex();
105 }
106 else
107 parentInstance = static_cast<Node*>(parent.internalPointer())->instance;
108 int idx = 0;
109 KJS::PropertyNameArray props;
110 parentInstance->getPropertyNames(exec, props);
111 for( KJS::PropertyNameArrayIterator ref = props.begin(); ref != props.end(); ref++)
112 {
113 if( idx == row)
114 {
115 childItem = new Node;
116 childItem->name = ref->ascii(); //### M.O.: this is wrong, can be unicode.
117 childItem->instance = parentInstance->get( exec,
118 childItem->name.constData() )->toObject(exec);
119 childItem->parent = static_cast<Node*>(parent.internalPointer());
120 break;
121 }
122 ++idx;
123 }
124 if (childItem)
125 return createIndex(row, column, childItem);
126
127 return QModelIndex();
128}
129
130QModelIndex KJSObjectModel::parent(const QModelIndex &index) const
131{
132 if (!index.isValid())
133 {
134 Node *node = new Node;
135 node->instance = m_root;
136 node->name = "Objects";
137 node->parent = 0;
138 return createIndex(0, index.column(), node);
139 }
140
141 Node *parentItem = static_cast<Node*>(index.internalPointer())->parent;
142 if ( parentItem )
143 {
144 Node *node = new Node;
145 node->instance = parentItem->instance;
146 node->name = parentItem->name;
147 node->parent = parentItem->parent;
148 return createIndex(0, index.column(), node);
149 }
150 else
151 return QModelIndex();
152}
153
154QVariant KJSObjectModel::data(const QModelIndex &index, int role) const
155{
156 if (!index.isValid())
157 return QVariant();
158
159 Node *item = static_cast<Node*>(index.internalPointer());
160 KJS::JSObject *instance = item->instance;
161
162 if (role == Qt::DecorationRole )
163 {
164 if( instance->implementsConstruct() )
165 return QPixmap(":/images/class.png");
166 else if( instance->implementsCall() )
167 return QPixmap(":/images/method.png");
168 else
169 return QPixmap(":/images/property.png");
170 }
171 if( role == Qt::TextColorRole )
172 {
173 if( instance->implementsConstruct() )
174 return QColor("blue");
175 else if( instance->implementsCall() )
176 return QColor("green");
177 else
178 return QColor("black");
179 }
180 if (role == Qt::DisplayRole)
181 return item->name;
182 return QVariant();
183}
184
185#include "kjs_object_model.moc"
KJSObjectModel::parent
QModelIndex parent(const QModelIndex &index) const
Definition: kjs_object_model.cpp:130
KJSObjectModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: kjs_object_model.cpp:154
KJSObjectModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: kjs_object_model.cpp:93
KJSObjectModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: kjs_object_model.cpp:81
KJSObjectModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: kjs_object_model.cpp:75
KJSObjectModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: kjs_object_model.cpp:61
KJSObjectModel::KJSObjectModel
KJSObjectModel(KJS::Interpreter *js, QObject *parent=0)
Definition: kjs_object_model.cpp:38
KJSObjectModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: kjs_object_model.cpp:53
KJSObjectModel::updateModel
void updateModel(KJS::JSObject *m_root)
Definition: kjs_object_model.cpp:43
KJSObjectModel::~KJSObjectModel
~KJSObjectModel()
Definition: kjs_object_model.cpp:49
QAbstractItemModel
QObject
kjs_object_model.h
DomNodeNS::name
END_VALUE_METHOD QString name
Definition: dom.cpp:82
parent
QObject * parent
Definition: qaction_binding.cpp:48
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal