2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2013-10-22 15:11:31 +00:00
|
|
|
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")
|
2013-10-22 15:11:31 +00:00
|
|
|
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-04-23 22:59:52 +00:00
|
|
|
|
2013-10-28 13:48:17 +00:00
|
|
|
local HighlightDialog = InputContainer:new{
|
|
|
|
buttons = nil,
|
|
|
|
tap_close_callback = nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
function HighlightDialog:init()
|
|
|
|
if Device:hasKeyboard() then
|
|
|
|
self.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-10-18 20:38:07 +00:00
|
|
|
local ReaderHighlight = InputContainer:new{}
|
2013-04-23 22:59:52 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
},
|
2013-04-23 22:59:52 +00:00
|
|
|
}
|
|
|
|
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)
|
2013-06-16 03:15:39 +00:00
|
|
|
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
|
2013-04-23 22:59:52 +00:00
|
|
|
UIManager:setDirty(self.dialog, "partial")
|
2013-06-15 15:13:19 +00:00
|
|
|
self.hold_pos = nil
|
2013-04-23 22:59:52 +00:00
|
|
|
return true
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
local pages = self.view:getCurrentPageList()
|
2013-06-16 03:15:39 +00:00
|
|
|
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
|
2013-10-12 15:07:13 +00:00
|
|
|
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
|
|
|
{
|
2013-10-12 15:07:13 +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
|
|
|
},
|
|
|
|
},
|
2013-10-12 15:07:13 +00:00
|
|
|
}
|
|
|
|
UIManager:show(self.edit_highlight_dialog)
|
|
|
|
return true
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
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
|
2013-05-03 11:04:31 +00:00
|
|
|
DEBUG("not inside page area")
|
|
|
|
return true
|
|
|
|
end
|
2013-10-12 15:07:13 +00:00
|
|
|
|
|
|
|
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 = {}
|
2013-10-12 15:07:13 +00:00
|
|
|
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)
|
2013-10-12 15:07:13 +00:00
|
|
|
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)
|
2013-10-12 15:07:13 +00:00
|
|
|
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
|
2013-10-12 15:07:13 +00:00
|
|
|
self.view.highlight.temp[self.hold_pos.page] = self.selected_text.sboxes
|
2013-06-16 08:58:26 +00:00
|
|
|
-- remove selected word if hold moves out of word box
|
|
|
|
if self.selected_word and
|
2013-10-28 13:48:17 +00:00
|
|
|
not self.selected_word.sbox:contains(self.selected_text.sboxes[1]) or
|
|
|
|
#self.selected_text.sboxes > 1 then
|
2013-06-16 08:58:26 +00:00
|
|
|
self.selected_word = nil
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
UIManager:setDirty(self.dialog, "partial")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-19 18:49:03 +00:00
|
|
|
function ReaderHighlight:lookup(selected_word)
|
|
|
|
-- if we extracted text directly
|
|
|
|
if selected_word.word then
|
2013-12-26 14:40:40 +00:00
|
|
|
local word_box = self.view:pageToScreenTransform(selected_word.page, selected_word.sbox)
|
|
|
|
self.ui:handleEvent(Event:new("LookupWord", self, selected_word.word, word_box))
|
2013-07-19 18:49:03 +00:00
|
|
|
-- or we will do OCR
|
|
|
|
else
|
2013-10-14 15:05:58 +00:00
|
|
|
local word = self.ui.document:getOCRWord(self.hold_pos.page, selected_word)
|
2013-07-19 18:49:03 +00:00
|
|
|
DEBUG("OCRed word:", word)
|
2013-12-26 14:40:40 +00:00
|
|
|
local word_box = self.view:pageToScreenTransform(selected_word.page, selected_word.sbox)
|
|
|
|
self.ui:handleEvent(Event:new("LookupWord", self, word, word_box))
|
2013-07-19 18:49:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:translate(selected_text)
|
|
|
|
if selected_text.text ~= "" then
|
2013-12-20 04:05:07 +00:00
|
|
|
self.ui:handleEvent(Event:new("TranslateText", self, selected_text.text))
|
2013-07-19 18:49:03 +00:00
|
|
|
-- or we will do OCR
|
|
|
|
else
|
2013-10-14 15:05:58 +00:00
|
|
|
local text = self.ui.document:getOCRText(self.hold_pos.page, selected_text)
|
2013-07-19 18:49:03 +00:00
|
|
|
DEBUG("OCRed text:", text)
|
2013-12-20 04:05:07 +00:00
|
|
|
self.ui:handleEvent(Event:new("TranslateText", self, text))
|
2013-07-19 18:49:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:onHoldRelease(arg, ges)
|
|
|
|
if self.selected_word then
|
2013-07-19 18:49:03 +00:00
|
|
|
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 = {
|
|
|
|
{
|
|
|
|
{
|
2013-06-16 03:15:39 +00:00
|
|
|
text = _("Highlight"),
|
2013-06-17 05:15:24 +00:00
|
|
|
callback = function()
|
|
|
|
self:saveHighlight()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
2013-12-20 04:17:11 +00:00
|
|
|
self:handleEvent(Event:new("Tap"))
|
2013-06-17 05:15:24 +00:00
|
|
|
end,
|
2013-06-15 15:13:19 +00:00
|
|
|
},
|
|
|
|
{
|
2013-06-16 03:15:39 +00:00
|
|
|
text = _("Add Note"),
|
|
|
|
enabled = false,
|
2013-06-17 05:15:24 +00:00
|
|
|
callback = function()
|
|
|
|
self:addNote()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
2013-12-20 04:17:11 +00:00
|
|
|
self:handleEvent(Event:new("Tap"))
|
2013-06-17 05:15:24 +00:00
|
|
|
end,
|
2013-06-15 15:13:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2013-07-19 18:49:03 +00:00
|
|
|
{
|
|
|
|
text = _("Translate"),
|
|
|
|
callback = function()
|
|
|
|
self:translate(self.selected_text)
|
|
|
|
UIManager:close(self.highlight_dialog)
|
2013-12-20 04:17:11 +00:00
|
|
|
self:handleEvent(Event:new("Tap"))
|
2013-07-19 18:49:03 +00:00
|
|
|
end,
|
|
|
|
},
|
2013-06-15 15:13:19 +00:00
|
|
|
{
|
2013-06-16 03:15:39 +00:00
|
|
|
text = _("Share"),
|
|
|
|
enabled = false,
|
2013-06-17 05:15:24 +00:00
|
|
|
callback = function()
|
|
|
|
self:shareHighlight()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
2013-12-20 04:17:11 +00:00
|
|
|
self:handleEvent(Event:new("Tap"))
|
2013-06-17 05:15:24 +00:00
|
|
|
end,
|
2013-06-15 15:13:19 +00:00
|
|
|
},
|
2013-07-19 18:49:03 +00:00
|
|
|
},
|
|
|
|
{
|
2013-06-15 15:13:19 +00:00
|
|
|
{
|
2013-06-16 03:15:39 +00:00
|
|
|
text = _("More"),
|
|
|
|
enabled = false,
|
2013-06-17 05:15:24 +00:00
|
|
|
callback = function()
|
|
|
|
self:moreAction()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
2013-12-20 04:17:11 +00:00
|
|
|
self:handleEvent(Event:new("Tap"))
|
2013-06-17 05:15:24 +00:00
|
|
|
end,
|
2013-06-15 15:13:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-12-20 04:17:11 +00:00
|
|
|
tap_close_callback = function() self:handleEvent(Event:new("Tap")) end,
|
2013-06-15 15:13:19 +00:00
|
|
|
}
|
|
|
|
UIManager:show(self.highlight_dialog)
|
2013-04-23 22:59:52 +00:00
|
|
|
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
|
2013-10-12 15:07:13 +00:00
|
|
|
hl_item["pos0"] = self.selected_text.pos0
|
|
|
|
hl_item["pos1"] = self.selected_text.pos1
|
2013-12-04 13:50:53 +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)
|
2013-11-30 13:17:13 +00:00
|
|
|
DEBUG("export highlight", item)
|
2013-06-16 11:00:15 +00:00
|
|
|
local clippings = io.open("/mnt/us/documents/My Clippings.txt", "a+")
|
2013-10-20 04:32:35 +00:00
|
|
|
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)
|
2013-06-17 05:15:24 +00:00
|
|
|
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
|