Example #1
2
void PdfViewer::showToolBarStylePopupMenu(const QPoint &pos)
{
	QMenu *popupMenu = new QMenu(this);
	popupMenu->setAttribute(Qt::WA_DeleteOnClose);
	popupMenu->move(mapToGlobal(QPoint(0,0)) + pos);

	QWidgetAction *iconOnlyAction = new QWidgetAction(popupMenu);
	QRadioButton *iconOnlyRadio = new QRadioButton(tr("&Icons Only"), popupMenu);
	iconOnlyAction->setDefaultWidget(iconOnlyRadio);

	QWidgetAction *textOnlyAction = new QWidgetAction(popupMenu);
	QRadioButton *textOnlyRadio = new QRadioButton(tr("&Text Only"), popupMenu);
	textOnlyAction->setDefaultWidget(textOnlyRadio);

	QWidgetAction *textBesideIconAction = new QWidgetAction(popupMenu);
	QRadioButton *textBesideIconRadio = new QRadioButton(tr("Text &Alongside Icons"), popupMenu);
	textBesideIconAction->setDefaultWidget(textBesideIconRadio);

	QWidgetAction *textUnderIconAction = new QWidgetAction(popupMenu);
	QRadioButton *textUnderIconRadio = new QRadioButton(tr("Text &Under Icons"), popupMenu);
	textUnderIconAction->setDefaultWidget(textUnderIconRadio);

	QButtonGroup *popupButtonGroup = new QButtonGroup(popupMenu);
	popupButtonGroup->addButton(iconOnlyRadio);
	popupButtonGroup->addButton(textOnlyRadio);
	popupButtonGroup->addButton(textBesideIconRadio);
	popupButtonGroup->addButton(textUnderIconRadio);
	popupButtonGroup->setId(iconOnlyRadio, 0);
	popupButtonGroup->setId(textOnlyRadio, 1);
	popupButtonGroup->setId(textBesideIconRadio, 2);
	popupButtonGroup->setId(textUnderIconRadio, 3);
	connect(popupButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotChangeToolBarStyle(int)));

	popupMenu->addAction(iconOnlyAction);
	popupMenu->addAction(textOnlyAction);
	popupMenu->addAction(textBesideIconAction);
	popupMenu->addAction(textUnderIconAction);
	popupMenu->setContentsMargins(5, 0, 5, 0);

	QSettings settings;
	settings.beginGroup("MainWindow");
	const int toolBarStyleNumber = settings.value("ToolBarStyle", 0).toInt();
	switch (toolBarStyleNumber)
	{
		case 0: iconOnlyRadio->setChecked(true); break;
		case 1: textOnlyRadio->setChecked(true); break;
		case 2: textBesideIconRadio->setChecked(true); break;
		case 3: textUnderIconRadio->setChecked(true); break;
	}
	settings.endGroup();

	popupMenu->show();
	// make sure that the popupMenu stays completely inside the screen (must be done after popupMenu->show() in order to have the correct width)
	const int desktopWidth = QApplication::desktop()->availableGeometry(this).width();
	if (popupMenu->x() + popupMenu->width() > desktopWidth)
		popupMenu->move(desktopWidth - popupMenu->width(), popupMenu->y());
}
void FlameGraphViewTest::testContextMenu()
{
    int targetWidth = 0;
    int targetHeight = 0;
    {
        QMenu testMenu;
        testMenu.addActions(QmlProfilerTool::profilerContextMenuActions());
        testMenu.addSeparator();
        testMenu.show();
        QTest::qWaitForWindowExposed(testMenu.window());
        targetWidth = testMenu.width() / 2;
        int prevHeight = testMenu.height();
        QAction dummy(QString("target"), this);
        testMenu.addAction(&dummy);
        targetHeight = (testMenu.height() + prevHeight) / 2;
    }

    QTimer timer;
    timer.setInterval(50);
    timer.start();

    connect(&timer, &QTimer::timeout, this, [&]() {
        auto activePopup = qApp->activePopupWidget();
        if (!activePopup || !activePopup->windowHandle()->isExposed())
            return;
        QTest::mouseMove(activePopup, QPoint(targetWidth, targetHeight));
        QTest::mouseClick(activePopup, Qt::LeftButton, Qt::NoModifier,
                          QPoint(targetWidth, targetHeight));

        if (!manager.isRestrictedToRange()) {
            // click somewhere else to remove the menu and return to outer function
            QTest::mouseClick(qApp->activePopupWidget(), Qt::LeftButton, Qt::NoModifier,
                              QPoint(500, 500));
        }
    });

    QTest::mouseMove(&view, QPoint(250, 250));
    QSignalSpy spy(&view, SIGNAL(showFullRange()));

    QContextMenuEvent event(QContextMenuEvent::Mouse, QPoint(250, 250));
    QVERIFY(qApp->notify(&view, &event));
    QCOMPARE(spy.count(), 0);

    manager.restrictToRange(1, 10);

    QVERIFY(qApp->notify(&view, &event));

    if (spy.count() != 1)
        QTRY_COMPARE(spy.count(), 1);
}
Example #3
0
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();
            if (layoutDirection() == Qt::LeftToRight) {
                menu->move(mapToGlobal(g.bottomLeft()));
            } else {
                // The position is not initially correct due to the unknown width,
                // causing it to overlap a bit the first time it is invoked.
                const QSize menuSize = menu->size();
                QPoint point = g.bottomRight() - QPoint(menu->width(), 0);
                menu->move(mapToGlobal(point));
            }
            menu->setFocus(Qt::MouseFocusReason);
            menu->raise();
            menu->show();
        } else {
            menu->raise();
        }
    }
}
QMenu *TikzCommandInserter::getMenu(const TikzCommandList &commandList)
{
	QMenu *menu = new QMenu(commandList.title, m_parentWidget);
	const int numOfCommands = commandList.commands.size();
	QAction *action = new QAction("test", menu);
	int whichSection = 0;

	// get left margin of the menu (to be added to the minimum width of the menu)
	menu->addAction(action);
	QFont actionFont = action->font();
	actionFont.setPointSize(actionFont.pointSize() - 1);
	QFontMetrics actionFontMetrics(actionFont);
	int menuLeftMargin = menu->width() - actionFontMetrics.boundingRect(action->text()).width();
	menu->removeAction(action);
	int menuMinimumWidth = 0;

	for (int i = 0; i < numOfCommands; ++i)
	{
		const QString name = commandList.commands.at(i).name;
		if (name.isEmpty()) // add separator or submenu
		{
			if (commandList.commands.at(i).type == 0)
			{
				action = new QAction(menu);
				action->setSeparator(true);
				menu->addAction(action);
			}
			else // type == -1, so add submenu; this assumes that the i-th command with type == -1 corresponds with the i-th submenu (see getCommands())
			{
				menu->addMenu(getMenu(commandList.children.at(whichSection)));
				++whichSection;
			}
		}
		else // add command
		{
			action = new QAction(name, menu);
			action->setData(commandList.commands.at(i).number); // link to the corresponding item in m_tikzCommandsList
			action->setStatusTip(commandList.commands.at(i).description);
			menuMinimumWidth = qMax(menuMinimumWidth, actionFontMetrics.boundingRect(commandList.commands.at(i).description).width());
			connect(action, SIGNAL(triggered()), this, SLOT(insertTag()));
			connect(action, SIGNAL(hovered()), this, SLOT(updateDescriptionMenuItem()));
			menu->addAction(action);
		}
	}

	// if the menu does not only contain submenus, then we add a menu item
	// at the bottom of the menu which shows the description of the currently
	// highlighted menu item
	if (whichSection < menu->actions().size())
	{
		action = new QAction(this);
		action->setSeparator(true);
		menu->addAction(action);

		action = new QAction(this);
		QFont actionFont = action->font();
		actionFont.setPointSize(actionFont.pointSize() - 1);
		action->setFont(actionFont);
		connect(action, SIGNAL(triggered()), this, SLOT(insertTag()));
		menu->addAction(action);

		// make sure that the menu width does not change when the content
		// of the above menu item changes
		menu->setMinimumWidth(menuMinimumWidth + menuLeftMargin);
	}

	return menu;
}