2 * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
19 // %1 is the template["card-background"]["elements"][0]
20 // %2 is the template["card-background"]["elements"][1]
21 // %3 is whether the loader should be asynchronous or not
22 // %4 is the template["card-background"] string
23 var kBackgroundLoaderCode = 'Loader {\n\
24 id: backgroundLoader; \n\
25 objectName: "backgroundLoader"; \n\
26 anchors.fill: parent; \n\
28 visible: status === Loader.Ready; \n\
29 sourceComponent: UbuntuShape { \n\
30 objectName: "background"; \n\
33 switch (root.backgroundShapeStyle) { \n\
34 case "inset": return UbuntuShape.Inset; \n\
35 case "shadow": return UbuntuShape.DropShadow; \n\
37 case "flat": return UbuntuShape.Flat; \n\
40 backgroundColor: getColor(0) || "white"; \n\
41 secondaryBackgroundColor: getColor(1) || backgroundColor; \n\
42 backgroundMode: UbuntuShape.VerticalGradient; \n\
43 anchors.fill: parent; \n\
44 source: backgroundImage.source ? backgroundImage : null; \n\
45 property real luminance: Style.luminance(backgroundColor); \n\
46 property Image backgroundImage: Image { \n\
47 objectName: "backgroundImage"; \n\
49 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
53 function getColor(index) { \n\
54 if (cardData && typeof cardData["background"] === "object" \n\
55 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
56 return cardData["background"]["elements"][index]; \n\
57 } else return index === 0 ? %1 : %2; \n\
62 // %1 is the aspect of the UbuntuShape
63 var kArtUbuntuShapeCode = 'UbuntuShape { \n\
64 anchors.fill: parent; \n\
66 sourceFillMode: UbuntuShape.PreserveAspectCrop; \n\
71 var kArtProportionalShapeCode = 'ProportionalShape { \n\
72 anchors.left: parent.left; \n\
73 anchors.right: parent.right; \n\
75 aspect: UbuntuShape.DropShadow; \n\
78 // %1 is used as anchors of artShapeLoader
79 // %2 is used as image width
80 // %3 is used as image height
81 // %4 is whether the image should be visible
82 // %5 is whether the loader should be asynchronous or not
83 // %6 is the shape code we want to use
84 // %7 is injected as code to artImage
85 // %8 is used as image fallback
86 var kArtShapeHolderCode = 'Loader { \n\
87 id: artShapeLoader; \n\
88 height: root.fixedArtShapeSize.height; \n\
89 width: root.fixedArtShapeSize.width; \n\
91 objectName: "artShapeLoader"; \n\
92 readonly property string cardArt: cardData && cardData["art"] || %8; \n\
93 onCardArtChanged: { if (item) { item.image.source = cardArt; } } \n\
94 active: cardArt != ""; \n\
96 visible: status === Loader.Ready; \n\
97 sourceComponent: Item { \n\
99 objectName: "artShape"; \n\
100 visible: image.status === Image.Ready; \n\
101 readonly property alias image: artImage; \n\
103 width: root.fixedArtShapeSize.width; \n\
104 height: root.fixedArtShapeSize.height; \n\
105 CroppedImageMinimumSourceSize { \n\
107 objectName: "artImage"; \n\
108 source: artShapeLoader.cardArt; \n\
109 asynchronous: %5; \n\
118 // %1 is used as anchors of artShapeLoader
119 // %2 is used as image width
120 // %3 is used as image height
121 // %4 is whether the image should be visible
122 // %5 is whether the loader should be asynchronous or not
123 // %6 is the shape code we want to use
124 // %7 is injected as code to artImage
125 // %8 is used as image fallback
126 var kArtShapeHolderCodeCardToolCard = 'Loader { \n\
127 id: artShapeLoader; \n\
129 objectName: "artShapeLoader"; \n\
130 readonly property string cardArt: cardData && cardData["art"] || %8; \n\
131 onCardArtChanged: { if (item) { item.image.source = cardArt; } } \n\
132 active: cardArt != ""; \n\
133 asynchronous: %5; \n\
134 visible: status === Loader.Ready; \n\
135 sourceComponent: Item { \n\
137 objectName: "artShape"; \n\
138 visible: image.status === Image.Ready; \n\
139 readonly property alias image: artImage; \n\
141 width: image.status !== Image.Ready ? 0 : image.width; \n\
142 height: image.status !== Image.Ready ? 0 : image.height; \n\
143 CroppedImageMinimumSourceSize { \n\
145 objectName: "artImage"; \n\
146 source: artShapeLoader.cardArt; \n\
147 asynchronous: %5; \n\
156 // %1 is anchors.fill
159 // %4 is whether the icon should be asynchronous or not
160 var kAudioButtonCode = 'AbstractButton { \n\
162 anchors.fill: %1; \n\
165 readonly property url source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
167 anchors.fill: parent; \n\
168 visible: parent.pressed; \n\
172 color: Qt.rgba(0, 0, 0, 0.5); \n\
173 anchors.centerIn: parent; \n\
174 width: parent.width * 0.5; \n\
176 radius: width / 2; \n\
179 anchors.centerIn: parent; \n\
180 width: parent.width * 0.3; \n\
183 name: DashAudioPlayer.playing && AudioUrlComparer.compare(parent.source, DashAudioPlayer.currentSource) ? "media-playback-pause" : "media-playback-start"; \n\
185 asynchronous: %4; \n\
188 if (AudioUrlComparer.compare(source, DashAudioPlayer.currentSource)) { \n\
189 if (DashAudioPlayer.playing) { \n\
190 DashAudioPlayer.pause(); \n\
192 DashAudioPlayer.play(); \n\
195 var playlist = (cardData["quickPreviewData"] && cardData["quickPreviewData"]["playlist"]) || null; \n\
196 DashAudioPlayer.playSource(source, playlist); \n\
199 onPressAndHold: { \n\
200 root.pressAndHold(); \n\
204 // %1 is whether the loader should be asynchronous or not
205 // %2 is the header height code
206 var kOverlayLoaderCode = 'Loader { \n\
207 id: overlayLoader; \n\
208 readonly property real overlayHeight: %2 + units.gu(2); \n\
209 anchors.fill: artShapeLoader; \n\
210 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
211 asynchronous: %1; \n\
212 visible: showHeader && status === Loader.Ready; \n\
213 sourceComponent: UbuntuShapeOverlay { \n\
215 property real luminance: Style.luminance(overlayColor); \n\
216 aspect: UbuntuShape.Flat; \n\
218 overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
219 overlayRect: Qt.rect(0, 1 - overlayLoader.overlayHeight / height, 1, 1); \n\
223 // multiple row version of HeaderRowCode
224 function kHeaderRowCodeGenerator() {
225 var kHeaderRowCodeTemplate = 'Row { \n\
227 objectName: "outerRow"; \n\
228 property real margins: units.gu(1); \n\
229 spacing: margins; \n\
232 anchors.right: parent.right; \n\
233 anchors.margins: margins; \n\
234 anchors.rightMargin: 0; \n\
239 var args = Array.prototype.slice.call(arguments);
240 var isCardTool = args.shift();
241 var heightCode = isCardTool ? "" : "height: root.fixedHeaderHeight; \n";
242 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(heightCode).arg(args.join(',\n'));
246 // multiple item version of kHeaderContainerCode
247 function kHeaderContainerCodeGenerator() {
248 var headerContainerCodeTemplate = 'Item { \n\
249 id: headerTitleContainer; \n\
251 width: parent.width - x; \n\
252 implicitHeight: %2; \n\
257 var args = Array.prototype.slice.call(arguments);
258 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(',\n'));
262 // %1 is used as anchors of mascotShapeLoader
263 // %2 is whether the loader should be asynchronous or not
264 var kMascotShapeLoaderCode = 'Loader { \n\
265 id: mascotShapeLoader; \n\
266 objectName: "mascotShapeLoader"; \n\
267 asynchronous: %2; \n\
268 active: mascotImage.status === Image.Ready; \n\
269 visible: showHeader && active && status === Loader.Ready; \n\
270 width: units.gu(6); \n\
271 height: units.gu(5.625); \n\
272 sourceComponent: UbuntuShape { aspect: UbuntuShape.Flat; image: mascotImage } \n\
276 // %1 is used as anchors of mascotImage
277 // %2 is used as visible of mascotImage
278 // %3 is injected as code to mascotImage
279 // %4 is used as fallback image
280 var kMascotImageCode = 'CroppedImageMinimumSourceSize { \n\
282 objectName: "mascotImage"; \n\
284 source: cardData && cardData["mascot"] || %4; \n\
285 width: units.gu(6); \n\
286 height: units.gu(5.625); \n\
287 horizontalAlignment: Image.AlignHCenter; \n\
288 verticalAlignment: Image.AlignVCenter; \n\
293 // %1 is used as anchors of titleLabel
294 // %2 is used as color of titleLabel
295 // %3 is used as extra condition for visible of titleLabel
296 // %4 is used as title width
297 // %5 is used as horizontal alignment
298 var kTitleLabelCode = 'Label { \n\
300 objectName: "titleLabel"; \n\
302 elide: Text.ElideRight; \n\
303 fontSize: "small"; \n\
304 wrapMode: Text.Wrap; \n\
305 maximumLineCount: 2; \n\
306 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
308 visible: showHeader %3; \n\
310 text: root.title; \n\
311 font.weight: Font.Normal; \n\
312 horizontalAlignment: %5; \n\
315 // %1 is used as extra anchors of emblemIcon
316 // %2 is used as color of emblemIcon
317 // FIXME The width code is a
318 // Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
319 var kEmblemIconCode = 'Icon { \n\
321 objectName: "emblemIcon"; \n\
323 bottom: titleLabel.baseline; \n\
324 right: parent.right; \n\
327 source: cardData && cardData["emblem"] || ""; \n\
329 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
330 width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
333 // %1 is used as anchors of touchdown effect
334 var kTouchdownCode = 'Loader { \n\
335 active: root.pressed; \n\
337 sourceComponent: UbuntuShape { \n\
338 objectName: "touchdown"; \n\
339 anchors.fill: parent; \n\
341 borderSource: "radius_pressed.sci" \n\
345 // %1 is used as anchors of subtitleLabel
346 // %2 is used as color of subtitleLabel
347 var kSubtitleLabelCode = 'Label { \n\
348 id: subtitleLabel; \n\
349 objectName: "subtitleLabel"; \n\
351 anchors.topMargin: units.dp(2); \n\
352 elide: Text.ElideRight; \n\
353 maximumLineCount: 1; \n\
354 fontSize: "x-small"; \n\
355 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
357 visible: titleLabel.visible && titleLabel.text; \n\
358 text: cardData && cardData["subtitle"] || ""; \n\
359 font.weight: Font.Light; \n\
362 // %1 is used as anchors of attributesRow
363 // %2 is used as color of attributesRow
364 var kAttributesRowCode = 'CardAttributes { \n\
365 id: attributesRow; \n\
366 objectName: "attributesRow"; \n\
369 fontScale: root.fontScale; \n\
370 model: cardData && cardData["attributes"]; \n\
373 // %1 is used as anchors of socialActionsRow
374 // %2 is used as color of socialActionsRow
375 var kSocialActionsRowCode = 'CardSocialActions { \n\
376 id: socialActionsRow; \n\
377 objectName: "socialActionsRow"; \n\
380 model: cardData && cardData["socialActions"]; \n\
381 onClicked: root.action(actionId); \n\
384 // %1 is used as top anchor of summary
385 // %2 is used as topMargin anchor of summary
386 // %3 is used as color of summary
387 var kSummaryLabelCode = 'Label { \n\
389 objectName: "summaryLabel"; \n\
392 left: parent.left; \n\
393 right: parent.right; \n\
394 margins: units.gu(1); \n\
397 wrapMode: Text.Wrap; \n\
398 maximumLineCount: 5; \n\
399 elide: Text.ElideRight; \n\
400 text: cardData && cardData["summary"] || ""; \n\
401 height: text ? implicitHeight : 0; \n\
402 fontSize: "x-small"; \n\
403 font.weight: Font.Light; \n\
407 // %1 is used as bottom anchor of audio progress bar
408 // %2 is used as left anchor of audio progress bar
409 // %3 is used as text color
410 var kAudioProgressBarCode = 'CardAudioProgress { \n\
411 id: audioProgressBar; \n\
412 duration: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["duration"]) || 0; \n\
413 source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
417 right: parent.right; \n\
418 margins: units.gu(1); \n\
423 function sanitizeColor(colorString) {
424 if (colorString !== undefined) {
425 if (colorString.match(/^[#a-z0-9]*$/i) === null) {
426 // This is not the perfect regexp for color
427 // but what we're trying to do here is just protect
428 // against injection so it's ok
435 function cardString(template, components, isCardTool, artShapeStyle, categoryLayout) {
438 var templateInteractive = (template == null ? true : (template["non-interactive"] !== undefined ? !template["non-interactive"] : true)) ? "true" : "false";
440 code = 'AbstractButton { \n\
442 property var cardData; \n\
443 property string backgroundShapeStyle: "flat"; \n\
444 property real fontScale: 1.0; \n\
445 property var scopeStyle: null; \n\
446 readonly property string title: cardData && cardData["title"] || ""; \n\
447 property bool showHeader: true; \n\
448 implicitWidth: childrenRect.width; \n\
450 \n'.arg(templateInteractive);
453 code += "property int fixedHeaderHeight: -1; \n\
454 property size fixedArtShapeSize: Qt.size(-1, -1); \n";
457 var hasArt = components["art"] && components["art"]["field"] || false;
458 var hasSummary = components["summary"] || false;
459 var isConciergeMode = components["art"] && components["art"]["conciergeMode"] || false;
460 var artAndSummary = hasArt && hasSummary && !isConciergeMode;
461 var isHorizontal = template["card-layout"] === "horizontal";
462 var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
463 (hasSummary && (template["card-background"] || components["background"]));
464 var hasTitle = components["title"] || false;
465 var hasMascot = components["mascot"] || false;
466 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
467 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
468 var hasSubtitle = hasTitle && components["subtitle"] || false;
469 var hasHeaderRow = hasMascot && hasTitle;
470 var hasAttributes = hasTitle && components["attributes"] && components["attributes"]["field"] || false;
471 var hasSocialActions = hasTitle && components["social-actions"] || false;
472 var isAudio = template["quick-preview-type"] === "audio";
473 var asynchronous = isCardTool ? "false" : "true";
475 code += 'signal action(var actionId);\n';
477 // For now we only support audio cards with [optional] art, title, subtitle
478 // in horizontal mode
479 // Anything else makes it behave not like an audio card
480 if (hasSummary) isAudio = false;
481 if (!isHorizontal) isAudio = false;
482 if (hasMascot) isAudio = false;
483 if (hasEmblem) isAudio = false;
484 if (headerAsOverlay) isAudio = false;
485 if (hasAttributes) isAudio = false;
489 var templateCardBackground;
490 if (template && typeof template["card-background"] === "string") {
491 templateCardBackground = 'decodeURI("' + encodeURI(template["card-background"]) + '")';
493 templateCardBackground = '""';
496 var backgroundElements0;
497 var backgroundElements1;
498 if (template && typeof template["card-background"] === "object" && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) {
499 var element0 = sanitizeColor(template["card-background"]["elements"][0]);
500 var element1 = sanitizeColor(template["card-background"]["elements"][1]);
501 if (element0 !== undefined) {
502 backgroundElements0 = '"%1"'.arg(element0);
504 if (element1 !== undefined) {
505 backgroundElements1 = '"%1"'.arg(element1);
508 code += kBackgroundLoaderCode.arg(backgroundElements0).arg(backgroundElements1).arg(asynchronous).arg(templateCardBackground);
514 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
516 var artShapeAspect = components["art"] && components["art"]["aspect-ratio"] || 1;
517 if (isNaN(artShapeAspect)) {
521 artShapeAspect = "(root.fixedArtShapeSize.width / root.fixedArtShapeSize.height)";
524 var widthCode, heightCode;
527 artAnchors = 'left: parent.left';
528 if (hasMascot || hasTitle) {
529 widthCode = 'height * ' + artShapeAspect;
530 heightCode = 'headerHeight + 2 * units.gu(1)';
532 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
533 // So we define a "random" height of the image height + 2 gu for the margins
534 widthCode = 'height * ' + artShapeAspect
535 heightCode = 'units.gu(7.625)';
538 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
539 widthCode = 'root.width'
540 heightCode = 'width / ' + artShapeAspect;
543 var fallback = !isCardTool && components["art"] && components["art"]["fallback"] || "";
544 fallback = encodeURI(fallback);
545 var fallbackStatusCode = "";
546 var fallbackURICode = '""';
547 if (fallback !== "") {
548 // fallbackStatusCode has %8 in it because we want to substitute it for fallbackURICode
549 // which in kArtShapeHolderCode is %8
550 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %8;';
551 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
553 var artShapeHolderShapeCode;
554 if (!isConciergeMode) {
555 if (artShapeStyle === "icon") {
556 artShapeHolderShapeCode = kArtProportionalShapeCode;
558 var artShapeHolderShapeAspect;
559 switch (artShapeStyle) {
560 case "inset": artShapeHolderShapeAspect = "UbuntuShape.Inset"; break;
561 case "shadow": artShapeHolderShapeAspect = "UbuntuShape.DropShadow"; break;
563 case "flat": artShapeHolderShapeAspect = "UbuntuShape.Flat"; break;
565 artShapeHolderShapeCode = kArtUbuntuShapeCode.arg(artShapeHolderShapeAspect);
568 artShapeHolderShapeCode = "";
570 var artShapeHolderCode = isCardTool ? kArtShapeHolderCodeCardToolCard : kArtShapeHolderCode;
571 code += artShapeHolderCode.arg(artAnchors)
574 .arg(isConciergeMode ? "true" : "false")
576 .arg(artShapeHolderShapeCode)
577 .arg(fallbackStatusCode)
578 .arg(fallbackURICode);
579 } else if (isCardTool) {
580 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
583 if (headerAsOverlay) {
584 var headerHeightCode = isCardTool ? "headerHeight" : "root.fixedHeaderHeight";
585 code += kOverlayLoaderCode.arg(asynchronous).arg(headerHeightCode);
588 var headerVerticalAnchors;
589 if (headerAsOverlay) {
590 headerVerticalAnchors = 'bottom: artShapeLoader.bottom; \n\
591 bottomMargin: units.gu(1);\n';
595 headerVerticalAnchors = 'top: artShapeLoader.top; \n\
596 topMargin: units.gu(1);\n';
598 headerVerticalAnchors = 'top: artShapeLoader.bottom; \n\
599 topMargin: units.gu(1);\n';
602 headerVerticalAnchors = 'top: parent.top; \n\
603 topMargin: units.gu(1);\n';
607 var headerLeftAnchor;
608 var headerLeftAnchorHasMargin = false;
609 if (isHorizontal && hasArt) {
610 headerLeftAnchor = 'left: artShapeLoader.right; \n\
611 leftMargin: units.gu(1);\n';
612 headerLeftAnchorHasMargin = true;
613 } else if (isHorizontal && isAudio) {
614 headerLeftAnchor = 'left: audioButton.right; \n\
615 leftMargin: units.gu(1);\n';
616 headerLeftAnchorHasMargin = true;
618 headerLeftAnchor = 'left: parent.left;\n';
621 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary && !isAudio;
624 code += 'readonly property int headerHeight: row.height;\n'
625 } else if (hasMascot) {
626 code += 'readonly property int headerHeight: mascotImage.height;\n'
627 } else if (hasAttributes) {
628 if (hasTitle && hasSubtitle) {
629 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
630 } else if (hasTitle) {
631 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
633 code += 'readonly property int headerHeight: attributesRow.height;\n'
635 } else if (isAudio) {
637 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
638 } else if (hasTitle) {
639 code += 'readonly property int headerHeight: titleLabel.height + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
641 code += 'readonly property int headerHeight: audioProgressBar.height;\n'
643 } else if (hasSubtitle) {
644 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
645 } else if (hasTitle) {
646 code += 'readonly property int headerHeight: titleLabel.height;\n'
648 code += 'readonly property int headerHeight: 0;\n'
651 var mascotShapeCode = '';
654 var useMascotShape = !hasBackground && !headerAsOverlay;
655 var mascotAnchors = '';
657 mascotAnchors += headerLeftAnchor;
658 mascotAnchors += headerVerticalAnchors;
659 if (!headerLeftAnchorHasMargin) {
660 mascotAnchors += 'leftMargin: units.gu(1);\n'
663 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
666 if (useMascotShape) {
667 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors).arg(asynchronous);
670 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
671 var fallback = !isCardTool && components["mascot"] && components["mascot"]["fallback"] || "";
672 fallback = encodeURI(fallback);
673 var fallbackStatusCode = "";
674 var fallbackURICode = '""';
675 if (fallback !== "") {
676 // fallbackStatusCode has %4 in it because we want to substitute it for fallbackURICode
677 // which in kMascotImageCode is %4
678 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %4;';
679 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
681 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible).arg(fallbackStatusCode).arg(fallbackURICode);
684 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")';
686 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
687 var titleSubtitleCode = '';
690 if (headerAsOverlay) {
691 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
692 } else if (hasSummary) {
693 titleColor = 'summary.color';
694 } else if (hasBackground) {
695 titleColor = summaryColorWithBackground;
697 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
702 var attributesAnchors;
703 var titleContainerAnchors;
704 var titleRightAnchor;
705 var titleWidth = "undefined";
707 var extraRightAnchor = '';
708 var extraLeftAnchor = '';
709 if (!touchdownOnArtShape) {
710 extraRightAnchor = 'rightMargin: units.gu(1); \n';
711 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
712 } else if (headerAsOverlay && !hasEmblem) {
713 extraRightAnchor = 'rightMargin: units.gu(1); \n';
717 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
719 titleContainerAnchors = 'right: parent.right; ';
720 titleContainerAnchors += headerLeftAnchor;
721 titleContainerAnchors += headerVerticalAnchors;
722 if (!headerLeftAnchorHasMargin) {
723 titleContainerAnchors += extraLeftAnchor;
727 titleRightAnchor = 'right: emblemIcon.left; \n\
728 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
730 titleRightAnchor = 'right: parent.right; \n'
731 titleRightAnchor += extraRightAnchor;
734 if (hasTitleContainer) {
735 // Using headerTitleContainer
736 titleAnchors = titleRightAnchor;
737 titleAnchors += 'left: parent.left; \n\
739 subtitleAnchors = 'right: parent.right; \n\
740 left: parent.left; \n';
741 subtitleAnchors += extraRightAnchor;
743 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
744 subtitleAnchors += 'top: titleLabel.bottom;\n';
746 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
748 } else if (hasMascot) {
749 // Using row without titleContainer
750 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
751 titleWidth = "parent.width - x";
753 if (headerAsOverlay) {
754 // Using anchors to the overlay
755 titleAnchors = titleRightAnchor;
756 titleAnchors += 'left: parent.left; \n\
757 leftMargin: units.gu(1); \n\
758 top: overlayLoader.top; \n\
759 topMargin: units.gu(1) + overlayLoader.height - overlayLoader.overlayHeight; \n';
761 // Using anchors to the mascot/parent
762 titleAnchors = titleRightAnchor;
763 titleAnchors += headerLeftAnchor;
764 titleAnchors += headerVerticalAnchors;
765 if (!headerLeftAnchorHasMargin) {
766 titleAnchors += extraLeftAnchor;
769 subtitleAnchors = 'left: titleLabel.left; \n\
770 leftMargin: titleLabel.leftMargin; \n';
771 subtitleAnchors += extraRightAnchor;
774 subtitleAnchors += 'right: parent.right; \n';
776 subtitleAnchors += 'right: titleLabel.right; \n';
780 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
781 subtitleAnchors += 'top: titleLabel.bottom;\n';
783 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
787 var titleAlignment = "Text.AlignHCenter";
788 if (template["card-layout"] === "horizontal"
789 || typeof components["title"] !== "object"
790 || components["title"]["align"] === "left") titleAlignment = "Text.AlignLeft";
791 var keys = ["mascot", "emblem", "subtitle", "attributes", "summary"];
792 for (var key in keys) {
795 if (typeof components[key] === "string"
796 || typeof components[key]["field"] === "string") titleAlignment = "Text.AlignLeft";
802 // code for different elements
803 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
804 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra).arg(titleWidth).arg(titleAlignment);
808 // code for the title container
809 var containerCode = [];
810 var containerHeight = 'titleLabel.height';
811 containerCode.push(titleCode);
813 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
814 containerCode.push(subtitleCode);
815 containerHeight += ' + subtitleLabel.height';
818 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
821 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
822 containerCode.push(attributesCode);
823 containerHeight += ' + attributesRow.height';
826 if (hasTitleContainer) {
828 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
831 titleSubtitleCode = titleCode;
833 titleSubtitleCode += subtitleCode;
836 titleSubtitleCode += attributesCode;
842 var rowCode = [mascotCode, titleSubtitleCode];
843 if (mascotShapeCode != '') {
844 rowCode.unshift(mascotShapeCode);
846 code += kHeaderRowCodeGenerator(isCardTool, headerVerticalAnchors + headerLeftAnchor, rowCode)
848 code += mascotShapeCode + mascotCode + titleSubtitleCode;
852 var audioProgressBarLeftAnchor = 'audioButton.right';
853 var audioProgressBarBottomAnchor = 'audioButton.bottom';
854 var audioProgressBarTextColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
856 code += kAudioProgressBarCode.arg(audioProgressBarBottomAnchor)
857 .arg(audioProgressBarLeftAnchor)
858 .arg(audioProgressBarTextColor);
860 var audioButtonAnchorsFill;
861 var audioButtonWidth;
862 var audioButtonHeight;
864 audioButtonAnchorsFill = 'artShapeLoader';
865 audioButtonWidth = 'undefined';
866 audioButtonHeight = 'undefined';
868 audioButtonAnchorsFill = 'undefined';
869 audioButtonWidth = 'height';
870 audioButtonHeight = isCardTool ? 'headerHeight + 2 * units.gu(1)'
871 : 'root.fixedHeaderHeight + 2 * units.gu(1)';
873 code += kAudioButtonCode.arg(audioButtonAnchorsFill).arg(audioButtonWidth).arg(audioButtonHeight).arg(asynchronous);
877 var summaryTopAnchor;
878 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeLoader.bottom';
879 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeLoader.bottom';
880 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
881 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
882 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
883 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
884 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
885 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
886 else if (hasArt) summaryTopAnchor = 'artShapeLoader.bottom';
887 else summaryTopAnchor = 'parent.top';
891 summaryColor = summaryColorWithBackground;
893 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
896 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
898 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
901 if (hasSocialActions) {
905 if (hasSummary) socialTopAnchor = 'summary.bottom;';
906 else if (isHorizontal && hasArt) socialTopAnchor = 'artShapeLoader.bottom;';
907 else if (headerAsOverlay && hasArt) socialTopAnchor = 'artShapeLoader.bottom;';
908 else if (hasHeaderRow) socialTopAnchor = 'row.bottom;';
909 else if (hasTitleContainer) socialTopAnchor = 'headerTitleContainer.bottom;';
910 else if (hasMascot) socialTopAnchor = 'mascotImage.bottom;';
911 else if (hasAttributes) socialTopAnchor = 'attributesRow.bottom;';
912 else if (hasSubtitle) socialTopAnchor = 'subtitleLabel.bottom;';
913 else if (hasTitle) socialTopAnchor = 'titleLabel.bottom;';
914 else if (hasArt) socialTopAnchor = 'artShapeLoader.bottom;';
915 else socialTopAnchor = 'parent.top';
917 socialAnchors = 'top: ' + socialTopAnchor + ' left: parent.left; right: parent.right; topMargin: units.gu(1);'
921 socialColor = summaryColorWithBackground;
923 socialColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
926 code += kSocialActionsRowCode.arg(socialAnchors).arg(socialColor);
929 if (artShapeStyle != "shadow" && artShapeStyle != "icon" && !isCardTool) {
930 var touchdownAnchors;
932 touchdownAnchors = 'fill: backgroundLoader';
933 } else if (touchdownOnArtShape) {
934 touchdownAnchors = 'fill: artShapeLoader';
936 touchdownAnchors = 'fill: root'
938 code += kTouchdownCode.arg(touchdownAnchors);
941 if (isCardTool || categoryLayout !== "grid") {
942 var implicitHeight = 'implicitHeight: ';
943 if (hasSocialActions) {
944 implicitHeight += 'socialActionsRow.y + socialActionsRow.height + units.gu(1);\n';
945 } else if (hasSummary) {
946 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
947 } else if (isAudio) {
948 implicitHeight += 'audioButton.height;\n';
949 } else if (headerAsOverlay) {
950 implicitHeight += 'artShapeLoader.height;\n';
951 } else if (hasHeaderRow) {
952 implicitHeight += 'row.y + row.height + units.gu(1);\n';
953 } else if (hasMascot) {
954 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
955 } else if (hasTitleContainer) {
956 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
957 } else if (hasAttributes) {
958 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
959 } else if (hasSubtitle) {
960 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
961 } else if (hasTitle) {
962 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
964 implicitHeight += 'artShapeLoader.height;\n';
972 // Close the AbstractButton
973 code += implicitHeight + '}\n';
978 function createCardComponent(parent, template, components, isCardTool, artShapeStyle, categoryLayout, identifier) {
979 var imports = 'import QtQuick 2.4; \n\
980 import Ubuntu.Components 1.3; \n\
981 import Ubuntu.Settings.Components 0.1; \n\
983 import Utils 0.1;\n';
984 var card = cardString(template, components, isCardTool, artShapeStyle, categoryLayout);
985 var code = imports + 'Component {\n' + card + '}\n';
988 return Qt.createQmlObject(code, parent, identifier);
990 console.error("ERROR: Invalid component created.");
991 console.error("Template:");
992 console.error(JSON.stringify(template));
993 console.error("Components:");
994 console.error(JSON.stringify(components));
995 console.error("Code:");