blob: b691acb299949cc4a6413723596286befa209dde [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001/*
2 * Copyright (C) 2019-2020 by Savoir-faire Linux
3 * Author: Yang Wang <yang.wang@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19import QtQuick 2.15
20import QtQuick.Window 2.15
21import QtQuick.Controls 2.15
22import QtQuick.Controls.Universal 2.12
23import QtQuick.Layouts 1.3
24import QtGraphicalEffects 1.14
25import QtQuick.Controls.Styles 1.4
26
27ComboBox {
28 id: control
29
30 delegate: ItemDelegate {
31 width: control.width
32 contentItem: Text {
33 text: {
34 var currentItem = control.delegateModel.items.get(index)
35 return currentItem.model[control.textRole].toString()
36 }
37 color: "black"
38 font: control.font
39 elide: Text.ElideRight
40 verticalAlignment: Text.AlignVCenter
41 }
42 highlighted: control.highlightedIndex === index
43 }
44
45 indicator: Canvas {
46 id: canvas
47 x: control.width - width - control.rightPadding
48 y: control.topPadding + (control.availableHeight - height) / 2
49 width: 12
50 height: 8
51 contextType: "2d"
52
53 Connections {
54 target: control
55 function onPressedChanged(){
56 canvas.requestPaint()
57 }
58 }
59
60 onPaint: {
61 context.reset();
62 context.moveTo(0, 0);
63 context.lineTo(width, 0);
64 context.lineTo(width / 2, height);
65 context.closePath();
66 context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";
67 context.fill();
68 }
69 }
70
71 contentItem: Text {
72 leftPadding: 0
73 rightPadding: control.indicator.width + control.spacing
74
75 text: control.displayText
76 font: control.font
77 color: "black"
78 verticalAlignment: Text.AlignVCenter
79 elide: Text.ElideRight
80 }
81
82 background: Rectangle {
83 implicitWidth: 120
84 implicitHeight: 40
85 border.color: "white"
86 border.width: control.visualFocus ? 2 : 1
87 radius: 2
88 }
89
90 popup: Popup {
91 y: control.height - 1
92 width: control.width
93 implicitHeight: contentItem.implicitHeight
94 padding: 1
95
96 contentItem: ListView {
97 clip: true
98 implicitHeight: contentHeight
99 model: control.delegateModel
100 currentIndex: control.highlightedIndex
101
102 ScrollIndicator.vertical: ScrollIndicator { }
103 }
104
105 background: Rectangle {
106 border.color: "gray"
107 radius: 2
108 }
109 }
110}