on 6a6e6a5 help sign in
unity8/plugins/Dash/CardCreator.js
873 lines 44 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 */
17.pragma library
19// %1 is the template["card-background"] string
20// %2 is the template["card-background"]["elements"][0]
21// %3 is the template["card-background"]["elements"][1]
22var kBackgroundLoaderCode = 'Loader {\n\
23 id: backgroundLoader; \n\
24 objectName: "backgroundLoader"; \n\
25 anchors.fill: parent; \n\
26 asynchronous: root.asynchronous; \n\
27 visible: status == Loader.Ready; \n\
28 sourceComponent: UbuntuShape { \n\
29 objectName: "background"; \n\
30 radius: "medium"; \n\
31 aspect: { \n\
32 switch (root.backgroundShapeStyle) { \n\
33 case "inset": return UbuntuShape.Inset; \n\
34 case "shadow": return UbuntuShape.DropShadow; \n\
35 default: \n\
36 case "flat": return UbuntuShape.Flat; \n\
37 } \n\
38 } \n\
39 backgroundColor: getColor(0) || "white"; \n\
40 secondaryBackgroundColor: getColor(1) || backgroundColor; \n\
41 backgroundMode: UbuntuShape.VerticalGradient; \n\
42 anchors.fill: parent; \n\
43 source: backgroundImage.source ? backgroundImage : null; \n\
44 property real luminance: Style.luminance(backgroundColor); \n\
45 property Image backgroundImage: Image { \n\
46 objectName: "backgroundImage"; \n\
47 source: { \n\
48 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
49 else return "%1"; \n\
50 } \n\
51 } \n\
52 function getColor(index) { \n\
53 if (cardData && typeof cardData["background"] === "object" \n\
54 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
55 return cardData["background"]["elements"][index]; \n\
56 } else return index === 0 ? %2 : %3; \n\
57 } \n\
58 } \n\
59 }\n';
61// %1 is used as anchors of artShapeHolder
62// %2 is used as image width
63// %3 is used as image height
64var kArtShapeHolderCode = 'Item { \n\
65 id: artShapeHolder; \n\
66 height: root.fixedArtShapeSize.height > 0 ? root.fixedArtShapeSize.height : artShapeLoader.height; \n\
67 width: root.fixedArtShapeSize.width > 0 ? root.fixedArtShapeSize.width : artShapeLoader.width; \n\
68 anchors { %1 } \n\
69 Loader { \n\
70 id: artShapeLoader; \n\
71 objectName: "artShapeLoader"; \n\
72 active: cardData && cardData["art"] || false; \n\
73 asynchronous: root.asynchronous; \n\
74 visible: status == Loader.Ready; \n\
75 sourceComponent: Item { \n\
76 id: artShape; \n\
77 objectName: "artShape"; \n\
78 readonly property bool doShapeItem: components["art"]["conciergeMode"] !== true; \n\
79 visible: image.status == Image.Ready; \n\
80 readonly property alias image: artImage; \n\
81 ShaderEffectSource { \n\
82 id: artShapeSource; \n\
83 sourceItem: artImage; \n\
84 anchors.centerIn: parent; \n\
85 width: 1; \n\
86 height: 1; \n\
87 hideSource: doShapeItem; \n\
88 } \n\
89 Loader { \n\
90 anchors.fill: parent; \n\
91 visible: artShape.doShapeItem; \n\
92 sourceComponent: root.artShapeStyle === "icon" ? artShapeIconComponent : artShapeShapeComponent; \n\
93 Component { \n\
94 id: artShapeShapeComponent; \n\
95 UbuntuShape { \n\
96 source: artShapeSource; \n\
97 sourceFillMode: UbuntuShape.PreserveAspectCrop; \n\
98 radius: "medium"; \n\
99 aspect: { \n\
100 switch (root.artShapeStyle) { \n\
101 case "inset": return UbuntuShape.Inset; \n\
102 case "shadow": return UbuntuShape.DropShadow; \n\
103 default: \n\
104 case "flat": return UbuntuShape.Flat; \n\
105 } \n\
106 } \n\
107 } \n\
108 } \n\
109 Component { \n\
110 id: artShapeIconComponent; \n\
111 ProportionalShape { source: artShapeSource; aspect: UbuntuShape.DropShadow; } \n\
112 } \n\
113 } \n\
114 readonly property real fixedArtShapeSizeAspect: (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) ? root.fixedArtShapeSize.width / root.fixedArtShapeSize.height : -1; \n\
115 readonly property real aspect: fixedArtShapeSizeAspect > 0 ? fixedArtShapeSizeAspect : components !== undefined ? components["art"]["aspect-ratio"] : 1; \n\
116 Component.onCompleted: { updateWidthHeightBindings(); } \n\
117 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
118 function updateWidthHeightBindings() { \n\
119 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
120 width = root.fixedArtShapeSize.width; \n\
121 height = root.fixedArtShapeSize.height; \n\
122 } else { \n\
123 width = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.width }); \n\
124 height = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.height }); \n\
125 } \n\
126 } \n\
127 CroppedImageMinimumSourceSize { \n\
128 id: artImage; \n\
129 objectName: "artImage"; \n\
130 source: cardData && cardData["art"] || ""; \n\
131 asynchronous: root.asynchronous; \n\
132 width: %2; \n\
133 height: %3; \n\
134 } \n\
135 } \n\
136 } \n\
137 }\n';
139// %1 is anchors.fill
140// %2 is width
141// %3 is height
142var kAudioButtonCode = 'AbstractButton { \n\
143 id: audioButton; \n\
144 anchors.fill: %1; \n\
145 width: %2; \n\
146 height: %3; \n\
147 readonly property url source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
148 UbuntuShape { \n\
149 anchors.fill: parent; \n\
150 visible: parent.pressed; \n\
151 radius: "medium"; \n\
152 } \n\
153 Icon { \n\
154 anchors.fill: parent; \n\
155 anchors.margins: parent.height > units.gu(5) ? units.gu(2) : 0; \n\
156 opacity: 0.9; \n\
157 name: DashAudioPlayer.playing && AudioUrlComparer.compare(parent.source, DashAudioPlayer.currentSource) ? "media-playback-pause" : "media-playback-start"; \n\
158 } \n\
159 onClicked: { \n\
160 if (AudioUrlComparer.compare(source, DashAudioPlayer.currentSource)) { \n\
161 if (DashAudioPlayer.playing) { \n\
162 DashAudioPlayer.pause(); \n\
163 } else { \n\
164 DashAudioPlayer.play(); \n\
165 } \n\
166 } else { \n\
167 var playlist = (cardData["quickPreviewData"] && cardData["quickPreviewData"]["playlist"]) || null; \n\
168 DashAudioPlayer.playSource(source, playlist); \n\
169 } \n\
170 } \n\
171 onPressAndHold: { \n\
172 root.pressAndHold(); \n\
173 } \n\
174 }';
176var kOverlayLoaderCode = 'Loader { \n\
177 id: overlayLoader; \n\
178 readonly property real overlayHeight: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
179 anchors.fill: artShapeHolder; \n\
180 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
181 asynchronous: root.asynchronous; \n\
182 visible: showHeader && status == Loader.Ready; \n\
183 sourceComponent: UbuntuShapeOverlay { \n\
184 id: overlay; \n\
185 property real luminance: Style.luminance(overlayColor); \n\
186 aspect: UbuntuShape.Flat; \n\
187 radius: "medium"; \n\
188 overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
189 overlayRect: Qt.rect(0, 1 - overlayLoader.overlayHeight / height, 1, 1); \n\
190 } \n\
191 }\n';
193// multiple row version of HeaderRowCode
194function kHeaderRowCodeGenerator() {
195 var kHeaderRowCodeTemplate = 'Row { \n\
196 id: row; \n\
197 objectName: "outerRow"; \n\
198 property real margins: units.gu(1); \n\
199 spacing: margins; \n\
200 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
201 anchors { %1 } \n\
202 anchors.right: parent.right; \n\
203 anchors.margins: margins; \n\
204 anchors.rightMargin: 0; \n\
205 data: [ \n\
206 %2 \n\
207 ] \n\
208 }\n';
209 var args = Array.prototype.slice.call(arguments);
210 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(args.join(',\n'));
211 return code;
214// multiple item version of kHeaderContainerCode
215function kHeaderContainerCodeGenerator() {
216 var headerContainerCodeTemplate = 'Item { \n\
217 id: headerTitleContainer; \n\
218 anchors { %1 } \n\
219 width: parent.width - x; \n\
220 implicitHeight: %2; \n\
221 data: [ \n\
222 %3 \n\
223 ]\n\
224 }\n';
225 var args = Array.prototype.slice.call(arguments);
226 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(',\n'));
227 return code;
230// %1 is used as anchors of mascotShapeLoader
231var kMascotShapeLoaderCode = 'Loader { \n\
232 id: mascotShapeLoader; \n\
233 objectName: "mascotShapeLoader"; \n\
234 asynchronous: root.asynchronous; \n\
235 active: mascotImage.status === Image.Ready; \n\
236 visible: showHeader && active && status == Loader.Ready; \n\
237 width: units.gu(6); \n\
238 height: units.gu(5.625); \n\
239 sourceComponent: UbuntuShape { image: mascotImage } \n\
240 anchors { %1 } \n\
241 }\n';
243// %1 is used as anchors of mascotImage
244// %2 is used as visible of mascotImage
245var kMascotImageCode = 'CroppedImageMinimumSourceSize { \n\
246 id: mascotImage; \n\
247 objectName: "mascotImage"; \n\
248 anchors { %1 } \n\
249 source: cardData && cardData["mascot"] || ""; \n\
250 width: units.gu(6); \n\
251 height: units.gu(5.625); \n\
252 horizontalAlignment: Image.AlignHCenter; \n\
253 verticalAlignment: Image.AlignVCenter; \n\
254 visible: %2; \n\
255 }\n';
257// %1 is used as anchors of titleLabel
258// %2 is used as color of titleLabel
259// %3 is used as extra condition for visible of titleLabel
260// %4 is used as title width
261var kTitleLabelCode = 'Label { \n\
262 id: titleLabel; \n\
263 objectName: "titleLabel"; \n\
264 anchors { %1 } \n\
265 elide: Text.ElideRight; \n\
266 fontSize: "small"; \n\
267 wrapMode: Text.Wrap; \n\
268 maximumLineCount: 2; \n\
269 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
270 color: %2; \n\
271 visible: showHeader %3; \n\
272 width: %4; \n\
273 text: root.title; \n\
274 font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
275 horizontalAlignment: root.titleAlignment; \n\
276 }\n';
278// %1 is used as extra anchors of emblemIcon
279// %2 is used as color of emblemIcon
280// FIXME The width code is a
281// Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
282var kEmblemIconCode = 'Icon { \n\
283 id: emblemIcon; \n\
284 objectName: "emblemIcon"; \n\
285 anchors { \n\
286 bottom: titleLabel.baseline; \n\
287 right: parent.right; \n\
288 %1 \n\
289 } \n\
290 source: cardData && cardData["emblem"] || ""; \n\
291 color: %2; \n\
292 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
293 width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
294 }\n';
296// %1 is used as anchors of touchdown effect
297var kTouchdownCode = 'UbuntuShape { \n\
298 id: touchdown; \n\
299 objectName: "touchdown"; \n\
300 anchors { %1 } \n\
301 visible: root.artShapeStyle != "shadow" && root.artShapeStyle != "icon" && root.pressed; \n\
302 radius: "medium"; \n\
303 borderSource: "radius_pressed.sci" \n\
304 }\n';
306// %1 is used as anchors of subtitleLabel
307// %2 is used as color of subtitleLabel
308var kSubtitleLabelCode = 'Label { \n\
309 id: subtitleLabel; \n\
310 objectName: "subtitleLabel"; \n\
311 anchors { %1 } \n\
312 anchors.topMargin: units.dp(2); \n\
313 elide: Text.ElideRight; \n\
314 maximumLineCount: 1; \n\
315 fontSize: "x-small"; \n\
316 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
317 color: %2; \n\
318 visible: titleLabel.visible && titleLabel.text; \n\
319 text: cardData && cardData["subtitle"] || ""; \n\
320 font.weight: Font.Light; \n\
321 }\n';
323// %1 is used as anchors of attributesRow
324// %2 is used as color of attributesRow
325var kAttributesRowCode = 'CardAttributes { \n\
326 id: attributesRow; \n\
327 objectName: "attributesRow"; \n\
328 anchors { %1 } \n\
329 color: %2; \n\
330 fontScale: root.fontScale; \n\
331 model: cardData && cardData["attributes"]; \n\
332 }\n';
334// %1 is used as anchors of socialAttributesRow
335// %2 is used as color of socialAttributesRow
336var kSocialAttributesRowCode = 'CardSocialAttributes { \n\
337 id: socialAttributesRow; \n\
338 objectName: "socialAttributesRow"; \n\
339 anchors { %1 } \n\
340 color: %2; \n\
341 fontScale: root.fontScale; \n\
342 model: cardData && cardData["socialAttributes"]; \n\
343 onClicked: root.clicked(result); \n\
344 }\n';
346// %1 is used as top anchor of summary
347// %2 is used as topMargin anchor of summary
348// %3 is used as color of summary
349var kSummaryLabelCode = 'Label { \n\
350 id: summary; \n\
351 objectName: "summaryLabel"; \n\
352 anchors { \n\
353 top: %1; \n\
354 left: parent.left; \n\
355 right: parent.right; \n\
356 margins: units.gu(1); \n\
357 topMargin: %2; \n\
358 } \n\
359 wrapMode: Text.Wrap; \n\
360 maximumLineCount: 5; \n\
361 elide: Text.ElideRight; \n\
362 text: cardData && cardData["summary"] || ""; \n\
363 height: text ? implicitHeight : 0; \n\
364 fontSize: "small"; \n\
365 color: %3; \n\
366 }\n';
368// %1 is used as bottom anchor of audio progress bar
369// %2 is used as left anchor of audio progress bar
370// %3 is used as text color
371var kAudioProgressBarCode = 'CardAudioProgress { \n\
372 id: audioProgressBar; \n\
373 duration: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["duration"]) || 0; \n\
374 source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
375 anchors { \n\
376 bottom: %1; \n\
377 left: %2; \n\
378 right: parent.right; \n\
379 margins: units.gu(1); \n\
380 } \n\
381 color: %3; \n\
382 }';
384function cardString(template, components) {
385 var code;
387 var templateInteractive = (template == null ? true : (template["non-interactive"] !== undefined ? !template["non-interactive"] : true)) ? "true" : "false";
389 code = 'AbstractButton { \n\
390 id: root; \n\
391 property var components; \n\
392 property var cardData; \n\
393 property string artShapeStyle: "inset"; \n\
394 property string backgroundShapeStyle: "inset"; \n\
395 property real fontScale: 1.0; \n\
396 property var scopeStyle: null; \n\
397 property int titleAlignment: Text.AlignLeft; \n\
398 property int fixedHeaderHeight: -1; \n\
399 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
400 readonly property string title: cardData && cardData["title"] || ""; \n\
401 property bool asynchronous: true; \n\
402 property bool showHeader: true; \n\
403 implicitWidth: childrenRect.width; \n\
404 enabled: %1; \n\
405 \n'.arg(templateInteractive);
407 var hasArt = components["art"] && components["art"]["field"] || false;
408 var hasSummary = components["summary"] || false;
409 var artAndSummary = hasArt && hasSummary && components["art"]["conciergeMode"] !== true;
410 var isHorizontal = template["card-layout"] === "horizontal";
411 var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
412 (hasSummary && (template["card-background"] || components["background"]));
413 var hasTitle = components["title"] || false;
414 var hasMascot = components["mascot"] || false;
415 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
416 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
417 var hasSubtitle = hasTitle && components["subtitle"] || false;
418 var hasHeaderRow = hasMascot && hasTitle;
419 var hasAttributes = hasTitle && components["attributes"] && components["attributes"]["field"] || false;
420 var hasSocialAttributes = hasTitle && components["socialAttributes"] || false;
421 var isAudio = template["quick-preview-type"] === "audio";
423 if (hasSocialAttributes) {
424 code += 'signal clicked(var result);\n';
425 }
426 if (isAudio) {
427 // For now we only support audio cards with [optional] art, title, subtitle
428 // in horizontal mode
429 // Anything else makes it behave not like an audio card
430 if (hasSummary) isAudio = false;
431 if (!isHorizontal) isAudio = false;
432 if (hasMascot) isAudio = false;
433 if (hasEmblem) isAudio = false;
434 if (headerAsOverlay) isAudio = false;
435 if (hasAttributes) isAudio = false;
436 }
438 if (hasBackground) {
439 var templateCardBackground = (template && typeof template["card-background"] === "string") ? template["card-background"] : "";
440 var backgroundElements0;
441 var backgroundElements1;
442 if (template && typeof template["card-background"] === "object" && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) {
443 if (template["card-background"]["elements"][0] !== undefined) {
444 backgroundElements0 = '"%1"'.arg(template["card-background"]["elements"][0]);
445 }
446 if (template["card-background"]["elements"][1] !== undefined) {
447 backgroundElements1 = '"%1"'.arg(template["card-background"]["elements"][1]);
448 }
449 }
450 code += kBackgroundLoaderCode.arg(templateCardBackground).arg(backgroundElements0).arg(backgroundElements1);
451 }
453 if (hasArt) {
454 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
456 var widthCode, heightCode;
457 var artAnchors;
458 if (isHorizontal) {
459 artAnchors = 'left: parent.left';
460 if (hasMascot || hasTitle) {
461 widthCode = 'height * artShape.aspect'
462 heightCode = 'headerHeight + 2 * units.gu(1)';
463 } else {
464 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
465 // So we define a "random" height of the image height + 2 gu for the margins
466 widthCode = 'height * artShape.aspect'
467 heightCode = 'units.gu(7.625)';
468 }
469 } else {
470 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
471 widthCode = 'root.width'
472 heightCode = 'width / artShape.aspect';
473 }
475 code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
476 var fallback = components["art"] && components["art"]["fallback"] || "";
477 if (fallback !== "") {
478 code += 'Connections { target: artShapeLoader.item ? artShapeLoader.item.image : null; onStatusChanged: if (artShapeLoader.item.image.status === Image.Error) artShapeLoader.item.image.source = "%1"; } \n'.arg(fallback);
479 }
480 } else {
481 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
482 }
484 if (headerAsOverlay) {
485 code += kOverlayLoaderCode;
486 }
488 var headerVerticalAnchors;
489 if (headerAsOverlay) {
490 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
491 bottomMargin: units.gu(1);\n';
492 } else {
493 if (hasArt) {
494 if (isHorizontal) {
495 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
496 topMargin: units.gu(1);\n';
497 } else {
498 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
499 topMargin: units.gu(1);\n';
500 }
501 } else {
502 headerVerticalAnchors = 'top: parent.top; \n\
503 topMargin: units.gu(1);\n';
504 }
505 }
507 var headerLeftAnchor;
508 var headerLeftAnchorHasMargin = false;
509 if (isHorizontal && hasArt) {
510 headerLeftAnchor = 'left: artShapeHolder.right; \n\
511 leftMargin: units.gu(1);\n';
512 headerLeftAnchorHasMargin = true;
513 } else if (isHorizontal && isAudio) {
514 headerLeftAnchor = 'left: audioButton.right; \n\
515 leftMargin: units.gu(1);\n';
516 headerLeftAnchorHasMargin = true;
517 } else {
518 headerLeftAnchor = 'left: parent.left;\n';
519 }
521 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary && !isAudio;
523 if (hasHeaderRow) {
524 code += 'readonly property int headerHeight: row.height;\n'
525 } else if (hasMascot) {
526 code += 'readonly property int headerHeight: mascotImage.height;\n'
527 } else if (hasAttributes) {
528 if (hasTitle && hasSubtitle) {
529 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
530 } else if (hasTitle) {
531 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
532 } else {
533 code += 'readonly property int headerHeight: attributesRow.height;\n'
534 }
535 } else if (isAudio) {
536 if (hasSubtitle) {
537 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
538 } else if (hasTitle) {
539 code += 'readonly property int headerHeight: titleLabel.height + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
540 } else {
541 code += 'readonly property int headerHeight: audioProgressBar.height;\n'
542 }
543 } else if (hasSubtitle) {
544 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
545 } else if (hasTitle) {
546 code += 'readonly property int headerHeight: titleLabel.height;\n'
547 } else {
548 code += 'readonly property int headerHeight: 0;\n'
549 }
551 var mascotShapeCode = '';
552 var mascotCode = '';
553 if (hasMascot) {
554 var useMascotShape = !hasBackground && !headerAsOverlay;
555 var mascotAnchors = '';
556 if (!hasHeaderRow) {
557 mascotAnchors += headerLeftAnchor;
558 mascotAnchors += headerVerticalAnchors;
559 if (!headerLeftAnchorHasMargin) {
560 mascotAnchors += 'leftMargin: units.gu(1);\n'
561 }
562 } else {
563 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
564 }
566 if (useMascotShape) {
567 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
568 }
570 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
571 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
572 var fallback = components["mascot"] && components["mascot"]["fallback"] || "";
573 if (fallback !== "") {
574 code += 'Connections { target: mascotImage; onStatusChanged: if (mascotImage.status === Image.Error) mascotImage.source = "%1"; } \n'.arg(fallback);
575 }
576 }
578 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")';
580 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
581 var titleSubtitleCode = '';
582 if (hasTitle) {
583 var titleColor;
584 if (headerAsOverlay) {
585 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
586 } else if (hasSummary) {
587 titleColor = 'summary.color';
588 } else if (hasBackground) {
589 titleColor = summaryColorWithBackground;
590 } else {
591 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
592 }
594 var titleAnchors;
595 var subtitleAnchors;
596 var attributesAnchors;
597 var titleContainerAnchors;
598 var titleRightAnchor;
599 var titleWidth = "undefined";
601 var extraRightAnchor = '';
602 var extraLeftAnchor = '';
603 if (!touchdownOnArtShape) {
604 extraRightAnchor = 'rightMargin: units.gu(1); \n';
605 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
606 } else if (headerAsOverlay && !hasEmblem) {
607 extraRightAnchor = 'rightMargin: units.gu(1); \n';
608 }
610 if (hasMascot) {
611 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
612 } else {
613 titleContainerAnchors = 'right: parent.right; ';
614 titleContainerAnchors += headerLeftAnchor;
615 titleContainerAnchors += headerVerticalAnchors;
616 if (!headerLeftAnchorHasMargin) {
617 titleContainerAnchors += extraLeftAnchor;
618 }
619 }
620 if (hasEmblem) {
621 titleRightAnchor = 'right: emblemIcon.left; \n\
622 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
623 } else {
624 titleRightAnchor = 'right: parent.right; \n'
625 titleRightAnchor += extraRightAnchor;
626 }
628 if (hasTitleContainer) {
629 // Using headerTitleContainer
630 titleAnchors = titleRightAnchor;
631 titleAnchors += 'left: parent.left; \n\
632 top: parent.top;';
633 subtitleAnchors = 'right: parent.right; \n\
634 left: parent.left; \n';
635 subtitleAnchors += extraRightAnchor;
636 if (hasSubtitle) {
637 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
638 subtitleAnchors += 'top: titleLabel.bottom;\n';
639 } else {
640 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
641 }
642 } else if (hasMascot) {
643 // Using row without titleContainer
644 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
645 titleWidth = "parent.width - x";
646 } else {
647 if (headerAsOverlay) {
648 // Using anchors to the overlay
649 titleAnchors = titleRightAnchor;
650 titleAnchors += 'left: parent.left; \n\
651 leftMargin: units.gu(1); \n\
652 top: overlayLoader.top; \n\
653 topMargin: units.gu(1) + overlayLoader.height - overlayLoader.overlayHeight; \n';
654 } else {
655 // Using anchors to the mascot/parent
656 titleAnchors = titleRightAnchor;
657 titleAnchors += headerLeftAnchor;
658 titleAnchors += headerVerticalAnchors;
659 if (!headerLeftAnchorHasMargin) {
660 titleAnchors += extraLeftAnchor;
661 }
662 }
663 subtitleAnchors = 'left: titleLabel.left; \n\
664 leftMargin: titleLabel.leftMargin; \n';
665 subtitleAnchors += extraRightAnchor;
666 if (hasEmblem) {
667 // using container
668 subtitleAnchors += 'right: parent.right; \n';
669 } else {
670 subtitleAnchors += 'right: titleLabel.right; \n';
671 }
673 if (hasSubtitle) {
674 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
675 subtitleAnchors += 'top: titleLabel.bottom;\n';
676 } else {
677 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
678 }
679 }
681 // code for different elements
682 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
683 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra).arg(titleWidth);
684 var subtitleCode;
685 var attributesCode;
687 // code for the title container
688 var containerCode = [];
689 var containerHeight = 'titleLabel.height';
690 containerCode.push(titleCode);
691 if (hasSubtitle) {
692 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
693 containerCode.push(subtitleCode);
694 containerHeight += ' + subtitleLabel.height';
695 }
696 if (hasEmblem) {
697 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
698 }
699 if (hasAttributes) {
700 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
701 containerCode.push(attributesCode);
702 containerHeight += ' + attributesRow.height';
703 }
705 if (hasTitleContainer) {
706 // use container
707 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
708 } else {
709 // no container
710 titleSubtitleCode = titleCode;
711 if (hasSubtitle) {
712 titleSubtitleCode += subtitleCode;
713 }
714 if (hasAttributes) {
715 titleSubtitleCode += attributesCode;
716 }
717 }
718 }
720 if (hasHeaderRow) {
721 var rowCode = [mascotCode, titleSubtitleCode];
722 if (mascotShapeCode != '') {
723 rowCode.unshift(mascotShapeCode);
724 }
725 code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
726 } else {
727 code += mascotShapeCode + mascotCode + titleSubtitleCode;
728 }
730 if (isAudio) {
731 var audioProgressBarLeftAnchor = 'audioButton.right';
732 var audioProgressBarBottomAnchor = 'audioButton.bottom';
733 var audioProgressBarTextColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
735 code += kAudioProgressBarCode.arg(audioProgressBarBottomAnchor)
736 .arg(audioProgressBarLeftAnchor)
737 .arg(audioProgressBarTextColor);
739 var audioButtonAnchorsFill;
740 var audioButtonWidth;
741 var audioButtonHeight;
742 if (hasArt) {
743 audioButtonAnchorsFill = 'artShapeHolder';
744 audioButtonWidth = 'undefined';
745 audioButtonHeight = 'undefined';
746 } else {
747 audioButtonAnchorsFill = 'undefined';
748 audioButtonWidth = 'height';
749 audioButtonHeight = '(root.fixedHeaderHeight > 0 ? root.fixedHeaderHeight : headerHeight) + 2 * units.gu(1)';
750 }
751 code += kAudioButtonCode.arg(audioButtonAnchorsFill).arg(audioButtonWidth).arg(audioButtonHeight);
752 }
754 if (hasSummary) {
755 var summaryTopAnchor;
756 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
757 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
758 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
759 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
760 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
761 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
762 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
763 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
764 else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
765 else summaryTopAnchor = 'parent.top';
767 var summaryColor;
768 if (hasBackground) {
769 summaryColor = summaryColorWithBackground;
770 } else {
771 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
772 }
774 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
776 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
777 }
779 if (hasSocialAttributes) {
780 var socialAnchors;
781 var socialTopAnchor;
783 if (hasSummary) {
784 socialTopAnchor = 'summary.bottom;';
785 } else {
786 if (isHorizontal && hasArt) socialTopAnchor = 'artShapeHolder.bottom;';
787 else if (headerAsOverlay && hasArt) socialTopAnchor = 'artShapeHolder.bottom;';
788 else if (hasHeaderRow) socialTopAnchor = 'row.bottom;';
789 else if (hasTitleContainer) socialTopAnchor = 'headerTitleContainer.bottom;';
790 else if (hasMascot) socialTopAnchor = 'mascotImage.bottom;';
791 else if (hasAttributes) socialTopAnchor = 'attributesRow.bottom;';
792 else if (hasSubtitle) socialTopAnchor = 'subtitleLabel.bottom;';
793 else if (hasTitle) socialTopAnchor = 'titleLabel.bottom;';
794 else if (hasArt) socialTopAnchor = 'artShapeHolder.bottom;';
795 else socialTopAnchor = 'parent.top';
796 }
797 socialAnchors = 'top: ' + socialTopAnchor + ' left: parent.left; right: parent.right; topMargin: units.gu(1);'
799 var socialColor;
800 if (hasBackground) {
801 socialColor = summaryColorWithBackground;
802 } else {
803 socialColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
804 }
806 code += kSocialAttributesRowCode.arg(socialAnchors).arg(socialColor);
807 }
809 var touchdownAnchors;
810 if (hasBackground) {
811 touchdownAnchors = 'fill: backgroundLoader';
812 } else if (touchdownOnArtShape) {
813 touchdownAnchors = 'fill: artShapeHolder';
814 } else {
815 touchdownAnchors = 'fill: root'
816 }
817 code += kTouchdownCode.arg(touchdownAnchors);
819 var implicitHeight = 'implicitHeight: ';
820 if (hasSocialAttributes) {
821 implicitHeight += 'socialAttributesRow.y + socialAttributesRow.height + units.gu(1);\n';
822 } else if (hasSummary) {
823 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
824 } else if (isAudio) {
825 implicitHeight += 'audioButton.height;\n';
826 } else if (headerAsOverlay) {
827 implicitHeight += 'artShapeHolder.height;\n';
828 } else if (hasHeaderRow) {
829 implicitHeight += 'row.y + row.height + units.gu(1);\n';
830 } else if (hasMascot) {
831 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
832 } else if (hasTitleContainer) {
833 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
834 } else if (hasAttributes) {
835 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
836 } else if (hasSubtitle) {
837 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
838 } else if (hasTitle) {
839 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
840 } else if (hasArt) {
841 implicitHeight += 'artShapeHolder.height;\n';
842 } else {
843 implicitHeight = '';
844 }
846 // Close the AbstractButton
847 code += implicitHeight + '}\n';
849 return code;
852function createCardComponent(parent, template, components) {
853 var imports = 'import QtQuick 2.4; \n\
854 import Ubuntu.Components 1.3; \n\
855 import Ubuntu.Settings.Components 0.1; \n\
856 import Dash 0.1;\n\
857 import Utils 0.1;\n';
858 var card = cardString(template, components);
859 var code = imports + 'Component {\n' + card + '}\n';
861 try {
862 return Qt.createQmlObject(code, parent, "createCardComponent");
863 } catch (e) {
864 console.error("ERROR: Invalid component created.");
865 console.error("Template:");
866 console.error(JSON.stringify(template));
867 console.error("Components:");
868 console.error(JSON.stringify(components));
869 console.error("Code:");
870 console.error(code);
871 throw e;
872 }