2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/reader/readerhighlight.lua

370 lines
9.1 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local FrameContainer = require("ui/widget/container/framecontainer")
2013-10-18 20:38:07 +00:00
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 UIManager = require("ui/uimanager")
local ButtonTable = require("ui/widget/buttontable")
local Input = require("ui/input")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
local _ = require("gettext")
2013-10-18 20:38:07 +00:00
local ReaderHighlight = InputContainer:new{}
function ReaderHighlight:init()
if Device:hasKeyboard() then
self.key_events = {
ShowToc = {
{ "." },
doc = _("highlight text") },
}
end
end
function ReaderHighlight:initGesListener()
self.ges_events = {
Tap = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight()
}
}
},
Hold = {
GestureRange:new{
ges = "hold",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight()
}
}
},
2013-06-15 15:13:19 +00:00
HoldRelease = {
GestureRange:new{
ges = "hold_release",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight()
}
}
},
HoldPan = {
GestureRange:new{
ges = "hold_pan",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight()
},
rate = 2.0,
}
},
}
end
function ReaderHighlight:onSetDimensions(dimen)
-- update listening according to new screen dimen
if Device:isTouchDevice() then
self:initGesListener()
end
end
function ReaderHighlight:onTap(arg, ges)
local function inside_box(ges, box)
2013-06-15 15:13:19 +00:00
local pos = self.view:screenToPageTransform(ges.pos)
2013-07-25 15:03:54 +00:00
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
2013-06-15 15:13:19 +00:00
end
end
if self.hold_pos then
self.view.highlight.temp[self.hold_pos.page] = nil
UIManager:setDirty(self.dialog, "partial")
2013-06-15 15:13:19 +00:00
self.hold_pos = nil
return true
end
2013-06-15 15:13:19 +00:00
local pages = self.view:getCurrentPageList()
for key, page in pairs(pages) do
2013-06-15 15:13:19 +00:00
local items = self.view.highlight.saved[page]
if not items then items = {} end
for i = 1, #items do
local pos0, pos1 = items[i].pos0, items[i].pos1
local boxes = self.ui.document:getPageBoxesFromPositions(page, pos0, pos1)
if boxes then
for index, box in pairs(boxes) do
if inside_box(ges, box) then
DEBUG("Tap on hightlight")
self.edit_highlight_dialog = HighlightDialog:new{
buttons = {
2013-06-15 15:13:19 +00:00
{
{
text = _("Delete"),
callback = function()
self:deleteHighlight(page, i)
UIManager:close(self.edit_highlight_dialog)
end,
},
{
text = _("Edit"),
enabled = false,
callback = function()
self:editHighlight()
UIManager:close(self.edit_highlight_dialog)
end,
},
2013-06-15 15:13:19 +00:00
},
},
}
UIManager:show(self.edit_highlight_dialog)
return true
end
2013-06-15 15:13:19 +00:00
end
end
end
end
end
function ReaderHighlight:onHold(arg, ges)
2013-06-15 15:13:19 +00:00
self.hold_pos = self.view:screenToPageTransform(ges.pos)
DEBUG("hold position in page", self.hold_pos)
if not self.hold_pos then
DEBUG("not inside page area")
return true
end
self.selected_word = self.ui.document:getWordFromPosition(self.hold_pos)
2013-06-15 15:13:19 +00:00
DEBUG("selected word:", self.selected_word)
if self.selected_word then
local boxes = {}
table.insert(boxes, self.selected_word.sbox)
2013-06-15 15:13:19 +00:00
self.view.highlight.temp[self.hold_pos.page] = boxes
UIManager:setDirty(self.dialog, "partial")
end
return true
end
function ReaderHighlight:onHoldPan(arg, ges)
if self.hold_pos == nil then
DEBUG("no previous hold position")
2013-06-15 15:13:19 +00:00
return true
end
self.holdpan_pos = self.view:screenToPageTransform(ges.pos)
DEBUG("holdpan position in page", self.holdpan_pos)
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.holdpan_pos)
DEBUG("selected text:", self.selected_text)
2013-06-15 15:13:19 +00:00
if self.selected_text then
self.view.highlight.temp[self.hold_pos.page] = self.selected_text.sboxes
-- remove selected word if hold moves out of word box
if self.selected_word and
not self.selected_word.sbox:contains(self.selected_text.sboxes[1]) then
self.selected_word = nil
end
2013-06-15 15:13:19 +00:00
UIManager:setDirty(self.dialog, "partial")
end
end
function ReaderHighlight:lookup(selected_word)
-- if we extracted text directly
if selected_word.word then
self.ui:handleEvent(Event:new("LookupWord", selected_word.word))
-- or we will do OCR
else
local word = self.ui.document:getOCRWord(self.hold_pos.page, selected_word)
DEBUG("OCRed word:", word)
self.ui:handleEvent(Event:new("LookupWord", word))
end
end
function ReaderHighlight:translate(selected_text)
if selected_text.text ~= "" then
self.ui:handleEvent(Event:new("TranslateText", selected_text.text))
-- or we will do OCR
else
local text = self.ui.document:getOCRText(self.hold_pos.page, selected_text)
DEBUG("OCRed text:", text)
self.ui:handleEvent(Event:new("TranslateText", text))
end
end
2013-07-21 06:23:54 +00:00
HighlightDialog = InputContainer:new{
buttons = nil,
tap_close_callback = nil,
}
function HighlightDialog:init()
if Device:hasKeyboard() then
key_events = {
AnyKeyPressed = { { Input.group.Any },
seqtext = "any key", doc = _("close dialog") }
}
else
self.ges_events.TapClose = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
}
}
end
self[1] = CenterContainer:new{
dimen = Screen:getSize(),
FrameContainer:new{
ButtonTable:new{
width = Screen:getWidth()*0.9,
buttons = self.buttons,
},
background = 0,
bordersize = 2,
radius = 7,
padding = 2,
}
}
end
function HighlightDialog:onTapClose()
UIManager:close(self)
if self.tap_close_callback then
self.tap_close_callback()
end
return true
end
2013-06-15 15:13:19 +00:00
function ReaderHighlight:onHoldRelease(arg, ges)
if self.selected_word then
self:lookup(self.selected_word)
2013-06-15 15:13:19 +00:00
self.selected_word = nil
elseif self.selected_text then
DEBUG("show highlight dialog")
2013-07-21 06:23:54 +00:00
self.highlight_dialog = HighlightDialog:new{
2013-06-15 15:13:19 +00:00
buttons = {
{
{
text = _("Highlight"),
callback = function()
self:saveHighlight()
UIManager:close(self.highlight_dialog)
self.ui:handleEvent(Event:new("Tap"))
end,
2013-06-15 15:13:19 +00:00
},
{
text = _("Add Note"),
enabled = false,
callback = function()
self:addNote()
UIManager:close(self.highlight_dialog)
self.ui:handleEvent(Event:new("Tap"))
end,
2013-06-15 15:13:19 +00:00
},
},
{
{
text = _("Translate"),
callback = function()
self:translate(self.selected_text)
UIManager:close(self.highlight_dialog)
self.ui:handleEvent(Event:new("Tap"))
end,
},
2013-06-15 15:13:19 +00:00
{
text = _("Share"),
enabled = false,
callback = function()
self:shareHighlight()
UIManager:close(self.highlight_dialog)
self.ui:handleEvent(Event:new("Tap"))
end,
2013-06-15 15:13:19 +00:00
},
},
{
2013-06-15 15:13:19 +00:00
{
text = _("More"),
enabled = false,
callback = function()
self:moreAction()
UIManager:close(self.highlight_dialog)
self.ui:handleEvent(Event:new("Tap"))
end,
2013-06-15 15:13:19 +00:00
},
},
},
tap_close_callback = function() self.ui:handleEvent(Event:new("Tap")) end,
}
UIManager:show(self.highlight_dialog)
end
return true
end
2013-06-15 15:13:19 +00:00
function ReaderHighlight:saveHighlight()
DEBUG("save highlight")
local page = self.hold_pos.page
if self.hold_pos and self.selected_text then
if not self.view.highlight.saved[page] then
self.view.highlight.saved[page] = {}
end
local hl_item = {}
hl_item["text"] = self.selected_text.text
hl_item["pos0"] = self.selected_text.pos0
hl_item["pos1"] = self.selected_text.pos1
2013-06-16 11:00:15 +00:00
hl_item["datetime"] = os.date("%Y-%m-%d %H:%M:%S"),
2013-06-15 15:13:19 +00:00
table.insert(self.view.highlight.saved[page], hl_item)
2013-06-16 11:00:15 +00:00
if self.selected_text.text ~= "" then
self:exportToClippings(page, hl_item)
end
2013-06-15 15:13:19 +00:00
end
--DEBUG("saved hightlights", self.view.highlight.saved[page])
end
2013-06-16 11:00:15 +00:00
function ReaderHighlight:exportToClippings(page, item)
DEBUG("export highlight to My Clippings")
local clippings = io.open("/mnt/us/documents/My Clippings.txt", "a+")
if clippings and item.text then
2013-06-16 11:00:15 +00:00
local current_locale = os.setlocale()
os.setlocale("C")
clippings:write(self.document.file:gsub("(.*/)(.*)", "%2").."\n")
clippings:write("- Koreader Highlight Page "..page.." ")
clippings:write("| Added on "..os.date("%A, %b %d, %Y %I:%M:%S %p\n\n"))
clippings:write(item["text"].."\n")
clippings:write("==========\n")
clippings:close()
os.setlocale(current_locale)
end
end
2013-06-15 15:13:19 +00:00
function ReaderHighlight:addNote()
DEBUG("add Note")
end
function ReaderHighlight:shareHighlight()
DEBUG("share highlight")
end
function ReaderHighlight:moreAction()
DEBUG("more action")
end
function ReaderHighlight:deleteHighlight(page, i)
DEBUG("delete highlight")
2013-06-15 15:13:19 +00:00
table.remove(self.view.highlight.saved[page], i)
end
function ReaderHighlight:editHighlight()
DEBUG("edit highlight")
end
2013-10-18 20:38:07 +00:00
return ReaderHighlight