Warning, /pim/kube/framework/qml/EntityComboBox.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  Copyright (C) 2018 Michael Bohlender, <bohlender@kolabsys.com>
0003  *  Copyright (C) 2019 Christian Mollekopf, <mollekopf@kolabsys.com>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License along
0016  *  with this program; if not, write to the Free Software Foundation, Inc.,
0017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 import QtQuick 2.4
0021 
0022 import org.kube.framework 1.0 as Kube
0023 
0024 Kube.ComboBox {
0025     id: root
0026 
0027     property alias accountId: entityModel.accountId
0028     property alias type: entityModel.type
0029     property alias filter: entityModel.filter
0030     property var initialSelection: null
0031     property var initialSelectionObject: null
0032 
0033     signal selected(var entity, var identifier)
0034 
0035     model: Kube.EntityModel {
0036         id: entityModel
0037         roles: ["name", "color"]
0038         sortRole: "name"
0039         onInitialItemsLoaded: {
0040             if (root.initialSelection) {
0041                 var foundIndex = entityModel.findIndex("identifier", root.initialSelection)
0042                 if (foundIndex >= 0) {
0043                     root.currentIndex = foundIndex;
0044                 }
0045             }
0046             if (root.initialSelectionObject) {
0047                 var foundIndex =  entityModel.findIndex("object", root.initialSelectionObject)
0048                 if (foundIndex >= 0) {
0049                     root.currentIndex = foundIndex;
0050                 }
0051             }
0052             if (root.currentIndex >= 0) {
0053                 //Set initial selection.
0054                 //onCurrentIndexChanged will not work because the as more items are added the currentIndex changes,
0055                 //but depending on the sorting it will point to a different item (Which is really a bug of the model or ComboBox).
0056                 var index = entityModel.data(currentIndex)
0057                 root.selected(index.object, index.identifier)
0058             }
0059         }
0060     }
0061 
0062     textRole: "name"
0063 
0064     onCurrentIndexChanged: {
0065         if (currentIndex >= 0) {
0066             var index = entityModel.data(currentIndex)
0067             root.selected(index.object, index.identifier)
0068         }
0069     }
0070 
0071     delegate: Kube.ListDelegate {
0072         width: root.popup.width
0073         height: Kube.Units.gridUnit * 1.5
0074 
0075         contentItem: Row {
0076             Item {
0077                 width: Kube.Units.smallSpacing
0078                 height: parent.height
0079             }
0080             Rectangle {
0081                 visible: !!model.color
0082                 anchors.verticalCenter: parent.verticalCenter
0083                 width: Kube.Units.gridUnit
0084                 height: Kube.Units.gridUnit
0085                 radius: Kube.Units.gridUnit / 2
0086                 color: !!model.color ? model.color : "blue"
0087             }
0088             Kube.Label {
0089                 padding: Kube.Units.smallSpacing
0090                 text: model[root.textRole]
0091                 color:  root.highlightedIndex === index ? Kube.Colors.highlightedTextColor : Kube.Colors.textColor
0092             }
0093         }
0094 
0095         MouseArea {
0096             anchors.fill: parent
0097 
0098             onClicked: {
0099                 root.currentIndex = root.highlightedIndex
0100                 root.popup.close()
0101             }
0102         }
0103     }
0104 }