on 3b6df80 help sign in
unity8/plugins/Dash/CardCreator.js
530 lines 27 kB history raw
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 */
17.pragma library
19var 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';
53// %1 is used as anchors of artShapeHolder
54// %2 is used as image width
55// %3 is used as image height
56var 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';
104var 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';
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
148var 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';
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
167var 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';
184// %1 is used as first child of the column
185// %2 is used as second child of the column
186var 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';
196// %1 is used as anchors of mascotShapeLoader
197var 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';
209// %1 is used as anchors of mascotImage
210// %2 is used as visible of mascotImage
211var 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';
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
229var 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';
245// %1 is used as anchors of subtitleLabel
246// %2 is used as color of subtitleLabel
247var 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';
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
264var 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';
283function 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 int headerAlignment: Text.AlignLeft; \n\
293 property int fixedHeaderHeight: -1; \n\
294 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
295 readonly property string title: cardData && cardData["title"] || ""; \n\
296 property bool asynchronous: true; \n\
297 property bool showHeader: true; \n\
298 implicitWidth: childrenRect.width; \n';
300 var hasArt = components["art"] && components["art"]["field"] || false;
301 var hasSummary = components["summary"] || false;
302 var artAndSummary = hasArt && hasSummary;
303 var isHorizontal = template["card-layout"] === "horizontal";
304 var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
305 var hasTitle = components["title"] || false;
306 var hasMascot = components["mascot"] || false;
307 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
308 var hasSubtitle = components["subtitle"] || false;
309 var hasHeaderRow = hasMascot && hasTitle;
311 if (hasBackground) {
312 code += kBackgroundLoaderCode;
313 }
315 if (hasArt) {
316 code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
317 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
319 var widthCode, heightCode;
320 var anchors;
321 if (isHorizontal) {
322 anchors = 'left: parent.left';
323 if (hasMascot || hasTitle) {
324 widthCode = 'height * artShape.aspect'
325 heightCode = 'headerHeight + 2 * units.gu(1)';
326 } else {
327 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
328 // So we define a "random" height of the image height + 2 gu for the margins
329 widthCode = 'height * artShape.aspect'
330 heightCode = 'units.gu(7.625)';
331 }
332 } else {
333 anchors = 'horizontalCenter: parent.horizontalCenter;';
334 widthCode = 'root.width'
335 heightCode = 'width / artShape.aspect';
336 }
338 code += kArtShapeHolderCode.arg(anchors).arg(widthCode).arg(heightCode);
339 } else {
340 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
341 }
343 if (headerAsOverlay) {
344 code += kOverlayLoaderCode;
345 }
347 var headerVerticalAnchors;
348 if (headerAsOverlay) {
349 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
350 bottomMargin: units.gu(1);\n';
351 } else {
352 if (hasArt) {
353 if (isHorizontal) {
354 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
355 topMargin: units.gu(1);\n';
356 } else {
357 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
358 topMargin: units.gu(1);\n';
359 }
360 } else {
361 headerVerticalAnchors = 'top: parent.top; \n\
362 topMargin: units.gu(1);\n';
363 }
364 }
365 var headerLeftAnchor;
366 var headerLeftAnchorHasMargin = false;
367 if (isHorizontal && hasArt) {
368 headerLeftAnchor = 'left: artShapeHolder.right; \n\
369 leftMargin: units.gu(1);\n';
370 headerLeftAnchorHasMargin = true;
371 } else {
372 headerLeftAnchor = 'left: parent.left;\n';
373 }
375 if (hasHeaderRow) {
376 code += 'readonly property int headerHeight: row.height;\n'
377 } else if (hasMascot) {
378 code += 'readonly property int headerHeight: mascotImage.height;\n'
379 } else if (hasSubtitle) {
380 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
381 } else if (hasTitle) {
382 code += 'readonly property int headerHeight: titleLabel.height;\n'
383 } else {
384 code += 'readonly property int headerHeight: 0;\n'
385 }
387 var mascotShapeCode = "";
388 var mascotCode = "";
389 if (hasMascot) {
390 var useMascotShape = !hasBackground && !headerAsOverlay;
391 var anchors = "";
392 if (!hasHeaderRow) {
393 anchors += headerLeftAnchor;
394 anchors += headerVerticalAnchors;
395 if (!headerLeftAnchorHasMargin) {
396 anchors += 'leftMargin: units.gu(1);\n'
397 }
398 } else {
399 anchors = "verticalCenter: parent.verticalCenter;"
400 }
402 if (useMascotShape) {
403 mascotShapeCode = kMascotShapeLoaderCode.arg(anchors);
404 }
406 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
407 mascotCode = kMascotImageCode.arg(anchors).arg(mascotImageVisible);
408 }
410 var summaryColorWithBackground = 'backgroundLoader.active && backgroundLoader.item && backgroundLoader.item.luminance < 0.7 ? "white" : "grey"';
412 var titleSubtitleCode = "";
413 if (hasTitle) {
414 var color;
415 if (headerAsOverlay) {
416 color = '"white"';
417 } else if (hasSummary) {
418 color = 'summary.color';
419 } else if (hasBackground) {
420 color = summaryColorWithBackground;
421 } else {
422 color = '"grey"';
423 }
425 var titleAnchors;
426 var subtitleAnchors;
427 if (hasMascot && hasSubtitle) {
428 // Using row + column
429 titleAnchors = 'left: parent.left; right: parent.right';
430 subtitleAnchors = titleAnchors;
431 } else if (hasMascot) {
432 // Using row + label
433 titleAnchors = 'verticalCenter: parent.verticalCenter;\n'
434 } else {
435 if (headerAsOverlay) {
436 // Using anchors to the overlay
437 titleAnchors = 'left: parent.left; \n\
438 leftMargin: units.gu(1); \n\
439 right: parent.right; \n\
440 top: overlayLoader.top; \n\
441 topMargin: units.gu(1);\n';
442 } else {
443 // Using anchors to the mascot/parent
444 titleAnchors = "right: parent.right;";
445 titleAnchors += headerLeftAnchor;
446 titleAnchors += headerVerticalAnchors;
447 if (!headerLeftAnchorHasMargin) {
448 titleAnchors += 'leftMargin: units.gu(1);\n'
449 }
450 }
451 subtitleAnchors = 'left: titleLabel.left; \n\
452 leftMargin: titleLabel.leftMargin; \n\
453 right: titleLabel.right; \n\
454 top: titleLabel.bottom; \n\
455 topMargin: units.dp(2);\n';
456 }
458 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
459 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(color).arg(titleLabelVisibleExtra);
460 var subtitleCode = "";
461 if (hasSubtitle) {
462 subtitleCode += kSubtitleLabelCode.arg(subtitleAnchors).arg(color);
463 }
465 if (hasMascot && hasSubtitle) {
466 // If using row + column wrap the code in the column
467 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
468 } else {
469 titleSubtitleCode = titleCode + subtitleCode;
470 }
471 }
473 if (hasHeaderRow) {
474 if (mascotShapeCode != "") {
475 code += kHeaderRow3Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotShapeCode).arg(mascotCode).arg(titleSubtitleCode);
476 } else {
477 code += kHeaderRow2Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotCode).arg(titleSubtitleCode);
478 }
479 } else {
480 code += mascotShapeCode + mascotCode + titleSubtitleCode;
481 }
483 if (hasSummary) {
484 var summaryTopAnchor;
485 if (isHorizontal && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
486 else if (headerAsOverlay && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
487 else if (hasHeaderRow) summaryTopAnchor = "row.bottom";
488 else if (hasMascot) summaryTopAnchor = "mascotImage.bottom";
489 else if (hasSubtitle) summaryTopAnchor = "subtitleLabel.bottom";
490 else if (hasTitle) summaryTopAnchor = "titleLabel.bottom";
491 else if (hasArt) summaryTopAnchor = "artShapeHolder.bottom";
492 else summaryTopAnchor = "parent.top";
494 var color;
495 if (hasBackground) {
496 color = summaryColorWithBackground;
497 } else {
498 color = '"grey"';
499 }
501 var summaryTopMargin = (hasMascot || hasSubtitle ? 'anchors.margins' : '0');
503 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(color);
504 }
506 if (hasSummary) {
507 code += 'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
508 } else if (hasHeaderRow) {
509 code += 'implicitHeight: row.y + row.height + units.gu(1);\n';
510 } else if (hasMascot) {
511 code += 'implicitHeight: mascotImage.y + mascotImage.height;\n';
512 } else if (hasSubtitle) {
513 code += 'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
514 } else if (hasTitle) {
515 code += 'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
516 }
517 // Close the AbstractButton
518 code += '}\n';
520 return code;
523function createCardComponent(parent, template, components) {
524 var imports = 'import QtQuick 2.2; \n\
525 import Ubuntu.Components 0.1; \n\
526 import Ubuntu.Thumbnailer 0.1;\n';
527 var card = cardString(template, components);
528 var code = imports + 'Component {\n' + card + '}\n';
529 return Qt.createQmlObject(code, parent, "createCardComponent");