Unity 8
CardGrid.qml
1 /*
2  * Copyright (C) 2013 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 import QtQuick 2.4
18 import "../Components"
19 
20 DashRenderer {
21  id: root
22 
23  readonly property int collapsedRows: {
24  if (!cardTool || !cardTool.template || typeof cardTool.template["collapsed-rows"] != "number") return 2;
25  return cardTool.template["collapsed-rows"];
26  }
27 
28  // ↓ This is the ubuntu store icon
29  readonly property string backgroundShapeStyle: cardTool.isAppLikeScope && !cardTool.isAppLikeScopeAppCategory ? "shadow" : "flat"
30 
31  expandedHeight: grid.totalContentHeight
32  collapsedHeight: Math.min(grid.contentHeightForRows(collapsedRows, grid.cellHeight), expandedHeight)
33  collapsedItemCount: collapsedRows * grid.columns
34  originY: grid.originY
35 
36  function cardPosition(index) {
37  var pos = {};
38  var row = Math.floor(index / grid.columns);
39  var column = index % grid.columns;
40  // Bit sad this is not symmetrical
41  pos.x = column * grid.cellWidth + grid.margins;
42  pos.y = row * grid.cellHeight;
43  return pos;
44  }
45 
46  ResponsiveGridView {
47  id: grid
48  anchors.fill: parent
49  minimumHorizontalSpacing: (cardTool.isAppLikeScopeAppCategory && cardTool.isWideView) ? units.gu(5) : units.gu(1)
50  delegateWidth: cardTool.cardWidth
51  delegateHeight: cardTool.cardHeight
52  verticalSpacing: units.gu(1)
53  model: root.model
54  displayMarginBeginning: root.displayMarginBeginning
55  displayMarginEnd: root.displayMarginEnd
56  cacheBuffer: root.cacheBuffer
57  interactive: false
58  delegate: Item {
59  width: grid.cellWidth
60  height: grid.cellHeight
61  Loader {
62  id: loader
63  sourceComponent: cardTool.cardComponent
64  anchors.horizontalCenter: parent.horizontalCenter
65  onLoaded: {
66  item.objectName = "delegate" + index;
67  item.width = Qt.binding(function() { return cardTool.cardWidth; });
68  item.height = Qt.binding(function() { return cardTool.cardHeight; });
69  item.fixedHeaderHeight = Qt.binding(function() { return cardTool.headerHeight; });
70  item.fixedArtShapeSize = Qt.binding(function() { return cardTool.artShapeSize; });
71  item.cardData = Qt.binding(function() { return model; });
72  item.scopeStyle = root.scopeStyle;
73  item.backgroundShapeStyle = root.backgroundShapeStyle;
74  }
75  Connections {
76  target: loader.item
77  onClicked: root.clicked(index, result, loader.item, model)
78  onPressAndHold: root.pressAndHold(index, result, model)
79  onAction: root.action(index, result, actionId)
80  }
81  }
82  }
83  }
84 }