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.0
18 import Ubuntu.Components 0.1
19 
20 /*! \brief This component constructs the Preview UI.
21  *
22  * Currently it displays all the widgets in a flickable column.
23  */
24 
25 Item {
26  id: root
27 
28  /*! \brief Model containing preview widgets.
29  *
30  * The model should expose "widgetId", "type" and "properties" roles, as well as
31  * have a triggered(QString widgetId, QString actionId, QVariantMap data) method,
32  * that's called when actions are executed in widgets.
33  */
34  property var previewModel
35 
36  //! \brief Should be set to true if this preview is currently displayed.
37  property bool isCurrent: false
38 
39  //! \brief The ScopeStyle component.
40  property var scopeStyle: null
41 
42  clip: true
43 
44  Binding {
45  target: previewModel
46  property: "widgetColumnCount"
47  value: row.columns
48  }
49 
50  MouseArea {
51  anchors.fill: parent
52  }
53 
54  Row {
55  id: row
56 
57  spacing: units.gu(1)
58  anchors { fill: parent; margins: spacing }
59 
60  property int columns: width >= units.gu(80) ? 2 : 1
61  property real columnWidth: width / columns
62 
63  Repeater {
64  model: previewModel
65 
66  delegate: ListView {
67  id: column
68  objectName: "previewListRow" + index
69  anchors {
70  top: parent.top
71  bottom: parent.bottom
72  }
73  width: row.columnWidth
74  spacing: row.spacing
75  bottomMargin: Qt.inputMethod.visible ? Qt.inputMethod.keyboardRectangle.height : 0
76  property var makeSureVisibleItem
77  property real previousVisibleHeight: 0
78  property real visibleHeight: height - bottomMargin
79  onVisibleHeightChanged: {
80  if (makeSureVisibleItem && makeSureVisibleItem.activeFocus && previousVisibleHeight > visibleHeight) {
81  var textAreaPos = makeSureVisibleItem.mapToItem(column, 0, 0);
82  if (textAreaPos.y + makeSureVisibleItem.height > column.visibleHeight) {
83  column.contentY += textAreaPos.y + makeSureVisibleItem.height - column.visibleHeight
84  }
85  }
86  previousVisibleHeight = visibleHeight;
87  }
88 
89  model: columnModel
90  cacheBuffer: height
91 
92  Behavior on contentY { UbuntuNumberAnimation { } }
93 
94  delegate: PreviewWidgetFactory {
95  widgetId: model.widgetId
96  widgetType: model.type
97  widgetData: model.properties
98  isCurrentPreview: root.isCurrent
99  scopeStyle: root.scopeStyle
100  anchors {
101  left: parent.left
102  right: parent.right
103  leftMargin: units.gu(1)
104  rightMargin: units.gu(1)
105  }
106 
107  onTriggered: {
108  previewModel.triggered(widgetId, actionId, data);
109  }
110  onMakeSureVisible: {
111  column.previousVisibleHeight=column.visibleHeight
112  column.makeSureVisibleItem=item
113  }
114 
115  onFocusChanged: if (focus) column.positionViewAtIndex(index, ListView.Contain)
116 
117  onHeightChanged: if (focus) column.positionViewAtIndex(index, ListView.Contain)
118  }
119  }
120  }
121  }
122 }