-
Notifications
You must be signed in to change notification settings - Fork 0
/
tofu.py
executable file
·182 lines (126 loc) · 4.83 KB
/
tofu.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
import sys, fitz
from pathlib import Path
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QVBoxLayout, QWidgetItem, QDesktopWidget
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
home = Path.home() # gets $HOME or equivalent
commands = []
class Tofu(QWidget):
"""Derived from QWidget; is the main widget of the application.
Parameters:
filename (string): name of the pdf file to open.
Returns:
Tofu: An instance of class Tofu.
"""
def __init__(self, filename):
'''Initialize non-UI components, and call initUI.'''
super().__init__()
# create '$HOME/.tofu_pdf' if not found
if not (home / '.tofu_pdf').exists():
(home / '.tofu_pdf').mkdir(parents = True, exist_ok = True)
# define keymaps
self.keymaps = {
'j': self.next_page,
'k': self.previous_page,
'q': self.quit,
'g': self.goto,
}
self.title = Path(filename).stem # get name of the file, preferred because metadata['title'] is not often available
self.doc = fitz.open(filename) # open the given file
self.metadata = self.doc.metadata
self.pageNumber = 0
# retrieve data stored about a file
self.fileDataPath = home / '.tofu_pdf' / self.title
if self.fileDataPath.exists():
oldData = self.fileDataPath.read_text()
storedData = dict()
if isinstance(eval(oldData), dict):
storedData = eval(oldData)
self.pageNumber = storedData.get('pageNumber', 0)
self.initUI()
def initUI(self):
'''Initialize UI components, and call renderPage.'''
# Center window on screen, not accurate right now. TODO fix this
qtRect = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRect.moveCenter(centerPoint)
self.move(qtRect.topLeft())
self.vbox = QVBoxLayout(self)
self.setLayout(self.vbox)
self.setWindowTitle('Tofu PDF')
self.show()
self.renderPage()
def clearLayout(self):
'''Remove label from the layout.'''
self.vbox.removeItem(self.vbox.itemAt(0))
self.update()
def renderPage(self):
'''Load and render current page from the file.'''
self.clearLayout()
page = self.doc[self.pageNumber]
pix = page.getPixmap()
fmt = QImage.Format_RGBA8888 if pix.alpha else QImage.Format_RGB888
img = QImage(pix.samples, pix.width, pix.height, pix.stride, fmt)
pixmap = QPixmap.fromImage(img)
label = QLabel(self)
label.setPixmap(pixmap)
label.setAlignment(Qt.AlignCenter)
self.vbox.addWidget(label)
self.vbox.setContentsMargins(0, 0, 0, 0)
self.update()
def saveToFile(self, key, value):
"""Save data about the current file.
Parameters:
key (string): key/name of the property.
value (any): value of the property.
"""
storedData = dict()
if self.fileDataPath.exists():
oldData = self.fileDataPath.read_text()
if isinstance(eval(oldData), dict):
storedData = eval(oldData)
storedData[key] = value
self.fileDataPath.write_text(str(storedData))
def next_page(self):
'''Move to the next page, moves to the first page if at the last page.'''
self.pageNumber += 1
self.renderPage()
def previous_page(self):
'''Move to the previous page, moves to the last page if at the first page.'''
self.pageNumber -= 1
self.renderPage()
def goto(self, number):
"""Move to the given page number.
Parameters:
number (int): page number to move to.
"""
self.pageNumber = number
self.renderPage()
def quit(self):
'''Store current page number and quit.'''
self.saveToFile("pageNumber", self.pageNumber)
QApplication.instance().quit()
def keyPressEvent(self, event):
'''Call functions associated with a key press, if any.'''
global commands
key = event.text()
if key == 'g' or key.isnumeric():
commands.append(key)
return
if event.key() == Qt.Key_Escape:
commands = []
return
if event.key() == Qt.Key_Return and commands:
command = commands[0]
number = int("".join(commands[1:]))
commands = []
self.keymaps[command](number)
return
if key in self.keymaps.keys():
self.keymaps[key]()
if __name__ == '__main__':
filename = sys.argv[1]
app = QApplication(sys.argv)
tofu = Tofu(filename)
sys.exit(app.exec_())