oopapidocs
2.0
|
00001 #include "qobjecttree.h" 00002 00003 QList<QObject*> QObjectTreeModel::sm_extraObjects; 00004 00005 void QObjectTreeModel::addExtraObject(QObject *extra) { 00006 sm_extraObjects << extra; 00007 } 00008 00009 QObjectTreeItem::QObjectTreeItem(QObject *obj_in, QObjectTreeItem *parent) { 00010 // receivers("test"); 00011 00012 m_parentItem=parent; 00013 m_obj=obj_in; 00014 if (m_obj) { 00015 00016 const QMetaObject *metaobj=m_obj->metaObject(); 00017 if (metaobj) { 00018 m_object_type=metaobj->className(); 00019 } 00020 else { 00021 m_object_type="Unknown"; 00022 } 00023 // Find a good name for the object. Check a few places. 00024 QString text = m_obj->objectName(); 00025 if (text.isEmpty()) 00026 text = m_obj->property("title").toString(); 00027 if (text.isEmpty()) 00028 text = m_obj->property("windowTitle").toString(); 00029 if (text.isEmpty()) 00030 text = m_obj->property("text").toString(); 00031 text = text.left(20); 00032 if (!text.isEmpty()) { 00033 m_object_type += QString(" (%1)").arg(text); 00034 } 00035 connect(m_obj, SIGNAL(destroyed()), 00036 this, SLOT(on_object_destroyed())); 00037 } 00038 else { 00039 m_object_type="Objects"; 00040 } 00041 } 00042 QObjectTreeItem::~QObjectTreeItem() { 00043 qDeleteAll(m_childItems); 00044 m_childItems.clear(); 00045 } 00046 00047 void QObjectTreeItem::appendChild(QObjectTreeItem *child) { 00048 m_childItems.append(child); 00049 } 00050 void QObjectTreeItem::removeChild(int row) { 00051 QObjectTreeItem *ptr=child(row); 00052 m_childItems.removeAt(row); 00053 if (ptr) { 00054 delete ptr; 00055 } 00056 } 00057 00058 QObjectTreeItem *QObjectTreeItem::child(int row) { 00059 return m_childItems.value(row); 00060 } 00061 int QObjectTreeItem::childCount() const { 00062 return m_childItems.count(); 00063 } 00064 int QObjectTreeItem::columnCount() const { 00065 return 1; 00066 } 00067 QVariant QObjectTreeItem::data(int column) const { 00068 if (column==0) 00069 return QVariant(m_object_type); 00070 else 00071 return QVariant(); 00072 00073 } 00074 int QObjectTreeItem::row() const { 00075 if (m_parentItem) 00076 return m_parentItem->m_childItems.indexOf(const_cast<QObjectTreeItem*>(this)); 00077 return 0; 00078 } 00079 QObjectTreeItem *QObjectTreeItem::parent() { 00080 return m_parentItem; 00081 } 00082 QObjectTreeItem *QObjectTreeItem::find_child(QObject *ptr) { 00083 int j; 00084 for (j=0; j<childCount(); j++) 00085 if (child(j)->m_obj==ptr) 00086 return child(j); 00087 return 0; 00088 } 00089 void QObjectTreeItem::on_object_destroyed() { 00090 m_obj=0; 00091 } 00092 00093 00094 00095 QObjectTreeModel::QObjectTreeModel(QObject *parent) : QAbstractItemModel(parent) { 00096 rootItem=new QObjectTreeItem(0); 00097 refresh(); 00098 } 00099 QObjectTreeModel::~QObjectTreeModel() { 00100 delete rootItem; 00101 } 00102 00103 QVariant QObjectTreeModel::data(const QModelIndex &index, int role) const { 00104 if (!index.isValid()) 00105 return QVariant(); 00106 00107 if (role==Qt::DisplayRole) { 00108 QObjectTreeItem *item = static_cast<QObjectTreeItem*>(index.internalPointer()); 00109 if (item) 00110 return item->data(index.column()); 00111 } 00112 else if (role==Qt::DecorationRole) { 00113 QObjectTreeItem *item = static_cast<QObjectTreeItem*>(index.internalPointer()); 00114 if (item) { 00115 if (item->m_obj->isWidgetType()) 00116 return QIcon(":/images/widget.png"); 00117 else 00118 return QIcon(":/images/object.png"); 00119 } 00120 } 00121 return QVariant(); 00122 } 00123 Qt::ItemFlags QObjectTreeModel::flags(const QModelIndex &index) const { 00124 if (!index.isValid()) 00125 return Qt::ItemIsEnabled; 00126 00127 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; 00128 } 00129 QVariant QObjectTreeModel::headerData(int section, Qt::Orientation orientation, 00130 int role) const 00131 { 00132 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) 00133 return rootItem->data(section); 00134 00135 return QVariant(); 00136 } 00137 QModelIndex QObjectTreeModel::index(int row, int column, 00138 const QModelIndex &parent) const 00139 { 00140 QObjectTreeItem *parentItem; 00141 00142 if (!parent.isValid()) 00143 parentItem = rootItem; 00144 else 00145 parentItem = static_cast<QObjectTreeItem*>(parent.internalPointer()); 00146 00147 QObjectTreeItem *childItem = parentItem->child(row); 00148 if (childItem) 00149 return createIndex(row, column, childItem); 00150 else 00151 return QModelIndex(); 00152 } 00153 QModelIndex QObjectTreeModel::parent(const QModelIndex &index) const { 00154 if (!index.isValid()) 00155 return QModelIndex(); 00156 00157 QObjectTreeItem *childItem = static_cast<QObjectTreeItem*>(index.internalPointer()); 00158 QObjectTreeItem *parentItem = childItem->parent(); 00159 00160 if (parentItem == rootItem) 00161 return QModelIndex(); 00162 00163 return createIndex(parentItem->row(), 0, parentItem); 00164 } 00165 QObjectTreeItem *QObjectTreeModel::find_item(QObjectTreeItem *ptr,QObject *obj) { 00166 if (!ptr) return 0; 00167 if (ptr->m_obj==obj) return ptr; 00168 int j; 00169 for (j=0; j<ptr->childCount(); j++) { 00170 QObjectTreeItem *ret=find_item(ptr->child(j),obj); 00171 if (ret) return ret; 00172 } 00173 return 0; 00174 } 00175 QModelIndex QObjectTreeModel::getindex(QObject *obj) { 00176 QObjectTreeItem *item=find_item(rootItem,obj); 00177 if (!item) return QModelIndex(); 00178 return createIndex(item->row(),0,item); 00179 } 00180 int QObjectTreeModel::rowCount(const QModelIndex &parent) const { 00181 QObjectTreeItem *parentItem; 00182 00183 if (!parent.isValid()) 00184 parentItem = rootItem; 00185 else 00186 parentItem = static_cast<QObjectTreeItem*>(parent.internalPointer()); 00187 00188 return parentItem->childCount(); 00189 } 00190 int QObjectTreeModel::columnCount(const QModelIndex &parent) const { 00191 if (parent.isValid()) 00192 return static_cast<QObjectTreeItem*>(parent.internalPointer())->columnCount(); 00193 else 00194 return rootItem->columnCount(); 00195 } 00196 void QObjectTreeModel::refresh() { 00197 layoutAboutToBeChanged(); 00198 refresh(QModelIndex()); 00199 layoutChanged(); 00200 } 00201 void QObjectTreeModel::clear_persistent_data(QModelIndex index) { 00202 int j; 00203 for (j=0; j<rowCount(index); j++) 00204 clear_persistent_data(index.child(j,0)); 00205 changePersistentIndex(index,QModelIndex()); 00206 } 00207 void QObjectTreeModel::refresh(const QModelIndex &aIndex) { 00208 QModelIndex index=aIndex; 00209 QObjectTreeItem *parent = static_cast<QObjectTreeItem *>(index.internalPointer()); 00210 if (!parent) { 00211 parent = rootItem; 00212 index=createIndex(0,0,rootItem); 00213 } 00214 if (!parent) return; 00215 if (!index.isValid()) return; 00216 00217 QObjectList object_list; 00218 00219 if (parent->m_obj) 00220 object_list=parent->m_obj->children(); 00221 else { 00222 object_list.append(qApp); 00223 object_list.append(sm_extraObjects); 00224 QWidgetList widgets=qApp->allWidgets(); 00225 int j; 00226 for (j=0; j<widgets.count(); j++) { 00227 if (!widgets[j]->parentWidget()) { //top level widget 00228 object_list.append(widgets[j]); 00229 } 00230 } 00231 } 00232 00233 int j; 00234 for (j=0; j<object_list.count(); j++) { 00235 QObjectTreeItem *child=parent->find_child(object_list[j]); 00236 if (!child) { 00237 //emit layoutAboutToBeChanged(); 00238 child=new QObjectTreeItem(object_list[j],parent); 00239 parent->appendChild(child); 00240 //emit layoutChanged(); 00241 } 00242 if (child) { 00243 refresh(index.child(child->row(),0)); 00244 } 00245 } 00246 00247 for (j=0; j<parent->childCount(); j++) { 00248 if (!object_list.contains(parent->child(j)->m_obj)) { 00249 //emit layoutAboutToBeChanged(); 00250 clear_persistent_data(index.child(j,0)); 00251 parent->removeChild(j); 00252 //emit layoutChanged(); 00253 j--; 00254 } 00255 } 00256 00257 } 00258 00259 00260 QObjectTree::QObjectTree() { 00261 setup_widget_menu(); 00262 } 00263 00264 void QObjectTree::setup_widget_menu() { 00265 widget_menu=new QMenu; 00266 00267 QAction *Act; 00268 00269 //Show 00270 Act = new QAction(tr("Show"), this); 00271 connect(Act, SIGNAL(triggered()), this, SIGNAL(show_widget())); 00272 widget_menu->addAction(Act); 00273 00274 //Hide 00275 Act = new QAction(tr("Hide"), this); 00276 connect(Act, SIGNAL(triggered()), this, SIGNAL(hide_widget())); 00277 widget_menu->addAction(Act); 00278 00279 //Focus 00280 Act = new QAction(tr("Focus"), this); 00281 connect(Act, SIGNAL(triggered()), this, SIGNAL(focus_widget())); 00282 widget_menu->addAction(Act); 00283 } 00284 00285 QObjectTree::~QObjectTree() { 00286 } 00287 void QObjectTree::currentChanged ( const QModelIndex & , const QModelIndex & ) { 00288 emit current_item_changed(); 00289 } 00290 void QObjectTree::mousePressEvent ( QMouseEvent * event ) { 00291 QTreeView::mousePressEvent(event); 00292 if (event->button()==Qt::RightButton) { 00293 widget_menu->popup(mapToGlobal(QPoint(event->x(),event->y()))); 00294 } 00295 }