void tst_QMenu::activeSubMenuPosition() { QPushButton lab("subMenuPosition test"); QMenu *sub = new QMenu("Submenu", &lab); sub->addAction("Sub-Item1"); QAction *subAction = sub->addAction("Sub-Item2"); QMenu *main = new QMenu("Menu-Title", &lab); (void)main->addAction("Item 1"); QAction *menuAction = main->addMenu(sub); (void)main->addAction("Item 3"); (void)main->addAction("Item 4"); main->setActiveAction(menuAction); sub->setActiveAction(subAction); main->popup(QPoint(200,200)); QVERIFY(main->isVisible()); QCOMPARE(main->activeAction(), menuAction); QVERIFY(sub->isVisible()); QVERIFY(sub->pos() != QPoint(0,0)); // well, it's enough to check the pos is not (0,0) but it's more safe // to check that submenu is to the right of the main menu too. #ifndef Q_OS_WINCE_WM QVERIFY(sub->pos().x() > main->pos().x()); QCOMPARE(sub->activeAction(), subAction); #endif }
QAction *QMenuProto::activeAction() const { QMenu *item = qscriptvalue_cast<QMenu*>(thisObject()); if (item) return item->activeAction(); return 0; }
void tst_QMenu::widgetActionFocus() { //test if the focus is correctly handled with a QWidgetAction QMenu m; QListWidget *l = new QListWidget(&m); for (int i = 1; i<3 ; i++) l->addItem(QString("item%1").arg(i)); QWidgetAction *wa = new QWidgetAction(&m); wa->setDefaultWidget(l); m.addAction(wa); m.setActiveAction(wa); l->setFocus(); //to ensure it has primarily the focus QAction *menuitem1=m.addAction("menuitem1"); QAction *menuitem2=m.addAction("menuitem2"); m.popup(QPoint()); QVERIFY(m.isVisible()); QVERIFY(l->hasFocus()); QVERIFY(l->currentItem()); QCOMPARE(l->currentItem()->text(), QString("item1")); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down); QVERIFY(l->currentItem()); QCOMPARE(l->currentItem()->text(), QString("item2")); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down); QVERIFY(m.hasFocus()); QCOMPARE(m.activeAction(), menuitem1); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down); QVERIFY(m.hasFocus()); QCOMPARE(m.activeAction(), menuitem2); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Up); QVERIFY(m.hasFocus()); QCOMPARE(m.activeAction(), menuitem1); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Up); QVERIFY(l->hasFocus()); QCOMPARE(m.activeAction(), (QAction *)wa); }
void tst_QMenu::activeSubMenuPosition() { QPushButton lab("subMenuPosition test"); QMenu *sub = new QMenu("Submenu", &lab); sub->addAction("Sub-Item1"); QAction *subAction = sub->addAction("Sub-Item2"); QMenu *main = new QMenu("Menu-Title", &lab); (void)main->addAction("Item 1"); QAction *menuAction = main->addMenu(sub); (void)main->addAction("Item 3"); (void)main->addAction("Item 4"); main->setActiveAction(menuAction); sub->setActiveAction(subAction); #ifdef Q_OS_SYMBIAN main->popup(QPoint(50,200)); #else main->popup(QPoint(200,200)); #endif QVERIFY(main->isVisible()); QCOMPARE(main->activeAction(), menuAction); QVERIFY(sub->isVisible()); QVERIFY(sub->pos() != QPoint(0,0)); // well, it's enough to check the pos is not (0,0) but it's more safe // to check that submenu is to the right of the main menu too. #ifdef Q_OS_WINCE_WM QSKIP("Not true for Windows Mobile Soft Keys", SkipSingle); #endif #ifdef Q_OS_SYMBIAN // On Symbian, QS60Style::pixelMetric(QStyle::PM_SubMenuOverlap) is different with other styles. QVERIFY(sub->pos().x() < main->pos().x()); #else QVERIFY(sub->pos().x() > main->pos().x()); #endif QCOMPARE(sub->activeAction(), subAction); }
QMenu *VerticalMenu::leafMenu() { QMenu *leaf = this; while (true) { QAction *act = leaf->activeAction(); if (act && act->menu() && act->menu()->isVisible()) { leaf = act->menu(); continue; } return leaf == this ? 0 : leaf; } return 0; // make gcc happy }
QStyleOptionMenuItem KxMenuItemWidget::getStyleOption() const { QWidget *q = parentWidget(); QMenu *menu = (QMenu *)q; KxMenuItem *action = fMenuItem; QStyleOptionMenuItem opt; opt.initFrom(this); opt.palette = q->palette(); opt.state = QStyle::State_None; if (q->window()->isActiveWindow()) opt.state |= QStyle::State_Active; if (q->isEnabled() && action->isEnabled() && (!action->menu() || action->menu()->isEnabled())) opt.state |= QStyle::State_Enabled; else opt.palette.setCurrentColorGroup(QPalette::Disabled); opt.font = action->font(); if (menu->activeAction() == action && !action->isSeparator()) { opt.state |= QStyle::State_Selected; // | (mouseDown ? QStyle::State_Sunken : QStyle::State_None); } if (!action->isCheckable()) { opt.checkType = QStyleOptionMenuItem::NotCheckable; } else { opt.checkType = (action->actionGroup() && action->actionGroup()->isExclusive()) ? QStyleOptionMenuItem::Exclusive : QStyleOptionMenuItem::NonExclusive; opt.checked = action->isChecked(); } if (action->menu()) opt.menuItemType = QStyleOptionMenuItem::SubMenu; else if (action->isSeparator()) opt.menuItemType = QStyleOptionMenuItem::Separator; else if (menu->defaultAction() == action) opt.menuItemType = QStyleOptionMenuItem::DefaultItem; else opt.menuItemType = QStyleOptionMenuItem::Normal; opt.icon = action->icon(); opt.text = action->text(); opt.menuRect = q->rect(); return opt; }
// ---------------------------------------------------------------------------- bool qMRMLTreeViewEventTranslator::translateEvent(QObject *Object, QEvent *Event, int EventType, bool &Error) { Q_UNUSED(Error); qMRMLTreeView* treeView = NULL; for(QObject* test = Object; treeView == NULL && test != NULL; test = test->parent()) { treeView = qobject_cast<qMRMLTreeView*>(test); } // qMRMLTreeView* treeView = qobject_cast<qMRMLTreeView*>(Object); if(!treeView) { return false; } // For the custom action when we have a right click QMenu* menu = NULL; for(QObject* test = Object; menu == NULL && test != NULL ; test = test->parent()) { menu = qobject_cast<QMenu*>(test); } if (menu) { if(Event->type() == QEvent::KeyPress) { QKeyEvent* e = static_cast<QKeyEvent*>(Event); if(e->key() == Qt::Key_Enter) { QAction* action = menu->activeAction(); if(action) { QString which = action->objectName(); if(which == QString::null) { which = action->text(); } if (which != "Rename" && which != "Delete" ) { emit recordEvent(menu, "activate", which); } } } } if(Event->type() == QEvent::MouseButtonRelease) { QMouseEvent* e = static_cast<QMouseEvent*>(Event); if(e->button() == Qt::LeftButton) { QAction* action = menu->actionAt(e->pos()); if (action && !action->menu()) { QString which = action->objectName(); if(which == QString::null) { which = action->text(); } if (which != "Rename" && which != "Delete" ) { emit recordEvent(menu, "activate", which); } } } } return true; } // We want to stop the action on the QDialog when we are renaming // and let passed the action for the "set_current". QInputDialog* dialog = NULL; for(QObject* test = Object; dialog == NULL && test != NULL; test = test->parent()) { dialog = qobject_cast<QInputDialog*>(test); if(dialog) { // block actions on the QInputDialog return true; } } if(Event->type() == QEvent::Enter && Object == treeView) { if(this->CurrentObject != Object) { if(this->CurrentObject) { disconnect(this->CurrentObject, 0, this, 0); } this->CurrentObject = Object; connect(treeView, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*))); connect(treeView, SIGNAL(currentNodeRenamed(QString)), this, SLOT(onCurrentNodeRenamed(QString))); // Can be better to do it on the model to recover the QModelIndex connect(treeView, SIGNAL(currentNodeDeleted(const QModelIndex&)), this, SLOT(onCurrentNodeDeleted(const QModelIndex&))); connect(treeView, SIGNAL(decorationClicked(QModelIndex)), this, SLOT(onDecorationClicked(QModelIndex))); connect(treeView->sceneModel(), SIGNAL(aboutToReparentByDragAndDrop(vtkMRMLNode*,vtkMRMLNode*)), this, SLOT(onAboutToReparentByDnD(vtkMRMLNode*,vtkMRMLNode*))); } return this->Superclass::translateEvent(Object, Event, EventType, Error); } return this->Superclass::translateEvent(Object, Event, EventType, Error); }