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 Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
75 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
76 function updateWidthHeightBindings() { \n\
77 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
78 width = root.fixedArtShapeSize.width; \n\
79 height = root.fixedArtShapeSize.height; \n\
81 width = Qt.binding(function() { return !visible ? 0 : image.width }); \n\
82 height = Qt.binding(function() { return !visible ? 0 : image.height }); \n\
86 objectName: "artImage"; \n\
87 source: cardData && cardData["art"] || ""; \n\
89 asynchronous: root.asynchronous; \n\
90 fillMode: Image.PreserveAspectCrop; \n\
98var kOverlayLoaderCode = 'Loader { \n\
99 id: overlayLoader; \n\
101 left: artShapeHolder.left; \n\
102 right: artShapeHolder.right; \n\
103 bottom: artShapeHolder.bottom; \n\
105 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
106 asynchronous: root.asynchronous; \n\
107 visible: showHeader && status == Loader.Ready; \n\
108 sourceComponent: ShaderEffect { \n\
110 height: (fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight) + units.gu(2); \n\
111 property real luminance: 0.2126 * overlayColor.r + 0.7152 * overlayColor.g + 0.0722 * overlayColor.b; \n\
112 property color overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
113 property var source: ShaderEffectSource { \n\
114 id: shaderSource; \n\
115 sourceItem: artShapeLoader.item; \n\
116 onVisibleChanged: if (visible) scheduleUpdate(); \n\
118 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
121 uniform highp mat4 qt_Matrix; \n\
122 attribute highp vec4 qt_Vertex; \n\
123 attribute highp vec2 qt_MultiTexCoord0; \n\
124 varying highp vec2 coord; \n\
126 coord = qt_MultiTexCoord0; \n\
127 gl_Position = qt_Matrix * qt_Vertex; \n\
129 fragmentShader: " \n\
130 varying highp vec2 coord; \n\
131 uniform sampler2D source; \n\
132 uniform lowp float qt_Opacity; \n\
133 uniform highp vec4 overlayColor; \n\
135 lowp vec4 tex = texture2D(source, coord); \n\
136 gl_FragColor = vec4(overlayColor.r, overlayColor.g, overlayColor.b, 1) * qt_Opacity * overlayColor.a * tex.a; \n\
141// %1 is used as anchors of row
142// %2 is used as first child of the row
143// %3 is used as second child of the row
144var kHeaderRow2Code = 'Row { \n\
146 objectName: "outerRow"; \n\
147 property real margins: units.gu(1); \n\
148 spacing: margins; \n\
149 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
151 anchors.right: parent.right; \n\
152 anchors.margins: margins;\n\
159// %1 is used as anchors of row
160// %2 is used as first child of the row
161// %3 is used as second child of the row
162// %4 is used as third child of the row
163var kHeaderRow3Code = 'Row { \n\
165 objectName: "outerRow"; \n\
166 property real margins: units.gu(1); \n\
167 spacing: margins; \n\
168 height: root.fixedHeaderHeight != -1 ? root.fixedHeaderHeight : implicitHeight; \n\
170 anchors.right: parent.right; \n\
171 anchors.margins: margins;\n\
180// %1 is used as first child of the column
181// %2 is used as second child of the column
182var kHeaderColumnCode = 'Column { \n\
183 anchors.verticalCenter: parent.verticalCenter; \n\
184 spacing: units.dp(2); \n\
185 width: parent.width - x;\n\
192// multiple column version of kHeaderColumnCode.
193function kHeaderColumnCodeGenerator() {
194 var headerColumnCodeTemplate = 'Column { \n\
195 anchors.verticalCenter: parent.verticalCenter; \n\
196 spacing: units.dp(2); \n\
197 width: parent.width - x;\n\
202 var args = Array.prototype.slice.call(arguments);
203 var code = headerColumnCodeTemplate.arg(args.join(',\n'));
207// %1 is used as anchors of mascotShapeLoader
208var kMascotShapeLoaderCode = 'Loader { \n\
209 id: mascotShapeLoader; \n\
210 objectName: "mascotShapeLoader"; \n\
211 asynchronous: root.asynchronous; \n\
212 active: mascotImage.status === Image.Ready; \n\
213 visible: showHeader && active && status == Loader.Ready; \n\
214 width: units.gu(6); \n\
215 height: units.gu(5.625); \n\
216 sourceComponent: UbuntuShape { image: mascotImage } \n\
220// %1 is used as anchors of mascotImage
221// %2 is used as visible of mascotImage
222var kMascotImageCode = 'Image { \n\
224 objectName: "mascotImage"; \n\
226 readonly property int maxSize: Math.max(width, height) * 4; \n\
227 source: cardData && cardData["mascot"]; \n\
228 width: units.gu(6); \n\
229 height: units.gu(5.625); \n\
230 sourceSize { width: maxSize; height: maxSize } \n\
231 fillMode: Image.PreserveAspectCrop; \n\
232 horizontalAlignment: Image.AlignHCenter; \n\
233 verticalAlignment: Image.AlignVCenter; \n\
237// %1 is used as anchors of titleLabel
238// %2 is used as color of titleLabel
239// %3 is used as extra condition for visible of titleLabel
240var kTitleLabelCode = 'Label { \n\
242 objectName: "titleLabel"; \n\
244 elide: Text.ElideRight; \n\
245 fontSize: "small"; \n\
246 wrapMode: Text.Wrap; \n\
247 maximumLineCount: 2; \n\
248 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
250 visible: showHeader %3; \n\
251 text: root.title; \n\
252 font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
253 horizontalAlignment: root.headerAlignment; \n\
256// %1 is used as anchors of touchdown effect
257var kTouchdownCode = 'UbuntuShape { \n\
259 objectName: "touchdown"; \n\
261 visible: root.pressed; \n\
262 radius: "medium"; \n\
263 borderSource: "radius_pressed.sci" \n\
266// %1 is used as anchors of subtitleLabel
267// %2 is used as color of subtitleLabel
268var kSubtitleLabelCode = 'Label { \n\
269 id: subtitleLabel; \n\
270 objectName: "subtitleLabel"; \n\
272 elide: Text.ElideRight; \n\
273 fontSize: "small"; \n\
274 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
276 visible: titleLabel.visible && titleLabel.text; \n\
277 text: cardData && cardData["subtitle"] || ""; \n\
278 font.weight: Font.Light; \n\
279 horizontalAlignment: root.headerAlignment; \n\
282// %1 is used as anchors of attributesRow
283var kAttributesRowCode = 'CardAttributes { \n\
284 id: attributesRow; \n\
285 objectName: "attributesRow"; \n\
288 model: cardData["attributes"]; \n\
291// %1 is used as top anchor of summary
292// %2 is used as topMargin anchor of summary
293// %3 is used as color of summary
294var kSummaryLabelCode = 'Label { \n\
296 objectName: "summaryLabel"; \n\
299 left: parent.left; \n\
300 right: parent.right; \n\
301 margins: units.gu(1); \n\
304 wrapMode: Text.Wrap; \n\
305 maximumLineCount: 5; \n\
306 elide: Text.ElideRight; \n\
307 text: cardData && cardData["summary"] || ""; \n\
308 height: text ? implicitHeight : 0; \n\
309 fontSize: "small"; \n\
313function cardString(template, components) {
315 code = 'AbstractButton { \n\
317 property var template; \n\
318 property var components; \n\
319 property var cardData; \n\
320 property var artShapeBorderSource: undefined; \n\
321 property real fontScale: 1.0; \n\
322 property var scopeStyle: null; \n\
323 property int headerAlignment: Text.AlignLeft; \n\
324 property int fixedHeaderHeight: -1; \n\
325 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
326 readonly property string title: cardData && cardData["title"] || ""; \n\
327 property bool asynchronous: true; \n\
328 property bool showHeader: true; \n\
329 implicitWidth: childrenRect.width; \n';
331 var hasArt = components["art"] && components["art"]["field"] || false;
332 var hasSummary = components["summary"] || false;
333 var artAndSummary = hasArt && hasSummary;
334 var isHorizontal = template["card-layout"] === "horizontal";
335 var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
336 var hasTitle = components["title"] || false;
337 var hasMascot = components["mascot"] || false;
338 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
339 var hasSubtitle = hasTitle && components["subtitle"] || false;
340 var hasHeaderRow = hasMascot && hasTitle;
341 var hasAttributes = hasTitle && components["attributes"] || false;
344 code += kBackgroundLoaderCode;
348 code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
349 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
351 var widthCode, heightCode;
354 anchors = 'left: parent.left';
355 if (hasMascot || hasTitle) {
356 widthCode = 'height * artShape.aspect'
357 heightCode = 'headerHeight + 2 * units.gu(1)';
359 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
360 // So we define a "random" height of the image height + 2 gu for the margins
361 widthCode = 'height * artShape.aspect'
362 heightCode = 'units.gu(7.625)';
365 anchors = 'horizontalCenter: parent.horizontalCenter;';
366 widthCode = 'root.width'
367 heightCode = 'width / artShape.aspect';
370 code += kArtShapeHolderCode.arg(anchors).arg(widthCode).arg(heightCode);
372 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
375 if (headerAsOverlay) {
376 code += kOverlayLoaderCode;
379 var headerVerticalAnchors;
380 if (headerAsOverlay) {
381 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
382 bottomMargin: units.gu(1);\n';
386 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
387 topMargin: units.gu(1);\n';
389 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
390 topMargin: units.gu(1);\n';
393 headerVerticalAnchors = 'top: parent.top; \n\
394 topMargin: units.gu(1);\n';
397 var headerLeftAnchor;
398 var headerLeftAnchorHasMargin = false;
399 if (isHorizontal && hasArt) {
400 headerLeftAnchor = 'left: artShapeHolder.right; \n\
401 leftMargin: units.gu(1);\n';
402 headerLeftAnchorHasMargin = true;
404 headerLeftAnchor = 'left: parent.left;\n';
408 code += 'readonly property int headerHeight: row.height;\n'
409 } else if (hasMascot) {
410 code += 'readonly property int headerHeight: mascotImage.height;\n'
411 } else if (hasAttributes) {
412 if (hasTitle && hasSubtitle) {
413 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
414 } else if (hasTitle) {
415 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
417 code += 'readonly property int headerHeight: attributesRow.height;\n'
419 } else if (hasSubtitle) {
420 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
421 } else if (hasTitle) {
422 code += 'readonly property int headerHeight: titleLabel.height;\n'
424 code += 'readonly property int headerHeight: 0;\n'
427 var mascotShapeCode = "";
430 var useMascotShape = !hasBackground && !headerAsOverlay;
433 anchors += headerLeftAnchor;
434 anchors += headerVerticalAnchors;
435 if (!headerLeftAnchorHasMargin) {
436 anchors += 'leftMargin: units.gu(1);\n'
439 anchors = "verticalCenter: parent.verticalCenter;"
442 if (useMascotShape) {
443 mascotShapeCode = kMascotShapeLoaderCode.arg(anchors);
446 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
447 mascotCode = kMascotImageCode.arg(anchors).arg(mascotImageVisible);
450 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")';
452 var titleSubtitleCode = "";
455 if (headerAsOverlay) {
456 color = 'overlayLoader.item.luminance < (root.scopeStyle ? root.scopeStyle.threshold : 0.7) ? (root.scopeStyle ? root.scopeStyle.light : "white") : (root.scopeStyle ? root.scopeStyle.dark : "grey")';
457 } else if (hasSummary) {
458 color = 'summary.color';
459 } else if (hasBackground) {
460 color = summaryColorWithBackground;
462 color = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
467 var attributesAnchors;
468 if (hasMascot && (hasSubtitle || hasAttributes)) {
469 // Using row + column
470 titleAnchors = 'left: parent.left; right: parent.right';
471 subtitleAnchors = titleAnchors;
472 attributesAnchors = subtitleAnchors;
473 } else if (hasMascot) {
475 titleAnchors = 'verticalCenter: parent.verticalCenter;\n'
477 if (headerAsOverlay) {
478 // Using anchors to the overlay
479 titleAnchors = 'left: parent.left; \n\
480 leftMargin: units.gu(1); \n\
481 right: parent.right; \n\
482 rightMargin: units.gu(1); \n\
483 top: overlayLoader.top; \n\
484 topMargin: units.gu(1);\n';
486 // Using anchors to the mascot/parent
487 titleAnchors = "right: parent.right;\n";
488 titleAnchors += "rightMargin: units.gu(1);\n";
489 titleAnchors += headerLeftAnchor;
490 titleAnchors += headerVerticalAnchors;
491 if (!headerLeftAnchorHasMargin) {
492 titleAnchors += 'leftMargin: units.gu(1);\n'
495 subtitleAnchors = 'left: titleLabel.left; \n\
496 leftMargin: titleLabel.leftMargin; \n\
497 right: titleLabel.right; \n\
498 rightMargin: titleLabel.rightMargin; \n\
499 top: titleLabel.bottom; \n\
500 topMargin: units.dp(2);\n';
502 attributesAnchors = 'left: subtitleLabel.left; \n\
503 leftMargin: subtitleLabel.leftMargin; \n\
504 right: subtitleLabel.right; \n\
505 rightMargin: subtitleLabel.rightMargin; \n\
506 top: subtitleLabel.bottom; \n\
507 topMargin: units.dp(2);\n';
509 attributesAnchors = subtitleAnchors;
513 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
514 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(color).arg(titleLabelVisibleExtra);
515 var subtitleCode = "";
517 subtitleCode += kSubtitleLabelCode.arg(subtitleAnchors).arg(color);
520 if (hasMascot && (hasSubtitle || hasAttributes)) {
521 // If using row + column wrap the code in the column
522 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
523 if (hasSubtitle && hasAttributes) {
524 var attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(color);
525 titleSubtitleCode = kHeaderColumnCodeGenerator(titleCode, subtitleCode, attributesCode);
526 } else if (hasSubtitle) {
527 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
528 } else if (hasAttributes) {
529 var attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(color);
530 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(attributesCode);
533 titleSubtitleCode = titleCode;
535 titleSubtitleCode = titleSubtitleCode + subtitleCode;
538 var attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(color);
539 titleSubtitleCode = titleSubtitleCode + attributesCode;
545 if (mascotShapeCode != "") {
546 code += kHeaderRow3Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotShapeCode).arg(mascotCode).arg(titleSubtitleCode);
548 code += kHeaderRow2Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotCode).arg(titleSubtitleCode);
551 code += mascotShapeCode + mascotCode + titleSubtitleCode;
555 var summaryTopAnchor;
556 if (isHorizontal && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
557 else if (headerAsOverlay && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
558 else if (hasHeaderRow) summaryTopAnchor = "row.bottom";
559 else if (hasMascot) summaryTopAnchor = "mascotImage.bottom";
560 else if (hasAttributes) summaryTopAnchor = "attributesRow.bottom";
561 else if (hasSubtitle) summaryTopAnchor = "subtitleLabel.bottom";
562 else if (hasTitle) summaryTopAnchor = "titleLabel.bottom";
563 else if (hasArt) summaryTopAnchor = "artShapeHolder.bottom";
564 else summaryTopAnchor = "parent.top";
568 color = summaryColorWithBackground;
570 color = 'root.scopeStyle ? root.scopeStyle.foreground : "grey"';
573 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
575 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(color);
578 var touchdownAnchors;
580 touchdownAnchors = 'fill: backgroundLoader';
581 } else if (hasArt && !hasMascot && !hasSummary) {
582 touchdownAnchors = 'fill: artShapeHolder';
584 touchdownAnchors = 'fill: root'
586 code += kTouchdownCode.arg(touchdownAnchors);
589 code += 'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
590 } else if (hasHeaderRow) {
591 code += 'implicitHeight: row.y + row.height + units.gu(1);\n';
592 } else if (hasMascot) {
593 code += 'implicitHeight: mascotImage.y + mascotImage.height;\n';
594 } else if (hasAttributes) {
595 code += 'implicitHeight: attributesRow.y + attributesRow.height + units.gu(1);\n';
596 } else if (hasSubtitle) {
597 code += 'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
598 } else if (hasTitle) {
599 code += 'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
601 code += 'implicitHeight: artShapeHolder.height;\n';
603 // Close the AbstractButton
609function createCardComponent(parent, template, components) {
610 var imports = 'import QtQuick 2.2; \n\
611 import Ubuntu.Components 0.1; \n\
613 var card = cardString(template, components);
614 var code = imports + 'Component {\n' + card + '}\n';
616 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