on 19f185e help sign in
unity8/plugins/Dash/CardCreator.js
674 lines 33 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
19var kBackgroundLoaderCode = 'Loader {\n\
20 id: backgroundLoader; \n\
21 objectName: "backgroundLoader"; \n\
22 anchors.fill: parent; \n\
23 asynchronous: root.asynchronous; \n\
24 visible: status == Loader.Ready; \n\
25 sourceComponent: UbuntuShape { \n\
26 objectName: "background"; \n\
27 radius: "medium"; \n\
28 color: getColor(0) || "white"; \n\
29 gradientColor: getColor(1) || color; \n\
30 anchors.fill: parent; \n\
31 image: backgroundImage.source ? backgroundImage : null; \n\
32 property real luminance: Style.luminance(color); \n\
33 property Image backgroundImage: Image { \n\
34 objectName: "backgroundImage"; \n\
35 source: { \n\
36 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
37 else if (template && typeof template["card-background"] === "string") return template["card-background"]; \n\
38 else return ""; \n\
39 } \n\
40 } \n\
41 function getColor(index) { \n\
42 if (cardData && typeof cardData["background"] === "object" \n\
43 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
44 return cardData["background"]["elements"][index]; \n\
45 } else if (template && typeof template["card-background"] === "object" \n\
46 && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) { \n\
47 return template["card-background"]["elements"][index]; \n\
48 } else return undefined; \n\
49 } \n\
50 } \n\
51 }\n';
53// %1 is used as anchors of artShapeHolder
54// %2 is used as image width
55// %3 is used as image height
56var kArtShapeHolderCode = 'Item { \n\
57 id: artShapeHolder; \n\
58 height: root.fixedArtShapeSize.height > 0 ? root.fixedArtShapeSize.height : artShapeLoader.height; \n\
59 width: root.fixedArtShapeSize.width > 0 ? root.fixedArtShapeSize.width : artShapeLoader.width; \n\
60 anchors { %1 } \n\
61 Loader { \n\
62 id: artShapeLoader; \n\
63 objectName: "artShapeLoader"; \n\
64 active: cardData && cardData["art"] || false; \n\
65 asynchronous: root.asynchronous; \n\
66 visible: status == Loader.Ready; \n\
67 sourceComponent: UbuntuShape { \n\
68 id: artShape; \n\
69 objectName: "artShape"; \n\
70 radius: "medium"; \n\
71 visible: image.status == Image.Ready; \n\
72 readonly property real fixedArtShapeSizeAspect: (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) ? root.fixedArtShapeSize.width / root.fixedArtShapeSize.height : -1; \n\
73 readonly property real aspect: fixedArtShapeSizeAspect > 0 ? fixedArtShapeSizeAspect : components !== undefined ? components["art"]["aspect-ratio"] : 1; \n\
74 Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
75 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
76 function updateWidthHeightBindings() { \n\
77 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
78 width = root.fixedArtShapeSize.width; \n\
79 height = root.fixedArtShapeSize.height; \n\
80 } else { \n\
81 width = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.width }); \n\
82 height = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.height }); \n\
83 } \n\
84 } \n\
85 CroppedImageMinimumSourceSize { \n\
86 id: artImage; \n\
87 objectName: "artImage"; \n\
88 source: cardData && cardData["art"] || ""; \n\
89 asynchronous: root.asynchronous; \n\
90 visible: false; \n\
91 width: %2; \n\
92 height: %3; \n\
93 } \n\
94 image: artImage.image; \n\
95 } \n\
96 } \n\
97 }\n';
99var kOverlayLoaderCode = 'Loader { \n\
100 id: overlayLoader; \n\
101 anchors { \n\
102 left: artShapeHolder.left; \n\
103 right: artShapeHolder.right; \n\
104 bottom: artShapeHolder.bottom; \n\
105 } \n\
106 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
107 asynchronous: root.asynchronous; \n\
108 visible: showHeader && status == Loader.Ready; \n\
109 sourceComponent: ShaderEffect { \n\
110 id: overlay; \n\
111 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
112 property real luminance: Style.luminance(overlayColor); \n\
113 property color overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
114 property var source: ShaderEffectSource { \n\
115 id: shaderSource; \n\
116 sourceItem: artShapeLoader.item; \n\
117 onVisibleChanged: if (visible) scheduleUpdate(); \n\
118 live: false; \n\
119 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
120 } \n\
121 vertexShader: " \n\
122 uniform highp mat4 qt_Matrix; \n\
123 attribute highp vec4 qt_Vertex; \n\
124 attribute highp vec2 qt_MultiTexCoord0; \n\
125 varying highp vec2 coord; \n\
126 void main() { \n\
127 coord = qt_MultiTexCoord0; \n\
128 gl_Position = qt_Matrix * qt_Vertex; \n\
129 }"; \n\
130 fragmentShader: " \n\
131 varying highp vec2 coord; \n\
132 uniform sampler2D source; \n\
133 uniform lowp float qt_Opacity; \n\
134 uniform highp vec4 overlayColor; \n\
135 void main() { \n\
136 lowp vec4 tex = texture2D(source, coord); \n\
137 gl_FragColor = vec4(overlayColor.r, overlayColor.g, overlayColor.b, 1) * qt_Opacity * overlayColor.a * tex.a; \n\
138 }"; \n\
139 } \n\
140 }\n';
142// multiple row version of HeaderRowCode
143function kHeaderRowCodeGenerator() {
144var kHeaderRowCodeTemplate = 'Row { \n\
145 id: row; \n\
146 objectName: "outerRow"; \n\
147 property real margins: units.gu(1); \n\
148 spacing: margins; \n\
149 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
150 anchors { %1 } \n\
151 anchors.right: parent.right; \n\
152 anchors.margins: margins; \n\
153 anchors.rightMargin: 0; \n\
154 data: [ \n\
155 %2 \n\
156 ] \n\
157 }\n';
158 var args = Array.prototype.slice.call(arguments);
159 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(args.join(',\n'));
160 return code;
163// multiple item version of kHeaderContainerCode
164function kHeaderContainerCodeGenerator() {
165 var headerContainerCodeTemplate = 'Item { \n\
166 id: headerTitleContainer; \n\
167 anchors { %1 } \n\
168 width: parent.width - x; \n\
169 implicitHeight: %2; \n\
170 data: [ \n\
171 %3 \n\
172 ]\n\
173 }\n';
174 var args = Array.prototype.slice.call(arguments);
175 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(',\n'));
176 return code;
179// %1 is used as anchors of mascotShapeLoader
180var kMascotShapeLoaderCode = 'Loader { \n\
181 id: mascotShapeLoader; \n\
182 objectName: "mascotShapeLoader"; \n\
183 asynchronous: root.asynchronous; \n\
184 active: mascotImage.image.status === Image.Ready; \n\
185 visible: showHeader && active && status == Loader.Ready; \n\
186 width: units.gu(6); \n\
187 height: units.gu(5.625); \n\
188 sourceComponent: UbuntuShape { image: mascotImage.image } \n\
189 anchors { %1 } \n\
190 }\n';
192// %1 is used as anchors of mascotImage
193// %2 is used as visible of mascotImage
194var kMascotImageCode = 'CroppedImageMinimumSourceSize { \n\
195 id: mascotImage; \n\
196 objectName: "mascotImage"; \n\
197 anchors { %1 } \n\
198 source: cardData && cardData["mascot"] || ""; \n\
199 width: units.gu(6); \n\
200 height: units.gu(5.625); \n\
201 horizontalAlignment: Image.AlignHCenter; \n\
202 verticalAlignment: Image.AlignVCenter; \n\
203 visible: %2; \n\
204 }\n';
206// %1 is used as anchors of titleLabel
207// %2 is used as color of titleLabel
208// %3 is used as extra condition for visible of titleLabel
209var kTitleLabelCode = 'Label { \n\
210 id: titleLabel; \n\
211 objectName: "titleLabel"; \n\
212 anchors { %1 } \n\
213 elide: Text.ElideRight; \n\
214 fontSize: "small"; \n\
215 wrapMode: Text.Wrap; \n\
216 maximumLineCount: 2; \n\
217 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
218 color: %2; \n\
219 visible: showHeader %3; \n\
220 text: root.title; \n\
221 font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
222 horizontalAlignment: root.titleAlignment; \n\
223 }\n';
225// %1 is used as extra anchors of emblemIcon
226// %2 is used as color of emblemIcon
227var kEmblemIconCode = 'Icon { \n\
228 id: emblemIcon; \n\
229 objectName: "emblemIcon"; \n\
230 anchors { \n\
231 bottom: titleLabel.baseline; \n\
232 right: parent.right; \n\
233 %1
234 } \n\
235 source: cardData && cardData["emblem"] || ""; \n\
236 color: %2; \n\
237 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
238 }\n';
240// %1 is used as anchors of touchdown effect
241var kTouchdownCode = 'UbuntuShape { \n\
242 id: touchdown; \n\
243 objectName: "touchdown"; \n\
244 anchors { %1 } \n\
245 visible: root.pressed; \n\
246 radius: "medium"; \n\
247 borderSource: "radius_pressed.sci" \n\
248 }\n';
250// %1 is used as anchors of subtitleLabel
251// %2 is used as color of subtitleLabel
252var kSubtitleLabelCode = 'Label { \n\
253 id: subtitleLabel; \n\
254 objectName: "subtitleLabel"; \n\
255 anchors { %1 } \n\
256 anchors.topMargin: units.dp(2); \n\
257 elide: Text.ElideRight; \n\
258 fontSize: "x-small"; \n\
259 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
260 color: %2; \n\
261 visible: titleLabel.visible && titleLabel.text; \n\
262 text: cardData && cardData["subtitle"] || ""; \n\
263 font.weight: Font.Light; \n\
264 }\n';
266// %1 is used as anchors of attributesRow
267// %2 is used as color of attributesRow
268var kAttributesRowCode = 'CardAttributes { \n\
269 id: attributesRow; \n\
270 objectName: "attributesRow"; \n\
271 anchors { %1 } \n\
272 color: %2; \n\
273 fontScale: root.fontScale; \n\
274 model: cardData && cardData["attributes"]; \n\
275 }\n';
277// %1 is used as top anchor of summary
278// %2 is used as topMargin anchor of summary
279// %3 is used as color of summary
280var kSummaryLabelCode = 'Label { \n\
281 id: summary; \n\
282 objectName: "summaryLabel"; \n\
283 anchors { \n\
284 top: %1; \n\
285 left: parent.left; \n\
286 right: parent.right; \n\
287 margins: units.gu(1); \n\
288 topMargin: %2; \n\
289 } \n\
290 wrapMode: Text.Wrap; \n\
291 maximumLineCount: 5; \n\
292 elide: Text.ElideRight; \n\
293 text: cardData && cardData["summary"] || ""; \n\
294 height: text ? implicitHeight : 0; \n\
295 fontSize: "small"; \n\
296 color: %3; \n\
297 }\n';
299function cardString(template, components) {
300 var code;
301 code = 'AbstractButton { \n\
302 id: root; \n\
303 property var template; \n\
304 property var components; \n\
305 property var cardData; \n\
306 property var artShapeBorderSource: undefined; \n\
307 property real fontScale: 1.0; \n\
308 property var scopeStyle: null; \n\
309 property int titleAlignment: Text.AlignLeft; \n\
310 property int fixedHeaderHeight: -1; \n\
311 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
312 readonly property string title: cardData && cardData["title"] || ""; \n\
313 property bool asynchronous: true; \n\
314 property bool showHeader: true; \n\
315 implicitWidth: childrenRect.width; \n\
316 enabled: root.template == null ? true : (root.template["non-interactive"] !== undefined ? !root.template["non-interactive"] : true); \n\
317 \n';
319 var hasArt = components["art"] && components["art"]["field"] || false;
320 var hasSummary = components["summary"] || false;
321 var artAndSummary = hasArt && hasSummary;
322 var isHorizontal = template["card-layout"] === "horizontal";
323 var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
324 var hasTitle = components["title"] || false;
325 var hasMascot = components["mascot"] || false;
326 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
327 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
328 var hasSubtitle = hasTitle && components["subtitle"] || false;
329 var hasHeaderRow = hasMascot && hasTitle;
330 var hasAttributes = hasTitle && components["attributes"]["field"] || false;
332 if (hasBackground) {
333 code += kBackgroundLoaderCode;
334 }
336 if (hasArt) {
337 code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
338 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
340 var widthCode, heightCode;
341 var artAnchors;
342 if (isHorizontal) {
343 artAnchors = 'left: parent.left';
344 if (hasMascot || hasTitle) {
345 widthCode = 'height * artShape.aspect'
346 heightCode = 'headerHeight + 2 * units.gu(1)';
347 } else {
348 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
349 // So we define a "random" height of the image height + 2 gu for the margins
350 widthCode = 'height * artShape.aspect'
351 heightCode = 'units.gu(7.625)';
352 }
353 } else {
354 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
355 widthCode = 'root.width'
356 heightCode = 'width / artShape.aspect';
357 }
359 code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
360 } else {
361 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
362 }
364 if (headerAsOverlay) {
365 code += kOverlayLoaderCode;
366 }
368 var headerVerticalAnchors;
369 if (headerAsOverlay) {
370 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
371 bottomMargin: units.gu(1);\n';
372 } else {
373 if (hasArt) {
374 if (isHorizontal) {
375 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
376 topMargin: units.gu(1);\n';
377 } else {
378 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
379 topMargin: units.gu(1);\n';
380 }
381 } else {
382 headerVerticalAnchors = 'top: parent.top; \n\
383 topMargin: units.gu(1);\n';
384 }
385 }
386 var headerLeftAnchor;
387 var headerLeftAnchorHasMargin = false;
388 if (isHorizontal && hasArt) {
389 headerLeftAnchor = 'left: artShapeHolder.right; \n\
390 leftMargin: units.gu(1);\n';
391 headerLeftAnchorHasMargin = true;
392 } else {
393 headerLeftAnchor = 'left: parent.left;\n';
394 }
396 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
398 if (hasHeaderRow) {
399 code += 'readonly property int headerHeight: row.height;\n'
400 } else if (hasMascot) {
401 code += 'readonly property int headerHeight: mascotImage.height;\n'
402 } else if (hasAttributes) {
403 if (hasTitle && hasSubtitle) {
404 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
405 } else if (hasTitle) {
406 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
407 } else {
408 code += 'readonly property int headerHeight: attributesRow.height;\n'
409 }
410 } else if (hasSubtitle) {
411 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
412 } else if (hasTitle) {
413 code += 'readonly property int headerHeight: titleLabel.height;\n'
414 } else {
415 code += 'readonly property int headerHeight: 0;\n'
416 }
418 var mascotShapeCode = '';
419 var mascotCode = '';
420 if (hasMascot) {
421 var useMascotShape = !hasBackground && !headerAsOverlay;
422 var mascotAnchors = '';
423 if (!hasHeaderRow) {
424 mascotAnchors += headerLeftAnchor;
425 mascotAnchors += headerVerticalAnchors;
426 if (!headerLeftAnchorHasMargin) {
427 mascotAnchors += 'leftMargin: units.gu(1);\n'
428 }
429 } else {
430 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
431 }
433 if (useMascotShape) {
434 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
435 }
437 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
438 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
439 }
441 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")';
443 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
444 var titleSubtitleCode = '';
445 if (hasTitle) {
446 var titleColor;
447 if (headerAsOverlay) {
448 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
449 } else if (hasSummary) {
450 titleColor = 'summary.color';
451 } else if (hasBackground) {
452 titleColor = summaryColorWithBackground;
453 } else {
454 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
455 }
457 var titleAnchors;
458 var subtitleAnchors;
459 var attributesAnchors;
460 var titleContainerAnchors;
461 var titleRightAnchor;
463 var extraRightAnchor = '';
464 var extraLeftAnchor = '';
465 if (!touchdownOnArtShape) {
466 extraRightAnchor = 'rightMargin: units.gu(1); \n';
467 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
468 } else if (headerAsOverlay && !hasEmblem) {
469 extraRightAnchor = 'rightMargin: units.gu(1); \n';
470 }
472 if (hasMascot) {
473 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
474 } else {
475 titleContainerAnchors = 'right: parent.right; ';
476 titleContainerAnchors += headerLeftAnchor;
477 titleContainerAnchors += headerVerticalAnchors;
478 if (!headerLeftAnchorHasMargin) {
479 titleContainerAnchors += extraLeftAnchor;
480 }
481 }
482 if (hasEmblem) {
483 titleRightAnchor = 'right: emblemIcon.left; \n\
484 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
485 } else {
486 titleRightAnchor = 'right: parent.right; \n'
487 titleRightAnchor += extraRightAnchor;
488 }
490 if (hasTitleContainer) {
491 // Using headerTitleContainer
492 titleAnchors = titleRightAnchor;
493 titleAnchors += 'left: parent.left; \n\
494 top: parent.top;';
495 subtitleAnchors = 'right: parent.right; \n\
496 left: parent.left; \n';
497 subtitleAnchors += extraRightAnchor;
498 if (hasSubtitle) {
499 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
500 subtitleAnchors += 'top: titleLabel.bottom;\n';
501 } else {
502 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
503 }
504 } else if (hasMascot) {
505 // Using row + titleContainer
506 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
507 } else {
508 if (headerAsOverlay) {
509 // Using anchors to the overlay
510 titleAnchors = titleRightAnchor;
511 titleAnchors += 'left: parent.left; \n\
512 leftMargin: units.gu(1); \n\
513 top: overlayLoader.top; \n\
514 topMargin: units.gu(1);\n';
515 } else {
516 // Using anchors to the mascot/parent
517 titleAnchors = titleRightAnchor;
518 titleAnchors += headerLeftAnchor;
519 titleAnchors += headerVerticalAnchors;
520 if (!headerLeftAnchorHasMargin) {
521 titleAnchors += extraLeftAnchor;
522 }
523 }
524 subtitleAnchors = 'left: titleLabel.left; \n\
525 leftMargin: titleLabel.leftMargin; \n';
526 subtitleAnchors += extraRightAnchor;
527 if (hasEmblem) {
528 // using container
529 subtitleAnchors += 'right: parent.right; \n';
530 } else {
531 subtitleAnchors += 'right: titleLabel.right; \n';
532 }
534 if (hasSubtitle) {
535 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
536 subtitleAnchors += 'top: titleLabel.bottom;\n';
537 } else {
538 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
539 }
540 }
542 // code for different elements
543 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
544 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
545 var subtitleCode;
546 var attributesCode;
548 // code for the title container
549 var containerCode = [];
550 var containerHeight = 'titleLabel.height';
551 containerCode.push(titleCode);
552 if (hasSubtitle) {
553 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
554 containerCode.push(subtitleCode);
555 containerHeight += ' + subtitleLabel.height';
556 }
557 if (hasEmblem) {
558 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
559 }
560 if (hasAttributes) {
561 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
562 containerCode.push(attributesCode);
563 containerHeight += ' + attributesRow.height';
564 }
566 if (hasTitleContainer) {
567 // use container
568 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
569 } else {
570 // no container
571 titleSubtitleCode = titleCode;
572 if (hasSubtitle) {
573 titleSubtitleCode += subtitleCode;
574 }
575 if (hasAttributes) {
576 titleSubtitleCode += attributesCode;
577 }
578 }
579 }
581 if (hasHeaderRow) {
582 var rowCode = [mascotCode, titleSubtitleCode];
583 if (mascotShapeCode != '') {
584 rowCode.unshift(mascotShapeCode);
585 }
586 code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
587 } else {
588 code += mascotShapeCode + mascotCode + titleSubtitleCode;
589 }
591 if (hasSummary) {
592 var summaryTopAnchor;
593 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
594 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
595 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
596 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
597 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
598 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
599 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
600 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
601 else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
602 else summaryTopAnchor = 'parent.top';
604 var summaryColor;
605 if (hasBackground) {
606 summaryColor = summaryColorWithBackground;
607 } else {
608 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
609 }
611 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
613 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
614 }
616 var touchdownAnchors;
617 if (hasBackground) {
618 touchdownAnchors = 'fill: backgroundLoader';
619 } else if (touchdownOnArtShape) {
620 touchdownAnchors = 'fill: artShapeHolder';
621 } else {
622 touchdownAnchors = 'fill: root'
623 }
624 code += kTouchdownCode.arg(touchdownAnchors);
626 var implicitHeight = 'implicitHeight: ';
627 if (hasSummary) {
628 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
629 } else if (hasHeaderRow) {
630 implicitHeight += 'row.y + row.height + units.gu(1);\n';
631 } else if (hasMascot) {
632 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
633 } else if (hasTitleContainer) {
634 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
635 } else if (hasAttributes) {
636 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
637 } else if (hasSubtitle) {
638 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
639 } else if (hasTitle) {
640 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
641 } else if (hasArt) {
642 implicitHeight += 'artShapeHolder.height;\n';
643 } else {
644 implicitHeight = '';
645 }
647 // Close the AbstractButton
648 code += implicitHeight + '}\n';
650 return code;
653function createCardComponent(parent, template, components) {
654 var imports = 'import QtQuick 2.2; \n\
655 import Ubuntu.Components 1.1; \n\
656 import Ubuntu.Settings.Components 0.1; \n\
657 import Dash 0.1;\n\
658 import Utils 0.1;\n';
659 var card = cardString(template, components);
660 var code = imports + 'Component {\n' + card + '}\n';
662 try {
663 return Qt.createQmlObject(code, parent, "createCardComponent");
664 } catch (e) {
665 console.error("ERROR: Invalid component created.");
666 console.error("Template:");
667 console.error(JSON.stringify(template));
668 console.error("Components:");
669 console.error(JSON.stringify(components));
670 console.error("Code:");
671 console.error(code);
672 throw e;
673 }