Unity 8
PreviewHeader.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 import Dash 0.1
20 import "../"
21 
22 /*! This preview widget shows a header
23  * The title comes in widgetData["title"]
24  * The mascot comes in widgetData["mascot"]
25  * The mascot fall back image comes in widgetData["fallback"]
26  * The subtitle comes in widgetData["subtitle"]
27  * The attributes comes in widgetData["attributes"]
28  * The emblem comes in widgetData["emblem"]
29  */
30 
31 PreviewWidget {
32  id: root
33 
34  implicitHeight: childrenRect.height
35 
36  Item {
37  id: headerRoot
38  objectName: "innerPreviewHeader"
39  readonly property url mascot: root.widgetData["mascot"] || fallback
40  readonly property url fallback: root.widgetData["fallback"] || ""
41  readonly property string title: root.widgetData["title"] || ""
42  readonly property string subtitle: root.widgetData["subtitle"] || ""
43  readonly property var attributes: root.widgetData["attributes"] || null
44  readonly property url emblem: root.widgetData["emblem"] || ""
45  readonly property color fontColor: root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText
46 
47  // Rewire the source since we may have unwired it on onStatusChanged
48  onMascotChanged: if (mascotShapeLoader.item) mascotShapeLoader.item.source.source = mascot;
49 
50  implicitHeight: row.height + row.margins * 2
51  width: parent.width
52 
53  Row {
54  id: row
55  objectName: "outerRow"
56 
57  property real margins: units.gu(1)
58 
59  spacing: mascotShapeLoader.active ? margins : 0
60  anchors {
61  top: parent.top; left: parent.left; right: parent.right
62  margins: margins
63  leftMargin: spacing
64  rightMargin: spacing
65  }
66 
67  Loader {
68  id: mascotShapeLoader
69  objectName: "mascotShapeLoader"
70  active: headerRoot.mascot != ""
71  visible: active
72 
73  anchors.verticalCenter: parent.verticalCenter
74  // TODO karni: Icon aspect-ratio is 8:7.5. Revisit these values to avoid fraction of pixels.
75  width: units.gu(6)
76  height: units.gu(5.625)
77  readonly property int maxSize: Math.max(width, height) * 4
78  asynchronous: true
79 
80  sourceComponent: UbuntuShape {
81  objectName: "mascotShape"
82  visible: source.status === Image.Ready
83  sourceFillMode: UbuntuShape.PreserveAspectCrop
84  sourceHorizontalAlignment: UbuntuShape.AlignHCenter
85  sourceVerticalAlignment: UbuntuShape.AlignVCenter
86  source: Image {
87  source: headerRoot.mascot
88  width: source ? mascotShapeLoader.width : 0
89  height: mascotShapeLoader.height
90 
91  sourceSize { width: mascotShapeLoader.maxSize; height: mascotShapeLoader.maxSize }
92  onStatusChanged: if (status === Image.Error) source = headerRoot.fallback;
93  }
94  }
95  }
96 
97  Column {
98  objectName: "column"
99  width: parent.width - x
100  spacing: units.dp(2)
101  anchors.verticalCenter: parent.verticalCenter
102 
103  Item {
104  anchors { left: parent.left; right: parent.right }
105  height: titleLabel.height
106 
107  Label {
108  id: titleLabel
109  objectName: "titleLabel"
110  anchors {
111  left: parent.left;
112  right: iconLoader.right
113  rightMargin: iconLoader.width > 0 ? units.gu(0.5) : 0
114  }
115  elide: Text.ElideRight
116  font.weight: Font.Normal
117  fontSize: "large"
118  wrapMode: Text.Wrap
119  color: headerRoot.fontColor
120  text: headerRoot.title
121  }
122 
123  Loader {
124  id: iconLoader
125  active: headerRoot.emblem != ""
126  anchors {
127  bottom: titleLabel.baseline
128  right: parent.right
129  }
130  sourceComponent: Icon {
131  objectName: "emblemIcon"
132  source: headerRoot.emblem
133  color: headerRoot.fontColor
134  height: source != "" ? titleLabel.font.pixelSize : 0
135  // FIXME Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
136  width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth
137  }
138  }
139  }
140 
141  Loader {
142  active: titleLabel.text && headerRoot.subtitle
143  anchors { left: parent.left; right: parent.right }
144  sourceComponent: Label {
145  id: subtitleLabel
146  objectName: "subtitleLabel"
147  elide: Text.ElideRight
148  fontSize: "small"
149  font.weight: Font.Light
150  color: headerRoot.fontColor
151  text: headerRoot.subtitle
152  }
153  }
154 
155  Loader {
156  active: titleLabel.text && headerRoot.attributes
157  anchors { left: parent.left; right: parent.right }
158  sourceComponent: CardAttributes {
159  id: previewAttributes
160  objectName: "previewAttributes"
161  model: headerRoot.attributes
162  }
163  }
164  }
165  }
166  }
167 
168 }