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: Style.luminance(color); \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 Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
75 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
76 function updateWidthHeightBindings() { \n\
77 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
78 width = root.fixedArtShapeSize.width; \n\
79 height = root.fixedArtShapeSize.height; \n\
81 width = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.width }); \n\
82 height = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.height }); \n\
85 CroppedImageMinimumSourceSize { \n\
87 objectName: "artImage"; \n\
88 source: cardData && cardData["art"] || ""; \n\
89 asynchronous: root.asynchronous; \n\
94 image: artImage.image; \n\
99 var kOverlayLoaderCode =
'Loader { \n\
100 id: overlayLoader; \n\
102 left: artShapeHolder.left; \n\
103 right: artShapeHolder.right; \n\
104 bottom: artShapeHolder.bottom; \n\
106 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
107 asynchronous: root.asynchronous; \n\
108 visible: showHeader && status == Loader.Ready; \n\
109 sourceComponent: ShaderEffect { \n\
111 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
112 property real luminance: Style.luminance(overlayColor); \n\
113 property color overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
114 property var source: ShaderEffectSource { \n\
115 id: shaderSource; \n\
116 sourceItem: artShapeLoader.item; \n\
117 onVisibleChanged: if (visible) scheduleUpdate(); \n\
119 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
122 uniform highp mat4 qt_Matrix; \n\
123 attribute highp vec4 qt_Vertex; \n\
124 attribute highp vec2 qt_MultiTexCoord0; \n\
125 varying highp vec2 coord; \n\
127 coord = qt_MultiTexCoord0; \n\
128 gl_Position = qt_Matrix * qt_Vertex; \n\
130 fragmentShader: " \n\
131 varying highp vec2 coord; \n\
132 uniform sampler2D source; \n\
133 uniform lowp float qt_Opacity; \n\
134 uniform highp vec4 overlayColor; \n\
136 lowp vec4 tex = texture2D(source, coord); \n\
137 gl_FragColor = vec4(overlayColor.r, overlayColor.g, overlayColor.b, 1) * qt_Opacity * overlayColor.a * tex.a; \n\
143 function kHeaderRowCodeGenerator() {
144 var kHeaderRowCodeTemplate =
'Row { \n\
146 objectName: "outerRow"; \n\
147 property real margins: units.gu(1); \n\
148 spacing: margins; \n\
149 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
151 anchors.right: parent.right; \n\
152 anchors.margins: margins; \n\
153 anchors.rightMargin: 0; \n\
158 var args = Array.prototype.slice.call(arguments);
159 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(args.join(
',\n'));
164 function kHeaderContainerCodeGenerator() {
165 var headerContainerCodeTemplate =
'Item { \n\
166 id: headerTitleContainer; \n\
168 width: parent.width - x; \n\
169 implicitHeight: %2; \n\
174 var args = Array.prototype.slice.call(arguments);
175 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(
',\n'));
180 var kMascotShapeLoaderCode =
'Loader { \n\
181 id: mascotShapeLoader; \n\
182 objectName: "mascotShapeLoader"; \n\
183 asynchronous: root.asynchronous; \n\
184 active: mascotImage.image.status === Image.Ready; \n\
185 visible: showHeader && active && status == Loader.Ready; \n\
186 width: units.gu(6); \n\
187 height: units.gu(5.625); \n\
188 sourceComponent: UbuntuShape { image: mascotImage.image } \n\
194 var kMascotImageCode =
'CroppedImageMinimumSourceSize { \n\
196 objectName: "mascotImage"; \n\
198 source: cardData && cardData["mascot"] || ""; \n\
199 width: units.gu(6); \n\
200 height: units.gu(5.625); \n\
201 horizontalAlignment: Image.AlignHCenter; \n\
202 verticalAlignment: Image.AlignVCenter; \n\
209 var kTitleLabelCode =
'Label { \n\
211 objectName: "titleLabel"; \n\
213 elide: Text.ElideRight; \n\
214 fontSize: "small"; \n\
215 wrapMode: Text.Wrap; \n\
216 maximumLineCount: 2; \n\
217 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
219 visible: showHeader %3; \n\
220 text: root.title; \n\
221 font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
222 horizontalAlignment: root.titleAlignment; \n\
229 var kEmblemIconCode =
'Icon { \n\
231 objectName: "emblemIcon"; \n\
233 bottom: titleLabel.baseline; \n\
234 right: parent.right; \n\
237 source: cardData && cardData["emblem"] || ""; \n\
239 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
240 width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
244 var kTouchdownCode =
'UbuntuShape { \n\
246 objectName: "touchdown"; \n\
248 visible: root.pressed; \n\
249 radius: "medium"; \n\
250 borderSource: "radius_pressed.sci" \n\
255 var kSubtitleLabelCode =
'Label { \n\
256 id: subtitleLabel; \n\
257 objectName: "subtitleLabel"; \n\
259 anchors.topMargin: units.dp(2); \n\
260 elide: Text.ElideRight; \n\
261 maximumLineCount: 1; \n\
262 fontSize: "x-small"; \n\
263 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
265 visible: titleLabel.visible && titleLabel.text; \n\
266 text: cardData && cardData["subtitle"] || ""; \n\
267 font.weight: Font.Light; \n\
272 var kAttributesRowCode =
'CardAttributes { \n\
273 id: attributesRow; \n\
274 objectName: "attributesRow"; \n\
277 fontScale: root.fontScale; \n\
278 model: cardData && cardData["attributes"]; \n\
284 var kSummaryLabelCode =
'Label { \n\
286 objectName: "summaryLabel"; \n\
289 left: parent.left; \n\
290 right: parent.right; \n\
291 margins: units.gu(1); \n\
294 wrapMode: Text.Wrap; \n\
295 maximumLineCount: 5; \n\
296 elide: Text.ElideRight; \n\
297 text: cardData && cardData["summary"] || ""; \n\
298 height: text ? implicitHeight : 0; \n\
299 fontSize: "small"; \n\
303 function cardString(
template, components) {
305 code =
'AbstractButton { \n\
307 property var template; \n\
308 property var components; \n\
309 property var cardData; \n\
310 property var artShapeBorderSource: undefined; \n\
311 property real fontScale: 1.0; \n\
312 property var scopeStyle: null; \n\
313 property int titleAlignment: Text.AlignLeft; \n\
314 property int fixedHeaderHeight: -1; \n\
315 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
316 readonly property string title: cardData && cardData["title"] || ""; \n\
317 property bool asynchronous: true; \n\
318 property bool showHeader: true; \n\
319 implicitWidth: childrenRect.width; \n\
320 enabled: root.template == null ? true : (root.template["non-interactive"] !== undefined ? !root.template["non-interactive"] : true); \n\
323 var hasArt = components[
"art"] && components[
"art"][
"field"] ||
false;
324 var hasSummary = components[
"summary"] ||
false;
325 var artAndSummary = hasArt && hasSummary;
326 var isHorizontal =
template[
"card-layout"] ===
"horizontal";
327 var hasBackground = (!isHorizontal && (
template[
"card-background"] || components[
"background"] || artAndSummary)) ||
328 (hasSummary && (
template[
"card-background"] || components[
"background"]));
329 var hasTitle = components[
"title"] ||
false;
330 var hasMascot = components[
"mascot"] ||
false;
331 var hasEmblem = components[
"emblem"] && !(hasMascot &&
template[
"card-size"] ===
"small") ||
false;
332 var headerAsOverlay = hasArt &&
template &&
template[
"overlay"] ===
true && (hasTitle || hasMascot);
333 var hasSubtitle = hasTitle && components[
"subtitle"] ||
false;
334 var hasHeaderRow = hasMascot && hasTitle;
335 var hasAttributes = hasTitle && components[
"attributes"][
"field"] ||
false;
338 code += kBackgroundLoaderCode;
342 code +=
'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
343 code +=
'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
345 var widthCode, heightCode;
348 artAnchors =
'left: parent.left';
349 if (hasMascot || hasTitle) {
350 widthCode =
'height * artShape.aspect'
351 heightCode =
'headerHeight + 2 * units.gu(1)';
355 widthCode =
'height * artShape.aspect'
356 heightCode =
'units.gu(7.625)';
359 artAnchors =
'horizontalCenter: parent.horizontalCenter;';
360 widthCode =
'root.width'
361 heightCode =
'width / artShape.aspect';
364 code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
366 code +=
'readonly property size artShapeSize: Qt.size(-1, -1);\n'
369 if (headerAsOverlay) {
370 code += kOverlayLoaderCode;
373 var headerVerticalAnchors;
374 if (headerAsOverlay) {
375 headerVerticalAnchors =
'bottom: artShapeHolder.bottom; \n\
376 bottomMargin: units.gu(1);\n';
380 headerVerticalAnchors =
'top: artShapeHolder.top; \n\
381 topMargin: units.gu(1);\n';
383 headerVerticalAnchors =
'top: artShapeHolder.bottom; \n\
384 topMargin: units.gu(1);\n';
387 headerVerticalAnchors =
'top: parent.top; \n\
388 topMargin: units.gu(1);\n';
391 var headerLeftAnchor;
392 var headerLeftAnchorHasMargin =
false;
393 if (isHorizontal && hasArt) {
394 headerLeftAnchor =
'left: artShapeHolder.right; \n\
395 leftMargin: units.gu(1);\n';
396 headerLeftAnchorHasMargin =
true;
398 headerLeftAnchor =
'left: parent.left;\n';
401 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
404 code +=
'readonly property int headerHeight: row.height;\n'
405 }
else if (hasMascot) {
406 code +=
'readonly property int headerHeight: mascotImage.height;\n'
407 }
else if (hasAttributes) {
408 if (hasTitle && hasSubtitle) {
409 code +=
'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
410 }
else if (hasTitle) {
411 code +=
'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
413 code +=
'readonly property int headerHeight: attributesRow.height;\n'
415 }
else if (hasSubtitle) {
416 code +=
'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
417 }
else if (hasTitle) {
418 code +=
'readonly property int headerHeight: titleLabel.height;\n'
420 code +=
'readonly property int headerHeight: 0;\n'
423 var mascotShapeCode =
'';
426 var useMascotShape = !hasBackground && !headerAsOverlay;
427 var mascotAnchors =
'';
429 mascotAnchors += headerLeftAnchor;
430 mascotAnchors += headerVerticalAnchors;
431 if (!headerLeftAnchorHasMargin) {
432 mascotAnchors +=
'leftMargin: units.gu(1);\n'
435 mascotAnchors =
'verticalCenter: parent.verticalCenter;'
438 if (useMascotShape) {
439 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
442 var mascotImageVisible = useMascotShape ?
'false' :
'showHeader';
443 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
446 var summaryColorWithBackground =
'backgroundLoader.active && backgroundLoader.item && root.scopeStyle ? root.scopeStyle.getTextColor(backgroundLoader.item.luminance) : (backgroundLoader.item && backgroundLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
448 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
449 var titleSubtitleCode =
'';
452 if (headerAsOverlay) {
453 titleColor =
'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
454 }
else if (hasSummary) {
455 titleColor =
'summary.color';
456 }
else if (hasBackground) {
457 titleColor = summaryColorWithBackground;
459 titleColor =
'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
464 var attributesAnchors;
465 var titleContainerAnchors;
466 var titleRightAnchor;
468 var extraRightAnchor =
'';
469 var extraLeftAnchor =
'';
470 if (!touchdownOnArtShape) {
471 extraRightAnchor =
'rightMargin: units.gu(1); \n';
472 extraLeftAnchor =
'leftMargin: units.gu(1); \n';
473 }
else if (headerAsOverlay && !hasEmblem) {
474 extraRightAnchor =
'rightMargin: units.gu(1); \n';
478 titleContainerAnchors =
'verticalCenter: parent.verticalCenter; ';
480 titleContainerAnchors =
'right: parent.right; ';
481 titleContainerAnchors += headerLeftAnchor;
482 titleContainerAnchors += headerVerticalAnchors;
483 if (!headerLeftAnchorHasMargin) {
484 titleContainerAnchors += extraLeftAnchor;
488 titleRightAnchor =
'right: emblemIcon.left; \n\
489 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
491 titleRightAnchor =
'right: parent.right; \n'
492 titleRightAnchor += extraRightAnchor;
495 if (hasTitleContainer) {
497 titleAnchors = titleRightAnchor;
498 titleAnchors +=
'left: parent.left; \n\
500 subtitleAnchors =
'right: parent.right; \n\
501 left: parent.left; \n';
502 subtitleAnchors += extraRightAnchor;
504 attributesAnchors = subtitleAnchors +
'top: subtitleLabel.bottom;\n';
505 subtitleAnchors +=
'top: titleLabel.bottom;\n';
507 attributesAnchors = subtitleAnchors +
'top: titleLabel.bottom;\n';
509 }
else if (hasMascot) {
511 titleAnchors =
'verticalCenter: parent.verticalCenter;\n';
513 if (headerAsOverlay) {
515 titleAnchors = titleRightAnchor;
516 titleAnchors +=
'left: parent.left; \n\
517 leftMargin: units.gu(1); \n\
518 top: overlayLoader.top; \n\
519 topMargin: units.gu(1);\n';
522 titleAnchors = titleRightAnchor;
523 titleAnchors += headerLeftAnchor;
524 titleAnchors += headerVerticalAnchors;
525 if (!headerLeftAnchorHasMargin) {
526 titleAnchors += extraLeftAnchor;
529 subtitleAnchors =
'left: titleLabel.left; \n\
530 leftMargin: titleLabel.leftMargin; \n';
531 subtitleAnchors += extraRightAnchor;
534 subtitleAnchors +=
'right: parent.right; \n';
536 subtitleAnchors +=
'right: titleLabel.right; \n';
540 attributesAnchors = subtitleAnchors +
'top: subtitleLabel.bottom;\n';
541 subtitleAnchors +=
'top: titleLabel.bottom;\n';
543 attributesAnchors = subtitleAnchors +
'top: titleLabel.bottom;\n';
548 var titleLabelVisibleExtra = (headerAsOverlay ?
'&& overlayLoader.active':
'');
549 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
554 var containerCode = [];
555 var containerHeight =
'titleLabel.height';
556 containerCode.push(titleCode);
558 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
559 containerCode.push(subtitleCode);
560 containerHeight +=
' + subtitleLabel.height';
563 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
566 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
567 containerCode.push(attributesCode);
568 containerHeight +=
' + attributesRow.height';
571 if (hasTitleContainer) {
573 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
576 titleSubtitleCode = titleCode;
578 titleSubtitleCode += subtitleCode;
581 titleSubtitleCode += attributesCode;
587 var rowCode = [mascotCode, titleSubtitleCode];
588 if (mascotShapeCode !=
'') {
589 rowCode.unshift(mascotShapeCode);
591 code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
593 code += mascotShapeCode + mascotCode + titleSubtitleCode;
597 var summaryTopAnchor;
598 if (isHorizontal && hasArt) summaryTopAnchor =
'artShapeHolder.bottom';
599 else if (headerAsOverlay && hasArt) summaryTopAnchor =
'artShapeHolder.bottom';
600 else if (hasHeaderRow) summaryTopAnchor =
'row.bottom';
601 else if (hasTitleContainer) summaryTopAnchor =
'headerTitleContainer.bottom';
602 else if (hasMascot) summaryTopAnchor =
'mascotImage.bottom';
603 else if (hasAttributes) summaryTopAnchor =
'attributesRow.bottom';
604 else if (hasSubtitle) summaryTopAnchor =
'subtitleLabel.bottom';
605 else if (hasTitle) summaryTopAnchor =
'titleLabel.bottom';
606 else if (hasArt) summaryTopAnchor =
'artShapeHolder.bottom';
607 else summaryTopAnchor =
'parent.top';
611 summaryColor = summaryColorWithBackground;
613 summaryColor =
'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
616 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ?
'anchors.margins' :
'0');
618 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
621 var touchdownAnchors;
623 touchdownAnchors =
'fill: backgroundLoader';
624 }
else if (touchdownOnArtShape) {
625 touchdownAnchors =
'fill: artShapeHolder';
627 touchdownAnchors =
'fill: root'
629 code += kTouchdownCode.arg(touchdownAnchors);
631 var implicitHeight =
'implicitHeight: ';
633 implicitHeight +=
'summary.y + summary.height + units.gu(1);\n';
634 }
else if (hasHeaderRow) {
635 implicitHeight +=
'row.y + row.height + units.gu(1);\n';
636 }
else if (hasMascot) {
637 implicitHeight +=
'mascotImage.y + mascotImage.height;\n';
638 }
else if (hasTitleContainer) {
639 implicitHeight +=
'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
640 }
else if (hasAttributes) {
641 implicitHeight +=
'attributesRow.y + attributesRow.height + units.gu(1);\n';
642 }
else if (hasSubtitle) {
643 implicitHeight +=
'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
644 }
else if (hasTitle) {
645 implicitHeight +=
'titleLabel.y + titleLabel.height + units.gu(1);\n';
647 implicitHeight +=
'artShapeHolder.height;\n';
653 code += implicitHeight +
'}\n';
658 function createCardComponent(parent,
template, components) {
659 var imports =
'import QtQuick 2.2; \n\
660 import Ubuntu.Components 1.1; \n\
661 import Ubuntu.Settings.Components 0.1; \n\
663 import Utils 0.1;\n';
664 var card = cardString(
template, components);
665 var code = imports +
'Component {\n' + card +
'}\n';
668 return Qt.createQmlObject(code, parent,
"createCardComponent");
670 console.error(
"ERROR: Invalid component created.");
671 console.error(
"Template:");
672 console.error(JSON.stringify(
template));
673 console.error(
"Components:");
674 console.error(JSON.stringify(components));
675 console.error(
"Code:");