web123456

qml implementation of a text editor

  • import QtQuick 2.2
  • import 1.1
  • import 1.4
  • import 1.3
  • import FileIO 1.0
  • ApplicationWindow {
  • id: q
  • visible: true
  • title: "Document Editor"
  • width: 640
  • height: 480
  • color: "white"
  • menuBar: MenuBar {
  • Menu {
  • title: "Documentation"
  • MenuItem {
  • id: open
  • text: "Open..."
  • iconName: "open"
  • shortcut:
  • onTriggered: open.open()
  • function open(){
  • fileDialog.open()
  • = true
  • =true
  • }
  • }
  • MenuItem {
  • text: "New."
  • shortcut:
  • onTriggered: fileDialog2.open()
  • }
  • MenuItem {
  • text: "Save."
  • shortcut:
  • onTriggered: console.log( "WRITE"+ myFile.write())
  • }
  • MenuItem {
  • text: "Save as."
  • shortcut:
  • onTriggered: fileDialog1.open()
  • }
  • }
  • Menu {
  • title: "Edit."
  • MenuItem {
  • text: "Copy."
  • shortcut:
  • onTriggered: edit.copy()
  • }
  • MenuItem {
  • text: "Cut."
  • shortcut:
  • onTriggered: edit.cut()
  • }
  • MenuItem {
  • text: "Paste."
  • shortcut:
  • onTriggered: edit.paste()
  • }
  • }
  • Menu {
  • title: "Help."
  • MenuItem {
  • text: "About..."
  • shortcut:
  • onTriggered: aboutDialog.open()
  • }
  • }
  • }
  • Flickable
  • {
  • id: flick
  • :parent
  • width: 300; height: 200;
  • contentWidth:
  • contentHeight:
  • clip: true
  • visible: false
  • function ensureVisible(r)
  • {
  • if (contentX >= )
  • contentX = ;
  • else if (contentX+width <= +)
  • contentX = +-width;
  • if (contentY >= )
  • contentY = ;
  • else if (contentY+height <= +)
  • contentY = +-height;
  • }
  • TextEdit
  • {
  • id: edit
  • width:
  • height:
  • focus: true
  • wrapMode:
  • selectByMouse: true
  • onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  • }
  • }
  • Rectangle
  • {
  • id: scrollbar
  • visible: false
  • :
  • x: 350
  • y: 0
  • width: 10
  • height:
  • color: "white"
  • Rectangle {
  • id: button
  • x: 0
  • y: *
  • width: 10
  • height: * ;
  • color: "grey"
  • // Mouse area
  • MouseArea {
  • id: mouseArea
  • : button
  • : button
  • :
  • : 0
  • : - // Drag
  • onMouseYChanged: {
  • = / *
  • }
  • }
  • }
  • }
  • FileDialog {
  • id: fileDialog
  • title: "Please select a file."
  • onAccepted: {
  • var path = .toString();
  • path = path.replace(/^(file:\/{3})/,"");
  • path = decodeURIComponent(path);
  • =path;
  • = myFile.read();
  • // ( "WRITE"+ ("TEST TEST file is OK"));
  • console.log("You chose: " + );
  • fileDialog.close()
  • }
  • onRejected: {
  • console.log("Canceled")
  • fileDialog.close()
  • }
  • }
  • FileDialog {
  • id: fileDialog1
  • title: "Please enter a filename."
  • selectExisting: false
  • onAccepted: {
  • var p=myFile.read();
  • var path = .toString();
  • path = path.replace(/^(file:\/{3})/,"");
  • path = decodeURIComponent(path);
  • =path;
  • console.log( "WRITE"+ myFile.write(p));
  • = myFile.read();
  • console.log("You chose: " + );
  • fileDialog.close();
  • }
  • onRejected: {
  • console.log("Canceled")
  • fileDialog.close()
  • }
  • }
  • FileDialog {
  • id: fileDialog2
  • title: "Please enter a filename."
  • selectExisting: false
  • onAccepted: {
  • var path = .toString();
  • path = path.replace(/^(file:\/{3})/,"");
  • path = decodeURIComponent(path);
  • =path;
  • console.log( "WRITE"+ myFile.write(""));
  • = myFile.read();
  • console.log("You chose: " + );
  • fileDialog.close();
  • }
  • onRejected: {
  • console.log("Canceled")
  • fileDialog.close()
  • }
  • }
  • FileIO {
  • id: myFile
  • onError: console.log(msg)
  • }
  • }