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