Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chapter title and page number info to the book page #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,33 @@
});
reader.listen("monocle:pagechange", function(event) {
var place = reader.getPlace();

// Following code taken from Monacle to estimate overall pages
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling


var book = place.properties.component.properties.book;
var componentIds = book.properties.componentIds;
var weights = book.componentWeights();
var cmptIndex = place.properties.component.properties.index;
var pc = weights[cmptIndex] * place.properties.percent;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume pc is short for "percent". Either way, let's spell it out.

for (var i = 0, ii = cmptIndex; i < ii; ++i) { pc += weights[i]; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why introduce ii?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, put the loop code on a new line.


// Note: This is a decent estimation of current page number and total
// number of pages, but it's very approximate. Could be improved by storing
// the page counts of all components accessed (since the dimensions of the
// reader last changed), and averaging the result across them. (You
// probably want to ignore calcs for components < 2 or 3 pages long, too.
// The bigger the component, the more accurate the calculation.)
//
var bkPages = place.properties.component.lastPageNumber() / weights[cmptIndex];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really need to be calculated every page turn? Couldn't it be cached and only recalculated when the current component changed?


Messaging.sendMessage("PageChange", { chapterSrc: place.chapterSrc(),
componentId: place.componentId(),
percent: place.percentageThrough() });
percent: place.percentageThrough(),
title: place.chapterTitle(),
page: Math.floor(pc * bkPages),
pages: Math.floor(bkPages),
totalPercentage: pc
});
});
}

Expand Down
40 changes: 39 additions & 1 deletion ui/BookPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Page {
property var history: new History.History(updateNavButtons)
property bool navjump: false

property string chapterTitle
property int pageNumber
property int pageCount
property real totalPercent

focus: true
Keys.onPressed: {
if (event.key == Qt.Key_Right || event.key == Qt.Key_Down || event.key == Qt.Key_Space
Expand All @@ -52,10 +57,38 @@ Page {
ListModel {
id: contentsListModel
}

Label {
id: header
anchors {
horizontalCenter: parent.horizontalCenter
top: parent.top
topMargin: units.gu(2)
}
text: chapterTitle
fontSize: "large"
}

Label {
id: footer
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
margins: units.gu(2)
}

text: i18n.tr("Page %1 of %2").arg(pageNumber).arg(pageCount)
}

WebView {
id: bookWebView
anchors.fill: parent
anchors {
left: parent.left
right: parent.right
top: header.bottom
bottom: footer.top
}

visible: false
focus: false

Expand Down Expand Up @@ -505,6 +538,11 @@ Page {
currentChapter = location.chapterSrc
setBookSetting("locus", { componentId: location.componentId,
percent: location.percent })

chapterTitle = location.title
pageNumber = location.page
pageCount = location.pages
totalPercent = location.totalPercentage
}

function onReady() {
Expand Down