diff --git a/src/controls/private/ActionsMenu.qml b/src/controls/private/ActionsMenu.qml new file mode 100644 index 00000000..e26892dc --- /dev/null +++ b/src/controls/private/ActionsMenu.qml @@ -0,0 +1,48 @@ +/* + * Copyright 2018 Aleix Pol Gonzalez + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.3 +import QtQuick.Controls 2.0 as Controls + +Controls.Menu +{ + id: theMenu + property alias actions: actionsRepeater.model + property Component submenuComponent + + Repeater { + id: actionsRepeater + + delegate: Controls.MenuItem { + id: menuItem + text: model.text +// TODO: enable when we depend on Qt 5.10 +// icon.name: model.iconName + visible: model.visible + enabled: modelData.enabled + checkable: modelData.checkable + checked: modelData.checked + onTriggered: { + modelData.trigger() + } + + readonly property var ourMenu: theMenu.submenuComponent ? theMenu.submenuComponent.createObject(menuItem, { actions: modelData.children }) : null + } + } +} diff --git a/src/controls/private/PrivateActionToolButton.qml b/src/controls/private/PrivateActionToolButton.qml index c6dfa558..6b3c2e5e 100644 --- a/src/controls/private/PrivateActionToolButton.qml +++ b/src/controls/private/PrivateActionToolButton.qml @@ -1,78 +1,90 @@ /* * Copyright 2016 Marco Martin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import QtQuick 2.6 import QtQuick.Layouts 1.2 import QtQuick.Controls 2.0 as Controls import org.kde.kirigami 2.2 Controls.ToolButton { id: control implicitWidth: showText ? Math.max(background.implicitWidth, layout.implicitWidth + 16) : height implicitHeight: background.implicitHeight hoverEnabled: true //TODO: replace with upstream action when we can property Action kirigamiAction property bool showText: true //we need our own text delegate text: "" checkable: kirigamiAction && kirigamiAction.checkable checked: kirigamiAction && kirigamiAction.checked enabled: kirigamiAction && kirigamiAction.enabled opacity: enabled ? 1 : 0.4 visible: kirigamiAction && kirigamiAction.visible onClicked: { if (kirigamiAction) { kirigamiAction.trigger(); } + if (menu.count>0 && !menu.visible) { + menu.popup(control, 0, control.height) + } + } + + ActionsMenu { + id: menu + y: control.height + actions: control.kirigamiAction ? control.kirigamiAction.children : "" + submenuComponent: Component { + ActionsMenu {} + } } flat: true contentItem: MouseArea { hoverEnabled: true onPressed: mouse.accepted = false Theme.colorSet: checked ? Theme.Selection : Theme.Window Theme.inherit: false RowLayout { id: layout anchors.centerIn: parent Icon { Layout.minimumWidth: 22 Layout.minimumHeight: 22 source: control.kirigamiAction ? (control.kirigamiAction.icon ? control.kirigamiAction.icon.name : control.kirigamiAction.iconName) : "" visible: control.kirigamiAction && control.kirigamiAction.iconName != "" color: control.kirigamiAction && control.kirigamiAction.icon && control.kirigamiAction.icon.color.a > 0 ? control.kirigamiAction.icon.color : Qt.rgba(0, 0, 0, 0) } Controls.Label { text: kirigamiAction ? kirigamiAction.text : "" visible: control.showText } } } Controls.ToolTip { visible: control.hovered text: kirigamiAction ? (kirigamiAction.tooltip.length ? kirigamiAction.tooltip : kirigamiAction.text) : "" delay: 1000 timeout: 5000 y: control.height } } diff --git a/tests/actionsMenu.qml b/tests/actionsMenu.qml new file mode 100644 index 00000000..39fd5cca --- /dev/null +++ b/tests/actionsMenu.qml @@ -0,0 +1,68 @@ +/* + * Copyright 2016 Aleix Pol Gonzalez + * Copyright 2016 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.7 +import QtQuick.Controls 2.0 +import org.kde.kirigami 2.2 as Kirigami + +Kirigami.ApplicationWindow +{ + id: main + + header: Kirigami.ToolBarApplicationHeader {} + + pageStack.initialPage: Kirigami.Page { + + Button { + text: "button" + onClicked: menu.popup() + Menu { + id: menu + + MenuItem { text: "xxx" } + MenuItem { text: "xxx" } + MenuItem { + text: "yyy" + Menu { + MenuItem { text: "yyy" } + } + } + } + } + + contextualActions: [ + Kirigami.Action { + text: "fua" + icon.name: "kalgebra" + + Kirigami.Action { text: "xxx"; onTriggered: console.log("xxx") } + Kirigami.Action { text: "xxx"; onTriggered: console.log("xxx") } + Kirigami.Action { text: "xxx"; onTriggered: console.log("xxx") } + Kirigami.Action { + text: "yyy" + Kirigami.Action { text: "yyy" } + Kirigami.Action { text: "yyy" } + Kirigami.Action { text: "yyy" } + Kirigami.Action { text: "yyy" } + } + } + ] + } +}