Lomiri
MenuItem.qml
1 /*
2  * Copyright 2016 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import QtQuick.Layouts 1.1
19 import Lomiri.Components 1.3
20 
21 ActionItem {
22  id: root
23  implicitHeight: units.gu(5)
24  implicitWidth: requiredWidth
25 
26  property var menuData: undefined
27 
28  readonly property real requiredWidth: {
29  var val = 0;
30  val += units.gu(1) + flagGutter.width;
31  if (iconSource != "") {
32  val += units.gu(1) + icon.width
33  }
34  val += units.gu(1) + title.contentWidth;
35  if (hasSubmenu) {
36  val += units.gu(1) + chevronIcon.width;
37  } else if (menuData && menuData.shortcut != undefined) {
38  val += units.gu(3) + shortcutLabel.contentWidth;
39  }
40  return val + units.gu(1);
41  }
42 
43  readonly property bool hasSubmenu: menuData ? menuData.hasSubmenu : false
44  readonly property bool _checked : action && action.checkable ? action.checked : false
45 
46  enabled: menuData ? menuData.sensitive : false
47 
48  action: Action {
49  enabled: root.enabled
50 
51  // FIXME - SDK Action:text modifies menu text with html underline for mnemonic
52  text: menuData ? menuData.label.replace("_", "&").replace("<u>", "&").replace("</u>", "") : ""
53  checkable: menuData && (menuData.isCheck || menuData.isRadio)
54  checked: menuData && menuData.isToggled
55  }
56 
57  width: {
58  if (!parent) return implicitWidth;
59  if (parent.width > implicitWidth) return parent.width;
60  return implicitWidth;
61  }
62 
63  Keys.onRightPressed: {
64  if (hasSubmenu) {
65  root.trigger();
66  } else {
67  event.accepted = false;
68  }
69  }
70  Keys.onReturnPressed: {
71  root.trigger();
72  }
73  Keys.onEnterPressed: {
74  root.trigger();
75  }
76 
77  RowLayout {
78  id: row
79  spacing: units.gu(1)
80  anchors.left: root.left
81  anchors.right: root.right
82  anchors.leftMargin: units.gu(1)
83  anchors.rightMargin: units.gu(1)
84  anchors.verticalCenter: root.verticalCenter
85 
86  Item {
87  Layout.minimumWidth: units.gu(1.5)
88  Layout.minimumHeight: units.gu(1.5)
89 
90  Icon {
91  id: flagGutter
92  width: units.gu(1.5)
93  height: units.gu(1.5)
94  visible: _checked
95  name: "tick"
96  }
97  }
98 
99  Icon {
100  id: icon
101  width: units.gu(2)
102  height: units.gu(2)
103 
104  visible: root.iconSource != "" || false
105  source: root.iconSource || ""
106  }
107 
108  RowLayout {
109  spacing: units.gu(3)
110 
111  Label {
112  id: title
113  elide: Text.ElideNone
114  wrapMode: Text.NoWrap
115  clip: true
116  color: enabled ? theme.palette.normal.overlayText : theme.palette.disabled.overlayText
117  Layout.fillWidth: true
118 
119  text: root.text ? root.text : ""
120  }
121 
122  Label {
123  id: shortcutLabel
124  elide: Text.ElideNone
125  wrapMode: Text.NoWrap
126  clip: true
127  color: enabled ? theme.palette.normal.overlaySecondaryText :
128  theme.palette.disabled.overlaySecondaryText
129 
130  visible: menuData && menuData.shortcut != undefined && !root.hasSubmenu && QuickUtils.keyboardAttached
131  text: menuData && menuData.shortcut ? menuData.shortcut : ""
132  }
133  }
134 
135  Icon {
136  id: chevronIcon
137  width: units.gu(2)
138  height: units.gu(2)
139  color: enabled ? theme.palette.normal.overlayText :
140  theme.palette.disabled.overlayText
141 
142  visible: root.hasSubmenu
143  name: "toolkit_chevron-ltr_2gu"
144  }
145  }
146 }