📄 qdesigner_menu.cpp
字号:
{ event->accept(); int index = findAction(mapFromGlobal(event->globalPos())); QAction *action = safeActionAt(index); if (qobject_cast<SpecialMenuAction*>(action)) return true; QMenu menu(this); QAction *a = menu.addAction(tr("Remove action '%1'").arg(action->objectName())); QVariant itemData; qVariantSetValue(itemData, action); a->setData(itemData); connect(&menu, SIGNAL(triggered(QAction*)), this, SLOT(slotRemoveSelectedAction(QAction*))); menu.exec(event->globalPos()); return true;}void QDesignerMenu::slotRemoveSelectedAction(QAction *action){ QAction *a = qvariant_cast<QAction*>(action->data()); Q_ASSERT(a != 0); int pos = actions().indexOf(a); QAction *action_before = 0; if (pos != -1) action_before = safeActionAt(pos + 1); RemoveActionFromCommand *cmd = new RemoveActionFromCommand(formWindow()); cmd->init(this, a, action_before); formWindow()->commandHistory()->push(cmd);}QRect QDesignerMenu::subMenuPixmapRect(QAction *action) const{ static const QPixmap pm(":/trolltech/formeditor/images/submenu.png"); QRect g = actionGeometry(action); int x = g.right() - pm.width() - 2; 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::paintEvent(QPaintEvent *event){ QMenu::paintEvent(event); QPainter p(this); QAction *current = currentAction(); foreach (QAction *a, actions()) { 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(":/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; } QRect g = actionGeometry(current); drawSelection(&p, g.adjusted(1, 1, -3, -2));}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: { QWidget *widget = qobject_cast<QWidget*>(object); if (dispatch && widget && (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{ int index = actionAtPosition(pos); if (index == -1) return realActionCount(); return index;}void QDesignerMenu::adjustIndicator(const QPoint &pos){ if (QDesignerActionProviderExtension *a = actionProvider()) { a->adjustIndicator(pos); }}QAction *QDesignerMenu::actionMimeData(const QMimeData *mimeData) const{ if (const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(mimeData)) { Q_ASSERT(!d->items.isEmpty()); return d->items.first(); } return 0;}bool QDesignerMenu::checkAction(QAction *action) const{ if (!action || (action->menu() && action->menu()->parentWidget() != const_cast<QDesignerMenu*>(this))) return false; // menu action!! nothing to do if (actions().contains(action)) return false; // we already have the action in the menu if (!Utils::isObjectAncestorOf(formWindow()->mainContainer(), action)) return false; // the action belongs to another form window return true;}void QDesignerMenu::dragEnterEvent(QDragEnterEvent *event){ QAction *action = actionMimeData(event->mimeData()); if (!action) return; m_dragging = true; event->acceptProposedAction(); if (checkAction(action)) { adjustIndicator(event->pos()); }}void QDesignerMenu::dragMoveEvent(QDragMoveEvent *event){ if (actionGeometry(m_addSeparator).contains(event->pos())) { event->ignore(); adjustIndicator(QPoint(-1, -1)); return; } QAction *action = actionMimeData(event->mimeData()); if (action == 0 || !checkAction(action)) { event->ignore(); return; } event->acceptProposedAction(); adjustIndicator(event->pos()); m_currentIndex = findAction(event->pos()); if (m_lastSubMenuIndex != m_currentIndex) m_showSubMenuTimer->start(300);}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; QAction *action = actionMimeData(event->mimeData()); if (action && checkAction(action)) { event->acceptProposedAction(); int index = findAction(event->pos()); index = qMin(index, actions().count() - 1); formWindow()->beginCommand(tr("Insert action")); InsertActionIntoCommand *cmd = new InsertActionIntoCommand(formWindow()); cmd->init(this, action, safeActionAt(index)); formWindow()->commandHistory()->push(cmd); m_currentIndex = index; if (parentMenu()) { QAction *parent_action = parentMenu()->currentAction(); if (parent_action->menu() == 0) { CreateSubmenuCommand *cmd = new CreateSubmenuCommand(formWindow()); cmd->init(parentMenu(), parentMenu()->currentAction()); formWindow()->commandHistory()->push(cmd); } } updateCurrentAction(); formWindow()->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 if (/*QDesignerMenuBar *mb = */parentMenuBar()) {#if 0 //. ### disabled for now.. it's a bit confusing hide(); mb->moveLeft();#endif } updateCurrentAction();}void QDesignerMenu::moveRight(){ QAction *action = currentAction(); if (qobject_cast<SpecialMenuAction*>(action) || action->isSeparator()) { closeMenuChain(); if (QDesignerMenuBar *mb = parentMenuBar()) { mb->moveRight(); } } else { m_lastSubMenuIndex = -1; // force a refresh slotShowSubMenuNow(); }}void QDesignerMenu::moveUp(bool ctrl){ if (m_currentIndex == 0) { hide(); return; } if (ctrl) (void) swap(m_currentIndex, m_currentIndex - 1); m_currentIndex = qMax(0, --m_currentIndex); updateCurrentAction();}void QDesignerMenu::moveDown(bool ctrl){ if (m_currentIndex == actions().count() - 1) { return; } if (ctrl) (void) swap(m_currentIndex + 1, m_currentIndex); m_currentIndex = qMin(actions().count() - 1, ++m_currentIndex); updateCurrentAction();}QAction *QDesignerMenu::currentAction() const{ if (m_currentIndex < 0 || m_currentIndex >= actions().count()) return 0; return safeActionAt(m_currentIndex);}int QDesignerMenu::realActionCount() const{ return actions().count() - 2; // 2 fake actions}void QDesignerMenu::updateCurrentAction()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -