-
Notifications
You must be signed in to change notification settings - Fork 1
/
SDIMV.py
463 lines (418 loc) · 19.8 KB
/
SDIMV.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import sys, subprocess, qdarkstyle
from PyQt6.QtWidgets import (
QApplication,
QFrame,
QGraphicsPixmapItem,
QGraphicsScene,
QGraphicsView,
QGridLayout,
QLabel,
QLineEdit,
QMenu,
QToolBar,
QVBoxLayout,
QHBoxLayout,
QWidget,
QPushButton,
QScrollArea,
QDockWidget,
QMessageBox,
)
from PyQt6.QtGui import QIcon, QAction, QFont, QPainter, QMovie, QPixmap, QDesktopServices
from PyQt6.QtCore import Qt, QRectF, QEvent, QUrl, QSettings, QSystemSemaphore, QSharedMemory
from PyQt6.QtMultimedia import QMediaPlayer
from PyQt6.QtMultimediaWidgets import QGraphicsVideoItem
from pathlib import Path
from qframelesswindow import FramelessMainWindow
from image import imageProcess
from file_handler import FileHandler
from custom_widgets import CustomDockWidget, CustomLineEdit, CustomTextEdit, CustomListWidget, CustomTitleBar, ZoomableGraphicsView
from icon import resource_path
from about_dialog import AboutDialog
class MainWindow(FramelessMainWindow):
def __init__(self):
super().__init__()
self.fileHandler = FileHandler(self)
#window size
self.setTitleBar(CustomTitleBar(self))
self.setWindowTitle('SDIMV')
self.titleBar.raise_()
self.settings = QSettings("maagic6", "SDIMV")
savedGeometry = self.settings.value("main_window_geometry")
if savedGeometry is not None:
self.restoreGeometry(savedGeometry)
else:
self.resize(720,720)
qr = self.frameGeometry()
cp = self.screen().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
iconPath = resource_path("icon/icon.ico")
self.setWindowIcon(QIcon(iconPath))
toolbar = QToolBar("Toolbar")
toolbar.setStyleSheet("QToolBar {background: transparent;}"
"QToolButton {background: transparent; border: none;}"
"QToolButton:hover {background: rgba(195, 195, 255, 50);}")
iconPath2 = resource_path("icon/add.png")
iconPath3 = resource_path("icon/remove.png")
iconPath4 = resource_path("icon/clear.png")
iconPath5 = resource_path("icon/about.png")
addAction = QAction(QIcon(iconPath2), "Add", self)
addAction.triggered.connect(self.fileHandler.openFileDialog)
removeAction = QAction(QIcon(iconPath3), "Remove", self)
removeAction.triggered.connect(self.fileHandler.removeSelectedItem)
clearAction = QAction(QIcon(iconPath4), "Clear", self)
clearAction.triggered.connect(self.fileHandler.clearFileList)
aboutAction = QAction(QIcon(iconPath5), "About", self)
aboutAction.triggered.connect(self.showAboutDialog)
toolbar.addAction(addAction)
toolbar.addAction(removeAction)
toolbar.addAction(clearAction)
toolbar.addAction(aboutAction)
toolbar.setObjectName("Toolbar")
self.addToolBar(toolbar)
self.imagePreviewFrame = QFrame()
self.imagePreviewFrame.setFrameShape(QFrame.Shape.Box)
self.imagePreviewFrame.setLineWidth(1)
self.imagePreviewFrame.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.imageFrame = QVBoxLayout()
self.imagePreviewFrame.setLayout(self.imageFrame)
self.imageScene = QGraphicsScene()
self.imageView = ZoomableGraphicsView(self.imageScene)
self.imageView.setRenderHint(QPainter.RenderHint.Antialiasing, True)
self.imageView.setDragMode(QGraphicsView.DragMode.ScrollHandDrag)
self.imageView.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.imageFrame.addWidget(self.imageView)
self.fileList = CustomListWidget()
self.fileList.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.fileList.customContextMenuRequested.connect(self.showContextMenu)
self.fileList.itemSelectionChanged.connect(self.handleItemSelectionChanged)
self.selectedFile = QLineEdit()
self.browseButton = QPushButton('Browse')
self.browseButton.clicked.connect(self.fileHandler.openFileDialog)
self.browseButton.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.clearButton = QPushButton('Clear')
self.clearButton.clicked.connect(self.fileHandler.clearFileList)
self.clearButton.setFocusPolicy(Qt.FocusPolicy.NoFocus)
bottomHalf = QScrollArea(self)
bottomHalf.setWidgetResizable(True)
scrollContent = QWidget()
self.gridLayout = QGridLayout(scrollContent)
bottomHalf.setWidget(scrollContent)
self.gridLayout.addWidget(QLabel('Selected file:'), 3, 0)
self.gridLayout.addWidget(self.selectedFile, 4, 0, 1, 5)
self.widgetInfo = [
('Positive prompt:', CustomTextEdit(), 'prompt'),
('Negative prompt:', CustomTextEdit(), 'negative_prompt'),
('Steps:', CustomLineEdit(), 'steps'),
('Sampler:', CustomLineEdit(), 'sampler'),
('CFG scale:', CustomLineEdit(), 'cfg_scale'),
('Seed:', CustomLineEdit(), 'seed'),
('Size:', CustomLineEdit(), 'size'),
('Model hash:', CustomLineEdit(), 'model_hash'),
('Model:', CustomLineEdit(), 'model'),
('LoRA:', CustomLineEdit(), 'lora'),
('Raw:', CustomTextEdit(), 'raw')
]
for row, (label_text, widget, widget_name) in enumerate(self.widgetInfo):
label = QLabel(label_text)
setattr(self, widget_name + "_label", label)
setattr(self, widget_name, widget)
self.gridLayout.addWidget(label, 2*row+5, 0, 1, 5)
self.gridLayout.addWidget(widget, 2*row+5+1, 0, 1, 5)
# set stretch factors
self.gridLayout.setColumnStretch(0, 1)
self.gridLayout.setColumnStretch(1, 1)
self.gridLayout.setColumnStretch(2, 1)
self.gridLayout.setColumnStretch(3, 1)
self.gridLayout.setColumnStretch(4, 1)
bottomHalf.setMinimumHeight(1)
# set alignments
self.gridLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.gridLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.fileListWidget = CustomDockWidget(self)
self.fileListWidget.setObjectName("FileListWidget")
titleBarWidget = QWidget(self)
titleBarLayout = QHBoxLayout(titleBarWidget)
titleLabel = QLabel("File list")
titleBarLayout.addWidget(titleLabel, alignment=Qt.AlignmentFlag.AlignHCenter)
titleBarLayout.addStretch()
titleBarLayout.addWidget(self.browseButton, alignment=Qt.AlignmentFlag.AlignHCenter)
titleBarLayout.addWidget(self.clearButton, alignment=Qt.AlignmentFlag.AlignHCenter)
titleBarWidget.setMaximumHeight(10)
self.fileListWidget.setWidget(self.fileList)
self.fileListWidget.setWindowTitle("File list")
self.fileList.setAcceptDrops(True)
self.fileListWidget.setTitleBarWidget(titleLabel)
self.fileListWidget.setAcceptDrops(True)
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.fileListWidget)
self.imageViewWidget = QDockWidget()
self.imageViewWidget.setObjectName("ImageViewWidget")
self.imageViewWidget.setWidget(self.imagePreviewFrame)
self.imageViewWidget.setFeatures(QDockWidget.DockWidgetFeature.NoDockWidgetFeatures)
self.imagePreviewFrame.setAcceptDrops(True)
self.imageView.setAcceptDrops(True)
self.imageViewWidget.setTitleBarWidget(QLabel("Image view"))
self.imageViewWidget.setAllowedAreas(Qt.DockWidgetArea.NoDockWidgetArea)
self.imageViewWidget.setAcceptDrops(True)
self.setCentralWidget(self.imageViewWidget)
self.metadataWidget = QDockWidget()
self.metadataWidget.setObjectName("MetadataWidget")
self.metadataWidget.setWidget(bottomHalf)
self.metadataWidget.setTitleBarWidget(QLabel("Metadata"))
self.metadataWidget.setWindowTitle("Metadata")
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.metadataWidget)
self.setContentsMargins(0,30,0,0)
self.isMediaPlayerDeleted = False
self.isMovieDeleted = False
self.fileListWidget.dockLocationChanged.connect(self.updateImageView)
self.metadataWidget.dockLocationChanged.connect(self.updateImageView)
self.fileListWidget.installEventFilter(self)
self.imageViewWidget.installEventFilter(self)
self.metadataWidget.installEventFilter(self)
self.installEventFilter(self)
# load settings
self.loadSettings()
# enable drop events
self.setAcceptDrops(True)
self.show()
if len(sys.argv) > 1:
new_files = []
for arg in sys.argv[1:]:
file_path = Path(arg)
if file_path.is_dir():
new_files.extend(self.fileHandler.getFilesFromFolder(file_path))
elif not self.fileHandler.isFileInList(str(file_path)):
new_files.append(str(file_path).replace('\\', '/'))
self.fileHandler.updateFileList(new_files)
def viewMetadata(self, item):
if item:
selectedFile = item.text()
self.selectedFile.setText(item.text())
if Path(selectedFile).exists():
if selectedFile.lower().endswith(('.gif','.webp')):
self.cleanup()
self.movie = QMovie(selectedFile)
#self.imageScene.clear()
self.pixmap_item = QGraphicsPixmapItem()
self.imageScene.addItem(self.pixmap_item)
self.isMovieDeleted = False
self.imageView.resetTransform()
self.movie.start()
self.movie.frameChanged.connect(lambda: self.pixmap_item.setPixmap(self.movie.currentPixmap()))
self.imageScene.setSceneRect(QRectF(self.movie.currentPixmap().rect()))
self.imageView.setScene(self.imageScene)
self.imageView.fitInView(self.imageScene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
self.imageView.resetZoom()
elif selectedFile.lower().endswith(('.png', '.jpg', '.jpeg','.bmp')):
self.cleanup()
pixmap = QPixmap(selectedFile)
#self.imageScene.clear()
self.imageScene.addPixmap(pixmap)
self.imageView.setScene(self.imageScene)
self.imageView.resetTransform()
self.imageScene.setSceneRect(QRectF(pixmap.rect()))
self.imageView.fitInView(self.imageScene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
self.imageView.resetZoom()
elif selectedFile.lower().endswith(('.mp4', '.mpeg4', '.avi')):
self.cleanup()
#self.imageScene.clear()
self.imageView.resetTransform()
self.media_player = QMediaPlayer()
self.video_item = QGraphicsVideoItem()
self.imageScene.addItem(self.video_item)
self.isMediaPlayerDeleted = False
self.media_player.setVideoOutput(self.video_item)
self.media_player.setSource(QUrl.fromLocalFile(selectedFile))
self.media_player.play()
self.media_player.mediaStatusChanged.connect(self.loopVideo)
self.video_item.nativeSizeChanged.connect(self.updateVideoView)
self.imageView.fitInView(self.imageScene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio) #workaround
self.imageView.resetZoom()
with open(selectedFile, 'rb') as file:
image = imageProcess(file)
prompt = image.positivePrompt()
if prompt == -1:
for _, widget, _ in self.widgetInfo:
widget.setText('')
else:
data = image.getInfo()
for _, widget, key in self.widgetInfo:
if key == 'raw':
widget.setText(str(image.getRaw()))
else:
widget.setText(str(data[key]))
else:
self.cleanup()
#self.imageScene.clear()
self.selectedFile.clear()
for _, widget, _ in self.widgetInfo:
widget.clear()
self.fileHandler.removeSelectedItem()
else:
self.cleanup()
self.imageScene.clear()
self.selectedFile.clear()
for _, widget, _ in self.widgetInfo:
widget.clear()
def loopVideo(self, status):
if status == QMediaPlayer.MediaStatus.EndOfMedia:
self.media_player.setPosition(0)
self.media_player.play()
else:
pass
def cleanup(self):
if hasattr(self, 'movie') and self.movie is not None and self.isMovieDeleted == False:
try:
self.movie.frameChanged.disconnect()
self.movie.stop()
self.imageScene.removeItem(self.pixmap_item)
self.movie.deleteLater()
del self.movie
del self.pixmap_item
self.isMovieDeleted = True
except TypeError as e:
print(f"Exception when disconnecting movie: {e}")
if hasattr(self, 'media_player') and self.media_player is not None and self.isMediaPlayerDeleted == False:
try:
#self.media_player.setSource(QUrl())
self.media_player.mediaStatusChanged.disconnect()
self.media_player.stop()
self.imageScene.removeItem(self.video_item)
self.media_player.deleteLater()
self.video_item.deleteLater()
self.isMediaPlayerDeleted = True
#del self.media_player
#del self.video_item
except Exception as e:
print(f"Exception when disconnecting media player: {e}")
self.imageScene.clear()
def dragEnterEvent(self, event):
mime_data = event.mimeData()
if mime_data.hasUrls():
for url in mime_data.urls():
if url.isLocalFile():
file_path = url.toLocalFile()
if Path(file_path).is_dir() or Path(file_path).suffix.lower() in ['.png', '.gif', '.webp', '.mp4', '.jpg']:
# accept local files
event.acceptProposedAction()
return
elif url.scheme() in ('http', 'https'):
# accept image links
event.acceptProposedAction()
return
def dropEvent(self, event):
mime_data = event.mimeData()
if mime_data.hasUrls():
new_files = []
for url in mime_data.urls():
if url.isLocalFile():
file_path = url.toLocalFile()
if Path(file_path).is_dir():
new_files.extend(self.fileHandler.getFilesFromFolder(file_path))
elif 'Temp' in Path(file_path).parts:
copied_path = self.fileHandler.copyTempImage(file_path)
new_files.append(copied_path)
else:
new_files.append(file_path)
elif url.scheme() == 'http' or url.scheme() == 'https':
downloaded_path = self.fileHandler.downloadImage(url)
if downloaded_path and not self.fileHandler.isFileInList(downloaded_path):
new_files.append(downloaded_path)
new_files = [file_path for file_path in new_files if not self.fileHandler.isFileInList(file_path)]
self.fileHandler.updateFileList(new_files)
event.acceptProposedAction()
def handleItemSelectionChanged(self):
selectedItem = self.fileList.currentItem()
if selectedItem:
self.viewMetadata(selectedItem)
#if 0 <= selectedIndex < len(self.selectedFiles):
#self.viewMetadata(selectedItem)
def updateImageView(self):
self.imageView.fitInView(self.imageScene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
self.imageView.resetZoom()
def updateVideoView(self):
self.imageView.resetTransform()
self.imageScene.setSceneRect(self.video_item.boundingRect())
self.imageView.setScene(self.imageScene)
self.imageView.fitInView(self.imageScene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
self.imageView.resetZoom()
def saveSettings(self):
file_paths = self.fileHandler.getFileList()
self.settings.setValue("selectedFiles", file_paths)
self.settings.setValue("main_window_state", self.saveState())
self.settings.setValue("main_window_geometry", self.saveGeometry())
def loadSettings(self):
file_paths = self.settings.value("selectedFiles", [])
self.fileHandler.updateFileList(file_paths)
if self.settings.value("main_window_state"):
self.restoreState(self.settings.value("main_window_state"))
def closeEvent(self, event):
self.saveSettings()
event.accept()
def eventFilter(self, obj, event):
if obj == self:
if event.type() == QEvent.Type.Resize:
self.updateImageView()
if obj in (self.fileListWidget, self.imageViewWidget):
if event.type() == QEvent.Type.Move:
self.updateImageView()
return super(MainWindow, self).eventFilter(obj, event)
def showContextMenu(self, event):
menu = QMenu(self)
view_action = QAction("View", self)
view_action.triggered.connect(self.openImage)
openfolder_action = QAction("Open folder", self)
openfolder_action.triggered.connect(self.openFolder)
remove_action = QAction("Remove", self)
remove_action.triggered.connect(self.fileHandler.removeSelectedItem)
menu.addAction(view_action)
menu.addAction(openfolder_action)
menu.addAction(remove_action)
menu.exec(self.fileList.mapToGlobal(event))
def openFolder(self):
selectedItem = self.fileList.currentItem()
if selectedItem:
selectedFile = selectedItem.text()
folder_path = Path(selectedFile).parent
QDesktopServices.openUrl(QUrl.fromLocalFile(str(folder_path)))
def openImage(self):
selectedItem = self.fileList.currentItem()
if selectedItem:
selectedFile = selectedItem.text()
subprocess.run(['start', '', selectedFile], shell=True)
def showAboutDialog(self):
self.setEnabled(False)
about_dialog = AboutDialog(self)
about_dialog.setModal(True)
about_dialog.show()
def launch():
app = QApplication(sys.argv)
window_id = 'application'
shared_mem_id = 'sharedmemid'
semaphore = QSystemSemaphore(window_id, 1)
semaphore.acquire()
if sys.platform != 'win32':
nix_fix_shared_mem = QSharedMemory(shared_mem_id)
if nix_fix_shared_mem.attach():
nix_fix_shared_mem.detach()
shared_memory = QSharedMemory(shared_mem_id)
if shared_memory.attach():
is_running = True
else:
shared_memory.create(1)
is_running = False
semaphore.release()
if is_running:
QMessageBox.warning(None, 'Application already running', 'One instance of the application is already running.')
return
app.setStyleSheet(qdarkstyle.load_stylesheet())
iconPath = resource_path("icon/icon.ico")
app.setWindowIcon(QIcon(iconPath))
font = QFont("Segoe UI", 10)
app.setFont(font)
window = MainWindow()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
launch()