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 !visible ? 0 : image.width }); \n\
82 height = Qt.binding(function() { return !visible ? 0 : image.height }); \n\
86 objectName: "artImage"; \n\
87 source: cardData && cardData["art"] || ""; \n\
89 asynchronous: root.asynchronous; \n\
90 fillMode: Image.PreserveAspectCrop; \n\
98 var kOverlayLoaderCode =
'Loader { \n\
99 id: overlayLoader; \n\
101 left: artShapeHolder.left; \n\
102 right: artShapeHolder.right; \n\
103 bottom: artShapeHolder.bottom; \n\
105 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
106 asynchronous: root.asynchronous; \n\
107 visible: showHeader && status == Loader.Ready; \n\
108 sourceComponent: ShaderEffect { \n\
110 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
111 property real luminance: Style.luminance(overlayColor); \n\
112 property color overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
113 property var source: ShaderEffectSource { \n\
114 id: shaderSource; \n\
115 sourceItem: artShapeLoader.item; \n\
116 onVisibleChanged: if (visible) scheduleUpdate(); \n\
118 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
121 uniform highp mat4 qt_Matrix; \n\
122 attribute highp vec4 qt_Vertex; \n\
123 attribute highp vec2 qt_MultiTexCoord0; \n\
124 varying highp vec2 coord; \n\
126 coord = qt_MultiTexCoord0; \n\
127 gl_Position = qt_Matrix * qt_Vertex; \n\
129 fragmentShader: " \n\
130 varying highp vec2 coord; \n\
131 uniform sampler2D source; \n\
132 uniform lowp float qt_Opacity; \n\
133 uniform highp vec4 overlayColor; \n\
135 lowp vec4 tex = texture2D(source, coord); \n\
136 gl_FragColor = vec4(overlayColor.r, overlayColor.g, overlayColor.b, 1) * qt_Opacity * overlayColor.a * tex.a; \n\
142 function kHeaderRowCodeGenerator() {
143 var kHeaderRowCodeTemplate =
'Row { \n\
145 objectName: "outerRow"; \n\
146 property real margins: units.gu(1); \n\
147 spacing: margins; \n\
148 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
150 anchors.right: parent.right; \n\
151 anchors.margins: margins; \n\
152 anchors.rightMargin: 0; \n\
157 var args = Array.prototype.slice.call(arguments);
158 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(args.join(
',\n'));
163 function kHeaderContainerCodeGenerator() {
164 var headerContainerCodeTemplate =
'Item { \n\
165 id: headerTitleContainer; \n\
167 width: parent.width - x; \n\
168 implicitHeight: %2; \n\
173 var args = Array.prototype.slice.call(arguments);
174 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(
',\n'));
179 var kMascotShapeLoaderCode =
'Loader { \n\
180 id: mascotShapeLoader; \n\
181 objectName: "mascotShapeLoader"; \n\
182 asynchronous: root.asynchronous; \n\
183 active: mascotImage.status === Image.Ready; \n\
184 visible: showHeader && active && status == Loader.Ready; \n\
185 width: units.gu(6); \n\
186 height: units.gu(5.625); \n\
187 sourceComponent: UbuntuShape { image: mascotImage } \n\
193 var kMascotImageCode =
'Image { \n\
195 objectName: "mascotImage"; \n\
197 readonly property int maxSize: Math.max(width, height) * 4; \n\
198 source: cardData && cardData["mascot"] || ""; \n\
199 width: units.gu(6); \n\
200 height: units.gu(5.625); \n\
201 sourceSize { width: maxSize; height: maxSize } \n\
202 fillMode: Image.PreserveAspectCrop; \n\
203 horizontalAlignment: Image.AlignHCenter; \n\
204 verticalAlignment: Image.AlignVCenter; \n\
211 var kTitleLabelCode =
'Label { \n\
213 objectName: "titleLabel"; \n\
215 elide: Text.ElideRight; \n\
216 fontSize: "small"; \n\
217 wrapMode: Text.Wrap; \n\
218 maximumLineCount: 2; \n\
219 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
221 visible: showHeader %3; \n\
222 text: root.title; \n\
223 font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
224 horizontalAlignment: root.headerAlignment; \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\
240 height: source != "" ? titleLabel.font.pixelSize : 0; \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 fontSize: "small"; \n\
262 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
264 visible: titleLabel.visible && titleLabel.text; \n\
265 text: cardData && cardData["subtitle"] || ""; \n\
266 font.weight: Font.Light; \n\
267 horizontalAlignment: root.headerAlignment; \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 headerAlignment: 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';
321 var hasArt = components[
"art"] && components[
"art"][
"field"] ||
false;
322 var hasSummary = components[
"summary"] ||
false;
323 var artAndSummary = hasArt && hasSummary;
324 var isHorizontal =
template[
"card-layout"] ===
"horizontal";
325 var hasBackground = !isHorizontal && (
template[
"card-background"] || components[
"background"] || artAndSummary);
326 var hasTitle = components[
"title"] ||
false;
327 var hasMascot = components[
"mascot"] ||
false;
328 var hasEmblem = components[
"emblem"] && !(hasMascot &&
template[
"card-size"] ===
"small") ||
false;
329 var headerAsOverlay = hasArt &&
template &&
template[
"overlay"] ===
true && (hasTitle || hasMascot);
330 var hasSubtitle = hasTitle && components[
"subtitle"] ||
false;
331 var hasHeaderRow = hasMascot && hasTitle;
332 var hasAttributes = hasTitle && components[
"attributes"][
"field"] ||
false;
335 code += kBackgroundLoaderCode;
339 code +=
'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
340 code +=
'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
342 var widthCode, heightCode;
345 artAnchors =
'left: parent.left';
346 if (hasMascot || hasTitle) {
347 widthCode =
'height * artShape.aspect'
348 heightCode =
'headerHeight + 2 * units.gu(1)';
352 widthCode =
'height * artShape.aspect'
353 heightCode =
'units.gu(7.625)';
356 artAnchors =
'horizontalCenter: parent.horizontalCenter;';
357 widthCode =
'root.width'
358 heightCode =
'width / artShape.aspect';
361 code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
363 code +=
'readonly property size artShapeSize: Qt.size(-1, -1);\n'
366 if (headerAsOverlay) {
367 code += kOverlayLoaderCode;
370 var headerVerticalAnchors;
371 if (headerAsOverlay) {
372 headerVerticalAnchors =
'bottom: artShapeHolder.bottom; \n\
373 bottomMargin: units.gu(1);\n';
377 headerVerticalAnchors =
'top: artShapeHolder.top; \n\
378 topMargin: units.gu(1);\n';
380 headerVerticalAnchors =
'top: artShapeHolder.bottom; \n\
381 topMargin: units.gu(1);\n';
384 headerVerticalAnchors =
'top: parent.top; \n\
385 topMargin: units.gu(1);\n';
388 var headerLeftAnchor;
389 var headerLeftAnchorHasMargin =
false;
390 if (isHorizontal && hasArt) {
391 headerLeftAnchor =
'left: artShapeHolder.right; \n\
392 leftMargin: units.gu(1);\n';
393 headerLeftAnchorHasMargin =
true;
395 headerLeftAnchor =
'left: parent.left;\n';
398 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
401 code +=
'readonly property int headerHeight: row.height;\n'
402 }
else if (hasMascot) {
403 code +=
'readonly property int headerHeight: mascotImage.height;\n'
404 }
else if (hasAttributes) {
405 if (hasTitle && hasSubtitle) {
406 code +=
'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
407 }
else if (hasTitle) {
408 code +=
'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
410 code +=
'readonly property int headerHeight: attributesRow.height;\n'
412 }
else if (hasSubtitle) {
413 code +=
'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
414 }
else if (hasTitle) {
415 code +=
'readonly property int headerHeight: titleLabel.height;\n'
417 code +=
'readonly property int headerHeight: 0;\n'
420 var mascotShapeCode =
'';
423 var useMascotShape = !hasBackground && !headerAsOverlay;
424 var mascotAnchors =
'';
426 mascotAnchors += headerLeftAnchor;
427 mascotAnchors += headerVerticalAnchors;
428 if (!headerLeftAnchorHasMargin) {
429 mascotAnchors +=
'leftMargin: units.gu(1);\n'
432 mascotAnchors =
'verticalCenter: parent.verticalCenter;'
435 if (useMascotShape) {
436 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
439 var mascotImageVisible = useMascotShape ?
'false' :
'showHeader';
440 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
443 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")';
445 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
446 var titleSubtitleCode =
'';
449 if (headerAsOverlay) {
450 titleColor =
'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
451 }
else if (hasSummary) {
452 titleColor =
'summary.color';
453 }
else if (hasBackground) {
454 titleColor = summaryColorWithBackground;
456 titleColor =
'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
461 var attributesAnchors;
462 var titleContainerAnchors;
463 var titleRightAnchor;
465 var extraRightAnchor =
'';
466 var extraLeftAnchor =
'';
467 if (!touchdownOnArtShape) {
468 extraRightAnchor =
'rightMargin: units.gu(1); \n';
469 extraLeftAnchor =
'leftMargin: units.gu(1); \n';
470 }
else if (headerAsOverlay && !hasEmblem) {
471 extraRightAnchor =
'rightMargin: units.gu(1); \n';
475 titleContainerAnchors =
'verticalCenter: parent.verticalCenter; ';
477 titleContainerAnchors =
'right: parent.right; ';
478 titleContainerAnchors += headerLeftAnchor;
479 titleContainerAnchors += headerVerticalAnchors;
480 if (!headerLeftAnchorHasMargin) {
481 titleContainerAnchors += extraLeftAnchor;
485 titleRightAnchor =
'right: emblemIcon.left; \n\
486 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
488 titleRightAnchor =
'right: parent.right; \n'
489 titleRightAnchor += extraRightAnchor;
492 if (hasTitleContainer) {
494 titleAnchors = titleRightAnchor;
495 titleAnchors +=
'left: parent.left; \n\
497 subtitleAnchors =
'right: parent.right; \n\
498 left: parent.left; \n';
499 subtitleAnchors += extraRightAnchor;
501 attributesAnchors = subtitleAnchors +
'top: subtitleLabel.bottom;\n';
502 subtitleAnchors +=
'top: titleLabel.bottom;\n';
504 attributesAnchors = subtitleAnchors +
'top: titleLabel.bottom;\n';
506 }
else if (hasMascot) {
508 titleAnchors =
'verticalCenter: parent.verticalCenter;\n';
510 if (headerAsOverlay) {
512 titleAnchors = titleRightAnchor;
513 titleAnchors +=
'left: parent.left; \n\
514 leftMargin: units.gu(1); \n\
515 top: overlayLoader.top; \n\
516 topMargin: units.gu(1);\n';
519 titleAnchors = titleRightAnchor;
520 titleAnchors += headerLeftAnchor;
521 titleAnchors += headerVerticalAnchors;
522 if (!headerLeftAnchorHasMargin) {
523 titleAnchors += extraLeftAnchor;
526 subtitleAnchors =
'left: titleLabel.left; \n\
527 leftMargin: titleLabel.leftMargin; \n';
528 subtitleAnchors += extraRightAnchor;
531 subtitleAnchors +=
'right: parent.right; \n';
533 subtitleAnchors +=
'right: titleLabel.right; \n';
537 attributesAnchors = subtitleAnchors +
'top: subtitleLabel.bottom;\n';
538 subtitleAnchors +=
'top: titleLabel.bottom;\n';
540 attributesAnchors = subtitleAnchors +
'top: titleLabel.bottom;\n';
545 var titleLabelVisibleExtra = (headerAsOverlay ?
'&& overlayLoader.active':
'');
546 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
551 var containerCode = [];
552 var containerHeight =
'titleLabel.height';
553 containerCode.push(titleCode);
555 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
556 containerCode.push(subtitleCode);
557 containerHeight +=
' + subtitleLabel.height';
560 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
563 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
564 containerCode.push(attributesCode);
565 containerHeight +=
' + attributesRow.height';
568 if (hasTitleContainer) {
570 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
573 titleSubtitleCode = titleCode;
575 titleSubtitleCode += subtitleCode;
578 titleSubtitleCode += attributesCode;
584 var rowCode = [mascotCode, titleSubtitleCode];
585 if (mascotShapeCode !=
'') {
586 rowCode.unshift(mascotShapeCode);
588 code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
590 code += mascotShapeCode + mascotCode + titleSubtitleCode;
594 var summaryTopAnchor;
595 if (isHorizontal && hasArt) summaryTopAnchor =
'artShapeHolder.bottom';
596 else if (headerAsOverlay && hasArt) summaryTopAnchor =
'artShapeHolder.bottom';
597 else if (hasHeaderRow) summaryTopAnchor =
'row.bottom';
598 else if (hasTitleContainer) summaryTopAnchor =
'headerTitleContainer.bottom';
599 else if (hasMascot) summaryTopAnchor =
'mascotImage.bottom';
600 else if (hasAttributes) summaryTopAnchor =
'attributesRow.bottom';
601 else if (hasSubtitle) summaryTopAnchor =
'subtitleLabel.bottom';
602 else if (hasTitle) summaryTopAnchor =
'titleLabel.bottom';
603 else if (hasArt) summaryTopAnchor =
'artShapeHolder.bottom';
604 else summaryTopAnchor =
'parent.top';
608 summaryColor = summaryColorWithBackground;
610 summaryColor =
'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
613 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ?
'anchors.margins' :
'0');
615 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
618 var touchdownAnchors;
620 touchdownAnchors =
'fill: backgroundLoader';
621 }
else if (touchdownOnArtShape) {
622 touchdownAnchors =
'fill: artShapeHolder';
624 touchdownAnchors =
'fill: root'
626 code += kTouchdownCode.arg(touchdownAnchors);
628 var implicitHeight =
'implicitHeight: ';
630 implicitHeight +=
'summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
631 }
else if (hasHeaderRow) {
632 implicitHeight +=
'row.y + row.height + units.gu(1);\n';
633 }
else if (hasMascot) {
634 implicitHeight +=
'mascotImage.y + mascotImage.height;\n';
635 }
else if (hasTitleContainer) {
636 implicitHeight +=
'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
637 }
else if (hasAttributes) {
638 implicitHeight +=
'attributesRow.y + attributesRow.height + units.gu(1);\n';
639 }
else if (hasSubtitle) {
640 implicitHeight +=
'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
641 }
else if (hasTitle) {
642 implicitHeight +=
'titleLabel.y + titleLabel.height + units.gu(1);\n';
644 implicitHeight +=
'artShapeHolder.height;\n';
650 code += implicitHeight +
'}\n';
655 function createCardComponent(parent,
template, components) {
656 var imports =
'import QtQuick 2.2; \n\
657 import Ubuntu.Components 1.1; \n\
659 import Utils 0.1;\n';
660 var card = cardString(
template, components);
661 var code = imports +
'Component {\n' + card +
'}\n';
664 return Qt.createQmlObject(code, parent,
"createCardComponent");
666 console.error(
"ERROR: Invalid component created.");
667 console.error(
"Template:");
668 console.error(JSON.stringify(
template));
669 console.error(
"Components:");
670 console.error(JSON.stringify(components));
671 console.error(
"Code:");