diff options
author | J-P Nurmi <jpnurmi@digia.com> | 2014-09-23 15:50:55 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@digia.com> | 2014-09-30 17:45:11 +0200 |
commit | 7909cd1e27996a475a2d111dba21210c4af947ad (patch) | |
tree | fb5900c697ea321ae89b33f546302fa91a0bca15 /src | |
parent | 83669a5a58d42e9ed09a3c5c39145b8a833fd87a (diff) | |
download | qtquickcontrols-7909cd1e27996a475a2d111dba21210c4af947ad.tar.gz |
ToolBar: add support for a stylable menu button
Change-Id: I75021d5a4b54104cccaf5988f0bc79efce3c4c9f
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/controls/Private/ToolMenuButton.qml | 125 | ||||
-rw-r--r-- | src/controls/Private/private.pri | 1 | ||||
-rw-r--r-- | src/controls/Private/qmldir | 1 | ||||
-rw-r--r-- | src/controls/Styles/Base/ToolBarStyle.qml | 15 | ||||
-rw-r--r-- | src/controls/ToolBar.qml | 19 |
5 files changed, 159 insertions, 2 deletions
diff --git a/src/controls/Private/ToolMenuButton.qml b/src/controls/Private/ToolMenuButton.qml new file mode 100644 index 00000000..111b6753 --- /dev/null +++ b/src/controls/Private/ToolMenuButton.qml @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.4 +import QtQuick.Controls 1.3 +import QtQuick.Controls.Private 1.0 + +FocusScope { + id: button + + property Menu menu + readonly property bool pressed: behavior.containsPress || behavior.keyPressed + readonly property alias hovered: behavior.containsMouse + + property alias panel: loader.sourceComponent + property alias __panel: loader.item + + activeFocusOnTab: true + Accessible.role: Accessible.Button + implicitWidth: __panel ? __panel.implicitWidth : 0 + implicitHeight: __panel ? __panel.implicitHeight : 0 + + Loader { + id: loader + anchors.fill: parent + property QtObject styleData: QtObject { + readonly property alias pressed: button.pressed + readonly property alias hovered: button.hovered + readonly property alias activeFocus: button.activeFocus + } + onStatusChanged: if (status === Loader.Error) console.error("Failed to load Style for", button) + } + + Keys.onPressed: { + if (event.key === Qt.Key_Space && !event.isAutoRepeat && !behavior.keyPressed) + behavior.keyPressed = true + } + Keys.onReleased: { + if (event.key === Qt.Key_Space && !event.isAutoRepeat && behavior.keyPressed) + behavior.keyPressed = false + } + onFocusChanged: { + if (!focus) + behavior.keyPressed = false + } + onPressedChanged: { + if (!Settings.hasTouchScreen && !pressed && menu) + popupMenuTimer.start() + } + + MouseArea { + id: behavior + property bool keyPressed: false + + anchors.fill: parent + enabled: !keyPressed + hoverEnabled: true + + onReleased: { + if (Settings.hasTouchScreen && containsMouse && menu) + popupMenuTimer.start() + } + + Timer { + id: popupMenuTimer + interval: 10 + onTriggered: { + behavior.keyPressed = false + if (Qt.application.layoutDirection === Qt.RightToLeft) + menu.__popup(Qt.rect(button.width, button.height, 0, 0), 0) + else + menu.__popup(Qt.rect(0, 0, button.width, button.height), 0) + } + } + } + + Binding { + target: menu + property: "__minimumWidth" + value: button.width + } + + Binding { + target: menu + property: "__visualItem" + value: button + } +} diff --git a/src/controls/Private/private.pri b/src/controls/Private/private.pri index 85e5c3ad..e69d0a29 100644 --- a/src/controls/Private/private.pri +++ b/src/controls/Private/private.pri @@ -60,6 +60,7 @@ PRIVATE_QML_FILES += \ $$PWD/EditMenu.qml \ $$PWD/EditMenu_base.qml \ $$PWD/EditMenu_ios.qml \ + $$PWD/ToolMenuButton.qml \ $$PWD/qmldir QML_FILES += $$PRIVATE_QML_FILES diff --git a/src/controls/Private/qmldir b/src/controls/Private/qmldir index 103d8347..82e1a985 100644 --- a/src/controls/Private/qmldir +++ b/src/controls/Private/qmldir @@ -32,3 +32,4 @@ TextInputWithHandles 1.0 TextInputWithHandles.qml EditMenu 1.0 EditMenu.qml EditMenu_base 1.0 EditMenu_base.qml EditMenu_ios 1.0 EditMenu_ios.qml +ToolMenuButton 1.0 ToolMenuButton.qml diff --git a/src/controls/Styles/Base/ToolBarStyle.qml b/src/controls/Styles/Base/ToolBarStyle.qml index 7a89cefb..5a6c3570 100644 --- a/src/controls/Styles/Base/ToolBarStyle.qml +++ b/src/controls/Styles/Base/ToolBarStyle.qml @@ -104,6 +104,21 @@ Style { } } + /*! This defines the menu button appearance on platforms + that have a unified tool bar and menu bar. + + \since QtQuick.Controls.Styles 1.3 + + The following read-only properties are available within the scope + of the menu button delegate: + \table + \row \li \b {styleData.pressed} : bool \li Whether the button is pressed. + \row \li \b {styleData.hovered} : bool \li Whether the button is hovered. + \row \li \b {styleData.activeFocus} : bool \li Whether the button has active focus. + \endtable + */ + property Component menuButton: null + property Component panel: Loader { sourceComponent: background } diff --git a/src/controls/ToolBar.qml b/src/controls/ToolBar.qml index 8498860c..8fed257a 100644 --- a/src/controls/ToolBar.qml +++ b/src/controls/ToolBar.qml @@ -115,6 +115,9 @@ FocusScope { /*! \internal */ default property alias __content: container.data + /*! \internal */ + property var __menu + /*! \qmlproperty Item ToolBar::contentItem @@ -148,7 +151,7 @@ FocusScope { anchors.topMargin: topMargin anchors.leftMargin: leftMargin - anchors.rightMargin: rightMargin + anchors.rightMargin: rightMargin + buttonLoader.active ? buttonLoader.width : 0 anchors.bottomMargin: bottomMargin property int topMargin: __style ? __style.padding.top : 0 @@ -163,5 +166,17 @@ FocusScope { property real layoutHeight: layoutItem ? (layoutItem.implicitHeight || layoutItem.height) + (layoutItem.anchors.fill ? layoutItem.anchors.topMargin + layoutItem.anchors.bottomMargin : 0) : 0 - }] + }, + Loader { + id: buttonLoader + anchors.right: parent.right + anchors.rightMargin: container.rightMargin + anchors.verticalCenter: parent.verticalCenter + sourceComponent: ToolMenuButton { + menu: toolbar.__menu + panel: toolbar.__style.menuButton || null + } + active: !!__menu && __menu.items.length > 0 && !!__style.menuButton + } + ] } |