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