Unity 8
 All Classes Functions Properties
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: 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; \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  readonly property bool aspectSmallerThanImageAspect: aspect < image.aspect; \n\
75  Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
76  onAspectSmallerThanImageAspectChanged: updateWidthHeightBindings(); \n\
77  Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
78  function updateWidthHeightBindings() { \n\
79  if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
80  width = root.fixedArtShapeSize.width; \n\
81  height = root.fixedArtShapeSize.height; \n\
82  } else if (aspectSmallerThanImageAspect) { \n\
83  width = Qt.binding(function() { return !visible ? 0 : image.width }); \n\
84  height = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.height : width / image.aspect }); \n\
85  } else { \n\
86  width = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.width : height * image.aspect }); \n\
87  height = Qt.binding(function() { return !visible ? 0 : image.height }); \n\
88  } \n\
89  } \n\
90  image: Image { \n\
91  objectName: "artImage"; \n\
92  source: cardData && cardData["art"] || ""; \n\
93  cache: true; \n\
94  asynchronous: root.asynchronous; \n\
95  fillMode: components && components["art"]["fill-mode"] === "fit" ? Image.PreserveAspectFit: Image.PreserveAspectCrop; \n\
96  readonly property real aspect: implicitWidth / implicitHeight; \n\
97  width: %2; \n\
98  height: %3; \n\
99  } \n\
100  } \n\
101  } \n\
102  }\n';
103 
104 var kOverlayLoaderCode = 'Loader { \n\
105  id: overlayLoader; \n\
106  anchors { \n\
107  left: artShapeHolder.left; \n\
108  right: artShapeHolder.right; \n\
109  bottom: artShapeHolder.bottom; \n\
110  } \n\
111  active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
112  asynchronous: root.asynchronous; \n\
113  visible: showHeader && status == Loader.Ready; \n\
114  sourceComponent: ShaderEffect { \n\
115  id: overlay; \n\
116  height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
117  opacity: 0.6; \n\
118  property var source: ShaderEffectSource { \n\
119  id: shaderSource; \n\
120  sourceItem: artShapeLoader.item; \n\
121  onVisibleChanged: if (visible) scheduleUpdate(); \n\
122  live: false; \n\
123  sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
124  } \n\
125  vertexShader: " \n\
126  uniform highp mat4 qt_Matrix; \n\
127  attribute highp vec4 qt_Vertex; \n\
128  attribute highp vec2 qt_MultiTexCoord0; \n\
129  varying highp vec2 coord; \n\
130  void main() { \n\
131  coord = qt_MultiTexCoord0; \n\
132  gl_Position = qt_Matrix * qt_Vertex; \n\
133  }"; \n\
134  fragmentShader: " \n\
135  varying highp vec2 coord; \n\
136  uniform sampler2D source; \n\
137  uniform lowp float qt_Opacity; \n\
138  void main() { \n\
139  lowp vec4 tex = texture2D(source, coord); \n\
140  gl_FragColor = vec4(0, 0, 0, tex.a) * qt_Opacity; \n\
141  }"; \n\
142  } \n\
143  }\n';
144 
145 // %1 is used as anchors of row
146 // %2 is used as first child of the row
147 // %3 is used as second child of the row
148 var kHeaderRow2Code = 'Row { \n\
149  id: row; \n\
150  objectName: "outerRow"; \n\
151  property real margins: units.gu(1); \n\
152  spacing: margins; \n\
153  height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
154  anchors { %1 } \n\
155  anchors.right: parent.right; \n\
156  anchors.margins: margins;\n\
157  data: [ %2\n\
158  ,\n\
159  %3 \n\
160  ] \n\
161  }\n';
162 
163 // %1 is used as anchors of row
164 // %2 is used as first child of the row
165 // %3 is used as second child of the row
166 // %4 is used as third child of the row
167 var kHeaderRow3Code = 'Row { \n\
168  id: row; \n\
169  objectName: "outerRow"; \n\
170  property real margins: units.gu(1); \n\
171  spacing: margins; \n\
172  height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
173  anchors { %1 } \n\
174  anchors.right: parent.right; \n\
175  anchors.margins: margins;\n\
176  data: [ %2\n\
177  ,\n\
178  %3 \n\
179  ,\n\
180  %4 \n\
181  ] \n\
182  }\n';
183 
184 // %1 is used as first child of the column
185 // %2 is used as second child of the column
186 var kHeaderColumnCode = 'Column { \n\
187  anchors.verticalCenter: parent.verticalCenter; \n\
188  spacing: units.dp(2); \n\
189  width: parent.width - x;\n\
190  data: [ %1\n\
191  ,\n\
192  %2 \n\
193  ] \n\
194  }\n';
195 
196 // %1 is used as anchors of mascotShapeLoader
197 var kMascotShapeLoaderCode = 'Loader { \n\
198  id: mascotShapeLoader; \n\
199  objectName: "mascotShapeLoader"; \n\
200  asynchronous: root.asynchronous; \n\
201  active: mascotImage.status === Image.Ready; \n\
202  visible: showHeader && active && status == Loader.Ready; \n\
203  width: units.gu(6); \n\
204  height: units.gu(5.625); \n\
205  sourceComponent: UbuntuShape { image: mascotImage } \n\
206  anchors { %1 } \n\
207  }\n';
208 
209 // %1 is used as anchors of mascotImage
210 // %2 is used as visible of mascotImage
211 var kMascotImageCode = 'Image { \n\
212  id: mascotImage; \n\
213  objectName: "mascotImage"; \n\
214  anchors { %1 } \n\
215  readonly property int maxSize: Math.max(width, height) * 4; \n\
216  source: cardData && cardData["mascot"]; \n\
217  width: units.gu(6); \n\
218  height: units.gu(5.625); \n\
219  sourceSize { width: maxSize; height: maxSize } \n\
220  fillMode: Image.PreserveAspectCrop; \n\
221  horizontalAlignment: Image.AlignHCenter; \n\
222  verticalAlignment: Image.AlignVCenter; \n\
223  visible: %2; \n\
224  }\n';
225 
226 // %1 is used as anchors of titleLabel
227 // %1 is used as color of titleLabel
228 // %3 is used as extra condition for visible of titleLabel
229 var kTitleLabelCode = 'Label { \n\
230  id: titleLabel; \n\
231  objectName: "titleLabel"; \n\
232  anchors { %1 } \n\
233  elide: Text.ElideRight; \n\
234  fontSize: "small"; \n\
235  wrapMode: Text.Wrap; \n\
236  maximumLineCount: 2; \n\
237  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
238  color: %2; \n\
239  visible: showHeader %3; \n\
240  text: root.title; \n\
241  font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
242  horizontalAlignment: root.headerAlignment; \n\
243  }\n';
244 
245 // %1 is used as anchors of subtitleLabel
246 // %2 is used as color of subtitleLabel
247 var kSubtitleLabelCode = 'Label { \n\
248  id: subtitleLabel; \n\
249  objectName: "subtitleLabel"; \n\
250  anchors { %1 } \n\
251  elide: Text.ElideRight; \n\
252  fontSize: "small"; \n\
253  font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
254  color: %2; \n\
255  visible: titleLabel.visible && titleLabel.text; \n\
256  text: cardData && cardData["subtitle"] || ""; \n\
257  font.weight: Font.Light; \n\
258  horizontalAlignment: root.headerAlignment; \n\
259  }\n';
260 
261 // %1 is used as top anchor of summary
262 // %2 is used as topMargin anchor of summary
263 // %3 is used as color of summary
264 var kSummaryLabelCode = 'Label { \n\
265  id: summary; \n\
266  objectName: "summaryLabel"; \n\
267  anchors { \n\
268  top: %1; \n\
269  left: parent.left; \n\
270  right: parent.right; \n\
271  margins: units.gu(1); \n\
272  topMargin: %2; \n\
273  } \n\
274  wrapMode: Text.Wrap; \n\
275  maximumLineCount: 5; \n\
276  elide: Text.ElideRight; \n\
277  text: cardData && cardData["summary"] || ""; \n\
278  height: text ? implicitHeight : 0; \n\
279  fontSize: "small"; \n\
280  color: %3; \n\
281  }\n';
282 
283 function cardString(template, components) {
284  var code;
285  code = 'AbstractButton { \n\
286  id: root; \n\
287  property var template; \n\
288  property var components; \n\
289  property var cardData; \n\
290  property var artShapeBorderSource: undefined; \n\
291  property real fontScale: 1.0; \n\
292  property var scopeStyle: null; \n\
293  property int headerAlignment: Text.AlignLeft; \n\
294  property int fixedHeaderHeight: -1; \n\
295  property size fixedArtShapeSize: Qt.size(-1, -1); \n\
296  readonly property string title: cardData && cardData["title"] || ""; \n\
297  property bool asynchronous: true; \n\
298  property bool showHeader: true; \n\
299  implicitWidth: childrenRect.width; \n';
300 
301  var hasArt = components["art"] && components["art"]["field"] || false;
302  var hasSummary = components["summary"] || false;
303  var artAndSummary = hasArt && hasSummary;
304  var isHorizontal = template["card-layout"] === "horizontal";
305  var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
306  var hasTitle = components["title"] || false;
307  var hasMascot = components["mascot"] || false;
308  var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
309  var hasSubtitle = hasTitle && components["subtitle"] || false;
310  var hasHeaderRow = hasMascot && hasTitle;
311 
312  if (hasBackground) {
313  code += kBackgroundLoaderCode;
314  }
315 
316  if (hasArt) {
317  code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
318  code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
319 
320  var widthCode, heightCode;
321  var anchors;
322  if (isHorizontal) {
323  anchors = 'left: parent.left';
324  if (hasMascot || hasTitle) {
325  widthCode = 'height * artShape.aspect'
326  heightCode = 'headerHeight + 2 * units.gu(1)';
327  } else {
328  // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
329  // So we define a "random" height of the image height + 2 gu for the margins
330  widthCode = 'height * artShape.aspect'
331  heightCode = 'units.gu(7.625)';
332  }
333  } else {
334  anchors = 'horizontalCenter: parent.horizontalCenter;';
335  widthCode = 'root.width'
336  heightCode = 'width / artShape.aspect';
337  }
338 
339  code += kArtShapeHolderCode.arg(anchors).arg(widthCode).arg(heightCode);
340  } else {
341  code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
342  }
343 
344  if (headerAsOverlay) {
345  code += kOverlayLoaderCode;
346  }
347 
348  var headerVerticalAnchors;
349  if (headerAsOverlay) {
350  headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
351  bottomMargin: units.gu(1);\n';
352  } else {
353  if (hasArt) {
354  if (isHorizontal) {
355  headerVerticalAnchors = 'top: artShapeHolder.top; \n\
356  topMargin: units.gu(1);\n';
357  } else {
358  headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
359  topMargin: units.gu(1);\n';
360  }
361  } else {
362  headerVerticalAnchors = 'top: parent.top; \n\
363  topMargin: units.gu(1);\n';
364  }
365  }
366  var headerLeftAnchor;
367  var headerLeftAnchorHasMargin = false;
368  if (isHorizontal && hasArt) {
369  headerLeftAnchor = 'left: artShapeHolder.right; \n\
370  leftMargin: units.gu(1);\n';
371  headerLeftAnchorHasMargin = true;
372  } else {
373  headerLeftAnchor = 'left: parent.left;\n';
374  }
375 
376  if (hasHeaderRow) {
377  code += 'readonly property int headerHeight: row.height;\n'
378  } else if (hasMascot) {
379  code += 'readonly property int headerHeight: mascotImage.height;\n'
380  } else if (hasSubtitle) {
381  code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
382  } else if (hasTitle) {
383  code += 'readonly property int headerHeight: titleLabel.height;\n'
384  } else {
385  code += 'readonly property int headerHeight: 0;\n'
386  }
387 
388  var mascotShapeCode = "";
389  var mascotCode = "";
390  if (hasMascot) {
391  var useMascotShape = !hasBackground && !headerAsOverlay;
392  var anchors = "";
393  if (!hasHeaderRow) {
394  anchors += headerLeftAnchor;
395  anchors += headerVerticalAnchors;
396  if (!headerLeftAnchorHasMargin) {
397  anchors += 'leftMargin: units.gu(1);\n'
398  }
399  } else {
400  anchors = "verticalCenter: parent.verticalCenter;"
401  }
402 
403  if (useMascotShape) {
404  mascotShapeCode = kMascotShapeLoaderCode.arg(anchors);
405  }
406 
407  var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
408  mascotCode = kMascotImageCode.arg(anchors).arg(mascotImageVisible);
409  }
410 
411  var summaryColorWithBackground = 'backgroundLoader.active && backgroundLoader.item && backgroundLoader.item.luminance < 0.7 ? "white" : (root.scopeStyle ? root.scopeStyle.foreground : "grey")';
412 
413  var titleSubtitleCode = "";
414  if (hasTitle) {
415  var color;
416  if (headerAsOverlay) {
417  color = '"white"';
418  } else if (hasSummary) {
419  color = 'summary.color';
420  } else if (hasBackground) {
421  color = summaryColorWithBackground;
422  } else {
423  color = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
424  }
425 
426  var titleAnchors;
427  var subtitleAnchors;
428  if (hasMascot && hasSubtitle) {
429  // Using row + column
430  titleAnchors = 'left: parent.left; right: parent.right';
431  subtitleAnchors = titleAnchors;
432  } else if (hasMascot) {
433  // Using row + label
434  titleAnchors = 'verticalCenter: parent.verticalCenter;\n'
435  } else {
436  if (headerAsOverlay) {
437  // Using anchors to the overlay
438  titleAnchors = 'left: parent.left; \n\
439  leftMargin: units.gu(1); \n\
440  right: parent.right; \n\
441  top: overlayLoader.top; \n\
442  topMargin: units.gu(1);\n';
443  } else {
444  // Using anchors to the mascot/parent
445  titleAnchors = "right: parent.right;";
446  titleAnchors += headerLeftAnchor;
447  titleAnchors += headerVerticalAnchors;
448  if (!headerLeftAnchorHasMargin) {
449  titleAnchors += 'leftMargin: units.gu(1);\n'
450  }
451  }
452  subtitleAnchors = 'left: titleLabel.left; \n\
453  leftMargin: titleLabel.leftMargin; \n\
454  right: titleLabel.right; \n\
455  top: titleLabel.bottom; \n\
456  topMargin: units.dp(2);\n';
457  }
458 
459  var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
460  var titleCode = kTitleLabelCode.arg(titleAnchors).arg(color).arg(titleLabelVisibleExtra);
461  var subtitleCode = "";
462  if (hasSubtitle) {
463  subtitleCode += kSubtitleLabelCode.arg(subtitleAnchors).arg(color);
464  }
465 
466  if (hasMascot && hasSubtitle) {
467  // If using row + column wrap the code in the column
468  titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
469  } else {
470  titleSubtitleCode = titleCode + subtitleCode;
471  }
472  }
473 
474  if (hasHeaderRow) {
475  if (mascotShapeCode != "") {
476  code += kHeaderRow3Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotShapeCode).arg(mascotCode).arg(titleSubtitleCode);
477  } else {
478  code += kHeaderRow2Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotCode).arg(titleSubtitleCode);
479  }
480  } else {
481  code += mascotShapeCode + mascotCode + titleSubtitleCode;
482  }
483 
484  if (hasSummary) {
485  var summaryTopAnchor;
486  if (isHorizontal && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
487  else if (headerAsOverlay && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
488  else if (hasHeaderRow) summaryTopAnchor = "row.bottom";
489  else if (hasMascot) summaryTopAnchor = "mascotImage.bottom";
490  else if (hasSubtitle) summaryTopAnchor = "subtitleLabel.bottom";
491  else if (hasTitle) summaryTopAnchor = "titleLabel.bottom";
492  else if (hasArt) summaryTopAnchor = "artShapeHolder.bottom";
493  else summaryTopAnchor = "parent.top";
494 
495  var color;
496  if (hasBackground) {
497  color = summaryColorWithBackground;
498  } else {
499  color = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
500  }
501 
502  var summaryTopMargin = (hasMascot || hasSubtitle ? 'anchors.margins' : '0');
503 
504  code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(color);
505  }
506 
507  if (hasSummary) {
508  code += 'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
509  } else if (hasHeaderRow) {
510  code += 'implicitHeight: row.y + row.height + units.gu(1);\n';
511  } else if (hasMascot) {
512  code += 'implicitHeight: mascotImage.y + mascotImage.height;\n';
513  } else if (hasSubtitle) {
514  code += 'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
515  } else if (hasTitle) {
516  code += 'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
517  } else if (hasArt) {
518  code += 'implicitHeight: artShapeHolder.height;\n';
519  }
520  // Close the AbstractButton
521  code += '}\n';
522 
523  return code;
524 }
525 
526 function createCardComponent(parent, template, components) {
527  var imports = 'import QtQuick 2.2; \n\
528  import Ubuntu.Components 0.1; \n\
529  import Ubuntu.Thumbnailer 0.1;\n';
530  var card = cardString(template, components);
531  var code = imports + 'Component {\n' + card + '}\n';
532  return Qt.createQmlObject(code, parent, "createCardComponent");
533 }