Unity 8
Preview.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 "../../Components"
20 
21 /*! \brief This component constructs the Preview UI.
22  *
23  * Currently it displays all the widgets in a flickable column.
24  */
25 
26 Item {
27  id: root
28 
29  /*! \brief Model containing preview widgets.
30  *
31  * The model should expose "widgetId", "type" and "properties" roles, as well as
32  * have a triggered(QString widgetId, QString actionId, QVariantMap data) method,
33  * that's called when actions are executed in widgets.
34  */
35  property var previewModel
36 
37  //! \brief The ScopeStyle component.
38  property var scopeStyle: null
39 
40  //! Should the orientation be locked
41  property int orientationLockCount: 0
42 
43  clip: true
44 
45  Binding {
46  target: previewModel
47  property: "widgetColumnCount"
48  value: row.columns
49  when: root.orientationLockCount === 0
50  }
51 
52  MouseArea {
53  anchors.fill: parent
54  }
55 
56  Row {
57  id: row
58 
59  spacing: units.gu(4)
60  anchors.fill: parent
61 
62  readonly property int columns: width >= units.gu(80) ? 2 : 1
63  readonly property real columnWidth: (width - (spacing * (columns - 1))) / columns
64  readonly property int singleColumnMargin: units.gu(2)
65 
66  Repeater {
67  model: previewModel
68 
69  delegate: ListView {
70  id: column
71  objectName: "previewListRow" + index
72  anchors {
73  top: parent.top
74  bottom: parent.bottom
75  }
76  topMargin: units.gu(2)
77  width: row.columnWidth
78  spacing: units.gu(1)
79 
80  readonly property int columnNumber: index
81 
82  ListViewOSKScroller {
83  id: oskScroller
84  list: column
85  }
86 
87  model: columnModel
88  cacheBuffer: height
89  highlightMoveDuration: 0 // QTBUG-53460
90 
91  Behavior on contentY { UbuntuNumberAnimation { } }
92 
93  delegate: PreviewWidgetFactory {
94  widgetId: model.widgetId
95  widgetType: model.type
96  widgetData: model.properties
97  scopeStyle: root.scopeStyle
98  parentFlickable: column
99 
100  anchors {
101  left: parent.left
102  right: parent.right
103  leftMargin: if (row.columns == 1) {
104  return singleColumnMarginless ? 0 : row.singleColumnMargin;
105  } else {
106  return column.columnNumber == 0 ? row.singleColumnMargin : 0;
107  }
108  rightMargin: if (row.columns == 1) {
109  return singleColumnMarginless ? 0 : row.singleColumnMargin;
110  } else {
111  return column.columnNumber == 1 ? row.singleColumnMargin : 0;
112  }
113  }
114 
115  onTriggered: {
116  previewModel.triggered(widgetId, actionId, data);
117  }
118 
119  onMakeSureVisible: {
120  oskScroller.setMakeSureVisibleItem(item);
121  }
122 
123  onFocusChanged: if (focus) column.positionViewAtIndex(index, ListView.Contain)
124 
125  onHeightChanged: if (focus) {
126  column.forceLayout();
127  column.positionViewAtIndex(index, ListView.Contain)
128  }
129 
130  onOrientationLockChanged: {
131  if (orientationLock)
132  root.orientationLockCount++;
133  else
134  root.orientationLockCount = Math.max(0, root.orientationLockCount--);
135  }
136 
137  Component.onDestruction: {
138  if (orientationLock)
139  root.orientationLockCount = Math.max(0, root.orientationLockCount--);
140  }
141  }
142  }
143  }
144  }
145 }