-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_preview.py
197 lines (159 loc) · 5.74 KB
/
file_preview.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
# Git-Annex-Metadata-Gui
# Copyright (C) 2017 Alper Nebi Yasak
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import mimetypes
from PyQt5 import Qt
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5 import QtWidgets
logger = logging.getLogger(__name__)
class FilePreview(QtWidgets.QStackedWidget):
def __init__(self, parent=None):
super().__init__(parent)
# These are set by Qt Designer
self.text_preview = None
self.graphics_preview = None
def addWidget(self, widget):
super().addWidget(widget)
if isinstance(widget, QtWidgets.QPlainTextEdit):
self.text_preview = widget
if isinstance(widget, QtWidgets.QGraphicsView):
self.graphics_preview = widget
@QtCore.pyqtSlot()
def clear(self):
if self.text_preview is not None:
self.text_preview.clear()
if self.graphics_preview is not None:
old_scene = self.graphics_preview.scene()
if old_scene:
old_scene.clear()
old_scene.deleteLater()
@QtCore.pyqtSlot(str)
def preview_text_file(self, path):
filename = path.split('/')[-1]
if self.text_preview is None:
msg = "Text preview widget not created yet."
logger.critical(msg)
return
if not self.isVisible():
msg = "Preview widget invisible, not previewing text."
logger.info(msg)
return
self.setCurrentWidget(self.text_preview)
try:
with open(path, 'r') as file:
text = file.read()
except UnicodeDecodeError:
fmt = "File '{}' should be a UTF-8 text file, but isn't."
msg = fmt.format(filename)
logger.error(msg)
return
self.text_preview.setPlainText(text)
fmt = "Previewed file '{}' as plain text."
msg = fmt.format(filename)
logger.info(msg)
@QtCore.pyqtSlot(str)
def preview_image_file(self, path):
filename = path.split('/')[-1]
if self.graphics_preview is None:
msg = "Graphics preview widget not created yet."
logger.critical(msg)
return
if not self.isVisible():
msg = "Preview widget invisible, not previewing image."
logger.info(msg)
return
self.setCurrentWidget(self.graphics_preview)
scene = QtWidgets.QGraphicsScene(self)
self.graphics_preview.setScene(scene)
# Using QImage instead of directly creating the QPixmap
# prevents a segmentation fault in my container setup
image = QtGui.QImage(path)
if image.isNull():
fmt = "File '{}' should be an image, but isn't."
msg = fmt.format(filename)
logger.error(msg)
return
pixmap = QtGui.QPixmap.fromImage(image)
if pixmap.isNull():
fmt = "Failed to generate pixmap from image '{}'."
msg = fmt.format(filename)
logger.critical(msg)
return
pixmap_item = QtWidgets.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmap_item)
self.graphics_preview.fitInView(
pixmap_item,
Qt.Qt.KeepAspectRatio,
)
fmt = "Previewed file '{}' as an image."
msg = fmt.format(filename)
logger.info(msg)
@QtCore.pyqtSlot(QtGui.QStandardItem)
def preview_item(self, item):
self.clear()
if not hasattr(item, 'key'):
return
try:
name = item.name
except AttributeError:
name = None
try:
path = item.contentlocation
except AttributeError:
fmt = "Item '{}' doesn't have a contentlocation property."
msg = fmt.format(repr(item))
logger.critical(msg)
return
if not path:
fmt = "Content for key '{}' is not available."
msg = fmt.format(item.key)
logger.error(msg)
return
mime, encoding = None, None
if name:
mime, encoding = mimetypes.guess_type(name)
if not mime:
mime, encoding = mimetypes.guess_type(path)
if encoding:
fmt = "Can't decode encoding '{}'."
msg = fmt.format(encoding)
logger.error(msg)
return
if not mime:
if hasattr(item, 'name'):
fmt = "Couldn't recognize mimetype for file '{}' ({})."
msg = fmt.format(item.name, item.key)
else:
fmt = "Couldn't recognize mimetype for key '{}'."
msg = fmt.format(item.key)
logger.error(msg)
return
if mime.startswith('text/'):
self.preview_text_file(path)
elif mime.startswith('image/'):
self.preview_image_file(path)
else:
fmt = "Can't preview mimetype '{}'."
msg = fmt.format(mime)
logger.error(msg)
return
def __repr__(self):
return "{name}.{cls}({args})".format(
name=__name__,
cls=self.__class__.__name__,
args='',
)