-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to do some TextField-related actions
Introduces copy, paste and select all when a text field is selected. Uses the __editMenu property introduced in Qt 5.6. Integrates the text field custom menu, as shown in the demo.
- Loading branch information
Showing
6 changed files
with
282 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
modules/QtQuick/Controls/Styles/Material/MaterialEditMenu.qml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* Copyright (C) 2015 by Aleix Pol Gonzalez <[email protected]> | ||
* Copyright (C) 2015 by Marco Martin <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Library General Public License as | ||
* published by the Free Software Foundation; either version 2, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Library General Public License for more details | ||
* | ||
* You should have received a copy of the GNU Library General Public | ||
* License along with this program; if not, write to the | ||
* Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 2.010-1301, USA. | ||
*/ | ||
|
||
import QtQuick 2.1 | ||
import QtQuick.Controls 1.1 | ||
import QtQuick.Controls.Styles 1.1 | ||
import QtQuick.Layouts 1.0 | ||
import Material 0.1 | ||
|
||
Item { | ||
id: root | ||
anchors.fill: parent | ||
|
||
property bool editing: true | ||
onEditingChanged: { | ||
updateCursorOpacity() | ||
} | ||
function updateCursorOpacity() { | ||
var opacity = root.editing ? 0 : 1; | ||
cursorHandle.opacity = opacity; | ||
selectionHandle.opacity = opacity; | ||
} | ||
Component.onCompleted: updateCursorOpacity() | ||
|
||
// https://www.google.com/design/spec/patterns/selection.html#selection-text-selection | ||
Component { | ||
id: editControls | ||
Item { | ||
id: popup | ||
visible: input.activeFocus && !root.editing | ||
z: 9999 | ||
|
||
Behavior on x { NumberAnimation { duration: 500; easing.type: Easing.InOutQuad } } | ||
|
||
Component.onCompleted: { | ||
var par = control | ||
//heuristic: if a flickable is found in the parents, | ||
//reparent to it, so it scrolls together | ||
while (par.parent && par.parent.contentY === undefined) { | ||
par = par.parent | ||
} | ||
|
||
popup.parent = par | ||
} | ||
|
||
function popup(startRect) { | ||
// var selectedTextTopPadding = Units.dp(8); | ||
var mapped = parent.mapFromItem(input, startRect.x, startRect.y); | ||
var mapWidget = parent.mapFromItem(input.parent, input.x, input.y, input.width, input.height); | ||
|
||
popup.x = Math.min(Math.max(mapWidget.x, mapped.x), mapWidget.x+mapWidget.width-bg.width); | ||
popup.y = Math.max(0, mapped.y - bg.height); | ||
} | ||
function dismiss() { | ||
popup.visible = false; | ||
} | ||
|
||
View { | ||
id: bg | ||
elevation: 1 | ||
anchors { | ||
fill: buttons | ||
topMargin: -Units.dp(12) | ||
bottomMargin: -Units.dp(14) | ||
leftMargin: -Units.dp(24) | ||
rightMargin: -Units.dp(16) | ||
} | ||
} | ||
|
||
RowLayout { | ||
id: buttons | ||
spacing: Units.dp(32) | ||
Button { | ||
text: qsTr("Cut") | ||
visible: input.selectedText != "" | ||
context: "editmenu" | ||
onClicked: { | ||
control.cut(); | ||
select(input.cursorPosition, input.cursorPosition); | ||
} | ||
} | ||
Button { | ||
text: qsTr("Copy") | ||
visible: input.selectedText != "" | ||
context: "editmenu" | ||
onClicked: { | ||
control.copy(); | ||
select(input.cursorPosition, input.cursorPosition); | ||
} | ||
} | ||
Button { | ||
text: qsTr("Paste") | ||
visible: input.canPaste | ||
context: "editmenu" | ||
onClicked: { | ||
control.paste(); | ||
} | ||
} | ||
Button { | ||
text: qsTr("Select All") | ||
visible: input.text != "" | ||
context: "editmenu" | ||
onClicked: { | ||
control.selectAll(); | ||
} | ||
} | ||
|
||
IconButton { | ||
iconName: "navigation/more_vert" | ||
visible: control.menu !== null | ||
onClicked: { | ||
getMenuInstance().popup() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Connections { | ||
target: mouseArea | ||
|
||
onClicked: { | ||
var pos = input.positionAt(mouse.x, mouse.y) | ||
input.moveHandles(pos, pos) | ||
input.activate() | ||
} | ||
onPressAndHold: { | ||
root.editing = false; | ||
var pos = input.positionAt(mouse.x, mouse.y) | ||
input.moveHandles(pos, control.selectByMouse ? -1 : pos) | ||
input.activate() | ||
} | ||
} | ||
|
||
Connections { | ||
target: input | ||
onSelectionStartChanged: popupTimer.restart() | ||
onSelectionEndChanged: popupTimer.restart() | ||
onActiveFocusChanged: { | ||
popupTimer.restart() | ||
root.editing = true; | ||
} | ||
onTextChanged: root.editing = true; | ||
} | ||
|
||
Connections { | ||
target: flickable | ||
onMovingChanged: popupTimer.restart() | ||
} | ||
|
||
property Item editControlsInstance: null | ||
function getEditControlsInstance() { | ||
// Lazy load the view when first requested | ||
if (!editControlsInstance) { | ||
editControlsInstance = editControls.createObject(control); | ||
} | ||
return editControlsInstance; | ||
} | ||
|
||
Timer { | ||
id: popupTimer | ||
interval: 200 | ||
onTriggered: { | ||
if (input.canPaste || selectionStart !== selectionEnd) { | ||
var startRect = input.positionToRectangle(input.selectionStart); | ||
|
||
getEditControlsInstance().popup(startRect); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2015 by Aleix Pol Gonzalez <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Library General Public License as | ||
* published by the Free Software Foundation; either version 2, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Library General Public License for more details | ||
* | ||
* You should have received a copy of the GNU Library General Public | ||
* License along with this program; if not, write to the | ||
* Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 2.010-1301, USA. | ||
*/ | ||
|
||
import QtQuick 2.1 | ||
import Material 0.1 | ||
|
||
Rectangle { | ||
id: root | ||
property int side: 0 //-1 left, 0 center, 1 right | ||
|
||
width: Units.dp(22) | ||
height: Units.dp(22) | ||
|
||
radius: width | ||
y: styleData.lineHeight | ||
|
||
Rectangle { | ||
id: rect | ||
width: parent.width/2 | ||
height: parent.height/2 | ||
color: parent.color | ||
} | ||
|
||
states: [ | ||
State { | ||
when: side<0 | ||
name: "right" | ||
PropertyChanges { target: root; x: -width} | ||
AnchorChanges { target: rect; anchors.right: parent.right } | ||
}, | ||
State { | ||
when: side>0 | ||
name: "left" | ||
AnchorChanges { target: rect; anchors.left: parent.left } | ||
}, | ||
State { | ||
when: side==0 | ||
name: "center" | ||
PropertyChanges { target: rect; rotation: 45 } | ||
PropertyChanges { target: rect; width: root.width/Math.SQRT2 } | ||
PropertyChanges { target: rect; height: rect.width } | ||
PropertyChanges { target: rect; x: (root.width-rect.width)/2 } | ||
|
||
PropertyChanges { target: root; y: styleData.lineHeight/*+root.height/2*/ } | ||
PropertyChanges { target: root; x: -root.width/2 } | ||
} | ||
] | ||
} |