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