📄 qdesigner_command.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 <QtDesigner/QtDesigner>#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_promotedwidget_p.h"#include "qdesigner_menu_p.h"#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>namespace qdesigner_internal {// ---- QDesignerFormEditorCommand ----QDesignerFormEditorCommand::QDesignerFormEditorCommand(const QString &description, QDesignerFormEditorInterface *core) : QUndoCommand(description), m_core(core){}QDesignerFormEditorInterface *QDesignerFormEditorCommand::core() const{ return m_core;}// ---- QDesignerFormWindowManagerCommand ----QDesignerFormWindowManagerCommand::QDesignerFormWindowManagerCommand(const QString &description, QDesignerFormWindowManagerInterface *formWindowManager) : QUndoCommand(description), m_formWindowManager(formWindowManager){}QDesignerFormWindowManagerInterface *QDesignerFormWindowManagerCommand::formWindowManager() const{ return m_formWindowManager;}// ---- QDesignerFormWindowCommand ----QDesignerFormWindowCommand::QDesignerFormWindowCommand(const QString &description, QDesignerFormWindowInterface *formWindow) : QUndoCommand(description), m_formWindow(formWindow){}QDesignerFormWindowInterface *QDesignerFormWindowCommand::formWindow() const{ return m_formWindow;}QDesignerFormEditorInterface *QDesignerFormWindowCommand::core() const{ if (QDesignerFormWindowInterface *fw = formWindow()) return fw->core(); return 0;}void QDesignerFormWindowCommand::undo(){ cheapUpdate();}void QDesignerFormWindowCommand::redo(){ cheapUpdate();}void QDesignerFormWindowCommand::cheapUpdate(){ if (core()->objectInspector()) core()->objectInspector()->setFormWindow(formWindow()); if (core()->actionEditor()) core()->actionEditor()->setFormWindow(formWindow());}bool QDesignerFormWindowCommand::hasLayout(QWidget *widget) const{ QDesignerFormEditorInterface *core = formWindow()->core(); if (widget && LayoutInfo::layoutType(core, widget) != LayoutInfo::NoLayout) { QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget); return item != 0; } return false;}void QDesignerFormWindowCommand::checkObjectName(QObject *){}void QDesignerFormWindowCommand::updateBuddies(const QString &old_name, const QString &new_name){ QDesignerFormEditorInterface *core = formWindow()->core(); QList<QDesignerLabel*> label_list = qFindChildren<QDesignerLabel*>(formWindow()); foreach (QDesignerLabel *label, label_list) { QDesignerPropertySheetExtension* propertySheet = qt_extension<QDesignerPropertySheetExtension*> (core->extensionManager(), label); if (propertySheet == 0) continue; int idx = propertySheet->indexOf(QLatin1String("buddy")); if (idx == -1) continue; if (propertySheet->property(idx).toString() == old_name) propertySheet->setProperty(idx, new_name); }}void QDesignerFormWindowCommand::checkSelection(QWidget *widget){ Q_UNUSED(widget);}void QDesignerFormWindowCommand::checkParent(QWidget *widget, QWidget *parentWidget){ Q_ASSERT(widget); if (widget->parentWidget() != parentWidget) widget->setParent(parentWidget);}// ---- SetPropertyCommand ----SetPropertyCommand::SetPropertyCommand(QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(QString(), formWindow), m_index(-1), m_propertySheet(0), m_changed(false){}QObject *SetPropertyCommand::object() const{ return m_object;}QWidget *SetPropertyCommand::widget() const{ return qobject_cast<QWidget*>(m_object);}QWidget *SetPropertyCommand::parentWidget() const{ if (QWidget *w = widget()) { return w->parentWidget(); } return 0;}void SetPropertyCommand::init(QObject *object, const QString &propertyName, const QVariant &newValue){ Q_ASSERT(object); m_object = object; m_parentWidget = parentWidget(); m_propertyName = propertyName; m_newValue = newValue; QDesignerFormEditorInterface *core = formWindow()->core(); m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), object); Q_ASSERT(m_propertySheet); m_index = m_propertySheet->indexOf(m_propertyName); Q_ASSERT(m_index != -1); m_changed = m_propertySheet->isChanged(m_index); m_oldValue = m_propertySheet->property(m_index); setText(QApplication::translate("Command", "changed '%1' of '%2'").arg(m_propertyName).arg(object->objectName()));}static QWidget *containerWindow(QWidget *widget){ while (widget) { if (widget->metaObject()->className() == QLatin1String("QDesignerFormWindow")) return widget; widget = widget->parentWidget(); } return 0;}static QSize checkSize(const QSize &size){ QSize s = size; if (s.width() > 0xFFFFFF) s.setWidth(0xFFFFFF); if (s.height() > 0xFFFFFF) s.setHeight(0xFFFFFF); return s;}static void setTopMinMaxSize(QDesignerFormWindowInterface *fw, QWidget *w, const QString &propertyName, const QVariant &value){ QDesignerFormWindowCursorInterface *cursor = fw->cursor(); if (w && cursor->isWidgetSelected(w)) { if (cursor->isWidgetSelected(fw->mainContainer())) { if (propertyName == QLatin1String("minimumSize")) { if (QWidget *container = containerWindow(fw)) { if (container->parentWidget() && container->parentWidget()->metaObject()->className() == QLatin1String("QWorkspaceChild")) { QSize diff = container->parentWidget()->geometry().size() - container->geometry().size(); container->parentWidget()->setMinimumSize(checkSize(value.toSize() + diff)); } container->setMinimumSize(value.toSize()); } } else if (propertyName == QLatin1String("maximumSize")) { if (QWidget *container = containerWindow(fw)) { if (container->parentWidget() && container->parentWidget()->metaObject()->className() == QLatin1String("QWorkspaceChild")) { QSize diff = container->parentWidget()->geometry().size() - container->geometry().size(); container->parentWidget()->setMaximumSize(checkSize(value.toSize() + diff)); } container->setMaximumSize(value.toSize()); } } } }}void SetPropertyCommand::redo(){ Q_ASSERT(m_propertySheet); Q_ASSERT(m_index != -1); m_propertySheet->setProperty(m_index, m_newValue); m_changed = m_propertySheet->isChanged(m_index); m_propertySheet->setChanged(m_index, true); if (m_propertyName == QLatin1String("geometry") && widget()) { checkSelection(widget()); checkParent(widget(), parentWidget()); } else if (m_propertyName == QLatin1String("objectName")) { checkObjectName(m_object); updateBuddies(m_oldValue.toString(), m_newValue.toString()); } if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) { if (propertyEditor->object() == object()) propertyEditor->setPropertyValue(propertyName(), m_newValue, true); else propertyEditor->setObject(propertyEditor->object()); // this is needed when f.ex. undo // changes parent's palette, but // the child is the active widget. } setTopMinMaxSize(formWindow(), qobject_cast<QWidget *>(m_object), m_propertyName, m_newValue); QAction *act = qobject_cast<QAction *>(m_object); if (m_propertyName == QLatin1String("objectName") || m_propertyName == QLatin1String("icon") && act || m_propertyName == QLatin1String("currentTabName")) { if (QDesignerObjectInspectorInterface *oi = formWindow()->core()->objectInspector()) oi->setFormWindow(formWindow()); } if (m_propertyName == QLatin1String("objectName") && act) { // emit act->changed(); cannot emit, signal is protected act->setData(QVariant(true)); // it triggers signal "changed" in QAction act->setData(QVariant(false)); } if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(m_object)) { if (m_propertyName == QLatin1String("minimumSize")) promoted->setMinimumSize(m_newValue.toSize()); else if (m_propertyName == QLatin1String("maximumSize")) promoted->setMaximumSize(m_newValue.toSize()); }}void SetPropertyCommand::undo(){ Q_ASSERT(m_propertySheet); Q_ASSERT(m_index != -1); m_propertySheet->setProperty(m_index, m_oldValue); m_propertySheet->setChanged(m_index, m_changed); if (m_propertyName == QLatin1String("geometry") && widget()) { checkSelection(widget()); checkParent(widget(), parentWidget()); } else if (m_propertyName == QLatin1String("objectName")) { checkObjectName(m_object); updateBuddies(m_newValue.toString(), m_oldValue.toString()); } if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) { if (propertyEditor->object() == widget()) propertyEditor->setPropertyValue(propertyName(), m_oldValue, m_changed); else propertyEditor->setObject(propertyEditor->object()); // this is needed when f.ex. undo // changes parent's palette, but // the child is the active widget. } setTopMinMaxSize(formWindow(), qobject_cast<QWidget *>(m_object), m_propertyName, m_oldValue); QAction *act = qobject_cast<QAction *>(m_object); if (m_propertyName == QLatin1String("objectName") || m_propertyName == QLatin1String("icon") && act || m_propertyName == QLatin1String("currentTabName")) { if (QDesignerObjectInspectorInterface *oi = formWindow()->core()->objectInspector()) oi->setFormWindow(formWindow()); } if (m_propertyName == QLatin1String("objectName") && act) { // emit act->changed(); cannot emit, signal is protected act->setData(QVariant(true)); // it triggers signal "changed" in QAction act->setData(QVariant(false)); } if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(m_object)) { if (m_propertyName == QLatin1String("minimumSize")) promoted->setMinimumSize(m_oldValue.toSize()); else if (m_propertyName == QLatin1String("maximumSize")) promoted->setMaximumSize(m_oldValue.toSize()); }}int SetPropertyCommand::id() const{ return 1976;}bool SetPropertyCommand::mergeWith(const QUndoCommand *other){ if (id() != other->id()) return false; if (const SetPropertyCommand *cmd = static_cast<const SetPropertyCommand*>(other)) { if (cmd->propertyName() == propertyName() && cmd->object() == object()) { if (!formWindow()->isDirty()) return false; m_newValue = cmd->newValue(); return true; } } return false;}// ---- SetFormPropertyCommand ----SetFormPropertyCommand::SetFormPropertyCommand(QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(QString(), formWindow), m_index(-1), m_changed(false), m_propertySheet(0){}void SetFormPropertyCommand::init(QObject *object, const QString &propertyName, const QVariant &newValue){ Q_ASSERT(object); m_newValue = newValue; m_propertyName = propertyName; QDesignerFormEditorInterface *core = formWindow()->core(); m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), object); Q_ASSERT(m_propertySheet); m_index = m_propertySheet->indexOf(m_propertyName); Q_ASSERT(m_index != -1); m_changed = m_propertySheet->isChanged(m_index); m_oldValue = m_propertySheet->property(m_index); setText(QApplication::translate("Command", "changed '%1' of '%2'").arg(m_propertyName).arg(object->objectName()));}void SetFormPropertyCommand::redo(){ m_changed = m_propertySheet->isChanged(m_index); m_propertySheet->setChanged(m_index, true); if (m_propertyName == QLatin1String("geometry")) updateFormWindowGeometry(m_newValue);}void SetFormPropertyCommand::undo(){ m_propertySheet->setChanged(m_index, m_changed); if (m_propertyName == QLatin1String("geometry")) updateFormWindowGeometry(m_oldValue);}int SetFormPropertyCommand::id() const{ return 1977;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -