2020-06-17 19:31:01 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtQuick.Layouts 1.13
|
|
|
|
import QtGraphicalEffects 1.13
|
2020-06-10 14:11:06 +00:00
|
|
|
import "../imports"
|
|
|
|
|
|
|
|
Item {
|
2020-08-06 07:25:53 +00:00
|
|
|
enum MenuAlignment {
|
|
|
|
Left,
|
2020-08-13 08:24:51 +00:00
|
|
|
Right,
|
|
|
|
Center
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
2020-06-10 14:11:06 +00:00
|
|
|
property string label: ""
|
|
|
|
readonly property bool hasLabel: label !== ""
|
2020-07-22 20:16:06 +00:00
|
|
|
property color bgColor: Style.current.inputBackground
|
2020-06-10 14:11:06 +00:00
|
|
|
readonly property int labelMargin: 7
|
2020-07-30 05:18:54 +00:00
|
|
|
property var model
|
|
|
|
property alias menu: selectMenu
|
2020-08-03 05:36:54 +00:00
|
|
|
property color bgColorHover: bgColor
|
|
|
|
property alias selectedItemView: selectedItemContainer.children
|
|
|
|
property int caretRightMargin: Style.current.padding
|
2020-08-20 04:45:29 +00:00
|
|
|
property int caretLeftMargin: Style.current.halfPadding
|
2020-08-03 05:36:54 +00:00
|
|
|
property alias select: inputRectangle
|
2020-08-06 07:25:53 +00:00
|
|
|
property int menuAlignment: Select.MenuAlignment.Right
|
|
|
|
property Item zeroItemsView: Item {}
|
2020-08-20 04:45:29 +00:00
|
|
|
property int selectedItemRightMargin: caret.width + caretRightMargin + caretLeftMargin
|
|
|
|
property string validationError: ""
|
|
|
|
property alias validationErrorAlignment: validationErrorText.horizontalAlignment
|
|
|
|
property int validationErrorTopMargin: Style.current.halfPadding
|
2020-08-03 05:36:54 +00:00
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.right: parent.right
|
2020-06-10 14:11:06 +00:00
|
|
|
|
2020-07-30 05:18:54 +00:00
|
|
|
id: root
|
2020-08-20 04:45:29 +00:00
|
|
|
height: inputRectangle.height + (hasLabel ? inputLabel.height + labelMargin : 0) + (!!validationError ? (validationErrorText.height + validationErrorTopMargin) : 0)
|
2020-06-10 14:11:06 +00:00
|
|
|
|
2020-06-19 18:06:58 +00:00
|
|
|
StyledText {
|
2020-06-10 14:11:06 +00:00
|
|
|
id: inputLabel
|
2020-08-03 05:36:54 +00:00
|
|
|
visible: hasLabel
|
2020-07-30 05:18:54 +00:00
|
|
|
text: root.label
|
2020-06-10 14:11:06 +00:00
|
|
|
font.weight: Font.Medium
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.leftMargin: 0
|
|
|
|
anchors.top: parent.top
|
|
|
|
anchors.topMargin: 0
|
|
|
|
font.pixelSize: 13
|
2020-07-30 05:18:54 +00:00
|
|
|
height: 18
|
2020-06-10 14:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rectangle {
|
2020-08-03 05:36:54 +00:00
|
|
|
property bool hovered: false
|
2020-06-10 14:11:06 +00:00
|
|
|
id: inputRectangle
|
2020-08-03 05:36:54 +00:00
|
|
|
height: 56
|
|
|
|
color: hovered ? bgColorHover : bgColor
|
2020-07-22 20:16:06 +00:00
|
|
|
radius: Style.current.radius
|
2020-07-30 05:18:54 +00:00
|
|
|
anchors.top: root.hasLabel ? inputLabel.bottom : parent.top
|
|
|
|
anchors.topMargin: root.hasLabel ? root.labelMargin : 0
|
2020-06-10 14:11:06 +00:00
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.left: parent.left
|
2020-08-20 04:45:29 +00:00
|
|
|
border.width: !!validationError ? 1 : 0
|
|
|
|
border.color: Style.current.danger
|
2020-06-10 14:11:06 +00:00
|
|
|
|
2020-08-03 05:36:54 +00:00
|
|
|
Item {
|
|
|
|
id: selectedItemContainer
|
|
|
|
anchors.fill: parent
|
2020-06-26 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SVGImage {
|
|
|
|
id: caret
|
|
|
|
width: 11
|
|
|
|
height: 6
|
|
|
|
anchors.right: parent.right
|
2020-08-03 05:36:54 +00:00
|
|
|
anchors.rightMargin: caretRightMargin
|
2020-06-26 17:29:03 +00:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
fillMode: Image.PreserveAspectFit
|
|
|
|
source: "../app/img/caret.svg"
|
|
|
|
}
|
|
|
|
ColorOverlay {
|
|
|
|
anchors.fill: caret
|
|
|
|
source: caret
|
2020-07-30 05:18:54 +00:00
|
|
|
color: Style.current.secondaryText
|
2020-06-26 17:29:03 +00:00
|
|
|
}
|
2020-07-30 05:18:54 +00:00
|
|
|
}
|
2020-06-26 17:29:03 +00:00
|
|
|
|
2020-07-30 05:18:54 +00:00
|
|
|
// create a drop shadow externally so that it is not clipped by the
|
|
|
|
// rounded corners of the menu background
|
|
|
|
Rectangle {
|
|
|
|
width: selectMenu.width
|
|
|
|
height: selectMenu.height
|
|
|
|
x: selectMenu.x
|
|
|
|
y: selectMenu.y
|
|
|
|
visible: selectMenu.opened
|
|
|
|
color: Style.current.background
|
|
|
|
radius: Style.current.radius
|
|
|
|
border.color: Style.current.border
|
|
|
|
layer.enabled: true
|
|
|
|
layer.effect: DropShadow {
|
|
|
|
verticalOffset: 3
|
|
|
|
radius: Style.current.radius
|
|
|
|
samples: 15
|
|
|
|
fast: true
|
|
|
|
cached: true
|
|
|
|
color: "#22000000"
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 20:16:06 +00:00
|
|
|
|
2020-07-30 05:18:54 +00:00
|
|
|
Menu {
|
|
|
|
id: selectMenu
|
|
|
|
property var items: []
|
|
|
|
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
|
|
|
|
width: parent.width
|
|
|
|
background: Rectangle {
|
|
|
|
// do not add a drop shadow effect here or it will be clipped
|
|
|
|
radius: Style.current.radius
|
|
|
|
color: Style.current.background
|
|
|
|
}
|
|
|
|
clip: true
|
|
|
|
delegate: menuItem
|
2020-07-22 20:16:06 +00:00
|
|
|
|
2020-07-30 05:18:54 +00:00
|
|
|
Repeater {
|
|
|
|
id: menuItems
|
|
|
|
model: root.model
|
2020-08-06 07:25:53 +00:00
|
|
|
property int zeroItemsViewHeight
|
2020-07-30 05:18:54 +00:00
|
|
|
delegate: selectMenu.delegate
|
2020-08-06 07:25:53 +00:00
|
|
|
onItemAdded: {
|
|
|
|
root.zeroItemsView.visible = false
|
|
|
|
root.zeroItemsView.height = 0
|
|
|
|
}
|
|
|
|
onItemRemoved: {
|
|
|
|
if (count === 0) {
|
|
|
|
root.zeroItemsView.visible = true
|
|
|
|
root.zeroItemsView.height = zeroItemsViewHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Component.onCompleted: {
|
|
|
|
zeroItemsViewHeight = root.zeroItemsView.height
|
|
|
|
root.zeroItemsView.visible = count === 0
|
|
|
|
root.zeroItemsView.height = count !== 0 ? 0 : root.zeroItemsView.height
|
|
|
|
selectMenu.insertItem(0, root.zeroItemsView)
|
|
|
|
}
|
2020-06-10 14:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 04:45:29 +00:00
|
|
|
TextEdit {
|
|
|
|
id: validationErrorText
|
|
|
|
visible: !!validationError
|
|
|
|
text: validationError
|
|
|
|
anchors.top: inputRectangle.bottom
|
|
|
|
anchors.topMargin: validationErrorTopMargin
|
|
|
|
selectByMouse: true
|
|
|
|
readOnly: true
|
|
|
|
font.pixelSize: 12
|
|
|
|
height: 16
|
|
|
|
color: Style.current.danger
|
|
|
|
width: parent.width
|
|
|
|
horizontalAlignment: TextEdit.AlignRight
|
|
|
|
}
|
2020-06-10 14:11:06 +00:00
|
|
|
MouseArea {
|
|
|
|
id: mouseArea
|
2020-07-30 05:18:54 +00:00
|
|
|
anchors.fill: inputRectangle
|
|
|
|
cursorShape: Qt.PointingHandCursor
|
2020-08-03 05:36:54 +00:00
|
|
|
hoverEnabled: true
|
|
|
|
onEntered: {
|
|
|
|
inputRectangle.hovered = true
|
|
|
|
}
|
|
|
|
onExited: {
|
|
|
|
inputRectangle.hovered = false
|
|
|
|
}
|
2020-06-10 14:11:06 +00:00
|
|
|
onClicked: {
|
2020-07-30 05:18:54 +00:00
|
|
|
if (selectMenu.opened) {
|
|
|
|
selectMenu.close()
|
|
|
|
} else {
|
2020-08-06 07:25:53 +00:00
|
|
|
const rightOffset = inputRectangle.width - selectMenu.width
|
2020-08-13 08:24:51 +00:00
|
|
|
let offset = rightOffset
|
|
|
|
switch (root.menuAlignment) {
|
|
|
|
case Select.MenuAlignment.Left:
|
|
|
|
offset = 0
|
|
|
|
break
|
|
|
|
case Select.MenuAlignment.Right:
|
|
|
|
offset = rightOffset
|
|
|
|
break
|
|
|
|
case Select.MenuAlignment.Center:
|
|
|
|
offset = rightOffset / 2
|
|
|
|
}
|
2020-08-03 05:36:54 +00:00
|
|
|
selectMenu.popup(inputRectangle.x + offset, inputRectangle.y + inputRectangle.height + 8)
|
2020-07-30 05:18:54 +00:00
|
|
|
}
|
2020-06-10 14:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*##^##
|
|
|
|
Designer {
|
|
|
|
D{i:0;formeditorColor:"#ffffff";formeditorZoom:1.25}
|
|
|
|
}
|
|
|
|
##^##*/
|