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