tools/designer/src/components/objectinspector/objectinspector.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the Qt Designer of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 /*
00025 TRANSLATOR qdesigner_internal::ObjectInspector
00026 */
00027 
00028 #include "objectinspector.h"
00029 
00030 // sdk
00031 #include <QtDesigner/QtDesigner>
00032 
00033 // shared
00034 #include <tree_widget_p.h>
00035 #include <qdesigner_promotedwidget_p.h>
00036 
00037 // Qt
00038 #include <QtGui/QAction>
00039 #include <QtGui/QMenu>
00040 #include <QtGui/QApplication>
00041 #include <QtGui/QHeaderView>
00042 #include <QtGui/QScrollBar>
00043 #include <QtGui/QItemDelegate>
00044 #include <QtGui/QPainter>
00045 #include <QtGui/QVBoxLayout>
00046 
00047 #include <QtCore/QStack>
00048 #include <QtCore/QPair>
00049 #include <QtCore/qdebug.h>
00050 
00051 using namespace qdesigner_internal;
00052 
00053 ObjectInspector::ObjectInspector(QDesignerFormEditorInterface *core, QWidget *parent)
00054     : QDesignerObjectInspectorInterface(parent),
00055       m_core(core)
00056 {
00057     QVBoxLayout *vbox = new QVBoxLayout(this);
00058     vbox->setMargin(0);
00059 
00060     m_treeWidget = new TreeWidget(this);
00061     vbox->addWidget(m_treeWidget);
00062 
00063     m_treeWidget->setColumnCount(2);
00064     m_treeWidget->headerItem()->setText(0, tr("Object"));
00065     m_treeWidget->headerItem()->setText(1, tr("Class"));
00066 
00067     m_treeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
00068     m_treeWidget->header()->setResizeMode(1, QHeaderView::Stretch);
00069 
00070     m_treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
00071 
00072     connect(m_treeWidget, SIGNAL(customContextMenuRequested(QPoint)),
00073             this, SLOT(slotPopupContextMenu(QPoint)));
00074 
00075     connect(m_treeWidget, SIGNAL(itemPressed(QTreeWidgetItem*,int)),
00076             this, SLOT(slotSelectionChanged()));
00077 
00078     connect(m_treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
00079             this, SLOT(slotSelectionChanged()));
00080 }
00081 
00082 ObjectInspector::~ObjectInspector()
00083 {
00084 }
00085 
00086 QDesignerFormEditorInterface *ObjectInspector::core() const
00087 {
00088     return m_core;
00089 }
00090 
00091 void ObjectInspector::slotPopupContextMenu(const QPoint &pos)
00092 {
00093     QTreeWidgetItem *item = m_treeWidget->itemAt(pos);
00094     if (!item)
00095         return;
00096 
00097     QObject *object = qvariant_cast<QObject *>(item->data(0, 1000));
00098     if (!object)
00099         return;
00100 
00101 #if defined(TASKMENU_INTEGRATION)
00102     QDesignerTaskMenuExtension *task;
00103 
00104     if (0 != (task = qt_extension<QDesignerTaskMenuExtension*>(core()->extensionManager(), object))) {
00105         QList<QAction*> actions = task->taskActions();
00106 
00107         if (!actions.isEmpty()) {
00108             QMenu menu(this);
00109 
00110             menu.addActions(actions);
00111             menu.exec(m_treeWidget->viewport()->mapToGlobal(pos));
00112         }
00113     }
00114 #endif
00115 }
00116 
00117 bool ObjectInspector::sortEntry(const QObject *a, const QObject *b)
00118 {
00119     return a->objectName() < b->objectName();
00120 }
00121 
00122 void ObjectInspector::setFormWindow(QDesignerFormWindowInterface *fw)
00123 {
00124     m_formWindow = fw;
00125 
00126     if (fw && fw->cursor())
00127         m_selected = core()->propertyEditor()->object();
00128 
00129     int xoffset = m_treeWidget->horizontalScrollBar()->value();
00130     int yoffset = m_treeWidget->verticalScrollBar()->value();
00131 
00132     m_treeWidget->clear();
00133 
00134     if (!fw || !fw->mainContainer())
00135         return;
00136 
00137     QDesignerWidgetDataBaseInterface *db = fw->core()->widgetDataBase();
00138 
00139     m_treeWidget->setUpdatesEnabled(false);
00140 
00141     QStack< QPair<QTreeWidgetItem*, QObject*> > workingList;
00142     QObject *rootObject = fw->mainContainer();
00143     workingList.append(qMakePair(new QTreeWidgetItem(m_treeWidget), rootObject));
00144     QTreeWidgetItem *theSelectedItem = 0;
00145 
00146     while (!workingList.isEmpty()) {
00147         QTreeWidgetItem *item = workingList.top().first;
00148         QObject *object = workingList.top().second;
00149         workingList.pop();
00150 
00151         if (m_selected == object)
00152             theSelectedItem = item;
00153 
00154         QString className = object->metaObject()->className();
00155         if (QDesignerWidgetDataBaseItemInterface *widgetItem = db->item(db->indexOfObject(object, true))) {
00156             className = widgetItem->name();
00157 
00158             if (object->isWidgetType() && className == QLatin1String("QLayoutWidget")
00159                     && static_cast<QWidget*>(object)->layout()) {
00160                 className = QLatin1String(static_cast<QWidget*>(object)->layout()->metaObject()->className());
00161             }
00162 
00163             item->setIcon(0, widgetItem->icon());
00164         }
00165 
00166         if (className.startsWith("QDesigner"))
00167             className.remove(1, 8);
00168 
00169         item->setText(1, className);
00170 
00171         item->setData(0, 1000, qVariantFromValue(object));
00172 
00173         if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(object))
00174             object = promoted->child();
00175 
00176         QString objectName = object->objectName();
00177         if (objectName.isEmpty())
00178             objectName = tr("<noname>");
00179 
00180         if (QAction *act = qobject_cast<QAction*>(object)) { // separator is reserved
00181             if (act->isSeparator()) {
00182                 objectName = tr("separator");
00183             }
00184             item->setIcon(0, act->icon());
00185         }
00186 
00187         item->setText(0, objectName);
00188 
00189         if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(fw->core()->extensionManager(), object)) {
00190             for (int i=0; i<c->count(); ++i) {
00191                 QObject *page = c->widget(i);
00192                 Q_ASSERT(page != 0);
00193 
00194                 QTreeWidgetItem *pageItem = new QTreeWidgetItem(item);
00195                 workingList.append(qMakePair(pageItem, page));
00196             }
00197         } else {
00198             QList<QObject*> children;
00199             if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(object))
00200                 children = promoted->child()->children();
00201             else
00202                 children = object->children();
00203 
00204             qSort(children.begin(), children.end(), ObjectInspector::sortEntry);
00205 
00206             foreach (QObject *child, children) {
00207                 QWidget *widget = qobject_cast<QWidget*>(child);
00208                 if (!widget || !fw->isManaged(widget))
00209                     continue;
00210 
00211                 QTreeWidgetItem *childItem = new QTreeWidgetItem(item);
00212                 workingList.append(qMakePair(childItem, child));
00213             }
00214 
00215             if (QWidget *widget = qobject_cast<QWidget*>(object)) {
00216                 QList<QAction*> actions = widget->actions();
00217                 foreach (QAction *action, actions) {
00218                     if (!fw->core()->metaDataBase()->item(action))
00219                         continue;
00220 
00221                     QObject *obj = action;
00222                     if (action->menu())
00223                         obj = action->menu();
00224 
00225                     QTreeWidgetItem *childItem = new QTreeWidgetItem(item);
00226                     workingList.append(qMakePair(childItem, obj));
00227                 }
00228             }
00229         }
00230 
00231         m_treeWidget->expandItem(item);
00232     }
00233 
00234     m_treeWidget->horizontalScrollBar()->setValue(xoffset);
00235     m_treeWidget->verticalScrollBar()->setValue(yoffset);
00236 
00237     if (theSelectedItem) {
00238         m_treeWidget->setCurrentItem(theSelectedItem);
00239         m_treeWidget->scrollToItem(theSelectedItem);
00240     }
00241 
00242     m_treeWidget->setUpdatesEnabled(true);
00243     m_treeWidget->update();
00244 
00245     m_treeWidget->resizeColumnToContents(0);
00246 }
00247 
00248 void ObjectInspector::slotSelectionChanged()
00249 {
00250     if (!m_formWindow)
00251         return;
00252 
00253     m_formWindow->clearSelection(false);
00254 
00255     QList<QTreeWidgetItem*> items = m_treeWidget->selectedItems();
00256 
00257     foreach (QTreeWidgetItem *item, items) {
00258         QObject *object = qvariant_cast<QObject *>(item->data(0, 1000));
00259         m_selected = object;
00260 
00261         QWidget *widget = qobject_cast<QWidget*>(object);
00262 
00263         if (widget && m_formWindow->isManaged(widget)) {
00264             m_formWindow->selectWidget(widget);
00265         } else if (core()->metaDataBase()->item(object)) {
00266             // refresh at least the property editor
00267             core()->propertyEditor()->setObject(object);
00268         }
00269     }
00270     QMetaObject::invokeMethod(m_formWindow->core()->formWindowManager(), "slotUpdateActions");
00271 }
00272 
00273 void ObjectInspector::showEvent(QShowEvent *event)
00274 {
00275     m_treeWidget->resizeColumnToContents(0);
00276     QDesignerObjectInspectorInterface::showEvent(event);
00277 }

Generated on Thu Mar 15 12:01:06 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1