📄 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) : QtCommand(description), m_core(core){}QDesignerFormEditorInterface *QDesignerFormEditorCommand::core() const{ return m_core;}// ---- QDesignerFormWindowManagerCommand ----QDesignerFormWindowManagerCommand::QDesignerFormWindowManagerCommand(const QString &description, QDesignerFormWindowManagerInterface *formWindowManager) : QtCommand(description), m_formWindowManager(formWindowManager){}QDesignerFormWindowManagerInterface *QDesignerFormWindowManagerCommand::formWindowManager() const{ return m_formWindowManager;}// ---- QDesignerFormWindowCommand ----QDesignerFormWindowCommand::QDesignerFormWindowCommand(const QString &description, QDesignerFormWindowInterface *formWindow) : QtCommand(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){ setCanMerge(true);}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); setDescription(tr("changed '%1' of '%2'").arg(m_propertyName).arg(object->objectName()));}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. } if (m_propertyName == QLatin1String("objectName") || m_propertyName == QLatin1String("icon") && qobject_cast<QAction *>(m_object)) { if (QDesignerObjectInspectorInterface *oi = formWindow()->core()->objectInspector()) oi->setFormWindow(formWindow()); }}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. } if (m_propertyName == QLatin1String("objectName") || m_propertyName == QLatin1String("icon") && qobject_cast<QAction *>(m_object)) { if (QDesignerObjectInspectorInterface *oi = formWindow()->core()->objectInspector()) oi->setFormWindow(formWindow()); }}bool SetPropertyCommand::mergeMeWith(QtCommand *other){ if (SetPropertyCommand *cmd = qobject_cast<SetPropertyCommand*>(other)) { if (cmd->propertyName() == propertyName() && cmd->widget() == widget()) { m_newValue = cmd->newValue(); return true; } } return false;}// ---- ResetPropertyCommand ----ResetPropertyCommand::ResetPropertyCommand(QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(QString(), formWindow), m_index(-1), m_propertySheet(0), m_changed(false){ setCanMerge(true);}QWidget *ResetPropertyCommand::widget() const{ return m_widget;}QWidget *ResetPropertyCommand::parentWidget() const{ return m_parentWidget;}void ResetPropertyCommand::init(QWidget *widget, const QString &propertyName){ Q_ASSERT(widget); m_widget = widget; m_parentWidget = widget->parentWidget(); m_propertyName = propertyName; QDesignerFormEditorInterface *core = formWindow()->core(); m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget); 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); setDescription(tr("reset '%1' of '%2'").arg(m_propertyName).arg(m_widget->objectName()));}void ResetPropertyCommand::redo(){ Q_ASSERT(m_propertySheet); Q_ASSERT(m_index != -1); QObject *obj = m_widget; if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(obj)) obj = promoted->child(); QVariant new_value; if (m_propertySheet->reset(m_index)) { new_value = m_propertySheet->property(m_index); } else { int item_idx = formWindow()->core()->widgetDataBase()->indexOfObject(obj); if (item_idx == -1) { new_value = m_oldValue; // We simply don't know the value in this case } else { QDesignerWidgetDataBaseItemInterface *item = formWindow()->core()->widgetDataBase()->item(item_idx); QList<QVariant> default_prop_values = item->defaultPropertyValues(); if (m_index < default_prop_values.size()) new_value = default_prop_values.at(m_index); else new_value = m_oldValue; // Again, we just don't know } m_propertySheet->setProperty(m_index, new_value); } m_propertySheet->setChanged(m_index, false); if (m_propertyName == QLatin1String("geometry")) { checkSelection(m_widget); checkParent(m_widget, m_parentWidget); } else if (m_propertyName == QLatin1String("objectName")) { checkObjectName(m_widget); } if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) { if (propertyEditor->object() == widget()) propertyEditor->setPropertyValue(propertyName(), new_value, false); }}void ResetPropertyCommand::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")) { checkSelection(m_widget); checkParent(m_widget, m_parentWidget); } else if (m_propertyName == QLatin1String("objectName")) { checkObjectName(m_widget); } if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) { if (propertyEditor->object() == widget()) propertyEditor->setPropertyValue(propertyName(), m_oldValue, m_changed); }}// ---- InsertWidgetCommand ----InsertWidgetCommand::InsertWidgetCommand(QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(QString(), formWindow){}void InsertWidgetCommand::init(QWidget *widget, bool already_in_form){ m_widget = widget; setDescription(tr("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;}static void recursiveUpdate(QWidget *w){ w->update(); const QObjectList &l = w->children(); QObjectList::const_iterator it = l.begin(); for (; it != l.end(); ++it) { if (QWidget *w = qobject_cast<QWidget*>(*it)) recursiveUpdate(w); }}void InsertWidgetCommand::redo(){ checkObjectName(m_widget); QWidget *parentWidget = m_widget->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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -