forked from Nyre221/dolphin-quick-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_view.py
299 lines (242 loc) · 10.6 KB
/
quick_view.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/python3
# import QT
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QStyle, \
QSizePolicy, QLabel , QShortcut
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
# import viewers
import Page_viewer
import Text_viewer
import Table_viewer
import Video_viewer
# other
from glob import glob
import textract
import subprocess
import sys
import os
class Main(QMainWindow):
def __init__(self, app):
super().__init__()
self.app = app
# a way to access and check if the widget has been created
self.added_widgets = []
self.page_viewer = None
self.media_player = None
self.text_viewer = None
self.table_viewer = None
if len(sys.argv) < 2:
print("No path or file given")
exit()
if os.path.isfile(sys.argv[1]):
self.current_file = sys.argv[1]
self.current_path = os.path.dirname(self.current_file)
self.files = glob(f"{self.current_path}/*.*")
self.current_index = self.files.index(self.current_file)
elif os.path.isdir(sys.argv[1]):
self.current_path = sys.argv[1]
self.files = glob(f"{self.current_path}/*.*")
self.current_file = self.files[0]
self.current_index = 0
else:
print("File or directory not found")
exit()
# setting up ui
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogContentsView))
screen_size = app.primaryScreen().size()
self.resize(int(screen_size.width() * 0.5), int(screen_size.height() * 0.55))
self.create_ui()
self.set_shortcut()
self.setFocus()
self.load_file_at_index(self.current_index)
def create_ui(self):
# central widget
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
# main layout
self.main_layout = QVBoxLayout(self.central_widget)
self.main_layout.setContentsMargins(0, 0, 0, 0)
# controls layout
controls_layout = QHBoxLayout()
controls_layout.setContentsMargins(0, 0, 0, 0)
# file not supported label (just repeat the name for now)
self.error_label = QLabel()
# adds the label to the list of added widgets so that it disappears automatically when you select another file.
self.added_widgets.append(self.error_label)
font = QFont("Monospace")
font.setPointSize(15)
self.error_label.setFont(font)
# self.error_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
self.error_label.setWordWrap(True)
self.error_label.setAlignment(Qt.AlignCenter)
# viewer_container and layout
self.viewer_container = QWidget()
self.viewer_container_layout = QVBoxLayout(self.viewer_container)
self.viewer_container_layout.setContentsMargins(0, 0, 0, 0)
# back button
self.back_button = QPushButton()
self.back_button.setFixedHeight(24)
self.back_button.setMinimumWidth(80)
self.back_button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
# forward button
self.forward_button = QPushButton()
self.forward_button.setFixedHeight(24)
self.forward_button.setMinimumWidth(80)
self.forward_button.setIcon(self.style().standardIcon(QStyle.SP_ArrowRight))
# open button
self.open_button = QPushButton()
self.open_button.setFixedHeight(24)
self.open_button.setMinimumWidth(80)
self.open_button.setSizePolicy(QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed))
self.open_button.setIcon(self.style().standardIcon(QStyle.SP_DialogOpenButton))
# button signal
self.back_button.pressed.connect(self.back)
self.forward_button.pressed.connect(self.forward)
self.open_button.pressed.connect(self.open_with_app)
# adding to layouts
self.viewer_container_layout.addWidget(self.error_label)
controls_layout.addWidget(self.back_button)
controls_layout.addWidget(self.open_button)
controls_layout.addWidget(self.forward_button)
self.main_layout.addWidget(self.viewer_container)
self.main_layout.addLayout(controls_layout)
def load_file_at_index(self, index):
self.hide_widgets()
self.current_file = self.files[index]
self.setWindowTitle(self.current_file.split("/")[-1])
extension = os.path.splitext(self.current_file.lower())[-1]
if extension in ".pdf":
self.load_page_viewer(self.current_file, "pdf")
elif extension in [".png", ".jpeg", ".jpg", ".webp"]:
self.load_page_viewer(self.current_file, "img")
elif extension in [".svg", ".svgz"]:
self.load_page_viewer(self.current_file, "svg")
elif extension in [".mp4", ".mp3"]:
self.load_video_viewer(self.current_file)
elif extension in [".doc", ".docx", ".odt", ".txt"]:
text = textract.process(self.current_file).decode("utf-8")
self.load_text_viewer(text)
elif extension in ".md":
with open(self.current_file, "r") as f:
self.load_text_viewer(f.read(), markdown=True)
elif extension in [".ods", ".xlsx", ".xls", ".csv"]:
self.load_table_viewer(self.current_file)
# for simple text (.sh,.txt,.xml,.html,etc) which may or may not have an extension.
# I didn't want to download yet another python module, and so I use subprocess.
elif "text/" in subprocess.run(["file", "--mime-type", self.current_file],
stdout=subprocess.PIPE).stdout.decode("utf-8"):
with open(self.current_file, "r") as f:
self.load_text_viewer(f.read())
else:
self.error_label.setText(os.path.basename(self.current_file))
self.error_label.show()
# to extend compatibility I used an if ladder, in the future I will remove it.
# (needs to be updated)
# match extension:
# case ".pdf":
# self.load_page_viewer(self.current_file,"pdf")
# case f if f in [".png",".jpeg",".jpg"]:
# self.load_page_viewer(self.current_file,"img")
# case ".svg":
# self.load_page_viewer(self.current_file,"svg")
# case f if f in [".mp4",".mp3"]:
# self.load_video_viewer(self.current_file)
# case f if f in [".doc",".docx",".odt",".txt"]:
# text = textract.process(self.current_file).decode("utf-8")
# self.load_text_viewer(text)
# case f if f in [".xml",".html"]:
# with open(self.current_file,"r") as f:
# self.load_text_viewer(f.read())
# case f if f in [".ods",".xlsx",".xls",".csv"]:
# self.load_table_viewer(self.current_file)
# case _:
# self.error_label.setText(os.path.basename(self.current_file))
# self.error_label.show()
def load_table_viewer(self, path):
if self.table_viewer is None:
self.table_viewer = Table_viewer.Table_viewer(self)
self.add_widget(self.table_viewer)
self.table_viewer.load_file(path)
self.table_viewer.show()
def load_text_viewer(self, text, markdown=False):
if self.text_viewer is None:
self.text_viewer = Text_viewer.Text_viewer(self)
self.add_widget(self.text_viewer)
font = QFont("Monospace")
self.text_viewer.setFont(font)
self.text_viewer.zoomIn(3)
# here I remove the previously set text to avoid showing the contents of another file if something wrong happens.
self.text_viewer.clear()
if markdown:
self.text_viewer.setMarkdown(text)
else:
self.text_viewer.setText(text)
self.text_viewer.show()
def load_video_viewer(self, path):
if self.media_player is None:
self.media_player = Video_viewer.VideoPlayer(self)
self.add_widget(self.media_player)
self.media_player.open(path)
self.media_player.show()
def load_page_viewer(self, path, _type):
if self.page_viewer is None:
self.page_viewer = Page_viewer.Viewer(self)
self.add_widget(self.page_viewer)
if _type == "pdf":
self.page_viewer.loadPdf(path)
self.page_viewer.pdf_mode()
elif _type == "img":
self.page_viewer.loadImages([path])
self.page_viewer.img_mode()
elif _type == "svg":
self.page_viewer.loadSvgs([path])
self.page_viewer.img_mode()
# to extend compatibility I used an if ladder, in the future I will remove it.
# match type:
# case "pdf":
# self.page_viewer.loadPdf(path)
# self.page_viewer.pdf_mode()
# case "img":
# self.page_viewer.loadImages([path])
# self.page_viewer.img_mode()
# case "svg":
# self.page_viewer.loadSvgs([path])
# self.page_viewer.img_mode()
self.page_viewer.show()
def add_widget(self, widget):
self.viewer_container_layout.addWidget(widget)
self.added_widgets.append(widget)
def open_with_app(self):
subprocess.run(["xdg-open", self.current_file])
exit()
def back(self):
if self.current_index == 0:
self.current_index = self.current_index = len(self.files) - 1
else:
self.current_index = self.current_index - 1
self.load_file_at_index(self.current_index)
def forward(self):
if len(self.files) - 1 == self.current_index:
self.current_index = 0
else:
self.current_index = self.current_index + 1
self.load_file_at_index(self.current_index)
def hide_widgets(self):
for w in self.added_widgets:
w.hide()
def set_shortcut(self):
for shortcut_close in ["q", Qt.Key_Escape, Qt.Key_Space]:
QShortcut(shortcut_close, self).activated.connect(exit)
for shortcut_back in ["a", Qt.Key_Left]:
QShortcut(shortcut_back, self).activated.connect(self.back)
for shortcut_forward in ["d", Qt.Key_Right]:
QShortcut(shortcut_forward, self).activated.connect(self.forward)
for shortcut_open in ["w", Qt.Key_Up]:
QShortcut(shortcut_open, self).activated.connect(self.open_with_app)
def launch():
app = QApplication(sys.argv)
window = Main(app)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
launch()