19 var kBackgroundLoaderCode =
'Loader {\n\
20 id: backgroundLoader; \n\
21 objectName: "backgroundLoader"; \n\
22 anchors.fill: parent; \n\
23 asynchronous: root.asynchronous; \n\
24 visible: status == Loader.Ready; \n\
25 sourceComponent: UbuntuShape { \n\
26 objectName: "background"; \n\
28 color: getColor(0) || "white"; \n\
29 gradientColor: getColor(1) || color; \n\
30 anchors.fill: parent; \n\
31 image: backgroundImage.source ? backgroundImage : null; \n\
32 property real luminance: 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; \n\
33 property Image backgroundImage: Image { \n\
34 objectName: "backgroundImage"; \n\
36 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
37 else if (template && typeof template["card-background"] === "string") return template["card-background"]; \n\
41 function getColor(index) { \n\
42 if (cardData && typeof cardData["background"] === "object" \n\
43 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
44 return cardData["background"]["elements"][index]; \n\
45 } else if (template && typeof template["card-background"] === "object" \n\
46 && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) { \n\
47 return template["card-background"]["elements"][index]; \n\
48 } else return undefined; \n\
56 var kArtShapeHolderCode =
'Item { \n\
57 id: artShapeHolder; \n\
58 height: root.fixedArtShapeSize.height > 0 ? root.fixedArtShapeSize.height : artShapeLoader.height; \n\
59 width: root.fixedArtShapeSize.width > 0 ? root.fixedArtShapeSize.width : artShapeLoader.width; \n\
62 id: artShapeLoader; \n\
63 objectName: "artShapeLoader"; \n\
64 active: cardData && cardData["art"] || false; \n\
65 asynchronous: root.asynchronous; \n\
66 visible: status == Loader.Ready; \n\
67 sourceComponent: UbuntuShape { \n\
69 objectName: "artShape"; \n\
71 visible: image.status == Image.Ready; \n\
72 readonly property real fixedArtShapeSizeAspect: (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) ? root.fixedArtShapeSize.width / root.fixedArtShapeSize.height : -1; \n\
73 readonly property real aspect: fixedArtShapeSizeAspect > 0 ? fixedArtShapeSizeAspect : components !== undefined ? components["art"]["aspect-ratio"] : 1; \n\
74 readonly property bool aspectSmallerThanImageAspect: aspect < image.aspect; \n\
75 Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
76 onAspectSmallerThanImageAspectChanged: updateWidthHeightBindings(); \n\
77 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
78 function updateWidthHeightBindings() { \n\
79 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
80 width = root.fixedArtShapeSize.width; \n\
81 height = root.fixedArtShapeSize.height; \n\
82 } else if (aspectSmallerThanImageAspect) { \n\
83 width = Qt.binding(function() { return !visible ? 0 : image.width }); \n\
84 height = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.height : width / image.aspect }); \n\
86 width = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.width : height * image.aspect }); \n\
87 height = Qt.binding(function() { return !visible ? 0 : image.height }); \n\
91 objectName: "artImage"; \n\
92 source: cardData && cardData["art"] || ""; \n\
94 asynchronous: root.asynchronous; \n\
95 fillMode: components && components["art"]["fill-mode"] === "fit" ? Image.PreserveAspectFit: Image.PreserveAspectCrop; \n\
96 readonly property real aspect: implicitWidth / implicitHeight; \n\
104 var kOverlayLoaderCode =
'Loader { \n\
105 id: overlayLoader; \n\
107 left: artShapeHolder.left; \n\
108 right: artShapeHolder.right; \n\
109 bottom: artShapeHolder.bottom; \n\
111 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
112 asynchronous: root.asynchronous; \n\
113 visible: showHeader && status == Loader.Ready; \n\
114 sourceComponent: ShaderEffect { \n\
116 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
118 property var source: ShaderEffectSource { \n\
119 id: shaderSource; \n\
120 sourceItem: artShapeLoader.item; \n\
121 onVisibleChanged: if (visible) scheduleUpdate(); \n\
123 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
126 uniform highp mat4 qt_Matrix; \n\
127 attribute highp vec4 qt_Vertex; \n\
128 attribute highp vec2 qt_MultiTexCoord0; \n\
129 varying highp vec2 coord; \n\
131 coord = qt_MultiTexCoord0; \n\
132 gl_Position = qt_Matrix * qt_Vertex; \n\
134 fragmentShader: " \n\
135 varying highp vec2 coord; \n\
136 uniform sampler2D source; \n\
137 uniform lowp float qt_Opacity; \n\
139 lowp vec4 tex = texture2D(source, coord); \n\
140 gl_FragColor = vec4(0, 0, 0, tex.a) * qt_Opacity; \n\
148 var kHeaderRow2Code =
'Row { \n\
150 objectName: "outerRow"; \n\
151 property real margins: units.gu(1); \n\
152 spacing: margins; \n\
153 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
155 anchors.right: parent.right; \n\
156 anchors.margins: margins;\n\
167 var kHeaderRow3Code =
'Row { \n\
169 objectName: "outerRow"; \n\
170 property real margins: units.gu(1); \n\
171 spacing: margins; \n\
172 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
174 anchors.right: parent.right; \n\
175 anchors.margins: margins;\n\
186 var kHeaderColumnCode =
'Column { \n\
187 anchors.verticalCenter: parent.verticalCenter; \n\
188 spacing: units.dp(2); \n\
189 width: parent.width - x;\n\
197 var kMascotShapeLoaderCode =
'Loader { \n\
198 id: mascotShapeLoader; \n\
199 objectName: "mascotShapeLoader"; \n\
200 asynchronous: root.asynchronous; \n\
201 active: mascotImage.status === Image.Ready; \n\
202 visible: showHeader && active && status == Loader.Ready; \n\
203 width: units.gu(6); \n\
204 height: units.gu(5.625); \n\
205 sourceComponent: UbuntuShape { image: mascotImage } \n\
211 var kMascotImageCode =
'Image { \n\
213 objectName: "mascotImage"; \n\
215 readonly property int maxSize: Math.max(width, height) * 4; \n\
216 source: cardData && cardData["mascot"]; \n\
217 width: units.gu(6); \n\
218 height: units.gu(5.625); \n\
219 sourceSize { width: maxSize; height: maxSize } \n\
220 fillMode: Image.PreserveAspectCrop; \n\
221 horizontalAlignment: Image.AlignHCenter; \n\
222 verticalAlignment: Image.AlignVCenter; \n\
229 var kTitleLabelCode =
'Label { \n\
231 objectName: "titleLabel"; \n\
233 elide: Text.ElideRight; \n\
234 fontSize: "small"; \n\
235 wrapMode: Text.Wrap; \n\
236 maximumLineCount: 2; \n\
237 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
239 visible: showHeader %3; \n\
240 text: root.title; \n\
241 font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
242 horizontalAlignment: root.headerAlignment; \n\
247 var kSubtitleLabelCode =
'Label { \n\
248 id: subtitleLabel; \n\
249 objectName: "subtitleLabel"; \n\
251 elide: Text.ElideRight; \n\
252 fontSize: "small"; \n\
253 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
255 visible: titleLabel.visible && titleLabel.text; \n\
256 text: cardData && cardData["subtitle"] || ""; \n\
257 font.weight: Font.Light; \n\
258 horizontalAlignment: root.headerAlignment; \n\
264 var kSummaryLabelCode =
'Label { \n\
266 objectName: "summaryLabel"; \n\
269 left: parent.left; \n\
270 right: parent.right; \n\
271 margins: units.gu(1); \n\
274 wrapMode: Text.Wrap; \n\
275 maximumLineCount: 5; \n\
276 elide: Text.ElideRight; \n\
277 text: cardData && cardData["summary"] || ""; \n\
278 height: text ? implicitHeight : 0; \n\
279 fontSize: "small"; \n\
283 function cardString(
template, components) {
285 code =
'AbstractButton { \n\
287 property var template; \n\
288 property var components; \n\
289 property var cardData; \n\
290 property var artShapeBorderSource: undefined; \n\
291 property real fontScale: 1.0; \n\
292 property var scopeStyle: null; \n\
293 property int headerAlignment: Text.AlignLeft; \n\
294 property int fixedHeaderHeight: -1; \n\
295 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
296 readonly property string title: cardData && cardData["title"] || ""; \n\
297 property bool asynchronous: true; \n\
298 property bool showHeader: true; \n\
299 implicitWidth: childrenRect.width; \n';
301 var hasArt = components[
"art"] && components[
"art"][
"field"] ||
false;
302 var hasSummary = components[
"summary"] ||
false;
303 var artAndSummary = hasArt && hasSummary;
304 var isHorizontal =
template[
"card-layout"] ===
"horizontal";
305 var hasBackground = !isHorizontal && (
template[
"card-background"] || components[
"background"] || artAndSummary);
306 var hasTitle = components[
"title"] ||
false;
307 var hasMascot = components[
"mascot"] ||
false;
308 var headerAsOverlay = hasArt &&
template &&
template[
"overlay"] ===
true && (hasTitle || hasMascot);
309 var hasSubtitle = hasTitle && components[
"subtitle"] ||
false;
310 var hasHeaderRow = hasMascot && hasTitle;
313 code += kBackgroundLoaderCode;
317 code +=
'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
318 code +=
'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
320 var widthCode, heightCode;
323 anchors =
'left: parent.left';
324 if (hasMascot || hasTitle) {
325 widthCode =
'height * artShape.aspect'
326 heightCode =
'headerHeight + 2 * units.gu(1)';
330 widthCode =
'height * artShape.aspect'
331 heightCode =
'units.gu(7.625)';
334 anchors =
'horizontalCenter: parent.horizontalCenter;';
335 widthCode =
'root.width'
336 heightCode =
'width / artShape.aspect';
339 code += kArtShapeHolderCode.arg(anchors).arg(widthCode).arg(heightCode);
341 code +=
'readonly property size artShapeSize: Qt.size(-1, -1);\n'
344 if (headerAsOverlay) {
345 code += kOverlayLoaderCode;
348 var headerVerticalAnchors;
349 if (headerAsOverlay) {
350 headerVerticalAnchors =
'bottom: artShapeHolder.bottom; \n\
351 bottomMargin: units.gu(1);\n';
355 headerVerticalAnchors =
'top: artShapeHolder.top; \n\
356 topMargin: units.gu(1);\n';
358 headerVerticalAnchors =
'top: artShapeHolder.bottom; \n\
359 topMargin: units.gu(1);\n';
362 headerVerticalAnchors =
'top: parent.top; \n\
363 topMargin: units.gu(1);\n';
366 var headerLeftAnchor;
367 var headerLeftAnchorHasMargin =
false;
368 if (isHorizontal && hasArt) {
369 headerLeftAnchor =
'left: artShapeHolder.right; \n\
370 leftMargin: units.gu(1);\n';
371 headerLeftAnchorHasMargin =
true;
373 headerLeftAnchor =
'left: parent.left;\n';
377 code +=
'readonly property int headerHeight: row.height;\n'
378 }
else if (hasMascot) {
379 code +=
'readonly property int headerHeight: mascotImage.height;\n'
380 }
else if (hasSubtitle) {
381 code +=
'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
382 }
else if (hasTitle) {
383 code +=
'readonly property int headerHeight: titleLabel.height;\n'
385 code +=
'readonly property int headerHeight: 0;\n'
388 var mascotShapeCode =
"";
391 var useMascotShape = !hasBackground && !headerAsOverlay;
394 anchors += headerLeftAnchor;
395 anchors += headerVerticalAnchors;
396 if (!headerLeftAnchorHasMargin) {
397 anchors +=
'leftMargin: units.gu(1);\n'
400 anchors =
"verticalCenter: parent.verticalCenter;"
403 if (useMascotShape) {
404 mascotShapeCode = kMascotShapeLoaderCode.arg(anchors);
407 var mascotImageVisible = useMascotShape ?
'false' :
'showHeader';
408 mascotCode = kMascotImageCode.arg(anchors).arg(mascotImageVisible);
411 var summaryColorWithBackground =
'backgroundLoader.active && backgroundLoader.item && backgroundLoader.item.luminance < 0.7 ? "white" : (root.scopeStyle ? root.scopeStyle.foreground : "grey")';
413 var titleSubtitleCode =
"";
416 if (headerAsOverlay) {
418 }
else if (hasSummary) {
419 color =
'summary.color';
420 }
else if (hasBackground) {
421 color = summaryColorWithBackground;
423 color =
'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
428 if (hasMascot && hasSubtitle) {
430 titleAnchors =
'left: parent.left; right: parent.right';
431 subtitleAnchors = titleAnchors;
432 }
else if (hasMascot) {
434 titleAnchors =
'verticalCenter: parent.verticalCenter;\n'
436 if (headerAsOverlay) {
438 titleAnchors =
'left: parent.left; \n\
439 leftMargin: units.gu(1); \n\
440 right: parent.right; \n\
441 top: overlayLoader.top; \n\
442 topMargin: units.gu(1);\n';
445 titleAnchors =
"right: parent.right;";
446 titleAnchors += headerLeftAnchor;
447 titleAnchors += headerVerticalAnchors;
448 if (!headerLeftAnchorHasMargin) {
449 titleAnchors +=
'leftMargin: units.gu(1);\n'
452 subtitleAnchors =
'left: titleLabel.left; \n\
453 leftMargin: titleLabel.leftMargin; \n\
454 right: titleLabel.right; \n\
455 top: titleLabel.bottom; \n\
456 topMargin: units.dp(2);\n';
459 var titleLabelVisibleExtra = (headerAsOverlay ?
'&& overlayLoader.active':
'');
460 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(color).arg(titleLabelVisibleExtra);
461 var subtitleCode =
"";
463 subtitleCode += kSubtitleLabelCode.arg(subtitleAnchors).arg(color);
466 if (hasMascot && hasSubtitle) {
468 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
470 titleSubtitleCode = titleCode + subtitleCode;
475 if (mascotShapeCode !=
"") {
476 code += kHeaderRow3Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotShapeCode).arg(mascotCode).arg(titleSubtitleCode);
478 code += kHeaderRow2Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotCode).arg(titleSubtitleCode);
481 code += mascotShapeCode + mascotCode + titleSubtitleCode;
485 var summaryTopAnchor;
486 if (isHorizontal && hasArt) summaryTopAnchor =
"artShapeHolder.bottom";
487 else if (headerAsOverlay && hasArt) summaryTopAnchor =
"artShapeHolder.bottom";
488 else if (hasHeaderRow) summaryTopAnchor =
"row.bottom";
489 else if (hasMascot) summaryTopAnchor =
"mascotImage.bottom";
490 else if (hasSubtitle) summaryTopAnchor =
"subtitleLabel.bottom";
491 else if (hasTitle) summaryTopAnchor =
"titleLabel.bottom";
492 else if (hasArt) summaryTopAnchor =
"artShapeHolder.bottom";
493 else summaryTopAnchor =
"parent.top";
497 color = summaryColorWithBackground;
499 color =
'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
502 var summaryTopMargin = (hasMascot || hasSubtitle ?
'anchors.margins' :
'0');
504 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(color);
508 code +=
'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
509 }
else if (hasHeaderRow) {
510 code +=
'implicitHeight: row.y + row.height + units.gu(1);\n';
511 }
else if (hasMascot) {
512 code +=
'implicitHeight: mascotImage.y + mascotImage.height;\n';
513 }
else if (hasSubtitle) {
514 code +=
'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
515 }
else if (hasTitle) {
516 code +=
'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
518 code +=
'implicitHeight: artShapeHolder.height;\n';
526 function createCardComponent(parent,
template, components) {
527 var imports =
'import QtQuick 2.2; \n\
528 import Ubuntu.Components 0.1; \n\
529 import Ubuntu.Thumbnailer 0.1;\n';
530 var card = cardString(
template, components);
531 var code = imports +
'Component {\n' + card +
'}\n';
532 return Qt.createQmlObject(code, parent,
"createCardComponent");