on 2f50de2 help sign in
unity8/plugins/Dash/CardCreator.js
650 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: 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; \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 readonly property bool aspectSmallerThanImageAspect: aspect < image.aspect; \n\
75 Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
76 onAspectSmallerThanImageAspectChanged: updateWidthHeightBindings(); \n\
77 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
78 function updateWidthHeightBindings() { \n\
79 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
80 width = root.fixedArtShapeSize.width; \n\
81 height = root.fixedArtShapeSize.height; \n\
82 } else if (aspectSmallerThanImageAspect) { \n\
83 width = Qt.binding(function() { return !visible ? 0 : image.width }); \n\
84 height = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.height : width / image.aspect }); \n\
85 } else { \n\
86 width = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.width : height * image.aspect }); \n\
87 height = Qt.binding(function() { return !visible ? 0 : image.height }); \n\
88 } \n\
89 } \n\
90 image: Image { \n\
91 objectName: "artImage"; \n\
92 source: cardData && cardData["art"] || ""; \n\
93 cache: true; \n\
94 asynchronous: root.asynchronous; \n\
95 fillMode: components && components["art"]["fill-mode"] === "fit" ? Image.PreserveAspectFit: Image.PreserveAspectCrop; \n\
96 readonly property real aspect: implicitWidth / implicitHeight; \n\
97 width: %2; \n\
98 height: %3; \n\
99 } \n\
100 } \n\
101 } \n\
102 }\n';
104var kOverlayLoaderCode = 'Loader { \n\
105 id: overlayLoader; \n\
106 anchors { \n\
107 left: artShapeHolder.left; \n\
108 right: artShapeHolder.right; \n\
109 bottom: artShapeHolder.bottom; \n\
110 } \n\
111 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
112 asynchronous: root.asynchronous; \n\
113 visible: showHeader && status == Loader.Ready; \n\
114 sourceComponent: ShaderEffect { \n\
115 id: overlay; \n\
116 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
117 opacity: 0.6; \n\
118 property var source: ShaderEffectSource { \n\
119 id: shaderSource; \n\
120 sourceItem: artShapeLoader.item; \n\
121 onVisibleChanged: if (visible) scheduleUpdate(); \n\
122 live: false; \n\
123 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
124 } \n\
125 vertexShader: " \n\
126 uniform highp mat4 qt_Matrix; \n\
127 attribute highp vec4 qt_Vertex; \n\
128 attribute highp vec2 qt_MultiTexCoord0; \n\
129 varying highp vec2 coord; \n\
130 void main() { \n\
131 coord = qt_MultiTexCoord0; \n\
132 gl_Position = qt_Matrix * qt_Vertex; \n\
133 }"; \n\
134 fragmentShader: " \n\
135 varying highp vec2 coord; \n\
136 uniform sampler2D source; \n\
137 uniform lowp float qt_Opacity; \n\
138 void main() { \n\
139 lowp vec4 tex = texture2D(source, coord); \n\
140 gl_FragColor = vec4(0, 0, 0, tex.a) * qt_Opacity; \n\
141 }"; \n\
142 } \n\
143 }\n';
145// multiple row version of HeaderRowCode
146function kHeaderRowCodeGenerator() {
147var kHeaderRowCodeTemplate = 'Row { \n\
148 id: row; \n\
149 objectName: "outerRow"; \n\
150 property real margins: units.gu(1); \n\
151 spacing: margins; \n\
152 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
153 anchors { %1 } \n\
154 anchors.right: parent.right; \n\
155 anchors.margins: margins; \n\
156 anchors.rightMargin: units.gu(0); \n\
157 data: [ \n\
158 %2 \n\
159 ] \n\
160 }\n';
161 var args = Array.prototype.slice.call(arguments);
162 var HeaderRowAnchors = args.shift();
163 var code = kHeaderRowCodeTemplate.arg(HeaderRowAnchors).arg(args.join(',\n'));
164 return code;
167// multiple item version of kHeaderContainerCode
168function kHeaderContainerCodeGenerator() {
169 var headerContainerCodeTemplate = 'Item { \n\
170 id: headerTitleContainer; \n\
171 anchors { %1 } \n\
172 width: parent.width - x; \n\
173 implicitHeight: %2; \n\
174 data: [ \n\
175 %3 \n\
176 ]\n\
177 }\n';
178 var args = Array.prototype.slice.call(arguments);
179 var headerContainerAnchors = args.shift();
180 var headerContainerHeight = args.shift();
181 var code = headerContainerCodeTemplate.arg(headerContainerAnchors).arg(headerContainerHeight).arg(args.join(',\n'));
182 return code;
185// %1 is used as anchors of mascotShapeLoader
186var kMascotShapeLoaderCode = 'Loader { \n\
187 id: mascotShapeLoader; \n\
188 objectName: "mascotShapeLoader"; \n\
189 asynchronous: root.asynchronous; \n\
190 active: mascotImage.status === Image.Ready; \n\
191 visible: showHeader && active && status == Loader.Ready; \n\
192 width: units.gu(6); \n\
193 height: units.gu(5.625); \n\
194 sourceComponent: UbuntuShape { image: mascotImage } \n\
195 anchors { %1 } \n\
196 }\n';
198// %1 is used as anchors of mascotImage
199// %2 is used as visible of mascotImage
200var kMascotImageCode = 'Image { \n\
201 id: mascotImage; \n\
202 objectName: "mascotImage"; \n\
203 anchors { %1 } \n\
204 readonly property int maxSize: Math.max(width, height) * 4; \n\
205 source: cardData && cardData["mascot"]; \n\
206 width: units.gu(6); \n\
207 height: units.gu(5.625); \n\
208 sourceSize { width: maxSize; height: maxSize } \n\
209 fillMode: Image.PreserveAspectCrop; \n\
210 horizontalAlignment: Image.AlignHCenter; \n\
211 verticalAlignment: Image.AlignVCenter; \n\
212 visible: %2; \n\
213 }\n';
215// %1 is used as anchors of titleLabel
216// %2 is used as color of titleLabel
217// %3 is used as extra condition for visible of titleLabel
218var kTitleLabelCode = 'Label { \n\
219 id: titleLabel; \n\
220 objectName: "titleLabel"; \n\
221 anchors { %1 } \n\
222 elide: Text.ElideRight; \n\
223 fontSize: "small"; \n\
224 wrapMode: Text.Wrap; \n\
225 maximumLineCount: 2; \n\
226 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
227 color: %2; \n\
228 visible: showHeader %3; \n\
229 text: root.title; \n\
230 font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
231 horizontalAlignment: root.headerAlignment; \n\
232 }\n';
234var kEmblemImageCode = 'Image { \n\
235 id: emblemImage; \n\
236 objectName: "emblemImage"; \n\
237 anchors { \n\
238 bottom: titleLabel.baseline; \n\
239 right: parent.right; \n\
240 rightMargin: units.gu(1); \n\
241 } \n\
242 source: cardData && cardData["emblem"]; \n\
243 width: height; \n\
244 height: titleLabel.font.pixelSize; \n\
245 fillMode: Image.PreserveAspectFit; \n\
246 }\n';
248// %1 is used as anchors of touchdown effect
249var kTouchdownCode = 'UbuntuShape { \n\
250 id: touchdown; \n\
251 objectName: "touchdown"; \n\
252 anchors { %1 } \n\
253 visible: root.pressed; \n\
254 radius: "medium"; \n\
255 borderSource: "radius_pressed.sci" \n\
256 }\n';
258// %1 is used as anchors of subtitleLabel
259// %2 is used as color of subtitleLabel
260var kSubtitleLabelCode = 'Label { \n\
261 id: subtitleLabel; \n\
262 objectName: "subtitleLabel"; \n\
263 anchors { %1 } \n\
264 anchors.topMargin: units.dp(2); \n\
265 elide: Text.ElideRight; \n\
266 fontSize: "small"; \n\
267 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
268 color: %2; \n\
269 visible: titleLabel.visible && titleLabel.text; \n\
270 text: cardData && cardData["subtitle"] || ""; \n\
271 font.weight: Font.Light; \n\
272 horizontalAlignment: root.headerAlignment; \n\
273 }\n';
275// %1 is used as anchors of attributesRow
276var kAttributesRowCode = 'CardAttributes { \n\
277 id: attributesRow; \n\
278 objectName: "attributesRow"; \n\
279 anchors { %1 } \n\
280 color: %2; \n\
281 model: cardData && cardData["attributes"] || undefined; \n\
282 }\n';
284// %1 is used as top anchor of summary
285// %2 is used as topMargin anchor of summary
286// %3 is used as color of summary
287var kSummaryLabelCode = 'Label { \n\
288 id: summary; \n\
289 objectName: "summaryLabel"; \n\
290 anchors { \n\
291 top: %1; \n\
292 left: parent.left; \n\
293 right: parent.right; \n\
294 margins: units.gu(1); \n\
295 topMargin: %2; \n\
296 } \n\
297 wrapMode: Text.Wrap; \n\
298 maximumLineCount: 5; \n\
299 elide: Text.ElideRight; \n\
300 text: cardData && cardData["summary"] || ""; \n\
301 height: text ? implicitHeight : 0; \n\
302 fontSize: "small"; \n\
303 color: %3; \n\
304 }\n';
306function cardString(template, components) {
307 var code;
308 code = 'AbstractButton { \n\
309 id: root; \n\
310 property var template; \n\
311 property var components; \n\
312 property var cardData; \n\
313 property var artShapeBorderSource: undefined; \n\
314 property real fontScale: 1.0; \n\
315 property var scopeStyle: null; \n\
316 property int headerAlignment: Text.AlignLeft; \n\
317 property int fixedHeaderHeight: -1; \n\
318 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
319 readonly property string title: cardData && cardData["title"] || ""; \n\
320 property bool asynchronous: true; \n\
321 property bool showHeader: true; \n\
322 implicitWidth: childrenRect.width; \n';
324 var hasArt = components["art"] && components["art"]["field"] || false;
325 var hasSummary = components["summary"] || false;
326 var artAndSummary = hasArt && hasSummary;
327 var isHorizontal = template["card-layout"] === "horizontal";
328 var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
329 var hasTitle = components["title"] || false;
330 var hasMascot = components["mascot"] || false;
331 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
332 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
333 var hasSubtitle = hasTitle && components["subtitle"] || false;
334 var hasHeaderRow = hasMascot && hasTitle;
335 var hasAttributes = hasTitle && components["attributes"] || false;
337 if (hasBackground) {
338 code += kBackgroundLoaderCode;
339 }
341 if (hasArt) {
342 code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
343 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
345 var widthCode, heightCode;
346 var artAnchors;
347 if (isHorizontal) {
348 artAnchors = 'left: parent.left';
349 if (hasMascot || hasTitle) {
350 widthCode = 'height * artShape.aspect'
351 heightCode = 'headerHeight + 2 * units.gu(1)';
352 } else {
353 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
354 // So we define a "random" height of the image height + 2 gu for the margins
355 widthCode = 'height * artShape.aspect'
356 heightCode = 'units.gu(7.625)';
357 }
358 } else {
359 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
360 widthCode = 'root.width'
361 heightCode = 'width / artShape.aspect';
362 }
364 code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
365 } else {
366 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
367 }
369 if (headerAsOverlay) {
370 code += kOverlayLoaderCode;
371 }
373 var headerVerticalAnchors;
374 if (headerAsOverlay) {
375 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
376 bottomMargin: units.gu(1);\n';
377 } else {
378 if (hasArt) {
379 if (isHorizontal) {
380 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
381 topMargin: units.gu(1);\n';
382 } else {
383 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
384 topMargin: units.gu(1);\n';
385 }
386 } else {
387 headerVerticalAnchors = 'top: parent.top; \n\
388 topMargin: units.gu(1);\n';
389 }
390 }
391 var headerLeftAnchor;
392 var headerLeftAnchorHasMargin = false;
393 if (isHorizontal && hasArt) {
394 headerLeftAnchor = 'left: artShapeHolder.right; \n\
395 leftMargin: units.gu(1);\n';
396 headerLeftAnchorHasMargin = true;
397 } else {
398 headerLeftAnchor = 'left: parent.left;\n';
399 }
401 if (hasHeaderRow) {
402 code += 'readonly property int headerHeight: row.height;\n'
403 } else if (hasMascot) {
404 code += 'readonly property int headerHeight: mascotImage.height;\n'
405 } else if (hasAttributes) {
406 if (hasTitle && hasSubtitle) {
407 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
408 } else if (hasTitle) {
409 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
410 } else {
411 code += 'readonly property int headerHeight: attributesRow.height;\n'
412 }
413 } else if (hasSubtitle) {
414 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
415 } else if (hasTitle) {
416 code += 'readonly property int headerHeight: titleLabel.height;\n'
417 } else {
418 code += 'readonly property int headerHeight: 0;\n'
419 }
421 var mascotShapeCode = '';
422 var mascotCode = '';
423 if (hasMascot) {
424 var useMascotShape = !hasBackground && !headerAsOverlay;
425 var mascotAnchors = '';
426 if (!hasHeaderRow) {
427 mascotAnchors += headerLeftAnchor;
428 mascotAnchors += headerVerticalAnchors;
429 if (!headerLeftAnchorHasMargin) {
430 mascotAnchors += 'leftMargin: units.gu(1);\n'
431 }
432 } else {
433 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
434 }
436 if (useMascotShape) {
437 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
438 }
440 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
441 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
442 }
444 var summaryColorWithBackground = 'backgroundLoader.active && backgroundLoader.item && backgroundLoader.item.luminance < 0.7 ? "white" : (root.scopeStyle ? root.scopeStyle.foreground : "grey")';
446 var titleSubtitleCode = '';
447 if (hasTitle) {
448 var titleColor;
449 if (headerAsOverlay) {
450 titleColor = '"white"';
451 } else if (hasSummary) {
452 titleColor = 'summary.color';
453 } else if (hasBackground) {
454 titleColor = summaryColorWithBackground;
455 } else {
456 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
457 }
459 var titleAnchors;
460 var subtitleAnchors;
461 var attributesAnchors;
462 var titleRightAnchor;
463 var titleContainerAnchors;
465 // GOOD LUCK!
467 if (hasMascot) {
468 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
469 } else {
470 titleContainerAnchors = 'right: parent.right; ';
471 titleContainerAnchors += headerLeftAnchor;
472 titleContainerAnchors += headerVerticalAnchors;
473 if (!headerLeftAnchorHasMargin) {
474 titleContainerAnchors += 'leftMargin: units.gu(1);\n';
475 }
476 }
477 if (hasEmblem) {
478 titleRightAnchor = 'right: emblemImage.left;\n';
479 } else {
480 titleRightAnchor = 'right: parent.right;\n';
481 }
483 if (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes))) {
484 // Using headerTitleContainer
485 titleAnchors = titleRightAnchor;
486 titleAnchors += 'left: parent.left; \n\
487 top: parent.top;';
488 subtitleAnchors = 'right: parent.right; \n\
489 left: parent.left; \n\
490 rightMargin: units.gu(1); \n';
491 if (hasSubtitle) {
492 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
493 subtitleAnchors += 'top: titleLabel.bottom;\n';
494 } else {
495 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
496 }
497 } else if (hasMascot) {
498 // Using row + titleContainer
499 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
500 } else {
501 if (headerAsOverlay) {
502 // Using anchors to the overlay
503 titleAnchors = titleRightAnchor;
504 titleAnchors += 'left: parent.left; \n\
505 leftMargin: units.gu(1); \n\
506 top: overlayLoader.top; \n\
507 topMargin: units.gu(1);\n';
508 } else {
509 // Using anchors to the mascot/parent
510 titleAnchors = titleRightAnchor;
511 titleAnchors += headerLeftAnchor;
512 titleAnchors += headerVerticalAnchors;
513 if (!headerLeftAnchorHasMargin) {
514 titleAnchors += 'leftMargin: units.gu(1);\n';
515 }
516 }
517 subtitleAnchors = 'left: titleLabel.left; \n\
518 leftMargin: titleLabel.leftMargin; \n\
519 rightMargin: units.gu(1); \n';
520 if (hasEmblem) {
521 // using container
522 subtitleAnchors += 'right: parent.right; \n';
523 } else {
524 subtitleAnchors += 'right: titleLabel.right; \n';
525 }
527 if (hasSubtitle) {
528 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
529 subtitleAnchors += 'top: titleLabel.bottom;\n';
530 } else {
531 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
532 }
533 }
535 // code for different elements
536 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
537 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
538 var subtitleCode;
539 var attributesCode;
540 var emblemCode;
542 // code for the title container
543 var containerCode = [];
544 var containerHeight = 'titleLabel.height';
545 containerCode.push(titleCode);
546 if (hasSubtitle) {
547 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
548 containerCode.push(subtitleCode);
549 containerHeight += ' + subtitleLabel.height';
550 }
551 if (hasEmblem) {
552 containerCode.push(kEmblemImageCode);
553 }
554 if (hasAttributes) {
555 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
556 containerCode.push(kAttributesRowCode.arg(attributesAnchors).arg(titleColor));
557 containerHeight += ' + attributesRow.height';
558 }
560 if (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes))) {
561 // use container
562 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
563 } else {
564 // no container
565 titleSubtitleCode = titleCode;
566 if (hasSubtitle) {
567 titleSubtitleCode += subtitleCode;
568 }
569 if (hasAttributes) {
570 titleSubtitleCode += attributesCode;
571 }
572 }
573 }
575 if (hasHeaderRow) {
576 var rowCode = [mascotCode, titleSubtitleCode];
577 if (mascotShapeCode != '') {
578 rowCode.unshift(mascotShapeCode);
579 }
580 code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
581 } else {
582 code += mascotShapeCode + mascotCode + titleSubtitleCode;
583 }
585 if (hasSummary) {
586 var summaryTopAnchor;
587 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
588 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
589 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
590 else if (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes))) summaryTopAnchor = 'headerTitleContainer.bottom';
591 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
592 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
593 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
594 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
595 else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
596 else summaryTopAnchor = 'parent.top';
598 var summaryColor;
599 if (hasBackground) {
600 summaryColor = summaryColorWithBackground;
601 } else {
602 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
603 }
605 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
607 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
608 }
610 var touchdownAnchors;
611 if (hasBackground) {
612 touchdownAnchors = 'fill: backgroundLoader';
613 } else if (hasArt && !hasMascot && !hasSummary) {
614 touchdownAnchors = 'fill: artShapeHolder';
615 } else {
616 touchdownAnchors = 'fill: root'
617 }
618 code += kTouchdownCode.arg(touchdownAnchors);
620 if (hasSummary) {
621 code += 'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
622 } else if (hasHeaderRow) {
623 code += 'implicitHeight: row.y + row.height + units.gu(1);\n';
624 } else if (hasMascot) {
625 code += 'implicitHeight: mascotImage.y + mascotImage.height;\n';
626 } else if (hasAttributes) {
627 code += 'implicitHeight: attributesRow.y + attributesRow.height + units.gu(1);\n';
628 } else if (hasSubtitle) {
629 code += 'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
630 } else if (hasTitle) {
631 code += 'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
632 } else if (hasArt) {
633 code += 'implicitHeight: artShapeHolder.height;\n';
634 }
635 // Close the AbstractButton
636 code += '}\n';
638 return code;
641function createCardComponent(parent, template, components) {
642 var imports = 'import QtQuick 2.2; \n\
643 import Ubuntu.Components 0.1; \n\
644 import Ubuntu.Thumbnailer 0.1;\n\
645 import Dash 0.1;\n';
646 var card = cardString(template, components);
647 var code = imports + 'Component {\n' + card + '}\n';
649 return Qt.createQmlObject(code, parent, "createCardComponent");