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