2020-10-20 15:32:03 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtGraphicalEffects 1.12
|
|
|
|
import QtQuick.Dialogs 1.3
|
2020-12-28 20:03:57 +00:00
|
|
|
import QtQuick.Layouts 1.13
|
2020-10-20 15:32:03 +00:00
|
|
|
import "../../imports"
|
|
|
|
import "../../shared"
|
|
|
|
|
|
|
|
Popup {
|
|
|
|
property var modelList
|
|
|
|
property alias listView: listView
|
2020-12-28 20:03:57 +00:00
|
|
|
property var getImageSource
|
|
|
|
property var getImageComponent
|
2020-10-20 15:32:03 +00:00
|
|
|
property var getText: function () {}
|
|
|
|
property var onClicked: function () {}
|
2020-11-17 03:07:01 +00:00
|
|
|
property int imageWidth: 22
|
|
|
|
property int imageHeight: 22
|
2020-12-28 20:03:57 +00:00
|
|
|
property string title
|
|
|
|
property bool showSearchBox: false
|
2020-10-20 15:32:03 +00:00
|
|
|
|
|
|
|
function openPopup(listParam) {
|
|
|
|
modelList = listParam
|
|
|
|
popup.open()
|
|
|
|
}
|
|
|
|
|
2020-12-28 20:03:57 +00:00
|
|
|
onOpened: {
|
|
|
|
listView.currentIndex = 0
|
|
|
|
if (showSearchBox) {
|
2021-01-05 18:54:17 +00:00
|
|
|
searchBox.text = ""
|
2020-12-28 20:03:57 +00:00
|
|
|
searchBox.textField.forceActiveFocus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:32:03 +00:00
|
|
|
id: popup
|
2020-12-28 20:03:57 +00:00
|
|
|
padding: Style.current.smallPadding
|
2020-10-20 15:32:03 +00:00
|
|
|
width: messageInput.width
|
2020-12-28 20:03:57 +00:00
|
|
|
height: {
|
|
|
|
let possibleHeight = listView.contentHeight + Style.current.smallPadding * 2
|
|
|
|
if (popupTitle.visible) {
|
|
|
|
possibleHeight += popupTitle.height + Style.current.smallPadding
|
|
|
|
}
|
|
|
|
if (searchBox.visible) {
|
|
|
|
possibleHeight += searchBox.height + Style.current.smallPadding
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.min(400, possibleHeight)
|
|
|
|
}
|
|
|
|
x: messageInput.x
|
2020-10-20 15:32:03 +00:00
|
|
|
y: -height
|
|
|
|
background: Rectangle {
|
|
|
|
id: bgRectangle
|
2020-12-28 20:03:57 +00:00
|
|
|
visible: !!popup.title || (!!popup.modelList && popup.modelList.length > 0)
|
2020-10-20 15:32:03 +00:00
|
|
|
color: Style.current.background
|
|
|
|
border.width: 0
|
|
|
|
radius: Style.current.radius
|
|
|
|
layer.enabled: true
|
|
|
|
layer.effect: DropShadow{
|
|
|
|
width: bgRectangle.width
|
|
|
|
height: bgRectangle.height
|
|
|
|
x: bgRectangle.x
|
|
|
|
y: bgRectangle.y + 10
|
|
|
|
visible: bgRectangle.visible
|
|
|
|
source: bgRectangle
|
|
|
|
horizontalOffset: 0
|
|
|
|
verticalOffset: 2
|
|
|
|
radius: 10
|
|
|
|
samples: 15
|
|
|
|
color: "#22000000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 20:03:57 +00:00
|
|
|
StyledText {
|
|
|
|
id: popupTitle
|
|
|
|
visible: !!popup.title
|
|
|
|
height: visible ? implicitHeight : 0
|
|
|
|
text: popup.title
|
|
|
|
font.pixelSize: 17
|
|
|
|
anchors.top: parent.top
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchBox {
|
|
|
|
id: searchBox
|
|
|
|
visible: showSearchBox
|
|
|
|
height: visible ? implicitHeight : 0
|
|
|
|
width: parent.width
|
|
|
|
anchors.top: popupTitle.bottom
|
|
|
|
anchors.topMargin: popupTitle.visible ? Style.current.smallPadding : 0
|
|
|
|
|
|
|
|
function goToNextAvailableIndex(up) {
|
|
|
|
do {
|
|
|
|
if (!up && listView.currentIndex === listView.count - 1) {
|
|
|
|
listView.currentIndex = 0
|
|
|
|
return
|
|
|
|
} else if (up && listView.currentIndex === 0) {
|
|
|
|
listView.currentIndex = listView.count - 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up) {
|
|
|
|
listView.decrementCurrentIndex()
|
|
|
|
} else {
|
|
|
|
listView.incrementCurrentIndex()
|
|
|
|
}
|
|
|
|
} while (!listView.currentItem.visible)
|
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onReleased: function onKeyPress(event) {
|
|
|
|
if (event.key === Qt.Key_Down) {
|
|
|
|
searchBox.goToNextAvailableIndex(false)
|
|
|
|
}
|
|
|
|
if (event.key === Qt.Key_Up) {
|
|
|
|
searchBox.goToNextAvailableIndex(true)
|
|
|
|
}
|
|
|
|
if (event.key === Qt.Key_Escape) {
|
|
|
|
return popup.close()
|
|
|
|
}
|
|
|
|
if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
|
|
|
|
return popup.onClicked(listView.currentIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:32:03 +00:00
|
|
|
ListView {
|
|
|
|
id: listView
|
|
|
|
model: popup.modelList || []
|
|
|
|
keyNavigationEnabled: true
|
2020-12-28 20:03:57 +00:00
|
|
|
Layout.fillHeight: true
|
|
|
|
width: parent.width
|
|
|
|
anchors.top: searchBox.bottom
|
|
|
|
anchors.topMargin: searchBox.visible ? Style.current.smallPadding : 0
|
|
|
|
anchors.bottom: parent.bottom
|
2020-10-20 15:32:03 +00:00
|
|
|
clip: true
|
|
|
|
|
|
|
|
delegate: Rectangle {
|
2020-12-28 20:03:57 +00:00
|
|
|
property string myText: {
|
|
|
|
if (typeof modelData === "undefined") {
|
|
|
|
return popup.getText(model)
|
|
|
|
}
|
|
|
|
return popup.getText(modelData)
|
|
|
|
}
|
2020-10-20 15:32:03 +00:00
|
|
|
id: rectangle
|
2020-12-28 20:03:57 +00:00
|
|
|
visible: searchBox.text === "" || myText.includes(searchBox.text)
|
2020-10-20 15:32:03 +00:00
|
|
|
color: listView.currentIndex === index ? Style.current.backgroundHover : Style.current.transparent
|
|
|
|
border.width: 0
|
|
|
|
width: parent.width
|
2020-12-28 20:03:57 +00:00
|
|
|
height: visible ? 42 : 0
|
2020-10-20 15:32:03 +00:00
|
|
|
radius: Style.current.radius
|
|
|
|
|
2020-12-28 20:03:57 +00:00
|
|
|
Loader {
|
|
|
|
id: imageLoader
|
|
|
|
active: !!popup.getImageComponent || !!popup.getImageSource
|
2020-11-17 03:07:01 +00:00
|
|
|
width: popup.imageWidth
|
|
|
|
height: popup.imageHeight
|
2020-10-20 15:32:03 +00:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.leftMargin: Style.current.smallPadding
|
2020-12-28 20:03:57 +00:00
|
|
|
sourceComponent: popup.getImageComponent ? customImageComponent : normalImageComponent
|
|
|
|
}
|
|
|
|
|
|
|
|
Component {
|
|
|
|
id: customImageComponent
|
|
|
|
Item {
|
|
|
|
id: imageComponentContainer
|
|
|
|
children: {
|
|
|
|
if (!popup.getImageComponent) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if (typeof modelData === "undefined") {
|
|
|
|
return popup.getImageComponent(imageComponentContainer, model)
|
|
|
|
}
|
|
|
|
return popup.getImageComponent(imageComponentContainer, modelData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Component {
|
|
|
|
id: normalImageComponent
|
|
|
|
SVGImage {
|
|
|
|
visible: !!source
|
|
|
|
width: popup.imageWidth
|
|
|
|
height: popup.imageHeight
|
|
|
|
source: {
|
|
|
|
if (!popup.getImageSource) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if (typeof modelData === "undefined") {
|
|
|
|
return popup.getImageSource(model)
|
|
|
|
}
|
|
|
|
return popup.getImageSource(modelData)
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 15:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StyledText {
|
2020-12-28 20:03:57 +00:00
|
|
|
text: rectangle.myText
|
2020-10-20 15:32:03 +00:00
|
|
|
color: Style.current.textColor
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
2020-12-28 20:03:57 +00:00
|
|
|
anchors.left: imageLoader.right
|
2020-10-20 15:32:03 +00:00
|
|
|
anchors.leftMargin: Style.current.smallPadding
|
|
|
|
font.pixelSize: 15
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
cursorShape: Qt.PointingHandCursor
|
|
|
|
anchors.fill: parent
|
|
|
|
hoverEnabled: true
|
|
|
|
onEntered: {
|
|
|
|
listView.currentIndex = index
|
|
|
|
}
|
|
|
|
onClicked: {
|
|
|
|
popup.onClicked(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|