Unity 8
CardTool.qml
1 /*
2  * Copyright (C) 2014 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 Ubuntu.Components 1.3
19 import Dash 0.1
20 
21 /*!
22  \brief Tool for introspecting Card properties.
23 
24  Some properties of Cards we need to determine category-wide (like card sizes in grid),
25  so we should not do them per-Card but in the category renderer.
26 
27  This component creates an invisible card filled with maximum mapped data and calculates
28  or measures card properties for this configuration.
29  */
30 
31 Item {
32  id: cardTool
33 
34  /*!
35  \brief Number of cards.
36  */
37  property int count
38 
39  /*!
40  \brief Width of the category view.
41  */
42  property real viewWidth
43 
44  /*!
45  \brief Scaling factor of selected Carousel item.
46  */
47  readonly property real carouselSelectedItemScaleFactor: 1.38 // XXX assuming 1.38 carousel scaling factor for cards
48 
49  /*!
50  \brief Template supplied for the category.
51  */
52  property var template
53 
54  /*!
55  \brief Component mapping supplied for the category.
56  */
57  property var components
58 
59  /*!
60  \brief The category layout for this card tool.
61  */
62  property string categoryLayout: {
63  var layout = template["category-layout"];
64 
65  // carousel fallback mode to grid
66  if (layout === "carousel" && count <= Math.ceil(carouselTool.realPathItemCount)) layout = "grid";
67  return layout;
68  }
69 
70 
71  // Not readonly because gets overwritten from GenericScopeView in some cases
72  property string artShapeStyle: categoryLayout === "carousel" ? "shadow" : "inset"
73 
74  // FIXME ? This seems like it should not be needed, but on Qt 5.4 + phone
75  // we are doing unneeded calls to getCardComponent with artShapeStyle and categoryLayout being empty
76  // Check when we move to newer Qts on the phone if we still need this
77  readonly property bool askForCardComponent: cardTool.template !== undefined &&
78  cardTool.components !== undefined &&
79  cardTool.artShapeStyle !== "" &&
80  cardTool.categoryLayout !== ""
81 
82  property var cardComponent: askForCardComponent
83  ? CardCreatorCache.getCardComponent(cardTool.template, cardTool.components, false, cardTool.artShapeStyle, cardTool.categoryLayout)
84  : undefined
85 
86  // FIXME: Saviq
87  // Only way for the card below to actually be laid out completely.
88  // If invisible or in "data" array, some components are not taken into account.
89  width: 0
90  height: 0
91  clip: true
92 
93  /*!
94  type:real \brief Width to be enforced on the card in this configuration.
95 
96  If -1, should use implicit width of the actual card.
97  */
98  property real cardWidth: {
99  switch (categoryLayout) {
100  case "grid":
101  case "vertical-journal":
102  var size = template["card-size"];
103  if (template["card-layout"] === "horizontal") size = "large";
104  switch (size) {
105  case "small": {
106  if (viewWidth <= units.gu(45)) return units.gu(12);
107  else return units.gu(14);
108  }
109  case "large": {
110  if (viewWidth >= units.gu(70)) return units.gu(42);
111  else return viewWidth - units.gu(2);
112  }
113  }
114  if (viewWidth <= units.gu(45)) return units.gu(18);
115  else if (viewWidth >= units.gu(70)) return units.gu(20);
116  else return units.gu(23);
117  case "carousel":
118  case "horizontal-list":
119  return carouselTool.minimumTileWidth;
120  case undefined:
121  case "organic-grid":
122  case "journal":
123  default:
124  return -1;
125  }
126  }
127 
128  /*!
129  type:real \brief Height to be enforced on the card in this configuration.
130 
131  If -1, should use implicit height of the actual card.
132  */
133  readonly property real cardHeight: {
134  switch (categoryLayout) {
135  case "journal":
136  if (template["card-size"] >= 12 && template["card-size"] <= 38) return units.gu(template["card-size"]);
137  return units.gu(18.5);
138  case "grid":
139  case "horizontal-list":
140  return cardLoader.item ? cardLoader.item.implicitHeight : 0
141  case "carousel":
142  return cardWidth / (components ? components["art"]["aspect-ratio"] : 1)
143  case undefined:
144  case "organic-grid":
145  case "vertical-journal":
146  default:
147  return -1;
148  }
149  }
150 
151  /*!
152  type:real \brief Height of the card's header.
153  */
154  readonly property int headerHeight: cardLoader.item ? cardLoader.item.headerHeight : 0
155  property size artShapeSize: cardLoader.item ? cardLoader.item.artShapeSize : Qt.size(0, 0)
156 
157  QtObject {
158  id: carouselTool
159 
160  property real minimumTileWidth: {
161  if (cardTool.viewWidth === undefined) return undefined;
162  if (cardTool.viewWidth <= units.gu(40)) return units.gu(18);
163  if (cardTool.viewWidth >= units.gu(128)) return units.gu(26);
164  return units.gu(18 + Math.round((cardTool.viewWidth - units.gu(40)) / units.gu(11)));
165  }
166 
167  readonly property real pathItemCount: 4.8457 /// (848 / 175) reference values
168 
169  property real realPathItemCount: {
170  var scaledMinimumTileWidth = minimumTileWidth / cardTool.carouselSelectedItemScaleFactor;
171  var tileWidth = Math.max(cardTool.viewWidth / pathItemCount, scaledMinimumTileWidth);
172  return Math.min(cardTool.viewWidth / tileWidth, pathItemCount);
173  }
174  }
175 
176  Item {
177  id: attributesModel
178  property int numOfAttributes: 0
179  property var model: []
180  readonly property bool hasAttributes: {
181  var attributes = components["attributes"];
182  var hasAttributesFlag = (attributes != undefined) && (attributes["field"] != undefined);
183 
184  if (hasAttributesFlag) {
185  if (attributes["max-count"]) {
186  numOfAttributes = attributes["max-count"];
187  }
188  }
189  return hasAttributesFlag
190  }
191 
192  onNumOfAttributesChanged: {
193  model = []
194  for (var i = 0; i < numOfAttributes; i++) {
195  model.push( {"value":"text"+(i+1), "icon":"image://theme/ok" } );
196  }
197  }
198  }
199 
200  Item {
201  id: socialActionsModel
202  property var model: []
203  readonly property bool hasActions: components["social-actions"] != undefined
204 
205  onHasActionsChanged: {
206  model = []
207  if (hasActions) {
208  model.push( {"id":"text", "icon":"image://theme/ok" } );
209  }
210  }
211  }
212 
213  Loader {
214  id: cardLoader
215  readonly property var cfields: ["art", "mascot", "title", "subtitle", "summary", "attributes", "social-actions"]
216  readonly property var dfields: ["art", "mascot", "title", "subtitle", "summary", "attributes", "socialActions"]
217  readonly property var maxData: {
218  "art": Qt.resolvedUrl("graphics/pixel.png"),
219  "mascot": Qt.resolvedUrl("graphics/pixel.png"),
220  "title": "—\n—",
221  "subtitle": "—",
222  "summary": "—\n—\n—\n—\n—",
223  "attributes": attributesModel.model,
224  "socialActions": socialActionsModel.model
225  }
226  sourceComponent: askForCardComponent
227  ? CardCreatorCache.getCardComponent(cardTool.template, cardTool.components, true, cardTool.artShapeStyle, cardTool.categoryLayout)
228  : undefined
229  onLoaded: {
230  item.objectName = "cardToolCard";
231  item.width = Qt.binding(function() { return cardTool.cardWidth !== -1 ? cardTool.cardWidth : item.implicitWidth; });
232  item.height = Qt.binding(function() { return cardTool.cardHeight !== -1 ? cardTool.cardHeight : item.implicitHeight; });
233  }
234  Connections {
235  target: cardTool
236  onTemplateChanged: cardLoader.updateCardData();
237  onComponentsChanged: cardLoader.updateCardData();
238  }
239  function updateCardData() {
240  var data = {};
241  for (var k in cfields) {
242  var ckey = cfields[k];
243  var component = cardTool.components[ckey];
244  if ((typeof component === "string" && component.length > 0) ||
245  (typeof component === "object" && component !== null
246  && typeof component["field"] === "string" && component["field"].length > 0)) {
247  var dkey = dfields[k];
248  data[dkey] = maxData[dkey];
249  }
250  }
251  item.cardData = data;
252  }
253  }
254 }