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)