Warning, /graphics/peruse/src/creator/qml/LinkEditorSheet.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 /**
0031  * \brief A sheet used to perform inline edits on \<a href... type links in a TextArea
0032  * 
0033  * The sheet makes use of a TextDocumentEditor helper, which should be attached to the TextArea's textdocument instance
0034  */
0035 Kirigami.OverlaySheet {
0036     id: component;
0037     showCloseButton: true
0038     property Item textField;
0039     property QtObject editorHelper;
0040     property QtObject model;
0041     function edit() {
0042         var linkHref = editorHelper.linkHref(textField.cursorPosition);
0043         if (linkHref.length > 0) {
0044             linkDestination.text = linkHref;
0045             linkText.text = editorHelper.linkText(textField.cursorPosition);
0046             // Select the start and end of the link we're currently sat inside
0047             var linkStartEnd = editorHelper.linkStartEnd(textField.cursorPosition);
0048             textField.select(linkStartEnd.x, linkStartEnd.y);
0049         } else {
0050             linkDestination.text = "";
0051             // Even if we don't have a selection, just set the text of the link to the selected
0052             // text (empty text if we don't have one, and we need to clear the field anyway)
0053             linkText.text = textField.selectedText;
0054         }
0055         // set the start and end of the link to the current selection
0056         component.linkStart = textField.selectionStart;
0057         component.linkEnd = textField.selectionEnd;
0058         component.open();
0059     }
0060     // The start of the entire link, including anchor markup
0061     property int linkStart;
0062     // The end of the entire link, including anchor markup
0063     property int linkEnd;
0064 
0065     header: RowLayout {
0066         Kirigami.Heading {
0067             text: i18nc("title text for a sheet which lets the user edit the parameters of a link", "Edit Link");
0068             Layout.fillWidth: true;
0069             elide: Text.ElideRight;
0070         }
0071         QtControls.ToolButton {
0072             icon.name: "document-save";
0073             text: i18nc("label for a button which updates the link in the text with the new information from the link editor", "OK");
0074             onClicked: {
0075                 var theLink = "";
0076                 if (linkDestination.text.length > 0 && linkText.text.length > 0) {
0077                     theLink = "<a href=\"" + linkDestination.text + "\">" + linkText.text + "</a>";
0078                 } else if (linkDestination.text.length > 0) {
0079                     theLink = "<a href=\"" + linkDestination.text + "\">" + linkDestination.text + "</a>";
0080                 } else if (linkText.text.length > 0) {
0081                     theLink = linkText.text;
0082                 }
0083                 textField.remove(component.linkStart, component.linkEnd);
0084                 textField.insert(component.linkStart, theLink);
0085                 component.close();
0086             }
0087         }
0088     }
0089     Kirigami.FormLayout {
0090         QtControls.TextField {
0091             id: linkText;
0092             Layout.fillWidth: true;
0093             Kirigami.FormData.label: i18nc("Label for the link text input field", "Text");
0094             placeholderText: i18nc("Placeholder text for the link text input field", "Enter the text of your link here");
0095         }
0096         ColumnLayout {
0097             Kirigami.FormData.label: i18nc("Label for the link destination input field", "Destination");
0098             Layout.fillWidth: true;
0099             QtControls.TextField {
0100                 id: linkDestination;
0101                 Layout.fillWidth: true;
0102                 placeholderText: i18nc("Placeholder text for the link destination input field", "Enter the destination for your link here");
0103                 onTextChanged: {
0104                     if (linkDestination.text.startsWith("#")) {
0105                         linkDestinationOptionsFilter.setFilterFixedString(linkDestination.text.slice(1));
0106                     } else {
0107                         linkDestinationOptionsFilter.setFilterFixedString(linkDestination.text);
0108                     }
0109                 }
0110             }
0111             ListView {
0112                 Layout.fillWidth: true;
0113                 Layout.minimumHeight: Math.min(count, 10) * Kirigami.Units.gridUnit + Kirigami.Units.smallSpacing;
0114                 Layout.maximumHeight: Layout.minimumHeight;
0115                 clip: true;
0116                 model: Peruse.FilterProxy {
0117                     id: linkDestinationOptionsFilter;
0118                     filterRole: 257 // the ID role in IdentifiedObjectModel
0119                     sourceModel: Peruse.IdentifiedObjectModel {
0120                         document: component.model.acbfData;
0121                     }
0122                 }
0123                 delegate: QtControls.Label {
0124                     width: ListView.view.width;
0125                     text: {
0126                         switch(model.type) {
0127                             case Peruse.IdentifiedObjectModel.ReferenceType:
0128                                 return i18nc("Entry in a dropdown list which gives the name of a reference object, and identifies it as one such", "%1 (Reference)", (model.id.length > 0 ? model.id : "no ID"))
0129                                 break;
0130                             case Peruse.IdentifiedObjectModel.BinaryType:
0131                                 return i18nc("Entry in a dropdown list which gives the name of a binary object, and identifies it as one such", "%1 (Binary)", (model.id.length > 0 ? model.id : "no ID"))
0132                                 break;
0133                             case Peruse.IdentifiedObjectModel.TextareaType:
0134                                 return i18nc("Entry in a dropdown list which gives the base details of a Textarea object, and identifies it as one such", "Textarea (ID: %1, text: %2)", (model.id.length > 0 ? model.id : "no ID"), model.object.paragraphs[0]);
0135                                 break;
0136                             case Peruse.IdentifiedObjectModel.FrameType:
0137                                 return i18nc("Entry in a dropdown list which gives the name of a frame object, and identifies it as one such", "%1 (Frame)", (model.id.length > 0 ? model.id : "no ID"))
0138                                 break;
0139                             case Peruse.IdentifiedObjectModel.PageType:
0140                                 return i18nc("Entry in a dropdown list which gives the title and ID of a page", "Page \"%1\" (ID: %2)", model.object.title(), (model.id.length > 0 ? model.id : "no ID"));
0141                                 break;
0142                             case Peruse.IdentifiedObjectModel.UnknownType:
0143                             default:
0144                                 return i18nc("Entry in a dropdown list which gives the name of an identified object of an unknown type, and marks it as one such", "%1 (Unknown Type)", model.id)
0145                                 break;
0146                         }
0147                     }
0148                     elide: Text.ElideRight;
0149                 }
0150             }
0151         }
0152         QtControls.Label {
0153             id: linkDemonstration;
0154             Layout.fillWidth: true;
0155             Kirigami.FormData.label: i18nc("Label for the link demonstration display field", "Demonstration");
0156             textFormat: Text.StyledText
0157             text: {
0158                 if (linkDestination.text.length > 0 && linkText.text.length > 0) {
0159                     return "<a href=\"" + linkDestination.text + "\">" + linkText.text + "</a>";
0160                 } else if (linkDestination.text.length > 0) {
0161                     return "<a href=\"" + linkDestination.text + "\">" + linkDestination.text + "</a>";
0162                 } else if (linkText.text.length > 0) {
0163                     return linkText.text;
0164                 }
0165                 return "";
0166             }
0167         }
0168     }
0169 }