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