qdesigner_menubar.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 953 行 · 第 1/2 页
CPP
953 行
action = menu->menuAction(); AddMenuActionCommand *cmd = new AddMenuActionCommand(fw); cmd->init(action, m_addMenu, this, this); fw->commandHistory()->push(cmd); } SetPropertyCommand *cmd = new SetPropertyCommand(fw); cmd->init(action, QLatin1String("text"), m_editor->text()); fw->commandHistory()->push(cmd); fw->endCommand();}void QDesignerMenuBar::showLineEdit(){ QAction *action = 0; if (m_currentIndex >= 0 && m_currentIndex < realActionCount()) action = safeActionAt(m_currentIndex); else action = m_addMenu; if (action->isSeparator()) return; // hideMenu(); m_lastFocusWidget = qApp->focusWidget(); // open edit field for item name const QString text = action != m_addMenu ? action->text() : QString(); m_editor->setText(text); m_editor->selectAll(); m_editor->setGeometry(actionGeometry(action)); m_editor->show(); qApp->setActiveWindow(m_editor); m_editor->setFocus(); m_editor->grabKeyboard();}bool QDesignerMenuBar::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 true; } bool dispatch = true; switch (event->type()) { default: break; case QEvent::KeyPress: case QEvent::KeyRelease: case QEvent::ContextMenu: case QEvent::MouseMove: case QEvent::MouseButtonPress: case QEvent::MouseButtonRelease: case QEvent::MouseButtonDblClick: 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; case QEvent::Shortcut: event->accept(); return true; } return false;};int QDesignerMenuBar::actionAtPosition(const QPoint &pos) const{ QList<QAction*> lst = actions(); int index = 0; for (; index<lst.count(); ++index) { QRect g = actionGeometry(lst.at(index)); if (QApplication::layoutDirection() == Qt::LeftToRight) g.setTopLeft(QPoint(0, 0)); else g.setTopRight(QPoint(rect().width(), 0)); if (g.contains(pos)) return index; } return -1;}int QDesignerMenuBar::findAction(const QPoint &pos) const{ const int index = actionAtPosition(pos); if (index == -1) return realActionCount(); return index;}void QDesignerMenuBar::adjustIndicator(const QPoint &pos){ const int index = findAction(pos); QAction *action = safeActionAt(index); Q_ASSERT(action != 0); if (pos != QPoint(-1, -1)) { QDesignerMenu *m = qobject_cast<QDesignerMenu*>(action->menu()); if (!m || m->parentMenu()) { m_currentIndex = index; showMenu(index); } } if (QDesignerActionProviderExtension *a = actionProvider()) { a->adjustIndicator(pos); }}QDesignerMenuBar::ActionDragCheck QDesignerMenuBar::checkAction(QAction *action) const{ // action belongs to another form if (!action || !Utils::isObjectAncestorOf(formWindow()->mainContainer(), action)) return NoActionDrag; if (!action->menu()) return ActionDragOnSubMenu; // simple action only on sub menus QDesignerMenu *m = qobject_cast<QDesignerMenu*>(action->menu()); if (m && m->parentMenu()) return ActionDragOnSubMenu; // it looks like a submenu if (actions().contains(action)) return ActionDragOnSubMenu; // we already have the action in the menubar return AcceptActionDrag;}void QDesignerMenuBar::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: m_dragging = true; d->accept(event); break; case AcceptActionDrag: m_dragging = true; d->accept(event); adjustIndicator(event->pos()); break; }}void QDesignerMenuBar::dragMoveEvent(QDragMoveEvent *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: event->ignore(); showMenu(findAction(event->pos())); break; case AcceptActionDrag: d->accept(event); adjustIndicator(event->pos()); break; }}void QDesignerMenuBar::dragLeaveEvent(QDragLeaveEvent *){ m_dragging = false; adjustIndicator(QPoint(-1, -1));}void QDesignerMenuBar::dropEvent(QDropEvent *event){ m_dragging = false; if (const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData())) { QAction *action = d->actionList().first(); if (checkAction(action) == AcceptActionDrag) { event->acceptProposedAction(); int index = findAction(event->pos()); index = qMin(index, actions().count() - 1); QDesignerFormWindowInterface *fw = formWindow(); InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); cmd->init(this, action, safeActionAt(index)); fw->commandHistory()->push(cmd); m_currentIndex = index; update(); adjustIndicator(QPoint(-1, -1)); return; } } event->ignore();}void QDesignerMenuBar::actionEvent(QActionEvent *event){ QMenuBar::actionEvent(event);}QDesignerFormWindowInterface *QDesignerMenuBar::formWindow() const{ return QDesignerFormWindowInterface::findFormWindow(const_cast<QDesignerMenuBar*>(this));}QDesignerActionProviderExtension *QDesignerMenuBar::actionProvider(){ if (QDesignerFormWindowInterface *fw = formWindow()) { QDesignerFormEditorInterface *core = fw->core(); return qt_extension<QDesignerActionProviderExtension*>(core->extensionManager(), this); } return 0;}QAction *QDesignerMenuBar::currentAction() const{ if (m_currentIndex < 0 || m_currentIndex >= actions().count()) return 0; return safeActionAt(m_currentIndex);}int QDesignerMenuBar::realActionCount() const{ return actions().count() - 1; // 1 fake actions}void QDesignerMenuBar::moveLeft(bool ctrl){ if (ctrl) (void) swap(m_currentIndex, m_currentIndex - 1); m_currentIndex = qMax(0, --m_currentIndex); // Always re-select, swapping destroys order updateCurrentAction(true);}bool QDesignerMenuBar::dragging() const{ return m_dragging;}void QDesignerMenuBar::moveRight(bool ctrl){ if (ctrl) (void) swap(m_currentIndex + 1, m_currentIndex); m_currentIndex = qMin(actions().count() - 1, ++m_currentIndex); updateCurrentAction(!ctrl);}void QDesignerMenuBar::moveUp(){ update();}void QDesignerMenuBar::moveDown(){ showMenu();}void QDesignerMenuBar::adjustSpecialActions(){ removeAction(m_addMenu); addAction(m_addMenu);}bool QDesignerMenuBar::interactive(bool i){ const bool old = m_interactive; m_interactive = i; return old;}void QDesignerMenuBar::hideMenu(int index){ if (index < 0 && m_currentIndex >= 0) index = m_currentIndex; if (index < 0 || index >= realActionCount()) return; QAction *action = safeActionAt(index); if (action && action->menu()) { action->menu()->hide(); if (QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(action->menu())) { menu->closeMenuChain(); } }}void QDesignerMenuBar::deleteMenu(){ QAction *action = currentAction(); if (action && !qobject_cast<SpecialMenuAction*>(action)) { const int pos = actions().indexOf(action); QAction *action_before = 0; if (pos != -1) action_before = safeActionAt(pos + 1); QDesignerFormWindowInterface *fw = formWindow(); RemoveMenuActionCommand *cmd = new RemoveMenuActionCommand(fw); cmd->init(action, action_before, this, this); fw->commandHistory()->push(cmd); }}void QDesignerMenuBar::showMenu(int index){ if (index < 0 && m_currentIndex >= 0) index = m_currentIndex; if (index < 0 || index >= realActionCount()) return; m_currentIndex = index; QAction *action = currentAction(); if (action && action->menu()) { if (m_lastMenuActionIndex != -1 && m_lastMenuActionIndex != index) { hideMenu(m_lastMenuActionIndex); } m_lastMenuActionIndex = index; QMenu *menu = action->menu(); const QRect g = actionGeometry(action); if (!menu->isVisible()) { if ((menu->windowFlags() & Qt::Popup) != Qt::Popup) menu->setWindowFlags(Qt::Popup); menu->adjustSize(); menu->move(mapToGlobal(g.bottomLeft())); menu->setFocus(Qt::MouseFocusReason); menu->raise(); menu->show(); } else { menu->raise(); } }}QAction *QDesignerMenuBar::safeActionAt(int index) const{ if (index < 0 || index >= actions().count()) return 0; return actions().at(index);}bool QDesignerMenuBar::swap(int a, int b){ const int left = qMin(a, b); int right = qMax(a, b); QAction *action_a = safeActionAt(left); QAction *action_b = safeActionAt(right); if (action_a == action_b || !action_a || !action_b || qobject_cast<SpecialMenuAction*>(action_a) || qobject_cast<SpecialMenuAction*>(action_b)) return false; // nothing to do right = qMin(right, realActionCount()); if (right < 0) return false; // nothing to do formWindow()->beginCommand(QApplication::translate("Command", "Move action")); QAction *action_b_before = safeActionAt(right + 1); QDesignerFormWindowInterface *fw = formWindow(); RemoveActionFromCommand *cmd1 = new RemoveActionFromCommand(fw); cmd1->init(this, action_b, action_b_before, false); fw->commandHistory()->push(cmd1); QAction *action_a_before = safeActionAt(left + 1); InsertActionIntoCommand *cmd2 = new InsertActionIntoCommand(fw); cmd2->init(this, action_b, action_a_before, false); fw->commandHistory()->push(cmd2); RemoveActionFromCommand *cmd3 = new RemoveActionFromCommand(fw); cmd3->init(this, action_a, action_b, false); fw->commandHistory()->push(cmd3); InsertActionIntoCommand *cmd4 = new InsertActionIntoCommand(fw); cmd4->init(this, action_a, action_b_before, true); fw->commandHistory()->push(cmd4); fw->endCommand(); return true;}void QDesignerMenuBar::keyPressEvent(QKeyEvent *event){ event->ignore();}void QDesignerMenuBar::keyReleaseEvent(QKeyEvent *event){ event->ignore();}void QDesignerMenuBar::updateCurrentAction(bool selectAction){ update(); if (!selectAction) return; QAction *action = currentAction(); if (!action || action == m_addMenu) return; QMenu *menu = action->menu(); if (!menu) return; QDesignerObjectInspector *oi = 0; if (QDesignerFormWindowInterface *fw = formWindow()) oi = qobject_cast<QDesignerObjectInspector *>(fw->core()->objectInspector()); if (!oi) return; oi->clearSelection(); oi->selectObject(menu);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?