mirror of https://github.com/koreader/koreader
Qrcode support (#6844)
2 new widgets: - QRWidget, that's like an ImageWidget, but with a text property that will be converted to a QR code ; - QRMessage, that's like an InfoMessage, but shows the message as QR code. Moreover, it adds the ability to export QR codes to the text editor. 1 new plugin: - Generate QR codes from clipboard Changes to text editor plugin: - Add the ability to export QR codes.reviewable/pr6848/r1
parent
8403154d4d
commit
43ba8a1173
@ -1 +1 @@
|
||||
Subproject commit 444a70965acb8348e8e15bed1af41560e9786831
|
||||
Subproject commit a4dab90a4c6bd2b7fdb51736dc0d3a5fcfe94c13
|
@ -0,0 +1,114 @@
|
||||
--[[--
|
||||
Widget that displays a qr code.
|
||||
|
||||
It vanishes on key press or after a given timeout.
|
||||
|
||||
Example:
|
||||
local UIManager = require("ui/uimanager")
|
||||
local _ = require("gettext")
|
||||
local Screen = require("device").screen
|
||||
local sample
|
||||
sample = QRMessage:new{
|
||||
text = _("my message"),
|
||||
height = Screen:scaleBySize(400),
|
||||
width = Screen:scaleBySize(400),
|
||||
timeout = 5, -- This widget will vanish in 5 seconds.
|
||||
}
|
||||
UIManager:show(sample)
|
||||
]]
|
||||
|
||||
local Blitbuffer = require("ffi/blitbuffer")
|
||||
local CenterContainer = require("ui/widget/container/centercontainer")
|
||||
local Device = require("device")
|
||||
local FrameContainer = require("ui/widget/container/framecontainer")
|
||||
local Geom = require("ui/geometry")
|
||||
local GestureRange = require("ui/gesturerange")
|
||||
local QRWidget = require("ui/widget/qrwidget")
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local Input = Device.input
|
||||
local Screen = Device.screen
|
||||
local Size = require("ui/size")
|
||||
|
||||
local QRMessage = InputContainer:new{
|
||||
modal = true,
|
||||
timeout = nil, -- in seconds
|
||||
text = nil, -- The text to encode.
|
||||
width = nil, -- The width. Keep it nil to use original width.
|
||||
height = nil, -- The height. Keep it nil to use original height.
|
||||
dismiss_callback = function() end,
|
||||
alpha = nil,
|
||||
scale_factor = 1,
|
||||
}
|
||||
|
||||
function QRMessage:init()
|
||||
if Device:hasKeys() then
|
||||
self.key_events = {
|
||||
AnyKeyPressed = { { Input.group.Any },
|
||||
seqtext = "any key", doc = "close dialog" }
|
||||
}
|
||||
end
|
||||
if Device:isTouchDevice() then
|
||||
self.ges_events.TapClose = {
|
||||
GestureRange:new{
|
||||
ges = "tap",
|
||||
range = Geom:new{
|
||||
x = 0, y = 0,
|
||||
w = Screen:getWidth(),
|
||||
h = Screen:getHeight(),
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
local padding = Size.padding.fullscreen
|
||||
|
||||
local image_widget = QRWidget:new{
|
||||
text = self.text,
|
||||
width = self.width and (self.width - 2 * padding),
|
||||
height = self.height and (self.height - 2 * padding),
|
||||
alpha = self.alpha,
|
||||
scale_factor = self.scale_factor,
|
||||
}
|
||||
|
||||
local frame = FrameContainer:new{
|
||||
background = Blitbuffer.COLOR_WHITE,
|
||||
padding = padding,
|
||||
image_widget,
|
||||
}
|
||||
self[1] = CenterContainer:new{
|
||||
dimen = Screen:getSize(),
|
||||
frame,
|
||||
}
|
||||
end
|
||||
|
||||
function QRMessage:onCloseWidget()
|
||||
UIManager:setDirty(nil, function()
|
||||
return "ui", self[1][1].dimen
|
||||
end)
|
||||
return true
|
||||
end
|
||||
|
||||
function QRMessage:onShow()
|
||||
-- triggered by the UIManager after we got successfully shown (not yet painted)
|
||||
UIManager:setDirty(self, function()
|
||||
return "ui", self[1][1].dimen
|
||||
end)
|
||||
if self.timeout then
|
||||
UIManager:scheduleIn(self.timeout, function() UIManager:close(self) end)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function QRMessage:onAnyKeyPressed()
|
||||
-- triggered by our defined key events
|
||||
self.dismiss_callback()
|
||||
UIManager:close(self)
|
||||
end
|
||||
|
||||
function QRMessage:onTapClose()
|
||||
self.dismiss_callback()
|
||||
UIManager:close(self)
|
||||
end
|
||||
|
||||
return QRMessage
|
@ -0,0 +1,54 @@
|
||||
--[[
|
||||
QRWidget shows a QR code for a given text.
|
||||
]]
|
||||
|
||||
local Blitbuffer = require("ffi/blitbuffer")
|
||||
local ImageWidget = require("ui/widget/imagewidget")
|
||||
local logger = require("logger")
|
||||
local qrencode = require("ffi/qrencode")
|
||||
local _ = require("gettext")
|
||||
|
||||
local QRWidget = ImageWidget:extend{
|
||||
scale_factor = nil,
|
||||
text = ""
|
||||
-- see ImageWidget for other options.
|
||||
}
|
||||
|
||||
function QRWidget:init()
|
||||
local text = self.text
|
||||
if #text > 2953 then
|
||||
local truncated = _("... (truncated...)")
|
||||
text = text:sub(1, 2953 - #truncated) .. truncated
|
||||
end
|
||||
local ok, grid = qrencode.qrcode(text)
|
||||
if not ok then
|
||||
logger.info("QRWidget: failed to generate QR code.")
|
||||
return
|
||||
else
|
||||
local sq_size
|
||||
if self.width then
|
||||
if self.height then
|
||||
sq_size = math.min(self.width, self.height)/#grid
|
||||
else
|
||||
sq_size = self.width/#grid
|
||||
end
|
||||
elseif self.height then
|
||||
sq_size = self.height/#grid
|
||||
else sq_size = 1
|
||||
end
|
||||
sq_size = math.floor(sq_size)
|
||||
local grid_size = sq_size * #grid
|
||||
local bb = Blitbuffer.new(grid_size, grid_size)
|
||||
local white = Blitbuffer.COLOR_WHITE
|
||||
for x, col in ipairs(grid) do
|
||||
for y, lgn in ipairs(col) do
|
||||
if lgn < 0 then
|
||||
bb:paintRect((x - 1) * sq_size, (y - 1) * sq_size, sq_size, sq_size, white)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.image = bb
|
||||
end
|
||||
end
|
||||
|
||||
return QRWidget
|
@ -0,0 +1,6 @@
|
||||
local _ = require("gettext")
|
||||
return {
|
||||
name = "qrclipboard",
|
||||
fullname = _("QR from clipboard"),
|
||||
description = _([[This plugin generates a QR code from clipboard content.]]),
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
--[[--
|
||||
This plugin generates a QR code from clipboard content.
|
||||
--]]--
|
||||
|
||||
local Device = require("device")
|
||||
local QRMessage = require("ui/widget/qrmessage")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
local _ = require("gettext")
|
||||
|
||||
local QRClipboard = WidgetContainer:new{
|
||||
name = "qrclipboard",
|
||||
is_doc_only = false,
|
||||
}
|
||||
|
||||
function QRClipboard:init()
|
||||
self.ui.menu:registerToMainMenu(self)
|
||||
end
|
||||
|
||||
function QRClipboard:addToMainMenu(menu_items)
|
||||
menu_items.qrclipboard = {
|
||||
text = _("QR from clipboard"),
|
||||
callback = function()
|
||||
UIManager:show(QRMessage:new{
|
||||
text = Device.input.getClipboardText(),
|
||||
width = Device.screen:getWidth(),
|
||||
height = Device.screen:getHeight()
|
||||
})
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
return QRClipboard
|
Loading…
Reference in New Issue