Merge pull request #452 from chrox/master

add goto link function for CreDocument
This commit is contained in:
Qingping Hou
2014-01-15 08:52:39 -08:00
5 changed files with 180 additions and 77 deletions

View File

@@ -202,6 +202,10 @@ function CreDocument:getCurrentPos()
return self._document:getCurrentPos()
end
function CreDocument:getPageLinks()
return self._document:getPageLinks()
end
function Document:gotoPos(pos)
DEBUG("CreDocument: goto position", pos)
self._document:gotoPos(pos)
@@ -212,6 +216,21 @@ function CreDocument:gotoPage(page)
self._document:gotoPage(page)
end
function CreDocument:gotoLink(link)
DEBUG("CreDocument: goto link", link)
self._document:gotoLink(link)
end
function CreDocument:goBack()
DEBUG("CreDocument: go back")
self._document:goBack()
end
function CreDocument:goForward(link)
DEBUG("CreDocument: go forward")
self._document:goForward()
end
function CreDocument:getCurrentPage()
return self._document:getCurrentPage()
end

View File

@@ -0,0 +1,71 @@
local InputContainer = require("ui/widget/container/inputcontainer")
local GestureRange = require("ui/gesturerange")
local Geom = require("ui/geometry")
local Screen = require("ui/screen")
local Device = require("ui/device")
local Event = require("ui/event")
local DEBUG = require("dbg")
local ReaderLink = InputContainer:new{}
function ReaderLink:init()
if Device:isTouchDevice() then
self:initGesListener()
end
end
function ReaderLink:initGesListener()
if Device:isTouchDevice() then
self.ges_events = {
Tap = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight()
}
}
},
}
end
end
function ReaderLink:onSetDimensions(dimen)
-- update listening according to new screen dimen
if Device:isTouchDevice() then
self:initGesListener()
end
end
function ReaderLink:onTap(arg, ges)
local function inside_box(pos, box)
if pos then
local x, y = pos.x, pos.y
if box.x <= x and box.y <= y
and box.x + box.w >= x
and box.y + box.h >= y then
return true
end
end
end
if self.view.links then
local pos = self.view:screenToPageTransform(ges.pos)
for i = 1, #self.view.links do
local link = self.view.links[i]
local lbox = Geom:new{
x = link.start_x, y = link.start_y,
w = link.end_x - link.start_x,
h = link.end_y - link.start_y > 0 and link.end_y - link.start_y or 30,
}
if inside_box(pos, lbox) then
DEBUG("goto link", link)
self.document:gotoLink(link.section)
self.ui:handleEvent(Event:new("UpdateXPointer"))
return true
end
end
end
end
return ReaderLink

View File

@@ -183,10 +183,12 @@ function ReaderRolling:onTapBackward()
end
function ReaderRolling:onSwipe(arg, ges)
if ges.direction == "west" or ges.direction == "north" then
self:onGotoViewRel(1)
elseif ges.direction == "east" or ges.direction == "south" then
self:onGotoViewRel(-1)
if ges.direction == "west" then
self.ui.document:goForward()
self:onUpdateXPointer()
elseif ges.direction == "east" then
self.ui.document:goBack()
self:onUpdateXPointer()
end
return true
end
@@ -204,11 +206,13 @@ end
function ReaderRolling:onPosUpdate(new_pos)
self.current_pos = new_pos
self:updatePageLink()
self:updateBatteryState()
end
function ReaderRolling:onPageUpdate(new_page)
self.current_page = new_page
self:updatePageLink()
self:updateBatteryState()
end
@@ -273,6 +277,16 @@ function ReaderRolling:updatePos()
UIManager.repaint_all = true
end
function ReaderRolling:onUpdateXPointer()
local xp = self.ui.document:getXPointer()
if self.view.view_mode == "page" then
self.ui:handleEvent(Event:new("PageUpdate", self.ui.document:getPageFromXPointer(xp)))
else
self.ui:handleEvent(Event:new("PosUpdate", self.ui.document:getPosFromXPointer(xp)))
end
return true
end
function ReaderRolling:onChangeViewMode()
self.ui.document:_readMetadata()
self.old_doc_height = self.ui.document.info.doc_height
@@ -347,6 +361,12 @@ function ReaderRolling:onGotoPage(number)
return true
end
function ReaderRolling:updatePageLink()
DEBUG("update page link")
local links = self.ui.document:getPageLinks()
self.view.links = links
end
function ReaderRolling:updateBatteryState()
DEBUG("update battery state")
if self.view.view_mode == "page" then

View File

@@ -30,6 +30,7 @@ local ReaderFrontLight = require("ui/reader/readerfrontlight")
local ReaderDictionary = require("ui/reader/readerdictionary")
local ReaderHyphenation = require("ui/reader/readerhyphenation")
local ReaderActivityIndicator = require("ui/reader/readeractivityindicator")
local ReaderLink = require("ui/reader/readerlink")
--[[
This is an abstraction for a reader interface
@@ -82,41 +83,46 @@ function ReaderUI:init()
ui = self,
document = self.document,
}
-- rotation controller
self[2] = ReaderRotation:new{
dialog = self.dialog,
view = self[1],
ui = self
}
-- reader menu controller
self[3] = ReaderMenu:new{
view = self[1],
ui = self
}
self.menu = self[3] -- hold reference to menu widget
-- Table of content controller
self[4] = ReaderToc:new{
dialog = self.dialog,
view = self[1],
ui = self
}
self.toc = self[4] -- hold reference to bm widget
-- bookmark controller
local reader_bm = ReaderBookmark:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, reader_bm)
-- text highlight
local highlight = ReaderHighlight:new{
-- link
table.insert(self, ReaderLink:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
})
-- text highlight
table.insert(self, ReaderHighlight:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
})
-- rotation controller
table.insert(self, ReaderRotation:new{
dialog = self.dialog,
view = self[1],
ui = self
})
-- reader menu controller
self.menu = ReaderMenu:new{
view = self[1],
ui = self
}
table.insert(self, highlight)
-- goto
table.insert(self, self.menu) -- hold reference to menu widget
-- Table of content controller
self.toc = ReaderToc:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, self.toc) -- hold reference to bm widget
-- bookmark controller
table.insert(self, ReaderBookmark:new{
dialog = self.dialog,
view = self[1],
ui = self
})
-- reader goto controller
table.insert(self, ReaderGoto:new{
dialog = self.dialog,
view = self[1],
@@ -124,20 +130,18 @@ function ReaderUI:init()
document = self.document,
})
-- dictionary
local dict = ReaderDictionary:new{
table.insert(self, ReaderDictionary:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
}
table.insert(self, dict)
})
-- screenshot controller
local reader_ss = ReaderScreenshot:new{
table.insert(self.active_widgets, ReaderScreenshot:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self.active_widgets, reader_ss)
})
-- frontlight controller
if Device:hasFrontlight() then
table.insert(self, ReaderFrontLight:new{
@@ -149,117 +153,106 @@ function ReaderUI:init()
-- configuable controller
if self.document.info.configurable then
-- config panel controller
local config_dialog = ReaderConfig:new{
table.insert(self, ReaderConfig:new{
configurable = self.document.configurable,
options = self.document.options,
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, config_dialog)
})
if not self.document.info.has_pages then
-- cre option controller
local coptlistener = ReaderCoptListener:new{
table.insert(self, ReaderCoptListener:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
}
table.insert(self, coptlistener)
})
end
end
-- for page specific controller
if self.document.info.has_pages then
-- if needed, insert a paging container
local pager = ReaderPaging:new{
table.insert(self, ReaderPaging:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, pager)
})
-- zooming controller
local zoomer = ReaderZooming:new{
self.zoom = ReaderZooming:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, zoomer)
table.insert(self, self.zoom) -- hold reference to zoom controller
-- panning controller
local panner = ReaderPanning:new{
table.insert(self, ReaderPanning:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, panner)
})
-- cropping controller
local cropper = ReaderCropping:new{
table.insert(self, ReaderCropping:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
}
table.insert(self, cropper)
})
-- hinting controller
local hinter = ReaderHinting:new{
table.insert(self, ReaderHinting:new{
dialog = self.dialog,
zoom = zoomer,
zoom = self.zoom,
view = self[1],
ui = self,
document = self.document,
}
table.insert(self, hinter)
})
else
-- make sure we load document first before calling any callback
table.insert(self.postInitCallback, function()
self.document:loadDocument()
end)
-- typeset controller
local typeset = ReaderTypeset:new{
table.insert(self, ReaderTypeset:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, typeset)
})
-- font menu
local font_menu = ReaderFont:new{
table.insert(self, ReaderFont:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, font_menu)
})
table.insert(self, ReaderHyphenation:new{
dialog = self.dialog,
view = self[1],
ui = self
})
-- rolling controller
local roller = ReaderRolling:new{
table.insert(self, ReaderRolling:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, roller)
})
end
-- configuable controller
if self.document.info.configurable then
if self.document.info.has_pages then
-- kopt option controller
local koptlistener = ReaderKoptListener:new{
table.insert(self, ReaderKoptListener:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
}
table.insert(self, koptlistener)
})
end
-- activity indicator
local activity_listener = ReaderActivityIndicator:new{
table.insert(self, ReaderActivityIndicator:new{
dialog = self.dialog,
view = self[1],
ui = self,
document = self.document,
}
table.insert(self, activity_listener)
})
end
--DEBUG(self.doc_settings)
-- we only read settings after all the widgets are initialized