ij-16.png   inspectorJ -- JavaTM Profiler
sf project site browse source checkout source
SourceForge.net Logo



src/inspectorj/model/classtreemodel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   inspectorJ - java profiler                                            *
00003  *   Copyright (C) 2007 by  James May
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00027 #include <QtGui>
00028 
00029 #include "inspectorj/model/treeitem.h"
00030 #include "inspectorj/model/classtreemodel.h"
00031 
00032 using inspectorj::model::TreeItem;
00033         
00034 namespace inspectorj {
00035 namespace model {     
00036         
00037 ClassTreeModel::ClassTreeModel(QObject *parent)
00038     : QAbstractItemModel(parent), javaClass(0), rootItem(0)
00039 {
00040 }
00041 
00042 ClassTreeModel::~ClassTreeModel()
00043 {
00044     if(rootItem) {
00045         delete rootItem;
00046         rootItem = 0;        
00047     }
00048 }
00049 
00050 int ClassTreeModel::columnCount(const QModelIndex &parent) const
00051 {
00052     if (parent.isValid())
00053         return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
00054     else
00055         return rootItem->columnCount();
00056 }
00057 
00058 QVariant ClassTreeModel::data(const QModelIndex &index, int role) const
00059 {
00060     if (!index.isValid())
00061         return QVariant();
00062 
00063     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
00064     if (role == Qt::DisplayRole) {        
00065         return item->data(index.column());
00066     }
00067     
00068     if (role == Qt::ForegroundRole) {
00069         if (item->parent() != 0) {
00070             if (item->parent()->parent() == 0)
00071                 return Qt::darkBlue;
00072         }
00073         if (item->childCount() > 0)
00074             return Qt::darkRed;
00075     }
00076     return QVariant();
00077 }
00078 
00079 Qt::ItemFlags ClassTreeModel::flags(const QModelIndex &index) const
00080 {
00081     if (!index.isValid())
00082         return Qt::ItemIsEnabled;
00083 
00084     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00085 }
00086 
00087 QVariant ClassTreeModel::headerData(int section, Qt::Orientation orientation,
00088                                 int role) const
00089 {
00090     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
00091         return rootItem->data(section);
00092 
00093     return QVariant();
00094 }
00095 
00096 QModelIndex ClassTreeModel::index(int row, int column, const QModelIndex &parent)
00097             const
00098 {
00099     TreeItem *parentItem;
00100 
00101     if (!parent.isValid())
00102         parentItem = rootItem;
00103     else
00104         parentItem = static_cast<TreeItem*>(parent.internalPointer());
00105 
00106     TreeItem *childItem = parentItem->child(row);
00107     if (childItem)
00108         return createIndex(row, column, childItem);
00109     else
00110         return QModelIndex();
00111 }
00112 
00113 QModelIndex ClassTreeModel::parent(const QModelIndex &index) const
00114 {
00115     if (!index.isValid())
00116         return QModelIndex();
00117 
00118     TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
00119     TreeItem *parentItem = childItem->parent();
00120     if (parentItem == rootItem)
00121         return QModelIndex();
00122 
00123     return createIndex(parentItem->row(), 0, parentItem);
00124 }
00125 
00126 int ClassTreeModel::rowCount(const QModelIndex &parent) const
00127 {
00128     TreeItem *parentItem;
00129 
00130     if (!parent.isValid())
00131         parentItem = rootItem;
00132     else
00133         parentItem = static_cast<TreeItem*>(parent.internalPointer());
00134 
00135     return parentItem->childCount();
00136 }
00137 
00138 void ClassTreeModel::setupModelData(TreeItem *root)
00139 {
00140     QList<QVariant> classlist;
00141     classlist << this->javaClass->getClassSignature();
00142     TreeItem *classItem = new TreeItem(classlist,root);
00143     root->appendChild(classItem);
00144     
00145     QList<QVariant> fields;
00146     fields << QString("fields");
00147     
00148     // Add Fields
00149     TreeItem *fieldsItem = new TreeItem(fields, classItem);
00150     QList<JavaField*>& fieldList = this->javaClass->getFields();
00151     QList<JavaField*>::iterator iter;
00152     for (iter = fieldList.begin(); iter != fieldList.end(); ++iter) {
00153         QList<QVariant> fieldData;
00154         fieldData << (*iter)->getFieldName()
00155                 << (*iter)->getFieldSignature()
00156                 << (*iter)->getFieldId().value;
00157         TreeItem *fieldItem = new TreeItem(fieldData, fieldsItem);
00158         fieldsItem->appendChild(fieldItem);
00159     }
00160     if (fieldList.size() > 0) {
00161         classItem->appendChild(fieldsItem);            
00162     }
00163     
00164     QList<QVariant> methods;
00165     methods << QString("methods");
00166     
00167     // Add Methods
00168     TreeItem *methodsItem = new TreeItem(methods, classItem);
00169     QList<JavaMethod*>& methodList = this->javaClass->getMethods();
00170     QList<JavaMethod*>::iterator m_iter;
00171     for (m_iter = methodList.begin(); m_iter != methodList.end(); ++m_iter) {
00172         QList<QVariant> methodData;
00173         methodData << (*m_iter)->getMethodSignature() 
00174                 << (*m_iter)->getJNIMethodSignature() 
00175                 << (*m_iter)->getMethodId().value;
00176         TreeItem *methodItem = new TreeItem(methodData, methodsItem);
00177         methodsItem->appendChild(methodItem);
00178     }    
00179     if (methodList.size() > 0) {
00180         classItem->appendChild(methodsItem);           
00181     } 
00182 }
00183 
00187 void ClassTreeModel::setJavaClass(JavaClass* javaClass)
00188 {
00189     this->javaClass = javaClass;
00190 }
00191 
00195 void ClassTreeModel::initModel()
00196 {
00197     if (! javaClass) {
00198         return;
00199     }
00200     
00201     QList<QVariant> rootData;
00202     rootData << this->javaClass->getJNIClassSignature() << QString("");
00203     if (rootItem) {
00204         delete rootItem;
00205         rootItem = 0;
00206     }
00207     rootItem = new TreeItem(rootData);
00208     setupModelData(rootItem);
00209     reset();
00210 }
00211 
00212 
00213 } // end namespace inspectorj
00214 } // end namespace model

Generated on Sun Aug 19 17:07:53 2007 for inspectorJ by  doxygen 1.5.1