| 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 |
Popover { |
| 23 |
id: comboBoxPopup |
| 24 |
|
| 25 |
implicitHeight: 5.5 * itemHeight |
| 26 |
contentHeight: Math.min(implicitHeight, listView.count * itemHeight) |
| 27 |
callerMargin: -units.gu(1) |
| 28 |
|
| 29 |
property var model |
| 30 |
property real itemHeight |
| 31 |
property real itemMargins |
| 32 |
property string textRole |
| 33 |
property ComboBox comboBox |
| 34 |
|
| 35 |
property bool square: true |
| 36 |
property real selectedItemY: currentIndex * itemHeight |
| 37 |
|
| 38 |
ScrollView { |
| 39 |
width: comboBoxPopup.contentWidth |
| 40 |
height: comboBoxPopup.contentHeight |
| 41 |
|
| 42 |
ListView { |
| 43 |
id: listView |
| 44 |
width: comboBoxPopup.contentWidth |
| 45 |
height: comboBoxPopup.contentHeight |
| 46 |
model: comboBoxPopup.model |
| 47 |
clip: true |
| 48 |
currentIndex: comboBoxPopup.comboBox.currentIndex |
| 49 |
delegate: MouseArea { |
| 50 |
id: mouseArea |
| 51 |
anchors { |
| 52 |
left: parent.left |
| 53 |
right: parent.right |
| 54 |
} |
| 55 |
height: comboBoxPopup.itemHeight |
| 56 |
onClicked: { |
| 57 |
comboBoxPopup.comboBox.currentIndex = index; |
| 58 |
PopupUtils.close(comboBoxPopup); |
| 59 |
} |
| 60 |
|
| 61 |
hoverEnabled: true |
| 62 |
|
| 63 |
Rectangle { |
| 64 |
visible: mouseArea.containsMouse |
| 65 |
anchors.fill: parent |
| 66 |
color: theme.palette.selected.overlay |
| 67 |
border.width: units.dp(1) |
| 68 |
border.color: Qt.darker(color, 1.02) |
| 69 |
antialiasing: true |
| 70 |
} |
| 71 |
|
| 72 |
Rectangle { |
| 73 |
visible: index == currentIndex |
| 74 |
x: comboBoxPopup.itemMargins / 2 - width / 2 |
| 75 |
anchors.verticalCenter: parent.verticalCenter |
| 76 |
width: units.gu(0.5) |
| 77 |
height: width |
| 78 |
color: theme.palette.selected.overlayText |
| 79 |
antialiasing: true |
| 80 |
radius: width/2 |
| 81 |
} |
| 82 |
|
| 83 |
Label { |
| 84 |
id: label |
| 85 |
anchors { |
| 86 |
fill: parent |
| 87 |
leftMargin: comboBoxPopup.itemMargins |
| 88 |
rightMargin: comboBoxPopup.itemMargins |
| 89 |
} |
| 90 |
text: textRole != "" ? (model[textRole] ? model[textRole] : modelData[textRole]) : modelData |
| 91 |
elide: Text.ElideRight |
| 92 |
verticalAlignment: Text.AlignVCenter |
| 93 |
color: index == currentIndex ? |
| 94 |
theme.palette.selected.overlayText |
| 95 |
: theme.palette.normal.overlayText |
| 96 |
|
| 97 |
Binding { |
| 98 |
target: label |
| 99 |
property: "font.family" |
| 100 |
value: comboBox.fontFamilyFromModel ? comboBox.fontFamilyFromModel(label.text) : undefined |
| 101 |
when: typeof comboBox.fontFamilyFromModel !== "undefined" |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |