Debian Codegraph @

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

raw
1 /*
2 * Copyright 2012 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 1.2
19 import Lomiri.Components.ListItems 1.0
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 visible: enabled && ((action === undefined) || action.visible)
124 height: visible ? implicitHeight : 0
125 }
126
127 grabDismissAreaEvents: false
128
129 Column {
130 id: body
131 anchors {
132 left: parent.left
133 top: parent.top
134 right: parent.right
135 }
136
137 property bool isActionsObject: (popover.actions !== undefined) &&
138 popover.actions.hasOwnProperty("actions")
139
140 Repeater {
141 id: repeater
142 model: body.isActionsObject ? popover.actions.children : popover.actions
143 Loader {
144 width: parent.width
145 height: modelData.visible ? item.height : 0
146 sourceComponent: delegate
147 onStatusChanged: {
148 if (item && status == Loader.Ready) {
149 // set model data
150 if (item.hasOwnProperty("action")) {
151 item.action = modelData;
152 item.objectName = item.action.objectName + '_button';
153 }
154 if (item.hasOwnProperty("refModelData"))
155 item.refModelData = modelData;
156 if (item.hasOwnProperty("modelData"))
157 item.modelData = modelData;
158 // auto-connect trigger
159 // if the delegate is a list item, hide divider of the last one
160 if (item.hasOwnProperty("showDivider"))
161 item.showDivider = index < (repeater.count - 1);
162 }
163 }
164 }
165 }
166 }
167 }