Unity 8
PreviewActions.qml
1 /*
2  * Copyright (C) 2014,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 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU 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 Ubuntu.Components 1.3
19 
20 /*! This preview widget shows either one button, two buttons or one button
21  * and a combo button depending on the number of items in widgetData["actions"].
22  * For each of the items we recognize the fields "label", "icon" and "id".
23  */
24 
25 PreviewWidget {
26  id: root
27 
28  implicitHeight: row.height + units.gu(1)
29  readonly property var actions: root.widgetData ? root.widgetData["actions"] : null
30  readonly property real maxButtonWidth: (width - units.gu(1)) / 2
31 
32  Row {
33  id: row
34  anchors.right: parent.right
35 
36  spacing: units.gu(1)
37 
38  Loader {
39  id: loader
40  objectName: "loader"
41  readonly property bool button: root.actions && root.actions.length == 2
42  readonly property bool combo: root.actions && root.actions.length > 2
43  source: button ? "PreviewActionButton.qml" : (combo ? "PreviewActionCombo.qml" : "")
44  width: Math.min(root.maxButtonWidth, implicitWidth + units.gu(2))
45  onLoaded: {
46  if (button) {
47  item.data = Qt.binding(function() { return root.actions[1]; });
48  } else if (combo) {
49  item.model = Qt.binding(function() { return root.actions.slice(1); });
50  }
51  }
52  Binding {
53  target: loader.item
54  property: "strokeColor"
55  value: root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText
56  }
57  Connections {
58  target: loader.item
59  onTriggeredAction: {
60  root.triggered(root.widgetId, actionData.id, actionData);
61  }
62  }
63  }
64 
65  PreviewActionButton {
66  data: visible ? root.actions[0] : null
67  visible: root.actions && root.actions.length > 0
68  onTriggeredAction: root.triggered(root.widgetId, actionData.id, actionData)
69  width: Math.min(root.maxButtonWidth, implicitWidth + units.gu(2))
70  color: root.scopeStyle ? root.scopeStyle.previewButtonColor : theme.palette.normal.positive
71  }
72  }
73 }