formwindow.cpp

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

CPP
2,326
字号
    } else {        QMessageBox::information(this, tr("Paste error"),                                  tr("Can't paste widgets. Designer couldn't find a container\n"                                      "to paste into which does not contain a layout. Break the layout\n"                                      "of the container you want to paste into and select this container\n"                                      "and then paste again."));    }}// Draw a dotted frame around containersbool FormWindow::frameNeeded(QWidget *w) const{    if (!core()->widgetDataBase()->isContainer(w))        return false;    if (qobject_cast<QGroupBox *>(w))        return false;    if (qobject_cast<QToolBox *>(w))        return false;    if (qobject_cast<QTabWidget *>(w))        return false;    if (qobject_cast<QStackedWidget *>(w))        return false;    if (qobject_cast<QDockWidget *>(w))        return false;    if (qobject_cast<QDesignerWidget *>(w))        return false;    if (qobject_cast<QMainWindow *>(w))        return false;    if (qobject_cast<QDialog *>(w))        return false;    if (qobject_cast<QLayoutWidget *>(w))        return false;    return true;}bool FormWindow::eventFilter(QObject *watched, QEvent *event){    const bool ret = FormWindowBase::eventFilter(watched, event);    if (event->type() != QEvent::Paint)        return ret;    Q_ASSERT(watched->isWidgetType());    QWidget *w = static_cast<QWidget *>(watched);    QPaintEvent *pe = static_cast<QPaintEvent*>(event);    const QRect widgetRect = w->rect();    const QRect paintRect =  pe->rect();    // Does the paint rectangle touch the borders of the widget rectangle    if (paintRect.x()     > widgetRect.x()     && paintRect.y()      > widgetRect.y() &&        paintRect.right() < widgetRect.right() && paintRect.bottom() < paintRect.bottom())        return ret;    QPainter p(w);    const QPen pen(QColor(0, 0, 0, 32), 0, Qt::DotLine);    p.setPen(pen);    p.setBrush(QBrush(Qt::NoBrush));    p.drawRect(widgetRect.adjusted(0, 0, -1, -1));    return ret;}void FormWindow::manageWidget(QWidget *w){    if (isManaged(w))        return;    Q_ASSERT(qobject_cast<QMenu*>(w) == 0);    if (w->hasFocus())        setFocus();    core()->metaDataBase()->add(w);    m_insertedWidgets.insert(w);    m_widgets.append(w);    setCursorToAll(Qt::ArrowCursor, w);    emit changed();    emit widgetManaged(w);    if (frameNeeded(w))        w->installEventFilter(this);}void FormWindow::unmanageWidget(QWidget *w){    if (!isManaged(w))        return;    m_selection->removeWidget(w);    emit aboutToUnmanageWidget(w);    if (w == m_currentWidget)        setCurrentWidget(mainContainer());    core()->metaDataBase()->remove(w);    m_insertedWidgets.remove(w);    m_widgets.removeAt(m_widgets.indexOf(w));    emit changed();    emit widgetUnmanaged(w);    if (frameNeeded(w))        w->removeEventFilter(this);}bool FormWindow::isManaged(QWidget *w) const{    return m_insertedWidgets.contains(w);}void FormWindow::breakLayout(QWidget *w){    if (w == this)        w = mainContainer();    w = core()->widgetFactory()->containerOfWidget(w);    beginCommand(tr("Break layout"));    for (;;) {        if (!w || w == this)            break;        if (LayoutInfo::layoutType(m_core, core()->widgetFactory()->containerOfWidget(w)) != LayoutInfo::NoLayout                && core()->widgetDataBase()->isContainer(w, false)) {            if (BreakLayoutCommand *cmd = breakLayoutCommand(w)) {                commandHistory()->push(cmd);                break;            }        }        w = w->parentWidget();    }    clearSelection(false);    endCommand();}BreakLayoutCommand *FormWindow::breakLayoutCommand(QWidget *w){    QWidgetList widgets;    QListIterator<QObject*> it(w->children());    while (it.hasNext()) {        QObject *obj = it.next();        if (!obj->isWidgetType()                || !core()->metaDataBase()->item(obj))            continue;        widgets.append(static_cast<QWidget*>(obj));    }    BreakLayoutCommand *cmd = new BreakLayoutCommand(this);    cmd->init(widgets, core()->widgetFactory()->widgetOfContainer(w));    return cmd;}void FormWindow::beginCommand(const QString &description){    m_commandHistory->beginMacro(description);}void FormWindow::endCommand(){    m_commandHistory->endMacro();}void FormWindow::raiseWidgets(){    QWidgetList widgets = selectedWidgets();    simplifySelection(&widgets);    foreach (QWidget *widget, widgets) {        widget->raise();    }}void FormWindow::lowerWidgets(){    QWidgetList widgets = selectedWidgets();    simplifySelection(&widgets);    foreach (QWidget *widget, widgets) {        widget->lower();    }}bool FormWindow::handleMouseButtonDblClickEvent(QWidget *, QWidget *managedWidget, QMouseEvent *e){    e->accept();    emit activated(managedWidget);    m_dblClicked = true;    return true;}QMenu *FormWindow::initializePopupMenu(QWidget *managedWidget){    if (!isManaged(managedWidget) || currentTool())        return 0;    // Make sure the managedWidget is selected and current since    // the SetPropertyCommands must use the right reference    // object obtained from the property editor for the property group    // of a multiselection to be correct.    const bool selected = isWidgetSelected(managedWidget);    bool update = false;    if (selected == false) {        clearSelection(false);        update = trySelectWidget(managedWidget, true);        raiseChildSelections(managedWidget); // raise selections and select widget    } else {        update = setCurrentWidget(managedWidget);    }    if (update) {        emitSelectionChanged();        QMetaObject::invokeMethod(core()->formWindowManager(), "slotUpdateActions");    }    QWidget *contextMenuWidget = 0;    if (isMainContainer(managedWidget)) { // press on a child widget        contextMenuWidget = mainContainer();    } else {  // press on a child widget        // if widget is laid out, find the first non-laid out super-widget        QWidget *realWidget = managedWidget; // but store the original one        QMainWindow *mw = qobject_cast<QMainWindow*>(mainContainer());        if (mw && mw->centralWidget() == realWidget) {            contextMenuWidget = managedWidget;        } else {            contextMenuWidget = realWidget;        }    }    if (!contextMenuWidget)        return 0;    QMenu *contextMenu = createPopupMenu(contextMenuWidget);    if (!contextMenu)        return 0;    emit contextMenuRequested(contextMenu, contextMenuWidget);    return contextMenu;}bool FormWindow::handleContextMenu(QWidget *, QWidget *managedWidget, QContextMenuEvent *e){    QMenu *contextMenu = initializePopupMenu(managedWidget);    if (!contextMenu)        return false;    contextMenu->exec(e->globalPos());    delete contextMenu;    e->accept();    return true;}void FormWindow::setContents(QIODevice *dev){    const bool saved = updatesEnabled();    setUpdatesEnabled(false);    clearSelection();    m_selection->clearSelectionPool();    m_insertedWidgets.clear();    m_widgets.clear();    // The main container is cleared as otherwise    // the names of the newly loaded objects will be unified.    clearMainContainer();    emit changed();    QDesignerResource r(this);    QWidget *w = r.load(dev, this);    setMainContainer(w);    emit changed();    setUpdatesEnabled(saved);}void FormWindow::setContents(const QString &contents){    QByteArray data = contents.toUtf8();    QBuffer b(&data);    if (b.open(QIODevice::ReadOnly))        setContents(&b);}void FormWindow::layoutHorizontalContainer(QWidget *w){    if (w == this)        w = mainContainer();    w = core()->widgetFactory()->containerOfWidget(w);    QList<QObject*> l = w->children();    if (l.isEmpty())        return;    QWidgetList widgets;    QListIterator<QObject*> it(l);    while (it.hasNext()) {        QObject* o = it.next();        if (!o->isWidgetType())            continue;        QWidget *widget = static_cast<QWidget*>(o);        if (widget->isVisibleTo(this) && isManaged(widget))            widgets.append(widget);    }    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), widgets, LayoutInfo::HBox, w);    clearSelection(false);    commandHistory()->push(cmd);}void FormWindow::layoutVerticalContainer(QWidget *w){    if (w == this)        w = mainContainer();    w = core()->widgetFactory()->containerOfWidget(w);    const QList<QObject*> l = w->children();    if (l.isEmpty())        return;    QListIterator<QObject*> it(l);    QWidgetList widgets;    while (it.hasNext()) {        QObject* o = it.next();        if (!o->isWidgetType())            continue;        QWidget *widget = static_cast<QWidget*>(o);        if (widget->isVisibleTo(this) && isManaged(widget))            widgets.append(widget);    }    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), widgets, LayoutInfo::VBox, w);    clearSelection(false);    commandHistory()->push(cmd);}void FormWindow::layoutGridContainer(QWidget *w){    if (w == this)        w = mainContainer();    w = core()->widgetFactory()->containerOfWidget(w);    const QList<QObject*> l = w->children();    if (l.isEmpty())        return;    QWidgetList widgets;    QListIterator<QObject*> it(l);    while (it.hasNext()) {        QObject* o = it.next();        if (!o->isWidgetType())            continue;        QWidget *widget = static_cast<QWidget*>(o);        if (widget->isVisibleTo(this) && isManaged(widget))            widgets.append(widget);    }    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), widgets, LayoutInfo::Grid, w);    clearSelection(false);    commandHistory()->push(cmd);}bool FormWindow::hasInsertedChildren(QWidget *widget) const // ### move{    if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {        widget = container->widget(container->currentIndex());    }    const QWidgetList l = widgets(widget);    foreach (QWidget *child, l) {        if (isManaged(child) && !LayoutInfo::isWidgetLaidout(core(), child) && child->isVisibleTo(const_cast<FormWindow*>(this)))            return true;    }    return false;}void FormWindow::layoutHorizontalSplit(){    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), selectedWidgets(), LayoutInfo::HBox, /*layoutBase=*/ 0, /*splitter=*/ true);    clearSelection(false);    commandHistory()->push(cmd);}void FormWindow::layoutVerticalSplit(){    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), selectedWidgets(), LayoutInfo::VBox, /*layoutBase=*/ 0, /*splitter=*/ true);    clearSelection(false);    commandHistory()->push(cmd);}QMenu *FormWindow::createPopupMenu(QWidget *w){    QMenu *popup = new QMenu;    // Query extension    if (const QDesignerTaskMenuExtension *taskMenu = qt_extension<QDesignerTaskMenuExtension*>(core()->extensionManager(), w)) {        const QList<QAction *> acts = taskMenu->taskActions();        if (!acts.empty()) {            popup->addActions( acts);            popup->addSeparator();        }    }    QDesignerFormWindowManagerInterface *manager = core()->formWindowManager();    const bool isFormWindow = qobject_cast<const FormWindow*>(w);    // Check for special containers and obtain the page menu from them to add layout actions.    if (!isFormWindow) {        if (QDesignerStackedWidget *stackedWidget  = qobject_cast<QDesignerStackedWidget*>(w)) {            stackedWidget->addContextMenuActions(popup);        } else {            if (QDesignerTabWidget *tabWidget = qobject_cast<QDesignerTabWidget*>(w)) {                tabWidget->addContextMenuActions(popup);            }  else {                if (QDesignerToolBox *toolBox = qobject_cast<QDesignerToolBox*>(w)) {                    toolBox->addContextMenuActions(popup);                }            }        }        popup->addAction(manager->actionCut());        popup->addAction(manager->actionCopy());    }    popup->addAction(manager->actionPaste());    popup->addAction(manager->actionSelectAll());    if (!isFormWindow) {        popup->addAction(manager->actionDelete());    }    popup->addSeparator();    QMenu *layoutMenu = popup->addMenu(tr("Lay out"));    layoutMenu->addAction(manager->actionAdjustSize());    layoutMenu->addAction(manager->actionHorizontalLayout());    layoutMenu->addAction(manager->actionVerticalLayout());    layoutMenu->addAction(manager->actionGridLayout());    if (!isFormWindow) {        layoutMenu->addAction(manager->actionSplitHorizontal());        layoutMenu->addAction(manager->actionSplitVertical());    }

⌨️ 快捷键说明

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