qdesigner_menu.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,357 行 · 第 1/3 页
CPP
1,357 行
if (ctrl) (void) swap(m_currentIndex, m_currentIndex - 1); m_currentIndex = qMax(0, --m_currentIndex); // Always re-select, swapping destroys order update(); selectCurrentAction();}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); update(); if (!ctrl) selectCurrentAction();}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::selectCurrentAction(){ QAction *action = currentAction(); if (!action || action == m_addSeparator || action == m_addItem) return; QDesignerObjectInspector *oi = 0; if (QDesignerFormWindowInterface *fw = formWindow()) oi = qobject_cast<QDesignerObjectInspector *>(fw->core()->objectInspector()); if (!oi) return; oi->clearSelection(); if (QMenu *menu = action->menu()) oi->selectObject(menu); else oi->selectObject(action);}void QDesignerMenu::createRealMenuAction(QAction *action){ if (action->menu()) return; // nothing to do QDesignerFormWindowInterface *fw = formWindow(); QDesignerFormEditorInterface *core = formWindow()->core(); QDesignerMenu *menu = findOrCreateSubMenu(action); m_subMenus.remove(action); action->setMenu(menu); menu->setTitle(action->text()); Q_ASSERT(fw); core->widgetFactory()->initialize(menu); const QString niceObjectName = ActionEditor::actionTextToName(menu->title(), QLatin1String("menu")); menu->setObjectName(niceObjectName); core->metaDataBase()->add(menu); fw->ensureUniqueObjectName(menu); QAction *menuAction = menu->menuAction(); core->metaDataBase()->add(menuAction);}void QDesignerMenu::removeRealMenu(QAction *action){ QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(action->menu()); if (menu == 0) return; action->setMenu(0); m_subMenus.insert(action, menu); QDesignerFormEditorInterface *core = formWindow()->core(); core->metaDataBase()->remove(menu);}QDesignerMenu *QDesignerMenu::findOrCreateSubMenu(QAction *action){ if (action->menu()) return qobject_cast<QDesignerMenu*>(action->menu()); QDesignerMenu *menu = m_subMenus.value(action); if (!menu) { menu = new QDesignerMenu(this); m_subMenus.insert(action, menu); } return menu;}bool QDesignerMenu::canCreateSubMenu(QAction *action) const // ### improve it's a bit too slow{ foreach (const QWidget *aw, action->associatedWidgets()) if (aw != this) { if (const QMenu *m = qobject_cast<const QMenu *>(aw)) { if (m->actions().contains(action)) return false; // sorry } else { if (const QToolBar *tb = qobject_cast<const QToolBar *>(aw)) if (tb->actions().contains(action)) return false; // sorry } } return true;}void QDesignerMenu::slotShowSubMenuNow(){ m_showSubMenuTimer->stop(); if (m_lastSubMenuIndex == m_currentIndex) return; if (m_lastSubMenuIndex != -1) hideSubMenu(); if (m_currentIndex >= realActionCount()) return; QAction *action = currentAction(); if (action->isSeparator() || !canCreateSubMenu(action)) return; if (QMenu *menu = findOrCreateSubMenu(action)) { if (!menu->isVisible()) { if ((menu->windowFlags() & Qt::Popup) != Qt::Popup) menu->setWindowFlags(Qt::Popup); const QRect g = actionGeometry(action); menu->move(mapToGlobal(g.topRight())); menu->show(); menu->setFocus(); } else { menu->raise(); } menu->setFocus(); m_lastSubMenuIndex = m_currentIndex; }}void QDesignerMenu::showSubMenu(QAction *action){ m_showSubMenuTimer->stop(); if (m_editor->isVisible() || !action || qobject_cast<SpecialMenuAction*>(action) || action->isSeparator() || !isVisible()) return; m_showSubMenuTimer->start(300);}QDesignerMenu *QDesignerMenu::parentMenu() const{ return qobject_cast<QDesignerMenu*>(parentWidget());}QDesignerMenuBar *QDesignerMenu::parentMenuBar() const{ if (QDesignerMenuBar *mb = qobject_cast<QDesignerMenuBar*>(parentWidget())) { return mb; } else if (QDesignerMenu *m = parentMenu()) { return m->parentMenuBar(); } return 0;}void QDesignerMenu::setVisible(bool visible){ if (visible) m_currentIndex = 0; else m_lastSubMenuIndex = -1; QMenu::setVisible(visible);}void QDesignerMenu::adjustSpecialActions(){ removeAction(m_addItem); removeAction(m_addSeparator); addAction(m_addItem); addAction(m_addSeparator);}bool QDesignerMenu::interactive(bool i){ const bool old = m_interactive; m_interactive = i; return old;}void QDesignerMenu::enterEditMode(){ if (m_currentIndex >= 0 && m_currentIndex <= realActionCount()) { showLineEdit(); } else { hideSubMenu(); QDesignerFormWindowInterface *fw = formWindow(); fw->beginCommand(tr("Add separator")); QAction *sep = createAction(QString(), true); InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); cmd->init(this, sep, safeActionAt(realActionCount())); 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(); m_currentIndex = actions().indexOf(m_addItem); update(); }}void QDesignerMenu::leaveEditMode(LeaveEditMode mode){ if (mode == Default) return; QAction *action = 0; QDesignerFormWindowInterface *fw = formWindow(); if (m_currentIndex < realActionCount()) { action = safeActionAt(m_currentIndex); fw->beginCommand(QApplication::translate("Command", "Set action text")); } else { Q_ASSERT(fw != 0); fw->beginCommand(QApplication::translate("Command", "Insert action")); action = createAction(ActionEditor::actionTextToName(m_editor->text())); InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); cmd->init(this, action, currentAction()); fw->commandHistory()->push(cmd); } SetPropertyCommand *cmd = new SetPropertyCommand(fw); cmd->init(action, QLatin1String("text"), m_editor->text()); 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(), action); fw->commandHistory()->push(cmd); } } update(); fw->endCommand();}QAction *QDesignerMenu::safeMenuAction(QDesignerMenu *menu) const{ QAction *action = menu->menuAction(); if (!action) action = m_subMenus.key(menu); return action;}void QDesignerMenu::showLineEdit(){ m_showSubMenuTimer->stop(); QAction *action = 0; if (m_currentIndex < realActionCount()) action = safeActionAt(m_currentIndex); else action = m_addItem; if (action->isSeparator()) return; hideSubMenu(); // open edit field for item name setFocus(); const QString text = action != m_addItem ? action->text() : QString(); m_editor->setText(text); m_editor->selectAll(); m_editor->setGeometry(actionGeometry(action).adjusted(1, 1, -2, -2)); m_editor->show(); m_editor->setFocus();}QAction *QDesignerMenu::createAction(const QString &objectName, bool separator){ QDesignerFormWindowInterface *fw = formWindow(); Q_ASSERT(fw); return ToolBarEventFilter::createAction(fw, objectName, separator);}// ### share with QDesignerMenu::swapbool QDesignerMenu::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 QDesignerFormWindowInterface *fw = formWindow(); fw->beginCommand(QApplication::translate("Command", "Move action")); QAction *action_b_before = safeActionAt(right + 1); 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;}QAction *QDesignerMenu::safeActionAt(int index) const{ if (index < 0 || index >= actions().count()) return 0; return actions().at(index);}void QDesignerMenu::hideSubMenu(){ m_lastSubMenuIndex = -1; foreach (QMenu *subMenu, qFindChildren<QMenu*>(this)) { subMenu->hide(); }}void QDesignerMenu::deleteAction(){ QAction *action = currentAction(); const int pos = actions().indexOf(action); QAction *action_before = 0; if (pos != -1) action_before = safeActionAt(pos + 1); QDesignerFormWindowInterface *fw = formWindow(); RemoveActionFromCommand *cmd = new RemoveActionFromCommand(fw); cmd->init(this, action, action_before); fw->commandHistory()->push(cmd); update();}void QDesignerMenu::deactivateMenu(){ m_deactivateWindowTimer->start(10);}void QDesignerMenu::slotDeactivateNow(){ m_deactivateWindowTimer->stop(); if (m_dragging) return; QDesignerMenu *root = findRootMenu(); if (! root->findActivatedMenu()) { root->hide(); root->hideSubMenu(); }}void QDesignerMenu::drawSelection(QPainter *p, const QRect &r){ p->save(); QColor c = Qt::blue; p->setPen(QPen(c, 1)); c.setAlpha(32); p->setBrush(c); p->drawRect(r); p->restore();}void QDesignerMenu::keyPressEvent(QKeyEvent *event){ event->ignore();}void QDesignerMenu::keyReleaseEvent(QKeyEvent *event){ event->ignore();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?