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
23var 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
63var kArtUbuntuShapeCode = 'UbuntuShape { \n\
64 anchors.fill: parent; \n\
66 sourceFillMode: UbuntuShape.PreserveAspectCrop; \n\
71var kArtProportionalShapeCode = 'ProportionalShape { \n\
72 anchors.fill: parent; \n\
74 aspect: UbuntuShape.DropShadow; \n\
77// %1 is used as anchors of artShapeHolder
78// %2 is used as image width
79// %3 is used as image height
80// %4 is whether the image should be visible
81// %5 is whether the loader should be asynchronous or not
82// %6 is the shape code we want to use
83// %7 is injected as code to artImage
84// %8 is used as image fallback
85var kArtShapeHolderCode = 'Item { \n\
86 id: artShapeHolder; \n\
87 height: root.fixedArtShapeSize.height; \n\
88 width: root.fixedArtShapeSize.width; \n\
91 id: artShapeLoader; \n\
92 objectName: "artShapeLoader"; \n\
93 readonly property string cardArt: cardData && cardData["art"] || %8; \n\
94 onCardArtChanged: { if (item) { item.image.source = cardArt; } } \n\
95 active: cardArt != ""; \n\
97 visible: status == Loader.Ready; \n\
98 sourceComponent: Item { \n\
100 objectName: "artShape"; \n\
101 visible: image.status == Image.Ready; \n\
102 readonly property alias image: artImage; \n\
104 width: root.fixedArtShapeSize.width; \n\
105 height: root.fixedArtShapeSize.height; \n\
106 CroppedImageMinimumSourceSize { \n\
108 objectName: "artImage"; \n\
109 source: artShapeLoader.cardArt; \n\
110 asynchronous: %5; \n\
120// %1 is used as anchors of artShapeHolder
121// %2 is used as image width
122// %3 is used as image height
123// %4 is whether the image should be visible
124// %5 is whether the loader should be asynchronous or not
125// %6 is the shape code we want to use
126// %7 is injected as code to artImage
127// %8 is used as image fallback
128var kArtShapeHolderCodeCardToolCard = 'Item { \n\
129 id: artShapeHolder; \n\
130 height: artShapeLoader.height; \n\
131 width: artShapeLoader.width; \n\
134 id: artShapeLoader; \n\
135 objectName: "artShapeLoader"; \n\
136 readonly property string cardArt: cardData && cardData["art"] || %8; \n\
137 onCardArtChanged: { if (item) { item.image.source = cardArt; } } \n\
138 active: cardArt != ""; \n\
139 asynchronous: %5; \n\
140 visible: status == Loader.Ready; \n\
141 sourceComponent: Item { \n\
143 objectName: "artShape"; \n\
144 visible: image.status == Image.Ready; \n\
145 readonly property alias image: artImage; \n\
147 width: image.status !== Image.Ready ? 0 : image.width; \n\
148 height: image.status !== Image.Ready ? 0 : image.height; \n\
149 CroppedImageMinimumSourceSize { \n\
151 objectName: "artImage"; \n\
152 source: artShapeLoader.cardArt; \n\
153 asynchronous: %5; \n\
166// %4 is whether the icon should be asynchronous or not
167var kAudioButtonCode = 'AbstractButton { \n\
169 anchors.fill: %1; \n\
172 readonly property url source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
174 anchors.fill: parent; \n\
175 visible: parent.pressed; \n\
176 radius: "medium"; \n\
179 color: Qt.rgba(0, 0, 0, 0.5); \n\
180 anchors.centerIn: parent; \n\
181 width: parent.width * 0.5; \n\
183 radius: width / 2; \n\
186 anchors.centerIn: parent; \n\
187 width: parent.width * 0.3; \n\
190 name: DashAudioPlayer.playing && AudioUrlComparer.compare(parent.source, DashAudioPlayer.currentSource) ? "media-playback-pause" : "media-playback-start"; \n\
192 asynchronous: %4; \n\
195 if (AudioUrlComparer.compare(source, DashAudioPlayer.currentSource)) { \n\
196 if (DashAudioPlayer.playing) { \n\
197 DashAudioPlayer.pause(); \n\
199 DashAudioPlayer.play(); \n\
202 var playlist = (cardData["quickPreviewData"] && cardData["quickPreviewData"]["playlist"]) || null; \n\
203 DashAudioPlayer.playSource(source, playlist); \n\
206 onPressAndHold: { \n\
207 root.pressAndHold(); \n\
211// %1 is whether the loader should be asynchronous or not
212// %2 is the header height code
213var kOverlayLoaderCode = 'Loader { \n\
214 id: overlayLoader; \n\
215 readonly property real overlayHeight: %2 + units.gu(2); \n\
216 anchors.fill: artShapeHolder; \n\
217 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
218 asynchronous: %1; \n\
219 visible: showHeader && status == Loader.Ready; \n\
220 sourceComponent: UbuntuShapeOverlay { \n\
222 property real luminance: Style.luminance(overlayColor); \n\
223 aspect: UbuntuShape.Flat; \n\
224 radius: "medium"; \n\
225 overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
226 overlayRect: Qt.rect(0, 1 - overlayLoader.overlayHeight / height, 1, 1); \n\
230// multiple row version of HeaderRowCode
231function kHeaderRowCodeGenerator() {
232 var kHeaderRowCodeTemplate = 'Row { \n\
234 objectName: "outerRow"; \n\
235 property real margins: units.gu(1); \n\
236 spacing: margins; \n\
239 anchors.right: parent.right; \n\
240 anchors.margins: margins; \n\
241 anchors.rightMargin: 0; \n\
246 var args = Array.prototype.slice.call(arguments);
247 var isCardTool = args.shift();
248 var heightCode = isCardTool ? "" : "height: root.fixedHeaderHeight; \n";
249 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(heightCode).arg(args.join(',\n'));
253// multiple item version of kHeaderContainerCode
254function kHeaderContainerCodeGenerator() {
255 var headerContainerCodeTemplate = 'Item { \n\
256 id: headerTitleContainer; \n\
258 width: parent.width - x; \n\
259 implicitHeight: %2; \n\
264 var args = Array.prototype.slice.call(arguments);
265 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(',\n'));
269// %1 is used as anchors of mascotShapeLoader
270// %2 is whether the loader should be asynchronous or not
271var kMascotShapeLoaderCode = 'Loader { \n\
272 id: mascotShapeLoader; \n\
273 objectName: "mascotShapeLoader"; \n\
274 asynchronous: %2; \n\
275 active: mascotImage.status === Image.Ready; \n\
276 visible: showHeader && active && status == Loader.Ready; \n\
277 width: units.gu(6); \n\
278 height: units.gu(5.625); \n\
279 sourceComponent: UbuntuShape { image: mascotImage } \n\
283// %1 is used as anchors of mascotImage
284// %2 is used as visible of mascotImage
285// %3 is injected as code to mascotImage
286// %4 is used as fallback image
287var kMascotImageCode = 'CroppedImageMinimumSourceSize { \n\
289 objectName: "mascotImage"; \n\
291 source: cardData && cardData["mascot"] || %4; \n\
292 width: units.gu(6); \n\
293 height: units.gu(5.625); \n\
294 horizontalAlignment: Image.AlignHCenter; \n\
295 verticalAlignment: Image.AlignVCenter; \n\
300// %1 is used as anchors of titleLabel
301// %2 is used as color of titleLabel
302// %3 is used as extra condition for visible of titleLabel
303// %4 is used as title width
304// %5 is used as horizontal alignment
305var kTitleLabelCode = 'Label { \n\
307 objectName: "titleLabel"; \n\
309 elide: Text.ElideRight; \n\
310 fontSize: "small"; \n\
311 wrapMode: Text.Wrap; \n\
312 maximumLineCount: 2; \n\
313 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
315 visible: showHeader %3; \n\
317 text: root.title; \n\
318 font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
319 horizontalAlignment: %5; \n\
322// %1 is used as extra anchors of emblemIcon
323// %2 is used as color of emblemIcon
324// FIXME The width code is a
325// Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
326var kEmblemIconCode = 'Icon { \n\
328 objectName: "emblemIcon"; \n\
330 bottom: titleLabel.baseline; \n\
331 right: parent.right; \n\
334 source: cardData && cardData["emblem"] || ""; \n\
336 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
337 width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
340// %1 is used as anchors of touchdown effect
341var kTouchdownCode = 'UbuntuShape { \n\
343 objectName: "touchdown"; \n\
345 visible: root.pressed; \n\
346 radius: "medium"; \n\
347 borderSource: "radius_pressed.sci" \n\
350// %1 is used as anchors of subtitleLabel
351// %2 is used as color of subtitleLabel
352var kSubtitleLabelCode = 'Label { \n\
353 id: subtitleLabel; \n\
354 objectName: "subtitleLabel"; \n\
356 anchors.topMargin: units.dp(2); \n\
357 elide: Text.ElideRight; \n\
358 maximumLineCount: 1; \n\
359 fontSize: "x-small"; \n\
360 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
362 visible: titleLabel.visible && titleLabel.text; \n\
363 text: cardData && cardData["subtitle"] || ""; \n\
364 font.weight: Font.Light; \n\
367// %1 is used as anchors of attributesRow
368// %2 is used as color of attributesRow
369var kAttributesRowCode = 'CardAttributes { \n\
370 id: attributesRow; \n\
371 objectName: "attributesRow"; \n\
374 fontScale: root.fontScale; \n\
375 model: cardData && cardData["attributes"]; \n\
378// %1 is used as anchors of socialActionsRow
379// %2 is used as color of socialActionsRow
380var kSocialActionsRowCode = 'CardSocialActions { \n\
381 id: socialActionsRow; \n\
382 objectName: "socialActionsRow"; \n\
385 model: cardData && cardData["socialActions"]; \n\
386 onClicked: root.action(actionId); \n\
389// %1 is used as top anchor of summary
390// %2 is used as topMargin anchor of summary
391// %3 is used as color of summary
392var kSummaryLabelCode = 'Label { \n\
394 objectName: "summaryLabel"; \n\
397 left: parent.left; \n\
398 right: parent.right; \n\
399 margins: units.gu(1); \n\
402 wrapMode: Text.Wrap; \n\
403 maximumLineCount: 5; \n\
404 elide: Text.ElideRight; \n\
405 text: cardData && cardData["summary"] || ""; \n\
406 height: text ? implicitHeight : 0; \n\
407 fontSize: "small"; \n\
411// %1 is used as bottom anchor of audio progress bar
412// %2 is used as left anchor of audio progress bar
413// %3 is used as text color
414var kAudioProgressBarCode = 'CardAudioProgress { \n\
415 id: audioProgressBar; \n\
416 duration: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["duration"]) || 0; \n\
417 source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
421 right: parent.right; \n\
422 margins: units.gu(1); \n\
427function sanitizeColor(colorString) {
428 if (colorString !== undefined) {
429 if (colorString.match(/^[#a-z0-9]*$/i) === null) {
430 // This is not the perfect regexp for color
431 // but what we're trying to do here is just protect
432 // against injection so it's ok
439function cardString(template, components, isCardTool, artShapeStyle) {
442 var templateInteractive = (template == null ? true : (template["non-interactive"] !== undefined ? !template["non-interactive"] : true)) ? "true" : "false";
444 code = 'AbstractButton { \n\
446 property var cardData; \n\
447 property string backgroundShapeStyle: "inset"; \n\
448 property real fontScale: 1.0; \n\
449 property var scopeStyle: null; \n\
450 readonly property string title: cardData && cardData["title"] || ""; \n\
451 property bool showHeader: true; \n\
452 implicitWidth: childrenRect.width; \n\
454 \n'.arg(templateInteractive);
457 code += "property int fixedHeaderHeight: -1; \n\
458 property size fixedArtShapeSize: Qt.size(-1, -1); \n";
461 var hasArt = components["art"] && components["art"]["field"] || false;
462 var hasSummary = components["summary"] || false;
463 var isConciergeMode = components["art"] && components["art"]["conciergeMode"] || false;
464 var artAndSummary = hasArt && hasSummary && !isConciergeMode;
465 var isHorizontal = template["card-layout"] === "horizontal";
466 var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
467 (hasSummary && (template["card-background"] || components["background"]));
468 var hasTitle = components["title"] || false;
469 var hasMascot = components["mascot"] || false;
470 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
471 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
472 var hasSubtitle = hasTitle && components["subtitle"] || false;
473 var hasHeaderRow = hasMascot && hasTitle;
474 var hasAttributes = hasTitle && components["attributes"] && components["attributes"]["field"] || false;
475 var hasSocialActions = hasTitle && components["social-actions"] || false;
476 var isAudio = template["quick-preview-type"] === "audio";
477 var asynchronous = isCardTool ? "false" : "true";
479 code += 'signal action(var actionId);\n';
481 // For now we only support audio cards with [optional] art, title, subtitle
482 // in horizontal mode
483 // Anything else makes it behave not like an audio card
484 if (hasSummary) isAudio = false;
485 if (!isHorizontal) isAudio = false;
486 if (hasMascot) isAudio = false;
487 if (hasEmblem) isAudio = false;
488 if (headerAsOverlay) isAudio = false;
489 if (hasAttributes) isAudio = false;
493 var templateCardBackground;
494 if (template && typeof template["card-background"] === "string") {
495 templateCardBackground = 'decodeURI("' + encodeURI(template["card-background"]) + '")';
497 templateCardBackground = '""';
500 var backgroundElements0;
501 var backgroundElements1;
502 if (template && typeof template["card-background"] === "object" && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) {
503 var element0 = sanitizeColor(template["card-background"]["elements"][0]);
504 var element1 = sanitizeColor(template["card-background"]["elements"][1]);
505 if (element0 !== undefined) {
506 backgroundElements0 = '"%1"'.arg(element0);
508 if (element1 !== undefined) {
509 backgroundElements1 = '"%1"'.arg(element1);
512 code += kBackgroundLoaderCode.arg(backgroundElements0).arg(backgroundElements1).arg(asynchronous).arg(templateCardBackground);
516 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
520 var artShapeAspect = components["art"] && components["art"]["aspect-ratio"] || 1;
521 if (isNaN(artShapeAspect)) {
525 artShapeAspect = "(root.fixedArtShapeSize.width / root.fixedArtShapeSize.height)";
528 var widthCode, heightCode;
531 artAnchors = 'left: parent.left';
532 if (hasMascot || hasTitle) {
533 widthCode = 'height * ' + artShapeAspect;
534 heightCode = 'headerHeight + 2 * units.gu(1)';
536 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
537 // So we define a "random" height of the image height + 2 gu for the margins
538 widthCode = 'height * ' + artShapeAspect
539 heightCode = 'units.gu(7.625)';
542 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
543 widthCode = 'root.width'
544 heightCode = 'width / ' + artShapeAspect;
547 var fallback = !isCardTool && components["art"] && components["art"]["fallback"] || "";
548 fallback = encodeURI(fallback);
549 var fallbackStatusCode = "";
550 var fallbackURICode = '""';
551 if (fallback !== "") {
552 // fallbackStatusCode has %8 in it because we want to substitute it for fallbackURICode
553 // which in kArtShapeHolderCode is %8
554 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %8;';
555 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
557 var artShapeHolderShapeCode;
558 if (!isConciergeMode) {
559 if (artShapeStyle === "icon") {
560 artShapeHolderShapeCode = kArtProportionalShapeCode;
562 var artShapeHolderShapeAspect;
563 switch (artShapeStyle) {
564 case "inset": artShapeHolderShapeAspect = "UbuntuShape.Inset"; break;
565 case "shadow": artShapeHolderShapeAspect = "UbuntuShape.DropShadow"; break;
567 case "flat": artShapeHolderShapeAspect = "UbuntuShape.Flat"; break;
569 artShapeHolderShapeCode = kArtUbuntuShapeCode.arg(artShapeHolderShapeAspect);
572 artShapeHolderShapeCode = "";
574 var artShapeHolderCode = isCardTool ? kArtShapeHolderCodeCardToolCard : kArtShapeHolderCode;
575 code += artShapeHolderCode.arg(artAnchors)
578 .arg(isConciergeMode ? "true" : "false")
580 .arg(artShapeHolderShapeCode)
581 .arg(fallbackStatusCode)
582 .arg(fallbackURICode);
584 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
587 if (headerAsOverlay) {
588 var headerHeightCode = isCardTool ? "headerHeight" : "root.fixedHeaderHeight";
589 code += kOverlayLoaderCode.arg(asynchronous).arg(headerHeightCode);
592 var headerVerticalAnchors;
593 if (headerAsOverlay) {
594 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
595 bottomMargin: units.gu(1);\n';
599 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
600 topMargin: units.gu(1);\n';
602 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
603 topMargin: units.gu(1);\n';
606 headerVerticalAnchors = 'top: parent.top; \n\
607 topMargin: units.gu(1);\n';
611 var headerLeftAnchor;
612 var headerLeftAnchorHasMargin = false;
613 if (isHorizontal && hasArt) {
614 headerLeftAnchor = 'left: artShapeHolder.right; \n\
615 leftMargin: units.gu(1);\n';
616 headerLeftAnchorHasMargin = true;
617 } else if (isHorizontal && isAudio) {
618 headerLeftAnchor = 'left: audioButton.right; \n\
619 leftMargin: units.gu(1);\n';
620 headerLeftAnchorHasMargin = true;
622 headerLeftAnchor = 'left: parent.left;\n';
625 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary && !isAudio;
628 code += 'readonly property int headerHeight: row.height;\n'
629 } else if (hasMascot) {
630 code += 'readonly property int headerHeight: mascotImage.height;\n'
631 } else if (hasAttributes) {
632 if (hasTitle && hasSubtitle) {
633 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
634 } else if (hasTitle) {
635 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
637 code += 'readonly property int headerHeight: attributesRow.height;\n'
639 } else if (isAudio) {
641 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
642 } else if (hasTitle) {
643 code += 'readonly property int headerHeight: titleLabel.height + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
645 code += 'readonly property int headerHeight: audioProgressBar.height;\n'
647 } else if (hasSubtitle) {
648 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
649 } else if (hasTitle) {
650 code += 'readonly property int headerHeight: titleLabel.height;\n'
652 code += 'readonly property int headerHeight: 0;\n'
655 var mascotShapeCode = '';
658 var useMascotShape = !hasBackground && !headerAsOverlay;
659 var mascotAnchors = '';
661 mascotAnchors += headerLeftAnchor;
662 mascotAnchors += headerVerticalAnchors;
663 if (!headerLeftAnchorHasMargin) {
664 mascotAnchors += 'leftMargin: units.gu(1);\n'
667 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
670 if (useMascotShape) {
671 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors).arg(asynchronous);
674 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
675 var fallback = !isCardTool && components["mascot"] && components["mascot"]["fallback"] || "";
676 fallback = encodeURI(fallback);
677 var fallbackStatusCode = "";
678 var fallbackURICode = '""';
679 if (fallback !== "") {
680 // fallbackStatusCode has %4 in it because we want to substitute it for fallbackURICode
681 // which in kMascotImageCode is %4
682 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %4;';
683 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
685 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible).arg(fallbackStatusCode).arg(fallbackURICode);
688 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")';
690 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
691 var titleSubtitleCode = '';
694 if (headerAsOverlay) {
695 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
696 } else if (hasSummary) {
697 titleColor = 'summary.color';
698 } else if (hasBackground) {
699 titleColor = summaryColorWithBackground;
701 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
706 var attributesAnchors;
707 var titleContainerAnchors;
708 var titleRightAnchor;
709 var titleWidth = "undefined";
711 var extraRightAnchor = '';
712 var extraLeftAnchor = '';
713 if (!touchdownOnArtShape) {
714 extraRightAnchor = 'rightMargin: units.gu(1); \n';
715 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
716 } else if (headerAsOverlay && !hasEmblem) {
717 extraRightAnchor = 'rightMargin: units.gu(1); \n';
721 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
723 titleContainerAnchors = 'right: parent.right; ';
724 titleContainerAnchors += headerLeftAnchor;
725 titleContainerAnchors += headerVerticalAnchors;
726 if (!headerLeftAnchorHasMargin) {
727 titleContainerAnchors += extraLeftAnchor;
731 titleRightAnchor = 'right: emblemIcon.left; \n\
732 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
734 titleRightAnchor = 'right: parent.right; \n'
735 titleRightAnchor += extraRightAnchor;
738 if (hasTitleContainer) {
739 // Using headerTitleContainer
740 titleAnchors = titleRightAnchor;
741 titleAnchors += 'left: parent.left; \n\
743 subtitleAnchors = 'right: parent.right; \n\
744 left: parent.left; \n';
745 subtitleAnchors += extraRightAnchor;
747 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
748 subtitleAnchors += 'top: titleLabel.bottom;\n';
750 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
752 } else if (hasMascot) {
753 // Using row without titleContainer
754 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
755 titleWidth = "parent.width - x";
757 if (headerAsOverlay) {
758 // Using anchors to the overlay
759 titleAnchors = titleRightAnchor;
760 titleAnchors += 'left: parent.left; \n\
761 leftMargin: units.gu(1); \n\
762 top: overlayLoader.top; \n\
763 topMargin: units.gu(1) + overlayLoader.height - overlayLoader.overlayHeight; \n';
765 // Using anchors to the mascot/parent
766 titleAnchors = titleRightAnchor;
767 titleAnchors += headerLeftAnchor;
768 titleAnchors += headerVerticalAnchors;
769 if (!headerLeftAnchorHasMargin) {
770 titleAnchors += extraLeftAnchor;
773 subtitleAnchors = 'left: titleLabel.left; \n\
774 leftMargin: titleLabel.leftMargin; \n';
775 subtitleAnchors += extraRightAnchor;
778 subtitleAnchors += 'right: parent.right; \n';
780 subtitleAnchors += 'right: titleLabel.right; \n';
784 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
785 subtitleAnchors += 'top: titleLabel.bottom;\n';
787 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
791 var titleAlignment = "Text.AlignHCenter";
792 if (template["card-layout"] === "horizontal"
793 || typeof components["title"] !== "object"
794 || components["title"]["align"] === "left") titleAlignment = "Text.AlignLeft";
795 var keys = ["mascot", "emblem", "subtitle", "attributes", "summary"];
796 for (var key in keys) {
799 if (typeof components[key] === "string"
800 || typeof components[key]["field"] === "string") titleAlignment = "Text.AlignLeft";
806 // code for different elements
807 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
808 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra).arg(titleWidth).arg(titleAlignment);
812 // code for the title container
813 var containerCode = [];
814 var containerHeight = 'titleLabel.height';
815 containerCode.push(titleCode);
817 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
818 containerCode.push(subtitleCode);
819 containerHeight += ' + subtitleLabel.height';
822 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
825 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
826 containerCode.push(attributesCode);
827 containerHeight += ' + attributesRow.height';
830 if (hasTitleContainer) {
832 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
835 titleSubtitleCode = titleCode;
837 titleSubtitleCode += subtitleCode;
840 titleSubtitleCode += attributesCode;
846 var rowCode = [mascotCode, titleSubtitleCode];
847 if (mascotShapeCode != '') {
848 rowCode.unshift(mascotShapeCode);
850 code += kHeaderRowCodeGenerator(isCardTool, headerVerticalAnchors + headerLeftAnchor, rowCode)
852 code += mascotShapeCode + mascotCode + titleSubtitleCode;
856 var audioProgressBarLeftAnchor = 'audioButton.right';
857 var audioProgressBarBottomAnchor = 'audioButton.bottom';
858 var audioProgressBarTextColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
860 code += kAudioProgressBarCode.arg(audioProgressBarBottomAnchor)
861 .arg(audioProgressBarLeftAnchor)
862 .arg(audioProgressBarTextColor);
864 var audioButtonAnchorsFill;
865 var audioButtonWidth;
866 var audioButtonHeight;
868 audioButtonAnchorsFill = 'artShapeHolder';
869 audioButtonWidth = 'undefined';
870 audioButtonHeight = 'undefined';
872 audioButtonAnchorsFill = 'undefined';
873 audioButtonWidth = 'height';
874 audioButtonHeight = isCardTool ? 'headerHeight + 2 * units.gu(1)'
875 : 'root.fixedHeaderHeight + 2 * units.gu(1)';
877 code += kAudioButtonCode.arg(audioButtonAnchorsFill).arg(audioButtonWidth).arg(audioButtonHeight).arg(asynchronous);
881 var summaryTopAnchor;
882 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
883 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
884 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
885 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
886 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
887 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
888 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
889 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
890 else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
891 else summaryTopAnchor = 'parent.top';
895 summaryColor = summaryColorWithBackground;
897 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
900 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
902 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
905 if (hasSocialActions) {
909 if (hasSummary) socialTopAnchor = 'summary.bottom;';
910 else if (isHorizontal && hasArt) socialTopAnchor = 'artShapeHolder.bottom;';
911 else if (headerAsOverlay && hasArt) socialTopAnchor = 'artShapeHolder.bottom;';
912 else if (hasHeaderRow) socialTopAnchor = 'row.bottom;';
913 else if (hasTitleContainer) socialTopAnchor = 'headerTitleContainer.bottom;';
914 else if (hasMascot) socialTopAnchor = 'mascotImage.bottom;';
915 else if (hasAttributes) socialTopAnchor = 'attributesRow.bottom;';
916 else if (hasSubtitle) socialTopAnchor = 'subtitleLabel.bottom;';
917 else if (hasTitle) socialTopAnchor = 'titleLabel.bottom;';
918 else if (hasArt) socialTopAnchor = 'artShapeHolder.bottom;';
919 else socialTopAnchor = 'parent.top';
921 socialAnchors = 'top: ' + socialTopAnchor + ' left: parent.left; right: parent.right; topMargin: units.gu(1);'
925 socialColor = summaryColorWithBackground;
927 socialColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
930 code += kSocialActionsRowCode.arg(socialAnchors).arg(socialColor);
933 if (artShapeStyle != "shadow" && artShapeStyle != "icon") {
934 var touchdownAnchors;
936 touchdownAnchors = 'fill: backgroundLoader';
937 } else if (touchdownOnArtShape) {
938 touchdownAnchors = 'fill: artShapeHolder';
940 touchdownAnchors = 'fill: root'
942 code += kTouchdownCode.arg(touchdownAnchors);
945 var implicitHeight = 'implicitHeight: ';
946 if (hasSocialActions) {
947 implicitHeight += 'socialActionsRow.y + socialActionsRow.height + units.gu(1);\n';
948 } else if (hasSummary) {
949 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
950 } else if (isAudio) {
951 implicitHeight += 'audioButton.height;\n';
952 } else if (headerAsOverlay) {
953 implicitHeight += 'artShapeHolder.height;\n';
954 } else if (hasHeaderRow) {
955 implicitHeight += 'row.y + row.height + units.gu(1);\n';
956 } else if (hasMascot) {
957 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
958 } else if (hasTitleContainer) {
959 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
960 } else if (hasAttributes) {
961 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
962 } else if (hasSubtitle) {
963 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
964 } else if (hasTitle) {
965 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
967 implicitHeight += 'artShapeHolder.height;\n';
972 // Close the AbstractButton
973 code += implicitHeight + '}\n';
978function createCardComponent(parent, template, components, isCardTool, artShapeStyle, 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);
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:");
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list