| 1 |
/* |
| 2 |
* Copyright 2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.3 |
| 19 |
import Lomiri.Components.Popups 1.3 |
| 20 |
|
| 21 |
Popover { |
| 22 |
id: popover |
| 23 |
objectName: "text_input_contextmenu" |
| 24 |
property Item target |
| 25 |
property bool canCopy: target && target.selectedText !== "" && !popover.password |
| 26 |
property bool password: target && target.hasOwnProperty('echoMode') && target.echoMode == TextInput.Password |
| 27 |
property list<Action> actions: [ |
| 28 |
Action { |
| 29 |
name: "select_all" |
| 30 |
text: i18n.dtr('lomiri-ui-toolkit', "Select All") |
| 31 |
iconName: "edit-select-all" |
| 32 |
enabled: target && target.text !== "" && target.selectedText === "" |
| 33 |
visible: target && (target.selectedText === "" || popover.password) |
| 34 |
onTriggered: target.selectAll() |
| 35 |
}, |
| 36 |
Action { |
| 37 |
name: "cut" |
| 38 |
text: i18n.dtr('lomiri-ui-toolkit', "Cut") |
| 39 |
iconName: "edit-cut" |
| 40 |
// If paste/editing is not possible, then disable also "Cut" operation |
| 41 |
// It is applicable for ReadOnly's TextFields and TextAreas |
| 42 |
enabled: !target.readOnly |
| 43 |
visible: popover.canCopy |
| 44 |
onTriggered: { |
| 45 |
lastWrittenFormula = "" |
| 46 |
PopupUtils.close(popover); |
| 47 |
target.cut(); |
| 48 |
} |
| 49 |
}, |
| 50 |
Action { |
| 51 |
name: "copy" |
| 52 |
text: i18n.dtr('lomiri-ui-toolkit', "Copy") |
| 53 |
iconName: "edit-copy" |
| 54 |
visible: popover.canCopy |
| 55 |
onTriggered: { |
| 56 |
PopupUtils.close(popover); |
| 57 |
target.copy(); |
| 58 |
} |
| 59 |
}, |
| 60 |
Action { |
| 61 |
name: "paste" |
| 62 |
text: i18n.dtr('lomiri-ui-toolkit', "Paste") |
| 63 |
iconName: "edit-paste" |
| 64 |
enabled: target && target.canPaste |
| 65 |
onTriggered: { |
| 66 |
PopupUtils.close(popover); |
| 67 |
target.paste(); |
| 68 |
formulaPush(target.text); |
| 69 |
} |
| 70 |
} |
| 71 |
] |
| 72 |
|
| 73 |
function show() { |
| 74 |
visible = true; |
| 75 |
__foreground.show(); |
| 76 |
// No input focus on the context menu! |
| 77 |
} |
| 78 |
|
| 79 |
// removes hide animation |
| 80 |
function hide() { |
| 81 |
popover.visible = false; |
| 82 |
} |
| 83 |
|
| 84 |
autoClose: false |
| 85 |
contentHeight: row.childrenRect.height + padding * 2 |
| 86 |
contentWidth: row.childrenRect.width + padding * 2 |
| 87 |
property int padding: units.gu(1) |
| 88 |
Row { |
| 89 |
id: row |
| 90 |
height: units.gu(6) |
| 91 |
x: popover.padding |
| 92 |
y: popover.padding |
| 93 |
|
| 94 |
Repeater { |
| 95 |
model: actions.length |
| 96 |
AbstractButton { |
| 97 |
id: button |
| 98 |
objectName: actions[modelData].name+"_button" |
| 99 |
width: Math.max(units.gu(5), implicitWidth) + units.gu(2) |
| 100 |
height: units.gu(6) |
| 101 |
action: actions[modelData] |
| 102 |
styleName: "ToolbarButtonStyle" |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
InverseMouseArea { |
| 108 |
anchors.fill: parent |
| 109 |
onPressed: PopupUtils.close(popover) |
| 110 |
} |
| 111 |
// this is necessary otherwise the popover will close itself |
| 112 |
// once the user tap "select all" |
| 113 |
Connections { |
| 114 |
target: popover.target |
| 115 |
onSelectedTextChanged: { |
| 116 |
if (target.selectedText != target.text) { |
| 117 |
PopupUtils.close(popover) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |