|
| 1 | +import QtQuick 2.15 |
| 2 | +import QtGraphicalEffects 1.15 |
| 3 | +import QtQuick.Templates 2.15 as T |
| 4 | + |
| 5 | +T.ToolTip { |
| 6 | + id: control |
| 7 | + |
| 8 | + enum Position |
| 9 | + { |
| 10 | + Position_Top = 0, |
| 11 | + Position_Bottom = 1, |
| 12 | + Position_Left = 2, |
| 13 | + Position_Right = 3 |
| 14 | + } |
| 15 | + |
| 16 | + property bool animationEnabled: true |
| 17 | + property bool arrowVisible: false |
| 18 | + property int position: DelToolTip.Position_Top |
| 19 | + property color colorText: "#000" |
| 20 | + property color colorBg: "#fff" |
| 21 | + |
| 22 | + component Arrow: Canvas { |
| 23 | + onWidthChanged: requestPaint(); |
| 24 | + onHeightChanged: requestPaint(); |
| 25 | + onColorBgChanged: requestPaint(); |
| 26 | + onPaint: { |
| 27 | + var ctx = getContext("2d"); |
| 28 | + ctx.fillStyle = colorBg; |
| 29 | + ctx.beginPath(); |
| 30 | + switch (position) { |
| 31 | + case DelToolTip.Position_Top: { |
| 32 | + ctx.moveTo(0, 0); |
| 33 | + ctx.lineTo(width, 0); |
| 34 | + ctx.lineTo(width * 0.5, height); |
| 35 | + } break; |
| 36 | + case DelToolTip.Position_Bottom: { |
| 37 | + ctx.moveTo(0, height); |
| 38 | + ctx.lineTo(width, height); |
| 39 | + ctx.lineTo(width * 0.5, 0); |
| 40 | + } break; |
| 41 | + case DelToolTip.Position_Left: { |
| 42 | + ctx.moveTo(0, 0); |
| 43 | + ctx.lineTo(0, height); |
| 44 | + ctx.lineTo(width, height * 0.5); |
| 45 | + } break; |
| 46 | + case DelToolTip.Position_Right: { |
| 47 | + ctx.moveTo(width, 0); |
| 48 | + ctx.lineTo(width, height); |
| 49 | + ctx.lineTo(0, height * 0.5); |
| 50 | + } break; |
| 51 | + } |
| 52 | + ctx.closePath(); |
| 53 | + ctx.fill(); |
| 54 | + } |
| 55 | + property color colorBg: control.colorBg |
| 56 | + } |
| 57 | + |
| 58 | + x: { |
| 59 | + switch (position) { |
| 60 | + case DelToolTip.Position_Top: |
| 61 | + case DelToolTip.Position_Bottom: |
| 62 | + return (__private.controlParentWidth - implicitWidth) * 0.5; |
| 63 | + case DelToolTip.Position_Left: |
| 64 | + return -implicitWidth - 4; |
| 65 | + case DelToolTip.Position_Right: |
| 66 | + return __private.controlParentWidth + 4; |
| 67 | + } |
| 68 | + } |
| 69 | + y: { |
| 70 | + switch (position) { |
| 71 | + case DelToolTip.Position_Top: |
| 72 | + return -implicitHeight - 4; |
| 73 | + case DelToolTip.Position_Bottom: |
| 74 | + return __private.controlParentHeight + 4; |
| 75 | + case DelToolTip.Position_Left: |
| 76 | + case DelToolTip.Position_Right: |
| 77 | + return (__private.controlParentHeight - implicitHeight) * 0.5; |
| 78 | + } |
| 79 | + } |
| 80 | + enter: Transition { |
| 81 | + NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; duration: control.animationEnabled ? 200 : 0 } |
| 82 | + } |
| 83 | + exit: Transition { |
| 84 | + NumberAnimation { property: "opacity"; from: 1.0; to: 0.0; duration: control.animationEnabled ? 200 : 0 } |
| 85 | + } |
| 86 | + delay: 300 |
| 87 | + padding: 0 |
| 88 | + implicitWidth: implicitContentWidth |
| 89 | + implicitHeight: implicitContentHeight |
| 90 | + font { |
| 91 | + family: "微软雅黑" |
| 92 | + pixelSize: 14 |
| 93 | + } |
| 94 | + closePolicy: T.Popup.CloseOnEscape | T.Popup.CloseOnPressOutsideParent | T.Popup.CloseOnReleaseOutsideParent |
| 95 | + contentItem: Item { |
| 96 | + implicitWidth: __bg.width + (__private.isHorizontal ? 0 : __arrow.width) |
| 97 | + implicitHeight: __bg.height + (__private.isHorizontal ? __arrow.height : 0) |
| 98 | + |
| 99 | + DropShadow { |
| 100 | + anchors.fill: __item |
| 101 | + radius: 8.0 |
| 102 | + samples: 17 |
| 103 | + color: "#80000000" |
| 104 | + source: __item |
| 105 | + } |
| 106 | + |
| 107 | + Item { |
| 108 | + id: __item |
| 109 | + anchors.fill: parent |
| 110 | + |
| 111 | + Arrow { |
| 112 | + id: __arrow |
| 113 | + x: __private.isHorizontal ? (-control.x + (__private.controlParentWidth - width) * 0.5) : 0 |
| 114 | + y: __private.isHorizontal ? 0 : (-control.y + (__private.controlParentHeight - height)) * 0.5 |
| 115 | + width: __private.arrowSize.width |
| 116 | + height: __private.arrowSize.height |
| 117 | + anchors.top: control.position == DelToolTip.Position_Bottom ? parent.top : undefined |
| 118 | + anchors.bottom: control.position == DelToolTip.Position_Top ? parent.bottom : undefined |
| 119 | + anchors.left: control.position == DelToolTip.Position_Right ? parent.left : undefined |
| 120 | + anchors.right: control.position == DelToolTip.Position_Left ? parent.right : undefined |
| 121 | + |
| 122 | + Connections { |
| 123 | + target: control |
| 124 | + function onPositionChanged() { |
| 125 | + __arrow.requestPaint(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Rectangle { |
| 131 | + id: __bg |
| 132 | + width: __text.implicitWidth + 14 |
| 133 | + height: __text.implicitHeight + 12 |
| 134 | + anchors.top: control.position == DelToolTip.Position_Top ? parent.top : undefined |
| 135 | + anchors.bottom: control.position == DelToolTip.Position_Bottom ? parent.bottom : undefined |
| 136 | + anchors.left: control.position == DelToolTip.Position_Left ? parent.left : undefined |
| 137 | + anchors.right: control.position == DelToolTip.Position_Right ? parent.right : undefined |
| 138 | + anchors.margins: 1 |
| 139 | + radius: 4 |
| 140 | + color: control.colorBg |
| 141 | + |
| 142 | + Text { |
| 143 | + id: __text |
| 144 | + text: control.text |
| 145 | + font: control.font |
| 146 | + color: control.colorText |
| 147 | + wrapMode: Text.Wrap |
| 148 | + anchors.centerIn: parent |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + background: Item { } |
| 154 | + |
| 155 | + QtObject { |
| 156 | + id: __private |
| 157 | + property bool isHorizontal: control.position == DelToolTip.Position_Top || control.position == DelToolTip.Position_Bottom |
| 158 | + property size arrowSize: control.arrowVisible ? (isHorizontal ? Qt.size(12, 6) : Qt.size(6, 12)) : Qt.size(0, 0) |
| 159 | + property real controlParentWidth: control.parent ? control.parent.width : 0 |
| 160 | + property real controlParentHeight: control.parent ? control.parent.height : 0 |
| 161 | + } |
| 162 | +} |
0 commit comments