qdesigner_menu.cpp

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

CPP
1,357
字号
    qVariantSetValue(itemData, action);    QAction *addSeparatorAction = menu.addAction(tr("Insert separator"));    addSeparatorAction->setData(itemData);    QAction *removeAction = 0;    if (action->isSeparator())        removeAction = menu.addAction(tr("Remove separator"));    else        removeAction = menu.addAction(tr("Remove action '%1'").arg(action->objectName()));    removeAction->setData(itemData);    connect(addSeparatorAction, SIGNAL(triggered(bool)), this, SLOT(slotAddSeparator()));    connect(removeAction, SIGNAL(triggered(bool)), this, SLOT(slotRemoveSelectedAction()));    menu.exec(event->globalPos());    return true;}void QDesignerMenu::slotAddSeparator(){    QAction *action = qobject_cast<QAction *>(sender());    if (!action)        return;    QAction *a = qvariant_cast<QAction*>(action->data());    Q_ASSERT(a != 0);    const int pos = actions().indexOf(a);    QAction *action_before = 0;    if (pos != -1)        action_before = safeActionAt(pos);    QDesignerFormWindowInterface *fw = formWindow();    fw->beginCommand(tr("Add separator"));    QAction *sep = createAction(QString(), true);    InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw);    cmd->init(this, sep, action_before);    fw->commandHistory()->push(cmd);    if (parentMenu()) {        QAction *parent_action = parentMenu()->currentAction();        if (parent_action->menu() == 0) {            CreateSubmenuCommand *cmd = new CreateSubmenuCommand(fw);            cmd->init(parentMenu(), parentMenu()->currentAction());            fw->commandHistory()->push(cmd);        }    }    fw->endCommand();}void QDesignerMenu::slotRemoveSelectedAction(){    QAction *action = qobject_cast<QAction *>(sender());    if (!action)        return;    QAction *a = qvariant_cast<QAction*>(action->data());    Q_ASSERT(a != 0);    const int pos = actions().indexOf(a);    QAction *action_before = 0;    if (pos != -1)        action_before = safeActionAt(pos + 1);    QDesignerFormWindowInterface *fw = formWindow();    RemoveActionFromCommand *cmd = new RemoveActionFromCommand(fw);    cmd->init(this, a, action_before);    fw->commandHistory()->push(cmd);}QRect QDesignerMenu::subMenuPixmapRect(QAction *action) const{    static const QPixmap pm(QLatin1String(":/trolltech/formeditor/images/submenu.png"));    const QRect g = actionGeometry(action);    const int x = g.right() - pm.width() - 2;    const int y = g.top() + (g.height() - pm.height())/2 + 1;    return QRect(x, y, pm.width(), pm.height());}bool QDesignerMenu::hasSubMenuPixmap(QAction *action) const{    return action != 0            && qobject_cast<SpecialMenuAction*>(action) == 0            && !action->isSeparator()            && !action->menu()            && canCreateSubMenu(action);}void QDesignerMenu::showEvent ( QShowEvent * event ){    selectCurrentAction();    QMenu::showEvent (event);}void QDesignerMenu::paintEvent(QPaintEvent *event){    QMenu::paintEvent(event);    QPainter p(this);    QAction *current = currentAction();    foreach (QAction *a, actions()) {        const QRect g = actionGeometry(a);        if (qobject_cast<SpecialMenuAction*>(a)) {            QLinearGradient lg(g.left(), g.top(), g.left(), g.bottom());            lg.setColorAt(0.0, Qt::transparent);            lg.setColorAt(0.7, QColor(0, 0, 0, 32));            lg.setColorAt(1.0, Qt::transparent);            p.fillRect(g, lg);        } else if (hasSubMenuPixmap(a)) {            static const QPixmap pm(QLatin1String(":/trolltech/formeditor/images/submenu.png"));            p.drawPixmap(subMenuPixmapRect(a).topLeft(), pm);        }    }    if (!hasFocus() || !current || m_dragging)        return;    if (QDesignerMenu *menu = parentMenu()) {        if (menu->dragging())            return;    }    if (QDesignerMenuBar *menubar = qobject_cast<QDesignerMenuBar*>(parentWidget())) {        if (menubar->dragging())            return;    }    const QRect g = actionGeometry(current);    drawSelection(&p, g.adjusted(1, 1, -3, -3));}bool QDesignerMenu::dragging() const{    return m_dragging;}QDesignerMenu *QDesignerMenu::findRootMenu() const{    if (parentMenu())        return parentMenu()->findRootMenu();    return const_cast<QDesignerMenu*>(this);}QDesignerMenu *QDesignerMenu::findActivatedMenu() const{    QList<QDesignerMenu*> candidates;    candidates.append(const_cast<QDesignerMenu*>(this));    candidates += qFindChildren<QDesignerMenu*>(this);    foreach (QDesignerMenu *m, candidates) {        if (m == qApp->activeWindow())            return m;    }    return 0;}bool QDesignerMenu::eventFilter(QObject *object, QEvent *event){    if (object != this && object != m_editor) {        return false;    }    if (!m_editor->isHidden() && object == m_editor && event->type() == QEvent::FocusOut) {        leaveEditMode(Default);        m_editor->hide();        update();        return false;    }    bool dispatch = true;    switch (event->type()) {        default: break;        case QEvent::WindowDeactivate:            deactivateMenu();            break;        case QEvent::ContextMenu:        case QEvent::MouseButtonPress:        case QEvent::MouseButtonRelease:        case QEvent::MouseButtonDblClick:            while (QApplication::activePopupWidget() && !qobject_cast<QDesignerMenu*>(QApplication::activePopupWidget())) {                QApplication::activePopupWidget()->close();            }        // fall through        case QEvent::KeyPress:        case QEvent::KeyRelease:        case QEvent::MouseMove:            dispatch = (object != m_editor);            // no break        case QEvent::Enter:        case QEvent::Leave:        case QEvent::FocusIn:        case QEvent::FocusOut:        if (dispatch)            if (QWidget *widget = qobject_cast<QWidget*>(object))                if (widget == this || isAncestorOf(widget))                    return handleEvent(widget, event);        break;    }    return false;};int QDesignerMenu::actionAtPosition(const QPoint &pos) const{    for (int i = 0; i<actions().count(); ++i) {        QRect g = actionGeometry(safeActionAt(i));        g.setTopLeft(QPoint(0, 0));        if (g.contains(pos))            return i;    }    return -1;}int QDesignerMenu::findAction(const QPoint &pos) const{    const int index = actionAtPosition(pos);    if (index == -1)        return realActionCount();    return index;}void QDesignerMenu::adjustIndicator(const QPoint &pos){    if (QDesignerActionProviderExtension *a = actionProvider()) {        a->adjustIndicator(pos);    }}QDesignerMenu::ActionDragCheck QDesignerMenu::checkAction(QAction *action) const{    if (!action || (action->menu() && action->menu()->parentWidget() != const_cast<QDesignerMenu*>(this)))        return NoActionDrag; // menu action!! nothing to do    if (!Utils::isObjectAncestorOf(formWindow()->mainContainer(), action))        return NoActionDrag; // the action belongs to another form window    if (actions().contains(action))        return ActionDragOnSubMenu; // we already have the action in the menu    return AcceptActionDrag;}void QDesignerMenu::dragEnterEvent(QDragEnterEvent *event){    const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData());    if (!d || d->actionList().empty()) {        event->ignore();        return;    }    QAction *action = d->actionList().first();    switch (checkAction(action)) {    case NoActionDrag:        event->ignore();        break;    case ActionDragOnSubMenu:        d->accept(event);        m_dragging = true;        break;    case AcceptActionDrag:        d->accept(event);        m_dragging = true;        adjustIndicator(event->pos());        break;    }}void QDesignerMenu::dragMoveEvent(QDragMoveEvent *event){    if (actionGeometry(m_addSeparator).contains(event->pos())) {        event->ignore();        adjustIndicator(QPoint(-1, -1));        return;    }    const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData());    if (!d || d->actionList().empty()) {        event->ignore();        return;    }    QAction *action = d->actionList().first();    const ActionDragCheck dc = checkAction(action);    switch (dc) {    case NoActionDrag:        event->ignore();        break;    case ActionDragOnSubMenu:    case AcceptActionDrag: { // Do not pop up submenu of action being dragged        const int newIndex = findAction(event->pos());        if (safeActionAt(newIndex) != action) {            m_currentIndex = newIndex;            if (m_lastSubMenuIndex != m_currentIndex)                m_showSubMenuTimer->start(300);        }        if (dc == AcceptActionDrag) {            adjustIndicator(event->pos());            d->accept(event);        } else {            event->ignore();        }    }        break;    }}void QDesignerMenu::dragLeaveEvent(QDragLeaveEvent *){    m_dragging = false;    adjustIndicator(QPoint(-1, -1));    m_showSubMenuTimer->stop();}void QDesignerMenu::dropEvent(QDropEvent *event){    m_showSubMenuTimer->stop();    hideSubMenu();    m_dragging = false;    QDesignerFormWindowInterface *fw = formWindow();    const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData());    if (!d || d->actionList().empty()) {        event->ignore();        return;    }    QAction *action = d->actionList().first();    if (action && checkAction(action) == AcceptActionDrag) {        event->acceptProposedAction();        int index = findAction(event->pos());        index = qMin(index, actions().count() - 1);        fw->beginCommand(tr("Insert action"));        InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw);        cmd->init(this, action, safeActionAt(index));        fw->commandHistory()->push(cmd);        m_currentIndex = index;        if (parentMenu()) {            QAction *parent_action = parentMenu()->currentAction();            if (parent_action->menu() == 0) {                CreateSubmenuCommand *cmd = new CreateSubmenuCommand(fw);                cmd->init(parentMenu(), parentMenu()->currentAction(), action);                fw->commandHistory()->push(cmd);            }        }        update();        fw->endCommand();    } else {        event->ignore();    }    adjustIndicator(QPoint(-1, -1));}void QDesignerMenu::actionEvent(QActionEvent *event){    QMenu::actionEvent(event);    m_adjustSizeTimer->start(0);}QDesignerFormWindowInterface *QDesignerMenu::formWindow() const{    if (parentMenu())        return parentMenu()->formWindow();    return QDesignerFormWindowInterface::findFormWindow(parentWidget());}QDesignerActionProviderExtension *QDesignerMenu::actionProvider(){    if (QDesignerFormWindowInterface *fw = formWindow()) {        QDesignerFormEditorInterface *core = fw->core();        return qt_extension<QDesignerActionProviderExtension*>(core->extensionManager(), this);    }    return 0;}void QDesignerMenu::closeMenuChain(){    m_showSubMenuTimer->stop();    QWidget *w = this;    while (w && qobject_cast<QMenu*>(w))        w = w->parentWidget();    if (w) {        foreach (QMenu *subMenu, qFindChildren<QMenu*>(w)) {            subMenu->hide();        }    }    m_lastSubMenuIndex = -1;}void QDesignerMenu::moveLeft(){    if (parentMenu()) {        hide();    } else {        closeMenuChain();        if (QDesignerMenuBar *mb = parentMenuBar()) {            if (QApplication::layoutDirection() == Qt::LeftToRight)                mb->moveLeft();            else                mb->moveRight();        }    }    update();}void QDesignerMenu::moveRight(){    QAction *action = currentAction();    if (qobject_cast<SpecialMenuAction*>(action) || action->isSeparator()) {        closeMenuChain();        if (QDesignerMenuBar *mb = parentMenuBar()) {            if (QApplication::layoutDirection() == Qt::LeftToRight)                mb->moveRight();            else                mb->moveLeft();        }    } else {        m_lastSubMenuIndex = -1; // force a refresh        slotShowSubMenuNow();    }}void QDesignerMenu::moveUp(bool ctrl){    if (m_currentIndex == 0) {        hide();        return;    }

⌨️ 快捷键说明

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