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