2 * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
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\
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\
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\
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\
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\
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\
69 objectName: "artShape"; \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\
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\
91 objectName: "artImage"; \n\
92 source: cardData && cardData["art"] || ""; \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\
104var kOverlayLoaderCode = 'Loader { \n\
105 id: overlayLoader; \n\
107 left: artShapeHolder.left; \n\
108 right: artShapeHolder.right; \n\
109 bottom: artShapeHolder.bottom; \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\
116 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
118 property var source: ShaderEffectSource { \n\
119 id: shaderSource; \n\
120 sourceItem: artShapeLoader.item; \n\
121 onVisibleChanged: if (visible) scheduleUpdate(); \n\
123 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \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\
131 coord = qt_MultiTexCoord0; \n\
132 gl_Position = qt_Matrix * qt_Vertex; \n\
134 fragmentShader: " \n\
135 varying highp vec2 coord; \n\
136 uniform sampler2D source; \n\
137 uniform lowp float qt_Opacity; \n\
139 lowp vec4 tex = texture2D(source, coord); \n\
140 gl_FragColor = vec4(0, 0, 0, tex.a) * qt_Opacity; \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\
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\
155 anchors.right: parent.right; \n\
156 anchors.margins: margins;\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\
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\
174 anchors.right: parent.right; \n\
175 anchors.margins: margins;\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\
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\
209// %1 is used as anchors of mascotImage
210// %2 is used as visible of mascotImage
211var kMascotImageCode = 'Image { \n\
213 objectName: "mascotImage"; \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\
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\
231 objectName: "titleLabel"; \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\
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\
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\
251 elide: Text.ElideRight; \n\
252 fontSize: "small"; \n\
253 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \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\
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\
266 objectName: "summaryLabel"; \n\
269 left: parent.left; \n\
270 right: parent.right; \n\
271 margins: units.gu(1); \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\
283function cardString(template, components) {
285 code = 'AbstractButton { \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';
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;
313 code += kBackgroundLoaderCode;
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';
320 var widthCode, heightCode;
323 anchors = 'left: parent.left';
324 if (hasMascot || hasTitle) {
325 widthCode = 'height * artShape.aspect'
326 heightCode = 'headerHeight + 2 * units.gu(1)';
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)';
334 anchors = 'horizontalCenter: parent.horizontalCenter;';
335 widthCode = 'root.width'
336 heightCode = 'width / artShape.aspect';
339 code += kArtShapeHolderCode.arg(anchors).arg(widthCode).arg(heightCode);
341 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
344 if (headerAsOverlay) {
345 code += kOverlayLoaderCode;
348 var headerVerticalAnchors;
349 if (headerAsOverlay) {
350 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
351 bottomMargin: units.gu(1);\n';
355 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
356 topMargin: units.gu(1);\n';
358 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
359 topMargin: units.gu(1);\n';
362 headerVerticalAnchors = 'top: parent.top; \n\
363 topMargin: units.gu(1);\n';
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;
373 headerLeftAnchor = 'left: parent.left;\n';
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'
385 code += 'readonly property int headerHeight: 0;\n'
388 var mascotShapeCode = "";
391 var useMascotShape = !hasBackground && !headerAsOverlay;
394 anchors += headerLeftAnchor;
395 anchors += headerVerticalAnchors;
396 if (!headerLeftAnchorHasMargin) {
397 anchors += 'leftMargin: units.gu(1);\n'
400 anchors = "verticalCenter: parent.verticalCenter;"
403 if (useMascotShape) {
404 mascotShapeCode = kMascotShapeLoaderCode.arg(anchors);
407 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
408 mascotCode = kMascotImageCode.arg(anchors).arg(mascotImageVisible);
411 var summaryColorWithBackground = 'backgroundLoader.active && backgroundLoader.item && backgroundLoader.item.luminance < (root.scopeStyle ? root.scopeStyle.threshold : 0.7) ? (root.scopeStyle ? root.scopeStyle.light : "white") : (root.scopeStyle ? root.scopeStyle.dark : "grey")';
413 var titleSubtitleCode = "";
416 if (headerAsOverlay) {
418 } else if (hasSummary) {
419 color = 'summary.color';
420 } else if (hasBackground) {
421 color = summaryColorWithBackground;
423 color = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
428 if (hasMascot && hasSubtitle) {
429 // Using row + column
430 titleAnchors = 'left: parent.left; right: parent.right';
431 subtitleAnchors = titleAnchors;
432 } else if (hasMascot) {
434 titleAnchors = 'verticalCenter: parent.verticalCenter;\n'
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 rightMargin: units.gu(1); \n\
442 top: overlayLoader.top; \n\
443 topMargin: units.gu(1);\n';
445 // Using anchors to the mascot/parent
446 titleAnchors = "right: parent.right;";
447 titleAnchors += headerLeftAnchor;
448 titleAnchors += headerVerticalAnchors;
449 if (!headerLeftAnchorHasMargin) {
450 titleAnchors += 'leftMargin: units.gu(1);\n'
453 subtitleAnchors = 'left: titleLabel.left; \n\
454 leftMargin: titleLabel.leftMargin; \n\
455 right: titleLabel.right; \n\
456 top: titleLabel.bottom; \n\
457 topMargin: units.dp(2);\n';
460 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
461 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(color).arg(titleLabelVisibleExtra);
462 var subtitleCode = "";
464 subtitleCode += kSubtitleLabelCode.arg(subtitleAnchors).arg(color);
467 if (hasMascot && hasSubtitle) {
468 // If using row + column wrap the code in the column
469 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
471 titleSubtitleCode = titleCode + subtitleCode;
476 if (mascotShapeCode != "") {
477 code += kHeaderRow3Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotShapeCode).arg(mascotCode).arg(titleSubtitleCode);
479 code += kHeaderRow2Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotCode).arg(titleSubtitleCode);
482 code += mascotShapeCode + mascotCode + titleSubtitleCode;
486 var summaryTopAnchor;
487 if (isHorizontal && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
488 else if (headerAsOverlay && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
489 else if (hasHeaderRow) summaryTopAnchor = "row.bottom";
490 else if (hasMascot) summaryTopAnchor = "mascotImage.bottom";
491 else if (hasSubtitle) summaryTopAnchor = "subtitleLabel.bottom";
492 else if (hasTitle) summaryTopAnchor = "titleLabel.bottom";
493 else if (hasArt) summaryTopAnchor = "artShapeHolder.bottom";
494 else summaryTopAnchor = "parent.top";
498 color = summaryColorWithBackground;
500 color = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
503 var summaryTopMargin = (hasMascot || hasSubtitle ? 'anchors.margins' : '0');
505 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(color);
509 code += 'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
510 } else if (hasHeaderRow) {
511 code += 'implicitHeight: row.y + row.height + units.gu(1);\n';
512 } else if (hasMascot) {
513 code += 'implicitHeight: mascotImage.y + mascotImage.height;\n';
514 } else if (hasSubtitle) {
515 code += 'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
516 } else if (hasTitle) {
517 code += 'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
519 code += 'implicitHeight: artShapeHolder.height;\n';
521 // Close the AbstractButton
527function createCardComponent(parent, template, components) {
528 var imports = 'import QtQuick 2.2; \n\
529 import Ubuntu.Components 0.1; \n\
530 import Ubuntu.Thumbnailer 0.1;\n';
531 var card = cardString(template, components);
532 var code = imports + 'Component {\n' + card + '}\n';
533 return Qt.createQmlObject(code, parent, "createCardComponent");
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list