Warning, /graphics/peruse/src/creator/qml/BookReferenceEditor.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * Copyright (C) 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  *
0020  */
0021 
0022 import QtQuick 2.12
0023 import QtQuick.Layouts 1.4
0024 import QtQuick.Controls 2.12 as QtControls
0025 
0026 import org.kde.kirigami 2.13 as Kirigami
0027 
0028 import org.kde.peruse 0.1 as Peruse
0029 /**
0030  * @brief the page which shows the references contained within the book
0031  */
0032 Kirigami.ScrollablePage {
0033     id: root;
0034     property string categoryName: "bookReferences";
0035     title: referenceLanguage.text === ""
0036         ? i18nc("title text for the reference editor page, when the reference has no language set", "Editing %1 (default language)", referenceIdentifier.text)
0037         : i18nc("title text for the reference editor page", "Editing %1 (language: %2)", referenceIdentifier.text, referenceLanguage.text);
0038     property QtObject reference;
0039     property QtObject model;
0040     onReferenceChanged: {
0041         textArea.text = reference.paragraphs.join("\n");
0042         referenceIdentifier.text = root.reference.id;
0043         referenceLanguage.text = root.reference.language;
0044     }
0045     function saveReference() {
0046         reference.id = referenceIdentifier.text;
0047         reference.language = referenceLanguage.text;
0048         reference.paragraphs = textDocumentEditor.paragraphs();
0049     }
0050 
0051     actions {
0052         contextualActions: textActions;
0053         main: okCloseAction;
0054         right: saveReferenceAction;
0055         left: abortChangesAction;
0056     }
0057     property list<QtObject> textActions: [
0058         Kirigami.Action {
0059             text: i18nc("Opens a sheet which lets the user change the name and language of the reference", "Edit Details...");
0060             icon.name: "documentinfo";
0061             onTriggered: referenceDetails.open();
0062         },
0063         Kirigami.Action { separator: true; },
0064         Kirigami.Action {
0065             text: i18nc("Copies the selected text", "Copy");
0066             icon.name: "edit-copy";
0067             enabled: textArea.selectedText;
0068             onTriggered: textArea.copy();
0069         },
0070         Kirigami.Action {
0071             text: i18nc("Cuts the selected text", "Cut");
0072             icon.name: "edit-cut";
0073             enabled: textArea.selectedText;
0074             onTriggered: textArea.cut();
0075         },
0076         Kirigami.Action {
0077             text: i18nc("Pastes the clipboard contents into the current location", "Paste");
0078             icon.name: "edit-paste";
0079             enabled: textArea.canPaste;
0080             onTriggered: textArea.paste();
0081         },
0082         Kirigami.Action { separator: true; },
0083         Kirigami.Action {
0084             text: i18nc("Edit the link the cursor is currently positioned on (or convert the selection to a link, or add a new one if there is no selection)", "Edit Link");
0085             icon.name: "edit-link"
0086             onTriggered: linkDetails.edit();
0087         }
0088     ]
0089     Kirigami.Action {
0090         id: okCloseAction;
0091         text: i18nc("Save the reference and close the editor", "Save and Close Editor");
0092         icon.name: "dialog-ok";
0093         onTriggered: {
0094             root.saveReference();
0095             pageStack.pop();
0096         }
0097     }
0098     Kirigami.Action {
0099         id: saveReferenceAction;
0100         text: i18nc("Save the reference and keep editing", "Save");
0101         icon.name: "document-save";
0102         onTriggered: {
0103             root.saveReference();
0104         }
0105     }
0106     Kirigami.Action {
0107         id: abortChangesAction;
0108         text: i18nc("Abort the changes made in the editor, and close the editor", "Abort Changes");
0109         icon.name: "dialog-cancel";
0110         onTriggered: {
0111             pageStack.pop();
0112         }
0113     }
0114 
0115     Kirigami.OverlaySheet {
0116         id: referenceDetails;
0117         showCloseButton: true
0118 
0119         header: RowLayout {
0120             Kirigami.Heading {
0121                 text: i18nc("title text for a sheet which lets the user edit the details of a reference", "Edit Reference Details");
0122                 Layout.fillWidth: true;
0123                 elide: Text.ElideRight;
0124             }
0125         }
0126         Kirigami.FormLayout {
0127             QtControls.TextField {
0128                 id: referenceIdentifier;
0129                 Layout.fillWidth: true;
0130                 Kirigami.FormData.label: i18nc("Label for the reference identifier input field", "Unique Identifier");
0131                 placeholderText: i18nc("Placeholder text for the reference identifier input field", "Enter your reference ID here (should be unique)");
0132             }
0133             QtControls.TextField {
0134                 id: referenceLanguage;
0135                 Layout.fillWidth: true;
0136                 Kirigami.FormData.label: i18nc("Label for the reference language input field", "Language");
0137                 placeholderText: i18nc("Placeholder text for the reference language input field", "Enter the language ID here (leave empty for the document default language)");
0138             }
0139         }
0140     }
0141 
0142     LinkEditorSheet {
0143         id: linkDetails;
0144         textField: textArea;
0145         editorHelper: textDocumentEditor;
0146         model: root.model;
0147     }
0148 
0149     TextEdit {
0150         id: textArea
0151         textFormat: Qt.RichText
0152         wrapMode: TextEdit.Wrap
0153         focus: true
0154         selectByMouse: true
0155         persistentSelection: true
0156 
0157         Peruse.TextDocumentEditor {
0158             id: textDocumentEditor;
0159             textDocument: textArea.textDocument;
0160         }
0161         MouseArea {
0162             acceptedButtons: Qt.RightButton
0163             anchors.fill: parent
0164             onClicked: {
0165                 mainWindow.contextDrawer.open()
0166             }
0167         }
0168 
0169         function ensureVisible(rectToMakeVisible)
0170         {
0171             if (root.flickable.contentX >= rectToMakeVisible.x) {
0172                 root.flickable.contentX = rectToMakeVisible.x;
0173             } else if (root.flickable.contentX + root.flickable.width <= rectToMakeVisible.x + rectToMakeVisible.width) {
0174                 root.flickable.contentX = rectToMakeVisible.x + rectToMakeVisible.width - root.flickable.width;
0175             }
0176             if (root.flickable.contentY >= rectToMakeVisible.y) {
0177                 root.flickable.contentY = rectToMakeVisible.y;
0178             } else if (root.flickable.contentY + root.flickable.height <= rectToMakeVisible.y + rectToMakeVisible.height) {
0179                 root.flickable.contentY = rectToMakeVisible.y + rectToMakeVisible.height - root.flickable.height;
0180             }
0181         }
0182         onCursorRectangleChanged: {
0183             ensureVisible(cursorRectangle);
0184         }
0185         onLinkActivated: {
0186             // This is the nastiest hack... for some reason, clicking a link does not position
0187             // the cursor where you clicked, but rather /after/ the link you clicked. Not helpful.
0188             textArea.cursorPosition = textArea.cursorPosition - 1;
0189             linkDetails.edit();
0190         }
0191     }
0192 }