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 a page slider to the contents menu... #77

Open
wants to merge 2 commits 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
22 changes: 21 additions & 1 deletion epubreader/epubreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QBuffer>
#include <QDir>
#include <QCryptographicHash>
#include <QDebug>
#include "quazip/quazip.h"
#include "quazip/quazipfile.h"
#include "../qhttpserver/qhttpresponse.h"
Expand Down Expand Up @@ -189,7 +190,26 @@ bool EpubReader::parseOPF()
this->sortmetadata[name] = fileas;
}
}

// And construct the components weight list, based on file size
if (!this->metadata.contains("componentWeights")) {
quint32 totalSize = 0;
QVariantList componentWeights;
foreach(const QString filename, this->spine) {
this->zip->setCurrentFile(filename);
QuaZipFileInfo info;
this->zip->getCurrentFileInfo(&info);
totalSize += info.uncompressedSize;
}
foreach(const QString filename, this->spine) {
this->zip->setCurrentFile(filename);
QuaZipFileInfo info;
this->zip->getCurrentFileInfo(&info);
QVariant thisComponentWeight;
thisComponentWeight = (float)info.uncompressedSize / (float)totalSize;
componentWeights.append(thisComponentWeight.toDouble());
}
this->metadata["componentWeights"] = componentWeights;
}
// If this is an Epub3, we've already found the table of contents. If not,
// we'll get the Epub2 table of contents.
if (this->navhref == "")
Expand Down
12 changes: 11 additions & 1 deletion html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
Messaging.registerHandler("ChangePage", function(dir) {
reader.moveTo({direction: dir});
});
Messaging.registerHandler("SetPage", function(page, componentId) {
reader.moveTo({page: page, component: componentId});
});

reader.listen("monocle:link:external", function(event) {
Messaging.sendMessage("ExternalLink", event.m.href.external);
Expand All @@ -56,7 +59,14 @@
var place = reader.getPlace();
Messaging.sendMessage("PageChange", { chapterSrc: place.chapterSrc(),
componentId: place.componentId(),
percent: place.percentageThrough() });
percent: place.percentageThrough(),
pageSizePercentage: place.pageSizePercentage(),
percentageOfBook: place.percentageOfBook(),
pageNumber: place.pageNumber(),
componentWeights: place.properties.component.properties.book.componentWeights(),
componentIndex: place.properties.component.properties.index,
componentIds: place.properties.component.properties.book.properties.componentIds
});
});
}

Expand Down
87 changes: 85 additions & 2 deletions ui/BookPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Page {
property var currentChapter: null
property var history: new History.History(updateNavButtons)
property bool navjump: false
property int currentPageNumber: 1
property int totalPageCount: 1
property var componentStartPages: []

focus: true
Keys.onPressed: {
Expand Down Expand Up @@ -168,14 +171,75 @@ Page {
id: contentsPopover
property var defaultTimeout: null

Item {
id: pageChooser
anchors {
left: parent.left
leftMargin: units.gu(2)
rightMargin: units.gu(2)
right: parent.right
top: parent.top
}
height: units.gu(11)

Label {
fontSize: "medium"
anchors {
left: parent.left
right: parent.right
top: parent.top
topMargin: units.gu(2)
}
horizontalAlignment: Text.AlignHCenter
text: bookPage.currentPageNumber + " of " + bookPage.totalPageCount
}
Slider {
property bool valueHasBeenChangedByUser: false
anchors {
left: parent.left
right: parent.right
bottom: parent.bottom
}
minimumValue: 1
maximumValue: bookPage.totalPageCount
value: bookPage.currentPageNumber
live: false
onPressedChanged: valueHasBeenChangedByUser = true
onValueChanged: {
if (!valueHasBeenChangedByUser) return; // stops valuechanged firing when we open the dialog
console.log("go to page", value, JSON.stringify(bookPage.componentStartPages));
var componentIndex = 0, chosenComponent;
while (true) {
if (bookPage.componentStartPages[componentIndex].start > value) {
chosenComponent = bookPage.componentStartPages[componentIndex-1];
break;
}
componentIndex += 1;
if (componentIndex >= bookPage.componentStartPages.length) {
// overrun the end of the book. this shouldn't happen
chosenComponent = bookPage.componentStartPages[0];
console.log("overrun");
break;
}
}
console.log("now go to page",
Math.round(value - chosenComponent.start),
"in component", chosenComponent.id);
valueHasBeenChangedByUser = false;
Messaging.sendMessage("SetPage", Math.round(value - chosenComponent.start), chosenComponent.id);
}
}
}

ListView {
id: contentsListView
clip: true
anchors {
left: parent.left
right: parent.right
top: parent.top
top: pageChooser.bottom
}
height: 0.9*(bookPage.height - toolbar.height)
height: (0.9*(bookPage.height - toolbar.height)) - pageChooser.height

model: contentsListModel
delegate: Standard {
Expand Down Expand Up @@ -615,6 +679,24 @@ Page {
currentChapter = location.chapterSrc
setBookSetting("locus", { componentId: location.componentId,
percent: location.percent })

var pgInComponent = location.pageNumber;
var pcInComponent = location.percent;
var totalPagesInComponent = pgInComponent / pcInComponent;
var thisComponentWeight = location.componentWeights[location.componentIndex];
totalPageCount = Math.round(totalPagesInComponent / thisComponentWeight);
var pcBeforeComponent = 0;
for (var i = 0, ii = location.componentIndex; i < ii; ++i) { pcBeforeComponent += location.componentWeights[i]; }
var pagesBeforeComponent = Math.round(pcBeforeComponent * totalPageCount);
currentPageNumber = pgInComponent + pagesBeforeComponent;
if (componentStartPages.length == 0) {
var startpage = 0;
for (var i=0; i<location.componentIds.length; i++) {
componentStartPages.push({id: location.componentIds[i], start: startpage});
startpage += Math.max(Math.round(location.componentWeights[i] * totalPageCount), 1);
}
}
console.log("pgchg", pagesBeforeComponent, pcBeforeComponent, pgInComponent, totalPageCount);
}

function onReady() {
Expand All @@ -632,6 +714,7 @@ Page {
Messaging.registerHandler("PageChange", onPageChange)
Messaging.registerHandler("Styles", bookStyles.load)
Messaging.registerHandler("Ready", onReady)
Messaging.registerHandler("LogMessage", function(msg) { console.log("console:" + JSON.stringify(msg)); });
server.epub.contentsReady.connect(parseContents)
onWidthChanged.connect(windowSizeChanged)
onHeightChanged.connect(windowSizeChanged)
Expand Down