📄 qdesigner_menu.cpp
字号:
{ update();}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(formWindow() != 0); core->widgetFactory()->initialize(menu); QString niceObjectName = ActionEditor::actionTextToName(menu->title()); if (niceObjectName.startsWith("action")) niceObjectName.replace(0, 6, 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{ QWidget *topLevel = formWindow()->mainContainer(); QList<QMenu*> menus = qFindChildren<QMenu*>(topLevel); QList<QToolBar*> toolBars = qFindChildren<QToolBar*>(topLevel); foreach (const QMenu *m, menus) { if (m != this && m->actions().contains(action)) { return false; // sorry } } foreach (QToolBar *tb, toolBars) { 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); 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){ bool old = m_interactive; m_interactive = i; return old;}void QDesignerMenu::enterEditMode(){ if (m_currentIndex >= 0 && m_currentIndex <= realActionCount()) { showLineEdit(); } else { hideSubMenu(); formWindow()->beginCommand(tr("Add separator")); QAction *sep = createAction(QString(), true); InsertActionIntoCommand *cmd = new InsertActionIntoCommand(formWindow()); cmd->init(this, sep, safeActionAt(realActionCount())); formWindow()->commandHistory()->push(cmd); 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); } } formWindow()->endCommand(); m_currentIndex = actions().indexOf(m_addItem); update(); }}void QDesignerMenu::leaveEditMode(LeaveEditMode mode){ if (mode == Default) return; QAction *action = 0; if (m_currentIndex < realActionCount()) { action = safeActionAt(m_currentIndex); formWindow()->beginCommand(QLatin1String("Set action text")); } else { Q_ASSERT(formWindow() != 0); formWindow()->beginCommand(QLatin1String("Insert action")); action = createAction(ActionEditor::actionTextToName(m_editor->text())); InsertActionIntoCommand *cmd = new InsertActionIntoCommand(formWindow()); cmd->init(this, action, currentAction()); formWindow()->commandHistory()->push(cmd); } SetPropertyCommand *cmd = new SetPropertyCommand(formWindow()); cmd->init(action, QLatin1String("text"), m_editor->text()); formWindow()->commandHistory()->push(cmd); 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();}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(); 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();}// ### Share me with QDesignerToolBar (a.k.a. he's a copy of me)QAction *QDesignerMenu::createAction(const QString &objectName, bool separator){ Q_ASSERT(formWindow() != 0); QDesignerFormWindowInterface *fw = formWindow(); QAction *action = new QAction(fw); fw->core()->widgetFactory()->initialize(action); if (separator) action->setSeparator(true); action->setObjectName(objectName); fw->ensureUniqueObjectName(action); AddActionCommand *cmd = new AddActionCommand(fw); cmd->init(action); fw->commandHistory()->push(cmd); return action;}// ### share with QDesignerMenu::swapbool QDesignerMenu::swap(int a, int b){ 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(QLatin1String("Move action")); QAction *action_b_before = safeActionAt(right + 1); RemoveActionFromCommand *cmd1 = new RemoveActionFromCommand(formWindow()); cmd1->init(this, action_b, action_b_before, false); formWindow()->commandHistory()->push(cmd1); QAction *action_a_before = safeActionAt(left + 1); InsertActionIntoCommand *cmd2 = new InsertActionIntoCommand(formWindow()); cmd2->init(this, action_b, action_a_before, false); formWindow()->commandHistory()->push(cmd2); RemoveActionFromCommand *cmd3 = new RemoveActionFromCommand(formWindow()); cmd3->init(this, action_a, action_b, false); formWindow()->commandHistory()->push(cmd3); InsertActionIntoCommand *cmd4 = new InsertActionIntoCommand(formWindow()); cmd4->init(this, action_a, action_b_before, true); formWindow()->commandHistory()->push(cmd4); formWindow()->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(); int pos = actions().indexOf(action); QAction *action_before = 0; if (pos != -1) action_before = safeActionAt(pos + 1); RemoveActionFromCommand *cmd = new RemoveActionFromCommand(formWindow()); cmd->init(this, action, action_before); formWindow()->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(c); 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -