Debian Codegraph @

qml/components/OptionsOverlay.qml

raw
1 /*
2 * Copyright 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.12
18 import QtQuick.Window 2.2
19 import Lomiri.Components 1.3
20
21 Item {
22 id: optionsOverlay
23
24 property list<ListModel> options
25 property bool valueSelectorOpened: optionValueSelector.caller != null
26
27 function closeValueSelector() {
28 optionValueSelector.hide();
29 }
30
31 height: optionsGrid.height + optionsGrid.rowSpacing
32
33 Grid {
34 id: optionsGrid
35 anchors {
36 horizontalCenter: parent.horizontalCenter
37 }
38
39 columns: 3
40 columnSpacing: units.gu(9.5)
41 rowSpacing: units.gu(3)
42
43 Repeater {
44 model: optionsOverlay.options
45 delegate: OptionButton {
46 id: optionButton
47 model: modelData
48 onClicked: optionValueSelector.toggle(model, optionButton)
49 enabled: model.available && (!optionValueSelector.caller || optionValueSelector.caller == optionButton)
50 opacity: enabled ? 1.0 : 0.05
51 Behavior on opacity {LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}}
52 customRotation: main.orientedRotationAngle
53 }
54 }
55 }
56
57 ListView {
58 id: optionValueSelector
59 objectName: "optionValueSelector"
60 height: Math.min( contentHeight, screenHeight / 2 )
61 snapMode: ListView.SnapToItem
62 clip: true
63 interactive: true
64 flickableDirection: Flickable.VerticalFlick
65 boundsBehavior: Flickable.DragAndOvershootBounds
66 rotation: main.orientedRotationAngle
67
68 Behavior on rotation {
69 RotationAnimator {
70 duration: LomiriAnimation.BriskDuration
71 easing: LomiriAnimation.StandardEasing
72 direction: RotationAnimator.Shortest
73 }
74 }
75
76 onRotationChanged: if (caller) { alignWith(caller); }
77
78 property OptionButton caller
79 property int screenHeight: ((Screen.orientation == Qt.PortraitOrientation || Screen.orientation == Qt.InvertedPortraitOrientation)
80 ? main.sensorHasDifferentOrientation ? Screen.width : Screen.height
81 : main.sensorHasDifferentOrientation ? Screen.height : Screen.width
82 )
83
84 function toggle(model, callerButton) {
85 if (optionValueSelectorVisible && optionValueSelector.model === model) {
86 hide();
87 } else {
88 show(model, callerButton);
89 }
90 }
91
92 function show(model, callerButton) {
93 optionValueSelector.caller = callerButton;
94 optionValueSelector.model = model;
95 alignWith(callerButton);
96 optionValueSelectorVisible = true;
97 positionViewAtIndex(model.selectedIndex, ListView.Contain);
98
99 }
100
101 function hide() {
102 optionValueSelectorVisible = false;
103 optionValueSelector.caller = null;
104 }
105
106 function alignWith(item) {
107 if(model) {
108 forceLayout();
109 width = contentItem.childrenRect.width;
110 }
111 // horizontally center optionValueSelector with the center of item
112 // if there is enough space to do so, that is as long as optionValueSelector
113 // does not get cropped by the edge of the screen
114 var itemX = parent.mapFromItem(item, 0, 0).x;
115 var centeredX = itemX + item.width / 2.0 - width / 2.0;
116 var margin = units.gu(1);
117 var offScreenToTheLeft = main.sensorHasDifferentOrientation ? centeredX + width / 2.0 - height / 2.0 < margin
118 : centeredX < margin;
119 var offScreenToTheRight = main.sensorHasDifferentOrientation ? centeredX + width / 2.0 + height / 2.0 > optionsOverlay.width - margin
120 : centeredX + width > optionsOverlay.width - margin;
121 var offScreenOffset = itemX + item.width - width;
122
123 if (offScreenToTheLeft) {
124 x = itemX;
125 } else if (offScreenToTheRight) {
126 x = offScreenOffset;
127 } else {
128 x = centeredX;
129 }
130
131 // vertically position the options above the caller button
132 y = Qt.binding(function() { return Math.max(-(screenHeight - (optionsOverlay.height - item.y) - units.gu(2))
133 , optionsGrid.y + item.y - (main.sensorHasDifferentOrientation ? height / 2 + width: height) - units.gu(2)) });
134 }
135
136 visible: opacity !== 0.0
137 onVisibleChanged: if (!visible) model = null;
138 opacity: optionValueSelectorVisible ? 1.0 : 0.0
139 Behavior on opacity {LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}}
140
141 Connections {
142 target: parent
143 onWidthChanged: {
144 console.log("Flipped");
145 if(optionValueSelector.caller) { optionValueSelector.alignWith(optionValueSelector.caller); }
146 }
147 }
148
149 headerPositioning: ListView.OverlayHeader
150 header: Icon {
151 anchors.horizontalCenter: parent.horizontalCenter
152 height: units.gu(3)
153 visible: !optionValueSelector.atYBeginning
154 name:"go-up"
155 }
156
157 delegate: OptionValueButton {
158 anchors.left: parent.left
159 columnWidth: optionValueSelector.width
160
161 label: (model && model.label) ? i18n.tr(model.label) : ""
162
163 iconName: (model && model.icon) ? model.icon : ""
164 selected: optionValueSelector.model.selectedIndex == index
165 Component.onCompleted: {
166 if (model && model.iconSource) {
167 iconSource = model.iconSource
168 }
169 }
170
171 isLast: index === optionValueSelector.count - 1
172 onClicked: {
173 if (optionValueSelector.model.setSettingProperty) {
174 optionValueSelector.model.setSettingProperty(optionValueSelector.model.get(index).value);
175 } else {
176 settings[optionValueSelector.model.settingsProperty] = optionValueSelector.model.get(index).value;
177 }
178 }
179 }
180
181 footerPositioning: ListView.OverlayFooter
182 footer: Icon {
183 anchors.horizontalCenter: parent.horizontalCenter
184 height: units.gu(3)
185 visible: !optionValueSelector.atYEnd
186 name:"go-down"
187 }
188 }
189 }