📄 qdesigner_command.cpp
字号:
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(); }}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();}// ---- RaiseWidgetCommand ----RaiseWidgetCommand::RaiseWidgetCommand(QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(QString(), formWindow){}void RaiseWidgetCommand::init(QWidget *widget){ m_widget = widget; setDescription(tr("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; setDescription(tr("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); switch (m_layoutType) { case LayoutInfo::VBox: m_index = static_cast<QVBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget); break; case LayoutInfo::HBox: m_index = static_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 = QList<QPointer<QWidget> >(); QList<QWidget *>children = qFindChildren<QWidget *>(m_widget); foreach (QPointer<QWidget> child, children) { if (formWindow()->isManaged(child)) m_managedChildren.append(child); } setDescription(tr("Delete '%1'").arg(widget->objectName()));}void DeleteWidgetCommand::redo(){ 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); formWindow()->emitSelectionChanged(); 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); } formWindow()->emitSelectionChanged();}void DeleteWidgetCommand::undo(){ QDesignerFormEditorInterface *core = formWindow()->core(); m_widget->setParent(m_parentWidget); if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_parentWidget)) { c->addWidget(m_widget); formWindow()->emitSelectionChanged(); 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); } formWindow()->emitSelectionChanged();}// ---- 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)); setDescription(tr("Reparent '%1'").arg(widget->objectName()));}void ReparentWidgetCommand::redo(){ m_widget->setParent(m_newParentWidget); m_widget->move(m_newPos); m_widget->show();}void ReparentWidgetCommand::undo(){ m_widget->setParent(m_oldParentWidget); m_widget->move(m_oldPos); m_widget->show();}// ---- PromoteToCustomWidgetCommand ----static void replace_widget_item(QDesignerFormWindowInterface *fw, QWidget *wgt, QWidget *promoted){ QDesignerFormEditorInterface *core = fw->core(); QWidget *parent = wgt->parentWidget(); QRect info; int splitter_idx = -1; if (QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parent)) { if (QSplitter *splitter = qobject_cast<QSplitter*>(parent)) { splitter_idx = splitter->indexOf(wgt); Q_ASSERT(splitter_idx != -1); wgt->setParent(0); } else { QLayout *layout = LayoutInfo::managedLayout(core, parent); Q_ASSERT(layout != 0); int old_index = layout->indexOf(wgt); Q_ASSERT(old_index != -1); info = deco->itemInfo(old_index); QLayoutItem *item = layout->takeAt(old_index); delete item; layout->activate(); } } if (qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parent)) { if (QSplitter *splitter = qobject_cast<QSplitter*>(parent)) { splitter->insertWidget(splitter_idx, promoted); } else { QLayout *layout = LayoutInfo::managedLayout(core, parent); Q_ASSERT(layout != 0); // ### check if `info' is valid! switch (LayoutInfo::layoutType(core, layout)) { default: Q_ASSERT(0); break; case LayoutInfo::VBox: insert_into_box_layout(static_cast<QBoxLayout*>(layout), info.top(), promoted); break; case LayoutInfo::HBox: insert_into_box_layout(static_cast<QBoxLayout*>(layout), info.left(), promoted); break; case LayoutInfo::Grid: add_to_grid_layout(static_cast<QGridLayout*>(layout), promoted, info.top(), info.left(), info.height(), info.width()); break; } } }}PromoteToCustomWidgetCommand::PromoteToCustomWidgetCommand (QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(tr("Promote to custom widget"), formWindow){ m_widget = 0; m_promoted = 0;}void PromoteToCustomWidgetCommand::init(QDesignerWidgetDataBaseItemInterface *item, QWidget *widget){ m_widget = widget; m_promoted = new QDesignerPromotedWidget(item, widget->parentWidget());}void PromoteToCustomWidgetCommand::redo(){ m_promoted->setObjectName(QLatin1String("__qt__promoted_") + m_widget->objectName()); m_promoted->setGeometry(m_widget->geometry()); replace_widget_item(formWindow(), m_widget, m_promoted); m_promoted->setChildWidget(m_widget); formWindow()->manageWidget(m_promoted); formWindow()->clearSelection(); formWindow()->selectWidget(m_promoted); m_promoted->show();}void PromoteToCustomWidgetCommand::undo(){ m_promoted->setChildWidget(0); m_widget->setParent(m_promoted->parentWidget()); m_widget->setGeometry(m_promoted->geometry()); replace_widget_item(formWindow(), m_promoted, m_widget); formWindow()->manageWidget(m_widget); formWindow()->unmanageWidget(m_promoted); m_promoted->hide(); m_widget->show(); formWindow()->clearSelection(); formWindow()->selectWidget(m_promoted);}// ---- DemoteFromCustomWidgetCommand ----DemoteFromCustomWidgetCommand::DemoteFromCustomWidgetCommand (QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(tr("Demote from custom widget"), formWindow){ m_promote_cmd = new PromoteToCustomWidgetCommand(formWindow);}void DemoteFromCustomWidgetCommand::init(QDesignerPromotedWidget *promoted){ m_promote_cmd->m_widget = promoted->child(); m_promote_cmd->m_promoted = promoted;}void DemoteFromCustomWidgetCommand::redo(){ m_promote_cmd->undo(); m_promote_cmd->m_widget->show();}void DemoteFromCustomWidgetCommand::undo(){ m_promote_cmd->redo();}// ---- LayoutCommand ----LayoutCommand::LayoutCommand(QDesignerFormWindowInterface *formWindow) : QDesignerFormWindowCommand(QString(), formWindow){}LayoutCommand::~LayoutCommand(){ m_layout->deleteLater();}void LayoutCommand::init(QWidget *parentWidget, const QList<QWidget*> &widgets, LayoutInfo::Type layoutType, QWidget *layoutBase, bool splitter){ m_parentWidget = parentWidget; m_widgets = widgets; formWindow()->simplifySelection(&m_widgets); QPoint grid = formWindow()->grid(); QSize sz(qMax(5, grid.x()), qMax(5, grid.y())); switch (layoutType) { case LayoutInfo::Grid: m_layout = new GridLayout(widgets, m_parentWidget, formWindow(), layoutBase, sz); setDescription(tr("Lay out using grid")); break; case LayoutInfo::VBox: m_layout = new VerticalLayout(widgets, m_parentWidget, formWindow(), layoutBase, splitter); setDescription(tr("Lay out vertically")); break; case LayoutInfo::HBox: m_layout = new HorizontalLayout(widgets, m_parentWidget, formWindow(), layoutBase, splitter); setDescription(tr("Lay out horizontally")); break; default: Q_ASSERT(0); } m_layout->setup();}void LayoutCommand::redo(){ m_layout->doLayout(); checkSelection(m_parentWidget);}void LayoutCommand::undo(){ QDesignerFormEditorInterface *core = formWindow()->core(); QWidget *lb = m_layout->layoutBaseWidget(); QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), lb); QWidget *p = m_layout->parentWidget(); if (!deco && hasLayout(p)) { deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), p); } m_layout->undoLayout(); delete deco; // release the extension // ### generalize (put in function) if (!m_layoutBase && lb != 0 && !(qobject_cast<QLayoutWidget*>(lb) || qobject_cast<QSplitter*>(lb))) { core->metaDataBase()->add(lb);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -