Unity 8
CardCreator.js
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  */
16 
17 .pragma library
18 
19 var 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';
52 
53 // %1 is used as anchors of artShapeHolder
54 // %2 is used as image width
55 // %3 is used as image height
56 var 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';
98 
99 var 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';
141 
142 // multiple row version of HeaderRowCode
143 function kHeaderRowCodeGenerator() {
144 var 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;
161 }
162 
163 // multiple item version of kHeaderContainerCode
164 function 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;
177 }
178 
179 // %1 is used as anchors of mascotShapeLoader
180 var 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';
191 
192 // %1 is used as anchors of mascotImage
193 // %2 is used as visible of mascotImage
194 var 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';
205 
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
209 var 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';
224 
225 // %1 is used as extra anchors of emblemIcon
226 // %2 is used as color of emblemIcon
227 // FIXME The width code is a
228 // Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
229 var kEmblemIconCode = 'Icon { \n\
230  id: emblemIcon; \n\
231  objectName: "emblemIcon"; \n\
232  anchors { \n\
233  bottom: titleLabel.baseline; \n\
234  right: parent.right; \n\
235  %1
236  } \n\
237  source: cardData && cardData["emblem"] || ""; \n\
238  color: %2; \n\
239  height: source != "" ? titleLabel.font.pixelSize : 0; \n\
240  width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
241  }\n';
242 
243 // %1 is used as anchors of touchdown effect
244 var kTouchdownCode = 'UbuntuShape { \n\
245  id: touchdown; \n\
246  objectName: "touchdown"; \n\
247  anchors { %1 } \n\
248  visible: root.pressed; \n\
249  radius: "medium"; \n\
250  borderSource: "radius_pressed.sci" \n\
251  }\n';
252 
253 // %1 is used as anchors of subtitleLabel
254 // %2 is used as color of subtitleLabel
255 var kSubtitleLabelCode = 'Label { \n\
256  id: subtitleLabel; \n\
257  objectName: "subtitleLabel"; \n\
258  anchors { %1 } \n\
259  anchors.topMargin: units.dp(2); \n\
260  elide: Text.ElideRight; \n\
261  maximumLineCount: 1; \n\
262  fontSize: "x-small"; \n\
263  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
264  color: %2; \n\
265  visible: titleLabel.visible && titleLabel.text; \n\
266  text: cardData && cardData["subtitle"] || ""; \n\
267  font.weight: Font.Light; \n\
268  }\n';
269 
270 // %1 is used as anchors of attributesRow
271 // %2 is used as color of attributesRow
272 var kAttributesRowCode = 'CardAttributes { \n\
273  id: attributesRow; \n\
274  objectName: "attributesRow"; \n\
275  anchors { %1 } \n\
276  color: %2; \n\
277  fontScale: root.fontScale; \n\
278  model: cardData && cardData["attributes"]; \n\
279  }\n';
280 
281 // %1 is used as top anchor of summary
282 // %2 is used as topMargin anchor of summary
283 // %3 is used as color of summary
284 var kSummaryLabelCode = 'Label { \n\
285  id: summary; \n\
286  objectName: "summaryLabel"; \n\
287  anchors { \n\
288  top: %1; \n\
289  left: parent.left; \n\
290  right: parent.right; \n\
291  margins: units.gu(1); \n\
292  topMargin: %2; \n\
293  } \n\
294  wrapMode: Text.Wrap; \n\
295  maximumLineCount: 5; \n\
296  elide: Text.ElideRight; \n\
297  text: cardData && cardData["summary"] || ""; \n\
298  height: text ? implicitHeight : 0; \n\
299  fontSize: "small"; \n\
300  color: %3; \n\
301  }\n';
302 
303 function cardString(template, components) {
304  var code;
305  code = 'AbstractButton { \n\
306  id: root; \n\
307  property var template; \n\
308  property var components; \n\
309  property var cardData; \n\
310  property var artShapeBorderSource: undefined; \n\
311  property real fontScale: 1.0; \n\
312  property var scopeStyle: null; \n\
313  property int titleAlignment: Text.AlignLeft; \n\
314  property int fixedHeaderHeight: -1; \n\
315  property size fixedArtShapeSize: Qt.size(-1, -1); \n\
316  readonly property string title: cardData && cardData["title"] || ""; \n\
317  property bool asynchronous: true; \n\
318  property bool showHeader: true; \n\
319  implicitWidth: childrenRect.width; \n\
320  enabled: root.template == null ? true : (root.template["non-interactive"] !== undefined ? !root.template["non-interactive"] : true); \n\
321  \n';
322 
323  var hasArt = components["art"] && components["art"]["field"] || false;
324  var hasSummary = components["summary"] || false;
325  var artAndSummary = hasArt && hasSummary;
326  var isHorizontal = template["card-layout"] === "horizontal";
327  var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
328  (hasSummary && (template["card-background"] || components["background"]));
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"]["field"] || false;
336 
337  if (hasBackground) {
338  code += kBackgroundLoaderCode;
339  }
340 
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';
344 
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  }
363 
364  code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
365  } else {
366  code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
367  }
368 
369  if (headerAsOverlay) {
370  code += kOverlayLoaderCode;
371  }
372 
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  }
400 
401  var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
402 
403  if (hasHeaderRow) {
404  code += 'readonly property int headerHeight: row.height;\n'
405  } else if (hasMascot) {
406  code += 'readonly property int headerHeight: mascotImage.height;\n'
407  } else if (hasAttributes) {
408  if (hasTitle && hasSubtitle) {
409  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
410  } else if (hasTitle) {
411  code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
412  } else {
413  code += 'readonly property int headerHeight: attributesRow.height;\n'
414  }
415  } else if (hasSubtitle) {
416  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
417  } else if (hasTitle) {
418  code += 'readonly property int headerHeight: titleLabel.height;\n'
419  } else {
420  code += 'readonly property int headerHeight: 0;\n'
421  }
422 
423  var mascotShapeCode = '';
424  var mascotCode = '';
425  if (hasMascot) {
426  var useMascotShape = !hasBackground && !headerAsOverlay;
427  var mascotAnchors = '';
428  if (!hasHeaderRow) {
429  mascotAnchors += headerLeftAnchor;
430  mascotAnchors += headerVerticalAnchors;
431  if (!headerLeftAnchorHasMargin) {
432  mascotAnchors += 'leftMargin: units.gu(1);\n'
433  }
434  } else {
435  mascotAnchors = 'verticalCenter: parent.verticalCenter;'
436  }
437 
438  if (useMascotShape) {
439  mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
440  }
441 
442  var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
443  mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
444  }
445 
446  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")';
447 
448  var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
449  var titleSubtitleCode = '';
450  if (hasTitle) {
451  var titleColor;
452  if (headerAsOverlay) {
453  titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
454  } else if (hasSummary) {
455  titleColor = 'summary.color';
456  } else if (hasBackground) {
457  titleColor = summaryColorWithBackground;
458  } else {
459  titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
460  }
461 
462  var titleAnchors;
463  var subtitleAnchors;
464  var attributesAnchors;
465  var titleContainerAnchors;
466  var titleRightAnchor;
467 
468  var extraRightAnchor = '';
469  var extraLeftAnchor = '';
470  if (!touchdownOnArtShape) {
471  extraRightAnchor = 'rightMargin: units.gu(1); \n';
472  extraLeftAnchor = 'leftMargin: units.gu(1); \n';
473  } else if (headerAsOverlay && !hasEmblem) {
474  extraRightAnchor = 'rightMargin: units.gu(1); \n';
475  }
476 
477  if (hasMascot) {
478  titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
479  } else {
480  titleContainerAnchors = 'right: parent.right; ';
481  titleContainerAnchors += headerLeftAnchor;
482  titleContainerAnchors += headerVerticalAnchors;
483  if (!headerLeftAnchorHasMargin) {
484  titleContainerAnchors += extraLeftAnchor;
485  }
486  }
487  if (hasEmblem) {
488  titleRightAnchor = 'right: emblemIcon.left; \n\
489  rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
490  } else {
491  titleRightAnchor = 'right: parent.right; \n'
492  titleRightAnchor += extraRightAnchor;
493  }
494 
495  if (hasTitleContainer) {
496  // Using headerTitleContainer
497  titleAnchors = titleRightAnchor;
498  titleAnchors += 'left: parent.left; \n\
499  top: parent.top;';
500  subtitleAnchors = 'right: parent.right; \n\
501  left: parent.left; \n';
502  subtitleAnchors += extraRightAnchor;
503  if (hasSubtitle) {
504  attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
505  subtitleAnchors += 'top: titleLabel.bottom;\n';
506  } else {
507  attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
508  }
509  } else if (hasMascot) {
510  // Using row + titleContainer
511  titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
512  } else {
513  if (headerAsOverlay) {
514  // Using anchors to the overlay
515  titleAnchors = titleRightAnchor;
516  titleAnchors += 'left: parent.left; \n\
517  leftMargin: units.gu(1); \n\
518  top: overlayLoader.top; \n\
519  topMargin: units.gu(1);\n';
520  } else {
521  // Using anchors to the mascot/parent
522  titleAnchors = titleRightAnchor;
523  titleAnchors += headerLeftAnchor;
524  titleAnchors += headerVerticalAnchors;
525  if (!headerLeftAnchorHasMargin) {
526  titleAnchors += extraLeftAnchor;
527  }
528  }
529  subtitleAnchors = 'left: titleLabel.left; \n\
530  leftMargin: titleLabel.leftMargin; \n';
531  subtitleAnchors += extraRightAnchor;
532  if (hasEmblem) {
533  // using container
534  subtitleAnchors += 'right: parent.right; \n';
535  } else {
536  subtitleAnchors += 'right: titleLabel.right; \n';
537  }
538 
539  if (hasSubtitle) {
540  attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
541  subtitleAnchors += 'top: titleLabel.bottom;\n';
542  } else {
543  attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
544  }
545  }
546 
547  // code for different elements
548  var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
549  var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
550  var subtitleCode;
551  var attributesCode;
552 
553  // code for the title container
554  var containerCode = [];
555  var containerHeight = 'titleLabel.height';
556  containerCode.push(titleCode);
557  if (hasSubtitle) {
558  subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
559  containerCode.push(subtitleCode);
560  containerHeight += ' + subtitleLabel.height';
561  }
562  if (hasEmblem) {
563  containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
564  }
565  if (hasAttributes) {
566  attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
567  containerCode.push(attributesCode);
568  containerHeight += ' + attributesRow.height';
569  }
570 
571  if (hasTitleContainer) {
572  // use container
573  titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
574  } else {
575  // no container
576  titleSubtitleCode = titleCode;
577  if (hasSubtitle) {
578  titleSubtitleCode += subtitleCode;
579  }
580  if (hasAttributes) {
581  titleSubtitleCode += attributesCode;
582  }
583  }
584  }
585 
586  if (hasHeaderRow) {
587  var rowCode = [mascotCode, titleSubtitleCode];
588  if (mascotShapeCode != '') {
589  rowCode.unshift(mascotShapeCode);
590  }
591  code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
592  } else {
593  code += mascotShapeCode + mascotCode + titleSubtitleCode;
594  }
595 
596  if (hasSummary) {
597  var summaryTopAnchor;
598  if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
599  else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
600  else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
601  else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
602  else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
603  else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
604  else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
605  else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
606  else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
607  else summaryTopAnchor = 'parent.top';
608 
609  var summaryColor;
610  if (hasBackground) {
611  summaryColor = summaryColorWithBackground;
612  } else {
613  summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
614  }
615 
616  var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
617 
618  code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
619  }
620 
621  var touchdownAnchors;
622  if (hasBackground) {
623  touchdownAnchors = 'fill: backgroundLoader';
624  } else if (touchdownOnArtShape) {
625  touchdownAnchors = 'fill: artShapeHolder';
626  } else {
627  touchdownAnchors = 'fill: root'
628  }
629  code += kTouchdownCode.arg(touchdownAnchors);
630 
631  var implicitHeight = 'implicitHeight: ';
632  if (hasSummary) {
633  implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
634  } else if (hasHeaderRow) {
635  implicitHeight += 'row.y + row.height + units.gu(1);\n';
636  } else if (hasMascot) {
637  implicitHeight += 'mascotImage.y + mascotImage.height;\n';
638  } else if (hasTitleContainer) {
639  implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
640  } else if (hasAttributes) {
641  implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
642  } else if (hasSubtitle) {
643  implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
644  } else if (hasTitle) {
645  implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
646  } else if (hasArt) {
647  implicitHeight += 'artShapeHolder.height;\n';
648  } else {
649  implicitHeight = '';
650  }
651 
652  // Close the AbstractButton
653  code += implicitHeight + '}\n';
654 
655  return code;
656 }
657 
658 function createCardComponent(parent, template, components) {
659  var imports = 'import QtQuick 2.2; \n\
660  import Ubuntu.Components 1.1; \n\
661  import Ubuntu.Settings.Components 0.1; \n\
662  import Dash 0.1;\n\
663  import Utils 0.1;\n';
664  var card = cardString(template, components);
665  var code = imports + 'Component {\n' + card + '}\n';
666 
667  try {
668  return Qt.createQmlObject(code, parent, "createCardComponent");
669  } catch (e) {
670  console.error("ERROR: Invalid component created.");
671  console.error("Template:");
672  console.error(JSON.stringify(template));
673  console.error("Components:");
674  console.error(JSON.stringify(components));
675  console.error("Code:");
676  console.error(code);
677  throw e;
678  }
679 }