Guest User

Sailfish Office with dictionary

a guest
Dec 25th, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 21.40 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2013-2014 Jolla Ltd.
  3.  * Contact: Robin Burchell <[email protected]>
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License
  7.  * as published by the Free Software Foundation; version 2 only.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  */
  18.  
  19. import QtQuick 2.0
  20. import Sailfish.Silica 1.0
  21. import Sailfish.Office.PDF 1.0 as PDF
  22. import org.nemomobile.configuration 1.0
  23. import org.nemomobile.notifications 1.0
  24. import QtQuick.LocalStorage 2.0
  25. import "PDFStorage.js" as PDFStorage
  26. import "js/yandex.js" as YandexAPI
  27. //import "google-translate/index.js" as Google
  28. DocumentPage {
  29.     id: base
  30.  
  31.     property var _settings // Handle save and restore the view settings using PDFStorage
  32.     property ContextMenu contextMenuLinks
  33.     property ContextMenu contextMenuText
  34.     property ContextMenu contextMenuHighlight
  35.  
  36.     busy: (!pdfDocument.loaded && !pdfDocument.failure) || pdfDocument.searching
  37.     source: pdfDocument.source
  38.     indexCount: pdfDocument.pageCount
  39.     drawerVisible: !(pdfDocument.failure || pdfDocument.locked)
  40.     documentItem: view
  41.  
  42.     function savePageSettings() {
  43.         if (!rememberPositionConfig.value || pdfDocument.failure || pdfDocument.locked) {
  44.             return
  45.         }
  46.        
  47.         if (!_settings) {
  48.             _settings = new PDFStorage.Settings(pdfDocument.source)
  49.         }
  50.         var last = view.getPagePosition()
  51.         _settings.setLastPage(last[0] + 1, last[1], last[2], view.itemWidth)
  52.     }
  53.  
  54.     attachedPage: Component {
  55.         PDFDocumentToCPage {
  56.             tocModel: pdfDocument.tocModel
  57.             pageCount: pdfDocument.pageCount
  58.             onPageSelected: view.goToPage(pageNumber)
  59.         }
  60.     }
  61.  
  62.     // Save and restore view settings when needed.
  63.     onStatusChanged: if (status == PageStatus.Inactive) { savePageSettings() }
  64.  
  65.     Connections {
  66.         target: Qt.application
  67.         onAboutToQuit: savePageSettings()
  68.     }
  69.     Connections {
  70.         target: view
  71.         onPageSizesReady: {
  72.             if (rememberPositionConfig.value) {
  73.                 if (!_settings) {
  74.                     _settings = new PDFStorage.Settings(pdfDocument.source)
  75.                 }
  76.                 var last = _settings.getLastPage()
  77.                 if (last[3] > 0) {
  78.                     view.itemWidth = last[3]
  79.                     view.adjust()
  80.                 }
  81.                 view.goToPage( last[0] - 1, last[1], last[2] )
  82.             }
  83.         }
  84.     }
  85.  
  86.     Binding {
  87.         target: base
  88.         property: "forwardNavigation"
  89.         value: false
  90.         when: (pdfDocument.failure || pdfDocument.locked)
  91.     }
  92.  
  93.     Loader {
  94.         parent: base
  95.         sourceComponent: (pdfDocument.failure || pdfDocument.locked) ? placeholderComponent : null
  96.         anchors.verticalCenter: parent.verticalCenter
  97.     }
  98.  
  99.     PDFView {
  100.         id: view
  101.  
  102.         // for cover state
  103.         property bool contentAvailable: pdfDocument.loaded && !(pdfDocument.failure || pdfDocument.locked)
  104.         property alias title: base.title
  105.         property alias mimeType: base.mimeType
  106.  
  107.         anchors.fill: parent
  108.         anchors.bottomMargin: toolbar.offset
  109.         document: pdfDocument
  110.         onCanMoveBackChanged: if (canMoveBack) toolbar.show()
  111.         onClicked: base.open = !base.open
  112.         onLinkClicked: {
  113.             base.open = false
  114.             if (!contextMenuLinks) {
  115.                 contextMenuLinks = contextMenuLinksComponent.createObject(base)
  116.             }
  117.             contextMenuLinks.url = linkTarget
  118.             hook.showMenu(contextMenuLinks)
  119.         }
  120.         onAnnotationClicked: {
  121.             base.open = false
  122.             switch (annotation.type) {
  123.             case PDF.Annotation.Highlight:
  124.                 if (!contextMenuHighlight) {
  125.                     contextMenuHighlight = contextMenuHighlightComponent.createObject(base)
  126.                 }
  127.                 contextMenuHighlight.annotation = annotation
  128.                 hook.showMenu(contextMenuHighlight)
  129.                 break
  130.             case PDF.Annotation.Caret:
  131.             case PDF.Annotation.Text:
  132.                 pdfDocument.edit(annotation)
  133.                 break
  134.             default:
  135.             }
  136.         }
  137.         onAnnotationLongPress: {
  138.             base.open = false
  139.             switch (annotation.type) {
  140.             case PDF.Annotation.Highlight:
  141.                 if (!contextMenuHighlight) {
  142.                     contextMenuHighlight = contextMenuHighlightComponent.createObject(base)
  143.                 }
  144.                 contextMenuHighlight.annotation = annotation
  145.                 hook.showMenu(contextMenuHighlight)
  146.                 break
  147.             case PDF.Annotation.Caret:
  148.             case PDF.Annotation.Text:
  149.                 if (!contextMenuText) {
  150.                     contextMenuText = contextMenuTextComponent.createObject(base)
  151.                 }
  152.                 contextMenuText.annotation = annotation
  153.                 hook.showMenu(contextMenuText)
  154.                 break
  155.             default:
  156.             }
  157.         }
  158.         onLongPress: {
  159.             base.open = false
  160.             if (!contextMenuText) {
  161.                 contextMenuText = contextMenuTextComponent.createObject(base)
  162.             }
  163.             contextMenuText.at = pressAt
  164.             contextMenuText.annotation = null
  165.             hook.showMenu(contextMenuText)
  166.         }
  167.         clip: anchors.bottomMargin > 0 || base.status !== PageStatus.Active
  168.     }
  169.     RemorsePopup {id: remorse}
  170.     TextArea {
  171.           id:translated
  172.           visible:false
  173.     }
  174.     ToolBar {
  175.         id: toolbar
  176.  
  177.         property Notification notice
  178.  
  179.         width: parent.width
  180.         height: base.orientation == Orientation.Portrait
  181.                 || base.orientation == Orientation.InvertedPortrait
  182.                 ? Theme.itemSizeLarge
  183.                 : Theme.itemSizeSmall
  184.         anchors.top: view.bottom
  185.         flickable: view
  186.         forceHidden: base.open || pdfDocument.failure || pdfDocument.locked
  187.                      || (contextMenuLinks && contextMenuLinks.active)
  188.                      || (contextMenuHighlight && contextMenuHighlight.active)
  189.                      || (contextMenuText && contextMenuText.active)
  190.         autoShowHide: !row.active
  191.  
  192.         function noticeShow(message) {
  193.             if (!notice) {
  194.                 notice = noticeComponent.createObject(toolbar)
  195.             }
  196.             notice.show(message)
  197.         }
  198.  
  199.         Connections {
  200.             target: view.selection
  201.             onSelectedChanged: if (view.selection.selected){
  202.                 toolbar.show()
  203.                 //Google.translate(view.selection.text,function(r) {translated.text=r})
  204.                 YandexAPI.translate(view.selection.text,["en"],["pl"],function(r){translated.text=r})                
  205.                 //remorse.execute(view.selection.text,remorse.close,1000)
  206.                 remorse.execute(translated.text,remorse.close,1000)
  207.         }
  208.         }
  209.         Component {
  210.             id: noticeComponent
  211.             Notification {
  212.                 property bool published
  213.                 function show(info) {
  214.                     previewSummary = info
  215.                     if (published) close()
  216.                     publish()
  217.                     published = true
  218.                 }
  219.                 function hide() {
  220.                     if (published) close()
  221.                     published = false
  222.                 }
  223.             }
  224.         }
  225.  
  226.         // Toolbar contain.
  227.         Row {
  228.             id: row
  229.             property bool active: pageCount.highlighted
  230.                                   || linkBack.visible
  231.                                   || search.highlighted
  232.                                   || !search.iconized
  233.                                   || textButton.highlighted
  234.                                   || highlightButton.highlighted
  235.                                   || view.selection.selected
  236.             property Item activeItem
  237.             property int nVisibleChildren: children.length - (linkBack.visible ? 0 : 1)
  238.             property real itemWidth: Math.max(toolbar.width - pageCount.width, 0)
  239.                                      / (nVisibleChildren - 1)
  240.             height: parent.height
  241.  
  242.             function toggle(item) {
  243.                 if (toolbar.notice) toolbar.notice.hide()
  244.                 view.selection.unselect()
  245.                 if (row.activeItem === item) {
  246.                     row.activeItem = null
  247.                 } else {
  248.                     row.activeItem = item
  249.                 }
  250.             }
  251.  
  252.             SearchBarItem {
  253.                 id: search
  254.                 width: toolbar.width
  255.                 iconizedWidth: row.itemWidth
  256.                 height: parent.height
  257.  
  258.                 searching: pdfDocument.searching
  259.                 searchProgress: pdfDocument.searchModel ? pdfDocument.searchModel.fraction : 0.
  260.                 matchCount: pdfDocument.searchModel ? pdfDocument.searchModel.count : -1
  261.  
  262.                 onRequestSearch: pdfDocument.search(text, view.currentPage - 1)
  263.                 onRequestPreviousMatch: view.prevSearchMatch()
  264.                 onRequestNextMatch: view.nextSearchMatch()
  265.                 onRequestCancel: pdfDocument.cancelSearch(!pdfDocument.searching)
  266.                 onClicked: row.toggle(search)
  267.             }
  268.             BackgroundItem {
  269.                 id: textTool
  270.                 property bool first: true
  271.  
  272.                 width: row.itemWidth
  273.                 height: parent.height
  274.                 highlighted: pressed || textButton.pressed
  275.                 onClicked: {
  276.                     row.toggle(textTool)
  277.                     if (textTool.first) {
  278.                         //% "Tap where you want to add a note"
  279.                         toolbar.noticeShow(qsTrId("sailfish-office-la-notice-anno-text"))
  280.                         textTool.first = false
  281.                     }
  282.                 }
  283.                 IconButton {
  284.                     id: textButton
  285.                     anchors.centerIn: parent
  286.                     highlighted: pressed || textTool.pressed || row.activeItem === textTool
  287.                     icon.source: row.activeItem === textTool ? "image://theme/icon-m-annotation-selected"
  288.                                                              : "image://theme/icon-m-annotation"
  289.                     onClicked: textTool.clicked(mouse)
  290.                 }
  291.                 MouseArea {
  292.                     parent: row.activeItem === textTool ? view : null
  293.                     anchors.fill: parent
  294.                     onClicked: {
  295.                         var annotation = textComponent.createObject(textTool)
  296.                         var pt = Qt.point(view.contentX + mouse.x,
  297.                                           view.contentY + mouse.y)
  298.                         pdfDocument.create(annotation,
  299.                                            function() {
  300.                                                var at = view.getPositionAt(pt)
  301.                                                annotation.attachAt(pdfDocument,
  302.                                                                    at[0], at[2], at[1])
  303.                                            })
  304.                         row.toggle(textTool)
  305.                     }
  306.                     Component {
  307.                         id: textComponent
  308.                         PDF.TextAnnotation { }
  309.                     }
  310.                 }
  311.             }
  312.             BackgroundItem {
  313.                 id: highlightTool
  314.                 property bool first: true
  315.  
  316.                 function highlightSelection() {
  317.                     var anno = highlightComponent.createObject(highlightTool)
  318.                     anno.color = highlightColorConfig.value
  319.                     anno.style = highlightStyleConfig.toEnum(highlightStyleConfig.value)
  320.                     anno.attach(pdfDocument, view.selection)
  321.                     toolbar.hide()
  322.                 }
  323.  
  324.                 width: row.itemWidth
  325.                 height: parent.height
  326.                 highlighted: pressed || highlightButton.pressed
  327.                 onClicked: {
  328.                     if (view.selection.selected) {
  329.                         highlightSelection()
  330.                         view.selection.unselect()
  331.                         return
  332.                     }
  333.                     row.toggle(highlightTool)
  334.                     if (highlightTool.first) {
  335.                         //% "Tap and move your finger over the area"
  336.                         toolbar.noticeShow(qsTrId("sailfish-office-la-notice-anno-highlight"))
  337.                         highlightTool.first = false
  338.                     }
  339.                 }
  340.  
  341.                 Component {
  342.                     id: highlightComponent
  343.                     PDF.HighlightAnnotation { }
  344.                 }
  345.  
  346.                 IconButton {
  347.                     id: highlightButton
  348.                     anchors.centerIn: parent
  349.                     highlighted: pressed || highlightTool.pressed || row.activeItem === highlightTool
  350.                     icon.source: row.activeItem === highlightTool ? "image://theme/icon-m-edit-selected"
  351.                                                                   : "image://theme/icon-m-edit"
  352.                     onClicked: highlightTool.clicked(mouse)
  353.                 }
  354.                 MouseArea {
  355.                     parent: row.activeItem === highlightTool ? view : null
  356.                     anchors.fill: parent
  357.                     preventStealing: true
  358.                     onPressed: {
  359.                         view.selection.selectAt(Qt.point(view.contentX + mouse.x,
  360.                                                          view.contentY + mouse.y))
  361.                     }
  362.                     onPositionChanged: {
  363.                         if (view.selection.count < 1) {
  364.                             view.selection.selectAt(Qt.point(view.contentX + mouse.x,
  365.                                                              view.contentY + mouse.y))
  366.                         } else {
  367.                             view.selection.handle2 = Qt.point(view.contentX + mouse.x,
  368.                                                               view.contentY + mouse.y)
  369.                         }
  370.                     }
  371.                     onReleased: {
  372.                         if (view.selection.selected) highlightTool.highlightSelection()
  373.                         row.toggle(highlightTool)
  374.                     }
  375.                     Binding {
  376.                         target: view
  377.                         property: "selectionDraggable"
  378.                         value: row.activeItem !== highlightTool
  379.                     }
  380.                 }
  381.             }
  382.             BackgroundItem {
  383.                 id: linkBack
  384.                 width: row.itemWidth
  385.                 height: parent.height
  386.                 highlighted: pressed || backButton.pressed
  387.                 opacity: view.canMoveBack ? 1. : 0.
  388.                 visible: opacity > 0
  389.                 Behavior on opacity { FadeAnimation{ duration: 400 } }
  390.                 IconButton {
  391.                     id: backButton
  392.                     anchors.centerIn: parent
  393.                     highlighted: pressed || linkBack.pressed
  394.                     icon.source: "image://theme/icon-m-back"
  395.                     onClicked: linkBack.clicked(mouse)
  396.                 }
  397.                 onClicked: {
  398.                     row.toggle(linkBack)
  399.                     view.moveBack()
  400.                     toolbar.hide()
  401.                 }
  402.             }
  403.             BackgroundItem {
  404.                 id: pageCount
  405.                 width: screen.sizeCategory <= Screen.Medium
  406.                        ? Math.max(toolbar.width / row.nVisibleChildren, Screen.width / 4)
  407.                        : toolbar.width / row.nVisibleChildren
  408.                 height: parent.height
  409.                 Label {
  410.                     id: pageLabel
  411.                     anchors.centerIn: parent
  412.                     width: Math.min(parent.width - Theme.paddingSmall, implicitWidth)
  413.                     fontSizeMode: Text.HorizontalFit
  414.                     color: pageCount.highlighted ? Theme.highlightColor : Theme.primaryColor
  415.                     text: view.currentPage + " | " + view.document.pageCount
  416.                 }
  417.                 onClicked: {
  418.                     row.toggle(pageCount)
  419.                     base.pushAttachedPage()
  420.                 }
  421.             }
  422.         }
  423.     }
  424.  
  425.     PDF.Document {
  426.         id: pdfDocument
  427.         source: base.path
  428.         autoSavePath: base.path
  429.  
  430.         function create(annotation, callback) {
  431.             var isText = (annotation.type == PDF.Annotation.Text
  432.                           || annotation.type == PDF.Annotation.Caret)
  433.             var dialog = pageStack.push(Qt.resolvedUrl("PDFAnnotationNew.qml"),
  434.                                         {"isTextAnnotation": isText})
  435.             dialog.accepted.connect(function() {
  436.                 annotation.contents = dialog.text
  437.             })
  438.             if (callback !== undefined) dialog.accepted.connect(callback)
  439.         }
  440.         function edit(annotation) {
  441.             var edit = pageStack.push(Qt.resolvedUrl("PDFAnnotationEdit.qml"),
  442.                                       {"annotation": annotation})
  443.             edit.remove.connect(function() {
  444.                 pageStack.pop()
  445.                 annotation.remove()
  446.             })
  447.         }
  448.     }
  449.  
  450.     Component {
  451.         id: placeholderComponent
  452.  
  453.         Column {
  454.             width: base.width
  455.  
  456.             InfoLabel {
  457.                 text: pdfDocument.failure ? //% "Broken file"
  458.                                             qsTrId("sailfish-office-me-broken-pdf")
  459.                                           : //% "Locked file"
  460.                                             qsTrId("sailfish-office-me-locked-pdf")
  461.             }
  462.  
  463.             InfoLabel {
  464.                 font.pixelSize: Theme.fontSizeLarge
  465.                 color: Theme.rgba(Theme.highlightColor, 0.4)
  466.                 text: pdfDocument.failure ? //% "Cannot read the PDF document"
  467.                                             qsTrId("sailfish-office-me-broken-pdf-hint")
  468.                                           : //% "Enter password to unlock"
  469.                                             qsTrId("sailfish-office-me-locked-pdf-hint")
  470.             }
  471.  
  472.             Item {
  473.                 visible:password.visible
  474.                 width: 1
  475.                 height: Theme.paddingLarge
  476.             }
  477.  
  478.             PasswordField {
  479.                 id: password
  480.  
  481.                 visible: pdfDocument.locked
  482.                 x: Theme.horizontalPageMargin
  483.                 width: parent.width - 2*x
  484.                 EnterKey.enabled: text
  485.                 EnterKey.iconSource: "image://theme/icon-m-enter-accept"
  486.                 EnterKey.onClicked: {
  487.                     focus = false
  488.                     pdfDocument.requestUnLock(text)
  489.                     text = ""
  490.                 }
  491.  
  492.                 Component.onCompleted: {
  493.                     if (visible)
  494.                         forceActiveFocus()
  495.                 }
  496.             }
  497.         }
  498.     }
  499.  
  500.     Component {
  501.         id: contextMenuLinksComponent
  502.         PDFContextMenuLinks { }
  503.     }
  504.  
  505.     Component {
  506.         id: contextMenuTextComponent
  507.         PDFContextMenuText { }
  508.     }
  509.  
  510.     Component {
  511.         id: contextMenuHighlightComponent
  512.         PDFContextMenuHighlight { }
  513.     }
  514.  
  515.     ConfigurationValue {
  516.         id: rememberPositionConfig
  517.        
  518.         key: "/apps/sailfish-office/settings/rememberPosition"
  519.         defaultValue: true
  520.     }
  521.     ConfigurationValue {
  522.        id: highlightColorConfig
  523.        key: "/apps/sailfish-office/settings/highlightColor"
  524.        defaultValue: "#ffff00"
  525.     }
  526.     ConfigurationValue {
  527.        id: highlightStyleConfig
  528.        key: "/apps/sailfish-office/settings/highlightStyle"
  529.        defaultValue: "highlight"
  530.        
  531.        function toEnum(configVal) {
  532.            if (configVal == "highlight") {
  533.                return PDF.HighlightAnnotation.Highlight
  534.            } else if (configVal == "squiggly") {
  535.                return PDF.HighlightAnnotation.Squiggly
  536.            } else if (configVal == "underline") {
  537.                return PDF.HighlightAnnotation.Underline
  538.            } else if (configVal == "strike") {
  539.                return PDF.HighlightAnnotation.StrikeOut
  540.            } else {
  541.                return PDF.HighlightAnnotation.Highlight
  542.            }
  543.        }
  544.        function fromEnum(enumVal) {
  545.             switch (enumVal) {
  546.             case PDF.HighlightAnnotation.Highlight:
  547.                 return "highlight"
  548.             case PDF.HighlightAnnotation.Squiggly:
  549.                 return "squiggly"
  550.             case PDF.HighlightAnnotation.Underline:
  551.                 return "underline"
  552.             case PDF.HighlightAnnotation.StrikeOut:
  553.                 return "strike"
  554.             default:
  555.                 return "highlight"
  556.             }
  557.        }
  558.     }
  559.  
  560.     Timer {
  561.         id: updateSourceSizeTimer
  562.         interval: 5000
  563.         onTriggered: linkArea.sourceSize = Qt.size(base.width, pdfCanvas.height)
  564.     }
  565. }
Advertisement
Add Comment
Please, Sign In to add comment