objectinspector.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 655 行 · 第 1/2 页

CPP
655
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************//*TRANSLATOR qdesigner_internal::ObjectInspector*/#include "objectinspector.h"#include "formwindow.h"// sdk#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QDesignerTaskMenuExtension>#include <QtDesigner/QExtensionManager>#include <QtDesigner/QDesignerFormWindowCursorInterface>#include <QtDesigner/QDesignerFormWindowManagerInterface>#include <QtDesigner/QDesignerWidgetDataBaseInterface>#include <QtDesigner/QDesignerContainerExtension>#include <QtDesigner/QDesignerMetaDataBaseInterface>#include <QtDesigner/QDesignerPropertyEditorInterface>// shared#include <formwindowbase_p.h>#include <tree_widget_p.h>#include <qdesigner_dnditem_p.h>// Qt#include <QtGui/QAction>#include <QtGui/QMenu>#include <QtGui/QApplication>#include <QtGui/QHeaderView>#include <QtGui/QScrollBar>#include <QtGui/QItemDelegate>#include <QtGui/QPainter>#include <QtGui/QVBoxLayout>#include <QtGui/QItemSelectionModel>#include <QtGui/qevent.h>#include <QtCore/QStack>#include <QtCore/QPair>#include <QtCore/qdebug.h>static inline QObject *objectOfItem(QTreeWidgetItem *item) {    return qvariant_cast<QObject *>(item->data(0, 1000));}namespace {    // Event filter to be installed on the treeview viewport    // that suppresses a range selection by dragging    // which does not really work due to the need to maintain    // a consistent selection    class ObjectInspectorEventFilter :public QObject {    public:        ObjectInspectorEventFilter(QObject *parent) : QObject(parent) {}        bool eventFilter(QObject * /*watched*/, QEvent *event )  {            if (event->type() == QEvent::MouseMove) {                event->ignore();                return true;            }            return false;        }    };}namespace qdesigner_internal {ObjectInspector::ObjectInspector(QDesignerFormEditorInterface *core, QWidget *parent)    : QDesignerObjectInspector(parent),      m_core(core),      m_treeWidget(new TreeWidget(this)){    m_treeWidget->viewport()->installEventFilter(new ObjectInspectorEventFilter(this));    QVBoxLayout *vbox = new QVBoxLayout(this);    vbox->setMargin(0);    vbox->addWidget(m_treeWidget);    m_treeWidget->setColumnCount(2);    m_treeWidget->headerItem()->setText(0, tr("Object"));    m_treeWidget->headerItem()->setText(1, tr("Class"));    m_treeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);    m_treeWidget->header()->setResizeMode(1, QHeaderView::Stretch);    m_treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);    m_treeWidget->setTextElideMode (Qt::ElideMiddle);    m_treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);    connect(m_treeWidget, SIGNAL(customContextMenuRequested(QPoint)),            this, SLOT(slotPopupContextMenu(QPoint)));    connect(m_treeWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),            this, SLOT(slotSelectionChanged(QItemSelection,QItemSelection)));    connect(m_treeWidget->header(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(slotHeaderDoubleClicked(int)));    setAcceptDrops(true);}ObjectInspector::~ObjectInspector(){}QDesignerFormEditorInterface *ObjectInspector::core() const{    return m_core;}void ObjectInspector::slotPopupContextMenu(const QPoint &pos){    if (m_formWindow == 0 || m_formWindow->currentTool() != 0)        return;    QTreeWidgetItem *item = m_treeWidget->itemAt(pos);    if (!item)        return;    QObject *object = objectOfItem(item);    if (!object)        return;    QMenu *menu = 0;    if (object->isWidgetType()) {        if (qdesigner_internal::FormWindowBase *fwb = qobject_cast<qdesigner_internal::FormWindowBase*>(m_formWindow))            menu = fwb->initializePopupMenu(qobject_cast<QWidget *>(object));    } else {        // Pull extension for non-widget        QDesignerTaskMenuExtension *taskMenu = qt_extension<QDesignerTaskMenuExtension*>(core()->extensionManager(), object);        if (taskMenu) {            QList<QAction*> actions = taskMenu->taskActions();            if (!actions.isEmpty()) {                menu = new QMenu(this);                menu->addActions(actions);            }        }    }    if (menu) {        menu->exec(m_treeWidget->viewport()->mapToGlobal(pos));        delete menu;    }}bool ObjectInspector::sortEntry(const QObject *a, const QObject *b){    return a->objectName() < b->objectName();}namespace {    enum SelectionType {        NoSelection,        // A QObject that has a meta database entry        QObjectSelection,        // Unmanaged widget, menu bar or the like        UnmanagedWidgetSelection,        // A widget managed by the form window        ManagedWidgetSelection };}static inline SelectionType selectionType(const QDesignerFormWindowInterface *fw, QObject *o){    if (!o->isWidgetType())        return fw->core()->metaDataBase()->item(o) ?  QObjectSelection : NoSelection;    return fw->isManaged(qobject_cast<QWidget *>(o)) ? ManagedWidgetSelection :  UnmanagedWidgetSelection;}ObjectInspector::PreviousSelection ObjectInspector::previousSelection(QDesignerFormWindowInterface *fw,                                                                      bool formWindowChanged) const{    PreviousSelection rc;    const QDesignerFormWindowCursorInterface* cursor = fw->cursor();    const QWidget *current = cursor->current();    const bool currentIsMainContainer = current == fw || current == fw->mainContainer();    const int selectedWidgetCount = cursor->selectedWidgetCount();    // If the selection is current main container only, check previously selected    // non-managed widgets or objects    if (currentIsMainContainer && selectedWidgetCount <= 1 && !formWindowChanged) {        typedef  QList<QTreeWidgetItem*> ItemList;        const ItemList selection = m_treeWidget->selectedItems ();        if (!selection.empty()) {            const ItemList::const_iterator cend = selection.constEnd();            for (ItemList::const_iterator it = selection.constBegin(); it != cend; ++it)                if (QObject *o = objectOfItem(*it))                    switch (selectionType(fw, o)) {                    case QObjectSelection:                    case UnmanagedWidgetSelection:                        rc.insert(o);                        break;                    default:                        break;                    }        }    }    // None - Add widget selection    if (rc.empty())        for (int i = 0; i < selectedWidgetCount; i++)            rc.insert(cursor->selectedWidget(i));    return rc;}void ObjectInspector::setFormWindow(QDesignerFormWindowInterface *fwi){    FormWindowBase *fw = qobject_cast<FormWindowBase *>(fwi);    const bool formWindowChanged = m_formWindow != fw;    const bool resizeToColumn = formWindowChanged;    m_formWindow = fw;    const int oldWidth = m_treeWidget->columnWidth(0);    const int xoffset = m_treeWidget->horizontalScrollBar()->value();    const int yoffset = m_treeWidget->verticalScrollBar()->value();    if (formWindowChanged)        m_formFakeDropTarget = 0;    const bool selectionBlocked = m_treeWidget->selectionModel()->blockSignals(true);    if (!fw || !fw->mainContainer()) {        m_treeWidget->clear();        m_treeWidget->selectionModel()->blockSignals(selectionBlocked);        return;    }    // maintain selection    const PreviousSelection oldSelection = previousSelection(fw, formWindowChanged);    m_treeWidget->clear();    const QDesignerWidgetDataBaseInterface *db = fw->core()->widgetDataBase();    m_treeWidget->setUpdatesEnabled(false);    typedef QPair<QTreeWidgetItem*, QObject*> ItemObjectPair;    QStack<ItemObjectPair> workingList;    QObject *rootObject = fw->mainContainer();    workingList.append(qMakePair(new QTreeWidgetItem(m_treeWidget), rootObject));    // remember the selection and apply later    typedef QVector<QTreeWidgetItem*> SelectionList;    SelectionList selectionList;    const QString qLayoutWidget = QLatin1String("QLayoutWidget");    const QString designerPrefix = QLatin1String("QDesigner");    static const QString noName = tr("<noname>");    static const QString separator =  tr("separator");    while (!workingList.isEmpty()) {        QTreeWidgetItem *item = workingList.top().first;        QObject *object = workingList.top().second;        workingList.pop();        const bool isWidget = object->isWidgetType();        // MainWindow can be current, but not explicitly be selected.        if (oldSelection.contains(object))            selectionList.push_back(item);        QString className = QLatin1String(object->metaObject()->className());        if (QDesignerWidgetDataBaseItemInterface *widgetItem = db->item(db->indexOfObject(object, true))) {            className = widgetItem->name();            if (isWidget && className == qLayoutWidget                    && static_cast<QWidget*>(object)->layout()) {                className = QLatin1String(static_cast<QWidget*>(object)->layout()->metaObject()->className());            }            item->setIcon(0, widgetItem->icon());        }        if (className.startsWith(designerPrefix))            className.remove(1, designerPrefix.size() - 1);        item->setText(1, className);        item->setToolTip(1, className);        item->setData(0, 1000, qVariantFromValue(object));        QString objectName = object->objectName();        if (objectName.isEmpty())            objectName = noName;        if (const QAction *act = qobject_cast<const QAction*>(object)) { // separator is reserved            if (act->isSeparator()) {                objectName = separator;            }            item->setIcon(0, act->icon());        }        item->setText(0, objectName);        item->setToolTip(0, objectName);        if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(fw->core()->extensionManager(), object)) {            for (int i=0; i<c->count(); ++i) {                QObject *page = c->widget(i);                Q_ASSERT(page != 0);                QTreeWidgetItem *pageItem = new QTreeWidgetItem(item);                workingList.append(qMakePair(pageItem, page));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?