Debian Codegraph @

src/imports/Components/Popups/2.0/ActionSelectionPopover.qml

raw
1 /*
2 * Copyright 2015 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 Lomiri.Components
19 import Lomiri.Components.ListItems
20
21 /*!
22 \qmltype ActionSelectionPopover
23 \inherits Popover
24 \inqmlmodule Lomiri.Components.Popups 1.0
25 \ingroup lomiri-popups
26 \brief A special popover presenting actions to the user. The popover is closed
27 automatically when the action is chosen.
28
29 The actions can be given either using ActionList or as an array of action
30 objects. The visualization of the actions is realized either using the default
31 visualization, which is realised using list items having centered text, or
32 using the component given as delegate. The actions are triggered with the
33 specified target as parameter.
34
35 The popover recognizes the following properties from the delegate:
36 \list
37 \li data properties like \b modelData, \b refModelData or \b action. When either of these is detected
38 the popover will set their value to the action object to be visualized.
39 \li trigger signals like \b clicked, \b accepted or \b triggered. When these
40 are detected, the popover will automatically connect those to the action's trigger.
41 \endlist
42
43 An example presenting list of actions using ActionList:
44 \qml
45 ActionSelectionPopover {
46 delegate: ListItems.Standard {
47 text: action.text
48 }
49 actions: ActionList {
50 Action {
51 text: "Action one"
52 onTriggered: print(text)
53 }
54 Action {
55 text: "Action two"
56 onTriggered: print(text)
57 }
58 }
59 }
60 \endqml
61
62 An array of actions can be used when the actions to be presented are reused
63 from a set of predefined actions:
64 \qml
65 Item {
66 Action {
67 id: action1
68 text: "Action one"
69 onTriggered: print(text)
70 }
71 Action {
72 id: action2
73 text: "Action two"
74 onTriggered: print(text)
75 }
76 Action {
77 id: action3
78 text: "Action three"
79 onTriggered: print(text)
80 }
81 ActionListPopover {
82 actions: [action1, action3]
83 delegate: ListItems.Standard {
84 text: action.text
85 }
86 }
87 }
88 \endqml
89 */
90
91 Popover {
92 id: popover
93
94 /*!
95 The property holds the object on which the action will be performed.
96 */
97 property Item target
98
99 /*!
100 The property holds the list of actions to be presented. Each action
101 triggered will use the actionHost as caller.
102 */
103 property var actions
104
105 /*!
106 The property holds the delegate to visualize the action. The delegate should
107 define one of the data holder properties recognized by the popover in order
108 to access action data.
109 */
110 property Component delegate: Empty {
111 id: listItem
112 Label {
113 text: listItem.text
114 anchors {
115 verticalCenter: parent.verticalCenter
116 horizontalCenter: parent.horizontalCenter
117 }
118 wrapMode: Text.Wrap
119 color: theme.palette.normal.overlayText
120 }
121 /*! \internal */
122 onTriggered: popover.hide()
123 // FIXME: Disabled actions should be visible, but greyed-out.
124 // See bug 1612579. Tests will also need to be updated.
125 visible: enabled && ((action === undefined) || action.visible)
126 height: visible ? implicitHeight : 0
127 }
128
129 grabDismissAreaEvents: false
130
131 Column {
132 id: body
133 anchors {
134 left: parent.left
135 top: parent.top
136 right: parent.right
137 }
138
139 property bool isActionsObject: (popover.actions !== undefined) &&
140 popover.actions.hasOwnProperty("actions")
141
142 Repeater {
143 id: repeater
144 model: body.isActionsObject ? popover.actions.children : popover.actions
145 Loader {
146 width: body.width
147 sourceComponent: delegate
148 onStatusChanged: {
149 if (item && status == Loader.Ready) {
150 // set model data
151 if (item.hasOwnProperty("action")) {
152 item.action = modelData;
153 item.objectName = item.action.objectName + '_button';
154 }
155 if (item.hasOwnProperty("refModelData"))
156 item.refModelData = modelData;
157 if (item.hasOwnProperty("modelData"))
158 item.modelData = modelData;
159 // auto-connect trigger
160 // if the delegate is a list item, hide divider of the last one
161 if (item.hasOwnProperty("showDivider"))
162 item.showDivider = index < (repeater.count - 1);
163 }
164 }
165 }
166 }
167 }
168 }