bool ToolBarEventFilter::handleContextMenuEvent(QContextMenuEvent * event ) { event->accept(); const QPoint globalPos = event->globalPos(); const int index = actionIndexAt(m_toolBar, m_toolBar->mapFromGlobal(globalPos), m_toolBar->orientation()); const ActionList actions = m_toolBar->actions(); QAction *action = index != -1 ?actions.at(index) : 0; QVariant itemData; QMenu menu(0); // Insert before if (action && index != 0 && !action->isSeparator()) { QAction *newSeperatorAct = menu.addAction(tr("Insert Separator before '%1'").arg(action->objectName())); qVariantSetValue(itemData, action); newSeperatorAct->setData(itemData); connect(newSeperatorAct, SIGNAL(triggered()), this, SLOT(slotInsertSeparator())); } // Append separator if (actions.empty() || !actions.back()->isSeparator()) { QAction *newSeperatorAct = menu.addAction(tr("Append Separator")); qVariantSetValue(itemData, static_cast<QAction*>(0)); newSeperatorAct->setData(itemData); connect(newSeperatorAct, SIGNAL(triggered()), this, SLOT(slotInsertSeparator())); } // Remove if (!menu.actions().empty()) menu.addSeparator(); // Remove if (action) { QAction *a = menu.addAction(tr("Remove action '%1'").arg(action->objectName())); qVariantSetValue(itemData, action); a->setData(itemData); connect(a, SIGNAL(triggered()), this, SLOT(slotRemoveSelectedAction())); } QAction *remove_toolbar = menu.addAction(tr("Remove Toolbar '%1'").arg(m_toolBar->objectName())); connect(remove_toolbar, SIGNAL(triggered()), this, SLOT(slotRemoveToolBar())); menu.exec(globalPos); return true; }
ActionList ToolBarEventFilter::contextMenuActions(const QPoint &globalPos) { ActionList rc; const int index = actionIndexAt(m_toolBar, m_toolBar->mapFromGlobal(globalPos), m_toolBar->orientation()); const ActionList actions = m_toolBar->actions(); QAction *action = index != -1 ?actions.at(index) : 0; QVariant itemData; // Insert before if (action && index != 0 && !action->isSeparator()) { QAction *newSeperatorAct = new QAction(tr("Insert Separator before '%1'").arg(action->objectName()), 0); itemData.setValue(action); newSeperatorAct->setData(itemData); connect(newSeperatorAct, SIGNAL(triggered()), this, SLOT(slotInsertSeparator())); rc.push_back(newSeperatorAct); } // Append separator if (actions.empty() || !actions.back()->isSeparator()) { QAction *newSeperatorAct = new QAction(tr("Append Separator"), 0); itemData.setValue(static_cast<QAction*>(0)); newSeperatorAct->setData(itemData); connect(newSeperatorAct, SIGNAL(triggered()), this, SLOT(slotInsertSeparator())); rc.push_back(newSeperatorAct); } // Promotion if (!m_promotionTaskMenu) m_promotionTaskMenu = new PromotionTaskMenu(m_toolBar, PromotionTaskMenu::ModeSingleWidget, this); m_promotionTaskMenu->addActions(formWindow(), PromotionTaskMenu::LeadingSeparator|PromotionTaskMenu::TrailingSeparator, rc); // Remove if (action) { QAction *a = new QAction(tr("Remove action '%1'").arg(action->objectName()), 0); itemData.setValue(action); a->setData(itemData); connect(a, SIGNAL(triggered()), this, SLOT(slotRemoveSelectedAction())); rc.push_back(a); } QAction *remove_toolbar = new QAction(tr("Remove Toolbar '%1'").arg(m_toolBar->objectName()), 0); connect(remove_toolbar, SIGNAL(triggered()), this, SLOT(slotRemoveToolBar())); rc.push_back(remove_toolbar); return rc; }