-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfenrirScreenshotManager.py
376 lines (318 loc) · 12.7 KB
/
fenrirScreenshotManager.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from PyQt5.QtWidgets import (QApplication,
QWidget,
QFileDialog,
QLabel,
QGraphicsDropShadowEffect,
QMessageBox)
from PyQt5.QtGui import QColor, QIcon, QPixmap, QRegion
from PyQt5.QtCore import pyqtSignal, QTimer, Qt, QRect
from PyQt5 import uic
from PIL import Image
from pathlib import Path
from libs.FlowLayout import FlowLayout
from libs.AsyncQtJob import AsyncQtJob
from libs.TitleFrame import TitleFrame
import libs.DiskUtil as disk_util
import io
import shutil
import sys
stylesheet = None
def getStyleSheet():
global stylesheet
if not stylesheet:
with open('ui/style.css', 'r') as file_ptr:
stylesheet = file_ptr.read()
return stylesheet
class CoverLabel(QLabel):
clicked = pyqtSignal()
def __init__(self, cover_url):
super().__init__()
self.cover_url = cover_url
def mousePressEvent(self, event):
self.clicked.emit()
loading_window_ui = 'ui/loading.ui'
loading_form, loading_base = uic.loadUiType(loading_window_ui)
class LoadingWindow(loading_base, loading_form):
def __init__(self, parent, label='Working...'):
super(select_folder_base, self).__init__(parent)
self.setupUi(self)
self.setStyleSheet(getStyleSheet())
self.setWindowFlag(Qt.FramelessWindowHint)
self.progressLabel.setText(label)
def keyPressEvent(self, event):
None
select_folder_window_ui = 'ui/selectFolder.ui'
select_folder_form, select_folder_base = uic.loadUiType(select_folder_window_ui)
class SelectFolderWindow(select_folder_base, select_folder_form):
def __init__(self):
super(select_folder_base, self).__init__()
self.setupUi(self)
self.setStyleSheet(getStyleSheet())
self.setWindowFlag(Qt.FramelessWindowHint)
self.setWindowFlag(Qt.WindowCloseButtonHint, False)
self.setWindowFlag(Qt.WindowContextHelpButtonHint, False)
self.browseButton.clicked.connect(self.browseGamesFolder)
def browseGamesFolder(self):
new_games = QFileDialog.getExistingDirectory()
if new_games:
self.directoryEdit.setText(new_games)
def done(self, ret_code):
path = self.directoryEdit.text()
screenshots = Path(path) / Path('screenshots')
if not screenshots.exists() and ret_code != 0:
msg = 'Invalid SD card path, \'{}\' directory not found. Create it and continue?'
answer = QMessageBox.question(None, 'Screenshots not found',
msg.format(str(screenshots)),
QMessageBox.Yes | QMessageBox.No)
if answer == QMessageBox.Yes:
screenshots.mkdir(parents=True)
super().done(ret_code)
else:
super().done(ret_code)
def exec(self):
super().exec()
return self.directoryEdit.text()
results_window_ui = 'ui/results.ui'
results_form, results_base = uic.loadUiType(results_window_ui)
class ResultsWindow(results_base, results_form):
def __init__(self, game_name):
super(results_base, self).__init__()
self.setupUi(self)
self.setStyleSheet(getStyleSheet())
self.setWindowFlag(Qt.FramelessWindowHint)
self.setEvents()
self.flowLayout = FlowLayout()
self.flowWidget = QWidget()
self.flowWidget.setLayout(self.flowLayout)
self.flowWidget.setAccessibleName('resultsWidget')
self.resultsScrollArea.setWidget(self.flowWidget)
self.searchEdit.setText('Sega Saturn {}'.format(game_name))
self.labels = []
self.active_cover = None
self.search()
def setEvents(self):
self.searchButton.clicked.connect(self.search)
def clearLayout(self):
for i in reversed(range(self.flowLayout.count())):
item = self.flowLayout.takeAt(i)
if item and item.widget():
item.widget().deleteLater()
def resetLabel(self, label):
label.graphicsEffect().setColor(QColor(0, 0, 0))
label.setStyleSheet("border: 1px solid black;")
def unselectLabels(self):
for label in self.labels:
self.resetLabel(label)
def coverClicked(self, label):
self.unselectLabels()
self.active_cover = label.cover_url
label.graphicsEffect().setColor(QColor(255, 0, 0))
label.setStyleSheet("border: 1px solid red;")
def setComponentsState(self, state):
self.searchEdit.setEnabled(state)
self.maxResultsEdit.setEnabled(state)
self.searchButton.setEnabled(state)
self.resultsScrollArea.setEnabled(state)
self.buttonBox.setEnabled(state)
def search(self):
self.active_cover = None
self.labels = []
self.clearLayout()
self.setComponentsState(False)
def query_func():
return disk_util._download_thumbnails(self.searchEdit.text(),
int(self.maxResultsEdit.text()))
self.searchQuery = AsyncQtJob(query_func, self.queryDone)
self.loading = LoadingWindow(self, 'Searching...')
self.loading.exec()
def queryDone(self, query):
self.loading.close()
self.loading = None
if query.done():
self.searchQuery = None
self.displaySearchResults()
def displaySearchResults(self):
self.setComponentsState(True)
items = disk_util.getImagesTempFolder().glob('*.jpg')
for filename in items:
new_label = CoverLabel(filename)
new_label.setPixmap(QPixmap(str(filename)).scaled(256, 192))
new_label.setStyleSheet("border: 3px solid black;")
new_label.clicked.connect(lambda l=new_label: self.coverClicked(l))
shadow_effect = QGraphicsDropShadowEffect()
shadow_effect.setOffset(3, 3)
shadow_effect.setBlurRadius(5)
shadow_effect.setColor(QColor(0, 0, 0))
new_label.setGraphicsEffect(shadow_effect)
self.resetLabel(new_label)
self.flowLayout.addWidget(new_label)
self.labels.append(new_label)
def accept(self):
if self.searchQuery:
return
else:
return super().accept()
def reject(self):
if self.searchQuery:
return
else:
return super().reject()
def done(self, res):
if self.searchQuery:
return
else:
return super().done(res)
def exec(self):
if super().exec():
return self.active_cover
else:
return None
main_window_ui = 'ui/main.ui'
main_form, main_base = uic.loadUiType(main_window_ui)
class MainWindow(main_base, main_form):
def __init__(self):
super(main_base, self).__init__()
self.setupUi(self)
self.setStyleSheet(getStyleSheet())
self.setEvents()
self.verticalLayout.setContentsMargins(10, 10, 10, 10)
self.setWindowFlag(Qt.FramelessWindowHint)
shadow_effect = QGraphicsDropShadowEffect()
shadow_effect.setOffset(2, 2)
shadow_effect.setBlurRadius(5)
shadow_effect.setColor(QColor(26, 88, 175))
self.titleFrame.setWindow(self)
self.titleLabel.setGraphicsEffect(shadow_effect)
self.games_directory = SelectFolderWindow().exec()
if self.games_directory:
self.games_screenshot_directory = self.games_directory / Path('screenshots')
disk_util.games_folder = Path(self.games_directory)
self.copyOriginalScreenshots()
self.configButton.setVisible(False)
self.loading_window = None
self.write_images_job = None
self.showGamesList()
def setComponentsState(self, state):
self.thumbnailDownloadButton.setEnabled(state)
self.thumbnailRemoveButton.setEnabled(state)
self.thumbnailRestoreButton.setEnabled(state)
self.gameList.setEnabled(state)
self.configButton.setEnabled(state)
self.writeImagesButton.setEnabled(state)
self.quitButton.setEnabled(state)
def copyOriginalScreenshots(self):
temp_directory = disk_util.getScreenshotsTempFolder()
if temp_directory.exists():
shutil.rmtree(temp_directory)
temp_directory.mkdir(parents=True)
original_files = self.games_screenshot_directory.glob('*.jpg')
for filename in original_files:
shutil.copyfile(filename, temp_directory / filename.name)
def showGamesList(self):
self.game_list = sorted(disk_util.get_game_list())
self.gameList.clear()
self.gameList.addItems(self.game_list)
def setEvents(self):
self.gameList.itemSelectionChanged.connect(self.gameSelectionChanged)
self.thumbnailDownloadButton.clicked.connect(self.replaceGameCover)
self.thumbnailRestoreButton.clicked.connect(self.restoreGameCover)
self.thumbnailRemoveButton.clicked.connect(self.removeGameCover)
self.writeImagesButton.clicked.connect(self.writeImages)
self.quitButton.clicked.connect(self.close)
def backupScreenshots(self):
backup_folder = self.games_directory / Path('screenshots_backup')
count = 0
while backup_folder.exists():
backup_folder = self.games_directory / Path('screenshots_backup{}'.format(count))
backup_folder.mkdir(parents=True)
original_files = self.games_screenshot_directory.glob('*.jpg')
for filename in original_files:
shutil.copyfile(filename, backup_folder / filename.name)
def writeImagesImpl(self):
source = disk_util.getScreenshotsTempFolder().glob('*.jpg')
destination = self.games_screenshot_directory
for filename in source:
shutil.copyfile(filename, destination / filename.name)
def writeImagesDone(self, job):
self.loading_window.close()
self.loading_window = None
self.write_images_job = None
if job.done():
QMessageBox.information(self, 'Complete', 'Files copied successfully')
self.setComponentsState(True)
def writeImages(self):
msg = ('This action will write the images to your SD card.' +
'Do you want a backup of your \'screenshots\' folder?' +
'If you do, sd:/screenshots_backup will be created')
answer = QMessageBox.question(None, 'Backup first?', msg,
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
if answer == QMessageBox.Cancel:
return
elif answer == QMessageBox.Yes:
def writeAll():
self.backupScreenshots()
self.writeImagesImpl()
self.write_images_job = AsyncQtJob(writeAll, self.writeImagesDone)
elif answer == QMessageBox.No:
self.write_images_job = AsyncQtJob(self.writeImagesImpl, self.writeImagesDone)
self.loading_window = LoadingWindow(self, 'Writing to SD...')
self.loading_window.show()
self.setComponentsState(False)
def setThumbnailToFile(self, filename):
if filename and filename.exists():
self.thumbnailLabel.setPixmap(QPixmap(str(filename)).scaled(256, 192))
else:
self.thumbnailLabel.setPixmap(QPixmap('ui/notfound.png').scaled(256, 192))
def restoreGameCover(self):
selection = self.gameList.selectedItems()
if not selection:
return
game_name = selection[0].text()
source = self.games_screenshot_directory / Path('{}.jpg'.format(game_name))
destination = disk_util.getScreenshotsTempFolder() / Path('{}.jpg'.format(game_name))
if source.exists():
shutil.copy(source, destination)
self.gameSelectionChanged()
def removeGameCover(self):
selection = self.gameList.selectedItems()
if not selection:
return
game_name = selection[0].text()
filename = disk_util.getScreenshotsTempFolder() / Path('{}.jpg'.format(game_name))
if filename.exists():
filename.unlink()
self.gameSelectionChanged()
def replaceGameCover(self):
selection = self.gameList.selectedItems()
if not selection:
return
game_name = selection[0].text()
results = ResultsWindow(disk_util.cleanName(game_name))
selected_cover = results.exec()
if selected_cover:
img = Image.open(selected_cover)
img = img.resize((128, 96))
output_name = disk_util.getScreenshotsTempFolder() / Path('{}.jpg'.format(game_name))
img.save(output_name, 'JPEG', quality=100)
self.setThumbnailToFile(output_name)
def gameSelectionChanged(self):
selection = self.gameList.selectedItems()
self.thumbnailDownloadButton.setEnabled(selection != None)
self.thumbnailRestoreButton.setEnabled(selection != None)
self.thumbnailRemoveButton.setEnabled(selection != None)
if not selection:
return
game_name = selection[0].text()
thumbnail_file = disk_util.get_thumbnail(game_name)
self.setThumbnailToFile(thumbnail_file)
def done(self, res):
if self.loading_window or self.write_images_job:
return
else:
return super().done(res)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
if ex.games_directory:
sys.exit(app.exec_())