-
Notifications
You must be signed in to change notification settings - Fork 3
/
scroll.lua
158 lines (134 loc) · 3.58 KB
/
scroll.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
--[[ The schollable UI
By Xiang Zhang @ New York University
Version 0.1, 03/07/2014
--]]
local Scroll = torch.class("Scroll")
-- Initialize a scroll interface
-- width: (optional) the pixel width of the scollable area. Default is 800.
-- title: (optional) title for the window
function Scroll:__init(width,title)
require("qtuiloader")
require("qtwidget")
require("qttorch")
self.file = "scroll.ui"
self.win = qtuiloader.load(self.file)
self.frame = self.win.frame
self.painter = qt.QtLuaPainter(self.frame)
self.width = width
self.height = 0
self.fontSize = 15
self.x = 0
self.y = 0
self.border = 1
self:resize(self.width, self.height)
self:setFontSize(self.fontSize)
if title then
self:setTitle(title)
end
self:show()
end
-- Resize the window to designated width and height
function Scroll:resize(width,height)
self.width = width or self.width
self.height = height or self.height
self.frame.size = qt.QSize{width = self.width,height = self.height}
end
-- Set the text width
function Scroll:setFontSize(size)
self.painter:setfontsize(size or 15)
self.fontSize = size
end
-- Set border width
function Scroll:setBorder(width)
self.border = width
end
-- Draw text
function Scroll:drawText(text)
-- Drawing text must happen on a new line
if self.x ~= 0 then
self.x = 0
self.y = self.height
end
-- Determine height and resize if necessary
if self.height < self.y+self.fontSize+1 then
self:resize(self.width,self.y+self.fontSize+1+self.border)
end
-- Draw the yellow main text
self.painter:gbegin()
self.painter:moveto(self.x,self.y+self.fontSize-1)
self.painter:setcolor(1,1,0,1)
self.painter:show(text)
self.painter:stroke()
self.painter:gend()
-- Draw the black shadow text
self.painter:gbegin()
self.painter:moveto(self.x,self.y+self.fontSize+1-1)
self.painter:setcolor(0,0,0,1)
self.painter:show(text)
self.painter:stroke()
self.painter:gend()
-- Move the cursor to next line
self.x = 0
if self.height < self.y+self.fontSize+1+self.border then
self:resize(self.width,self.y+self.fontSize+1+self.border)
end
self.y = self.height
end
-- Draw image
function Scroll:drawImage(im,scale)
-- Get the image height and width
local scale = scale or 1
local height,width
if im:dim() == 2 then
height = im:size(1)*scale
width = im:size(2)*scale
elseif im:dim() == 3 then
height = im:size(2)*scale
width = im:size(3)*scale
else
error("Image must be 2-dim or 3-dim data")
end
-- Determine whether a new line is needed
if self.x ~=0 and self.x + width > self.width then
self.x = 0
self.y = self.height
end
-- Determine whether need to resize the document area
if self.y + height > self.height then
self:resize(self.width,self.y+height+self.border)
end
-- Draw the image
self.painter:gbegin()
self.painter:image(self.x,self.y,width,height,qt.QImage.fromTensor(im))
self.painter:stroke()
self.painter:gend()
-- Move the cursor
self.x = self.x + width + self.border
end
-- Draw a new line
function Scroll:drawEndOfLine()
self.x = 0
self.y = self.height
end
-- Show the window
function Scroll:show()
self.win:show()
end
-- Hide the window
function Scroll:hide()
self.win:hide()
end
-- Save to file
function Scroll:save(file)
self.painter:write(file)
end
-- Set window title
function Scroll:setTitle(title)
self.win:setWindowTitle(title)
end
-- Reset the drawing area
function Scroll:clear()
self:resize(self.width,0)
self.x = 0
self.y = 0
end