Debian Codegraph @

src/GUI/qml/PopupChoice.qml

raw
1 import QtQuick 2.4
2 import FlightGear.Launcher 1.0
3 import FlightGear 1.0
4
5 Item {
6 id: root
7 property alias label: label.text
8
9 property var model: nil
10 property string displayRole: "display"
11 property bool enabled: true
12 property int currentIndex: 0
13 property string headerText: ""
14
15 implicitHeight: Math.max(label.implicitHeight, currentChoiceFrame.height)
16 implicitWidth: label.implicitWidth + (Style.margin * 2) + currentChoiceFrame.__naturalWidth
17
18 function select(index)
19 {
20 root.currentIndex = index;
21 }
22
23 ModelDataExtractor {
24 id: currentItemText
25 model: root.model
26 role: root.displayRole
27 index: root.currentIndex
28 }
29
30 function haveHeader()
31 {
32 return headerText !== "";
33 }
34
35 function currentText()
36 {
37 if ((currentIndex == -1) && haveHeader())
38 return headerText;
39
40 if (!currentItemText.data)
41 return "";
42
43 return currentItemText.data
44 }
45
46 StyledText {
47 id: label
48 anchors.left: root.left
49 anchors.leftMargin: Style.margin
50 anchors.verticalCenter: parent.verticalCenter
51 horizontalAlignment: Text.AlignRight
52 enabled: root.enabled
53 hover: mouseArea.containsMouse
54 }
55
56 Rectangle {
57 id: currentChoiceFrame
58 radius: Style.roundRadius
59 border.color: root.enabled ? (mouseArea.containsMouse ? Style.themeColor : Style.minorFrameColor)
60 : Style.disabledMinorFrameColor
61 border.width: 1
62 height: currentChoiceText.implicitHeight + Style.margin
63 clip: true
64 color: Style.backgroundColor
65
66 anchors.left: label.right
67 anchors.leftMargin: Style.margin
68
69 // width of current item, or available space after the label
70 width: Math.min(root.width - (label.width + Style.margin), __naturalWidth);
71
72 readonly property int __naturalWidth: currentChoiceText.implicitWidth + (Style.margin * 3) + upDownIcon.width
73 anchors.verticalCenter: parent.verticalCenter
74
75 StyledText {
76 enabled: root.enabled
77 id: currentChoiceText
78 anchors.verticalCenter: parent.verticalCenter
79 anchors.left: parent.left
80 anchors.right: parent.right
81 anchors.margins: Style.margin
82 text: currentText()
83 elide: Text.ElideRight
84 maximumLineCount: 1
85 }
86
87 Image {
88 id: upDownIcon
89 visible: root.enabled
90 source: mouseArea.containsMouse ? "image://colored-icon/up-down?theme" : "image://colored-icon/up-down?text"
91 anchors.right: parent.right
92 anchors.rightMargin: Style.margin
93 anchors.verticalCenter: parent.verticalCenter
94 }
95 }
96
97 MouseArea {
98 anchors.fill: parent
99 id: mouseArea
100 hoverEnabled: root.enabled
101 enabled: root.enabled
102 onClicked: {
103 OverlayShared.globalOverlay.showOverlayAtItemOffset(menu, root,
104 Qt.point(currentChoiceFrame.x, root.height))
105 }
106 }
107
108 Component {
109 id: menu
110 OverlayMenu {
111 currentIndex: root.currentIndex
112 model: root.model
113 headerText: root.headerText
114 onSelect: function doSelect(index) {
115 root.select(index);
116 }
117
118 displayRole: root.displayRole
119 }
120 }
121
122
123 }