Skip to content

Commit b08043b

Browse files
author
koskasdediegor
committed
Took out TextField Component
1 parent 5713c50 commit b08043b

2 files changed

Lines changed: 130 additions & 127 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import QtQuick
2+
import QtQuick.Controls
3+
import QtQuick.Layouts
4+
5+
RowLayout {
6+
id: root
7+
required property string text
8+
required property bool mandatory
9+
property bool editable
10+
anchors.fill: parent
11+
TextField {
12+
id: textField
13+
Layout.fillWidth: true
14+
readOnly: !root.editable
15+
text: root.text
16+
placeholderText: root.mandatory ? "This field is required" : ""
17+
placeholderTextColor: "gray"
18+
// Don't disable the component to keep interactive features (text selection, context menu...).
19+
// Only override the look by using the Disabled palette.
20+
SystemPalette {
21+
id: disabledPalette
22+
colorGroup: SystemPalette.Disabled
23+
}
24+
background: Rectangle {
25+
border.color: errorMessages.length ? "orange" : "transparent"
26+
color: Qt.darker(palette.window, 1.2)
27+
radius: 2
28+
}
29+
states: [
30+
State {
31+
when: readOnly
32+
PropertyChanges {
33+
target: textField
34+
color: disabledPalette.text
35+
}
36+
}
37+
]
38+
selectByMouse: true
39+
persistentSelection: false
40+
onEditingFinished: {
41+
setTextFieldAttribute(text)
42+
}
43+
onAccepted: {
44+
setTextFieldAttribute(text)
45+
parameterLabel.forceActiveFocus()
46+
}
47+
Keys.onPressed: function(event) {
48+
if ((event.key == Qt.Key_Escape)) {
49+
event.accepted = true
50+
parameterLabel.forceActiveFocus()
51+
}
52+
}
53+
Component.onDestruction: {
54+
if (activeFocus)
55+
setTextFieldAttribute(text)
56+
}
57+
DropArea {
58+
enabled: root.editable
59+
anchors.fill: parent
60+
onDropped: function(drop) {
61+
if (drop.hasUrls)
62+
setTextFieldAttribute(Filepath.urlToString(drop.urls[0]))
63+
else if (drop.hasText && drop.text != '')
64+
setTextFieldAttribute(drop.text)
65+
}
66+
}
67+
onPressed: (event) => {
68+
if (event.button == Qt.RightButton) {
69+
// Keep selection persistent while context menu is open to
70+
// visualize what is being copied or what will be replaced on paste.
71+
persistentSelection = true
72+
const menu = textFieldMenuComponent.createObject(textField)
73+
menu.popup()
74+
if (selectedText === "") {
75+
cursorPosition = positionAt(event.x, event.y)
76+
}
77+
}
78+
}
79+
Component {
80+
id: textFieldMenuComponent
81+
Menu {
82+
onOpened: {
83+
// Keep cursor visible to see where pasting would happen.
84+
textField.cursorVisible = true
85+
}
86+
onClosed: {
87+
// Disable selection persistency behavior once menu is closed and
88+
// give focus back to the parent TextField.
89+
textField.persistentSelection = false
90+
textField.forceActiveFocus()
91+
destroy()
92+
}
93+
MenuItem {
94+
text: "Copy"
95+
enabled: root.text != ""
96+
onTriggered: {
97+
const hasSelection = textField.selectionStart !== textField.selectionEnd
98+
if (hasSelection) {
99+
// Use `TextField.copy` to copy only the current selection.
100+
textField.copy()
101+
}
102+
else {
103+
Clipboard.setText(root.text)
104+
}
105+
}
106+
}
107+
MenuItem {
108+
text: "Paste"
109+
enabled: !readOnly
110+
onTriggered: {
111+
const clipboardText = Clipboard.getText()
112+
if (clipboardText.length === 0) {
113+
return
114+
}
115+
const before = textField.text.substr(0, textField.selectionStart)
116+
const after = textField.text.substr(textField.selectionEnd, textField.text.length)
117+
const updatedValue = before + clipboardText + after
118+
setTextFieldAttribute(updatedValue)
119+
// Set the cursor at the end of the added text
120+
textField.cursorPosition = before.length + clipboardText.length
121+
}
122+
}
123+
}
124+
}
125+
}
126+
}

meshroom/ui/qml/GraphEditor/AttributeItemDelegate.qml

Lines changed: 4 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -500,133 +500,10 @@ RowLayout {
500500

501501
Component {
502502
id: textFieldComponent
503-
504-
RowLayout {
505-
anchors.fill: parent
506-
507-
TextField {
508-
id: textField
509-
Layout.fillWidth: true
510-
511-
readOnly: !root.editable
512-
text: attribute.value
513-
placeholderText: attribute.isMandatory ? "This field is required" : ""
514-
placeholderTextColor: "gray"
515-
// Don't disable the component to keep interactive features (text selection, context menu...).
516-
// Only override the look by using the Disabled palette.
517-
SystemPalette {
518-
id: disabledPalette
519-
colorGroup: SystemPalette.Disabled
520-
}
521-
522-
background: Rectangle {
523-
border.color: errorMessages.length ? "orange" : "transparent"
524-
color: Qt.darker(palette.window, 1.2)
525-
radius: 2
526-
}
527-
528-
states: [
529-
State {
530-
when: readOnly
531-
PropertyChanges {
532-
target: textField
533-
color: disabledPalette.text
534-
}
535-
}
536-
]
537-
538-
selectByMouse: true
539-
persistentSelection: false
540-
541-
onEditingFinished: {
542-
setTextFieldAttribute(text)
543-
}
544-
545-
onAccepted: {
546-
setTextFieldAttribute(text)
547-
parameterLabel.forceActiveFocus()
548-
}
549-
Keys.onPressed: function(event) {
550-
if ((event.key == Qt.Key_Escape)) {
551-
event.accepted = true
552-
parameterLabel.forceActiveFocus()
553-
}
554-
}
555-
Component.onDestruction: {
556-
if (activeFocus)
557-
setTextFieldAttribute(text)
558-
}
559-
DropArea {
560-
enabled: root.editable
561-
anchors.fill: parent
562-
onDropped: function(drop) {
563-
if (drop.hasUrls)
564-
setTextFieldAttribute(Filepath.urlToString(drop.urls[0]))
565-
else if (drop.hasText && drop.text != '')
566-
setTextFieldAttribute(drop.text)
567-
}
568-
}
569-
onPressed: (event) => {
570-
if (event.button == Qt.RightButton) {
571-
// Keep selection persistent while context menu is open to
572-
// visualize what is being copied or what will be replaced on paste.
573-
persistentSelection = true
574-
const menu = textFieldMenuComponent.createObject(textField)
575-
menu.popup()
576-
577-
if (selectedText === "") {
578-
cursorPosition = positionAt(event.x, event.y)
579-
}
580-
}
581-
}
582-
583-
Component {
584-
id: textFieldMenuComponent
585-
Menu {
586-
onOpened: {
587-
// Keep cursor visible to see where pasting would happen.
588-
textField.cursorVisible = true
589-
}
590-
onClosed: {
591-
// Disable selection persistency behavior once menu is closed and
592-
// give focus back to the parent TextField.
593-
textField.persistentSelection = false
594-
textField.forceActiveFocus()
595-
destroy()
596-
}
597-
MenuItem {
598-
text: "Copy"
599-
enabled: attribute.value != ""
600-
onTriggered: {
601-
const hasSelection = textField.selectionStart !== textField.selectionEnd
602-
if (hasSelection) {
603-
// Use `TextField.copy` to copy only the current selection.
604-
textField.copy()
605-
}
606-
else {
607-
Clipboard.setText(attribute.value)
608-
}
609-
}
610-
}
611-
MenuItem {
612-
text: "Paste"
613-
enabled: !readOnly
614-
onTriggered: {
615-
const clipboardText = Clipboard.getText()
616-
if (clipboardText.length === 0) {
617-
return
618-
}
619-
const before = textField.text.substr(0, textField.selectionStart)
620-
const after = textField.text.substr(textField.selectionEnd, textField.text.length)
621-
const updatedValue = before + clipboardText + after
622-
setTextFieldAttribute(updatedValue)
623-
// Set the cursor at the end of the added text
624-
textField.cursorPosition = before.length + clipboardText.length
625-
}
626-
}
627-
}
628-
}
629-
}
503+
AttributeControls.TextField{
504+
text: attribute.value
505+
mandatory: attribute.isMandatory
506+
editable: root.editable
630507
}
631508
}
632509

0 commit comments

Comments
 (0)