Skip to content

Commit

Permalink
[office] Implement the interface for the search capability.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcaliste committed May 28, 2015
1 parent dbfe4d9 commit ac93c15
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 13 deletions.
2 changes: 1 addition & 1 deletion plugin/DocumentPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Page {
}
}

BusyIndicator { id: busyIndicator; anchors.centerIn: parent; size: BusyIndicatorSize.Large; }
BusyIndicator { id: busyIndicator; anchors.centerIn: parent; size: BusyIndicatorSize.Large; z: 1 }

Component.onDestruction: window.documentItem = null
Drawer {
Expand Down
63 changes: 52 additions & 11 deletions plugin/PDFDocumentPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ DocumentPage {
height: base.height;

document: pdfDocument;
searchModel: pdfDocument.searchModel

onClicked: base.open = !base.open;

Expand Down Expand Up @@ -91,11 +90,17 @@ DocumentPage {
parentHeight: base.height
flickable: view
hidden: base.open
autoHide: search.text.length == 0 && !search.activeFocus

// Toolbar contain.
Row {
id: row
height: parent.height
x: search.activeFocus ? -pageCount.width : 0

Behavior on x {
NumberAnimation { easing.type: Easing.InOutQuad; duration: 400 }
}

Item {
anchors.verticalCenter: parent.verticalCenter
Expand All @@ -117,17 +122,53 @@ DocumentPage {
onClicked: base.pushAttachedPage()
}
}
Item {
// Spacer, to be replaced later with a search field.
width: toolbar.width - pageCount.width - pdfTOC.width
height: parent.height
SearchField {
id: search
width: activeFocus ? toolbar.width : toolbar.width - pageCount.width - ( pdfDocument.searchModel ? searchPrev.width + searchNext.width : 0)
anchors.verticalCenter: parent.verticalCenter

enabled: !pdfDocument.searching

inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhPreferLowercase | Qt.ImhNoPredictiveText
EnterKey.onClicked: { focus = false; pdfDocument.search(text, view.currentPage - 1) }

Behavior on width {
NumberAnimation { easing.type: Easing.InOutQuad; duration: 400 }
}
}
IconButton {
id: searchPrev
anchors.verticalCenter: parent.verticalCenter
icon.source: "image://theme/icon-m-left"
enabled: pdfDocument.searchModel && pdfDocument.searchModel.count > 0
onClicked: view.prevSearchMatch()
}
IconButton {
id: pdfTOC
IconButton {
id: searchNext
anchors.verticalCenter: parent.verticalCenter
icon.source: "image://theme/icon-m-menu"
onClicked: base.pushAttachedPage()
}
icon.source: "image://theme/icon-m-right"
enabled: pdfDocument.searchModel && pdfDocument.searchModel.count > 0
onClicked: view.nextSearchMatch()
}
}
// Additional information
Label {
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom

opacity: pdfDocument.searchModel && !search.activeFocus ? 1. : 0.
visible: opacity > 0.
text: pdfDocument.searchModel && pdfDocument.searchModel.count > 0
//% "%n item(s) found"
? qsTrId("sailfish-office-lb-%n-matches", pdfDocument.searchModel.count)
//% "no matching found"
: qsTrId("sailfish-office-lb-no-matches")
font.pixelSize: Theme.fontSizeSmall
color: Theme.secondaryHighlightColor

Behavior on opacity {
FadeAnimation {}
}
}
}

Expand All @@ -136,7 +177,7 @@ DocumentPage {
source: base.path;
}

busy: !pdfDocument.loaded && !pdfDocument.failure;
busy: (!pdfDocument.loaded && !pdfDocument.failure) || pdfDocument.searching;
source: pdfDocument.source;
indexCount: pdfDocument.pageCount;

Expand Down
50 changes: 49 additions & 1 deletion plugin/PDFView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ SilicaFlickable {
property alias itemHeight: pdfCanvas.height;
property alias document: pdfCanvas.document;
property alias currentPage: pdfCanvas.currentPage
property alias searchModel: searchDisplay.model

property bool scaled: pdfCanvas.width != width;

Expand Down Expand Up @@ -75,6 +74,51 @@ SilicaFlickable {
contentY = oldContentY * pdfCanvas.height / oldHeight
}

function moveToSearchMatch(index) {
if (index < 0 || index >= searchDisplay.count) return

searchDisplay.currentIndex = index

var match = searchDisplay.itemAt(index)
var cX = match.x + match.width / 2. - width / 2.
transX.to = (cX < 0) ? 0 : (cX > pdfCanvas.width - width) ? pdfCanvas.width - width : cX
var cY = match.y + match.height / 2. - height / 2.
transY.to = (cY < 0) ? 0 : (cY > pdfCanvas.height - height) ? pdfCanvas.height - height : cY

scaleIn.target = match
scaleOut.target = match

focusSearchMatch.start()
}

function nextSearchMatch() {
if (searchDisplay.currentIndex + 1 >= searchDisplay.count) {
moveToSearchMatch(0)
} else {
moveToSearchMatch(searchDisplay.currentIndex + 1)
}
}

function prevSearchMatch() {
if (searchDisplay.currentIndex < 1) {
moveToSearchMatch(searchDisplay.count - 1)
} else {
moveToSearchMatch(searchDisplay.currentIndex - 1)
}
}

SequentialAnimation {
id: focusSearchMatch
ParallelAnimation {
NumberAnimation { id: transX; target: base; property: "contentX"; duration: 400; easing.type: Easing.InOutCubic }
NumberAnimation { id: transY; target: base; property: "contentY"; duration: 400; easing.type: Easing.InOutCubic }
}
SequentialAnimation {
NumberAnimation { id: scaleIn; property: "scale"; duration: 200; to: 3.; easing.type: Easing.InOutCubic }
NumberAnimation { id: scaleOut; property: "scale"; duration: 200; to: 1.; easing.type: Easing.InOutCubic }
}
}

// Ensure proper zooming level when device is rotated.
onWidthChanged: adjust()

Expand Down Expand Up @@ -106,6 +150,10 @@ SilicaFlickable {

Repeater {
id: searchDisplay
property int currentIndex
model: pdfCanvas.document.searchModel
onModelChanged: moveToSearchMatch(0)

delegate: Rectangle {
property int page: model.page
property rect pageRect: model.rect
Expand Down

0 comments on commit ac93c15

Please sign in to comment.