From 4059b38a8f90dc58558e0c3f6fcf7ad493188230 Mon Sep 17 00:00:00 2001
From: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Date: Mon, 29 Apr 2013 12:22:36 +0200
Subject: [PATCH] Menu: Add __xOffset, __yOffset properties

These are for tweaking the popup position according to the current
style. First usage goes to ComboBox.

Also prepares for proper support of 'small' and 'mini' style hints.

Change-Id: I7bc682d5d82bf6fa7a36916a9d4c64dfb2bdb56e
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
---
 src/controls/ComboBox.qml            |  9 +++------
 src/controls/qquickmenu.cpp          | 18 +++++++++++++++---
 src/controls/qquickmenu_p.h          |  9 +++++++++
 src/private/qquickstyleitem.cpp      | 11 ++++++++++-
 src/styles/Desktop/ComboBoxStyle.qml | 10 ++++------
 5 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml
index f34f49f5e..e09e276b4 100644
--- a/src/controls/ComboBox.qml
+++ b/src/controls/ComboBox.qml
@@ -93,6 +93,8 @@ Control {
     readonly property bool __pressed: mouseArea.pressed && mouseArea.containsMouse || popup.__popupVisible
     /*! \internal */
     property alias __containsMouse: mouseArea.containsMouse
+    /*! \internal */
+    property var __popup: popup
 
     style: Qt.createComponent(Settings.theme() + "/ComboBoxStyle.qml", comboBox)
 
@@ -114,9 +116,6 @@ Control {
     Component.onCompleted: {
         if (currentIndex === -1)
             currentIndex = 0
-        if (Qt.platform.os === "mac") {
-            popup.y += 6
-        }
 
         popup.ready = true
         popup.resolveTextValue(textRole)
@@ -136,11 +135,9 @@ Control {
         property bool ready: false
         property bool isPopup: __panel ? __panel.popup : false
 
-        property int x: 0
         property int y: isPopup ? (comboBox.__panel.height - comboBox.__panel.implicitHeight) / 2.0 : comboBox.__panel.height
         __minimumWidth: comboBox.width
         __visualItem: comboBox
-        __font: __panel.font
 
         property ExclusiveGroup eg: ExclusiveGroup { id: eg }
 
@@ -199,7 +196,7 @@ Control {
             if (items[__selectedIndex])
                 items[__selectedIndex].checked = true
             __currentIndex = comboBox.currentIndex
-            __popup(x, y, isPopup ? __selectedIndex : 0)
+            __popup(0, y, isPopup ? __selectedIndex : 0)
         }
     }
 
diff --git a/src/controls/qquickmenu.cpp b/src/controls/qquickmenu.cpp
index 6ec15e23c..088e6ad5a 100644
--- a/src/controls/qquickmenu.cpp
+++ b/src/controls/qquickmenu.cpp
@@ -243,7 +243,9 @@ QQuickMenu::QQuickMenu(QObject *parent)
       m_popupWindow(0),
       m_menuContentItem(0),
       m_popupVisible(false),
-      m_containersCount(0)
+      m_containersCount(0),
+      m_xOffset(0),
+      m_yOffset(0)
 {
     connect(this, SIGNAL(__textChanged()), this, SIGNAL(titleChanged()));
 
@@ -290,6 +292,16 @@ void QQuickMenu::setFont(const QFont &arg)
         m_platformMenu->setFont(arg);
 }
 
+void QQuickMenu::setXOffset(qreal x)
+{
+    m_xOffset = x;
+}
+
+void QQuickMenu::setYOffset(qreal y)
+{
+    m_yOffset = y;
+}
+
 void QQuickMenu::setSelectedIndex(int index)
 {
     if (m_selectedIndex == index)
@@ -349,7 +361,7 @@ void QQuickMenu::__popup(qreal x, qreal y, int atItemIndex)
     QQuickWindow *parentWindow = findParentWindow();
 
     if (m_platformMenu) {
-        QPointF screenPosition(x, y);
+        QPointF screenPosition(x + m_xOffset, y + m_yOffset);
         if (visualItem())
             screenPosition = visualItem()->mapToScene(screenPosition);
         m_platformMenu->showPopup(parentWindow, screenPosition.toPoint(), atItem ? atItem->platformItem() : 0);
@@ -364,7 +376,7 @@ void QQuickMenu::__popup(qreal x, qreal y, int atItemIndex)
 
         connect(m_popupWindow, SIGNAL(visibleChanged(bool)), this, SLOT(windowVisibleChanged(bool)));
 
-        m_popupWindow->setPosition(x, y);
+        m_popupWindow->setPosition(x + m_xOffset, y + m_yOffset);
         m_popupWindow->show();
     }
 }
diff --git a/src/controls/qquickmenu_p.h b/src/controls/qquickmenu_p.h
index d2017d451..d57de1266 100644
--- a/src/controls/qquickmenu_p.h
+++ b/src/controls/qquickmenu_p.h
@@ -70,6 +70,8 @@ class QQuickMenu : public QQuickMenuText
     Q_PROPERTY(QQuickItem *__contentItem READ menuContentItem WRITE setMenuContentItem)
     Q_PROPERTY(int __minimumWidth READ minimumWidth WRITE setMinimumWidth)
     Q_PROPERTY(QFont __font WRITE setFont)
+    Q_PROPERTY(qreal __xOffset READ xOffset WRITE setXOffset)
+    Q_PROPERTY(qreal __yOffset READ yOffset WRITE setYOffset)
 
 public:
     Q_INVOKABLE void popup();
@@ -115,6 +117,11 @@ public:
 
     void setFont(const QFont &font);
 
+    qreal xOffset() const { return m_xOffset; }
+    void setXOffset(qreal);
+    qreal yOffset() const { return m_yOffset; }
+    void setYOffset(qreal);
+
     QQuickItem *menuContentItem() const { return m_menuContentItem; }
     bool popupVisible() const { return m_popupVisible; }
 
@@ -162,6 +169,8 @@ private:
     QQuickItem * m_menuContentItem;
     bool m_popupVisible;
     int m_containersCount;
+    qreal m_xOffset;
+    qreal m_yOffset;
 };
 
 QT_END_NAMESPACE
diff --git a/src/private/qquickstyleitem.cpp b/src/private/qquickstyleitem.cpp
index aafacead5..4cc0d42a9 100644
--- a/src/private/qquickstyleitem.cpp
+++ b/src/private/qquickstyleitem.cpp
@@ -440,10 +440,19 @@ void QQuickStyleItem::initStyleOption()
         if (!m_styleoption)
             m_styleoption = new QStyleOptionComboBox();
         QStyleOptionComboBox *opt = qstyleoption_cast<QStyleOptionComboBox*>(m_styleoption);
-        if (const QFont *font = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::PushButtonFont))
+        const QFont *font = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::PushButtonFont);
+        if (font)
             opt->fontMetrics = QFontMetrics(*font);
         opt->currentText = text();
         opt->editable = false;
+#ifdef Q_OS_MAC
+        if (m_properties["popup"].canConvert<QObject *>() && style() == "mac") {
+            QObject *popup = m_properties["popup"].value<QObject *>();
+            popup->setProperty("__yOffset", 6);
+            if (font)
+                popup->setProperty("__font", *font);
+        }
+#endif
     }
         break;
     case SpinBox: {
diff --git a/src/styles/Desktop/ComboBoxStyle.qml b/src/styles/Desktop/ComboBoxStyle.qml
index 720b659f7..83215a13d 100644
--- a/src/styles/Desktop/ComboBoxStyle.qml
+++ b/src/styles/Desktop/ComboBoxStyle.qml
@@ -44,12 +44,6 @@ import QtQuick.Controls.Private 1.0
 Style {
     property Component panel: Item {
         property int popup: styleItem.styleHint("comboboxpopup")
-        property font font: itemstyle.font
-        StyleItem {
-            id: itemstyle
-            elementType: "comboboxitem"
-            visible: false
-        }
 
         implicitWidth: 115
         implicitHeight: styleItem.implicitHeight
@@ -72,6 +66,10 @@ Style {
             hasFocus: control.activeFocus
             // contentHeight as in QComboBox
             contentHeight: Math.max(Math.ceil(textHeight("")), 14) + 2
+
+            properties: {
+                "popup": control.__popup
+            }
         }
     }
 
-- 
GitLab