summaryrefslogtreecommitdiff
path: root/src/controls
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@digia.com>2014-09-26 10:56:26 +0200
committerJ-P Nurmi <jpnurmi@digia.com>2014-09-30 15:27:06 +0200
commite1ba563285270fd20e0aa8b9692b1bf1686b0128 (patch)
tree642863bfafe1287b45a10a14d3839a8951a16a41 /src/controls
parent00665083ce5ccccab00b852164f03fe1efa9c504 (diff)
downloadqtquickcontrols-e1ba563285270fd20e0aa8b9692b1bf1686b0128.tar.gz
qquickmenu: add support for providing a target rect to __popup()
Using a target rect as menu location instead of a position has been supported in QPlatformMenu for a while. The reason for specifying a rect instead of a position is that then the OS can decide if the popup should be placed above or below the target rect so that it fits inside the screen. A typical example is when showing an edit menu around a text selection. If the selection (target rectangle) is far up on the screen, the popup (with arrow) will be placed below the selection instead of above, which is the normal. Change-Id: Ie6cd6a92f1d9ef480c1e455960021c7f18f86569 Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Diffstat (limited to 'src/controls')
-rw-r--r--src/controls/Button.qml4
-rw-r--r--src/controls/ComboBox.qml4
-rw-r--r--src/controls/MenuBar.qml4
-rw-r--r--src/controls/Private/EditMenu_base.qml2
-rw-r--r--src/controls/Private/EditMenu_ios.qml14
-rw-r--r--src/controls/Private/MenuContentItem.qml2
-rw-r--r--src/controls/qquickmenu.cpp18
-rw-r--r--src/controls/qquickmenu_p.h2
8 files changed, 25 insertions, 25 deletions
diff --git a/src/controls/Button.qml b/src/controls/Button.qml
index bd862d45..f21d34c8 100644
--- a/src/controls/Button.qml
+++ b/src/controls/Button.qml
@@ -124,9 +124,9 @@ BasicButton {
onTriggered: {
__behavior.keyPressed = false
if (Qt.application.layoutDirection === Qt.RightToLeft)
- menu.__popup(button.width, button.height, 0)
+ menu.__popup(Qt.rect(button.width, button.height, 0, 0), 0)
else
- menu.__popup(0, button.height, 0)
+ menu.__popup(Qt.rect(0, button.height, 0, 0), 0)
}
}
}
diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml
index 55161d39..5252b4e5 100644
--- a/src/controls/ComboBox.qml
+++ b/src/controls/ComboBox.qml
@@ -633,9 +633,9 @@ Control {
items[__selectedIndex].checked = true
__currentIndex = comboBox.currentIndex
if (Qt.application.layoutDirection === Qt.RightToLeft)
- __popup(comboBox.width, y, isPopup ? __selectedIndex : 0)
+ __popup(Qt.rect(comboBox.width, y, 0, 0), isPopup ? __selectedIndex : 0)
else
- __popup(0, y, isPopup ? __selectedIndex : 0)
+ __popup(Qt.rect(0, y, 0, 0), isPopup ? __selectedIndex : 0)
}
}
diff --git a/src/controls/MenuBar.qml b/src/controls/MenuBar.qml
index 9c9fa698..5d90f91d 100644
--- a/src/controls/MenuBar.qml
+++ b/src/controls/MenuBar.qml
@@ -252,8 +252,8 @@ MenuBarPrivate {
if (d.openedMenuIndex === index) {
if (__menuItem.__usingDefaultStyle)
__menuItem.style = d.style.menuStyle
- __menuItem.__popup(row.LayoutMirroring.enabled ? menuItemLoader.width : 0,
- menuBarLoader.height - d.heightPadding, 0)
+ __menuItem.__popup(Qt.rect(row.LayoutMirroring.enabled ? menuItemLoader.width : 0,
+ menuBarLoader.height - d.heightPadding, 0, 0), 0)
if (d.preselectMenuItem)
__menuItem.__currentIndex = 0
} else {
diff --git a/src/controls/Private/EditMenu_base.qml b/src/controls/Private/EditMenu_base.qml
index aed4e464..597541bc 100644
--- a/src/controls/Private/EditMenu_base.qml
+++ b/src/controls/Private/EditMenu_base.qml
@@ -106,7 +106,7 @@ Item {
if (control.menu) {
getMenuInstance().__dismissMenu();
var menuPos = mapToItem(null, mouse.x, mouse.y)
- getMenuInstance().__popup(menuPos.x, menuPos.y, -1, MenuPrivate.EditMenu);
+ getMenuInstance().__popup(Qt.rect(menuPos.x, menuPos.y, 0, 0), -1, MenuPrivate.EditMenu);
}
}
}
diff --git a/src/controls/Private/EditMenu_ios.qml b/src/controls/Private/EditMenu_ios.qml
index 1fe92e46..aeb21b03 100644
--- a/src/controls/Private/EditMenu_ios.qml
+++ b/src/controls/Private/EditMenu_ios.qml
@@ -173,15 +173,13 @@ Item {
&& (!cursorHandle.pressed && !selectionHandle.pressed)
&& (!flickable || !flickable.moving)
&& (cursorHandle.delegate)) {
- // center menu on top of selection:
- var r1 = input.positionToRectangle(input.selectionStart);
- var r2 = input.cursorRectangle;
- var xMin = Math.min(r1.x, r2.x);
- var xMax = Math.max(r1.x, r2.x);
- var centerX = xMin + ((xMax - xMin) / 2);
- var popupPos = input.mapToItem(null, centerX, r1.y);
+ var p1 = input.positionToRectangle(input.selectionStart);
+ var p2 = input.positionToRectangle(input.selectionEnd);
+ var topLeft = input.mapToItem(null, p1.x, p1.y);
+ var size = Qt.size(p2.x - p1.x + p1.width, p2.y - p1.y + p1.height)
+ var targetRect = Qt.rect(topLeft.x, topLeft.y, size.width, size.height);
getMenuInstance().__dismissMenu();
- getMenuInstance().__popup(popupPos.x, popupPos.y, -1, MenuPrivate.EditMenu);
+ getMenuInstance().__popup(targetRect, -1, MenuPrivate.EditMenu);
} else {
getMenuInstance().__dismissMenu();
}
diff --git a/src/controls/Private/MenuContentItem.qml b/src/controls/Private/MenuContentItem.qml
index 62e5d033..a503d1d4 100644
--- a/src/controls/Private/MenuContentItem.qml
+++ b/src/controls/Private/MenuContentItem.qml
@@ -207,7 +207,7 @@ Loader {
if (__menu.__currentIndex === __menuItemIndex) {
if (__menuItem.__usingDefaultStyle)
__menuItem.style = __menu.style
- __menuItem.__popup(menuFrameLoader.width - (d.style.submenuOverlap + d.style.padding.right), -d.style.padding.top, -1)
+ __menuItem.__popup(Qt.rect(menuFrameLoader.width - (d.style.submenuOverlap + d.style.padding.right), -d.style.padding.top, 0, 0), -1)
}
} else {
openMenuTimer.start()
diff --git a/src/controls/qquickmenu.cpp b/src/controls/qquickmenu.cpp
index f96a326e..69328349 100644
--- a/src/controls/qquickmenu.cpp
+++ b/src/controls/qquickmenu.cpp
@@ -377,10 +377,10 @@ void QQuickMenu::popup()
if (QQuickWindow *parentWindow = findParentWindow())
mousePos = parentWindow->mapFromGlobal(mousePos);
- __popup(mousePos.x(), mousePos.y());
+ __popup(QRectF(mousePos.x(), mousePos.y(), 0, 0));
}
-void QQuickMenu::__popup(qreal x, qreal y, int atItemIndex, MenuType menuType)
+void QQuickMenu::__popup(const QRectF &targetRect, int atItemIndex, MenuType menuType)
{
if (popupVisible()) {
__closeMenu();
@@ -401,14 +401,16 @@ void QQuickMenu::__popup(qreal x, qreal y, int atItemIndex, MenuType menuType)
parentWindow = renderWindow; // may not be a QQuickWindow anymore (happens when using QQuickWidget)
if (m_platformMenu) {
- QPointF screenPosition(x + m_xOffset, y + m_yOffset);
+ QRectF globalTargetRect = targetRect.translated(m_xOffset, m_yOffset);
if (visualItem()) {
- if (qGuiApp->isRightToLeft())
- screenPosition.rx() -= qMax(static_cast<qreal>(m_minimumWidth), m_menuContentItem->width());
- screenPosition = visualItem()->mapToScene(screenPosition);
+ if (qGuiApp->isRightToLeft()) {
+ qreal w = qMax(static_cast<qreal>(m_minimumWidth), m_menuContentItem->width());
+ globalTargetRect.moveLeft(w - targetRect.x() - targetRect.width());
+ }
+ globalTargetRect = visualItem()->mapRectToScene(globalTargetRect);
}
m_platformMenu->setMenuType(QPlatformMenu::MenuType(menuType));
- m_platformMenu->showPopup(parentWindow, screenPosition.toPoint(), atItem ? atItem->platformItem() : 0);
+ m_platformMenu->showPopup(parentWindow, globalTargetRect.toRect(), atItem ? atItem->platformItem() : 0);
} else {
m_popupWindow = new QQuickMenuPopupWindow();
if (visualItem())
@@ -421,7 +423,7 @@ void QQuickMenu::__popup(qreal x, qreal y, int atItemIndex, MenuType menuType)
connect(m_popupWindow, SIGNAL(visibleChanged(bool)), this, SLOT(windowVisibleChanged(bool)));
connect(m_popupWindow, SIGNAL(geometryChanged()), this, SIGNAL(__popupGeometryChanged()));
- m_popupWindow->setPosition(x + m_xOffset, y + m_yOffset);
+ m_popupWindow->setPosition(targetRect.x() + m_xOffset, targetRect.y() + targetRect.height() + m_yOffset);
m_popupWindow->show();
}
}
diff --git a/src/controls/qquickmenu_p.h b/src/controls/qquickmenu_p.h
index 943c45bf..f21f2ce0 100644
--- a/src/controls/qquickmenu_p.h
+++ b/src/controls/qquickmenu_p.h
@@ -83,7 +83,7 @@ public:
Q_INVOKABLE void removeItem(QQuickMenuBase *);
Q_INVOKABLE void clear();
- Q_INVOKABLE void __popup(qreal x, qreal y, int atActionIndex = -1, MenuType menuType = DefaultMenu);
+ Q_INVOKABLE void __popup(const QRectF &targetRect, int atItemIndex = -1, MenuType menuType = DefaultMenu);
public Q_SLOTS:
void __closeMenu();