| 1 |
/* |
| 2 |
* Copyright (C) 2016 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 version 3 as |
| 6 |
* published by the Free Software Foundation. |
| 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 |
* Authored-by: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
import QtQuick 2.4 |
| 19 |
import Lomiri.Components 1.3 |
| 20 |
import Lomiri.Components.Popups 1.3 |
| 21 |
|
| 22 |
MouseArea { |
| 23 |
id: comboBox |
| 24 |
|
| 25 |
property var model |
| 26 |
property int currentIndex: 0 |
| 27 |
property var bindingTarget |
| 28 |
property string bindingProperty |
| 29 |
readonly property var currentItem: model.get ? model.get(currentIndex) |
| 30 |
: model[currentIndex] |
| 31 |
readonly property string currentText: textRole ? currentItem[textRole] : currentItem |
| 32 |
property string textRole |
| 33 |
property string valueRole |
| 34 |
|
| 35 |
implicitWidth: units.gu(21) |
| 36 |
implicitHeight: units.gu(4) |
| 37 |
|
| 38 |
DoubleBinding { |
| 39 |
source: bindingTarget |
| 40 |
sourceProperty: bindingProperty |
| 41 |
target: comboBox |
| 42 |
targetProperty: "currentIndex" |
| 43 |
|
| 44 |
function listModelIndexOf(listModel, value, role) { |
| 45 |
for (var i = 0; i < listModel.count; i++) { |
| 46 |
if (listModel.get(i)[role] === value) { |
| 47 |
return i; |
| 48 |
} |
| 49 |
} |
| 50 |
return -1; |
| 51 |
} |
| 52 |
|
| 53 |
function jsListIndexOf(listModel, value, role) { |
| 54 |
if (role == "") { |
| 55 |
return listModel.indexOf(value); |
| 56 |
} |
| 57 |
|
| 58 |
for (var i = 0; i < listModel.length; i++) { |
| 59 |
if (listModel[i][role] === value) { |
| 60 |
return i; |
| 61 |
} |
| 62 |
} |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
function sourceToTarget(sourceValue) { |
| 67 |
return comboBox.model.get ? listModelIndexOf(comboBox.model, sourceValue, comboBox.valueRole) |
| 68 |
: jsListIndexOf(comboBox.model, sourceValue, comboBox.valueRole); |
| 69 |
} |
| 70 |
function targetToSource(currentIndex) { |
| 71 |
return comboBox.valueRole ? comboBox.currentItem[comboBox.valueRole] : comboBox.currentItem; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
Rectangle { |
| 76 |
anchors.fill: parent |
| 77 |
|
| 78 |
color: theme.palette.normal.field |
| 79 |
border.color: theme.palette.normal.base |
| 80 |
border.width: units.dp(1) |
| 81 |
radius: units.dp(3) |
| 82 |
|
| 83 |
Icon { |
| 84 |
anchors { |
| 85 |
right: parent.right |
| 86 |
rightMargin: units.gu(2) |
| 87 |
verticalCenter: parent.verticalCenter |
| 88 |
verticalCenterOffset: -units.gu(0.5) |
| 89 |
} |
| 90 |
|
| 91 |
width: units.gu(1) |
| 92 |
height: units.gu(1) |
| 93 |
asynchronous: true |
| 94 |
name: "up" |
| 95 |
} |
| 96 |
|
| 97 |
Icon { |
| 98 |
anchors { |
| 99 |
right: parent.right |
| 100 |
rightMargin: units.gu(2) |
| 101 |
verticalCenter: parent.verticalCenter |
| 102 |
verticalCenterOffset: units.gu(0.5) |
| 103 |
} |
| 104 |
|
| 105 |
width: units.gu(1) |
| 106 |
height: units.gu(1) |
| 107 |
asynchronous: true |
| 108 |
name: "down" |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
Label { |
| 113 |
id: currentLabel |
| 114 |
anchors { |
| 115 |
fill: parent |
| 116 |
leftMargin: units.gu(2) |
| 117 |
rightMargin: units.gu(5) |
| 118 |
} |
| 119 |
|
| 120 |
text: comboBox.currentText |
| 121 |
elide: Text.ElideRight |
| 122 |
verticalAlignment: Text.AlignVCenter |
| 123 |
color: theme.palette.normal.overlayText |
| 124 |
} |
| 125 |
|
| 126 |
Binding { |
| 127 |
target: currentLabel |
| 128 |
property: "font.family" |
| 129 |
value: comboBox.fontFamilyFromModel ? comboBox.fontFamilyFromModel(comboBox.currentText) : undefined |
| 130 |
when: typeof comboBox.fontFamilyFromModel !== "undefined" |
| 131 |
} |
| 132 |
|
| 133 |
onWheel: { |
| 134 |
var count = comboBox.model.length ? comboBox.model.length : |
| 135 |
comboBox.model.count ? comboBox.model.count : Infinity |
| 136 |
if (wheel.angleDelta.y > 0 && comboBox.currentIndex > 0) { |
| 137 |
comboBox.currentIndex--; |
| 138 |
} else if (wheel.angleDelta.y < 0 && comboBox.currentIndex < count - 1) { |
| 139 |
comboBox.currentIndex++; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
preventStealing: true |
| 144 |
onPressed: { |
| 145 |
var properties = { |
| 146 |
"model": comboBox.model, |
| 147 |
"itemHeight": comboBox.height, |
| 148 |
"itemMargins": currentLabel.anchors.leftMargin, |
| 149 |
"contentWidth": comboBox.width, |
| 150 |
"comboBox": comboBox, |
| 151 |
"textRole": comboBox.textRole |
| 152 |
} |
| 153 |
|
| 154 |
PopupUtils.open(Qt.resolvedUrl("ComboBoxPopup.qml"), comboBox, properties); |
| 155 |
} |
| 156 |
} |