Unity 8
 All Classes Functions
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  image: CroppedImageMinimumSourceSize { \n\
86  objectName: "artImage"; \n\
87  property bool doLoadSource: !NetworkingStatus.limitedBandwith; \n\
88  source: { if (root.visible) doLoadSource = true; return doLoadSource && cardData && cardData["art"] || ""; } \n\
89  cache: true; \n\
90  asynchronous: root.asynchronous; \n\
91  visible: false; \n\
92  width: %2; \n\
93  height: %3; \n\
94  } \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.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 } \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  property bool doLoadSource: !NetworkingStatus.limitedBandwith; \n\
199  source: { if (root.visible) doLoadSource = true; return doLoadSource && cardData && cardData["mascot"] || ""; } \n\
200  width: units.gu(6); \n\
201  height: units.gu(5.625); \n\
202  horizontalAlignment: Image.AlignHCenter; \n\
203  verticalAlignment: Image.AlignVCenter; \n\
204  visible: %2; \n\
205  }\n';
206 
207 // %1 is used as anchors of titleLabel
208 // %2 is used as color of titleLabel
209 // %3 is used as extra condition for visible of titleLabel
210 var kTitleLabelCode = 'Label { \n\
211  id: titleLabel; \n\
212  objectName: "titleLabel"; \n\
213  anchors { %1 } \n\
214  elide: Text.ElideRight; \n\
215  fontSize: "small"; \n\
216  wrapMode: Text.Wrap; \n\
217  maximumLineCount: 2; \n\
218  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
219  color: %2; \n\
220  visible: showHeader %3; \n\
221  text: root.title; \n\
222  font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
223  horizontalAlignment: root.titleAlignment; \n\
224  }\n';
225 
226 // %1 is used as extra anchors of emblemIcon
227 // %2 is used as color of emblemIcon
228 var kEmblemIconCode = 'StatusIcon { \n\
229  id: emblemIcon; \n\
230  objectName: "emblemIcon"; \n\
231  anchors { \n\
232  bottom: titleLabel.baseline; \n\
233  right: parent.right; \n\
234  %1
235  } \n\
236  source: cardData && cardData["emblem"] || ""; \n\
237  color: %2; \n\
238  height: source != "" ? titleLabel.font.pixelSize : 0; \n\
239  }\n';
240 
241 // %1 is used as anchors of touchdown effect
242 var kTouchdownCode = 'UbuntuShape { \n\
243  id: touchdown; \n\
244  objectName: "touchdown"; \n\
245  anchors { %1 } \n\
246  visible: root.pressed; \n\
247  radius: "medium"; \n\
248  borderSource: "radius_pressed.sci" \n\
249  }\n';
250 
251 // %1 is used as anchors of subtitleLabel
252 // %2 is used as color of subtitleLabel
253 var kSubtitleLabelCode = 'Label { \n\
254  id: subtitleLabel; \n\
255  objectName: "subtitleLabel"; \n\
256  anchors { %1 } \n\
257  anchors.topMargin: units.dp(2); \n\
258  elide: Text.ElideRight; \n\
259  fontSize: "x-small"; \n\
260  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
261  color: %2; \n\
262  visible: titleLabel.visible && titleLabel.text; \n\
263  text: cardData && cardData["subtitle"] || ""; \n\
264  font.weight: Font.Light; \n\
265  }\n';
266 
267 // %1 is used as anchors of attributesRow
268 // %2 is used as color of attributesRow
269 var kAttributesRowCode = 'CardAttributes { \n\
270  id: attributesRow; \n\
271  objectName: "attributesRow"; \n\
272  anchors { %1 } \n\
273  color: %2; \n\
274  fontScale: root.fontScale; \n\
275  model: cardData && cardData["attributes"]; \n\
276  }\n';
277 
278 // %1 is used as top anchor of summary
279 // %2 is used as topMargin anchor of summary
280 // %3 is used as color of summary
281 var kSummaryLabelCode = 'Label { \n\
282  id: summary; \n\
283  objectName: "summaryLabel"; \n\
284  anchors { \n\
285  top: %1; \n\
286  left: parent.left; \n\
287  right: parent.right; \n\
288  margins: units.gu(1); \n\
289  topMargin: %2; \n\
290  } \n\
291  wrapMode: Text.Wrap; \n\
292  maximumLineCount: 5; \n\
293  elide: Text.ElideRight; \n\
294  text: cardData && cardData["summary"] || ""; \n\
295  height: text ? implicitHeight : 0; \n\
296  fontSize: "small"; \n\
297  color: %3; \n\
298  }\n';
299 
300 function cardString(template, components) {
301  var code;
302  code = 'AbstractButton { \n\
303  id: root; \n\
304  property var template; \n\
305  property var components; \n\
306  property var cardData; \n\
307  property var artShapeBorderSource: undefined; \n\
308  property real fontScale: 1.0; \n\
309  property var scopeStyle: null; \n\
310  property int titleAlignment: Text.AlignLeft; \n\
311  property int fixedHeaderHeight: -1; \n\
312  property size fixedArtShapeSize: Qt.size(-1, -1); \n\
313  readonly property string title: cardData && cardData["title"] || ""; \n\
314  property bool asynchronous: true; \n\
315  property bool showHeader: true; \n\
316  implicitWidth: childrenRect.width; \n';
317 
318  var hasArt = components["art"] && components["art"]["field"] || false;
319  var hasSummary = components["summary"] || false;
320  var artAndSummary = hasArt && hasSummary;
321  var isHorizontal = template["card-layout"] === "horizontal";
322  var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
323  var hasTitle = components["title"] || false;
324  var hasMascot = components["mascot"] || false;
325  var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
326  var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
327  var hasSubtitle = hasTitle && components["subtitle"] || false;
328  var hasHeaderRow = hasMascot && hasTitle;
329  var hasAttributes = hasTitle && components["attributes"]["field"] || false;
330 
331  if (hasBackground) {
332  code += kBackgroundLoaderCode;
333  }
334 
335  if (hasArt) {
336  code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
337  code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
338 
339  var widthCode, heightCode;
340  var artAnchors;
341  if (isHorizontal) {
342  artAnchors = 'left: parent.left';
343  if (hasMascot || hasTitle) {
344  widthCode = 'height * artShape.aspect'
345  heightCode = 'headerHeight + 2 * units.gu(1)';
346  } else {
347  // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
348  // So we define a "random" height of the image height + 2 gu for the margins
349  widthCode = 'height * artShape.aspect'
350  heightCode = 'units.gu(7.625)';
351  }
352  } else {
353  artAnchors = 'horizontalCenter: parent.horizontalCenter;';
354  widthCode = 'root.width'
355  heightCode = 'width / artShape.aspect';
356  }
357 
358  code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
359  } else {
360  code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
361  }
362 
363  if (headerAsOverlay) {
364  code += kOverlayLoaderCode;
365  }
366 
367  var headerVerticalAnchors;
368  if (headerAsOverlay) {
369  headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
370  bottomMargin: units.gu(1);\n';
371  } else {
372  if (hasArt) {
373  if (isHorizontal) {
374  headerVerticalAnchors = 'top: artShapeHolder.top; \n\
375  topMargin: units.gu(1);\n';
376  } else {
377  headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
378  topMargin: units.gu(1);\n';
379  }
380  } else {
381  headerVerticalAnchors = 'top: parent.top; \n\
382  topMargin: units.gu(1);\n';
383  }
384  }
385  var headerLeftAnchor;
386  var headerLeftAnchorHasMargin = false;
387  if (isHorizontal && hasArt) {
388  headerLeftAnchor = 'left: artShapeHolder.right; \n\
389  leftMargin: units.gu(1);\n';
390  headerLeftAnchorHasMargin = true;
391  } else {
392  headerLeftAnchor = 'left: parent.left;\n';
393  }
394 
395  var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
396 
397  if (hasHeaderRow) {
398  code += 'readonly property int headerHeight: row.height;\n'
399  } else if (hasMascot) {
400  code += 'readonly property int headerHeight: mascotImage.height;\n'
401  } else if (hasAttributes) {
402  if (hasTitle && hasSubtitle) {
403  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
404  } else if (hasTitle) {
405  code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
406  } else {
407  code += 'readonly property int headerHeight: attributesRow.height;\n'
408  }
409  } else if (hasSubtitle) {
410  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
411  } else if (hasTitle) {
412  code += 'readonly property int headerHeight: titleLabel.height;\n'
413  } else {
414  code += 'readonly property int headerHeight: 0;\n'
415  }
416 
417  var mascotShapeCode = '';
418  var mascotCode = '';
419  if (hasMascot) {
420  var useMascotShape = !hasBackground && !headerAsOverlay;
421  var mascotAnchors = '';
422  if (!hasHeaderRow) {
423  mascotAnchors += headerLeftAnchor;
424  mascotAnchors += headerVerticalAnchors;
425  if (!headerLeftAnchorHasMargin) {
426  mascotAnchors += 'leftMargin: units.gu(1);\n'
427  }
428  } else {
429  mascotAnchors = 'verticalCenter: parent.verticalCenter;'
430  }
431 
432  if (useMascotShape) {
433  mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
434  }
435 
436  var mascotImageVisible = useMascotShape ? 'false' : 'showHeader && resized';
437  mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
438  }
439 
440  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")';
441 
442  var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
443  var titleSubtitleCode = '';
444  if (hasTitle) {
445  var titleColor;
446  if (headerAsOverlay) {
447  titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
448  } else if (hasSummary) {
449  titleColor = 'summary.color';
450  } else if (hasBackground) {
451  titleColor = summaryColorWithBackground;
452  } else {
453  titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
454  }
455 
456  var titleAnchors;
457  var subtitleAnchors;
458  var attributesAnchors;
459  var titleContainerAnchors;
460  var titleRightAnchor;
461 
462  var extraRightAnchor = '';
463  var extraLeftAnchor = '';
464  if (!touchdownOnArtShape) {
465  extraRightAnchor = 'rightMargin: units.gu(1); \n';
466  extraLeftAnchor = 'leftMargin: units.gu(1); \n';
467  } else if (headerAsOverlay && !hasEmblem) {
468  extraRightAnchor = 'rightMargin: units.gu(1); \n';
469  }
470 
471  if (hasMascot) {
472  titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
473  } else {
474  titleContainerAnchors = 'right: parent.right; ';
475  titleContainerAnchors += headerLeftAnchor;
476  titleContainerAnchors += headerVerticalAnchors;
477  if (!headerLeftAnchorHasMargin) {
478  titleContainerAnchors += extraLeftAnchor;
479  }
480  }
481  if (hasEmblem) {
482  titleRightAnchor = 'right: emblemIcon.left; \n\
483  rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
484  } else {
485  titleRightAnchor = 'right: parent.right; \n'
486  titleRightAnchor += extraRightAnchor;
487  }
488 
489  if (hasTitleContainer) {
490  // Using headerTitleContainer
491  titleAnchors = titleRightAnchor;
492  titleAnchors += 'left: parent.left; \n\
493  top: parent.top;';
494  subtitleAnchors = 'right: parent.right; \n\
495  left: parent.left; \n';
496  subtitleAnchors += extraRightAnchor;
497  if (hasSubtitle) {
498  attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
499  subtitleAnchors += 'top: titleLabel.bottom;\n';
500  } else {
501  attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
502  }
503  } else if (hasMascot) {
504  // Using row + titleContainer
505  titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
506  } else {
507  if (headerAsOverlay) {
508  // Using anchors to the overlay
509  titleAnchors = titleRightAnchor;
510  titleAnchors += 'left: parent.left; \n\
511  leftMargin: units.gu(1); \n\
512  top: overlayLoader.top; \n\
513  topMargin: units.gu(1);\n';
514  } else {
515  // Using anchors to the mascot/parent
516  titleAnchors = titleRightAnchor;
517  titleAnchors += headerLeftAnchor;
518  titleAnchors += headerVerticalAnchors;
519  if (!headerLeftAnchorHasMargin) {
520  titleAnchors += extraLeftAnchor;
521  }
522  }
523  subtitleAnchors = 'left: titleLabel.left; \n\
524  leftMargin: titleLabel.leftMargin; \n';
525  subtitleAnchors += extraRightAnchor;
526  if (hasEmblem) {
527  // using container
528  subtitleAnchors += 'right: parent.right; \n';
529  } else {
530  subtitleAnchors += 'right: titleLabel.right; \n';
531  }
532 
533  if (hasSubtitle) {
534  attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
535  subtitleAnchors += 'top: titleLabel.bottom;\n';
536  } else {
537  attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
538  }
539  }
540 
541  // code for different elements
542  var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
543  var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
544  var subtitleCode;
545  var attributesCode;
546 
547  // code for the title container
548  var containerCode = [];
549  var containerHeight = 'titleLabel.height';
550  containerCode.push(titleCode);
551  if (hasSubtitle) {
552  subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
553  containerCode.push(subtitleCode);
554  containerHeight += ' + subtitleLabel.height';
555  }
556  if (hasEmblem) {
557  containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
558  }
559  if (hasAttributes) {
560  attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
561  containerCode.push(attributesCode);
562  containerHeight += ' + attributesRow.height';
563  }
564 
565  if (hasTitleContainer) {
566  // use container
567  titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
568  } else {
569  // no container
570  titleSubtitleCode = titleCode;
571  if (hasSubtitle) {
572  titleSubtitleCode += subtitleCode;
573  }
574  if (hasAttributes) {
575  titleSubtitleCode += attributesCode;
576  }
577  }
578  }
579 
580  if (hasHeaderRow) {
581  var rowCode = [mascotCode, titleSubtitleCode];
582  if (mascotShapeCode != '') {
583  rowCode.unshift(mascotShapeCode);
584  }
585  code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
586  } else {
587  code += mascotShapeCode + mascotCode + titleSubtitleCode;
588  }
589 
590  if (hasSummary) {
591  var summaryTopAnchor;
592  if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
593  else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
594  else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
595  else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
596  else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
597  else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
598  else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
599  else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
600  else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
601  else summaryTopAnchor = 'parent.top';
602 
603  var summaryColor;
604  if (hasBackground) {
605  summaryColor = summaryColorWithBackground;
606  } else {
607  summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
608  }
609 
610  var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
611 
612  code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
613  }
614 
615  var touchdownAnchors;
616  if (hasBackground) {
617  touchdownAnchors = 'fill: backgroundLoader';
618  } else if (touchdownOnArtShape) {
619  touchdownAnchors = 'fill: artShapeHolder';
620  } else {
621  touchdownAnchors = 'fill: root'
622  }
623  code += kTouchdownCode.arg(touchdownAnchors);
624 
625  var implicitHeight = 'implicitHeight: ';
626  if (hasSummary) {
627  implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
628  } else if (hasHeaderRow) {
629  implicitHeight += 'row.y + row.height + units.gu(1);\n';
630  } else if (hasMascot) {
631  implicitHeight += 'mascotImage.y + mascotImage.height;\n';
632  } else if (hasTitleContainer) {
633  implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
634  } else if (hasAttributes) {
635  implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
636  } else if (hasSubtitle) {
637  implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
638  } else if (hasTitle) {
639  implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
640  } else if (hasArt) {
641  implicitHeight += 'artShapeHolder.height;\n';
642  } else {
643  implicitHeight = '';
644  }
645 
646  // Close the AbstractButton
647  code += implicitHeight + '}\n';
648 
649  return code;
650 }
651 
652 function createCardComponent(parent, template, components) {
653  var imports = 'import QtQuick 2.2; \n\
654  import Ubuntu.Components 1.1; \n\
655  import Ubuntu.Settings.Components 0.1; \n\
656  import Ubuntu.Connectivity 1.0; \n\
657  import Dash 0.1;\n\
658  import Utils 0.1;\n';
659  var card = cardString(template, components);
660  var code = imports + 'Component {\n' + card + '}\n';
661 
662  try {
663  return Qt.createQmlObject(code, parent, "createCardComponent");
664  } catch (e) {
665  console.error("ERROR: Invalid component created.");
666  console.error("Template:");
667  console.error(JSON.stringify(template));
668  console.error("Components:");
669  console.error(JSON.stringify(components));
670  console.error("Code:");
671  console.error(code);
672  throw e;
673  }
674 }