Warning, /pim/kube/framework/qml/TextEditor.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  Copyright (C) 2017 Michael Bohlender, <michael.bohlender@kdemail.net>
0003  *  Copyright (C) 2017 Christian Mollekopf, <mollekopf@kolabsys.com>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License along
0016  *  with this program; if not, write to the Free Software Foundation, Inc.,
0017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 import QtQuick 2.9
0021 import QtQuick.Controls 2
0022 
0023 import org.kube.framework 1.0 as Kube
0024 
0025 FocusScope {
0026     id: root
0027     readonly property string text: document.text
0028     property bool htmlEnabled: document.containsFormatting
0029 
0030     property alias bold: document.bold
0031     property alias italic: document.italic
0032     property alias underline: document.underline
0033     property alias border: rect.border
0034 
0035     property alias font: edit.font
0036     property string initialText
0037     property string placeholderText
0038     onInitialTextChanged: {
0039         //Avoid loosing the cursor position if we don't have to
0040         //This codepath is triggered on save
0041         if (edit.text != initialText) {
0042             edit.text = initialText
0043         }
0044     }
0045 
0046     function clearFormatting() {
0047         document.resetFormat()
0048     }
0049 
0050     Kube.TextDocumentHandler {
0051         id: document
0052         document: edit.textDocument
0053         selectionStart: edit.selectionStart
0054         selectionEnd: edit.selectionEnd
0055         cursorPosition: edit.cursorPosition
0056     }
0057 
0058     Rectangle {
0059         id: rect
0060         anchors.fill: parent
0061         border.width: 1
0062         border.color: root.activeFocus ? Kube.Colors.highlightColor : Kube.Colors.buttonColor
0063         color: Kube.Colors.viewBackgroundColor
0064 
0065         Flickable {
0066             id: flickableItem
0067             anchors.fill: parent
0068             ScrollBar.vertical: Kube.ScrollBar {}
0069             clip: true
0070 
0071             Kube.ScrollHelper {
0072                 anchors.fill: parent
0073                 flickable: flickableItem
0074             }
0075 
0076             //Fixed size content width
0077             contentWidth: width
0078             //Grow with the content, but at least height
0079             contentHeight: Math.max(edit.implicitHeight, height)
0080 
0081             function ensureVisible(r) {
0082                 if (contentX >= r.x) {
0083                     contentX = r.x
0084                 } else if (contentX+width <= r.x+r.width) {
0085                     contentX = r.x+r.width-width;
0086                 }
0087                 if (contentY >= r.y) {
0088                     contentY = r.y;
0089                 } else if (contentY+height <= r.y+r.height) {
0090                     contentY = r.y+r.height-height;
0091                 }
0092             }
0093 
0094 
0095             TextEdit {
0096                 id: edit
0097 
0098                 width: flickableItem.contentWidth
0099                 height: flickableItem.contentHeight
0100 
0101                 focus: true
0102                 selectByMouse: true
0103                 wrapMode: TextEdit.Wrap
0104                 textFormat: Qt.AutoText
0105                 onCursorRectangleChanged: flickableItem.ensureVisible(cursorRectangle)
0106 
0107                 color: Kube.Colors.textColor
0108                 font.family: Kube.Font.fontFamily
0109                 selectionColor: Kube.Colors.highlightColor
0110                 padding: Kube.Units.smallSpacing
0111                 MouseArea {
0112                     anchors.fill: parent
0113                     acceptedButtons: Qt.NoButton // we don't want to eat clicks on the Text
0114                     cursorShape: Qt.IBeamCursor
0115                 }
0116                 Label {
0117                     anchors {
0118                         top: parent.top
0119                         topMargin: parent.topPadding
0120                         left: parent.left
0121                         leftMargin: parent.leftPadding
0122                     }
0123                     visible: !root.text
0124                     text: root.placeholderText
0125                     font.family: Kube.Font.fontFamily
0126                     font.pointSize: Kube.Units.largeFontSize
0127                     color: Kube.Colors.disabledTextColor
0128                 }
0129             }
0130         }
0131     }
0132 }