From d77d8fba437f9a17ae6fe59241b6e45a24a353cd Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Thu, 5 Jul 2012 11:21:46 +0200 Subject: [PATCH] Cleanup popup menu position. The menus on toolbuttons are not placed next to the menu, like other toolbuttons, instead of being opened at mouse position. The code is similar to the code in QToolButton implementation. Change-Id: I7893b0badcd35e00d0c6a27749d2bcf0b6f1d44b Reviewed-by: Eike Ziller --- src/libs/utils/fancylineedit.cpp | 38 +++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/libs/utils/fancylineedit.cpp b/src/libs/utils/fancylineedit.cpp index f1b7d807ad4..1a36faa46e2 100644 --- a/src/libs/utils/fancylineedit.cpp +++ b/src/libs/utils/fancylineedit.cpp @@ -44,6 +44,39 @@ #include #include #include +#include + +/*! Opens a menu at the specified widget position. + * This functions computes the position where to show the menu, and opens it with + * QMenu::exec(). + * \param menu The menu to open + * \param widget The widget next to which to open the menu + */ +static void execMenuAtWidget(QMenu *menu, QWidget *widget) +{ + QPoint p; + QRect screen = qApp->desktop()->availableGeometry(widget); + QSize sh = menu->sizeHint(); + QRect rect = widget->rect(); + if (widget->isRightToLeft()) { + if (widget->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) { + p = widget->mapToGlobal(rect.bottomRight()); + } else { + p = widget->mapToGlobal(rect.topRight() - QPoint(0, sh.height())); + } + p.rx() -= sh.width(); + } else { + if (widget->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) { + p = widget->mapToGlobal(rect.bottomLeft()); + } else { + p = widget->mapToGlobal(rect.topLeft() - QPoint(0, sh.height())); + } + } + p.rx() = qMax(screen.left(), qMin(p.x(), screen.right() - sh.width())); + p.ry() += 1; + + menu->exec(p); +} /*! \class Utils::FancyLineEdit @@ -111,8 +144,7 @@ bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event) case QEvent::FocusIn: if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) { m_lineEdit->setFocus(); - m_menu[buttonIndex]->exec(m_iconbutton[buttonIndex]->mapToGlobal( - m_iconbutton[buttonIndex]->rect().center())); + execMenuAtWidget(m_menu[buttonIndex], m_iconbutton[buttonIndex]); return true; } default: @@ -172,7 +204,7 @@ void FancyLineEdit::iconClicked() if (index == -1) return; if (d->m_menu[index]) { - d->m_menu[index]->exec(QCursor::pos()); + execMenuAtWidget(d->m_menu[index], button); } else { emit buttonClicked((Side)index); if (index == Left)