kjsembed
kjs_object_model.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 #include "kjs_object_model.h" 00023 00024 #include <QtGui/QPixmap> 00025 #include <QtCore/QDebug> 00026 00027 #include <kjs/object.h> 00028 #include <kjs/interpreter.h> 00029 #include <kjs/PropertyNameArray.h> 00030 00031 struct Node 00032 { 00033 QByteArray name; 00034 KJS::JSObject *instance; 00035 Node *parent; 00036 }; 00037 00038 KJSObjectModel::KJSObjectModel(KJS::Interpreter *js, QObject *parent ): 00039 QAbstractItemModel(parent), m_js(js) 00040 { 00041 } 00042 00043 void KJSObjectModel::updateModel( KJS::JSObject *root) 00044 { 00045 m_root = root; 00046 reset(); 00047 } 00048 00049 KJSObjectModel::~KJSObjectModel() 00050 { 00051 } 00052 00053 Qt::ItemFlags KJSObjectModel::flags(const QModelIndex &index) const 00054 { 00055 if (!index.isValid()) 00056 return Qt::ItemIsEnabled; 00057 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; 00058 } 00059 00060 00061 int KJSObjectModel::rowCount(const QModelIndex &parent ) const 00062 { 00063 KJS::ExecState *exec = m_js->globalExec(); 00064 KJS::PropertyNameArray props; 00065 if (!parent.isValid()) 00066 m_root->getPropertyNames(exec, props); 00067 else 00068 { 00069 Node *item = static_cast<Node*>(parent.internalPointer()); 00070 item->instance->getPropertyNames(exec, props); 00071 } 00072 return props.size(); 00073 } 00074 00075 int KJSObjectModel::columnCount(const QModelIndex &parent ) const 00076 { 00077 Q_UNUSED(parent); 00078 return 1; 00079 } 00080 00081 QVariant KJSObjectModel::headerData(int section, Qt::Orientation orientation, int role) const 00082 { 00083 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) 00084 { 00085 if( section == 0) 00086 return "Object Name"; 00087 else 00088 return "Value"; 00089 } 00090 return QVariant(); 00091 } 00092 00093 QModelIndex KJSObjectModel::index(int row, int column, const QModelIndex &parent ) const 00094 { 00095 KJS::JSObject *parentInstance = 0; 00096 Node *childItem = 0; 00097 KJS::ExecState *exec = m_js->globalExec(); 00098 00099 if (!parent.isValid()) 00100 { 00101 if (m_root) 00102 parentInstance = m_root; 00103 else 00104 return QModelIndex(); 00105 } 00106 else 00107 parentInstance = static_cast<Node*>(parent.internalPointer())->instance; 00108 int idx = 0; 00109 KJS::PropertyNameArray props; 00110 parentInstance->getPropertyNames(exec, props); 00111 for( KJS::PropertyNameArrayIterator ref = props.begin(); ref != props.end(); ref++) 00112 { 00113 if( idx == row) 00114 { 00115 childItem = new Node; 00116 childItem->name = ref->ascii(); //### M.O.: this is wrong, can be unicode. 00117 childItem->instance = parentInstance->get( exec, 00118 childItem->name.constData() )->toObject(exec); 00119 childItem->parent = static_cast<Node*>(parent.internalPointer()); 00120 break; 00121 } 00122 ++idx; 00123 } 00124 if (childItem) 00125 return createIndex(row, column, childItem); 00126 00127 return QModelIndex(); 00128 } 00129 00130 QModelIndex KJSObjectModel::parent(const QModelIndex &index) const 00131 { 00132 if (!index.isValid()) 00133 { 00134 Node *node = new Node; 00135 node->instance = m_root; 00136 node->name = "Objects"; 00137 node->parent = 0; 00138 return createIndex(0, index.column(), node); 00139 } 00140 00141 Node *parentItem = static_cast<Node*>(index.internalPointer())->parent; 00142 if ( parentItem ) 00143 { 00144 Node *node = new Node; 00145 node->instance = parentItem->instance; 00146 node->name = parentItem->name; 00147 node->parent = parentItem->parent; 00148 return createIndex(0, index.column(), node); 00149 } 00150 else 00151 return QModelIndex(); 00152 } 00153 00154 QVariant KJSObjectModel::data(const QModelIndex &index, int role) const 00155 { 00156 if (!index.isValid()) 00157 return QVariant(); 00158 00159 Node *item = static_cast<Node*>(index.internalPointer()); 00160 KJS::JSObject *instance = item->instance; 00161 00162 if (role == Qt::DecorationRole ) 00163 { 00164 if( instance->implementsConstruct() ) 00165 return QPixmap(":/images/class.png"); 00166 else if( instance->implementsCall() ) 00167 return QPixmap(":/images/method.png"); 00168 else 00169 return QPixmap(":/images/property.png"); 00170 } 00171 if( role == Qt::TextColorRole ) 00172 { 00173 if( instance->implementsConstruct() ) 00174 return QColor("blue"); 00175 else if( instance->implementsCall() ) 00176 return QColor("green"); 00177 else 00178 return QColor("black"); 00179 } 00180 if (role == Qt::DisplayRole) 00181 return item->name; 00182 return QVariant(); 00183 } 00184 00185 #include "kjs_object_model.moc"
KDE 4.6 API Reference