qdesigner_command.cpp

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

CPP
2,136
字号
/******************************************************************************** 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.******************************************************************************/#include "qdesigner_command_p.h"#include "qdesigner_utils_p.h"#include "layout_p.h"#include "qlayout_widget_p.h"#include "qdesigner_widget_p.h"#include "qdesigner_menu_p.h"#include "metadatabase_p.h"#include <QtDesigner/QDesignerFormWindowInterface>#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QDesignerPropertySheetExtension>#include <QtDesigner/QDesignerActionEditorInterface>#include <QtDesigner/QDesignerPropertyEditorInterface>#include <QtDesigner/QExtensionManager>#include <QtDesigner/QDesignerContainerExtension>#include <QtDesigner/QDesignerLayoutDecorationExtension>#include <QtDesigner/QDesignerWidgetFactoryInterface>#include <QtDesigner/QDesignerObjectInspectorInterface>#include <QtCore/qdebug.h>#include <QtGui/QMenuBar>#include <QtGui/QStatusBar>#include <QtGui/QToolBar>#include <QtGui/QToolBox>#include <QtGui/QStackedWidget>#include <QtGui/QTabWidget>#include <QtGui/QTableWidget>#include <QtGui/QTreeWidget>#include <QtGui/QListWidget>#include <QtGui/QComboBox>#include <QtGui/QSplitter>#include <QtGui/QDockWidget>#include <QtGui/QMainWindow>#include <QtGui/QApplication>Q_DECLARE_METATYPE(QWidgetList)namespace qdesigner_internal {// ---- InsertWidgetCommand ----InsertWidgetCommand::InsertWidgetCommand(QDesignerFormWindowInterface *formWindow)    : QDesignerFormWindowCommand(QString(), formWindow){}void InsertWidgetCommand::init(QWidget *widget, bool already_in_form){    m_widget = widget;    setText(QApplication::translate("Command", "Insert '%1'").arg(widget->objectName()));    QWidget *parentWidget = m_widget->parentWidget();    QDesignerFormEditorInterface *core = formWindow()->core();    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);    m_insertMode = deco ? deco->currentInsertMode() : QDesignerLayoutDecorationExtension::InsertWidgetMode;    m_cell = deco ? deco->currentCell() : qMakePair(0, 0);    m_widgetWasManaged = already_in_form;    QList<QWidget *> list = qVariantValue<QWidgetList>(parentWidget->property("_q_widgetOrder"));    list.append(widget);    QVariant v;    qVariantSetValue(v, list);    parentWidget->setProperty("_q_widgetOrder", v);}static void recursiveUpdate(QWidget *w){    w->update();    const QObjectList &l = w->children();    const QObjectList::const_iterator cend = l.end();    for ( QObjectList::const_iterator it = l.begin(); it != cend; ++it) {        if (QWidget *w = qobject_cast<QWidget*>(*it))            recursiveUpdate(w);    }}void InsertWidgetCommand::redo(){    QWidget *parentWidget = m_widget->parentWidget();    Q_ASSERT(parentWidget);    QDesignerFormEditorInterface *core = formWindow()->core();    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);    if (deco != 0) {        if (LayoutInfo::layoutType(core, parentWidget) == LayoutInfo::Grid) {            switch (m_insertMode) {                case QDesignerLayoutDecorationExtension::InsertRowMode: {                    deco->insertRow(m_cell.first);                } break;                case QDesignerLayoutDecorationExtension::InsertColumnMode: {                    deco->insertColumn(m_cell.second);                } break;                default: break;            } // end switch        }        deco->insertWidget(m_widget, m_cell);    }    if (!m_widgetWasManaged)        formWindow()->manageWidget(m_widget);    m_widget->show();    formWindow()->emitSelectionChanged();    if (parentWidget && parentWidget->layout()) {        recursiveUpdate(parentWidget);        parentWidget->layout()->invalidate();    }    refreshBuddyLabels();}void InsertWidgetCommand::undo(){    QWidget *parentWidget = m_widget->parentWidget();    QDesignerFormEditorInterface *core = formWindow()->core();    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);    if (deco) {        deco->removeWidget(m_widget);        deco->simplify();    }    if (!m_widgetWasManaged) {        formWindow()->unmanageWidget(m_widget);        m_widget->hide();    }    formWindow()->emitSelectionChanged();    refreshBuddyLabels();}void InsertWidgetCommand::refreshBuddyLabels(){    typedef QList<QLabel*> LabelList;    const LabelList label_list = qFindChildren<QLabel*>(formWindow());    if (label_list.empty())        return;    const QString buddyProperty = QLatin1String("buddy");    const QString objectName = m_widget->objectName();    const LabelList::const_iterator cend = label_list.constEnd();    for (LabelList::const_iterator it = label_list.constBegin(); it != cend; ++it ) {        if (QDesignerPropertySheetExtension* sheet = propertySheet(*it)) {            const int idx = sheet->indexOf(buddyProperty);            if (idx != -1 && sheet->property(idx).toString() == objectName)                sheet->setProperty(idx, objectName);        }    }}// ---- RaiseWidgetCommand ----RaiseWidgetCommand::RaiseWidgetCommand(QDesignerFormWindowInterface *formWindow)    : QDesignerFormWindowCommand(QString(), formWindow){}void RaiseWidgetCommand::init(QWidget *widget){    m_widget = widget;    setText(QApplication::translate("Command", "Raise '%1'").arg(widget->objectName()));}void RaiseWidgetCommand::redo(){    m_widget->raise();}void RaiseWidgetCommand::undo(){}// ---- LowerWidgetCommand ----LowerWidgetCommand::LowerWidgetCommand(QDesignerFormWindowInterface *formWindow)    : QDesignerFormWindowCommand(QString(), formWindow){}void LowerWidgetCommand::init(QWidget *widget){    m_widget = widget;    setText(QApplication::translate("Command", "Lower '%1'").arg(widget->objectName()));}void LowerWidgetCommand::redo(){    m_widget->raise();}void LowerWidgetCommand::undo(){}// ---- DeleteWidgetCommand ----DeleteWidgetCommand::DeleteWidgetCommand(QDesignerFormWindowInterface *formWindow)    : QDesignerFormWindowCommand(QString(), formWindow){}void DeleteWidgetCommand::init(QWidget *widget){    m_widget = widget;    m_parentWidget = widget->parentWidget();    m_geometry = widget->geometry();    m_layoutType = LayoutInfo::NoLayout;    m_index = -1;    if (hasLayout(m_parentWidget)) {        m_layoutType = LayoutInfo::layoutType(formWindow()->core(), m_parentWidget);        if (QSplitter *splitter = qobject_cast<QSplitter *>(m_parentWidget)) {            m_index = splitter->indexOf(widget);        } else {            // Check for managed layout            if (formWindow()->core()->metaDataBase()->item(m_parentWidget->layout()) == 0)                m_layoutType = LayoutInfo::NoLayout;            switch (m_layoutType) {                case LayoutInfo::VBox:                    m_index = qobject_cast<QVBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget);                    break;                case LayoutInfo::HBox:                    m_index = qobject_cast<QHBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget);                    break;                case LayoutInfo::Grid: {                    m_index = 0;                    while (QLayoutItem *item = m_parentWidget->layout()->itemAt(m_index)) {                        if (item->widget() == m_widget)                            break;                        ++m_index;                    }                    static_cast<QGridLayout*>(m_parentWidget->layout())->getItemPosition(m_index, &m_row, &m_col, &m_rowspan, &m_colspan);                } break;                default:                    break;            } // end switch        }    }    m_formItem = formWindow()->core()->metaDataBase()->item(formWindow());    m_tabOrderIndex = m_formItem->tabOrder().indexOf(widget);    // Build the list of managed children    m_managedChildren.clear();    QList<QWidget *>children = qFindChildren<QWidget *>(m_widget);    foreach (QPointer<QWidget> child, children) {        if (formWindow()->isManaged(child))            m_managedChildren.append(child);    }    setText(QApplication::translate("Command", "Delete '%1'").arg(widget->objectName()));}void DeleteWidgetCommand::redo(){    formWindow()->clearSelection();    QDesignerFormEditorInterface *core = formWindow()->core();    if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_parentWidget)) {        for (int i=0; i<c->count(); ++i) {            if (c->widget(i) == m_widget) {                c->remove(i);                return;            }        }    }    if (QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), m_parentWidget)) {        deco->removeWidget(m_widget);    }    // Unmanage the managed children first    foreach (QWidget *child, m_managedChildren)        formWindow()->unmanageWidget(child);    formWindow()->unmanageWidget(m_widget);    m_widget->setParent(formWindow());    m_widget->hide();    if (m_tabOrderIndex != -1) {        QList<QWidget*> tab_order = m_formItem->tabOrder();        tab_order.removeAt(m_tabOrderIndex);        m_formItem->setTabOrder(tab_order);    }}void DeleteWidgetCommand::undo(){    QDesignerFormEditorInterface *core = formWindow()->core();    formWindow()->clearSelection();        m_widget->setParent(m_parentWidget);    if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_parentWidget)) {        c->addWidget(m_widget);        return;    }    m_widget->setGeometry(m_geometry);    formWindow()->manageWidget(m_widget);    // Manage the managed children    foreach (QWidget *child, m_managedChildren)        formWindow()->manageWidget(child);    // ### set up alignment    switch (m_layoutType) {        case LayoutInfo::VBox: {            QVBoxLayout *vbox = static_cast<QVBoxLayout*>(m_parentWidget->layout());            insert_into_box_layout(vbox, m_index, m_widget);        } break;        case LayoutInfo::HBox: {            QHBoxLayout *hbox = static_cast<QHBoxLayout*>(m_parentWidget->layout());            insert_into_box_layout(hbox, m_index, m_widget);        } break;        case LayoutInfo::Grid: {            QGridLayout *grid = static_cast<QGridLayout*>(m_parentWidget->layout());            add_to_grid_layout(grid, m_widget, m_row, m_col, m_rowspan, m_colspan);        } break;        default:            break;    } // end switch    m_widget->show();    if (m_tabOrderIndex != -1) {        QList<QWidget*> tab_order = m_formItem->tabOrder();        tab_order.insert(m_tabOrderIndex, m_widget);        m_formItem->setTabOrder(tab_order);    }}// ---- ReparentWidgetCommand ----ReparentWidgetCommand::ReparentWidgetCommand(QDesignerFormWindowInterface *formWindow)    : QDesignerFormWindowCommand(QString(), formWindow){}void ReparentWidgetCommand::init(QWidget *widget, QWidget *parentWidget){    Q_ASSERT(widget);    m_widget = widget;    m_oldParentWidget = widget->parentWidget();    m_newParentWidget = parentWidget;    m_oldPos = m_widget->pos();    m_newPos = m_newParentWidget->mapFromGlobal(m_oldParentWidget->mapToGlobal(m_oldPos));    setText(QApplication::translate("Command", "Reparent '%1'").arg(widget->objectName()));    m_oldParentList = qVariantValue<QWidgetList>(m_oldParentWidget->property("_q_widgetOrder"));}void ReparentWidgetCommand::redo(){    m_widget->setParent(m_newParentWidget);    m_widget->move(m_newPos);    QWidgetList oldList = m_oldParentList;    oldList.removeAll(m_widget);    QVariant v;    qVariantSetValue(v, oldList);    m_oldParentWidget->setProperty("_q_widgetOrder", v);    QWidgetList newList = qVariantValue<QWidgetList>(m_newParentWidget->property("_q_widgetOrder"));    newList.append(m_widget);    qVariantSetValue(v, newList);    m_newParentWidget->setProperty("_q_widgetOrder", v);    m_widget->show();}void ReparentWidgetCommand::undo(){    m_widget->setParent(m_oldParentWidget);    m_widget->move(m_oldPos);    QVariant v;    qVariantSetValue(v, m_oldParentList);    m_oldParentWidget->setProperty("_q_widgetOrder", v);

⌨️ 快捷键说明

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