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: Image { \n\
86  objectName: "artImage"; \n\
87  source: cardData && cardData["art"] || ""; \n\
88  cache: true; \n\
89  asynchronous: root.asynchronous; \n\
90  fillMode: Image.PreserveAspectCrop; \n\
91  width: %2; \n\
92  height: %3; \n\
93  } \n\
94  } \n\
95  } \n\
96  }\n';
97 
98 var kOverlayLoaderCode = 'Loader { \n\
99  id: overlayLoader; \n\
100  anchors { \n\
101  left: artShapeHolder.left; \n\
102  right: artShapeHolder.right; \n\
103  bottom: artShapeHolder.bottom; \n\
104  } \n\
105  active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
106  asynchronous: root.asynchronous; \n\
107  visible: showHeader && status == Loader.Ready; \n\
108  sourceComponent: ShaderEffect { \n\
109  id: overlay; \n\
110  height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
111  property real luminance: Style.luminance(overlayColor); \n\
112  property color overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
113  property var source: ShaderEffectSource { \n\
114  id: shaderSource; \n\
115  sourceItem: artShapeLoader.item; \n\
116  onVisibleChanged: if (visible) scheduleUpdate(); \n\
117  live: false; \n\
118  sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
119  } \n\
120  vertexShader: " \n\
121  uniform highp mat4 qt_Matrix; \n\
122  attribute highp vec4 qt_Vertex; \n\
123  attribute highp vec2 qt_MultiTexCoord0; \n\
124  varying highp vec2 coord; \n\
125  void main() { \n\
126  coord = qt_MultiTexCoord0; \n\
127  gl_Position = qt_Matrix * qt_Vertex; \n\
128  }"; \n\
129  fragmentShader: " \n\
130  varying highp vec2 coord; \n\
131  uniform sampler2D source; \n\
132  uniform lowp float qt_Opacity; \n\
133  uniform highp vec4 overlayColor; \n\
134  void main() { \n\
135  lowp vec4 tex = texture2D(source, coord); \n\
136  gl_FragColor = vec4(overlayColor.r, overlayColor.g, overlayColor.b, 1) * qt_Opacity * overlayColor.a * tex.a; \n\
137  }"; \n\
138  } \n\
139  }\n';
140 
141 // multiple row version of HeaderRowCode
142 function kHeaderRowCodeGenerator() {
143 var kHeaderRowCodeTemplate = 'Row { \n\
144  id: row; \n\
145  objectName: "outerRow"; \n\
146  property real margins: units.gu(1); \n\
147  spacing: margins; \n\
148  height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
149  anchors { %1 } \n\
150  anchors.right: parent.right; \n\
151  anchors.margins: margins; \n\
152  anchors.rightMargin: 0; \n\
153  data: [ \n\
154  %2 \n\
155  ] \n\
156  }\n';
157  var args = Array.prototype.slice.call(arguments);
158  var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(args.join(',\n'));
159  return code;
160 }
161 
162 // multiple item version of kHeaderContainerCode
163 function kHeaderContainerCodeGenerator() {
164  var headerContainerCodeTemplate = 'Item { \n\
165  id: headerTitleContainer; \n\
166  anchors { %1 } \n\
167  width: parent.width - x; \n\
168  implicitHeight: %2; \n\
169  data: [ \n\
170  %3 \n\
171  ]\n\
172  }\n';
173  var args = Array.prototype.slice.call(arguments);
174  var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(',\n'));
175  return code;
176 }
177 
178 // %1 is used as anchors of mascotShapeLoader
179 var kMascotShapeLoaderCode = 'Loader { \n\
180  id: mascotShapeLoader; \n\
181  objectName: "mascotShapeLoader"; \n\
182  asynchronous: root.asynchronous; \n\
183  active: mascotImage.status === Image.Ready; \n\
184  visible: showHeader && active && status == Loader.Ready; \n\
185  width: units.gu(6); \n\
186  height: units.gu(5.625); \n\
187  sourceComponent: UbuntuShape { image: mascotImage } \n\
188  anchors { %1 } \n\
189  }\n';
190 
191 // %1 is used as anchors of mascotImage
192 // %2 is used as visible of mascotImage
193 var kMascotImageCode = 'Image { \n\
194  id: mascotImage; \n\
195  objectName: "mascotImage"; \n\
196  anchors { %1 } \n\
197  readonly property int maxSize: Math.max(width, height) * 4; \n\
198  source: cardData && cardData["mascot"] || ""; \n\
199  width: units.gu(6); \n\
200  height: units.gu(5.625); \n\
201  sourceSize { width: maxSize; height: maxSize } \n\
202  fillMode: Image.PreserveAspectCrop; \n\
203  horizontalAlignment: Image.AlignHCenter; \n\
204  verticalAlignment: Image.AlignVCenter; \n\
205  visible: %2; \n\
206  }\n';
207 
208 // %1 is used as anchors of titleLabel
209 // %2 is used as color of titleLabel
210 // %3 is used as extra condition for visible of titleLabel
211 var kTitleLabelCode = 'Label { \n\
212  id: titleLabel; \n\
213  objectName: "titleLabel"; \n\
214  anchors { %1 } \n\
215  elide: Text.ElideRight; \n\
216  fontSize: "small"; \n\
217  wrapMode: Text.Wrap; \n\
218  maximumLineCount: 2; \n\
219  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
220  color: %2; \n\
221  visible: showHeader %3; \n\
222  text: root.title; \n\
223  font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
224  horizontalAlignment: root.headerAlignment; \n\
225  }\n';
226 
227 // %1 is used as extra anchors of emblemIcon
228 // %2 is used as color of emblemIcon
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  width: height; \n\
240  height: source != "" ? titleLabel.font.pixelSize : 0; \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  fontSize: "small"; \n\
262  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
263  color: %2; \n\
264  visible: titleLabel.visible && titleLabel.text; \n\
265  text: cardData && cardData["subtitle"] || ""; \n\
266  font.weight: Font.Light; \n\
267  horizontalAlignment: root.headerAlignment; \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 headerAlignment: 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 
321  var hasArt = components["art"] && components["art"]["field"] || false;
322  var hasSummary = components["summary"] || false;
323  var artAndSummary = hasArt && hasSummary;
324  var isHorizontal = template["card-layout"] === "horizontal";
325  var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
326  var hasTitle = components["title"] || false;
327  var hasMascot = components["mascot"] || false;
328  var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
329  var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
330  var hasSubtitle = hasTitle && components["subtitle"] || false;
331  var hasHeaderRow = hasMascot && hasTitle;
332  var hasAttributes = hasTitle && components["attributes"]["field"] || false;
333 
334  if (hasBackground) {
335  code += kBackgroundLoaderCode;
336  }
337 
338  if (hasArt) {
339  code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
340  code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
341 
342  var widthCode, heightCode;
343  var artAnchors;
344  if (isHorizontal) {
345  artAnchors = 'left: parent.left';
346  if (hasMascot || hasTitle) {
347  widthCode = 'height * artShape.aspect'
348  heightCode = 'headerHeight + 2 * units.gu(1)';
349  } else {
350  // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
351  // So we define a "random" height of the image height + 2 gu for the margins
352  widthCode = 'height * artShape.aspect'
353  heightCode = 'units.gu(7.625)';
354  }
355  } else {
356  artAnchors = 'horizontalCenter: parent.horizontalCenter;';
357  widthCode = 'root.width'
358  heightCode = 'width / artShape.aspect';
359  }
360 
361  code += kArtShapeHolderCode.arg(artAnchors).arg(widthCode).arg(heightCode);
362  } else {
363  code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
364  }
365 
366  if (headerAsOverlay) {
367  code += kOverlayLoaderCode;
368  }
369 
370  var headerVerticalAnchors;
371  if (headerAsOverlay) {
372  headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
373  bottomMargin: units.gu(1);\n';
374  } else {
375  if (hasArt) {
376  if (isHorizontal) {
377  headerVerticalAnchors = 'top: artShapeHolder.top; \n\
378  topMargin: units.gu(1);\n';
379  } else {
380  headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
381  topMargin: units.gu(1);\n';
382  }
383  } else {
384  headerVerticalAnchors = 'top: parent.top; \n\
385  topMargin: units.gu(1);\n';
386  }
387  }
388  var headerLeftAnchor;
389  var headerLeftAnchorHasMargin = false;
390  if (isHorizontal && hasArt) {
391  headerLeftAnchor = 'left: artShapeHolder.right; \n\
392  leftMargin: units.gu(1);\n';
393  headerLeftAnchorHasMargin = true;
394  } else {
395  headerLeftAnchor = 'left: parent.left;\n';
396  }
397 
398  var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary;
399 
400  if (hasHeaderRow) {
401  code += 'readonly property int headerHeight: row.height;\n'
402  } else if (hasMascot) {
403  code += 'readonly property int headerHeight: mascotImage.height;\n'
404  } else if (hasAttributes) {
405  if (hasTitle && hasSubtitle) {
406  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
407  } else if (hasTitle) {
408  code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
409  } else {
410  code += 'readonly property int headerHeight: attributesRow.height;\n'
411  }
412  } else if (hasSubtitle) {
413  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
414  } else if (hasTitle) {
415  code += 'readonly property int headerHeight: titleLabel.height;\n'
416  } else {
417  code += 'readonly property int headerHeight: 0;\n'
418  }
419 
420  var mascotShapeCode = '';
421  var mascotCode = '';
422  if (hasMascot) {
423  var useMascotShape = !hasBackground && !headerAsOverlay;
424  var mascotAnchors = '';
425  if (!hasHeaderRow) {
426  mascotAnchors += headerLeftAnchor;
427  mascotAnchors += headerVerticalAnchors;
428  if (!headerLeftAnchorHasMargin) {
429  mascotAnchors += 'leftMargin: units.gu(1);\n'
430  }
431  } else {
432  mascotAnchors = 'verticalCenter: parent.verticalCenter;'
433  }
434 
435  if (useMascotShape) {
436  mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors);
437  }
438 
439  var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
440  mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible);
441  }
442 
443  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")';
444 
445  var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
446  var titleSubtitleCode = '';
447  if (hasTitle) {
448  var titleColor;
449  if (headerAsOverlay) {
450  titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? Theme.palette.normal.baseText : "white")';
451  } else if (hasSummary) {
452  titleColor = 'summary.color';
453  } else if (hasBackground) {
454  titleColor = summaryColorWithBackground;
455  } else {
456  titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
457  }
458 
459  var titleAnchors;
460  var subtitleAnchors;
461  var attributesAnchors;
462  var titleContainerAnchors;
463  var titleRightAnchor;
464 
465  var extraRightAnchor = '';
466  var extraLeftAnchor = '';
467  if (!touchdownOnArtShape) {
468  extraRightAnchor = 'rightMargin: units.gu(1); \n';
469  extraLeftAnchor = 'leftMargin: units.gu(1); \n';
470  } else if (headerAsOverlay && !hasEmblem) {
471  extraRightAnchor = 'rightMargin: units.gu(1); \n';
472  }
473 
474  if (hasMascot) {
475  titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
476  } else {
477  titleContainerAnchors = 'right: parent.right; ';
478  titleContainerAnchors += headerLeftAnchor;
479  titleContainerAnchors += headerVerticalAnchors;
480  if (!headerLeftAnchorHasMargin) {
481  titleContainerAnchors += extraLeftAnchor;
482  }
483  }
484  if (hasEmblem) {
485  titleRightAnchor = 'right: emblemIcon.left; \n\
486  rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
487  } else {
488  titleRightAnchor = 'right: parent.right; \n'
489  titleRightAnchor += extraRightAnchor;
490  }
491 
492  if (hasTitleContainer) {
493  // Using headerTitleContainer
494  titleAnchors = titleRightAnchor;
495  titleAnchors += 'left: parent.left; \n\
496  top: parent.top;';
497  subtitleAnchors = 'right: parent.right; \n\
498  left: parent.left; \n';
499  subtitleAnchors += extraRightAnchor;
500  if (hasSubtitle) {
501  attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
502  subtitleAnchors += 'top: titleLabel.bottom;\n';
503  } else {
504  attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
505  }
506  } else if (hasMascot) {
507  // Using row + titleContainer
508  titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
509  } else {
510  if (headerAsOverlay) {
511  // Using anchors to the overlay
512  titleAnchors = titleRightAnchor;
513  titleAnchors += 'left: parent.left; \n\
514  leftMargin: units.gu(1); \n\
515  top: overlayLoader.top; \n\
516  topMargin: units.gu(1);\n';
517  } else {
518  // Using anchors to the mascot/parent
519  titleAnchors = titleRightAnchor;
520  titleAnchors += headerLeftAnchor;
521  titleAnchors += headerVerticalAnchors;
522  if (!headerLeftAnchorHasMargin) {
523  titleAnchors += extraLeftAnchor;
524  }
525  }
526  subtitleAnchors = 'left: titleLabel.left; \n\
527  leftMargin: titleLabel.leftMargin; \n';
528  subtitleAnchors += extraRightAnchor;
529  if (hasEmblem) {
530  // using container
531  subtitleAnchors += 'right: parent.right; \n';
532  } else {
533  subtitleAnchors += 'right: titleLabel.right; \n';
534  }
535 
536  if (hasSubtitle) {
537  attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
538  subtitleAnchors += 'top: titleLabel.bottom;\n';
539  } else {
540  attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
541  }
542  }
543 
544  // code for different elements
545  var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
546  var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra);
547  var subtitleCode;
548  var attributesCode;
549 
550  // code for the title container
551  var containerCode = [];
552  var containerHeight = 'titleLabel.height';
553  containerCode.push(titleCode);
554  if (hasSubtitle) {
555  subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
556  containerCode.push(subtitleCode);
557  containerHeight += ' + subtitleLabel.height';
558  }
559  if (hasEmblem) {
560  containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
561  }
562  if (hasAttributes) {
563  attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
564  containerCode.push(attributesCode);
565  containerHeight += ' + attributesRow.height';
566  }
567 
568  if (hasTitleContainer) {
569  // use container
570  titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
571  } else {
572  // no container
573  titleSubtitleCode = titleCode;
574  if (hasSubtitle) {
575  titleSubtitleCode += subtitleCode;
576  }
577  if (hasAttributes) {
578  titleSubtitleCode += attributesCode;
579  }
580  }
581  }
582 
583  if (hasHeaderRow) {
584  var rowCode = [mascotCode, titleSubtitleCode];
585  if (mascotShapeCode != '') {
586  rowCode.unshift(mascotShapeCode);
587  }
588  code += kHeaderRowCodeGenerator(headerVerticalAnchors + headerLeftAnchor, rowCode)
589  } else {
590  code += mascotShapeCode + mascotCode + titleSubtitleCode;
591  }
592 
593  if (hasSummary) {
594  var summaryTopAnchor;
595  if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
596  else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
597  else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
598  else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
599  else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
600  else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
601  else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
602  else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
603  else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
604  else summaryTopAnchor = 'parent.top';
605 
606  var summaryColor;
607  if (hasBackground) {
608  summaryColor = summaryColorWithBackground;
609  } else {
610  summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText';
611  }
612 
613  var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
614 
615  code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
616  }
617 
618  var touchdownAnchors;
619  if (hasBackground) {
620  touchdownAnchors = 'fill: backgroundLoader';
621  } else if (touchdownOnArtShape) {
622  touchdownAnchors = 'fill: artShapeHolder';
623  } else {
624  touchdownAnchors = 'fill: root'
625  }
626  code += kTouchdownCode.arg(touchdownAnchors);
627 
628  var implicitHeight = 'implicitHeight: ';
629  if (hasSummary) {
630  implicitHeight += 'summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
631  } else if (hasHeaderRow) {
632  implicitHeight += 'row.y + row.height + units.gu(1);\n';
633  } else if (hasMascot) {
634  implicitHeight += 'mascotImage.y + mascotImage.height;\n';
635  } else if (hasTitleContainer) {
636  implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
637  } else if (hasAttributes) {
638  implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
639  } else if (hasSubtitle) {
640  implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
641  } else if (hasTitle) {
642  implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
643  } else if (hasArt) {
644  implicitHeight += 'artShapeHolder.height;\n';
645  } else {
646  implicitHeight = '';
647  }
648 
649  // Close the AbstractButton
650  code += implicitHeight + '}\n';
651 
652  return code;
653 }
654 
655 function createCardComponent(parent, template, components) {
656  var imports = 'import QtQuick 2.2; \n\
657  import Ubuntu.Components 1.1; \n\
658  import Dash 0.1;\n\
659  import Utils 0.1;\n';
660  var card = cardString(template, components);
661  var code = imports + 'Component {\n' + card + '}\n';
662 
663  try {
664  return Qt.createQmlObject(code, parent, "createCardComponent");
665  } catch (e) {
666  console.error("ERROR: Invalid component created.");
667  console.error("Template:");
668  console.error(JSON.stringify(template));
669  console.error("Components:");
670  console.error(JSON.stringify(components));
671  console.error("Code:");
672  console.error(code);
673  throw e;
674  }
675 }