diff options
Diffstat (limited to 'examples/quickcontrols/texteditor/qml/texteditor.qml')
-rw-r--r-- | examples/quickcontrols/texteditor/qml/texteditor.qml | 349 |
1 files changed, 206 insertions, 143 deletions
diff --git a/examples/quickcontrols/texteditor/qml/texteditor.qml b/examples/quickcontrols/texteditor/qml/texteditor.qml index decf0801e4..154a531752 100644 --- a/examples/quickcontrols/texteditor/qml/texteditor.qml +++ b/examples/quickcontrols/texteditor/qml/texteditor.qml @@ -6,9 +6,6 @@ import QtCore import QtQuick.Controls import QtQuick.Window import QtQuick.Dialogs -import Qt.labs.platform as Platform - -import io.qt.examples.texteditor // TODO: // - make designer-friendly @@ -18,7 +15,8 @@ ApplicationWindow { width: 1024 height: 600 visible: true - title: document.fileName + " - Text Editor Example" + title: textArea.textDocument.source + + " - Text Editor Example" + (textArea.textDocument.modified ? " *" : "") Component.onCompleted: { x = Screen.width / 2 - width / 2 @@ -27,122 +25,194 @@ ApplicationWindow { Action { id: openAction + text: qsTr("&Open") shortcut: StandardKey.Open - onTriggered: openDialog.open() + onTriggered: { + if (textArea.textDocument.modified) + discardDialog.open() + else + openDialog.open() + } + } + + Action { + id: saveAction + text: qsTr("&Save…") + shortcut: StandardKey.Save + enabled: textArea.textDocument.modified + onTriggered: textArea.textDocument.save() } Action { id: saveAsAction + text: qsTr("Save &As…") shortcut: StandardKey.SaveAs onTriggered: saveDialog.open() } Action { id: quitAction + text: qsTr("&Quit") shortcut: StandardKey.Quit onTriggered: close() } Action { id: copyAction + text: qsTr("&Copy") shortcut: StandardKey.Copy + enabled: textArea.selectedText onTriggered: textArea.copy() } Action { id: cutAction + text: qsTr("Cu&t") shortcut: StandardKey.Cut + enabled: textArea.selectedText onTriggered: textArea.cut() } Action { id: pasteAction + text: qsTr("&Paste") shortcut: StandardKey.Paste + enabled: textArea.canPaste onTriggered: textArea.paste() } Action { id: boldAction + text: qsTr("&Bold") shortcut: StandardKey.Bold - onTriggered: document.bold = !document.bold + checkable: true + checked: textArea.cursorSelection.font.bold + onTriggered: textArea.cursorSelection.font = Qt.font({ bold: checked }) } Action { id: italicAction + text: qsTr("&Italic") shortcut: StandardKey.Italic - onTriggered: document.italic = !document.italic + checkable: true + checked: textArea.cursorSelection.font.italic + onTriggered: textArea.cursorSelection.font = Qt.font({ italic: checked }) } Action { id: underlineAction + text: qsTr("&Underline") shortcut: StandardKey.Underline - onTriggered: document.underline = !document.underline + checkable: true + checked: textArea.cursorSelection.font.underline + onTriggered: textArea.cursorSelection.font = Qt.font({ underline: checked }) + } + + Action { + id: strikeoutAction + text: qsTr("&Strikeout") + checkable: true + checked: textArea.cursorSelection.font.strikeout + onTriggered: textArea.cursorSelection.font = Qt.font({ strikeout: checked }) + } + + Action { + id: alignLeftAction + text: qsTr("Align &Left") + shortcut: "Ctrl+{" + checkable: true + checked: textArea.cursorSelection.alignment === Qt.AlignLeft + onTriggered: textArea.cursorSelection.alignment = Qt.AlignLeft + } + + Action { + id: alignCenterAction + text: qsTr("&Center") + shortcut: "Ctrl+|" + checkable: true + checked: textArea.cursorSelection.alignment === Qt.AlignCenter + onTriggered: textArea.cursorSelection.alignment = Qt.AlignCenter + } + + Action { + id: alignRightAction + text: qsTr("Align &Right") + shortcut: "Ctrl+}" + checkable: true + checked: textArea.cursorSelection.alignment === Qt.AlignRight + onTriggered: textArea.cursorSelection.alignment = Qt.AlignRight + } + + Action { + id: alignJustifyAction + text: qsTr("&Justify") + shortcut: "Ctrl+Alt+}" + checkable: true + checked: textArea.cursorSelection.alignment === Qt.AlignJustify + onTriggered: textArea.cursorSelection.alignment = Qt.AlignJustify } - Platform.MenuBar { - Platform.Menu { + menuBar: MenuBar { + Menu { title: qsTr("&File") - Platform.MenuItem { - text: qsTr("&Open") - onTriggered: openDialog.open() + MenuItem { + action: openAction } - Platform.MenuItem { - text: qsTr("&Save As...") - onTriggered: saveDialog.open() + MenuItem { + action: saveAction } - Platform.MenuItem { - text: qsTr("&Quit") - onTriggered: close() + MenuItem { + action: saveAsAction + } + MenuItem { + action: quitAction } } - Platform.Menu { + Menu { title: qsTr("&Edit") - Platform.MenuItem { - text: qsTr("&Copy") - enabled: textArea.selectedText - onTriggered: textArea.copy() + MenuItem { + action: copyAction } - Platform.MenuItem { - text: qsTr("Cu&t") - enabled: textArea.selectedText - onTriggered: textArea.cut() + MenuItem { + action: cutAction } - Platform.MenuItem { - text: qsTr("&Paste") - enabled: textArea.canPaste - onTriggered: textArea.paste() + MenuItem { + action: pasteAction } } - Platform.Menu { + Menu { title: qsTr("F&ormat") - Platform.MenuItem { - text: qsTr("&Bold") - checkable: true - checked: document.bold - onTriggered: document.bold = !document.bold + MenuItem { + action: boldAction + } + MenuItem { + action: italicAction + } + MenuItem { + action: underlineAction + } + MenuItem { + action: strikeoutAction + } + + MenuSeparator {} + + MenuItem { + action: alignLeftAction } - Platform.MenuItem { - text: qsTr("&Italic") - checkable: true - checked: document.italic - onTriggered: document.italic = !document.italic + MenuItem { + action: alignCenterAction } - Platform.MenuItem { - text: qsTr("&Underline") - checkable: true - checked: document.underline - onTriggered: document.underline = !document.underline + MenuItem { + action: alignJustifyAction } - Platform.MenuItem { - text: qsTr("&Strikeout") - checkable: true - checked: document.strikeout - onTriggered: document.strikeout = !document.strikeout + MenuItem { + action: alignRightAction } } } @@ -153,28 +223,29 @@ ApplicationWindow { selectedNameFilter.index: 1 nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)", "Markdown files (*.md *.markdown)"] currentFolder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation) - onAccepted: document.load(selectedFile) + onAccepted: { + textArea.textDocument.modified = false // we asked earlier, if necessary + textArea.textDocument.source = selectedFile + } } FileDialog { id: saveDialog fileMode: FileDialog.SaveFile - defaultSuffix: document.fileType nameFilters: openDialog.nameFilters - selectedNameFilter.index: document.fileType === "txt" ? 0 : 1 currentFolder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation) - onAccepted: document.saveAs(selectedFile) + onAccepted: textArea.textDocument.saveAs(selectedFile) } FontDialog { id: fontDialog - onAccepted: document.font = selectedFont + onAccepted: textArea.cursorSelection.font = selectedFont } ColorDialog { id: colorDialog selectedColor: "black" - onAccepted: document.textColor = selectedColor + onAccepted: textArea.cursorSelection.color = selectedColor } MessageDialog { @@ -187,7 +258,23 @@ ApplicationWindow { title: qsTr("Quit?") text: qsTr("The file has been modified. Quit anyway?") buttons: MessageDialog.Yes | MessageDialog.No - onButtonClicked: function (button, role) { if (role === MessageDialog.YesRole) Qt.quit() } + onButtonClicked: function (button, role) { + if (role === MessageDialog.YesRole) { + textArea.textDocument.modified = false + Qt.quit() + } + } + } + + MessageDialog { + id : discardDialog + title: qsTr("Discard changes?") + text: qsTr("The file has been modified. Open a new file anyway?") + buttons: MessageDialog.Yes | MessageDialog.No + onButtonClicked: function (button, role) { + if (role === MessageDialog.YesRole) + openDialog.open() + } } header: ToolBar { @@ -206,6 +293,13 @@ ApplicationWindow { action: openAction focusPolicy: Qt.TabFocus } + ToolButton { + id: saveButton + text: "\uE80A" // icon-floppy-disk + font.family: "fontello" + action: saveAction + focusPolicy: Qt.TabFocus + } ToolSeparator { contentItem.visible: fileRow.y === editRow.y } @@ -218,7 +312,6 @@ ApplicationWindow { text: "\uF0C5" // icon-docs font.family: "fontello" focusPolicy: Qt.TabFocus - enabled: textArea.selectedText action: copyAction } ToolButton { @@ -226,7 +319,6 @@ ApplicationWindow { text: "\uE802" // icon-scissors font.family: "fontello" focusPolicy: Qt.TabFocus - enabled: textArea.selectedText action: cutAction } ToolButton { @@ -234,7 +326,6 @@ ApplicationWindow { text: "\uF0EA" // icon-paste font.family: "fontello" focusPolicy: Qt.TabFocus - enabled: textArea.canPaste action: pasteAction } ToolSeparator { @@ -249,8 +340,6 @@ ApplicationWindow { text: "\uE800" // icon-bold font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.bold action: boldAction } ToolButton { @@ -258,8 +347,6 @@ ApplicationWindow { text: "\uE801" // icon-italic font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.italic action: italicAction } ToolButton { @@ -267,8 +354,6 @@ ApplicationWindow { text: "\uF0CD" // icon-underline font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.underline action: underlineAction } ToolButton { @@ -276,21 +361,19 @@ ApplicationWindow { text: "\uF0CC" font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.strikeout - onClicked: document.strikeout = !document.strikeout + action: strikeoutAction } ToolButton { id: fontFamilyToolButton text: qsTr("\uE808") // icon-font font.family: "fontello" - font.bold: document.bold - font.italic: document.italic - font.underline: document.underline - font.strikeout: document.strikeout + font.bold: textArea.cursorSelection.font.bold + font.italic: textArea.cursorSelection.font.italic + font.underline: textArea.cursorSelection.font.underline + font.strikeout: textArea.cursorSelection.font.strikeout focusPolicy: Qt.TabFocus onClicked: function () { - fontDialog.selectedFont = document.font + fontDialog.selectedFont = textArea.cursorSelection.font fontDialog.open() } } @@ -300,14 +383,14 @@ ApplicationWindow { font.family: "fontello" focusPolicy: Qt.TabFocus onClicked: function () { - colorDialog.selectedColor = document.textColor + colorDialog.selectedColor = textArea.cursorSelection.color colorDialog.open() } Rectangle { width: aFontMetrics.width + 3 height: 2 - color: document.textColor + color: textArea.cursorSelection.color parent: textColorButton.contentItem anchors.horizontalCenter: parent.horizontalCenter anchors.baseline: parent.baseline @@ -332,79 +415,43 @@ ApplicationWindow { text: "\uE803" // icon-align-left font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.alignment == Qt.AlignLeft - onClicked: document.alignment = Qt.AlignLeft + action: alignLeftAction } ToolButton { id: alignCenterButton text: "\uE804" // icon-align-center font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.alignment == Qt.AlignHCenter - onClicked: document.alignment = Qt.AlignHCenter + action: alignCenterAction } ToolButton { id: alignRightButton text: "\uE805" // icon-align-right font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.alignment == Qt.AlignRight - onClicked: document.alignment = Qt.AlignRight + action: alignRightAction } ToolButton { id: alignJustifyButton text: "\uE806" // icon-align-justify font.family: "fontello" focusPolicy: Qt.TabFocus - checkable: true - checked: document.alignment == Qt.AlignJustify - onClicked: document.alignment = Qt.AlignJustify + action: alignJustifyAction } } } } - DocumentHandler { - id: document - document: textArea.textDocument - cursorPosition: textArea.cursorPosition - selectionStart: textArea.selectionStart - selectionEnd: textArea.selectionEnd - - property alias family: document.font.family - property alias bold: document.font.bold - property alias italic: document.font.italic - property alias underline: document.font.underline - property alias strikeout: document.font.strikeout - property alias size: document.font.pointSize - - Component.onCompleted: { - if (Qt.application.arguments.length === 2) - document.load("file:" + Qt.application.arguments[1]); - else - document.load("qrc:/texteditor.html") - } - onLoaded: function (text, format) { - textArea.textFormat = format - textArea.text = text - } - onError: function (message) { - errorDialog.text = message - errorDialog.open() - } - } - Flickable { id: flickable flickableDirection: Flickable.VerticalFlick anchors.fill: parent + ScrollBar.vertical: ScrollBar {} + TextArea.flickable: TextArea { id: textArea - textFormat: Qt.RichText + textFormat: Qt.AutoText wrapMode: TextArea.Wrap focus: true selectByMouse: true @@ -418,60 +465,76 @@ ApplicationWindow { bottomPadding: 0 background: null - MouseArea { + TapHandler { acceptedButtons: Qt.RightButton - anchors.fill: parent - onClicked: contextMenu.open() + onTapped: contextMenu.popup() } onLinkActivated: function (link) { Qt.openUrlExternally(link) } - } - ScrollBar.vertical: ScrollBar {} + Component.onCompleted: { + if (Qt.application.arguments.length === 2) + textDocument.source = "file:" + Qt.application.arguments[1] + else + textDocument.source = "qrc:/texteditor.html" + } + + textDocument.onStatusChanged: { + // a message lookup table using computed properties: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer + const statusMessages = { + [ TextDocument.ReadError ]: qsTr("Failed to load “%1”"), + [ TextDocument.WriteError ]: qsTr("Failed to save “%1”"), + [ TextDocument.NonLocalFileError ]: qsTr("Not a local file: “%1”"), + } + const err = statusMessages[textDocument.status] + if (err) { + errorDialog.text = err.arg(textDocument.source) + errorDialog.open() + } + } + } } - Platform.Menu { + Menu { id: contextMenu - Platform.MenuItem { + MenuItem { text: qsTr("Copy") - enabled: textArea.selectedText - onTriggered: textArea.copy() + action: copyAction } - Platform.MenuItem { + MenuItem { text: qsTr("Cut") - enabled: textArea.selectedText - onTriggered: textArea.cut() + action: cutAction } - Platform.MenuItem { + MenuItem { text: qsTr("Paste") - enabled: textArea.canPaste - onTriggered: textArea.paste() + action: pasteAction } - Platform.MenuSeparator {} + MenuSeparator {} - Platform.MenuItem { + MenuItem { text: qsTr("Font...") onTriggered: function () { - fontDialog.selectedFont = document.font + fontDialog.selectedFont = textArea.cursorSelection.font fontDialog.open() } } - Platform.MenuItem { + MenuItem { text: qsTr("Color...") onTriggered: function () { - colorDialog.selectedColor = document.textColor + colorDialog.selectedColor = textArea.cursorSelection.color colorDialog.open() } } } onClosing: function (close) { - if (document.modified) { + if (textArea.textDocument.modified) { quitDialog.open() close.accepted = false } |