on d4f6496 help sign in
unity8/plugins/Dash/CardCreator.js
711 lines 36 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 backgroundColor: getColor(0) || "white"; \n\
32 secondaryBackgroundColor: getColor(1) || backgroundColor; \n\
33 backgroundMode: UbuntuShape.VerticalGradient; \n\
34 anchors.fill: parent; \n\
35 source: backgroundImage.source ? backgroundImage : null; \n\
36 property real luminance: Style.luminance(backgroundColor); \n\
37 property Image backgroundImage: Image { \n\
38 objectName: "backgroundImage"; \n\
39 source: { \n\
40 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
41 else return "%1"; \n\
42 } \n\
43 } \n\
44 function getColor(index) { \n\
45 if (cardData && typeof cardData["background"] === "object" \n\
46 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
47 return cardData["background"]["elements"][index]; \n\
48 } else return index === 0 ? %2 : %3; \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 readonly property bool doShapeItem: components["art"]["conciergeMode"] !== true; \n\
71 visible: image.status == Image.Ready; \n\
72 readonly property alias image: artImage; \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 Loader { \n\
82 anchors.fill: parent; \n\
83 visible: artShape.doShapeItem; \n\
84 sourceComponent: root.artShapeStyle === "icon" ? artShapeIconComponent : artShapeShapeComponent; \n\
85 Component { \n\
86 id: artShapeShapeComponent; \n\
87 UbuntuShape { \n\
88 source: artShapeSource; \n\
89 sourceFillMode: UbuntuShape.PreserveAspectCrop; \n\
90 radius: "medium"; \n\
91 aspect: { \n\
92 switch (root.artShapeStyle) { \n\
93 case "inset": return UbuntuShape.Inset; \n\
94 case "shadow": return UbuntuShape.DropShadow; \n\
95 default: \n\
96 case "flat": return UbuntuShape.Flat; \n\
97 } \n\
98 } \n\
99 } \n\
100 } \n\
101 Component { \n\
102 id: artShapeIconComponent; \n\
103 ProportionalShape { source: artShapeSource; aspect: UbuntuShape.DropShadow; } \n\
104 } \n\
105 } \n\
106 readonly property real fixedArtShapeSizeAspect: (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) ? root.fixedArtShapeSize.width / root.fixedArtShapeSize.height : -1; \n\
107 readonly property real aspect: fixedArtShapeSizeAspect > 0 ? fixedArtShapeSizeAspect : components !== undefined ? components["art"]["aspect-ratio"] : 1; \n\
108 Component.onCompleted: { updateWidthHeightBindings(); } \n\
109 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
110 function updateWidthHeightBindings() { \n\
111 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
112 width = root.fixedArtShapeSize.width; \n\
113 height = root.fixedArtShapeSize.height; \n\
114 } else { \n\
115 width = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.width }); \n\
116 height = Qt.binding(function() { return image.status !== Image.Ready ? 0 : image.height }); \n\
117 } \n\
118 } \n\
119 CroppedImageMinimumSourceSize { \n\
120 id: artImage; \n\
121 objectName: "artImage"; \n\
122 source: cardData && cardData["art"] || ""; \n\
123 asynchronous: root.asynchronous; \n\
124 width: %2; \n\
125 height: %3; \n\
126 } \n\
127 } \n\
128 } \n\
129 }\n';
131var kOverlayLoaderCode = 'Loader { \n\
132 id: overlayLoader; \n\
133 readonly property real overlayHeight: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
134 anchors.fill: artShapeHolder; \n\
135 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
136 asynchronous: root.asynchronous; \n\
137 visible: showHeader && status == Loader.Ready; \n\
138 sourceComponent: UbuntuShapeOverlay { \n\
139 id: overlay; \n\
140 property real luminance: Style.luminance(overlayColor); \n\
141 aspect: UbuntuShape.Flat; \n\
142 radius: "medium"; \n\
143 overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
144 overlayRect: Qt.rect(0, 1 - overlayLoader.overlayHeight / height, 1, 1); \n\
145 } \n\
146 }\n';
148// multiple row version of HeaderRowCode
149function kHeaderRowCodeGenerator() {
150 var kHeaderRowCodeTemplate = 'Row { \n\
151 id: row; \n\
152 objectName: "outerRow"; \n\
153 property real margins: units.gu(1); \n\
154 spacing: margins; \n\
155 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
156 anchors { %1 } \n\
157 anchors.right: parent.right; \n\
158 anchors.margins: margins; \n\
159 anchors.rightMargin: 0; \n\
160 data: [ \n\
161 %2 \n\
162 ] \n\
163 }\n';
164 var args = Array.prototype.slice.call(arguments);
165 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(args.join(',\n'));
166 return code;
169// multiple item version of kHeaderContainerCode
170function kHeaderContainerCodeGenerator() {
171 var headerContainerCodeTemplate = 'Item { \n\
172 id: headerTitleContainer; \n\
173 anchors { %1 } \n\
174 width: parent.width - x; \n\
175 implicitHeight: %2; \n\
176 data: [ \n\
177 %3 \n\
178 ]\n\
179 }\n';
180 var args = Array.prototype.slice.call(arguments);
181 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).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 = 'CroppedImageMinimumSourceSize { \n\
201 id: mascotImage; \n\
202 objectName: "mascotImage"; \n\
203 anchors { %1 } \n\
204 source: cardData && cardData["mascot"] || ""; \n\
205 width: units.gu(6); \n\
206 height: units.gu(5.625); \n\
207 horizontalAlignment: Image.AlignHCenter; \n\
208 verticalAlignment: Image.AlignVCenter; \n\
209 visible: %2; \n\
210 }\n';
212// %1 is used as anchors of titleLabel
213// %2 is used as color of titleLabel
214// %3 is used as extra condition for visible of titleLabel
215// %4 is used as title width
216var kTitleLabelCode = 'Label { \n\
217 id: titleLabel; \n\
218 objectName: "titleLabel"; \n\
219 anchors { %1 } \n\
220 elide: Text.ElideRight; \n\
221 fontSize: "small"; \n\
222 wrapMode: Text.Wrap; \n\
223 maximumLineCount: 2; \n\
224 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
225 color: %2; \n\
226 visible: showHeader %3; \n\
227 width: %4; \n\
228 text: root.title; \n\
229 font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
230 horizontalAlignment: root.titleAlignment; \n\
231 }\n';
233// %1 is used as extra anchors of emblemIcon
234// %2 is used as color of emblemIcon
235// FIXME The width code is a
236// Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
237var kEmblemIconCode = 'Icon { \n\
238 id: emblemIcon; \n\
239 objectName: "emblemIcon"; \n\
240 anchors { \n\
241 bottom: titleLabel.baseline; \n\
242 right: parent.right; \n\
243 %1 \n\
244 } \n\
245 source: cardData && cardData["emblem"] || ""; \n\
246 color: %2; \n\
247 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
248 width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
249 }\n';
251// %1 is used as anchors of touchdown effect
252var kTouchdownCode = 'UbuntuShape { \n\
253 id: touchdown; \n\
254 objectName: "touchdown"; \n\
255 anchors { %1 } \n\
256 visible: root.artShapeStyle != "shadow" && root.artShapeStyle != "icon" && root.pressed; \n\
257 radius: "medium"; \n\
258 borderSource: "radius_pressed.sci" \n\
259 }\n';
261// %1 is used as anchors of subtitleLabel
262// %2 is used as color of subtitleLabel
263var kSubtitleLabelCode = 'Label { \n\
264 id: subtitleLabel; \n\
265 objectName: "subtitleLabel"; \n\
266 anchors { %1 } \n\
267 anchors.topMargin: units.dp(2); \n\
268 elide: Text.ElideRight; \n\
269 maximumLineCount: 1; \n\
270 fontSize: "x-small"; \n\
271 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
272 color: %2; \n\
273 visible: titleLabel.visible && titleLabel.text; \n\
274 text: cardData && cardData["subtitle"] || ""; \n\
275 font.weight: Font.Light; \n\
276 }\n';
278// %1 is used as anchors of attributesRow
279// %2 is used as color of attributesRow
280var kAttributesRowCode = 'CardAttributes { \n\
281 id: attributesRow; \n\
282 objectName: "attributesRow"; \n\
283 anchors { %1 } \n\
284 color: %2; \n\
285 fontScale: root.fontScale; \n\
286 model: cardData && cardData["attributes"]; \n\
287 }\n';
289// %1 is used as top anchor of summary
290// %2 is used as topMargin anchor of summary
291// %3 is used as color of summary
292var kSummaryLabelCode = 'Label { \n\
293 id: summary; \n\
294 objectName: "summaryLabel"; \n\
295 anchors { \n\
296 top: %1; \n\
297 left: parent.left; \n\
298 right: parent.right; \n\
299 margins: units.gu(1); \n\
300 topMargin: %2; \n\
301 } \n\
302 wrapMode: Text.Wrap; \n\
303 maximumLineCount: 5; \n\
304 elide: Text.ElideRight; \n\
305 text: cardData && cardData["summary"] || ""; \n\
306 height: text ? implicitHeight : 0; \n\
307 fontSize: "small"; \n\
308 color: %3; \n\
309 }\n';
311function cardString(template, components) {
312 var code;
314 var templateInteractive = (template == null ? true : (template["non-interactive"] !== undefined ? !template["non-interactive"] : true)) ? "true" : "false";
316 code = 'AbstractButton { \n\
317 id: root; \n\
318 property var components; \n\
319 property var cardData; \n\
320 property string artShapeStyle: "inset"; \n\
321 property real fontScale: 1.0; \n\
322 property var scopeStyle: null; \n\
323 property int titleAlignment: Text.AlignLeft; \n\
324 property int fixedHeaderHeight: -1; \n\
325 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
326 readonly property string title: cardData && cardData["title"] || ""; \n\
327 property bool asynchronous: true; \n\
328 property bool showHeader: true; \n\
329 implicitWidth: childrenRect.width; \n\
330 enabled: %1; \n\
331 \n'.arg(templateInteractive);
333 var hasArt = components["art"] && components["art"]["field"] || false;
334 var hasSummary = components["summary"] || false;
335 var artAndSummary = hasArt && hasSummary && components["art"]["conciergeMode"] !== true;
336 var isHorizontal = template["card-layout"] === "horizontal";
337 var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
338 (hasSummary && (template["card-background"] || components["background"]));
339 var hasTitle = components["title"] || false;
340 var hasMascot = components["mascot"] || false;
341 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
342 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
343 var hasSubtitle = hasTitle && components["subtitle"] || false;
344 var hasHeaderRow = hasMascot && hasTitle;
345 var hasAttributes = hasTitle && components["attributes"]["field"] || false;
347 if (hasBackground) {
348 var templateCardBackground = (template && typeof template["card-background"] === "string") ? template["card-background"] : "";
349 var backgroundElements0;
350 var backgroundElements1;
351 if (template && typeof template["card-background"] === "object" && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) {
352 if (template["card-background"]["elements"][0] !== undefined) {
353 backgroundElements0 = '"%1"'.arg(template["card-background"]["elements"][0]);
354 }
355 if (template["card-background"]["elements"][1] !== undefined) {
356 backgroundElements1 = '"%1"'.arg(template["card-background"]["elements"][1]);
357 }
358 }
359 code += kBackgroundLoaderCode.arg(templateCardBackground).arg(backgroundElements0).arg(backgroundElements1);
360 }
362 if (hasArt) {
363 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
365 var widthCode, heightCode;
366 var artAnchors;
367 if (isHorizontal) {
368 artAnchors = 'left: parent.left';
369 if (hasMascot || hasTitle) {
370 widthCode = 'height * artShape.aspect'
371 heightCode = 'headerHeight + 2 * units.gu(1)';
372 } else {
373 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
374 // So we define a "random" height of the image height + 2 gu for the margins
375 widthCode = 'height * artShape.aspect'
376 heightCode = 'units.gu(7.625)';
377 }
378 } else {
379 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
380 widthCode = 'root.width'
381 heightCode = 'width / artShape.aspect';
382 }
384 code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
385 var fallback = components["art"] && components["art"]["fallback"] || "";
386 if (fallback !== "") {
387 code += 'Connections { target: artShapeLoader.item ? artShapeLoader.item.image : null; onStatusChanged: if (artShapeLoader.item.image.status === Image.Error) artShapeLoader.item.image.source = "%1"; } \n'.arg(fallback);
388 }
389 } else {
390 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
391 }
393 if (headerAsOverlay) {
394 code += kOverlayLoaderCode;
395 }
397 var headerVerticalAnchors;
398 if (headerAsOverlay) {
399 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
400 bottomMargin: units.gu(1);\n';
401 } else {
402 if (hasArt) {
403 if (isHorizontal) {
404 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
405 topMargin: units.gu(1);\n';
406 } else {
407 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
408 topMargin: units.gu(1);\n';
409 }
410 } else {
411 headerVerticalAnchors = 'top: parent.top; \n\
412 topMargin: units.gu(1);\n';
413 }
414 }
415 var headerLeftAnchor;
416 var headerLeftAnchorHasMargin = false;
417 if (isHorizontal && hasArt) {
418 headerLeftAnchor = 'left: artShapeHolder.right; \n\
419 leftMargin: units.gu(1);\n';
420 headerLeftAnchorHasMargin = true;
421 } else {
422 headerLeftAnchor = 'left: parent.left;\n';
423 }
425 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
427 if (hasHeaderRow) {
428 code += 'readonly property int headerHeight: row.height;\n'
429 } else if (hasMascot) {
430 code += 'readonly property int headerHeight: mascotImage.height;\n'
431 } else if (hasAttributes) {
432 if (hasTitle && hasSubtitle) {
433 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
434 } else if (hasTitle) {
435 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
436 } else {
437 code += 'readonly property int headerHeight: attributesRow.height;\n'
438 }
439 } else if (hasSubtitle) {
440 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
441 } else if (hasTitle) {
442 code += 'readonly property int headerHeight: titleLabel.height;\n'
443 } else {
444 code += 'readonly property int headerHeight: 0;\n'
445 }
447 var mascotShapeCode = '';
448 var mascotCode = '';
449 if (hasMascot) {
450 var useMascotShape = !hasBackground && !headerAsOverlay;
451 var mascotAnchors = '';
452 if (!hasHeaderRow) {
453 mascotAnchors += headerLeftAnchor;
454 mascotAnchors += headerVerticalAnchors;
455 if (!headerLeftAnchorHasMargin) {
456 mascotAnchors += 'leftMargin: units.gu(1);\n'
457 }
458 } else {
459 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
460 }
462 if (useMascotShape) {
463 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
464 }
466 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
467 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
468 var fallback = components["mascot"] && components["mascot"]["fallback"] || "";
469 if (fallback !== "") {
470 code += 'Connections { target: mascotImage; onStatusChanged: if (mascotImage.status === Image.Error) mascotImage.source = "%1"; } \n'.arg(fallback);
471 }
472 }
474 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")';
476 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
477 var titleSubtitleCode = '';
478 if (hasTitle) {
479 var titleColor;
480 if (headerAsOverlay) {
481 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
482 } else if (hasSummary) {
483 titleColor = 'summary.color';
484 } else if (hasBackground) {
485 titleColor = summaryColorWithBackground;
486 } else {
487 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
488 }
490 var titleAnchors;
491 var subtitleAnchors;
492 var attributesAnchors;
493 var titleContainerAnchors;
494 var titleRightAnchor;
495 var titleWidth = "undefined";
497 var extraRightAnchor = '';
498 var extraLeftAnchor = '';
499 if (!touchdownOnArtShape) {
500 extraRightAnchor = 'rightMargin: units.gu(1); \n';
501 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
502 } else if (headerAsOverlay && !hasEmblem) {
503 extraRightAnchor = 'rightMargin: units.gu(1); \n';
504 }
506 if (hasMascot) {
507 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
508 } else {
509 titleContainerAnchors = 'right: parent.right; ';
510 titleContainerAnchors += headerLeftAnchor;
511 titleContainerAnchors += headerVerticalAnchors;
512 if (!headerLeftAnchorHasMargin) {
513 titleContainerAnchors += extraLeftAnchor;
514 }
515 }
516 if (hasEmblem) {
517 titleRightAnchor = 'right: emblemIcon.left; \n\
518 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
519 } else {
520 titleRightAnchor = 'right: parent.right; \n'
521 titleRightAnchor += extraRightAnchor;
522 }
524 if (hasTitleContainer) {
525 // Using headerTitleContainer
526 titleAnchors = titleRightAnchor;
527 titleAnchors += 'left: parent.left; \n\
528 top: parent.top;';
529 subtitleAnchors = 'right: parent.right; \n\
530 left: parent.left; \n';
531 subtitleAnchors += extraRightAnchor;
532 if (hasSubtitle) {
533 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
534 subtitleAnchors += 'top: titleLabel.bottom;\n';
535 } else {
536 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
537 }
538 } else if (hasMascot) {
539 // Using row without titleContainer
540 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
541 titleWidth = "parent.width - x";
542 } else {
543 if (headerAsOverlay) {
544 // Using anchors to the overlay
545 titleAnchors = titleRightAnchor;
546 titleAnchors += 'left: parent.left; \n\
547 leftMargin: units.gu(1); \n\
548 top: overlayLoader.top; \n\
549 topMargin: units.gu(1) + overlayLoader.height - overlayLoader.overlayHeight; \n';
550 } else {
551 // Using anchors to the mascot/parent
552 titleAnchors = titleRightAnchor;
553 titleAnchors += headerLeftAnchor;
554 titleAnchors += headerVerticalAnchors;
555 if (!headerLeftAnchorHasMargin) {
556 titleAnchors += extraLeftAnchor;
557 }
558 }
559 subtitleAnchors = 'left: titleLabel.left; \n\
560 leftMargin: titleLabel.leftMargin; \n';
561 subtitleAnchors += extraRightAnchor;
562 if (hasEmblem) {
563 // using container
564 subtitleAnchors += 'right: parent.right; \n';
565 } else {
566 subtitleAnchors += 'right: titleLabel.right; \n';
567 }
569 if (hasSubtitle) {
570 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
571 subtitleAnchors += 'top: titleLabel.bottom;\n';
572 } else {
573 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
574 }
575 }
577 // code for different elements
578 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
579 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra).arg(titleWidth);
580 var subtitleCode;
581 var attributesCode;
583 // code for the title container
584 var containerCode = [];
585 var containerHeight = 'titleLabel.height';
586 containerCode.push(titleCode);
587 if (hasSubtitle) {
588 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
589 containerCode.push(subtitleCode);
590 containerHeight += ' + subtitleLabel.height';
591 }
592 if (hasEmblem) {
593 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
594 }
595 if (hasAttributes) {
596 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
597 containerCode.push(attributesCode);
598 containerHeight += ' + attributesRow.height';
599 }
601 if (hasTitleContainer) {
602 // use container
603 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
604 } else {
605 // no container
606 titleSubtitleCode = titleCode;
607 if (hasSubtitle) {
608 titleSubtitleCode += subtitleCode;
609 }
610 if (hasAttributes) {
611 titleSubtitleCode += attributesCode;
612 }
613 }
614 }
616 if (hasHeaderRow) {
617 var rowCode = [mascotCode, titleSubtitleCode];
618 if (mascotShapeCode != '') {
619 rowCode.unshift(mascotShapeCode);
620 }
621 code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
622 } else {
623 code += mascotShapeCode + mascotCode + titleSubtitleCode;
624 }
626 if (hasSummary) {
627 var summaryTopAnchor;
628 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
629 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
630 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
631 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
632 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
633 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
634 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
635 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
636 else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
637 else summaryTopAnchor = 'parent.top';
639 var summaryColor;
640 if (hasBackground) {
641 summaryColor = summaryColorWithBackground;
642 } else {
643 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
644 }
646 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
648 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
649 }
651 var touchdownAnchors;
652 if (hasBackground) {
653 touchdownAnchors = 'fill: backgroundLoader';
654 } else if (touchdownOnArtShape) {
655 touchdownAnchors = 'fill: artShapeHolder';
656 } else {
657 touchdownAnchors = 'fill: root'
658 }
659 code += kTouchdownCode.arg(touchdownAnchors);
661 var implicitHeight = 'implicitHeight: ';
662 if (hasSummary) {
663 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
664 } else if (headerAsOverlay) {
665 implicitHeight += 'artShapeHolder.height;\n';
666 } else if (hasHeaderRow) {
667 implicitHeight += 'row.y + row.height + units.gu(1);\n';
668 } else if (hasMascot) {
669 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
670 } else if (hasTitleContainer) {
671 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
672 } else if (hasAttributes) {
673 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
674 } else if (hasSubtitle) {
675 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
676 } else if (hasTitle) {
677 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
678 } else if (hasArt) {
679 implicitHeight += 'artShapeHolder.height;\n';
680 } else {
681 implicitHeight = '';
682 }
684 // Close the AbstractButton
685 code += implicitHeight + '}\n';
687 return code;
690function createCardComponent(parent, template, components) {
691 var imports = 'import QtQuick 2.4; \n\
692 import Ubuntu.Components 1.3; \n\
693 import Ubuntu.Settings.Components 0.1; \n\
694 import Dash 0.1;\n\
695 import Utils 0.1;\n';
696 var card = cardString(template, components);
697 var code = imports + 'Component {\n' + card + '}\n';
699 try {
700 return Qt.createQmlObject(code, parent, "createCardComponent");
701 } catch (e) {
702 console.error("ERROR: Invalid component created.");
703 console.error("Template:");
704 console.error(JSON.stringify(template));
705 console.error("Components:");
706 console.error(JSON.stringify(components));
707 console.error("Code:");
708 console.error(code);
709 throw e;
710 }